mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-22 17:51:53 +00:00
Log script messages to debug
instead of info
.
This commit is contained in:
parent
34f7c6e780
commit
447360594d
@ -44,16 +44,16 @@ public class ScriptLoader {
|
|||||||
if (sm != null) {
|
if (sm != null) {
|
||||||
throw new Exception("Script loader already initialized");
|
throw new Exception("Script loader already initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create script engine
|
// Create script engine
|
||||||
sm = new ScriptEngineManager();
|
sm = new ScriptEngineManager();
|
||||||
engine = sm.getEngineByName("luaj");
|
engine = sm.getEngineByName("luaj");
|
||||||
factory = getEngine().getFactory();
|
factory = getEngine().getFactory();
|
||||||
|
|
||||||
// Lua stuff
|
// Lua stuff
|
||||||
fileType = "lua";
|
fileType = "lua";
|
||||||
serializer = new LuaSerializer();
|
serializer = new LuaSerializer();
|
||||||
|
|
||||||
// Set engine to replace require as a temporary fix to missing scripts
|
// Set engine to replace require as a temporary fix to missing scripts
|
||||||
LuajContext ctx = (LuajContext) engine.getContext();
|
LuajContext ctx = (LuajContext) engine.getContext();
|
||||||
ctx.globals.set("require", new OneArgFunction() {
|
ctx.globals.set("require", new OneArgFunction() {
|
||||||
@ -62,11 +62,11 @@ public class ScriptLoader {
|
|||||||
return LuaValue.ZERO;
|
return LuaValue.ZERO;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
LuaTable table = new LuaTable();
|
LuaTable table = new LuaTable();
|
||||||
Arrays.stream(EntityType.values()).forEach(e -> table.set(e.name().toUpperCase(), e.getValue()));
|
Arrays.stream(EntityType.values()).forEach(e -> table.set(e.name().toUpperCase(), e.getValue()));
|
||||||
ctx.globals.set("EntityType", table);
|
ctx.globals.set("EntityType", table);
|
||||||
|
|
||||||
ctx.globals.set("EventType", CoerceJavaToLua.coerce(new EventType())); // TODO - make static class to avoid instantiating a new class every scene
|
ctx.globals.set("EventType", CoerceJavaToLua.coerce(new EventType())); // TODO - make static class to avoid instantiating a new class every scene
|
||||||
ctx.globals.set("GadgetState", CoerceJavaToLua.coerce(new ScriptGadgetState()));
|
ctx.globals.set("GadgetState", CoerceJavaToLua.coerce(new ScriptGadgetState()));
|
||||||
ctx.globals.set("RegionShape", CoerceJavaToLua.coerce(new ScriptRegionShape()));
|
ctx.globals.set("RegionShape", CoerceJavaToLua.coerce(new ScriptRegionShape()));
|
||||||
@ -75,11 +75,11 @@ public class ScriptLoader {
|
|||||||
scriptLibLua = CoerceJavaToLua.coerce(scriptLib);
|
scriptLibLua = CoerceJavaToLua.coerce(scriptLib);
|
||||||
ctx.globals.set("ScriptLib", scriptLibLua);
|
ctx.globals.set("ScriptLib", scriptLibLua);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ScriptEngine getEngine() {
|
public static ScriptEngine getEngine() {
|
||||||
return engine;
|
return engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getScriptType() {
|
public static String getScriptType() {
|
||||||
return fileType;
|
return fileType;
|
||||||
}
|
}
|
||||||
@ -109,7 +109,7 @@ public class ScriptLoader {
|
|||||||
return sc.get();
|
return sc.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
Grasscutter.getLogger().info("Loading script " + path);
|
Grasscutter.getLogger().debug("Loading script " + path);
|
||||||
|
|
||||||
File file = new File(path);
|
File file = new File(path);
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ public class SceneBlock {
|
|||||||
private transient boolean loaded; // Not an actual variable in the scripts either
|
private transient boolean loaded; // Not an actual variable in the scripts either
|
||||||
|
|
||||||
public boolean isLoaded() {
|
public boolean isLoaded() {
|
||||||
return loaded;
|
return this.loaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLoaded(boolean loaded) {
|
public void setLoaded(boolean loaded) {
|
||||||
@ -40,19 +40,19 @@ public class SceneBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean contains(Position pos) {
|
public boolean contains(Position pos) {
|
||||||
return pos.getX() <= max.getX() && pos.getX() >= min.getX() &&
|
return pos.getX() <= this.max.getX() && pos.getX() >= this.min.getX() &&
|
||||||
pos.getZ() <= max.getZ() && pos.getZ() >= min.getZ();
|
pos.getZ() <= this.max.getZ() && pos.getZ() >= this.min.getZ();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SceneBlock load(int sceneId, Bindings bindings){
|
public SceneBlock load(int sceneId, Bindings bindings){
|
||||||
if(loaded){
|
if(this.loaded){
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
this.sceneId = sceneId;
|
this.sceneId = sceneId;
|
||||||
setLoaded(true);
|
this.setLoaded(true);
|
||||||
|
|
||||||
CompiledScript cs = ScriptLoader.getScriptByPath(
|
CompiledScript cs = ScriptLoader.getScriptByPath(
|
||||||
SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_block" + id + "." + ScriptLoader.getScriptType()));
|
SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_block" + this.id + "." + ScriptLoader.getScriptType()));
|
||||||
|
|
||||||
if (cs == null) {
|
if (cs == null) {
|
||||||
return null;
|
return null;
|
||||||
@ -63,19 +63,19 @@ public class SceneBlock {
|
|||||||
cs.eval(bindings);
|
cs.eval(bindings);
|
||||||
|
|
||||||
// Set groups
|
// Set groups
|
||||||
groups = ScriptLoader.getSerializer().toList(SceneGroup.class, bindings.get("groups")).stream()
|
this.groups = ScriptLoader.getSerializer().toList(SceneGroup.class, bindings.get("groups")).stream()
|
||||||
.collect(Collectors.toMap(x -> x.id, y -> y));
|
.collect(Collectors.toMap(x -> x.id, y -> y));
|
||||||
|
|
||||||
groups.values().forEach(g -> g.block_id = id);
|
this.groups.values().forEach(g -> g.block_id = this.id);
|
||||||
this.sceneGroupIndex = SceneIndexManager.buildIndex(3, groups.values(), g -> g.pos.toPoint());
|
this.sceneGroupIndex = SceneIndexManager.buildIndex(3, this.groups.values(), g -> g.pos.toPoint());
|
||||||
} catch (ScriptException e) {
|
} catch (ScriptException exception) {
|
||||||
Grasscutter.getLogger().error("Error loading block " + id + " in scene " + sceneId, e);
|
Grasscutter.getLogger().error("An error occurred while loading block " + this.id + " in scene " + sceneId, exception);
|
||||||
}
|
}
|
||||||
Grasscutter.getLogger().info("scene {} block {} is loaded successfully.", sceneId, id);
|
Grasscutter.getLogger().debug("Successfully loaded block {} in scene {}.", this.id, sceneId);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Rectangle toRectangle() {
|
public Rectangle toRectangle() {
|
||||||
return Rectangle.create(min.toXZDoubleArray(), max.toXZDoubleArray());
|
return Rectangle.create(this.min.toXZDoubleArray(), this.max.toXZDoubleArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -25,7 +25,7 @@ import static emu.grasscutter.Configuration.SCRIPT;
|
|||||||
@Setter
|
@Setter
|
||||||
public class SceneGroup {
|
public class SceneGroup {
|
||||||
public transient int block_id; // Not an actual variable in the scripts but we will keep it here for reference
|
public transient int block_id; // Not an actual variable in the scripts but we will keep it here for reference
|
||||||
|
|
||||||
public int id;
|
public int id;
|
||||||
public int refresh_id;
|
public int refresh_id;
|
||||||
public Position pos;
|
public Position pos;
|
||||||
@ -52,133 +52,133 @@ public class SceneGroup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLoaded() {
|
public boolean isLoaded() {
|
||||||
return loaded;
|
return this.loaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLoaded(boolean loaded) {
|
public void setLoaded(boolean loaded) {
|
||||||
this.loaded = loaded;
|
this.loaded = loaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBusinessType() {
|
public int getBusinessType() {
|
||||||
return this.business == null ? 0 : this.business.type;
|
return this.business == null ? 0 : this.business.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SceneGadget> getGarbageGadgets() {
|
public List<SceneGadget> getGarbageGadgets() {
|
||||||
return this.garbages == null ? null : this.garbages.gadgets;
|
return this.garbages == null ? null : this.garbages.gadgets;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompiledScript getScript() {
|
public CompiledScript getScript() {
|
||||||
return script;
|
return this.script;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SceneSuite getSuiteByIndex(int index) {
|
public SceneSuite getSuiteByIndex(int index) {
|
||||||
return suites.get(index - 1);
|
return this.suites.get(index - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bindings getBindings() {
|
public Bindings getBindings() {
|
||||||
return bindings;
|
return this.bindings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized SceneGroup load(int sceneId){
|
public synchronized SceneGroup load(int sceneId){
|
||||||
if(loaded){
|
if(this.loaded){
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
// Set flag here so if there is no script, we dont call this function over and over again.
|
// Set flag here so if there is no script, we don't call this function over and over again.
|
||||||
setLoaded(true);
|
this.setLoaded(true);
|
||||||
|
|
||||||
this.bindings = ScriptLoader.getEngine().createBindings();
|
this.bindings = ScriptLoader.getEngine().createBindings();
|
||||||
|
|
||||||
CompiledScript cs = ScriptLoader.getScriptByPath(
|
CompiledScript cs = ScriptLoader.getScriptByPath(
|
||||||
SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_group" + id + "." + ScriptLoader.getScriptType()));
|
SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_group" + this.id + "." + ScriptLoader.getScriptType()));
|
||||||
|
|
||||||
if (cs == null) {
|
if (cs == null) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.script = cs;
|
this.script = cs;
|
||||||
|
|
||||||
// Eval script
|
// Eval script
|
||||||
try {
|
try {
|
||||||
cs.eval(bindings);
|
cs.eval(this.bindings);
|
||||||
|
|
||||||
// Set
|
// Set
|
||||||
monsters = ScriptLoader.getSerializer().toList(SceneMonster.class, bindings.get("monsters")).stream()
|
this.monsters = ScriptLoader.getSerializer().toList(SceneMonster.class, this.bindings.get("monsters")).stream()
|
||||||
.collect(Collectors.toMap(x -> x.config_id, y -> y));
|
.collect(Collectors.toMap(x -> x.config_id, y -> y));
|
||||||
monsters.values().forEach(m -> m.group = this);
|
this.monsters.values().forEach(m -> m.group = this);
|
||||||
|
|
||||||
gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, bindings.get("gadgets")).stream()
|
this.gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, this.bindings.get("gadgets")).stream()
|
||||||
.collect(Collectors.toMap(x -> x.config_id, y -> y));
|
.collect(Collectors.toMap(x -> x.config_id, y -> y));
|
||||||
gadgets.values().forEach(m -> m.group = this);
|
this.gadgets.values().forEach(m -> m.group = this);
|
||||||
|
|
||||||
triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, bindings.get("triggers")).stream()
|
this.triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, this.bindings.get("triggers")).stream()
|
||||||
.collect(Collectors.toMap(x -> x.name, y -> y));
|
.collect(Collectors.toMap(x -> x.name, y -> y));
|
||||||
triggers.values().forEach(t -> t.currentGroup = this);
|
this.triggers.values().forEach(t -> t.currentGroup = this);
|
||||||
|
|
||||||
suites = ScriptLoader.getSerializer().toList(SceneSuite.class, bindings.get("suites"));
|
this.suites = ScriptLoader.getSerializer().toList(SceneSuite.class, this.bindings.get("suites"));
|
||||||
regions = ScriptLoader.getSerializer().toList(SceneRegion.class, bindings.get("regions")).stream()
|
this.regions = ScriptLoader.getSerializer().toList(SceneRegion.class, this.bindings.get("regions")).stream()
|
||||||
.collect(Collectors.toMap(x -> x.config_id, y -> y));
|
.collect(Collectors.toMap(x -> x.config_id, y -> y));
|
||||||
regions.values().forEach(m -> m.group = this);
|
this.regions.values().forEach(m -> m.group = this);
|
||||||
|
|
||||||
init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, bindings.get("init_config"));
|
this.init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, this.bindings.get("init_config"));
|
||||||
|
|
||||||
// Garbages TODO fix properly later
|
// Garbages // TODO: fix properly later
|
||||||
Object garbagesValue = bindings.get("garbages");
|
Object garbagesValue = this.bindings.get("garbages");
|
||||||
if (garbagesValue != null && garbagesValue instanceof LuaValue garbagesTable) {
|
if (garbagesValue instanceof LuaValue garbagesTable) {
|
||||||
garbages = new SceneGarbage();
|
this.garbages = new SceneGarbage();
|
||||||
if (garbagesTable.checktable().get("gadgets") != LuaValue.NIL) {
|
if (garbagesTable.checktable().get("gadgets") != LuaValue.NIL) {
|
||||||
garbages.gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, garbagesTable.checktable().get("gadgets").checktable());
|
this.garbages.gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, garbagesTable.checktable().get("gadgets").checktable());
|
||||||
garbages.gadgets.forEach(m -> m.group = this);
|
this.garbages.gadgets.forEach(m -> m.group = this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add variables to suite
|
// Add variables to suite
|
||||||
variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables"));
|
this.variables = ScriptLoader.getSerializer().toList(SceneVar.class, this.bindings.get("variables"));
|
||||||
// NPC in groups
|
// NPC in groups
|
||||||
npc = ScriptLoader.getSerializer().toList(SceneNPC.class, bindings.get("npcs")).stream()
|
this.npc = ScriptLoader.getSerializer().toList(SceneNPC.class, this.bindings.get("npcs")).stream()
|
||||||
.collect(Collectors.toMap(x -> x.npc_id, y -> y));
|
.collect(Collectors.toMap(x -> x.npc_id, y -> y));
|
||||||
npc.values().forEach(n -> n.group = this);
|
this.npc.values().forEach(n -> n.group = this);
|
||||||
|
|
||||||
// Add monsters and gadgets to suite
|
// Add monsters and gadgets to suite
|
||||||
for (SceneSuite suite : suites) {
|
for (SceneSuite suite : this.suites) {
|
||||||
suite.sceneMonsters = new ArrayList<>(
|
suite.sceneMonsters = new ArrayList<>(
|
||||||
suite.monsters.stream()
|
suite.monsters.stream()
|
||||||
.filter(monsters::containsKey)
|
.filter(this.monsters::containsKey)
|
||||||
.map(monsters::get)
|
.map(this.monsters::get)
|
||||||
.toList()
|
.toList()
|
||||||
);
|
);
|
||||||
|
|
||||||
suite.sceneGadgets = new ArrayList<>(
|
suite.sceneGadgets = new ArrayList<>(
|
||||||
suite.gadgets.stream()
|
suite.gadgets.stream()
|
||||||
.filter(gadgets::containsKey)
|
.filter(this.gadgets::containsKey)
|
||||||
.map(gadgets::get)
|
.map(this.gadgets::get)
|
||||||
.toList()
|
.toList()
|
||||||
);
|
);
|
||||||
|
|
||||||
suite.sceneTriggers = new ArrayList<>(
|
suite.sceneTriggers = new ArrayList<>(
|
||||||
suite.triggers.stream()
|
suite.triggers.stream()
|
||||||
.filter(triggers::containsKey)
|
.filter(this.triggers::containsKey)
|
||||||
.map(triggers::get)
|
.map(this.triggers::get)
|
||||||
.toList()
|
.toList()
|
||||||
);
|
);
|
||||||
|
|
||||||
suite.sceneRegions = new ArrayList<>(
|
suite.sceneRegions = new ArrayList<>(
|
||||||
suite.regions.stream()
|
suite.regions.stream()
|
||||||
.filter(regions::containsKey)
|
.filter(this.regions::containsKey)
|
||||||
.map(regions::get)
|
.map(this.regions::get)
|
||||||
.toList()
|
.toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (ScriptException e) {
|
} catch (ScriptException e) {
|
||||||
Grasscutter.getLogger().error("Error loading group " + id + " in scene " + sceneId, e);
|
Grasscutter.getLogger().error("An error occurred while loading group " + this.id + " in scene " + sceneId + ".", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
Grasscutter.getLogger().info("group {} in scene {} is loaded successfully.", id, sceneId);
|
Grasscutter.getLogger().debug("Successfully loaded group {} in scene {}.", this.id, sceneId);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<SceneBossChest> searchBossChestInGroup() {
|
public Optional<SceneBossChest> searchBossChestInGroup() {
|
||||||
return gadgets.values().stream()
|
return this.gadgets.values().stream()
|
||||||
.filter(g -> g.boss_chest != null && g.boss_chest.monster_config_id > 0)
|
.filter(g -> g.boss_chest != null && g.boss_chest.monster_config_id > 0)
|
||||||
.map(g -> g.boss_chest)
|
.map(g -> g.boss_chest)
|
||||||
.findFirst();
|
.findFirst();
|
||||||
|
@ -43,18 +43,18 @@ public class SceneMeta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create bindings
|
// Create bindings
|
||||||
context = ScriptLoader.getEngine().createBindings();
|
this.context = ScriptLoader.getEngine().createBindings();
|
||||||
|
|
||||||
// Eval script
|
// Eval script
|
||||||
try {
|
try {
|
||||||
cs.eval(context);
|
cs.eval(this.context);
|
||||||
|
|
||||||
this.config = ScriptLoader.getSerializer().toObject(SceneConfig.class, context.get("scene_config"));
|
this.config = ScriptLoader.getSerializer().toObject(SceneConfig.class, this.context.get("scene_config"));
|
||||||
|
|
||||||
// TODO optimize later
|
// TODO optimize later
|
||||||
// Create blocks
|
// Create blocks
|
||||||
List<Integer> blockIds = ScriptLoader.getSerializer().toList(Integer.class, context.get("blocks"));
|
List<Integer> blockIds = ScriptLoader.getSerializer().toList(Integer.class, this.context.get("blocks"));
|
||||||
List<SceneBlock> blocks = ScriptLoader.getSerializer().toList(SceneBlock.class, context.get("block_rects"));
|
List<SceneBlock> blocks = ScriptLoader.getSerializer().toList(SceneBlock.class, this.context.get("block_rects"));
|
||||||
|
|
||||||
for (int i = 0; i < blocks.size(); i++) {
|
for (int i = 0; i < blocks.size(); i++) {
|
||||||
SceneBlock block = blocks.get(i);
|
SceneBlock block = blocks.get(i);
|
||||||
@ -65,11 +65,11 @@ public class SceneMeta {
|
|||||||
this.blocks = blocks.stream().collect(Collectors.toMap(b -> b.id, b -> b));
|
this.blocks = blocks.stream().collect(Collectors.toMap(b -> b.id, b -> b));
|
||||||
this.sceneBlockIndex = SceneIndexManager.buildIndex(2, blocks, SceneBlock::toRectangle);
|
this.sceneBlockIndex = SceneIndexManager.buildIndex(2, blocks, SceneBlock::toRectangle);
|
||||||
|
|
||||||
} catch (ScriptException e) {
|
} catch (ScriptException exception) {
|
||||||
Grasscutter.getLogger().error("Error running script", e);
|
Grasscutter.getLogger().error("An error occurred while running a script.", exception);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Grasscutter.getLogger().info("scene {} metadata is loaded successfully.", sceneId);
|
Grasscutter.getLogger().debug("Successfully loaded metadata in scene {}.", sceneId);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user