From 7af0d425f84c57a608cd5d0718e9745a9cbd9519 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Sun, 30 Oct 2022 12:48:52 +0200 Subject: [PATCH] Fix variable resolving in Forge run configs --- .../providers/forge/ForgeRunsProvider.java | 163 +++++++++--------- 1 file changed, 79 insertions(+), 84 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeRunsProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeRunsProvider.java index bf30cce2..4634feab 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeRunsProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/forge/ForgeRunsProvider.java @@ -78,88 +78,88 @@ public class ForgeRunsProvider implements ConfigValue.Resolver { return new ForgeRunsProvider(project, json); } - public String processTemplates(String string) { - if (string.startsWith("{")) { - String key = string.substring(1, string.length() - 1); + @Override + public String resolve(ConfigValue.Variable variable) { + String key = variable.name(); + String string = '{' + key + '}'; - // TODO: Look into ways to not hardcode - if (key.equals("runtime_classpath")) { - string = runtimeClasspath().stream() - .map(File::getAbsolutePath) - .collect(Collectors.joining(File.pathSeparator)); - } else if (key.equals("minecraft_classpath")) { - string = minecraftClasspath().stream() - .map(File::getAbsolutePath) - .collect(Collectors.joining(File.pathSeparator)); - } else if (key.equals("runtime_classpath_file")) { - Path path = extension.getFiles().getProjectPersistentCache().toPath().resolve("forge_runtime_classpath.txt"); + // TODO: Look into ways to not hardcode + if (key.equals("runtime_classpath")) { + string = runtimeClasspath().stream() + .map(File::getAbsolutePath) + .collect(Collectors.joining(File.pathSeparator)); + } else if (key.equals("minecraft_classpath")) { + string = minecraftClasspath().stream() + .map(File::getAbsolutePath) + .collect(Collectors.joining(File.pathSeparator)); + } else if (key.equals("runtime_classpath_file")) { + Path path = extension.getFiles().getProjectPersistentCache().toPath().resolve("forge_runtime_classpath.txt"); - try { - Files.writeString(path, runtimeClasspath().stream() - .map(File::getAbsolutePath) - .collect(Collectors.joining("\n")), - StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); - } catch (IOException e) { - throw new RuntimeException(e); - } - - string = path.toAbsolutePath().toString(); - } else if (key.equals("minecraft_classpath_file")) { - Path path = extension.getFiles().getProjectPersistentCache().toPath().resolve("forge_minecraft_classpath.txt"); - - try { - Files.writeString(path, minecraftClasspath().stream() - .map(File::getAbsolutePath) - .collect(Collectors.joining("\n")), - StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); - } catch (IOException e) { - throw new RuntimeException(e); - } - - string = path.toAbsolutePath().toString(); - } else if (key.equals("asset_index")) { - string = extension.getMinecraftProvider().getVersionInfo().assetIndex().fabricId(extension.getMinecraftProvider().minecraftVersion()); - } else if (key.equals("assets_root")) { - string = new File(extension.getFiles().getUserCache(), "assets").getAbsolutePath(); - } else if (key.equals("natives")) { - string = extension.getFiles().getNativesDirectory(project).getAbsolutePath(); - } else if (key.equals("source_roots")) { - // Use a set-valued multimap for deduplicating paths. - Multimap modClasses = MultimapBuilder.hashKeys().linkedHashSetValues().build(); - - for (ModSettings mod : extension.getMods()) { - for (File file : SourceSetHelper.getClasspath(mod, project)) { - modClasses.put(mod.getName(), file.getAbsolutePath()); - } - } - - string = modClasses.entries().stream() - .map(entry -> entry.getKey() + "%%" + entry.getValue()) - .collect(Collectors.joining(File.pathSeparator)); - } else if (key.equals("mcp_mappings")) { - string = "loom.stub"; - } else if (json.has(key)) { - JsonElement element = json.get(key); - - if (element.isJsonArray()) { - string = StreamSupport.stream(element.getAsJsonArray().spliterator(), false) - .map(JsonElement::getAsString) - .flatMap(str -> { - if (str.contains(":")) { - return DependencyDownloader.download(project, str, false, false).getFiles().stream() - .map(File::getAbsolutePath) - .filter(dep -> !dep.contains("bootstraplauncher")); // TODO: Hack - } - - return Stream.of(str); - }) - .collect(Collectors.joining(File.pathSeparator)); - } else { - string = element.toString(); - } - } else { - project.getLogger().warn("Unrecognized template! " + string); + try { + Files.writeString(path, runtimeClasspath().stream() + .map(File::getAbsolutePath) + .collect(Collectors.joining("\n")), + StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + } catch (IOException e) { + throw new RuntimeException(e); } + + string = path.toAbsolutePath().toString(); + } else if (key.equals("minecraft_classpath_file")) { + Path path = extension.getFiles().getProjectPersistentCache().toPath().resolve("forge_minecraft_classpath.txt"); + + try { + Files.writeString(path, minecraftClasspath().stream() + .map(File::getAbsolutePath) + .collect(Collectors.joining("\n")), + StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + } catch (IOException e) { + throw new RuntimeException(e); + } + + string = path.toAbsolutePath().toString(); + } else if (key.equals("asset_index")) { + string = extension.getMinecraftProvider().getVersionInfo().assetIndex().fabricId(extension.getMinecraftProvider().minecraftVersion()); + } else if (key.equals("assets_root")) { + string = new File(extension.getFiles().getUserCache(), "assets").getAbsolutePath(); + } else if (key.equals("natives")) { + string = extension.getFiles().getNativesDirectory(project).getAbsolutePath(); + } else if (key.equals("source_roots")) { + // Use a set-valued multimap for deduplicating paths. + Multimap modClasses = MultimapBuilder.hashKeys().linkedHashSetValues().build(); + + for (ModSettings mod : extension.getMods()) { + for (File file : SourceSetHelper.getClasspath(mod, project)) { + modClasses.put(mod.getName(), file.getAbsolutePath()); + } + } + + string = modClasses.entries().stream() + .map(entry -> entry.getKey() + "%%" + entry.getValue()) + .collect(Collectors.joining(File.pathSeparator)); + } else if (key.equals("mcp_mappings")) { + string = "loom.stub"; + } else if (json.has(key)) { + JsonElement element = json.get(key); + + if (element.isJsonArray()) { + string = StreamSupport.stream(element.getAsJsonArray().spliterator(), false) + .map(JsonElement::getAsString) + .flatMap(str -> { + if (str.contains(":")) { + return DependencyDownloader.download(project, str, false, false).getFiles().stream() + .map(File::getAbsolutePath) + .filter(dep -> !dep.contains("bootstraplauncher")); // TODO: Hack + } + + return Stream.of(str); + }) + .collect(Collectors.joining(File.pathSeparator)); + } else { + string = element.toString(); + } + } else { + project.getLogger().warn("Unrecognized template! " + string); } return string; @@ -173,9 +173,4 @@ public class ForgeRunsProvider implements ConfigValue.Resolver { private Set minecraftClasspath() { return DependencyDownloader.resolveFiles(project, project.getConfigurations().getByName(Constants.Configurations.FORGE_RUNTIME_LIBRARY), true); } - - @Override - public String resolve(ConfigValue.Variable variable) { - return processTemplates(variable.name()); - } }