Skip to content

Commit d34cb2a

Browse files
committed
hacked up fixed require function that works now
1 parent 0491edf commit d34cb2a

4 files changed

Lines changed: 39 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ this mod uses the FiguraMC implementation of Lua.
2828
- [x] math (vectors, mainly)
2929
- other
3030
- [x] autogen documentation (actively working on)
31-
- [ ] support `require` function (and therefore libraries)
31+
- [x] support `require` function (and therefore libraries)
3232

3333
## what im not worried about right now
3434
- scripting language independence; im just focusing on lua

autodocs/autodoc.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
--#region Information
44
-- GENERATED AUTODOC
5-
-- Generated: 2026-01-29T20:35:30.443182800
5+
-- Generated: 2026-01-30T10:10:00.200472100
66
-- Luafy Version: 2.0.0
77
-- Format: Lua LS library file
88
--#endregion

src/main/java/dev/diamond/luafy/resource/ScriptResourceLoader.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
public class ScriptResourceLoader implements SimpleSynchronousResourceReloadListener {
1414

15-
private static final String PATH = "luafy/scripts";
15+
public static final String PATH = "luafy/scripts";
1616
private static final String LIBRARY_PATH = "lib";
17-
private static final String EXT = ".lua";
17+
public static final String EXT = ".lua";
1818

1919
@Override
2020
public @NotNull Identifier getFabricId() {
@@ -34,7 +34,8 @@ public void onResourceManagerReload(ResourceManager manager) {
3434
String s = new String(bytes, StandardCharsets.UTF_8);
3535

3636

37-
Identifier id = idFromBadPath(loc.getPath(), loc.getNamespace());
37+
String fixedPath = loc.getPath().substring(PATH.length() + 1, loc.getPath().length() - EXT.length());
38+
Identifier id = Identifier.fromNamespaceAndPath(loc.getNamespace(), fixedPath);
3839

3940

4041
// get script
@@ -49,9 +50,4 @@ public void onResourceManagerReload(ResourceManager manager) {
4950

5051
Luafy.LOGGER.info("Loaded {} scripts", count);
5152
}
52-
53-
public static Identifier idFromBadPath(String badPath, String namespace) {
54-
String fixedPath = badPath.substring(PATH.length() + 1, badPath.length() - EXT.length());
55-
return Identifier.fromNamespaceAndPath(namespace, fixedPath);
56-
}
5753
}

src/main/java/dev/diamond/luafy/script/LuaScript.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
import java.nio.charset.StandardCharsets;
2323
import java.util.HashMap;
2424
import java.util.concurrent.Future;
25+
import java.util.regex.Matcher;
26+
2527
import net.minecraft.commands.CommandSourceStack;
28+
import org.luaj.vm2.lib.BaseLib;
29+
import org.luaj.vm2.lib.VarArgFunction;
2630

2731
public class LuaScript {
2832

@@ -138,10 +142,36 @@ private ScriptExecutionResult executor(@NotNull CommandSourceStack src, @Nullabl
138142
}
139143

140144
private static void modifyRequire(LuaScript script) {
145+
var oldFunc = script.globals.get("require");
146+
147+
script.globals.set("require", new VarArgFunction() {
148+
@Override
149+
public Varargs invoke(Varargs args) {
150+
String out;
151+
String path = args.arg1().tojstring();
152+
if (args.narg() == 2) {
153+
String namespace = args.arg(2).tojstring();
154+
out = namespace + ":" + path;
155+
} else {
156+
out = path;
157+
}
158+
159+
return oldFunc.call(LuaValue.valueOf(out));
160+
}
161+
});
162+
141163
script.globals.finder = s -> {
142-
String namespace = ""; // todo get somehow
143-
Identifier id = ScriptResourceLoader.idFromBadPath(s, namespace);
144-
return new ByteArrayInputStream(Luafy.SCRIPT_MANAGER.get(id).source.getBytes(StandardCharsets.UTF_8));
164+
if (s.contains(":")) {
165+
String[] splits = s.split(":");
166+
String namespace = splits[0];
167+
String path = splits[1];
168+
String fixedPath = path.substring(0, path.length() - ScriptResourceLoader.EXT.length());
169+
fixedPath = fixedPath.replaceAll(Matcher.quoteReplacement("\\"), "/");
170+
Identifier id = Identifier.fromNamespaceAndPath(namespace, fixedPath);
171+
return new ByteArrayInputStream(Luafy.SCRIPT_MANAGER.get(id).source.getBytes(StandardCharsets.UTF_8));
172+
} else {
173+
return BaseLib.class.getResourceAsStream(s.startsWith("/") ? s : "/" + s);
174+
}
145175
};
146176
}
147177

0 commit comments

Comments
 (0)