-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAbstractScriptManager.java
More file actions
202 lines (170 loc) · 6.57 KB
/
AbstractScriptManager.java
File metadata and controls
202 lines (170 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package dev.amble.lib.script;
import dev.amble.lib.AmbleKit;
import dev.amble.lib.script.lua.LuaBinder;
import dev.amble.lib.script.lua.MinecraftData;
import dev.amble.lib.script.lua.SandboxedGlobals;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Abstract base class for script managers.
* Provides common functionality for loading, caching, and managing Lua scripts.
*/
public abstract class AbstractScriptManager implements SimpleSynchronousResourceReloadListener {
protected final Map<Identifier, LuaScript> cache = new HashMap<>();
protected final Map<Identifier, LuaValue> dataCache = new HashMap<>();
protected final Set<Identifier> enabledScripts = new HashSet<>();
/**
* Creates the MinecraftData instance for a script.
* Subclasses should override to provide client or server-specific data.
*/
protected abstract MinecraftData createMinecraftData();
/**
* Gets the log prefix for this script manager (e.g., "script" or "server script").
*/
protected abstract String getLogPrefix();
/**
* Reloads all scripts from the given resource manager.
*/
@Override
public void reload(ResourceManager manager) {
// Disable all scripts before clearing cache
for (Identifier id : new HashSet<>(enabledScripts)) {
disable(id);
}
cache.clear();
dataCache.clear();
// Discover all script files and populate the cache
manager.findResources("script", id -> id.getPath().endsWith(".lua"))
.keySet()
.forEach(id -> {
try {
load(id, manager);
} catch (Exception e) {
AmbleKit.LOGGER.error("Failed to load {} {}", getLogPrefix(), id, e);
}
});
AmbleKit.LOGGER.info("Loaded {} {}s", cache.size(), getLogPrefix());
}
/**
* Loads a script from the resource manager.
*/
public LuaScript load(Identifier id, ResourceManager manager) {
return cache.computeIfAbsent(id, key -> {
try {
Resource res = manager.getResource(key).orElseThrow();
// Use sandboxed globals to prevent access to dangerous APIs like luajava
Globals globals = SandboxedGlobals.create();
// Create and cache the minecraft data for this script
MinecraftData data = createMinecraftData();
data.setScriptName(key.toString());
LuaValue boundData = LuaBinder.bind(data);
dataCache.put(key, boundData);
// Inject minecraft global for scripts to use
globals.set("minecraft", boundData);
LuaValue chunk = globals.load(
new InputStreamReader(res.getInputStream()),
key.toString()
);
chunk.call();
LuaScript script = new LuaScript(globals);
// Call onRegister when script is first loaded into the manager
if (script.onRegister() != null && !script.onRegister().isnil()) {
try {
script.onRegister().call(boundData);
} catch (Exception e) {
AmbleKit.LOGGER.error("Error in onRegister for {} {}", getLogPrefix(), key, e);
}
}
return script;
} catch (Exception e) {
throw new RuntimeException("Failed to load " + getLogPrefix() + " " + key, e);
}
});
}
public Map<Identifier, LuaScript> getCache() {
return cache;
}
public Set<Identifier> getEnabledScripts() {
return enabledScripts;
}
public boolean isEnabled(Identifier id) {
return enabledScripts.contains(id);
}
public boolean enable(Identifier id) {
if (enabledScripts.contains(id)) {
return false; // Already enabled
}
LuaScript script = cache.get(id);
if (script == null) {
return false;
}
enabledScripts.add(id);
// Call onEnable with minecraft data as first argument
if (script.onEnable() != null && !script.onEnable().isnil()) {
try {
LuaValue data = dataCache.get(id);
script.onEnable().call(data);
} catch (Exception e) {
AmbleKit.LOGGER.error("Error in onEnable for {} {}", getLogPrefix(), id, e);
}
}
AmbleKit.LOGGER.info("Enabled {}: {}", getLogPrefix(), id);
return true;
}
public boolean disable(Identifier id) {
if (!enabledScripts.contains(id)) {
return false; // Not enabled
}
LuaScript script = cache.get(id);
// Call onDisable with minecraft data as first argument before removing
if (script != null && script.onDisable() != null && !script.onDisable().isnil()) {
try {
LuaValue data = dataCache.get(id);
script.onDisable().call(data);
} catch (Exception e) {
AmbleKit.LOGGER.error("Error in onDisable for {} {}", getLogPrefix(), id, e);
}
}
enabledScripts.remove(id);
AmbleKit.LOGGER.info("Disabled {}: {}", getLogPrefix(), id);
return true;
}
public boolean toggle(Identifier id) {
if (isEnabled(id)) {
return disable(id);
} else {
return enable(id);
}
}
/**
* Called each tick to update enabled scripts.
*/
public void tick() {
for (Identifier id : enabledScripts) {
LuaScript script = cache.get(id);
if (script != null && script.onTick() != null && !script.onTick().isnil()) {
try {
LuaValue data = dataCache.get(id);
script.onTick().call(data);
} catch (Exception e) {
AmbleKit.LOGGER.error("Error in onTick for {} {}", getLogPrefix(), id, e);
}
}
}
}
/**
* Get the bound minecraft data for a script.
*/
public LuaValue getScriptData(Identifier id) {
return dataCache.get(id);
}
}