@@ -187,7 +187,7 @@ fun downloadVoAssets(version: String, targetDir: Path): List<Path> {
187187 val filename = Paths .get(URI .create(u).toURL().path).fileName.toString()
188188 val dst = VO_DOWNLOAD_TMP .resolve(filename)
189189 try {
190- log.info(" Downloading {} -> {}" ,u, dst)
190+ log.info(" Downloading {} -> {}" , u, dst)
191191 downloadFile(u, dst)
192192 // rename to include .v{version}.nx2 before .nx2
193193 val newName = filename.replace(Regex (" \\ .nx2$" ), " .v$version .nx2" )
@@ -225,21 +225,27 @@ fun downloadVoAssets(version: String, targetDir: Path): List<Path> {
225225fun dbConnection () =
226226 DriverManager .getConnection(" jdbc:mariadb://$DB_HOST /$DB_NAME ?useSSL=false&serverTimezone=UTC" , DB_USER , DB_PASS )
227227
228- fun readExisting (conn : java.sql.Connection , mod : String ): Map <Int , Pair <String ?, String ?>> {
228+ data class ExistingFile (val fileId : Int , val name : String , val md5 : String , val version : Int )
229+
230+ fun readExisting (conn : java.sql.Connection , mod : String ): Map <Int , ExistingFile > {
229231 val sql = """
230- SELECT uf.fileId, uf.name, uf.md5
232+ SELECT uf.fileId, uf.name, uf.md5, t.v
231233 FROM (
232234 SELECT fileId, MAX(version) AS v
233235 FROM updates_${mod} _files
234236 GROUP BY fileId
235237 ) t
236238 JOIN updates_${mod} _files uf ON uf.fileId = t.fileId AND uf.version = t.v
237239 """ .trimIndent()
238- val out = mutableMapOf<Int , Pair < String ?, String ?> >()
240+ val out = mutableMapOf<Int , ExistingFile >()
239241 conn.prepareStatement(sql).use { stmt ->
240242 val rs = stmt.executeQuery()
241243 while (rs.next()) {
242- out [rs.getInt(1 )] = rs.getString(2 ) to rs.getString(3 )
244+ val fileId = rs.getInt(1 )
245+ val name = rs.getString(2 )
246+ val md5 = rs.getString(3 )
247+ val version = rs.getInt(4 )
248+ out [fileId] = ExistingFile (fileId = fileId, name = name, md5 = md5, version = version)
243249 }
244250 }
245251 return out
@@ -275,8 +281,7 @@ fun processItem(
275281 version : Int ,
276282 fileId : Int ,
277283 nameFmt : String ,
278- sources : List <Path >? ,
279- voDownloadedMap : Map <String , Path >
284+ sources : List <Path >?
280285) {
281286 val name = nameFmt.format(version)
282287 val outDir = TARGET_DIR .resolve(" updates_${mod} _files" )
@@ -294,17 +299,17 @@ fun processItem(
294299 return
295300 }
296301 val newMd5 = md5(candidate)
297- val oldMd5 = readExisting(conn, mod)[fileId]?.second
298- if (newMd5 != oldMd5 ) {
302+ val oldFile = readExisting(conn, mod)[fileId]
303+ if (newMd5 != oldFile?.md5 ) {
299304 updateDb(conn, mod, fileId, version, expectedName, newMd5)
300305 } else {
301- log.info(" VO {} unchanged" , expectedName)
306+ log.info(" VO {} unchanged from version {} " , expectedName, oldFile.version )
302307 }
303308 return
304309 }
305310
306311 // sources present -> create zip or copy single file
307- val existing = sources.filter { p -> Files . exists(p) }
312+ val existing = sources.filter( Files :: exists)
308313 if (existing.isEmpty()) {
309314 log.info(" Warning: no existing sources for {}, skipping" , name)
310315 return
@@ -314,20 +319,21 @@ fun processItem(
314319 if (existing.size == 1 && Files .isRegularFile(existing[0 ])) {
315320 val src = existing[0 ]
316321 log.info(" Single file source for {}: copying {} -> {}" , name, src, target)
322+
323+ val newMd5 = md5(src)
324+ val oldFile = readExisting(conn, mod)[fileId]
325+ if (newMd5 == oldFile?.md5) {
326+ log.info(" {} unchanged from version {}, skipping" , name, oldFile.version)
327+ return
328+ }
329+
317330 if (! DRYRUN ) {
318331 Files .copy(src, target, StandardCopyOption .REPLACE_EXISTING )
319332 setPerm664(target)
320- } else {
321- log.info(" [DRYRUN] Would copy {} -> {}" , src, target)
322- }
323- val newMd5 = md5(target)
324- val oldMd5 = readExisting(conn, mod)[fileId]?.second
325- if (newMd5 != oldMd5) {
326333 updateDb(conn, mod, fileId, version, name, newMd5)
327334 } else {
328- log.info(" {} unchanged " , name )
335+ log.info(" [DRYRUN] Would copy {} -> {} " , src, target )
329336 }
330- return
331337 }
332338
333339 // multiple sources -> zip them; determine base as common parent of the directories (so top-level folders like 'mods'/'units' remain)
@@ -340,20 +346,24 @@ fun processItem(
340346 val tmp = Files .createTempFile(" coop" , " .zip" )
341347 log.info(" Zipping sources with base={} -> {}" , base, tmp)
342348 zipPreserveStructure(existing, tmp, base)
343- log.info(" Moving zip to {}" , target)
349+
350+ val newMd5 = md5(tmp)
351+ val oldFile = readExisting(conn, mod)[fileId]
352+
353+ if (newMd5 == oldFile?.md5) {
354+ log.info(" {} unchanged from version {}, skipping" , name, oldFile.version)
355+ return
356+ }
357+
344358 if (! DRYRUN ) {
359+ log.info(" Moving zip to {}" , target)
345360 Files .move(tmp, target, StandardCopyOption .REPLACE_EXISTING )
346361 setPerm664(target)
347- } else {
348- log.info(" [DRYRUN] Would move {} -> {}" , tmp, target)
349- }
350362
351- val newMd5 = md5(target)
352- val oldMd5 = readExisting(conn, mod)[fileId]?.second
353- if (newMd5 != oldMd5) {
363+ log.info(" Writing fileId {} with version {} to database" , fileId, version)
354364 updateDb(conn, mod, fileId, version, name, newMd5)
355365 } else {
356- log.info(" {} unchanged " , name )
366+ log.info(" [DRYRUN] Would move {} -> {} " , tmp, target )
357367 }
358368}
359369
@@ -413,7 +423,7 @@ fun main() {
413423 )
414424
415425 for ((fileId, fmt, srcs) in filesList) {
416- processItem(conn, " coop" , PATCH_VERSION .toInt(), fileId, fmt, srcs?.map { it }, voMap )
426+ processItem(conn, " coop" , PATCH_VERSION .toInt(), fileId, fmt, srcs?.map { it })
417427 }
418428 } finally {
419429 conn.close()
0 commit comments