diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 9a5c3b2dae..941dfdf79e 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -25,9 +25,9 @@ jobs: if: ${{ github.repository == 'Anuken/Mindustry' }} run: | ./gradlew updateBundles + git diff --exit-code - if [ $? ]; - then + if [ $? ]; then git add core/assets/bundles/* git commit -m "Automatic bundle update" git push diff --git a/tools/build.gradle b/tools/build.gradle index d61e4e01a9..903e0c12cf 100644 --- a/tools/build.gradle +++ b/tools/build.gradle @@ -2,12 +2,18 @@ sourceSets.main.java.srcDirs = ["src/"] import arc.files.Fi +import arc.func.Func2 import arc.graphics.Color import arc.graphics.Pixmap import arc.packer.TexturePacker import arc.struct.IntIntMap import arc.struct.IntMap +import arc.struct.ObjectMap +import arc.struct.OrderedMap +import arc.struct.Seq +import arc.util.Log import arc.util.async.Threads +import arc.util.io.PropertiesUtils import java.util.concurrent.ExecutorService import java.util.concurrent.Executors @@ -274,11 +280,105 @@ task updateScripts(dependsOn: classes, type: JavaExec){ workingDir = "../" } -task updateBundles(dependsOn: classes, type: JavaExec){ - file(genFolder).mkdirs() +task updateBundles{ + doLast{ + def uniEscape = { String string -> + StringBuilder outBuffer = new StringBuilder(); + int len = string.length(); + for(int i = 0; i < len; i++){ + char ch = string.charAt(i); + if((ch > 61) && (ch < 127)){ + outBuffer.append(ch == '\\' ? "\\\\" : ch); + continue; + } - mainClass = "mindustry.tools.BundleLauncher" - classpath = sourceSets.main.runtimeClasspath - standardInput = System.in - workingDir = "../core/assets/bundles/" + if(ch >= 0xE000){ + String hex = Integer.toHexString((int)ch); + outBuffer.append("\\u"); + for(int j = 0; j < 4 - hex.length(); j++){ + outBuffer.append('0'); + } + outBuffer.append(hex); + }else{ + outBuffer.append(ch); + } + } + + return outBuffer.toString(); + } + + OrderedMap base = new OrderedMap<>(); + PropertiesUtils.load(base, Fi.get("core/assets/bundles/bundle.properties").reader()); + Seq removals = new Seq<>(); + + Fi.get("core/assets/bundles").walk(child -> { + if(child.name().equals("bundle.properties") || child.toString().contains("output")) return; + + Log.info("Parsing bundle: @", child); + + OrderedMap other = new OrderedMap<>(); + + //find the last known comment of each line + ObjectMap comments = new ObjectMap<>(); + StringBuilder curComment = new StringBuilder(); + + for(String line : Seq.with(child.readString().split("\n", -1))){ + if(line.startsWith("#") || line.isEmpty()){ + curComment.append(line).append("\n"); + }else if(line.contains("=")){ + String lastKey = line.substring(0, line.indexOf("=")).trim(); + if(curComment.length() != 0){ + comments.put(lastKey, curComment.toString()); + curComment.setLength(0); + } + } + } + + ObjectMap extras = new OrderedMap<>(); + PropertiesUtils.load(other, child.reader()); + removals.clear(); + + for(String key : other.orderedKeys()){ + if(!base.containsKey(key) && key.contains(".details")){ + extras.put(key, other.get(key)); + }else if(!base.containsKey(key)){ + removals.add(key); + Log.info("&lr- Removing unused key '@'...", key); + } + } + Log.info("&lr@ keys removed.", removals.size); + for(String s : removals){ + other.remove(s); + } + + int added = 0; + + for(String key : base.orderedKeys()){ + if(other.get(key) == null || other.get(key).trim().isEmpty()){ + other.put(key, base.get(key)); + added++; + Log.info("&lc- Adding missing key '@'...", key); + } + } + + Func2 processor = (key, value) -> + (comments.containsKey(key) ? comments.get(key) : "") + //append last known comment if present + (key + " =" + (value.trim().isEmpty() ? "" : " ") + uniEscape(value)).replace("\n", "\\n") + "\n"; + Fi output = child.sibling("output/" + child.name()); + + Log.info("&lc@ keys added.", added); + Log.info("Writing bundle to @", output); + StringBuilder result = new StringBuilder(); + + //add everything ordered + for(String key : base.orderedKeys().copy().and(extras.keys().toSeq())){ + if(other.get(key) == null) continue; + + result.append(processor.get(key, other.get(key))); + other.remove(key); + } + + child.writeString(result.toString()); + }); + } } \ No newline at end of file diff --git a/tools/src/mindustry/tools/BundleLauncher.java b/tools/src/mindustry/tools/BundleLauncher.java deleted file mode 100644 index 19e9e32374..0000000000 --- a/tools/src/mindustry/tools/BundleLauncher.java +++ /dev/null @@ -1,112 +0,0 @@ -package mindustry.tools; - -import arc.files.*; -import arc.func.*; -import arc.struct.*; -import arc.util.*; -import arc.util.io.*; - -public class BundleLauncher{ - - public static void main(String[] args){ - OrderedMap base = new OrderedMap<>(); - PropertiesUtils.load(base, Fi.get("bundle.properties").reader()); - Seq removals = new Seq<>(); - - Fi.get(".").walk(child -> { - if(child.name().equals("bundle.properties") || child.toString().contains("output")) return; - - Log.info("Parsing bundle: @", child); - - OrderedMap other = new OrderedMap<>(); - - //find the last known comment of each line - ObjectMap comments = new ObjectMap<>(); - StringBuilder curComment = new StringBuilder(); - - for(String line : Seq.with(child.readString().split("\n", -1))){ - if(line.startsWith("#") || line.isEmpty()){ - curComment.append(line).append("\n"); - }else if(line.contains("=")){ - String lastKey = line.substring(0, line.indexOf("=")).trim(); - if(curComment.length() != 0){ - comments.put(lastKey, curComment.toString()); - curComment.setLength(0); - } - } - } - - ObjectMap extras = new OrderedMap<>(); - PropertiesUtils.load(other, child.reader()); - removals.clear(); - - for(String key : other.orderedKeys()){ - if(!base.containsKey(key) && key.contains(".details")){ - extras.put(key, other.get(key)); - }else if(!base.containsKey(key)){ - removals.add(key); - Log.info("&lr- Removing unused key '@'...", key); - } - } - Log.info("&lr@ keys removed.", removals.size); - for(String s : removals){ - other.remove(s); - } - - int added = 0; - - for(String key : base.orderedKeys()){ - if(other.get(key) == null || other.get(key).trim().isEmpty()){ - other.put(key, base.get(key)); - added++; - Log.info("&lc- Adding missing key '@'...", key); - } - } - - Func2 processor = (key, value) -> - (comments.containsKey(key) ? comments.get(key) : "") + //append last known comment if present - (key + " =" + (value.trim().isEmpty() ? "" : " ") + uniEscape(value)).replace("\n", "\\n") + "\n"; - Fi output = child.sibling("output/" + child.name()); - - Log.info("&lc@ keys added.", added); - Log.info("Writing bundle to @", output); - StringBuilder result = new StringBuilder(); - - //add everything ordered - for(String key : base.orderedKeys().copy().and(extras.keys().toSeq())){ - if(other.get(key) == null) continue; - - result.append(processor.get(key, other.get(key))); - other.remove(key); - } - - child.writeString(result.toString()); - }); - } - - static String uniEscape(String string){ - StringBuilder outBuffer = new StringBuilder(); - int len = string.length(); - for(int i = 0; i < len; i++){ - char ch = string.charAt(i); - if((ch > 61) && (ch < 127)){ - outBuffer.append(ch == '\\' ? "\\\\" : ch); - continue; - } - - if(ch >= 0xE000){ - String hex = Integer.toHexString(ch); - outBuffer.append("\\u"); - for(int j = 0; j < 4 - hex.length(); j++){ - outBuffer.append('0'); - } - outBuffer.append(hex); - }else{ - outBuffer.append(ch); - } - } - - return outBuffer.toString(); - } - -}