|
| 1 | +@file:Suppress("PackageDirectoryMismatch") |
| 2 | + |
| 3 | +package com.faforever |
| 4 | + |
| 5 | +import java.io.File |
| 6 | +import kotlin.system.exitProcess |
| 7 | + |
| 8 | +/** |
| 9 | + * Runs [fixScmapPaths] over every map file in a checkout of the coop missions and fails if |
| 10 | + * anything is off. Meant for CI, so a change to the path rewriting cannot quietly break a |
| 11 | + * map file. |
| 12 | + * |
| 13 | + * The important one is the identity check: rewriting with a negative version has to |
| 14 | + * reproduce the input byte for byte. It covers every mission, not only the handful that |
| 15 | + * need the fix, and therefore also the old file formats. Scoping a section to the wrong |
| 16 | + * file version breaks exactly those and nothing else. |
| 17 | + * |
| 18 | + * Point MAPS_REPO at a checkout of https://github.com/FAForever/faf-coop-maps. |
| 19 | + */ |
| 20 | +fun main() { |
| 21 | + val repo = File(System.getenv("MAPS_REPO") ?: "/tmp/faf-coop-maps") |
| 22 | + require(repo.isDirectory) { "MAPS_REPO does not point at a directory: $repo" } |
| 23 | + |
| 24 | + val maps = repo.walkTopDown() |
| 25 | + .filter { it.isFile && it.name.lowercase().endsWith(".scmap") } |
| 26 | + .sortedBy { it.path } |
| 27 | + .toList() |
| 28 | + require(maps.isNotEmpty()) { "no .scmap files found below $repo" } |
| 29 | + |
| 30 | + val failures = mutableListOf<String>() |
| 31 | + var placeholders = 0 |
| 32 | + var identical = 0 |
| 33 | + var rewrittenMaps = 0 |
| 34 | + |
| 35 | + println("%-44s %-8s %-10s %s".format("mission", "format", "size", "result")) |
| 36 | + |
| 37 | + maps.forEach { file -> |
| 38 | + val folder = file.parentFile.name |
| 39 | + val bytes = file.readBytes() |
| 40 | + |
| 41 | + if (!bytes.isScmap()) { |
| 42 | + placeholders++ |
| 43 | + println("%-44s %-8s %-10s %s".format(folder, "-", bytes.size, "not a map file, skipped")) |
| 44 | + return@forEach |
| 45 | + } |
| 46 | + |
| 47 | + val notes = mutableListOf<String>() |
| 48 | + |
| 49 | + try { |
| 50 | + val copy = fixScmapPaths(bytes, folder, -1).bytes |
| 51 | + if (copy.contentEquals(bytes)) { |
| 52 | + identical++ |
| 53 | + notes += "identical" |
| 54 | + } else { |
| 55 | + failures += "$folder: copy is ${copy.size} bytes, input is ${bytes.size}" |
| 56 | + notes += "NOT IDENTICAL" |
| 57 | + } |
| 58 | + } catch (e: Exception) { |
| 59 | + failures += "$folder: ${e.message}" |
| 60 | + notes += "FAILED: ${e.message}" |
| 61 | + } |
| 62 | + |
| 63 | + if (bytes.referencesOwnMapFolder(folder)) { |
| 64 | + try { |
| 65 | + // delta accounting and the round trip are asserted inside fixScmapPaths |
| 66 | + val fix = fixScmapPaths(bytes, folder, 4) |
| 67 | + rewrittenMaps++ |
| 68 | + notes += "${fix.rewritten.size} path(s) rewritten" |
| 69 | + |
| 70 | + fix.rewritten.filterNot { it.contains(".v0004/") } |
| 71 | + .forEach { failures += "$folder: not versioned: $it" } |
| 72 | + fix.rewritten.filter { it != it.lowercase() } |
| 73 | + .forEach { failures += "$folder: not lower cased: $it" } |
| 74 | + fix.rewritten.filter { DOUBLE_VERSION.containsMatchIn(it) } |
| 75 | + .forEach { failures += "$folder: version added twice: $it" } |
| 76 | + } catch (e: Exception) { |
| 77 | + failures += "$folder: ${e.message}" |
| 78 | + notes += "FAILED: ${e.message}" |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + println("%-44s %-8s %-10s %s".format(folder, "v" + formatVersion(bytes), bytes.size, notes.joinToString(", "))) |
| 83 | + } |
| 84 | + |
| 85 | + println() |
| 86 | + println("$identical of ${maps.size - placeholders} map file(s) reproduced byte for byte") |
| 87 | + println("$rewrittenMaps map file(s) reference their own folder and were rewritten") |
| 88 | + println("$placeholders placeholder file(s) skipped") |
| 89 | + |
| 90 | + if (failures.isNotEmpty()) { |
| 91 | + println() |
| 92 | + failures.forEach { println("FAIL $it") } |
| 93 | + println("${failures.size} failure(s)") |
| 94 | + exitProcess(1) |
| 95 | + } |
| 96 | + println("all good") |
| 97 | +} |
| 98 | + |
| 99 | +private val DOUBLE_VERSION = Regex("""\.v\d{4}\.v\d{4}""") |
| 100 | + |
| 101 | +/** Map file format version, stored behind the preview image. */ |
| 102 | +private fun formatVersion(bytes: ByteArray): Int { |
| 103 | + fun int(at: Int) = (bytes[at].toInt() and 0xFF) or |
| 104 | + ((bytes[at + 1].toInt() and 0xFF) shl 8) or |
| 105 | + ((bytes[at + 2].toInt() and 0xFF) shl 16) or |
| 106 | + ((bytes[at + 3].toInt() and 0xFF) shl 24) |
| 107 | + return int(34 + int(30)) |
| 108 | +} |
0 commit comments