|
| 1 | +package de.peeeq.wurstio.languageserver; |
| 2 | + |
| 3 | +import config.WurstProjectConfigData; |
| 4 | +import de.peeeq.wurstscript.WLogger; |
| 5 | +import net.moonlightflower.wc3libs.port.GameVersion; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Locale; |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +import static de.peeeq.wurstio.languageserver.ProjectConfigBuilder.FILE_NAME; |
| 17 | + |
| 18 | +public final class WurstBuildConfig { |
| 19 | + |
| 20 | + public enum ScriptMode { |
| 21 | + LUA, |
| 22 | + JASS |
| 23 | + } |
| 24 | + |
| 25 | + public enum Wc3Patch { |
| 26 | + REFORGED, |
| 27 | + PRE_129 |
| 28 | + } |
| 29 | + |
| 30 | + private final Optional<ScriptMode> scriptMode; |
| 31 | + private final Optional<Wc3Patch> wc3Patch; |
| 32 | + |
| 33 | + private WurstBuildConfig(Optional<ScriptMode> scriptMode, Optional<Wc3Patch> wc3Patch) { |
| 34 | + this.scriptMode = scriptMode; |
| 35 | + this.wc3Patch = wc3Patch; |
| 36 | + } |
| 37 | + |
| 38 | + public static WurstBuildConfig empty() { |
| 39 | + return new WurstBuildConfig(Optional.empty(), Optional.empty()); |
| 40 | + } |
| 41 | + |
| 42 | + public static WurstBuildConfig fromWorkspaceRoot(WFile workspaceRoot) { |
| 43 | + return fromBuildFile(Path.of(workspaceRoot.toString(), FILE_NAME)); |
| 44 | + } |
| 45 | + |
| 46 | + public static WurstBuildConfig fromProject(WurstProjectConfigData projectConfig, WFile workspaceRoot) { |
| 47 | + WurstBuildConfig fileConfig = fromWorkspaceRoot(workspaceRoot); |
| 48 | + Optional<ScriptMode> scriptMode = readEnumGetter(projectConfig, "getScriptMode", ScriptMode::valueOf) |
| 49 | + .or(fileConfig::scriptMode); |
| 50 | + Optional<Wc3Patch> wc3Patch = readEnumGetter(projectConfig, "getWc3Patch", WurstBuildConfig::parsePatchName) |
| 51 | + .or(fileConfig::wc3Patch); |
| 52 | + return new WurstBuildConfig(scriptMode, wc3Patch); |
| 53 | + } |
| 54 | + |
| 55 | + static WurstBuildConfig fromBuildFile(Path buildFile) { |
| 56 | + if (!Files.exists(buildFile)) { |
| 57 | + return empty(); |
| 58 | + } |
| 59 | + Optional<ScriptMode> scriptMode = Optional.empty(); |
| 60 | + Optional<Wc3Patch> wc3Patch = Optional.empty(); |
| 61 | + try { |
| 62 | + for (String rawLine : Files.readAllLines(buildFile)) { |
| 63 | + String line = stripComment(rawLine).trim(); |
| 64 | + if (line.isEmpty() || Character.isWhitespace(rawLine.charAt(0))) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + int colon = line.indexOf(':'); |
| 68 | + if (colon < 0) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + String key = line.substring(0, colon).trim(); |
| 72 | + String value = normalizeScalar(line.substring(colon + 1).trim()); |
| 73 | + if (key.equals("scriptMode")) { |
| 74 | + scriptMode = parseScriptMode(value); |
| 75 | + } else if (key.equals("wc3Patch")) { |
| 76 | + wc3Patch = parsePatch(value); |
| 77 | + } |
| 78 | + } |
| 79 | + } catch (IOException e) { |
| 80 | + WLogger.warning("Could not read " + buildFile + " for build settings", e); |
| 81 | + } |
| 82 | + return new WurstBuildConfig(scriptMode, wc3Patch); |
| 83 | + } |
| 84 | + |
| 85 | + public Optional<ScriptMode> scriptMode() { |
| 86 | + return scriptMode; |
| 87 | + } |
| 88 | + |
| 89 | + public Optional<Wc3Patch> wc3Patch() { |
| 90 | + return wc3Patch; |
| 91 | + } |
| 92 | + |
| 93 | + public Wc3Patch wc3PatchOrReforged() { |
| 94 | + return wc3Patch.orElse(Wc3Patch.REFORGED); |
| 95 | + } |
| 96 | + |
| 97 | + public GameVersion fallbackGameVersion() { |
| 98 | + if (wc3PatchOrReforged() == Wc3Patch.PRE_129) { |
| 99 | + return new GameVersion("1.28"); |
| 100 | + } |
| 101 | + return GameVersion.VERSION_1_32; |
| 102 | + } |
| 103 | + |
| 104 | + public List<String> applyToCompileArgs(List<String> compileArgs) { |
| 105 | + if (!scriptMode.isPresent()) { |
| 106 | + return compileArgs; |
| 107 | + } |
| 108 | + List<String> result = new ArrayList<>(); |
| 109 | + for (String arg : compileArgs) { |
| 110 | + if (!"-lua".equals(arg)) { |
| 111 | + result.add(arg); |
| 112 | + } |
| 113 | + } |
| 114 | + if (scriptMode.get() == ScriptMode.LUA) { |
| 115 | + result.add("-lua"); |
| 116 | + } |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + public boolean shouldUseReforgedLaunchArgs(Optional<GameVersion> detectedVersion) { |
| 121 | + return detectedVersion |
| 122 | + .map(version -> version.compareTo(GameVersion.VERSION_1_32) >= 0) |
| 123 | + .orElse(wc3PatchOrReforged() == Wc3Patch.REFORGED); |
| 124 | + } |
| 125 | + |
| 126 | + public boolean shouldUseClassicWindowArg(Optional<GameVersion> detectedVersion) { |
| 127 | + return detectedVersion |
| 128 | + .map(version -> version.compareTo(GameVersion.VERSION_1_31) < 0) |
| 129 | + .orElse(wc3PatchOrReforged() == Wc3Patch.PRE_129); |
| 130 | + } |
| 131 | + |
| 132 | + public boolean shouldCopyRunMapToWarcraftMapDir(Optional<GameVersion> detectedVersion) { |
| 133 | + return detectedVersion |
| 134 | + .map(version -> version.compareTo(GameVersion.VERSION_1_32) < 0) |
| 135 | + .orElse(wc3PatchOrReforged() == Wc3Patch.PRE_129); |
| 136 | + } |
| 137 | + |
| 138 | + public boolean shouldUseInstallDirForMaps(Optional<GameVersion> detectedVersion) { |
| 139 | + return detectedVersion.orElseGet(this::fallbackGameVersion) |
| 140 | + .compareTo(new GameVersion("1.27.9")) <= 0; |
| 141 | + } |
| 142 | + |
| 143 | + private static String stripComment(String line) { |
| 144 | + int commentStart = line.indexOf('#'); |
| 145 | + return commentStart >= 0 ? line.substring(0, commentStart) : line; |
| 146 | + } |
| 147 | + |
| 148 | + private static String normalizeScalar(String value) { |
| 149 | + String result = value; |
| 150 | + if ((result.startsWith("\"") && result.endsWith("\"")) |
| 151 | + || (result.startsWith("'") && result.endsWith("'"))) { |
| 152 | + result = result.substring(1, result.length() - 1); |
| 153 | + } |
| 154 | + return result.trim(); |
| 155 | + } |
| 156 | + |
| 157 | + private static Optional<ScriptMode> parseScriptMode(String value) { |
| 158 | + String normalized = value.toUpperCase(Locale.ROOT); |
| 159 | + try { |
| 160 | + return Optional.of(ScriptMode.valueOf(normalized)); |
| 161 | + } catch (IllegalArgumentException e) { |
| 162 | + WLogger.warning("Ignoring unknown scriptMode in wurst.build: " + value); |
| 163 | + return Optional.empty(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private static Optional<Wc3Patch> parsePatch(String value) { |
| 168 | + try { |
| 169 | + return Optional.of(parsePatchName(value)); |
| 170 | + } catch (IllegalArgumentException e) { |
| 171 | + WLogger.warning("Ignoring unknown wc3Patch in wurst.build: " + value); |
| 172 | + return Optional.empty(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static Wc3Patch parsePatchName(String value) { |
| 177 | + String normalized = value.toUpperCase(Locale.ROOT) |
| 178 | + .replace(".", "_") |
| 179 | + .replace("-", "_"); |
| 180 | + if (normalized.equals("PRE1_29")) { |
| 181 | + normalized = "PRE_129"; |
| 182 | + } |
| 183 | + return Wc3Patch.valueOf(normalized); |
| 184 | + } |
| 185 | + |
| 186 | + private interface EnumParser<T> { |
| 187 | + T parse(String value); |
| 188 | + } |
| 189 | + |
| 190 | + private static <T> Optional<T> readEnumGetter(WurstProjectConfigData projectConfig, String getterName, EnumParser<T> parser) { |
| 191 | + try { |
| 192 | + Method getter = projectConfig.getClass().getMethod(getterName); |
| 193 | + Object value = getter.invoke(projectConfig); |
| 194 | + if (value == null) { |
| 195 | + return Optional.empty(); |
| 196 | + } |
| 197 | + return Optional.of(parser.parse(value.toString())); |
| 198 | + } catch (NoSuchMethodException ignored) { |
| 199 | + return Optional.empty(); |
| 200 | + } catch (Exception e) { |
| 201 | + WLogger.warning("Could not read " + getterName + " from wurst.build config", e); |
| 202 | + return Optional.empty(); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments