-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathWurstBuildConfig.java
More file actions
155 lines (128 loc) · 5.71 KB
/
Copy pathWurstBuildConfig.java
File metadata and controls
155 lines (128 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package de.peeeq.wurstio.languageserver;
import config.WurstProjectConfigData;
import de.peeeq.wurstscript.WLogger;
import net.moonlightflower.wc3libs.port.GameVersion;
import org.wurstscript.projectconfig.Wc3PatchTarget;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import static de.peeeq.wurstio.languageserver.ProjectConfigBuilder.FILE_NAME;
public final class WurstBuildConfig {
public enum ScriptMode {
LUA,
JASS
}
public enum Wc3Patch {
REFORGED,
CLASSIC,
PRE_129
}
private final org.wurstscript.projectconfig.WurstBuildConfig sharedConfig;
private WurstBuildConfig(org.wurstscript.projectconfig.WurstBuildConfig sharedConfig) {
this.sharedConfig = sharedConfig == null ? org.wurstscript.projectconfig.WurstBuildConfig.empty() : sharedConfig;
}
public static WurstBuildConfig empty() {
return new WurstBuildConfig(org.wurstscript.projectconfig.WurstBuildConfig.empty());
}
public static WurstBuildConfig fromWorkspaceRoot(WFile workspaceRoot) {
if (workspaceRoot == null) {
return empty();
}
return fromBuildFile(Path.of(workspaceRoot.toString(), FILE_NAME));
}
public static WurstBuildConfig fromProject(WurstProjectConfigData projectConfig, WFile workspaceRoot) {
WurstBuildConfig fileConfig = fromWorkspaceRoot(workspaceRoot);
if (projectConfig == null) {
return fileConfig;
}
Optional<org.wurstscript.projectconfig.ScriptMode> scriptMode = readStringGetter(projectConfig, "getScriptMode")
.flatMap(WurstBuildConfig::parseSharedScriptMode)
.or(fileConfig.sharedConfig::scriptMode);
Optional<Wc3PatchTarget> wc3Patch = readStringGetter(projectConfig, "getWc3Patch")
.flatMap(Wc3PatchTarget::parse)
.or(fileConfig.sharedConfig::wc3Patch);
return new WurstBuildConfig(new org.wurstscript.projectconfig.WurstBuildConfig(scriptMode, wc3Patch));
}
static WurstBuildConfig fromBuildFile(Path buildFile) {
try {
return new WurstBuildConfig(org.wurstscript.projectconfig.WurstBuildConfig.fromBuildFile(buildFile));
} catch (IOException e) {
WLogger.warning("Could not read " + buildFile + " for build settings", e);
return empty();
}
}
public Optional<ScriptMode> scriptMode() {
return sharedConfig.scriptMode().map(mode -> ScriptMode.valueOf(mode.name()));
}
public Optional<Wc3Patch> wc3Patch() {
return sharedConfig.wc3Patch().map(WurstBuildConfig::patchKind);
}
public Optional<String> wc3PatchName() {
return sharedConfig.wc3Patch().map(Wc3PatchTarget::name);
}
public Optional<GameVersion> configuredGameVersion() {
return sharedConfig.wc3Patch()
.map(Wc3PatchTarget::gameVersion)
.map(GameVersion::new);
}
public Wc3Patch wc3PatchOrReforged() {
return wc3Patch().orElse(Wc3Patch.REFORGED);
}
public GameVersion fallbackGameVersion() {
return configuredGameVersion().orElse(GameVersion.VERSION_1_32);
}
public List<String> applyToCompileArgs(List<String> compileArgs) {
return sharedConfig.applyToCompileArgs(compileArgs);
}
public boolean shouldUseReforgedLaunchArgs(Optional<GameVersion> detectedVersion) {
return sharedConfig.shouldUseReforgedLaunchArgs(versionString(detectedVersion));
}
public boolean shouldUseClassicWindowArg(Optional<GameVersion> detectedVersion) {
return sharedConfig.shouldUseClassicWindowArg(versionString(detectedVersion));
}
public boolean shouldCopyRunMapToWarcraftMapDir(Optional<GameVersion> detectedVersion) {
return sharedConfig.shouldCopyRunMapToWarcraftMapDir(versionString(detectedVersion));
}
public boolean shouldUseInstallDirForMaps(Optional<GameVersion> detectedVersion) {
Optional<GameVersion> effectiveVersion = detectedVersion == null ? Optional.empty() : detectedVersion;
return effectiveVersion.or(this::configuredGameVersion)
.map(version -> version.compareTo(new GameVersion("1.27.9")) <= 0)
.orElse(false);
}
private static Optional<String> versionString(Optional<GameVersion> version) {
return version == null ? Optional.empty() : version.map(GameVersion::toString);
}
private static Optional<org.wurstscript.projectconfig.ScriptMode> parseSharedScriptMode(String value) {
try {
return Optional.of(org.wurstscript.projectconfig.ScriptMode.valueOf(value.trim().toUpperCase()));
} catch (IllegalArgumentException | NullPointerException e) {
return Optional.empty();
}
}
private static Wc3Patch patchKind(Wc3PatchTarget target) {
if (target.kind() == Wc3PatchTarget.Kind.PRE_129) {
return Wc3Patch.PRE_129;
}
if (target.kind() == Wc3PatchTarget.Kind.CLASSIC) {
return Wc3Patch.CLASSIC;
}
return Wc3Patch.REFORGED;
}
private static Optional<String> readStringGetter(WurstProjectConfigData projectConfig, String getterName) {
try {
Method getter = projectConfig.getClass().getMethod(getterName);
Object value = getter.invoke(projectConfig);
if (value == null) {
return Optional.empty();
}
return Optional.of(value.toString());
} catch (NoSuchMethodException ignored) {
return Optional.empty();
} catch (Exception e) {
WLogger.debug("Could not read " + getterName + " from wurst.build config: " + e);
return Optional.empty();
}
}
}