From 0d8b7a34ba417ac4562cd307904910160a29ae0d Mon Sep 17 00:00:00 2001 From: sharlottes Date: Sun, 7 Nov 2021 01:39:07 +0900 Subject: [PATCH 1/2] h --- gradle.properties | 2 +- src/UnitInfo/SVars.java | 2 +- src/UnitInfo/core/ContentJSON.java | 84 ++++++++++++++++++++++-------- src/UnitInfo/core/Main.java | 65 +++++++++++++++++++++++ src/UnitInfo/core/OverDrawer.java | 1 + 5 files changed, 129 insertions(+), 25 deletions(-) diff --git a/gradle.properties b/gradle.properties index ecfdf6a..10b2f0b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,6 +3,6 @@ pArtifactFilename = UnitInfo.jar # Group of project (should line up with dirs in src/) pGroup = sharlotte # Version of Mindustry to use (if not working, consult jitpack.io) -pMindustryVersion = v133 +pMindustryVersion = v134.1 # Minimum API level for d8 (if you don't know what this is, leave it unless d8 yells at you) pMinApi = 26 \ No newline at end of file diff --git a/src/UnitInfo/SVars.java b/src/UnitInfo/SVars.java index bc57f30..1516631 100644 --- a/src/UnitInfo/SVars.java +++ b/src/UnitInfo/SVars.java @@ -16,5 +16,5 @@ public class SVars { public static TextureRegion error = atlas.find("error"); public static RangeShader turretRange; public static LineShader lineShader; - public static boolean jsonGen = false; + public static boolean jsonGen = true; } diff --git a/src/UnitInfo/core/ContentJSON.java b/src/UnitInfo/core/ContentJSON.java index ef47dd6..5f2a72b 100644 --- a/src/UnitInfo/core/ContentJSON.java +++ b/src/UnitInfo/core/ContentJSON.java @@ -1,49 +1,87 @@ package UnitInfo.core; import arc.Core; -import arc.files.Fi; import arc.struct.Seq; -import arc.util.Log; +import arc.util.*; import mindustry.*; -import mindustry.ctype.Content; -import mindustry.ctype.MappableContent; +import mindustry.ctype.*; import org.hjson.*; -import java.lang.reflect.Field; +import java.lang.reflect.*; import static mindustry.Vars.modDirectory; public class ContentJSON { + public static void collect(String name, Object object, JsonObject obj) { + Object preval = obj.get(name); + if(preval != null) obj.set(name, preval + " or " + object); + else { + if(object == null) obj.add(name, "null"); + else if(!Modifier.isStatic(object.getClass().getModifiers())) { //no static + if(object instanceof Integer val) obj.add(name, val); + else if(object instanceof Double val) obj.add(name, val); + else if(object instanceof Float val) obj.add(name, val); + else if(object instanceof Long val) obj.add(name, val); + else if(object instanceof String val) obj.add(name, val); + else if(object instanceof Boolean val) obj.add(name, val); + else if(object instanceof Content cont){ //create new json object + //obj.add(name, getContent(cont, new JsonObject())); + } + else if(object instanceof Seq seq && seq.any()) { + StringBuilder str = new StringBuilder("["); + for(int i = 0; i < seq.size; i++) { + if(seq.get(i) != null) str.append(seq.get(i).toString()).append(i+1==seq.size ? "" : ", "); + } + str.append("]"); + obj.add(name, str.toString()); + } + else { + if(object.getClass().isArray()) { + StringBuilder str = new StringBuilder("["); + for(int i = 0; i < Array.getLength(object); i++) { + if(Array.get(object, i) != null) str.append(Array.get(object, i).toString()).append(i+1==Array.getLength(object) ? "" : ", "); + } + str.append("]"); + obj.add(name, str.toString()); + } else { + obj.add(name, object.toString()); + } + } + } + } + } + + public static JsonObject getContent(Content cont, JsonObject obj) { + for(Field field : cont.getClass().getFields()){ + try { + collect(field.getName(), field.get(cont), obj); + } catch(Throwable e) { + Log.warn(e.getMessage() + "on " + cont); + obj.add(field.getName(), "### ERROR ###"); + } + } + + return obj; + } + public static void save() { for(Seq content : Vars.content.getContentMap()) { + if(content.isEmpty()) continue; + JsonObject data = new JsonObject(); content.each(cont -> { JsonObject obj = new JsonObject(); - Seq seq = new Seq(cont.getClass().getFields()); - seq.reverse(); obj.add("type", cont.getClass().getName()); - for(Field field : seq){ - try { - String str = field.getName(); - Object object = field.get(cont); - if(object instanceof Integer val) obj.add(str, val); - if(object instanceof Long val) obj.add(str, val); - if(object instanceof Float val) obj.add(str, val); - if(object instanceof String val) obj.add(str, val); - if(object instanceof Double val) obj.add(str, val); - if(object instanceof Boolean val) obj.add(str, val); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } + getContent(cont, obj); + String name = cont.toString(); if(cont instanceof MappableContent mapCont) name = mapCont.name; data.add(name, obj); }); try { modDirectory.child("UnitInfo").child(content.peek().getContentType().toString() + ".json").writeString(data.toString(Stringify.FORMATTED)); - } catch (Throwable t){ - + } catch (Throwable e){ + Log.warn(e.getMessage()); } } Log.info("JSON file is completely updated!"); diff --git a/src/UnitInfo/core/Main.java b/src/UnitInfo/core/Main.java index d8b787c..243445f 100644 --- a/src/UnitInfo/core/Main.java +++ b/src/UnitInfo/core/Main.java @@ -2,9 +2,22 @@ package UnitInfo.core; import UnitInfo.shaders.*; import arc.*; +import arc.audio.Sound; +import arc.files.Fi; +import arc.input.KeyCode; +import arc.scene.ui.TextArea; +import arc.struct.Seq; +import arc.util.Log; +import arc.util.Nullable; +import arc.util.Strings; +import arc.util.Timer; import mindustry.*; import mindustry.game.EventType.*; +import mindustry.gen.Call; +import mindustry.gen.Player; +import mindustry.gen.Sounds; import mindustry.mod.*; +import mindustry.ui.dialogs.BaseDialog; import static UnitInfo.SVars.*; import static arc.Core.*; @@ -46,5 +59,57 @@ public class Main extends Mod { hud = new HudUi(); hud.addWaveTable(); }); + + Events.run(Trigger.update, () -> { + if((input.keyDown(KeyCode.shiftRight) || input.keyDown(KeyCode.shiftLeft))){ + if(input.keyTap(KeyCode.h)) { + mmid_playMusicSeq(mmid_parseMusicString(Fi.get("C:/Users/user/Desktop/test/output.txt").readString()), null); + }; + if(input.keyTap(KeyCode.c)) { + schedules.each(Timer.Task::cancel); + } + } + }); + } + + Seq mmid_parseMusicString(String s) { + String[] notes = s.split(";"); + Seq output = new Seq(); + for (String value : notes) { + String[] note = value.split(","); + if (note.length > 0) { + output.add(new Float[]{ + Float.parseFloat(note[0]), + (note.length < 2 || note[1] == null) ? 0 : Float.parseFloat(note[1]), + (note.length < 3 || note[2] == null) ? 0 : Float.parseFloat(note[2]), + (note.length < 4 || note[3] == null) ? 1 : Float.parseFloat(note[3]), + (note.length < 5 || note[4] == null) ? 0 : Float.parseFloat(note[4]) + }); + } + } + Log.info(output); + return output; + }; + + Seq schedules = new Seq<>(); + void mmid_playMusicSeq(Seq s, @Nullable Player p) { + Object[][] mmid_instruments = { //Sound, pitch, volume + {Sounds.minebeam, 0.98f, 20f}, + {Sounds.minebeam, 2.2f, 0.5f}}; + + s.each(n-> { + schedules.add(Timer.schedule(() -> { + Log.info(mmid_instruments[n[2].intValue()][0].toString() + " sound is called"); + if(p == null || p.con == null) Call.sound( + (Sound)mmid_instruments[n[2].intValue()][0], + n[3]*(float)mmid_instruments[n[2].intValue()][2], + (float)mmid_instruments[n[2].intValue()][1]*(float)Math.pow(1.0595,n[1]), n[4]); + else Call.sound(p.con, + (Sound)mmid_instruments[n[2].intValue()][0], + n[3]*(float)mmid_instruments[n[2].intValue()][2], + (float)mmid_instruments[n[2].intValue()][1]*1f*(float)Math.pow(1.0595,n[1]), n[4]); + },n[0])); + Log.info("start sound after" + n[0] + "sec"); + }); } } diff --git a/src/UnitInfo/core/OverDrawer.java b/src/UnitInfo/core/OverDrawer.java index e15be0e..76b9ca1 100644 --- a/src/UnitInfo/core/OverDrawer.java +++ b/src/UnitInfo/core/OverDrawer.java @@ -2,6 +2,7 @@ package UnitInfo.core; import UnitInfo.ui.*; import arc.*; +import arc.audio.Sound; import arc.graphics.*; import arc.graphics.g2d.*; import arc.graphics.gl.*; From 9c99d6986c1bc9da3793a96f1debaee917af343f Mon Sep 17 00:00:00 2001 From: sharlottes Date: Sun, 7 Nov 2021 01:53:45 +0900 Subject: [PATCH 2/2] h --- src/UnitInfo/core/ContentJSON.java | 33 +++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/UnitInfo/core/ContentJSON.java b/src/UnitInfo/core/ContentJSON.java index 5f2a72b..0d6d30e 100644 --- a/src/UnitInfo/core/ContentJSON.java +++ b/src/UnitInfo/core/ContentJSON.java @@ -1,6 +1,7 @@ package UnitInfo.core; import arc.Core; +import arc.struct.ObjectMap; import arc.struct.Seq; import arc.util.*; import mindustry.*; @@ -12,7 +13,7 @@ import java.lang.reflect.*; import static mindustry.Vars.modDirectory; public class ContentJSON { - public static void collect(String name, Object object, JsonObject obj) { + public static void collect(String name, Object object, JsonObject obj, boolean isloop) { Object preval = obj.get(name); if(preval != null) obj.set(name, preval + " or " + object); else { @@ -25,7 +26,25 @@ public class ContentJSON { else if(object instanceof String val) obj.add(name, val); else if(object instanceof Boolean val) obj.add(name, val); else if(object instanceof Content cont){ //create new json object - //obj.add(name, getContent(cont, new JsonObject())); + try { + obj.add(name, getContent(cont, new JsonObject(), isloop)); + } catch(Throwable e) { + Log.info(e.getMessage() + " ### " + cont); + } + } + else if(object instanceof ObjectMap map){ //create new json object + /* + try { + JsonObject object1 = new JsonObject(); + map.each((k, v) -> { + object1.add(k.toString(), getContent(v, new JsonObject(), isloop)) + }); + obj.add(name, getContent(cont, new JsonObject(), isloop)); + } catch(Throwable e) { + Log.info(e.getMessage() + " ### " + cont); + + } + */ } else if(object instanceof Seq seq && seq.any()) { StringBuilder str = new StringBuilder("["); @@ -51,12 +70,12 @@ public class ContentJSON { } } - public static JsonObject getContent(Content cont, JsonObject obj) { - for(Field field : cont.getClass().getFields()){ + public static JsonObject getContent(Content cont, JsonObject obj, boolean isloop) { + for(Field field : cont.getClass().getFields()){ try { - collect(field.getName(), field.get(cont), obj); + if(!isloop) collect(field.getName(), field.get(cont), obj, true); } catch(Throwable e) { - Log.warn(e.getMessage() + "on " + cont); + Log.info(e.getMessage() + " ### " + cont); obj.add(field.getName(), "### ERROR ###"); } } @@ -72,7 +91,7 @@ public class ContentJSON { content.each(cont -> { JsonObject obj = new JsonObject(); obj.add("type", cont.getClass().getName()); - getContent(cont, obj); + getContent(cont, obj, false); String name = cont.toString(); if(cont instanceof MappableContent mapCont) name = mapCont.name;