2727import de .peeeq .wurstscript .jassprinter .JassPrinter ;
2828import de .peeeq .wurstscript .luaAst .LuaCompilationUnit ;
2929import de .peeeq .wurstscript .parser .WPos ;
30+ import de .peeeq .wurstscript .translation .lua .translation .LuaTranslator ;
3031import de .peeeq .wurstscript .utils .LineOffsets ;
3132import de .peeeq .wurstscript .utils .Utils ;
3233import net .moonlightflower .wc3libs .bin .app .W3I ;
@@ -167,6 +168,7 @@ protected File compileMap(File projectFolder, WurstGui gui, Optional<File> mapCo
167168 luaCode .get ().print (sb , 0 );
168169
169170 String compiledMapScript = sb .toString ();
171+ LuaTranslator .assertNoLeakedHashtableNativeCalls (compiledMapScript );
170172 File buildDir = getBuildDir ();
171173 File outFile = new File (buildDir , BUILD_COMPILED_LUA_NAME );
172174 Files .write (compiledMapScript .getBytes (Charsets .UTF_8 ), outFile );
@@ -418,8 +420,12 @@ private String resolveCachedMapFileName() {
418420 if (!cachedMapFileName .isEmpty ()) {
419421 return cachedMapFileName ;
420422 }
423+ return resolveCachedMapFileName (runArgs .isLua ());
424+ }
425+
426+ private String resolveCachedMapFileName (boolean luaMode ) {
421427 if (!map .isPresent ()) {
422- return "cached_map .w3x" ;
428+ return luaMode ? "cached_map_lua.w3x" : "cached_map_jass .w3x" ;
423429 }
424430 File inputMap = map .get ();
425431 String inputName = inputMap .getName ();
@@ -428,7 +434,8 @@ private String resolveCachedMapFileName() {
428434 // Keep only filesystem-safe characters and avoid collisions for same basename from different folders.
429435 String safeBase = baseName .replaceAll ("[^a-zA-Z0-9._-]" , "_" );
430436 String pathHash = Integer .toUnsignedString (inputMap .getAbsolutePath ().hashCode (), 36 );
431- return safeBase + "_" + pathHash + "_cached.w3x" ;
437+ String modeSuffix = luaMode ? "lua" : "jass" ;
438+ return safeBase + "_" + pathHash + "_" + modeSuffix + "_cached.w3x" ;
432439 }
433440
434441 protected File ensureWritableTargetFile (File targetFile , String dialogTitle , String lockMessage ,
@@ -498,6 +505,7 @@ private boolean isLocked(File targetMap) {
498505 */
499506 protected File ensureCachedMap (WurstGui gui ) throws IOException {
500507 File cachedMap = getCachedMapFile ();
508+ cleanupOppositeModeCacheAndOutputs ();
501509
502510 if (!map .isPresent ()) {
503511 throw new RequestFailedException (MessageType .Error , "No source map provided" );
@@ -515,6 +523,29 @@ protected File ensureCachedMap(WurstGui gui) throws IOException {
515523 return cachedMap ;
516524 }
517525
526+ private void cleanupOppositeModeCacheAndOutputs () {
527+ if (cachedMapFileName .isEmpty ()) {
528+ File cacheDir = new File (getBuildDir (), "cache" );
529+ String oppositeModeCacheName = resolveCachedMapFileName (!runArgs .isLua ());
530+ java .nio .file .Path oppositeModeCache = new File (cacheDir , oppositeModeCacheName ).toPath ();
531+ try {
532+ java .nio .file .Files .deleteIfExists (oppositeModeCache );
533+ } catch (IOException e ) {
534+ WLogger .warning ("Could not delete opposite-mode cached map: " + oppositeModeCache + " (" + e .getMessage () + ")" );
535+ }
536+ }
537+
538+ File buildDir = getBuildDir ();
539+ File oppositeCompiledOutput = runArgs .isLua ()
540+ ? new File (buildDir , BUILD_COMPILED_JASS_NAME )
541+ : new File (buildDir , BUILD_COMPILED_LUA_NAME );
542+ try {
543+ java .nio .file .Files .deleteIfExists (oppositeCompiledOutput .toPath ());
544+ } catch (IOException e ) {
545+ WLogger .warning ("Could not delete opposite-mode compiled output: " + oppositeCompiledOutput + " (" + e .getMessage () + ")" );
546+ }
547+ }
548+
518549 protected CompilationResult compileScript (ModelManager modelManager , WurstGui gui , Optional <File > testMap ,
519550 WurstProjectConfigData projectConfigData , File buildDir ,
520551 boolean isProd ) throws Exception {
0 commit comments