mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2026-05-23 12:49:51 +08:00
Add events to support scene group substitution (#2413)
* Add events to support scene group substitution * make event members private with getter/setter * delete stray unused var
This commit is contained in:
committed by
GitHub
Unverified
parent
269f7b4fbf
commit
cf6fb275be
@@ -17,6 +17,7 @@ import emu.grasscutter.net.proto.VisionTypeOuterClass;
|
||||
import emu.grasscutter.scripts.constants.EventType;
|
||||
import emu.grasscutter.scripts.data.*;
|
||||
import emu.grasscutter.scripts.service.*;
|
||||
import emu.grasscutter.server.event.game.SceneMetaLoadEvent;
|
||||
import emu.grasscutter.server.packet.send.PacketGroupSuiteNotify;
|
||||
import emu.grasscutter.utils.*;
|
||||
import io.netty.util.concurrent.FastThreadLocalThread;
|
||||
@@ -38,6 +39,7 @@ public class SceneScriptManager {
|
||||
private final Map<String, Integer> variables;
|
||||
private SceneMeta meta;
|
||||
private boolean isInit;
|
||||
private boolean noCacheGroupGridsToDisk;
|
||||
|
||||
private final Map<String, SceneTimeAxis> timeAxis = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -134,7 +136,7 @@ public class SceneScriptManager {
|
||||
public void registerTrigger(SceneTrigger trigger) {
|
||||
this.triggerInvocations.put(trigger.getName(), new AtomicInteger(0));
|
||||
this.getTriggersByEvent(trigger.getEvent()).add(trigger);
|
||||
Grasscutter.getLogger().trace("Registered trigger {}", trigger.getName());
|
||||
Grasscutter.getLogger().trace("Registered trigger {} from group {}", trigger.getName(), trigger.getCurrentGroup().id);
|
||||
}
|
||||
|
||||
public void deregisterTrigger(List<SceneTrigger> triggers) {
|
||||
@@ -143,7 +145,7 @@ public class SceneScriptManager {
|
||||
|
||||
public void deregisterTrigger(SceneTrigger trigger) {
|
||||
this.getTriggersByEvent(trigger.getEvent()).remove(trigger);
|
||||
Grasscutter.getLogger().trace("deregistered trigger {}", trigger.getName());
|
||||
Grasscutter.getLogger().trace("deregistered trigger {} from group {}", trigger.getName(), trigger.getCurrentGroup().id);
|
||||
}
|
||||
|
||||
public void resetTriggers(int eventId) {
|
||||
@@ -438,6 +440,17 @@ public class SceneScriptManager {
|
||||
}
|
||||
|
||||
private void init() {
|
||||
var event = new SceneMetaLoadEvent(getScene());
|
||||
event.call();
|
||||
|
||||
if (event.isOverride()) {
|
||||
// Group grids should not be cached to disk when a scene
|
||||
// group override is in effect. Otherwise, when the server
|
||||
// next runs without that override, the cached content
|
||||
// will not make sense.
|
||||
noCacheGroupGridsToDisk = true;
|
||||
}
|
||||
|
||||
var meta = ScriptLoader.getSceneMeta(getScene().getId());
|
||||
if (meta == null) {
|
||||
return;
|
||||
@@ -455,7 +468,7 @@ public class SceneScriptManager {
|
||||
return groupGridsCache.get(sceneId);
|
||||
} else {
|
||||
var path = FileUtils.getCachePath("scene" + sceneId + "_grid.json");
|
||||
if (path.toFile().isFile() && !Grasscutter.config.server.game.cacheSceneEntitiesEveryRun) {
|
||||
if (path.toFile().isFile() && !Grasscutter.config.server.game.cacheSceneEntitiesEveryRun && !noCacheGroupGridsToDisk) {
|
||||
try {
|
||||
var groupGrids = JsonUtils.loadToList(path, Grid.class);
|
||||
groupGridsCache.put(sceneId, groupGrids);
|
||||
@@ -585,15 +598,17 @@ public class SceneScriptManager {
|
||||
}
|
||||
groupGridsCache.put(scene.getId(), groupGrids);
|
||||
|
||||
try {
|
||||
Files.createDirectories(path.getParent());
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
try (var file = new FileWriter(path.toFile())) {
|
||||
file.write(JsonUtils.encode(groupGrids));
|
||||
Grasscutter.getLogger().info("Scene {} saved grid file.", getScene().getId());
|
||||
} catch (Exception e) {
|
||||
Grasscutter.getLogger().error("Scene {} unable to save grid file.", getScene().getId(), e);
|
||||
if (!noCacheGroupGridsToDisk) {
|
||||
try {
|
||||
Files.createDirectories(path.getParent());
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
try (var file = new FileWriter(path.toFile())) {
|
||||
file.write(JsonUtils.encode(groupGrids));
|
||||
Grasscutter.getLogger().info("Scene {} saved grid file.", getScene().getId());
|
||||
} catch (Exception e) {
|
||||
Grasscutter.getLogger().error("Scene {} unable to save grid file.", getScene().getId(), e);
|
||||
}
|
||||
}
|
||||
return groupGrids;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import emu.grasscutter.scripts.serializer.*;
|
||||
import emu.grasscutter.utils.FileUtils;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -172,6 +172,17 @@ public class ScriptLoader {
|
||||
* @return The sources of the script.
|
||||
*/
|
||||
public static String readScript(String path) {
|
||||
return readScript(path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the sources of a script.
|
||||
*
|
||||
* @param path The path of the script.
|
||||
* @param useAbsPath Use path as-is; don't look under Scripts resources.
|
||||
* @return The sources of the script.
|
||||
*/
|
||||
public static String readScript(String path, boolean useAbsPath) {
|
||||
// Check if the path is cached.
|
||||
var cached = ScriptLoader.tryGet(ScriptLoader.scriptSources.get(path));
|
||||
if (cached.isPresent()) {
|
||||
@@ -179,8 +190,11 @@ public class ScriptLoader {
|
||||
}
|
||||
|
||||
// Attempt to load the script.
|
||||
var scriptPath = FileUtils.getScriptPath(path);
|
||||
if (!Files.exists(scriptPath)) return null;
|
||||
var scriptPath = useAbsPath ? Paths.get(path) : FileUtils.getScriptPath(path);
|
||||
if (!Files.exists(scriptPath)) {
|
||||
Grasscutter.getLogger().error("Could not find script at path {}", path);
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
var source = Files.readString(scriptPath);
|
||||
@@ -201,6 +215,17 @@ public class ScriptLoader {
|
||||
* @return The compiled script.
|
||||
*/
|
||||
public static CompiledScript getScript(String path) {
|
||||
return getScript(path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a script and compiles it, or uses the cached varient.
|
||||
*
|
||||
* @param path The path of the script.
|
||||
* @param useAbsPath Use path as-is; don't look under Scripts resources.
|
||||
* @return The compiled script.
|
||||
*/
|
||||
public static CompiledScript getScript(String path, boolean useAbsPath) {
|
||||
// Check if the script is cached.
|
||||
var sc = ScriptLoader.tryGet(ScriptLoader.scriptsCache.get(path));
|
||||
if (sc.isPresent()) {
|
||||
@@ -211,15 +236,18 @@ public class ScriptLoader {
|
||||
CompiledScript script;
|
||||
if (Configuration.FAST_REQUIRE) {
|
||||
// Attempt to load the script.
|
||||
var scriptPath = FileUtils.getScriptPath(path);
|
||||
if (!Files.exists(scriptPath)) return null;
|
||||
var scriptPath = useAbsPath ? Paths.get(path) : FileUtils.getScriptPath(path);
|
||||
if (!Files.exists(scriptPath)) {
|
||||
Grasscutter.getLogger().error("Could not find script at path {}", path);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Compile the script from the file.
|
||||
var source = Files.newBufferedReader(scriptPath);
|
||||
script = ScriptLoader.getEngine().compile(source);
|
||||
} else {
|
||||
// Load the script sources.
|
||||
var sources = ScriptLoader.readScript(path);
|
||||
var sources = ScriptLoader.readScript(path, useAbsPath);
|
||||
if (sources == null) return null;
|
||||
|
||||
// Check to see if the script references other scripts.
|
||||
@@ -237,7 +265,7 @@ public class ScriptLoader {
|
||||
var scriptName = line.substring(9, line.length() - 1);
|
||||
// Resolve the script path.
|
||||
var scriptPath = "Common/" + scriptName + ".lua";
|
||||
var scriptSource = ScriptLoader.readScript(scriptPath);
|
||||
var scriptSource = ScriptLoader.readScript(scriptPath, useAbsPath);
|
||||
if (scriptSource == null) continue;
|
||||
|
||||
// Append the script source.
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.github.davidmoten.rtreemulti.geometry.*;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.game.world.Position;
|
||||
import emu.grasscutter.scripts.*;
|
||||
import emu.grasscutter.server.event.game.SceneBlockLoadedEvent;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.script.*;
|
||||
@@ -64,6 +65,10 @@ public class SceneBlock {
|
||||
.collect(Collectors.toMap(x -> x.id, y -> y, (a, b) -> a));
|
||||
|
||||
this.groups.values().forEach(g -> g.block_id = this.id);
|
||||
|
||||
var event = new SceneBlockLoadedEvent(this);
|
||||
event.call();
|
||||
|
||||
this.sceneGroupIndex =
|
||||
SceneIndexManager.buildIndex(3, this.groups.values(), g -> g.pos.toPoint());
|
||||
} catch (ScriptException exception) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package emu.grasscutter.scripts.data;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.game.world.Position;
|
||||
import emu.grasscutter.scripts.ScriptLoader;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.script.*;
|
||||
@@ -39,6 +40,7 @@ public final class SceneGroup {
|
||||
private transient boolean loaded;
|
||||
private transient CompiledScript script;
|
||||
private transient Bindings bindings;
|
||||
public String overrideScriptPath;
|
||||
|
||||
public static SceneGroup of(int groupId) {
|
||||
var group = new SceneGroup();
|
||||
@@ -86,8 +88,12 @@ public final class SceneGroup {
|
||||
// Create the bindings.
|
||||
this.bindings = ScriptLoader.getEngine().createBindings();
|
||||
|
||||
var cs =
|
||||
ScriptLoader.getScript("Scene/%s/scene%s_group%s.lua".formatted(sceneId, sceneId, this.id));
|
||||
CompiledScript cs;
|
||||
if (overrideScriptPath != null && !overrideScriptPath.equals("")) {
|
||||
cs = ScriptLoader.getScript(overrideScriptPath, true);
|
||||
} else {
|
||||
cs = ScriptLoader.getScript("Scene/%s/scene%s_group%s.lua".formatted(sceneId, sceneId, this.id));
|
||||
}
|
||||
|
||||
if (cs == null) {
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user