33import config .WurstProjectConfigData ;
44import de .peeeq .wurstscript .WLogger ;
55import net .moonlightflower .wc3libs .port .GameVersion ;
6+ import org .wurstscript .projectconfig .Wc3PatchTarget ;
67
78import java .io .IOException ;
89import java .lang .reflect .Method ;
9- import java .nio .file .Files ;
1010import java .nio .file .Path ;
11- import java .util .ArrayList ;
1211import java .util .List ;
13- import java .util .Locale ;
1412import java .util .Optional ;
1513
1614import static de .peeeq .wurstio .languageserver .ProjectConfigBuilder .FILE_NAME ;
@@ -24,181 +22,133 @@ public enum ScriptMode {
2422
2523 public enum Wc3Patch {
2624 REFORGED ,
25+ CLASSIC ,
2726 PRE_129
2827 }
2928
30- private final Optional <ScriptMode > scriptMode ;
31- private final Optional <Wc3Patch > wc3Patch ;
29+ private final org .wurstscript .projectconfig .WurstBuildConfig sharedConfig ;
3230
33- private WurstBuildConfig (Optional <ScriptMode > scriptMode , Optional <Wc3Patch > wc3Patch ) {
34- this .scriptMode = scriptMode ;
35- this .wc3Patch = wc3Patch ;
31+ private WurstBuildConfig (org .wurstscript .projectconfig .WurstBuildConfig sharedConfig ) {
32+ this .sharedConfig = sharedConfig == null ? org .wurstscript .projectconfig .WurstBuildConfig .empty () : sharedConfig ;
3633 }
3734
3835 public static WurstBuildConfig empty () {
39- return new WurstBuildConfig (Optional . empty (), Optional .empty ());
36+ return new WurstBuildConfig (org . wurstscript . projectconfig . WurstBuildConfig .empty ());
4037 }
4138
4239 public static WurstBuildConfig fromWorkspaceRoot (WFile workspaceRoot ) {
40+ if (workspaceRoot == null ) {
41+ return empty ();
42+ }
4343 return fromBuildFile (Path .of (workspaceRoot .toString (), FILE_NAME ));
4444 }
4545
4646 public static WurstBuildConfig fromProject (WurstProjectConfigData projectConfig , WFile workspaceRoot ) {
4747 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 );
48+ if (projectConfig == null ) {
49+ return fileConfig ;
50+ }
51+ Optional <org .wurstscript .projectconfig .ScriptMode > scriptMode = readStringGetter (projectConfig , "getScriptMode" )
52+ .flatMap (WurstBuildConfig ::parseSharedScriptMode )
53+ .or (fileConfig .sharedConfig ::scriptMode );
54+ Optional <Wc3PatchTarget > wc3Patch = readStringGetter (projectConfig , "getWc3Patch" )
55+ .flatMap (Wc3PatchTarget ::parse )
56+ .or (fileConfig .sharedConfig ::wc3Patch );
57+ return new WurstBuildConfig (new org .wurstscript .projectconfig .WurstBuildConfig (scriptMode , wc3Patch ));
5358 }
5459
5560 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 ();
6161 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- }
62+ return new WurstBuildConfig (org .wurstscript .projectconfig .WurstBuildConfig .fromBuildFile (buildFile ));
7963 } catch (IOException e ) {
8064 WLogger .warning ("Could not read " + buildFile + " for build settings" , e );
65+ return empty ();
8166 }
82- return new WurstBuildConfig (scriptMode , wc3Patch );
8367 }
8468
8569 public Optional <ScriptMode > scriptMode () {
86- return scriptMode ;
70+ return sharedConfig . scriptMode (). map ( mode -> ScriptMode . valueOf ( mode . name ())) ;
8771 }
8872
8973 public Optional <Wc3Patch > wc3Patch () {
90- return wc3Patch ;
74+ return sharedConfig .wc3Patch ().map (WurstBuildConfig ::patchKind );
75+ }
76+
77+ public Optional <String > wc3PatchName () {
78+ return sharedConfig .wc3Patch ().map (Wc3PatchTarget ::name );
79+ }
80+
81+ public Optional <GameVersion > configuredGameVersion () {
82+ return sharedConfig .wc3Patch ()
83+ .map (Wc3PatchTarget ::gameVersion )
84+ .map (GameVersion ::new );
9185 }
9286
9387 public Wc3Patch wc3PatchOrReforged () {
94- return wc3Patch .orElse (Wc3Patch .REFORGED );
88+ return wc3Patch () .orElse (Wc3Patch .REFORGED );
9589 }
9690
9791 public GameVersion fallbackGameVersion () {
98- if (wc3PatchOrReforged () == Wc3Patch .PRE_129 ) {
99- return new GameVersion ("1.28" );
100- }
101- return GameVersion .VERSION_1_32 ;
92+ return configuredGameVersion ().orElse (GameVersion .VERSION_1_32 );
10293 }
10394
10495 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 ;
96+ return sharedConfig .applyToCompileArgs (compileArgs );
11897 }
11998
12099 public boolean shouldUseReforgedLaunchArgs (Optional <GameVersion > detectedVersion ) {
121- return detectedVersion
122- .map (version -> version .compareTo (GameVersion .VERSION_1_32 ) >= 0 )
123- .orElse (wc3PatchOrReforged () == Wc3Patch .REFORGED );
100+ return sharedConfig .shouldUseReforgedLaunchArgs (versionString (detectedVersion ));
124101 }
125102
126103 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 );
104+ return sharedConfig .shouldUseClassicWindowArg (versionString (detectedVersion ));
130105 }
131106
132107 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 );
108+ return sharedConfig .shouldCopyRunMapToWarcraftMapDir (versionString (detectedVersion ));
136109 }
137110
138111 public boolean shouldUseInstallDirForMaps (Optional <GameVersion > detectedVersion ) {
139- return detectedVersion .orElseGet (this ::fallbackGameVersion )
140- .compareTo (new GameVersion ("1.27.9" )) <= 0 ;
112+ Optional <GameVersion > effectiveVersion = detectedVersion == null ? Optional .empty () : detectedVersion ;
113+ return effectiveVersion .or (this ::configuredGameVersion )
114+ .map (version -> version .compareTo (new GameVersion ("1.27.9" )) <= 0 )
115+ .orElse (false );
141116 }
142117
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 ();
118+ private static Optional <String > versionString (Optional <GameVersion > version ) {
119+ return version == null ? Optional .empty () : version .map (GameVersion ::toString );
155120 }
156121
157- private static Optional <ScriptMode > parseScriptMode (String value ) {
158- String normalized = value .toUpperCase (Locale .ROOT );
122+ private static Optional <org .wurstscript .projectconfig .ScriptMode > parseSharedScriptMode (String value ) {
159123 try {
160- return Optional .of (ScriptMode .valueOf (normalized ));
161- } catch (IllegalArgumentException e ) {
162- WLogger .warning ("Ignoring unknown scriptMode in wurst.build: " + value );
124+ return Optional .of (org .wurstscript .projectconfig .ScriptMode .valueOf (value .trim ().toUpperCase ()));
125+ } catch (IllegalArgumentException | NullPointerException e ) {
163126 return Optional .empty ();
164127 }
165128 }
166129
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 ();
130+ private static Wc3Patch patchKind (Wc3PatchTarget target ) {
131+ if (target .kind () == Wc3PatchTarget .Kind .PRE_129 ) {
132+ return Wc3Patch .PRE_129 ;
173133 }
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" ;
134+ if (target .kind () == Wc3PatchTarget .Kind .CLASSIC ) {
135+ return Wc3Patch .CLASSIC ;
182136 }
183- return Wc3Patch .valueOf (normalized );
184- }
185-
186- private interface EnumParser <T > {
187- T parse (String value );
137+ return Wc3Patch .REFORGED ;
188138 }
189139
190- private static < T > Optional <T > readEnumGetter (WurstProjectConfigData projectConfig , String getterName , EnumParser < T > parser ) {
140+ private static Optional <String > readStringGetter (WurstProjectConfigData projectConfig , String getterName ) {
191141 try {
192142 Method getter = projectConfig .getClass ().getMethod (getterName );
193143 Object value = getter .invoke (projectConfig );
194144 if (value == null ) {
195145 return Optional .empty ();
196146 }
197- return Optional .of (parser . parse ( value .toString () ));
147+ return Optional .of (value .toString ());
198148 } catch (NoSuchMethodException ignored ) {
199149 return Optional .empty ();
200150 } catch (Exception e ) {
201- WLogger .warning ("Could not read " + getterName + " from wurst.build config" , e );
151+ WLogger .debug ("Could not read " + getterName + " from wurst.build config: " + e );
202152 return Optional .empty ();
203153 }
204154 }
0 commit comments