Skip to content

Commit

Permalink
Merge pull request #136 from KitCat962/patch-it-hurry
Browse files Browse the repository at this point in the history
#135 broke listfiles. Oops. Fixed
  • Loading branch information
UnlikePaladin committed Dec 12, 2023
2 parents d6f6792 + dd38146 commit 5ca9061
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static void loadAvatar(Path path, UserData target) {

// scripts
loadState = LoadState.SCRIPTS;
CompoundTag scripts = loadScripts(finalPath);
CompoundTag scripts = loadScripts(finalPath, finalPath);
if (!scripts.getAllKeys().isEmpty())
nbt.put("scripts", scripts);

Expand Down Expand Up @@ -218,7 +218,7 @@ private static void matchPathsRecursive(Map<String, Path> pathMap, Path parent,
}
}

private static CompoundTag loadScripts(Path path) throws IOException {
private static CompoundTag loadScripts(Path path, Path finalPath) throws Exception {
CompoundTag nbt = new CompoundTag();
List<Path> subFiles = IOUtils.listPaths(path);
if (subFiles != null)
Expand All @@ -227,12 +227,16 @@ private static CompoundTag loadScripts(Path path) throws IOException {
continue;
String name = IOUtils.getFileNameOrEmpty(file);
if (Files.isDirectory(file)) {
CompoundTag folderNbt = loadScripts(file);
if (nbt.get(name) != null)
throw new Exception("Script \"" + name + "\" and Folder \"" + name + "\" in " + finalPath.relativize(path) + " cannot have the same name");
CompoundTag folderNbt = loadScripts(file, finalPath);
if (folderNbt.getAllKeys().isEmpty())
continue;
nbt.put(name, folderNbt);
} else if (file.toString().toLowerCase().endsWith(".lua")) {
name = name.substring(0, name.length() - 4);
if (nbt.get(name) != null)
throw new Exception("Script \"" + name + "\" and Folder \"" + name + "\" in " + finalPath.relativize(path) + " cannot have the same name");
nbt.put(name, LuaScriptParser.parseScript(name, IOUtils.readFile(file)));
}
}
Expand Down
18 changes: 10 additions & 8 deletions common/src/main/java/org/figuramc/figura/lua/FiguraLuaRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public LuaValue call(LuaValue folderPath, LuaValue includeSubfolders) {
int i = 1;
LuaTable table = new LuaTable();
for (String s : scripts.keySet()) {
Path scriptPath = PathUtils.getPath("/" + s);
Path scriptPath = PathUtils.getPath(s);

// Add to table only if the beginning of the path matches
if (!(scriptPath.startsWith(targetPath)))
Expand Down Expand Up @@ -348,28 +348,30 @@ public int read() {
// init event //

private Varargs initializeScript(String str){
Path path = PathUtils.getPath(str);
String name = PathUtils.computeSafeString(path);

// already loaded
Varargs val = loadedScripts.get(str);
Varargs val = loadedScripts.get(name);
if (val != null)
return val;

// not found
String src = scripts.get(str);
String src = scripts.get(name);
if (src == null)
throw new LuaError("Tried to require nonexistent script \"" + str + "\"!");
throw new LuaError("Tried to require nonexistent script \"" + name + "\"!");

this.loadingScripts.push(str);
this.loadingScripts.push(name);

// load
Path path = Path.of(str);
String directory = PathUtils.computeSafeString(path.getParent());
String fileName = PathUtils.computeSafeString(path.getFileName());
Varargs value = userGlobals.load(src, str).invoke(LuaValue.varargsOf(LuaValue.valueOf(directory), LuaValue.valueOf(fileName)));
Varargs value = userGlobals.load(src, name).invoke(LuaValue.varargsOf(LuaValue.valueOf(directory), LuaValue.valueOf(fileName)));
if (value == LuaValue.NIL)
value = LuaValue.TRUE;

// cache and return
loadedScripts.put(str, value);
loadedScripts.put(name, value);
loadingScripts.pop();
return value;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import net.minecraft.nbt.*;

import org.figuramc.figura.FiguraMod;
import org.figuramc.figura.config.Configs;
import org.figuramc.figura.lua.api.FileAPI;
import org.figuramc.figura.model.ParentType;
import org.figuramc.figura.model.rendering.texture.RenderTypes;
import org.figuramc.figura.utils.PathUtils;
import org.figuramc.figura.utils.Version;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -74,8 +74,7 @@ public static CompoundTag parse(String json, String filename) {
if (metadata.autoScripts != null) {
ListTag autoScripts = new ListTag();
for (String name : metadata.autoScripts) {
name = name.replaceAll("\\.lua$", "").replaceAll("[/\\\\]", ".");
autoScripts.add(StringTag.valueOf(name));
autoScripts.add(StringTag.valueOf(PathUtils.computeSafeString(name.replaceAll("\\.lua$", ""))));
}
nbt.put("autoScripts", autoScripts);
}
Expand Down
13 changes: 11 additions & 2 deletions common/src/main/java/org/figuramc/figura/utils/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class PathUtils {

public static Path getPath (String path) {
if (path.isEmpty()) return Path.of("/");
if (!path.startsWith(".")) path = "/" + path;
return Path.of(path
.replaceAll("\\\\", "/")
.replaceAll("[\\.]([^\\./])", "/$1")
Expand All @@ -23,18 +24,26 @@ public static Path getPath (LuaValue path) {
public static Path getWorkingDirectory(LuaFunction debugGetinfo) {
String file = debugGetinfo.call(LuaValue.valueOf(1)).get("source").checkjstring();
Path path = Path.of("/" + file);
return path.getNameCount() > 1 ? path.resolve("../") : Path.of("/");
return path.getNameCount() > 1 ? path.resolve("../").normalize() : Path.of("/");
}

public static String computeSafeString(Path path) {
String str = path == null ? "" : path.normalize().toString();
return str
.replaceAll("\\\\", "/")
.replaceAll("^/", "");
.replaceAll("^\\/+", "");
}

public static String computeSafeString(String path) {
return computeSafeString(getPath(path));
}

public static boolean isAbsolute(Path path) {
return path.getRoot() != null || !path.toString().startsWith(".");
}

public static boolean isAbsolute(String path) {
return isAbsolute(getPath(path));
}

}

0 comments on commit 5ca9061

Please sign in to comment.