|
| 1 | +/* |
| 2 | + * Copyright © 2022 antD97 |
| 3 | + * Licensed under the MIT License https://antD.mit-license.org/ |
| 4 | + */ |
| 5 | +package shorthandcommands |
| 6 | + |
| 7 | +import java.io.File |
| 8 | +import java.io.IOException |
| 9 | +import java.io.StringReader |
| 10 | +import java.util.* |
| 11 | +import kotlin.system.exitProcess |
| 12 | + |
| 13 | +fun main() { |
| 14 | + |
| 15 | + // load from properties file |
| 16 | + val properties = Properties() |
| 17 | + // replace \ with \\ before loading the configuration file |
| 18 | + File("shorthand.conf").readText() |
| 19 | + .replace("\\", "\\\\") |
| 20 | + .let { StringReader(it).use { sr -> properties.load(sr) } } |
| 21 | + |
| 22 | + val projDir = File(properties.getProperty("project") ?: badConfig("project")) |
| 23 | + |
| 24 | + // project directory check |
| 25 | + if (!projDir.isDirectory) { |
| 26 | + println("Project directory `${projDir.path}` from `shorthand.conf` is not a directory." + |
| 27 | + "\nExiting...") |
| 28 | + exitProcess(-1) |
| 29 | + } |
| 30 | + |
| 31 | + val buildDir = File(properties.getProperty("build") ?: badConfig("build")) |
| 32 | + |
| 33 | + // build directory check |
| 34 | + if (!buildDir.isDirectory) { |
| 35 | + println("Build directory `${buildDir.path}` from `shorthand.conf` is not a directory." + |
| 36 | + "\nExiting...") |
| 37 | + exitProcess(-1) |
| 38 | + } |
| 39 | + |
| 40 | + val transformedProjDir = buildDir.resolve(projDir.name) |
| 41 | + |
| 42 | + // transformed project directory check |
| 43 | + if (transformedProjDir.isFile) { |
| 44 | + println("The save location for the converted project `${transformedProjDir.path}` is a " + |
| 45 | + "file and will not be overwritten. Please move or delete this file and run the " + |
| 46 | + "tool again." + |
| 47 | + "\nExiting...") |
| 48 | + exitProcess(-1) |
| 49 | + } |
| 50 | + |
| 51 | + // transformed project directory = build directory check |
| 52 | + if (projDir.canonicalFile == transformedProjDir.canonicalFile) { |
| 53 | + println("The save location for the converted project is the same as the project " + |
| 54 | + "directory `${projDir.path}`. Please change the build location in " + |
| 55 | + "`shorthand.conf` file and run the tool again." + |
| 56 | + "\nExiting...") |
| 57 | + exitProcess(-1) |
| 58 | + } |
| 59 | + |
| 60 | + // data directory & `pack.mcmeta` check |
| 61 | + val projDataDir = projDir.listFiles().find { it.name == "data" && it.isDirectory } |
| 62 | + val projPackFile = projDir.listFiles().find { it.name == "pack.mcmeta" && it.isFile } |
| 63 | + if (projDataDir == null || projPackFile == null) { |
| 64 | + println("The project directory `${projDir.path}` needs to contain a data directory and " + |
| 65 | + "`pack.mcmeta` file." + |
| 66 | + "\nExiting...") |
| 67 | + exitProcess(-1) |
| 68 | + } |
| 69 | + |
| 70 | + var headerFile: File? = File("header.txt") |
| 71 | + |
| 72 | + // header file check |
| 73 | + if (!headerFile!!.isFile) { |
| 74 | + println("Could not locate a `header.txt` file. No header will be added for newly created " + |
| 75 | + ".mcfunction files.") |
| 76 | + headerFile = null |
| 77 | + } |
| 78 | + |
| 79 | + // delete old transformed project directory |
| 80 | + if (transformedProjDir.exists()) { |
| 81 | + |
| 82 | + val transformedProjPackFile = transformedProjDir.listFiles() |
| 83 | + .find { it.name == "pack.mcmeta" && it.isFile } |
| 84 | + |
| 85 | + // directory to overwrite is not a datapack |
| 86 | + if (transformedProjDir.listFiles().none { it.name == "data" && it.isDirectory } |
| 87 | + || transformedProjPackFile == null) { |
| 88 | + println("The save location for the converted project `${transformedProjDir.path}` is " + |
| 89 | + "not a datapack and will not be overwritten. Please move or delete this " + |
| 90 | + "directory and run the tool again." + |
| 91 | + "\nExiting...") |
| 92 | + exitProcess(-1) |
| 93 | + } |
| 94 | + |
| 95 | + // directory to overwrite does not use the same `pack.mcmeta` file |
| 96 | + else if (projPackFile.readText() != transformedProjPackFile.readText()) { |
| 97 | + println("The save location for the converted project `${transformedProjDir.path}` " + |
| 98 | + "has a different `pack.mcmeta` file than the source project " + |
| 99 | + "`${projDir.path}` and will not be overwritten. Please move or delete this " + |
| 100 | + "project directory and run the tool again." + |
| 101 | + "\nExiting...") |
| 102 | + exitProcess(-1) |
| 103 | + } |
| 104 | + |
| 105 | + // directory to overwrite is the same datapack and is safe to overwrite |
| 106 | + else { |
| 107 | + print("Okay to delete the previously converted project " + |
| 108 | + "`${transformedProjDir.path}`? (y/n): ") |
| 109 | + if (readLine()!!.lowercase() in listOf("y", "yes")) { |
| 110 | + |
| 111 | + println("Deleting `$transformedProjDir`...") |
| 112 | + if (!transformedProjDir.deleteRecursively()) { |
| 113 | + println("Failed to delete `${transformedProjDir.path}`\nExiting...") |
| 114 | + exitProcess(-1) |
| 115 | + } |
| 116 | + } else { |
| 117 | + println("Exiting...") |
| 118 | + exitProcess(-1) |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // find each functions directory |
| 124 | + val projFunctionsDirs = projDataDir.listFiles() |
| 125 | + .filter { it.name != "minecraft" && it.isDirectory } |
| 126 | + .filter { |
| 127 | + val containsFunctionsDir = it.listFiles().any { |
| 128 | + it.name == "functions" && it.isDirectory |
| 129 | + } |
| 130 | + if (!containsFunctionsDir) println("Namespace directory `${it.path}` does not " + |
| 131 | + "contain a `function` directory. Skipping...") |
| 132 | + containsFunctionsDir |
| 133 | + } |
| 134 | + .map { it.resolve("functions") } |
| 135 | + |
| 136 | + // copy the source project directory |
| 137 | + println("Copying source project...") |
| 138 | + projDir.copyDir(transformedProjDir, projFunctionsDirs) |
| 139 | + |
| 140 | + // transform function directories |
| 141 | + for (functionDir in projFunctionsDirs) { |
| 142 | + Transformer.transformFunctionDir( |
| 143 | + functionDir, |
| 144 | + transformedProjDir.resolve(functionDir.relativeTo(projDir)), |
| 145 | + headerFile |
| 146 | + ) |
| 147 | + } |
| 148 | + |
| 149 | + println("Done.") |
| 150 | +} |
| 151 | + |
| 152 | +fun badConfig(property: String): String { |
| 153 | + println("\"$property\" missing from `shorthand.conf`.\nExiting...") |
| 154 | + exitProcess(-1) |
| 155 | +} |
| 156 | + |
| 157 | +/** Recursively copies [this] file/directory to [targetLoc] while skipping [skipFiles]. */ |
| 158 | +fun File.copyDir(targetLoc: File, skipFiles: List<File>) { |
| 159 | + |
| 160 | + // skip |
| 161 | + if (this in skipFiles) return |
| 162 | + |
| 163 | + // copy directory |
| 164 | + if (isDirectory) { |
| 165 | + try { |
| 166 | + if (!targetLoc.mkdir()) { |
| 167 | + println("Failed to create directory: `${targetLoc.path}`\nExiting...") |
| 168 | + exitProcess(-1) |
| 169 | + } |
| 170 | + } catch (e: IOException) { |
| 171 | + println("Failed to create directory: `${targetLoc.path}`\nExiting...") |
| 172 | + exitProcess(-1) |
| 173 | + } |
| 174 | + |
| 175 | + this.listFiles().forEach { it.copyDir(targetLoc.resolve(it.name), skipFiles) } |
| 176 | + } |
| 177 | + |
| 178 | + // copy file |
| 179 | + else { |
| 180 | + try { |
| 181 | + this.copyTo(targetLoc) |
| 182 | + } catch (e: IOException) { |
| 183 | + println("Failed to create file: `${targetLoc.name}`\nExiting...") |
| 184 | + exitProcess(-1) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments