2020import de .peeeq .wurstscript .gui .WurstGui ;
2121import de .peeeq .wurstscript .jassAst .JassProg ;
2222import de .peeeq .wurstscript .jassprinter .JassPrinter ;
23+ import de .peeeq .wurstscript .luaAst .LuaCompilationUnit ;
2324import de .peeeq .wurstscript .parser .WPos ;
25+ import de .peeeq .wurstscript .translation .lua .printing .LuaPrinter ;
2426import de .peeeq .wurstscript .utils .LineOffsets ;
2527import de .peeeq .wurstscript .utils .Utils ;
2628import org .eclipse .lsp4j .MessageParams ;
4547
4648public abstract class MapRequest extends UserRequest <Object > {
4749 protected final ConfigProvider configProvider ;
48- protected final @ Nullable File map ;
50+ protected final @ Nullable
51+ File map ;
4952 protected final List <String > compileArgs ;
5053 protected final WFile workspaceRoot ;
5154 protected final RunArgs runArgs ;
@@ -88,23 +91,35 @@ protected void processMapScript(RunArgs runArgs, WurstGui gui, ModelManager mode
8891 return ;
8992 } else {
9093 throw new CompileError (new WPos ("" , new LineOffsets (), 0 , 0 ),
91- "RunArg noExtractMapScript is set but no mapscript is provided inside the wurst folder" );
94+ "RunArg noExtractMapScript is set but no mapscript is provided inside the wurst folder" );
9295 }
9396 }
9497 WLogger .info ("extracting mapscript" );
95- byte [] extractedScript ;
98+ byte [] extractedScript = null ;
9699 try (MpqEditor mpqEditor = MpqEditorFactory .getEditor (mapCopy )) {
97- extractedScript = mpqEditor .extractFile ("war3map.j" );
100+ if (mpqEditor .hasFile ("war3map.j" )) {
101+ extractedScript = mpqEditor .extractFile ("war3map.j" );
102+ }
98103 }
99- if (new String (extractedScript , StandardCharsets .UTF_8 ).startsWith (JassPrinter .WURST_COMMENT_RAW )) {
104+ if (extractedScript == null ) {
105+ if (existingScript .exists ()) {
106+ String msg = "No war3map.j in map file, using old extracted file" ;
107+ WLogger .info (msg );
108+ } else {
109+ CompileError err = new CompileError (new WPos (mapCopy .toString (), new LineOffsets (), 0 , 0 ),
110+ "No war3map.j found in map file." );
111+ gui .showInfoMessage (err .getMessage ());
112+ WLogger .severe (err );
113+ }
114+ } else if (new String (extractedScript , StandardCharsets .UTF_8 ).startsWith (JassPrinter .WURST_COMMENT_RAW )) {
100115 WLogger .info ("map has already been compiled with wurst" );
101116 // file generated by wurst, do not use
102117 if (existingScript .exists ()) {
103- WLogger . info (
104- "Cannot use war3map.j from map file, because it already was compiled with wurst. " + "Using war3map.j from Wurst directory instead." );
118+ String msg = "Cannot use war3map.j from map file, because it already was compiled with wurst. " + "Using war3map.j from Wurst directory instead." ;
119+ WLogger . info ( msg );
105120 } else {
106121 CompileError err = new CompileError (new WPos (mapCopy .toString (), new LineOffsets (), 0 , 0 ),
107- "Cannot use war3map.j from map file, because it already was compiled with wurst. " + "Please add war3map.j to the wurst directory." );
122+ "Cannot use war3map.j from map file, because it already was compiled with wurst. " + "Please add war3map.j to the wurst directory." );
108123 gui .showInfoMessage (err .getMessage ());
109124 WLogger .severe (err );
110125 }
@@ -124,7 +139,7 @@ protected File compileMap(File projectFolder, WurstGui gui, File mapCopy, RunArg
124139 //WurstGui gui = new WurstGuiLogger();
125140 if (mpqEditor != null && !mpqEditor .canWrite ()) {
126141 WLogger .severe ("The supplied map is invalid/corrupted/protected and Wurst cannot write to it.\n " +
127- "Please supply a valid .w3x input map that can be opened in the world editor." );
142+ "Please supply a valid .w3x input map that can be opened in the world editor." );
128143 throw new NonWritableChannelException ();
129144 }
130145 WurstCompilerJassImpl compiler = new WurstCompilerJassImpl (projectFolder , gui , mpqEditor , runArgs );
@@ -148,39 +163,60 @@ protected File compileMap(File projectFolder, WurstGui gui, File mapCopy, RunArg
148163
149164 compiler .runCompiletime ();
150165
151- print ("translating program to jass ... " );
152- compiler .transformProgToJass ();
166+ if (runArgs .isLua ()) {
167+ print ("translating program to Lua ... " );
168+ LuaCompilationUnit luaCode = compiler .transformProgToLua ();
153169
154- JassProg jassProg = compiler .getProg ();
155- if (jassProg == null ) {
156- print ("Could not compile project\n " );
157- throw new RuntimeException ("Could not compile project (error in JASS translation)" );
158- }
170+ if (luaCode == null ) {
171+ print ("Could not compile project\n " );
172+ throw new RuntimeException ("Could not compile project (error in LUA translation)" );
173+ }
174+
175+ StringBuilder sb = new StringBuilder ();
176+ luaCode .print (sb , 0 );
159177
160- gui .sendProgress ("Printing program" );
161- JassPrinter printer = new JassPrinter (!runArgs .isOptimize (), jassProg );
162- String compiledMapScript = printer .printProg ();
163-
164- File buildDir = getBuildDir ();
165- File outFile = new File (buildDir , "compiled.j.txt" );
166- Files .write (compiledMapScript .getBytes (Charsets .UTF_8 ), outFile );
167-
168- if (!runArgs .isDisablePjass ()) {
169- gui .sendProgress ("Running PJass" );
170- Pjass .Result pJassResult = Pjass .runPjass (outFile );
171- WLogger .info (pJassResult .getMessage ());
172- if (!pJassResult .isOk ()) {
173- for (CompileError err : pJassResult .getErrors ()) {
174- gui .sendError (err );
178+ String compiledMapScript = sb .toString ();
179+ File buildDir = getBuildDir ();
180+ File outFile = new File (buildDir , "compiled.lua" );
181+ Files .write (compiledMapScript .getBytes (Charsets .UTF_8 ), outFile );
182+ return outFile ;
183+
184+ } else {
185+ print ("translating program to jass ... " );
186+ compiler .transformProgToJass ();
187+
188+ JassProg jassProg = compiler .getProg ();
189+ if (jassProg == null ) {
190+ print ("Could not compile project\n " );
191+ throw new RuntimeException ("Could not compile project (error in JASS translation)" );
192+ }
193+
194+ gui .sendProgress ("Printing program" );
195+ JassPrinter printer = new JassPrinter (!runArgs .isOptimize (), jassProg );
196+ String compiledMapScript = printer .printProg ();
197+ File buildDir = getBuildDir ();
198+ File outFile = new File (buildDir , "compiled.j.txt" );
199+ Files .write (compiledMapScript .getBytes (Charsets .UTF_8 ), outFile );
200+
201+ if (!runArgs .isDisablePjass ()) {
202+ gui .sendProgress ("Running PJass" );
203+ Pjass .Result pJassResult = Pjass .runPjass (outFile );
204+ WLogger .info (pJassResult .getMessage ());
205+ if (!pJassResult .isOk ()) {
206+ for (CompileError err : pJassResult .getErrors ()) {
207+ gui .sendError (err );
208+ }
209+ return null ;
175210 }
176- return null ;
177211 }
212+
213+ if (runArgs .isHotStartmap ()) {
214+ gui .sendProgress ("Running JHCR" );
215+ return runJassHotCodeReload (outFile );
216+ }
217+ return outFile ;
178218 }
179- if (runArgs .isHotStartmap ()) {
180- gui .sendProgress ("Running JHCR" );
181- return runJassHotCodeReload (outFile );
182- }
183- return outFile ;
219+
184220 } catch (Exception e ) {
185221 throw new RuntimeException (e );
186222 }
@@ -214,7 +250,7 @@ private File runJassHotCodeReload(File mapScript) throws IOException, Interrupte
214250 private void purgeUnimportedFiles (WurstModel model ) {
215251
216252 Set <CompilationUnit > imported = model .stream ()
217- .filter (cu -> isInWurstFolder (cu .getCuInfo ().getFile ()) || cu .getCuInfo ().getFile ().endsWith (".j" )).distinct ().collect (Collectors .toSet ());
253+ .filter (cu -> isInWurstFolder (cu .getCuInfo ().getFile ()) || cu .getCuInfo ().getFile ().endsWith (".j" )).distinct ().collect (Collectors .toSet ());
218254 addImports (imported , imported );
219255
220256 model .removeIf (cu -> !imported .contains (cu ));
@@ -229,8 +265,8 @@ private boolean isInWurstFolder(String file) {
229265 return false ;
230266 }
231267 return p .startsWith (w )
232- && java .nio .file .Files .exists (p )
233- && Utils .isWurstFile (file );
268+ && java .nio .file .Files .exists (p )
269+ && Utils .isWurstFile (file );
234270 }
235271
236272 protected File getBuildDir () {
@@ -248,13 +284,13 @@ protected File getBuildDir() {
248284
249285 private void addImports (Set <CompilationUnit > result , Set <CompilationUnit > toAdd ) {
250286 Set <CompilationUnit > imported =
251- toAdd .stream ()
252- .flatMap ((CompilationUnit cu ) -> cu .getPackages ().stream ())
253- .flatMap ((WPackage p ) -> p .getImports ().stream ())
254- .map (WImport ::attrImportedPackage )
255- .filter (Objects ::nonNull )
256- .map (WPackage ::attrCompilationUnit )
257- .collect (Collectors .toSet ());
287+ toAdd .stream ()
288+ .flatMap ((CompilationUnit cu ) -> cu .getPackages ().stream ())
289+ .flatMap ((WPackage p ) -> p .getImports ().stream ())
290+ .map (WImport ::attrImportedPackage )
291+ .filter (Objects ::nonNull )
292+ .map (WPackage ::attrCompilationUnit )
293+ .collect (Collectors .toSet ());
258294 boolean changed = result .addAll (imported );
259295 if (changed ) {
260296 // recursive call terminates, as there are only finitely many compilation units
0 commit comments