This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/core/quickjs/QuickJSModules.java

92 lines
3.4 KiB
Java
Raw Normal View History

2020-04-28 00:15:24 +00:00
package com.thebrokenrail.scriptcraft.core.quickjs;
2020-04-25 13:33:17 +00:00
2020-04-28 00:15:24 +00:00
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
2020-05-24 18:09:25 +00:00
import com.thebrokenrail.scriptcraft.core.ScriptCraftEntryPoint;
2020-04-25 13:33:17 +00:00
import net.fabricmc.loader.api.FabricLoader;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
@SuppressWarnings("unused")
2020-04-28 00:15:24 +00:00
public class QuickJSModules {
2020-05-26 14:12:45 +00:00
private static boolean isDirectory(String data, String path, Class<?> clazz) {
String[] lines = data.split("\n");
for (String line : lines) {
InputStream stream = clazz.getResourceAsStream(path + File.separator + line);
return stream != null;
}
return false;
}
2020-04-25 13:33:17 +00:00
public static String loadModule(String name) {
2020-05-24 18:09:25 +00:00
List<ScriptCraftEntryPoint> mods = FabricLoader.getInstance().getEntrypoints(ScriptCraftCore.NAMESPACE, ScriptCraftEntryPoint.class);
for (ScriptCraftEntryPoint mod : mods) {
//noinspection CatchMayIgnoreException
try {
2020-05-26 14:12:45 +00:00
String path = File.separator + ScriptCraftCore.NAMESPACE + File.separator + name;
InputStream stream = mod.getClass().getResourceAsStream(path);
2020-04-25 13:33:17 +00:00
2020-05-24 18:09:25 +00:00
if (stream != null) {
StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader(stream))) {
int c;
while ((c = reader.read()) != -1) {
textBuilder.append((char) c);
2020-04-25 13:33:17 +00:00
}
}
2020-05-24 18:09:25 +00:00
stream.close();
2020-05-26 14:12:45 +00:00
return isDirectory(textBuilder.toString(), path, mod.getClass()) ? null : textBuilder.toString();
2020-04-25 13:33:17 +00:00
}
2020-05-24 18:09:25 +00:00
} catch (IOException e) {
2020-04-25 13:33:17 +00:00
}
}
return null;
}
2020-05-24 18:09:25 +00:00
private static final String[] SUPPORTED_EXTENSIONS;
static {
final String[] supportedExtensions = new String[]{"", ".js", ".json", ".mjs"};
SUPPORTED_EXTENSIONS = new String[supportedExtensions.length * 2];
for (int i = 0; i < supportedExtensions.length; i++) {
SUPPORTED_EXTENSIONS[i + supportedExtensions.length] = File.separator + "index" + supportedExtensions[i];
SUPPORTED_EXTENSIONS[i] = supportedExtensions[i];
}
}
2020-04-25 13:33:17 +00:00
public static String normalizeModule(String baseName, String name) {
String normalizedPath;
2020-05-24 18:09:25 +00:00
baseName = baseName.replaceAll("/", File.separator);
name = name.replaceAll("/", File.separator);
2020-04-25 13:33:17 +00:00
if (name.startsWith(".")) {
2020-05-24 18:09:25 +00:00
Path parentPath = Paths.get(baseName).getParent();
2020-04-25 13:33:17 +00:00
if (parentPath == null) {
parentPath = Paths.get(File.separator);
}
2020-05-24 18:09:25 +00:00
normalizedPath = Paths.get(parentPath.toString(), name).normalize().toString();
2020-04-25 13:33:17 +00:00
if (normalizedPath.charAt(0) == File.separatorChar) {
normalizedPath = normalizedPath.substring(1);
}
} else {
normalizedPath = name;
}
2020-05-24 18:09:25 +00:00
for (String extension : SUPPORTED_EXTENSIONS) {
if (loadModule(normalizedPath + extension) != null) {
return normalizedPath + extension;
2020-04-25 13:33:17 +00:00
}
}
2020-05-26 14:12:45 +00:00
return normalizedPath;
2020-04-25 13:33:17 +00:00
}
}