Skip to content

Commit

Permalink
define classes per load stage
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Jul 10, 2023
1 parent a50c51d commit 44a773c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public GroovyScriptSandbox(URL... scriptEnvironment) {
registerBinding("EventManager", GroovyEventManager.INSTANCE);
registerBinding("eventManager", GroovyEventManager.INSTANCE);
registerBinding("event_manager", GroovyEventManager.INSTANCE);
importCustomizer.addStaticStars(GroovyHelper.class.getName(), MathHelper.class.getName());
importCustomizer.addImports("net.minecraft.world.World",
this.importCustomizer.addStaticStars(GroovyHelper.class.getName(), MathHelper.class.getName());
this.importCustomizer.addImports("net.minecraft.world.World",
"net.minecraft.block.state.IBlockState",
"net.minecraft.block.Block",
"net.minecraft.block.SoundType",
Expand Down Expand Up @@ -174,7 +174,7 @@ protected void postRun() {

@Override
public Collection<File> getClassFiles() {
return GroovyScript.getRunConfig().getClassFiles();
return GroovyScript.getRunConfig().getClassFiles(this.currentLoadStage.getName());
}

@Override
Expand Down
52 changes: 41 additions & 11 deletions src/main/java/com/cleanroommc/groovyscript/sandbox/RunConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public static JsonObject createDefaultJson() {
json.addProperty("packId", "placeholdername");
json.addProperty("version", "1.0.0");
json.addProperty("debug", false);
JsonArray classes = new JsonArray();
JsonObject classes = new JsonObject();
JsonArray preInit = new JsonArray();
classes.add("preInit", preInit);
json.add("classes", classes);
JsonObject loaders = new JsonObject();
json.add("loaders", loaders);
JsonArray preInit = new JsonArray();
preInit = new JsonArray();
loaders.add("preInit", preInit);
preInit.add("preInit/");
JsonArray postInit = new JsonArray();
Expand All @@ -55,7 +57,7 @@ public static JsonObject createDefaultJson() {
private final String packName;
private final String packId;
private final String version;
private final List<String> classes = new ArrayList<>();
private final Map<String, List<String>> classes = new Object2ObjectOpenHashMap<>();
private final Map<String, List<String>> loaderPaths = new Object2ObjectOpenHashMap<>();
// TODO pack modes
private final Map<String, List<String>> packmodePaths = new Object2ObjectOpenHashMap<>();
Expand Down Expand Up @@ -106,16 +108,32 @@ public void reload(JsonObject json) {

String regex = File.separatorChar == '\\' ? "/" : "\\\\";
String replacement = getSeparator();
JsonArray jsonClasses = JsonHelper.getJsonArray(json, "classes");
if (json.has("classes")) {
JsonElement jsonClasses = json.get("classes");

for (JsonElement element : jsonClasses) {
String path = element.getAsString().replaceAll(regex, replacement);
while (path.endsWith("/") || path.endsWith("\\")) {
path = path.substring(0, path.length() - 1);
if (jsonClasses.isJsonArray()) {
List<String> classes = this.classes.computeIfAbsent("all", key -> new ArrayList<>());
for (JsonElement element : jsonClasses.getAsJsonArray()) {
classes.add(sanitizePath(element.getAsString().replaceAll(regex, replacement)));
}
} else if (jsonClasses.isJsonObject()) {
for (Map.Entry<String, JsonElement> entry : jsonClasses.getAsJsonObject().entrySet()) {
List<String> classes = this.classes.computeIfAbsent(entry.getKey(), key -> new ArrayList<>());
if (entry.getValue().isJsonPrimitive()) {
classes.add(sanitizePath(entry.getValue().getAsString().replaceAll(regex, replacement)));
} else if (entry.getValue().isJsonArray()) {
for (JsonElement element : entry.getValue().getAsJsonArray()) {
classes.add(sanitizePath(element.getAsString().replaceAll(regex, replacement)));
}
}
if (classes.isEmpty()) {
this.classes.remove(entry.getKey());
}
}
}
classes.add(path);
}


JsonObject jsonLoaders = JsonHelper.getJsonObject(json, "loaders");
List<Pair<String, String>> pathsList = new ArrayList<>();

Expand Down Expand Up @@ -175,8 +193,13 @@ public ResourceLocation makeLoc(String name) {
return new ResourceLocation(getPackId(), name);
}

public Collection<File> getClassFiles() {
return getSortedFilesOf(this.classes);
public Collection<File> getClassFiles(String loader) {
List<String> paths = this.classes.get("all");
paths = paths == null ? new ArrayList<>() : new ArrayList<>(paths);
if (this.classes.containsKey(loader)) {
paths.addAll(this.classes.get(loader));
}
return getSortedFilesOf(paths);
}

public Collection<File> getSortedFiles(String loader) {
Expand Down Expand Up @@ -216,6 +239,13 @@ private Collection<File> getSortedFilesOf(Collection<String> paths) {
return files.keySet().stream().map(file -> mainPath.relativize(file.toPath()).toFile()).collect(Collectors.toList());
}

private static String sanitizePath(String path) {
while (path.endsWith("/") || path.endsWith("\\")) {
path = path.substring(0, path.length() - 1);
}
return path;
}

private static String getSeparator() {
return File.separatorChar == '\\' ? "\\\\" : File.separator;
}
Expand Down

0 comments on commit 44a773c

Please sign in to comment.