-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCoopDeployer.kt
More file actions
executable file
·456 lines (387 loc) · 16.4 KB
/
CoopDeployer.kt
File metadata and controls
executable file
·456 lines (387 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
@file:Suppress("PackageDirectoryMismatch")
package com.faforever.coopdeployer
import com.faforever.CHECKSUMS_FILENAME
import com.faforever.FafDatabase
import com.faforever.GitRepo
import com.faforever.Log
import com.faforever.extractChecksumsFromZip
import com.faforever.generateChecksums
import com.faforever.md5
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
import org.slf4j.LoggerFactory
import java.io.IOException
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import java.nio.file.attribute.PosixFilePermission
import java.time.Duration
private val log = LoggerFactory.getLogger("CoopDeployer")
fun Path.setPerm664() {
val perms = mutableSetOf<PosixFilePermission>().apply {
add(PosixFilePermission.OWNER_READ); add(PosixFilePermission.OWNER_WRITE)
add(PosixFilePermission.GROUP_READ); add(PosixFilePermission.GROUP_WRITE)
add(PosixFilePermission.OTHERS_READ)
}
Files.setPosixFilePermissions(this, perms)
}
data class GithubReleaseAssetDownloader(
val repoOwner: String = "FAForever",
val repoName: String,
val suffix: String,
val version: String,
val dryRun: Boolean,
) {
companion object {
private const val API_URL = "https://api.github.com/repos/%s/%s/releases/tags/v%s"
private val DOWNLOAD_URL_REGEX = Regex(""""browser_download_url"\s*:\s*"(?<url>[^"]+)"""")
}
private val httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.followRedirects(HttpClient.Redirect.NORMAL)
.build()
fun downloadAssets(targetDir: Path): List<Path> {
log.info("Downloading assets for version $version from GitHub releases...")
val tempDir = Paths.get("/tmp", "asset_download_$version")
val urls = getAssetUrisBySuffix()
if (urls.isEmpty()) {
log.info("No .nx2 assets found in release v{}", version)
return emptyList()
}
val downloaded = mutableListOf<Path>()
Files.createDirectories(tempDir)
for (u in urls) {
val filename = Paths.get(u.toURL().path).fileName.toString()
val dst = tempDir.resolve(filename)
downloadFile(u, dst)
// rename to include .v{version}.nx2 before .nx2
val newName = filename.replace(Regex("\\.nx2$"), ".v$version.nx2")
val newPath = tempDir.resolve(newName)
Files.move(dst, newPath, StandardCopyOption.REPLACE_EXISTING)
downloaded.add(newPath)
}
// copy to target updates dir
val outDir = targetDir.resolve("updates_coop_files")
Files.createDirectories(outDir)
for (p in downloaded) {
val dest = outDir.resolve(p.fileName.toString())
log.info("Copying VO {} -> {}", p, dest)
if (!dryRun) {
Files.copy(p, dest, StandardCopyOption.REPLACE_EXISTING)
dest.setPerm664()
} else {
log.info("[DRYRUN] Would copy {} -> {}", p, dest)
}
}
return downloaded.map { outDir.resolve(it.fileName) }
}
private fun getAssetUrisBySuffix(): List<URI> {
val request = HttpRequest.newBuilder()
.uri(URI.create(API_URL.format(repoOwner, repoName, version)))
.timeout(Duration.ofSeconds(10))
.header("Accept", "application/vnd.github.v3+json")
.GET()
.build()
val apiResponse = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body()
return DOWNLOAD_URL_REGEX.findAll(apiResponse)
.mapNotNull { it.groups["url"]?.value }
.filter { it.endsWith(suffix, ignoreCase = true) }
.map { URI.create(it) }
.toList()
}
private fun downloadFile(source: URI, dest: Path) {
log.debug("Downloading {} -> {}", source, dest)
val request = HttpRequest.newBuilder()
.uri(source)
.GET()
.build()
Files.createDirectories(dest.parent)
val response = httpClient.send(
request,
HttpResponse.BodyHandlers.ofFile(dest)
)
if (response.statusCode() !in 200..299) {
Files.deleteIfExists(dest)
throw IOException("Failed to download $source -> HTTP ${response.statusCode()}")
}
}
}
data class CoopDatabase(
val dryRun: Boolean
) : FafDatabase() {
/**
* Definition of an existing file in the database
*/
data class PatchFile(val mod: String, val fileId: Int, val name: String, val md5: String, val version: Int)
fun getCurrentPatchFile(mod: String, fileId: Int): PatchFile? {
val sql = """
SELECT uf.fileId, uf.name, uf.md5, t.v
FROM (
SELECT fileId, MAX(version) AS v
FROM updates_${mod}_files
GROUP BY fileId
) t
JOIN updates_${mod}_files uf ON uf.fileId = t.fileId AND uf.version = t.v
WHERE uf.fileId = ?
""".trimIndent()
prepareStatement(sql).use { stmt ->
stmt.setInt(1, fileId)
val rs = stmt.executeQuery()
while (rs.next()) {
val fileId = rs.getInt(1)
val name = rs.getString(2)
val md5 = rs.getString(3)
val version = rs.getInt(4)
return PatchFile(mod = mod, fileId = fileId, name = name, md5 = md5, version = version)
}
}
return null
}
fun insertOrReplace(mod: String, fileId: Int, version: Int, name: String, md5: String) {
log.info("Updating DB: {} (fileId={}, version={}) md5={}", name, fileId, version, md5)
if (dryRun) {
log.info("[DRYRUN] DB: would delete/insert for {},{}", fileId, version)
return
}
val del = "DELETE FROM updates_${mod}_files WHERE fileId=? AND version=?"
val ins = "INSERT INTO updates_${mod}_files (fileId, version, name, md5, obselete) VALUES (?, ?, ?, ?, 0)"
prepareStatement(del).use {
it.setInt(1, fileId)
it.setInt(2, version)
it.executeUpdate()
}
prepareStatement(ins).use {
it.setInt(1, fileId)
it.setInt(2, version)
it.setString(3, name)
it.setString(4, md5)
it.executeUpdate()
}
}
}
class Patcher(
val patchVersion: Int,
val targetDir: Path,
val db: CoopDatabase,
val dryRun: Boolean,
) {
/**
* Definition of a file to create a patch for
*/
data class PatchFile(val id: Int, val fileTemplate: String, val includes: List<Path>?, val mod: String = "coop")
fun process(patchFile: PatchFile) {
val mod: String = patchFile.mod
val fileId: Int = patchFile.id
val name = patchFile.fileTemplate.format(patchVersion)
val outDir = targetDir.resolve("updates_${mod}_files")
Files.createDirectories(outDir)
val target = outDir.resolve(name)
log.info("Processing {} (fileId {})", name, fileId)
if (patchFile.includes == null) {
// VO file: look for downloaded file in target dir (name formatted)
val expectedName = name // matches nameFmt.format(version)
val candidate = outDir.resolve(expectedName)
if (!Files.exists(candidate)) {
log.debug("VO file {} not found in {}, skipping", expectedName, outDir)
return
}
val newMd5 = candidate.md5()
val oldFile = db.getCurrentPatchFile(mod, fileId)
if (newMd5 != oldFile?.md5) {
db.insertOrReplace(mod, fileId, patchVersion, expectedName, newMd5)
} else {
log.info("VO {} unchanged from version {}", expectedName, oldFile.version)
}
return
}
// sources present -> create zip or copy single file
val existing = patchFile.includes.filter(Files::exists)
if (existing.isEmpty()) {
log.info("Warning: no existing sources for {}, skipping", name)
return
}
// if single source and it's a file, copy it directly (like init_coop.lua)
if (existing.size == 1 && Files.isRegularFile(existing[0])) {
val src = existing[0]
log.info("Single file source for {}: copying {} -> {}", name, src, target)
val newMd5 = src.md5()
val oldFile = db.getCurrentPatchFile(mod, fileId)
if (newMd5 == oldFile?.md5) {
log.info("{} unchanged from version {}, skipping", name, oldFile.version)
return
}
if (!dryRun) {
Files.copy(src, target, StandardCopyOption.REPLACE_EXISTING)
target.setPerm664()
db.insertOrReplace(mod, fileId, patchVersion, name, newMd5)
} else {
log.info("[DRYRUN] Would copy {} -> {}", src, target)
}
return
}
// multiple sources -> zip them; determine base as common parent of the directories (so top-level folders like 'mods'/'units' remain)
// compute base as common path of all existing sources
var base = existing[0].toAbsolutePath().normalize()
for (i in 1 until existing.size) {
base = base.commonPath(existing[i].toAbsolutePath().normalize())
}
val tmp = Files.createTempFile("coop", ".zip")
log.info("Zipping sources with base={} -> {}", base, tmp)
val newChecksums = zipPreserveStructure(existing, tmp, base)
// Compare checksums.md5 content with existing ZIP (if any)
val oldFile = db.getCurrentPatchFile(mod, fileId)
val existingZip = oldFile?.let { outDir.resolve(it.name) }
val oldChecksums = existingZip?.let { extractChecksumsFromZip(it) }
if (newChecksums == oldChecksums) {
log.info("{} unchanged from version {}, skipping", name, oldFile.version)
Files.deleteIfExists(tmp)
return
}
// ZIP content changed - compute MD5 for database record
val newMd5 = tmp.md5()
if (!dryRun) {
log.info("Moving zip to {}", target)
Files.move(tmp, target, StandardCopyOption.REPLACE_EXISTING)
target.setPerm664()
log.info("Writing fileId {} with version {} to database", fileId, patchVersion)
db.insertOrReplace(mod, fileId, patchVersion, name, newMd5)
} else {
log.info("[DRYRUN] Would move {} -> {}", tmp, target)
Files.deleteIfExists(tmp)
}
}
private fun Path.commonPath(other: Path): Path {
val a = toAbsolutePath().normalize()
val b = other.toAbsolutePath().normalize()
val commonCount = (0 until minOf(a.nameCount, b.nameCount))
.takeWhile { a.getName(it) == b.getName(it) }
.count()
return if (commonCount == 0) a.root
else a.root.resolve(a.subpath(0, commonCount))
}
/**
* Create a ZIP archive with checksums.md5 embedded for content-based change detection.
* Returns the checksums.md5 content for comparison purposes.
*/
private fun zipPreserveStructure(sources: List<Path>, outputFile: Path, base: Path): String {
Files.createDirectories(outputFile.parent)
// First, collect all files to include
val allFiles = mutableListOf<Path>()
for (src in sources) {
if (!Files.exists(src)) {
log.warn("Could not find path {}", src)
continue
}
if (Files.isDirectory(src)) {
Files.walk(src).use { stream ->
stream.filter { Files.isRegularFile(it) }.forEach { allFiles.add(it) }
}
} else {
allFiles.add(src)
}
}
// Sort files for deterministic ordering
allFiles.sortBy { base.relativize(it).toString() }
// Generate checksums.md5 content
val checksums = generateChecksums(allFiles, base)
// Never pass a stream here; this will cause extended local headers to be used, making it incompatible to FA!
ZipArchiveOutputStream(outputFile.toFile()).use { zos ->
zos.setMethod(ZipArchiveEntry.DEFLATED)
// Write checksums.md5 as first entry
val checksumBytes = checksums.toByteArray()
val checksumEntry = ZipArchiveEntry(CHECKSUMS_FILENAME).apply {
size = checksumBytes.size.toLong()
}
zos.putArchiveEntry(checksumEntry)
zos.write(checksumBytes)
zos.closeArchiveEntry()
// Write all files
for (path in allFiles) {
zos.pushNormalizedFile(base, path)
}
}
return checksums
}
private fun ZipArchiveOutputStream.pushNormalizedFile(base: Path, path: Path) {
require(Files.isRegularFile(path)) { "Path $path is not a regular file" }
val archiveName = base.relativize(path).toString().replace("\\", "/")
// Use the same constructor as the FAF API:
val entry = ZipArchiveEntry(path.toFile(), archiveName)
this.putArchiveEntry(entry)
Files.newInputStream(path).use { inp -> inp.copyTo(this) }
this.closeArchiveEntry()
}
}
fun main() {
Log.init()
val PATCH_VERSION = System.getenv("PATCH_VERSION") ?: error("PATCH_VERSION required")
val REPO_URL = System.getenv("GIT_REPO_URL") ?: "https://github.com/FAForever/fa-coop.git"
val GIT_REF = System.getenv("GIT_REF") ?: "v$PATCH_VERSION"
val WORKDIR = System.getenv("GIT_WORKDIR") ?: "/tmp/fa-coop"
val DRYRUN = (System.getenv("DRY_RUN") ?: "false").lowercase() in listOf("1", "true", "yes")
val TARGET_DIR = Paths.get("./legacy-featured-mod-files")
log.info("=== Kotlin Coop Deployer v{} ===", PATCH_VERSION)
val repo = GitRepo(
workDir = Paths.get(WORKDIR),
repoUrl = REPO_URL,
gitRef = GIT_REF
).checkout()
// Download VO assets first
GithubReleaseAssetDownloader(
repoName = "fa-coop",
suffix = ".nx2",
version = PATCH_VERSION,
dryRun = DRYRUN
).downloadAssets(TARGET_DIR)
val filesList = listOf(
Patcher.PatchFile(1, "init_coop.v%d.lua", listOf(repo.resolve("init_coop.lua"))),
Patcher.PatchFile(
2, "lobby_coop_v%d.cop", listOf(
repo.resolve("mods"),
repo.resolve("units"),
repo.resolve("mod_info.lua"),
repo.resolve("readme.md"),
repo.resolve("changelog.md")
)
),
// all VO files (no sources → already downloaded externally)
Patcher.PatchFile(3, "A01_VO.v%d.nx2", null),
Patcher.PatchFile(4, "A02_VO.v%d.nx2", null),
Patcher.PatchFile(5, "A03_VO.v%d.nx2", null),
Patcher.PatchFile(6, "A04_VO.v%d.nx2", null),
Patcher.PatchFile(7, "A05_VO.v%d.nx2", null),
Patcher.PatchFile(8, "A06_VO.v%d.nx2", null),
Patcher.PatchFile(9, "C01_VO.v%d.nx2", null),
Patcher.PatchFile(10, "C02_VO.v%d.nx2", null),
Patcher.PatchFile(11, "C03_VO.v%d.nx2", null),
Patcher.PatchFile(12, "C04_VO.v%d.nx2", null),
Patcher.PatchFile(13, "C05_VO.v%d.nx2", null),
Patcher.PatchFile(14, "C06_VO.v%d.nx2", null),
Patcher.PatchFile(15, "E01_VO.v%d.nx2", null),
Patcher.PatchFile(16, "E02_VO.v%d.nx2", null),
Patcher.PatchFile(17, "E03_VO.v%d.nx2", null),
Patcher.PatchFile(18, "E04_VO.v%d.nx2", null),
Patcher.PatchFile(19, "E05_VO.v%d.nx2", null),
Patcher.PatchFile(20, "E06_VO.v%d.nx2", null),
Patcher.PatchFile(21, "Prothyon16_VO.v%d.nx2", null),
Patcher.PatchFile(22, "TCR_VO.v%d.nx2", null),
Patcher.PatchFile(23, "SCCA_Briefings.v%d.nx2", null),
Patcher.PatchFile(24, "SCCA_FMV.v%d.nx2", null),
Patcher.PatchFile(25, "FAF_Coop_Operation_Tight_Spot_VO.v%d.nx2", null),
)
CoopDatabase(dryRun = DRYRUN).use { db ->
val patcher = Patcher(
patchVersion = PATCH_VERSION.toInt(),
targetDir = TARGET_DIR,
db = db,
dryRun = DRYRUN,
)
filesList.forEach(patcher::process)
}
log.info("=== Deployment complete ===")
}