mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2026-03-29 03:52:37 +02:00
Enable script in big world (#884)
* add docs for tower * fix: LEAK: ByteBuf.release() was not called * enableScriptInBigWorld * not print log when loaded scripts from cache * revert the change of server tick * revert the change of server tick * fix * optimize the performance: lazy load & cache * fix the refresh group * fix NPE Co-authored-by: Melledy <52122272+Melledy@users.noreply.github.com>
This commit is contained in:
@@ -1,17 +1,69 @@
|
||||
package emu.grasscutter.scripts.data;
|
||||
|
||||
import ch.ethz.globis.phtree.PhTree;
|
||||
import ch.ethz.globis.phtree.v16.PhTree16;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.scripts.SceneIndexManager;
|
||||
import emu.grasscutter.scripts.ScriptLoader;
|
||||
import emu.grasscutter.utils.Position;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptException;
|
||||
import java.util.List;
|
||||
|
||||
import emu.grasscutter.utils.Position;
|
||||
import static emu.grasscutter.Configuration.SCRIPT;
|
||||
|
||||
public class SceneBlock {
|
||||
public int id;
|
||||
public Position max;
|
||||
public Position min;
|
||||
|
||||
public int sceneId;
|
||||
public List<SceneGroup> groups;
|
||||
|
||||
public PhTree<SceneGroup> sceneGroupIndex = new PhTree16<>(3);
|
||||
|
||||
private transient boolean loaded; // Not an actual variable in the scripts either
|
||||
|
||||
public boolean isLoaded() {
|
||||
return loaded;
|
||||
}
|
||||
|
||||
public void setLoaded(boolean loaded) {
|
||||
this.loaded = loaded;
|
||||
}
|
||||
|
||||
public boolean contains(Position pos) {
|
||||
return pos.getX() <= max.getX() && pos.getX() >= min.getX() &&
|
||||
pos.getZ() <= max.getZ() && pos.getZ() >= min.getZ();
|
||||
}
|
||||
}
|
||||
|
||||
public SceneBlock load(int sceneId, Bindings bindings){
|
||||
if(loaded){
|
||||
return this;
|
||||
}
|
||||
this.sceneId = sceneId;
|
||||
setLoaded(true);
|
||||
|
||||
CompiledScript cs = ScriptLoader.getScriptByPath(
|
||||
SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "_block" + id + "." + ScriptLoader.getScriptType()));
|
||||
|
||||
if (cs == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Eval script
|
||||
try {
|
||||
cs.eval(bindings);
|
||||
|
||||
// Set groups
|
||||
groups = ScriptLoader.getSerializer().toList(SceneGroup.class, bindings.get("groups"));
|
||||
groups.forEach(g -> g.block_id = id);
|
||||
SceneIndexManager.buildIndex(this.sceneGroupIndex, groups, g -> g.pos.toLongArray());
|
||||
} catch (ScriptException e) {
|
||||
Grasscutter.getLogger().error("Error loading block " + id + " in scene " + sceneId, e);
|
||||
}
|
||||
Grasscutter.getLogger().info("scene {} block {} is loaded successfully.", sceneId, id);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,6 @@
|
||||
package emu.grasscutter.scripts.data;
|
||||
|
||||
import emu.grasscutter.utils.Position;
|
||||
|
||||
public class SceneGadget {
|
||||
public int level;
|
||||
public int config_id;
|
||||
public class SceneGadget extends SceneObject{
|
||||
public int gadget_id;
|
||||
public int state;
|
||||
public Position pos;
|
||||
public Position rot;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
package emu.grasscutter.scripts.data;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.scripts.ScriptLoader;
|
||||
import emu.grasscutter.utils.Position;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static emu.grasscutter.Configuration.SCRIPTS_FOLDER;
|
||||
|
||||
public class SceneGroup {
|
||||
public transient int block_id; // Not an actual variable in the scripts but we will keep it here for reference
|
||||
@@ -21,18 +32,94 @@ public class SceneGroup {
|
||||
public List<SceneRegion> regions;
|
||||
public List<SceneSuite> suites;
|
||||
public SceneInitConfig init_config;
|
||||
|
||||
private transient boolean isLoaded; // Not an actual variable in the scripts either
|
||||
|
||||
public List<SceneVar> variables;
|
||||
private transient boolean loaded; // Not an actual variable in the scripts either
|
||||
|
||||
public boolean isLoaded() {
|
||||
return isLoaded;
|
||||
}
|
||||
|
||||
public boolean setLoaded(boolean loaded) {
|
||||
return loaded;
|
||||
}
|
||||
|
||||
public void setLoaded(boolean loaded) {
|
||||
this.loaded = loaded;
|
||||
}
|
||||
|
||||
private transient CompiledScript script; // Not an actual variable in the scripts either
|
||||
|
||||
public CompiledScript getScript() {
|
||||
return script;
|
||||
}
|
||||
|
||||
public SceneSuite getSuiteByIndex(int index) {
|
||||
return suites.get(index - 1);
|
||||
}
|
||||
|
||||
public SceneGroup load(int sceneId, Bindings bindings){
|
||||
if(loaded){
|
||||
return this;
|
||||
}
|
||||
// Set flag here so if there is no script, we dont call this function over and over again.
|
||||
setLoaded(true);
|
||||
|
||||
CompiledScript cs = ScriptLoader.getScriptByPath(
|
||||
SCRIPTS_FOLDER + "Scene/" + sceneId + "/scene" + sceneId + "_group" + id + "." + ScriptLoader.getScriptType());
|
||||
|
||||
if (cs == null) {
|
||||
return this;
|
||||
}
|
||||
|
||||
this.script = cs;
|
||||
// Eval script
|
||||
try {
|
||||
cs.eval(bindings);
|
||||
|
||||
// Set
|
||||
monsters = ScriptLoader.getSerializer().toList(SceneMonster.class, bindings.get("monsters")).stream()
|
||||
.collect(Collectors.toMap(x -> x.config_id, y -> y));
|
||||
gadgets = ScriptLoader.getSerializer().toList(SceneGadget.class, bindings.get("gadgets"));
|
||||
triggers = ScriptLoader.getSerializer().toList(SceneTrigger.class, bindings.get("triggers"));
|
||||
suites = ScriptLoader.getSerializer().toList(SceneSuite.class, bindings.get("suites"));
|
||||
regions = ScriptLoader.getSerializer().toList(SceneRegion.class, bindings.get("regions"));
|
||||
init_config = ScriptLoader.getSerializer().toObject(SceneInitConfig.class, bindings.get("init_config"));
|
||||
|
||||
// Add variables to suite
|
||||
variables = ScriptLoader.getSerializer().toList(SceneVar.class, bindings.get("variables"));
|
||||
|
||||
|
||||
// Add monsters to suite TODO optimize
|
||||
Int2ObjectMap<Object> map = new Int2ObjectOpenHashMap<>();
|
||||
monsters.entrySet().forEach(m -> map.put(m.getValue().config_id, m));
|
||||
monsters.values().forEach(m -> m.groupId = id);
|
||||
gadgets.forEach(m -> map.put(m.config_id, m));
|
||||
gadgets.forEach(m -> m.groupId = id);
|
||||
|
||||
for (SceneSuite suite : suites) {
|
||||
suite.sceneMonsters = new ArrayList<>(suite.monsters.size());
|
||||
suite.monsters.forEach(id -> {
|
||||
Object objEntry = map.get(id.intValue());
|
||||
if (objEntry instanceof Map.Entry<?,?> monsterEntry) {
|
||||
Object monster = monsterEntry.getValue();
|
||||
if(monster instanceof SceneMonster sceneMonster){
|
||||
suite.sceneMonsters.add(sceneMonster);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
suite.sceneGadgets = new ArrayList<>(suite.gadgets.size());
|
||||
for (int id : suite.gadgets) {
|
||||
try {
|
||||
SceneGadget gadget = (SceneGadget) map.get(id);
|
||||
if (gadget != null) {
|
||||
suite.sceneGadgets.add(gadget);
|
||||
}
|
||||
} catch (Exception ignored) { }
|
||||
}
|
||||
}
|
||||
|
||||
} catch (ScriptException e) {
|
||||
Grasscutter.getLogger().error("Error loading group " + id + " in scene " + sceneId, e);
|
||||
}
|
||||
Grasscutter.getLogger().info("group {} in scene {} is loaded successfully.", id, sceneId);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
71
src/main/java/emu/grasscutter/scripts/data/SceneMeta.java
Normal file
71
src/main/java/emu/grasscutter/scripts/data/SceneMeta.java
Normal file
@@ -0,0 +1,71 @@
|
||||
package emu.grasscutter.scripts.data;
|
||||
|
||||
import ch.ethz.globis.phtree.PhTree;
|
||||
import ch.ethz.globis.phtree.v16.PhTree16;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.scripts.SceneIndexManager;
|
||||
import emu.grasscutter.scripts.ScriptLoader;
|
||||
|
||||
import javax.script.Bindings;
|
||||
import javax.script.CompiledScript;
|
||||
import javax.script.ScriptException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static emu.grasscutter.Configuration.SCRIPT;
|
||||
|
||||
public class SceneMeta {
|
||||
|
||||
public SceneConfig config;
|
||||
public Map<Integer, SceneBlock> blocks;
|
||||
|
||||
public Bindings context;
|
||||
|
||||
public PhTree<SceneBlock> sceneBlockIndex = new PhTree16<>(2);
|
||||
|
||||
public static SceneMeta of(int sceneId) {
|
||||
return new SceneMeta().load(sceneId);
|
||||
}
|
||||
|
||||
public SceneMeta load(int sceneId){
|
||||
// Get compiled script if cached
|
||||
CompiledScript cs = ScriptLoader.getScriptByPath(
|
||||
SCRIPT("Scene/" + sceneId + "/scene" + sceneId + "." + ScriptLoader.getScriptType()));
|
||||
|
||||
if (cs == null) {
|
||||
Grasscutter.getLogger().warn("No script found for scene " + sceneId);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Create bindings
|
||||
context = ScriptLoader.getEngine().createBindings();
|
||||
|
||||
// Eval script
|
||||
try {
|
||||
cs.eval(context);
|
||||
|
||||
this.config = ScriptLoader.getSerializer().toObject(SceneConfig.class, context.get("scene_config"));
|
||||
|
||||
// TODO optimize later
|
||||
// Create blocks
|
||||
List<Integer> blockIds = ScriptLoader.getSerializer().toList(Integer.class, context.get("blocks"));
|
||||
List<SceneBlock> blocks = ScriptLoader.getSerializer().toList(SceneBlock.class, context.get("block_rects"));
|
||||
|
||||
for (int i = 0; i < blocks.size(); i++) {
|
||||
SceneBlock block = blocks.get(i);
|
||||
block.id = blockIds.get(i);
|
||||
|
||||
}
|
||||
|
||||
this.blocks = blocks.stream().collect(Collectors.toMap(b -> b.id, b -> b));
|
||||
SceneIndexManager.buildIndex(this.sceneBlockIndex, blocks, g -> g.min.toXZLongArray());
|
||||
SceneIndexManager.buildIndex(this.sceneBlockIndex, blocks, g -> g.max.toXZLongArray());
|
||||
} catch (ScriptException e) {
|
||||
Grasscutter.getLogger().error("Error running script", e);
|
||||
return null;
|
||||
}
|
||||
Grasscutter.getLogger().info("scene {} metadata is loaded successfully.", sceneId);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,5 @@
|
||||
package emu.grasscutter.scripts.data;
|
||||
|
||||
import emu.grasscutter.utils.Position;
|
||||
|
||||
public class SceneMonster {
|
||||
public int level;
|
||||
public int config_id;
|
||||
public class SceneMonster extends SceneObject{
|
||||
public int monster_id;
|
||||
public Position pos;
|
||||
public Position rot;
|
||||
}
|
||||
}
|
||||
15
src/main/java/emu/grasscutter/scripts/data/SceneObject.java
Normal file
15
src/main/java/emu/grasscutter/scripts/data/SceneObject.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package emu.grasscutter.scripts.data;
|
||||
|
||||
import emu.grasscutter.utils.Position;
|
||||
|
||||
public class SceneObject {
|
||||
public int level;
|
||||
public int config_id;
|
||||
|
||||
public Position pos;
|
||||
public Position rot;
|
||||
/**
|
||||
* not set by lua
|
||||
*/
|
||||
public int groupId;
|
||||
}
|
||||
Reference in New Issue
Block a user