108 lines
4.1 KiB
Java
108 lines
4.1 KiB
Java
package com.thebrokenrail.scriptcraft.core.quickjs;
|
|
|
|
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
|
|
import com.thebrokenrail.scriptcraft.core.ScriptCraftEntrypoint;
|
|
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.Arrays;
|
|
import java.util.List;
|
|
|
|
@SuppressWarnings("unused")
|
|
public class QuickJSModules {
|
|
public static String loadModule(String name) {
|
|
String[] arr = name.split(File.separator, 2);
|
|
if (ScriptCraftCore.MOD_ID_PATTERN.matcher(arr[0]).matches()) {
|
|
List<ScriptCraftEntrypoint> mods = FabricLoader.getInstance().getEntrypoints(ScriptCraftCore.NAMESPACE, ScriptCraftEntrypoint.class);
|
|
if (arr.length == 1) {
|
|
return null;
|
|
} else {
|
|
for (ScriptCraftEntrypoint mod : mods) {
|
|
if (mod.getModID().equals(arr[0])) {
|
|
//noinspection CatchMayIgnoreException
|
|
try {
|
|
InputStream stream = mod.getClass().getResourceAsStream(File.separator + ScriptCraftCore.NAMESPACE + File.separator + arr[0] + File.separator + arr[1]);
|
|
|
|
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);
|
|
}
|
|
}
|
|
stream.close();
|
|
|
|
return textBuilder.toString();
|
|
}
|
|
} catch (IOException e) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static final String[] SUPPORTED_EXTENSION = new String[]{"", ".js", ".json", ".mjs"};
|
|
private static final String[] BUILTIN_MODULES = new String[]{"scriptcraft-core"};
|
|
|
|
public static String normalizeModule(String baseName, String name) {
|
|
if (Arrays.asList(BUILTIN_MODULES).contains(name)) {
|
|
return name;
|
|
}
|
|
|
|
String normalizedPath;
|
|
|
|
if (name.startsWith(".")) {
|
|
Path parentPath = Paths.get(baseName.replaceAll("/", File.separator)).getParent();
|
|
if (parentPath == null) {
|
|
parentPath = Paths.get(File.separator);
|
|
}
|
|
normalizedPath = Paths.get(parentPath.toString(), name.replaceAll("/", File.separator)).normalize().toString();
|
|
if (normalizedPath.charAt(0) == File.separatorChar) {
|
|
normalizedPath = normalizedPath.substring(1);
|
|
}
|
|
} else {
|
|
normalizedPath = name;
|
|
}
|
|
|
|
String result = null;
|
|
boolean success = false;
|
|
|
|
String[] arr = normalizedPath.split(File.separator, 2);
|
|
if (ScriptCraftCore.MOD_ID_PATTERN.matcher(arr[0]).matches()) {
|
|
List<ScriptCraftEntrypoint> mods = FabricLoader.getInstance().getEntrypoints(ScriptCraftCore.NAMESPACE, ScriptCraftEntrypoint.class);
|
|
if (arr.length == 1) {
|
|
for (ScriptCraftEntrypoint mod : mods) {
|
|
if (mod.getModID().equals(arr[0])) {
|
|
result = arr[0] + File.separator + mod.getModIndex();
|
|
success = true;
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
result = arr[0] + File.separator + arr[1];
|
|
success = true;
|
|
}
|
|
}
|
|
|
|
if (success) {
|
|
for (String extension : SUPPORTED_EXTENSION) {
|
|
if (loadModule(result + extension) != null) {
|
|
return result + extension;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|