From d3747f0d4c5945b55c0885c6e5152c87e8983d1e Mon Sep 17 00:00:00 2001 From: Anuken Date: Sat, 7 Dec 2019 15:41:52 -0500 Subject: [PATCH] Content loading improvements --- core/assets/scripts/base.js | 2 +- core/assets/scripts/global.js | 2 +- .../anuke/mindustry/core/ContentLoader.java | 12 ++++++ .../mindustry/ctype/MappableContent.java | 2 +- .../mindustry/ctype/UnlockableContent.java | 4 +- core/src/io/anuke/mindustry/mod/Mods.java | 37 +++++++++++-------- core/src/io/anuke/mindustry/type/Item.java | 1 - core/src/io/anuke/mindustry/type/Liquid.java | 1 - core/src/io/anuke/mindustry/type/Mech.java | 1 - .../src/io/anuke/mindustry/type/UnitType.java | 1 - core/src/io/anuke/mindustry/world/Block.java | 1 - 11 files changed, 38 insertions(+), 26 deletions(-) diff --git a/core/assets/scripts/base.js b/core/assets/scripts/base.js index 124acce5b7..4d8cb451e6 100755 --- a/core/assets/scripts/base.js +++ b/core/assets/scripts/base.js @@ -3,7 +3,7 @@ const log = function(context, obj){ } const extendContent = function(classType, name, params){ - return new JavaAdapter(classType, params, modName + "-" + name) + return new JavaAdapter(classType, params, name) } const extend = function(classType, params){ diff --git a/core/assets/scripts/global.js b/core/assets/scripts/global.js index d3c53473ee..b92b9f22e2 100755 --- a/core/assets/scripts/global.js +++ b/core/assets/scripts/global.js @@ -5,7 +5,7 @@ const log = function(context, obj){ } const extendContent = function(classType, name, params){ - return new JavaAdapter(classType, params, modName + "-" + name) + return new JavaAdapter(classType, params, name) } const extend = function(classType, params){ diff --git a/core/src/io/anuke/mindustry/core/ContentLoader.java b/core/src/io/anuke/mindustry/core/ContentLoader.java index b3672d5c00..32a5ee1b56 100644 --- a/core/src/io/anuke/mindustry/core/ContentLoader.java +++ b/core/src/io/anuke/mindustry/core/ContentLoader.java @@ -3,6 +3,7 @@ package io.anuke.mindustry.core; import io.anuke.arc.collection.*; import io.anuke.arc.func.*; import io.anuke.arc.graphics.*; +import io.anuke.arc.util.ArcAnnotate.*; import io.anuke.arc.util.*; import io.anuke.mindustry.content.*; import io.anuke.mindustry.ctype.*; @@ -23,6 +24,7 @@ public class ContentLoader{ private ObjectMap[] contentNameMap = new ObjectMap[ContentType.values().length]; private Array[] contentMap = new Array[ContentType.values().length]; private MappableContent[][] temporaryMapper; + private @Nullable LoadedMod currentMod; private ObjectSet> initialization = new ObjectSet<>(); private ContentList[] content = { new Fx(), @@ -144,13 +146,23 @@ public class ContentLoader{ public void handleContent(Content content){ contentMap[content.getContentType().ordinal()].add(content); + } + public void setCurrentMod(LoadedMod mod){ + this.currentMod = mod; + } + + public String transformName(String name){ + return currentMod == null ? name : currentMod.name + "-" + name; } public void handleMappableContent(MappableContent content){ if(contentNameMap[content.getContentType().ordinal()].containsKey(content.name)){ throw new IllegalArgumentException("Two content objects cannot have the same name! (issue: '" + content.name + "')"); } + if(currentMod != null){ + content.mod = currentMod; + } contentNameMap[content.getContentType().ordinal()].put(content.name, content); } diff --git a/core/src/io/anuke/mindustry/ctype/MappableContent.java b/core/src/io/anuke/mindustry/ctype/MappableContent.java index 3063157c13..709e7652d4 100644 --- a/core/src/io/anuke/mindustry/ctype/MappableContent.java +++ b/core/src/io/anuke/mindustry/ctype/MappableContent.java @@ -6,7 +6,7 @@ public abstract class MappableContent extends Content{ public final String name; public MappableContent(String name){ - this.name = name; + this.name = Vars.content.transformName(name); Vars.content.handleMappableContent(this); } diff --git a/core/src/io/anuke/mindustry/ctype/UnlockableContent.java b/core/src/io/anuke/mindustry/ctype/UnlockableContent.java index bb50e115cb..7082b4a431 100644 --- a/core/src/io/anuke/mindustry/ctype/UnlockableContent.java +++ b/core/src/io/anuke/mindustry/ctype/UnlockableContent.java @@ -20,8 +20,8 @@ public abstract class UnlockableContent extends MappableContent{ public UnlockableContent(String name){ super(name); - this.localizedName = Core.bundle.get(getContentType() + "." + name + ".name", name); - this.description = Core.bundle.getOrNull(getContentType() + "." + name + ".description"); + this.localizedName = Core.bundle.get(getContentType() + "." + this.name + ".name", this.name); + this.description = Core.bundle.getOrNull(getContentType() + "." + this.name + ".description"); } /** Generate any special icons for this content. Called asynchronously.*/ diff --git a/core/src/io/anuke/mindustry/mod/Mods.java b/core/src/io/anuke/mindustry/mod/Mods.java index 1f7120ce06..33e567148c 100644 --- a/core/src/io/anuke/mindustry/mod/Mods.java +++ b/core/src/io/anuke/mindustry/mod/Mods.java @@ -382,27 +382,32 @@ public class Mods implements Loadable{ public void loadScripts(){ Time.mark(); - for(LoadedMod mod : loaded){ - if(mod.root.child("scripts").exists()){ - mod.scripts = mod.root.child("scripts").findAll(f -> f.extension().equals("js")); - Log.info("[{0}] Found {1} scripts.", mod.meta.name, mod.scripts.size); + try{ + for(LoadedMod mod : loaded){ + if(mod.root.child("scripts").exists()){ + content.setCurrentMod(mod.name); + mod.scripts = mod.root.child("scripts").findAll(f -> f.extension().equals("js")); + Log.info("[{0}] Found {1} scripts.", mod.meta.name, mod.scripts.size); - for(FileHandle file : mod.scripts){ - try{ - if(scripts == null){ - scripts = platform.createScripts(); + for(FileHandle file : mod.scripts){ + try{ + if(scripts == null){ + scripts = platform.createScripts(); + } + scripts.run(mod, file); + }catch(Throwable e){ + Core.app.post(() -> { + Log.err("Error loading script {0} for mod {1}.", file.name(), mod.meta.name); + e.printStackTrace(); + //if(!headless) ui.showException(e); + }); + break; } - scripts.run(mod, file); - }catch(Throwable e){ - Core.app.post(() -> { - Log.err("Error loading script {0} for mod {1}.", file.name(), mod.meta.name); - e.printStackTrace(); - //if(!headless) ui.showException(e); - }); - break; } } } + }finally{ + content.setCurrentMod(null); } Log.info("Time to initialize modded scripts: {0}", Time.elapsed()); diff --git a/core/src/io/anuke/mindustry/type/Item.java b/core/src/io/anuke/mindustry/type/Item.java index 6ec12ae475..86951ab093 100644 --- a/core/src/io/anuke/mindustry/type/Item.java +++ b/core/src/io/anuke/mindustry/type/Item.java @@ -34,7 +34,6 @@ public class Item extends UnlockableContent{ public Item(String name, Color color){ super(name); this.color = color; - this.description = Core.bundle.getOrNull("item." + this.name + ".description"); } public Item(String name){ diff --git a/core/src/io/anuke/mindustry/type/Liquid.java b/core/src/io/anuke/mindustry/type/Liquid.java index bc97a18990..63839c479c 100644 --- a/core/src/io/anuke/mindustry/type/Liquid.java +++ b/core/src/io/anuke/mindustry/type/Liquid.java @@ -31,7 +31,6 @@ public class Liquid extends UnlockableContent{ public Liquid(String name, Color color){ super(name); this.color = new Color(color); - this.description = Core.bundle.getOrNull("liquid." + name + ".description"); } /** For modding only.*/ diff --git a/core/src/io/anuke/mindustry/type/Mech.java b/core/src/io/anuke/mindustry/type/Mech.java index 8eb9681548..a9419a116b 100644 --- a/core/src/io/anuke/mindustry/type/Mech.java +++ b/core/src/io/anuke/mindustry/type/Mech.java @@ -39,7 +39,6 @@ public class Mech extends UnlockableContent{ public Mech(String name, boolean flying){ super(name); this.flying = flying; - this.description = Core.bundle.get("mech." + name + ".description"); } public Mech(String name){ diff --git a/core/src/io/anuke/mindustry/type/UnitType.java b/core/src/io/anuke/mindustry/type/UnitType.java index b2602a16ad..ddcaa4f36f 100644 --- a/core/src/io/anuke/mindustry/type/UnitType.java +++ b/core/src/io/anuke/mindustry/type/UnitType.java @@ -51,7 +51,6 @@ public class UnitType extends UnlockableContent{ public UnitType(String name){ super(name); - this.description = Core.bundle.getOrNull("unit." + name + ".description"); } public void create(Prov mainConstructor){ diff --git a/core/src/io/anuke/mindustry/world/Block.java b/core/src/io/anuke/mindustry/world/Block.java index e05f047b21..90abba2283 100644 --- a/core/src/io/anuke/mindustry/world/Block.java +++ b/core/src/io/anuke/mindustry/world/Block.java @@ -156,7 +156,6 @@ public class Block extends BlockStorage{ public Block(String name){ super(name); - this.description = Core.bundle.getOrNull("block." + name + ".description"); this.solid = false; }