Commit 1a5d4cf4 authored by Melledy's avatar Melledy
Browse files

Implement lazy loading of scripts when they enter a new block

parent 3af5d204
...@@ -482,8 +482,8 @@ public class Scene { ...@@ -482,8 +482,8 @@ public class Scene {
for (SceneBlock block : visible) { for (SceneBlock block : visible) {
if (!this.getLoadedBlocks().contains(block)) { if (!this.getLoadedBlocks().contains(block)) {
this.getLoadedBlocks().add(block);
this.onLoadBlock(block); this.onLoadBlock(block);
this.getLoadedBlocks().add(block);
} }
} }
} }
...@@ -491,11 +491,17 @@ public class Scene { ...@@ -491,11 +491,17 @@ public class Scene {
// TODO optimize // TODO optimize
public void onLoadBlock(SceneBlock block) { public void onLoadBlock(SceneBlock block) {
for (SceneGroup group : block.groups) { for (SceneGroup group : block.groups) {
// We load the script files for the groups here
if (!group.isLoaded()) {
this.getScriptManager().loadGroupFromScript(group);
}
group.triggers.forEach(getScriptManager()::registerTrigger); group.triggers.forEach(getScriptManager()::registerTrigger);
} }
// Spawn gadgets AFTER triggers are added
for (SceneGroup group : block.groups) { for (SceneGroup group : block.groups) {
this.getScriptManager().spawnGadgetsInGroup(block, group); this.getScriptManager().spawnGadgetsInGroup(group);
} }
} }
......
...@@ -145,7 +145,7 @@ public class SceneScriptManager { ...@@ -145,7 +145,7 @@ public class SceneScriptManager {
SceneBlock block = blocks.get(0); SceneBlock block = blocks.get(0);
block.id = blockIds.get(i); block.id = blockIds.get(i);
loadBlock(block); loadBlockFromScript(block);
} }
this.blocks = blocks; this.blocks = blocks;
...@@ -162,7 +162,7 @@ public class SceneScriptManager { ...@@ -162,7 +162,7 @@ public class SceneScriptManager {
return isInit; return isInit;
} }
private void loadBlock(SceneBlock block) { private void loadBlockFromScript(SceneBlock block) {
CompiledScript cs = ScriptLoader.getScriptByPath( CompiledScript cs = ScriptLoader.getScriptByPath(
Grasscutter.getConfig().SCRIPTS_FOLDER + "Scene/" + getScene().getId() + "/scene" + getScene().getId() + "_block" + block.id + "." + ScriptLoader.getScriptType()); Grasscutter.getConfig().SCRIPTS_FOLDER + "Scene/" + getScene().getId() + "/scene" + getScene().getId() + "_block" + block.id + "." + ScriptLoader.getScriptType());
...@@ -176,13 +176,16 @@ public class SceneScriptManager { ...@@ -176,13 +176,16 @@ public class SceneScriptManager {
// Set groups // Set groups
block.groups = ScriptLoader.getSerializer().toList(SceneGroup.class, bindings.get("groups")); block.groups = ScriptLoader.getSerializer().toList(SceneGroup.class, bindings.get("groups"));
block.groups.forEach(this::loadGroup); block.groups.forEach(g -> g.block_id = block.id);
} catch (ScriptException e) { } catch (ScriptException e) {
Grasscutter.getLogger().error("Error loading block " + block.id + " in scene " + getScene().getId(), e); Grasscutter.getLogger().error("Error loading block " + block.id + " in scene " + getScene().getId(), e);
} }
} }
private void loadGroup(SceneGroup group) { public void loadGroupFromScript(SceneGroup group) {
// Set flag here so if there is no script, we dont call this function over and over again.
group.setLoaded(true);
CompiledScript cs = ScriptLoader.getScriptByPath( CompiledScript cs = ScriptLoader.getScriptByPath(
Grasscutter.getConfig().SCRIPTS_FOLDER + "Scene/" + getScene().getId() + "/scene" + getScene().getId() + "_group" + group.id + "." + ScriptLoader.getScriptType()); Grasscutter.getConfig().SCRIPTS_FOLDER + "Scene/" + getScene().getId() + "/scene" + getScene().getId() + "_group" + group.id + "." + ScriptLoader.getScriptType());
...@@ -213,13 +216,13 @@ public class SceneScriptManager { ...@@ -213,13 +216,13 @@ public class SceneScriptManager {
} }
public void spawnGadgetsInGroup(SceneBlock block, SceneGroup group) { public void spawnGadgetsInGroup(SceneGroup group) {
for (SceneGadget g : group.gadgets) { for (SceneGadget g : group.gadgets) {
EntityGadget entity = new EntityGadget(getScene(), g.gadget_id, g.pos); EntityGadget entity = new EntityGadget(getScene(), g.gadget_id, g.pos);
if (entity.getGadgetData() == null) continue; if (entity.getGadgetData() == null) continue;
entity.setBlockId(block.id); entity.setBlockId(group.block_id);
entity.setConfigId(g.config_id); entity.setConfigId(g.config_id);
entity.setGroupId(group.id); entity.setGroupId(group.id);
entity.getRotation().set(g.rot); entity.getRotation().set(g.rot);
......
...@@ -5,6 +5,8 @@ import java.util.List; ...@@ -5,6 +5,8 @@ import java.util.List;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
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 int id; public int id;
public int refresh_id; public int refresh_id;
public Position pos; public Position pos;
...@@ -14,4 +16,14 @@ public class SceneGroup { ...@@ -14,4 +16,14 @@ public class SceneGroup {
public List<SceneTrigger> triggers; public List<SceneTrigger> triggers;
public List<SceneSuite> suites; public List<SceneSuite> suites;
public SceneInitConfig init_config; public SceneInitConfig init_config;
private transient boolean isLoaded; // Not an actual variable in the scripts either
public boolean isLoaded() {
return isLoaded;
}
public boolean setLoaded(boolean loaded) {
return loaded;
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment