@@ -342,18 +342,6 @@ private static void replaceBaseScriptWithConfig(ModelManager modelManager, File
342342 }
343343 }
344344
345- /**
346- * Gets the cached map file location
347- */
348- protected File getCachedMapFile () {
349- File buildDir = getBuildDir ();
350- File cacheDir = new File (buildDir , "cache" );
351- if (!cacheDir .exists ()) {
352- UtilsIO .mkdirs (cacheDir );
353- }
354- return new File (cacheDir , "cached_map.w3x" );
355- }
356-
357345 /**
358346 * Checks if we can use the cached map
359347 */
@@ -374,31 +362,53 @@ protected boolean canUseCachedMap(File cachedMap) {
374362 }
375363
376364
365+ /**
366+ * Gets or creates the cached map file location
367+ */
368+ protected File getCachedMapFile () {
369+ File buildDir = getBuildDir ();
370+ File cacheDir = new File (buildDir , "cache" );
371+ if (!cacheDir .exists ()) {
372+ UtilsIO .mkdirs (cacheDir );
373+ }
374+ return new File (cacheDir , "cached_map.w3x" );
375+ }
376+
377+ /**
378+ * Ensures cached map exists and is up to date
379+ */
380+ protected File ensureCachedMap (WurstGui gui ) throws IOException {
381+ File cachedMap = getCachedMapFile ();
382+
383+ if (!map .isPresent ()) {
384+ throw new RequestFailedException (MessageType .Error , "No source map provided" );
385+ }
386+
387+ File sourceMap = map .get ();
388+
389+ // If cached map doesn't exist or source is newer, update cache
390+ if (!cachedMap .exists () || sourceMap .lastModified () > cachedMap .lastModified ()) {
391+ WLogger .info ("Updating cached map from source" );
392+ gui .sendProgress ("Updating cached map" );
393+ Files .copy (sourceMap , cachedMap );
394+ }
395+
396+ return cachedMap ;
397+ }
398+
377399 protected CompilationResult compileScript (ModelManager modelManager , WurstGui gui , Optional <File > testMap ,
378400 WurstProjectConfigData projectConfigData , File buildDir ,
379401 boolean isProd ) throws Exception {
380402
381- File cachedMap = getCachedMapFile ();
403+ // Ensure we're working with the cached map
404+ File cachedMap = ensureCachedMap (gui );
382405
383- if (!cachedMap .exists ()) {
384- if (testMap .get ().exists ()) {
385- boolean deleteOk = testMap .get ().delete ();
386- if (!deleteOk ) {
387- throw new RequestFailedException (MessageType .Error , "Could not delete old mapfile: " + testMap );
388- }
389- }
390- if (map .isPresent ()) {
391- gui .sendProgress ("Creating new cached map" );
392- Files .copy (map .get (), testMap .get ());
393- // Also update the cache
394- Files .copy (map .get (), cachedMap );
395- }
396- }
406+ // Update testMap to point to cached map
407+ testMap = Optional .of (cachedMap );
397408
398409 CompilationResult result ;
399410
400411 if (runArgs .isHotReload ()) {
401- // For hot reload use cached war3map if it exists
402412 result = new CompilationResult ();
403413 result .script = new File (buildDir , "war3mapj_with_config.j.txt" );
404414 if (!result .script .exists ()) {
@@ -414,7 +424,7 @@ protected CompilationResult compileScript(ModelManager modelManager, WurstGui gu
414424 result = applyProjectConfig (gui , testMap , buildDir , projectConfigData , scriptFile );
415425 }
416426
417- // first compile the script:
427+ // Compile the script
418428 result .script = compileScript (gui , modelManager , compileArgs , testMap , projectConfigData , isProd , result .script );
419429
420430 Optional <WurstModel > model = Optional .ofNullable (modelManager .getModel ());
@@ -430,6 +440,84 @@ protected CompilationResult compileScript(ModelManager modelManager, WurstGui gu
430440 return result ;
431441 }
432442
443+ protected void injectMapData (WurstGui gui , Optional <File > testMap , CompilationResult result ) throws Exception {
444+ gui .sendProgress ("Injecting map data" );
445+ timeTaker .beginPhase ("Injecting map data" );
446+
447+ // Work directly with the cached map
448+ File cachedMap = getCachedMapFile ();
449+
450+ if (!cachedMap .exists ()) {
451+ throw new RequestFailedException (MessageType .Error , "Cached map does not exist" );
452+ }
453+
454+ try (MpqEditor mpqEditor = MpqEditorFactory .getEditor (Optional .of (cachedMap ))) {
455+ String mapScriptName ;
456+ if (runArgs .isLua ()) {
457+ mapScriptName = "war3map.lua" ;
458+ injectExternalLuaFiles (result .script );
459+ } else {
460+ mapScriptName = "war3map.j" ;
461+ }
462+
463+ // Delete old scripts
464+ if (mpqEditor .hasFile ("war3map.j" )) {
465+ mpqEditor .deleteFile ("war3map.j" );
466+ }
467+ if (mpqEditor .hasFile ("war3map.lua" )) {
468+ mpqEditor .deleteFile ("war3map.lua" );
469+ }
470+
471+ // Insert new script
472+ mpqEditor .insertFile (mapScriptName , result .script );
473+
474+ // Insert w3i if it changed
475+ if (result .w3i != null ) {
476+ String w3iHash = ImportFile .calculateFileHash (result .w3i );
477+
478+ Optional <ImportFile .CacheManifest > manifestOpt = ImportFile .getCachedManifest (mpqEditor );
479+ boolean w3iChanged = true ;
480+
481+ if (manifestOpt .isPresent () && manifestOpt .get ().w3iConfigMatches (w3iHash )) {
482+ WLogger .info ("W3I file unchanged, skipping injection" );
483+ w3iChanged = false ;
484+ }
485+
486+ if (w3iChanged ) {
487+ WLogger .info ("W3I file changed, injecting" );
488+ if (mpqEditor .hasFile (W3I .GAME_PATH .getName ())) {
489+ mpqEditor .deleteFile (W3I .GAME_PATH .getName ());
490+ }
491+ mpqEditor .insertFile (W3I .GAME_PATH .getName (), result .w3i );
492+
493+ // Update manifest
494+ ImportFile .CacheManifest manifest = manifestOpt .orElse (new ImportFile .CacheManifest ());
495+ manifest .setW3iConfig (w3iHash );
496+ ImportFile .saveManifest (mpqEditor , manifest );
497+ }
498+ }
499+
500+ // CRITICAL: Import files into THIS mpq editor instance
501+ gui .sendProgress ("Importing resource files" );
502+ timeTaker .beginPhase ("Importing files" );
503+ try {
504+ ImportFile .ImportResult importResult = ImportFile .importFilesFromImports (
505+ workspaceRoot .getFile (),
506+ mpqEditor
507+ );
508+ WLogger .info ("Import result: " + importResult .toString ());
509+ } catch (Exception e ) {
510+ WLogger .severe ("Failed to import files: " + e .getMessage ());
511+ throw e ;
512+ }
513+ timeTaker .endPhase ();
514+ }
515+
516+ timeTaker .endPhase ();
517+
518+ WLogger .info ("Cached map size after injection: " + (cachedMap .length () / 1024 / 1024 ) + " MB" );
519+ }
520+
433521 private static boolean startsWith (byte [] data , byte [] prefix ) {
434522 if (data .length < prefix .length ) return false ;
435523 for (int i = 0 ; i < prefix .length ; i ++) {
@@ -532,58 +620,6 @@ private W3InstallationData getBestW3InstallationData() throws RequestFailedExcep
532620 }
533621 }
534622
535- // In MapRequest.java - Updated injectMapData method
536-
537- protected void injectMapData (WurstGui gui , Optional <File > testMap , CompilationResult result ) throws Exception {
538- gui .sendProgress ("Injecting map data" );
539- timeTaker .beginPhase ("Injecting map data" );
540-
541- File cachedMap = getCachedMapFile ();
542-
543- try (MpqEditor mpqEditor = MpqEditorFactory .getEditor (Optional .ofNullable (cachedMap ))) {
544- String mapScriptName ;
545- if (runArgs .isLua ()) {
546- mapScriptName = "war3map.lua" ;
547- injectExternalLuaFiles (result .script );
548- } else {
549- mapScriptName = "war3map.j" ;
550- }
551-
552- // Delete old scripts
553- mpqEditor .deleteFile ("war3map.j" );
554- mpqEditor .deleteFile ("war3map.lua" );
555-
556- // Insert new script
557- mpqEditor .insertFile (mapScriptName , result .script );
558-
559- // Insert w3i if it changed
560- if (result .w3i != null ) {
561- // Calculate hash to track w3i changes
562- String w3iHash = ImportFile .calculateFileHash (result .w3i );
563-
564- Optional <ImportFile .CacheManifest > manifestOpt = ImportFile .getCachedManifest (mpqEditor );
565- boolean w3iChanged = true ;
566-
567- if (manifestOpt .isPresent () && manifestOpt .get ().w3iConfigMatches (w3iHash )) {
568- WLogger .info ("W3I file unchanged, skipping injection" );
569- w3iChanged = false ;
570- }
571-
572- if (w3iChanged ) {
573- WLogger .info ("W3I file changed, injecting" );
574- mpqEditor .deleteFile (W3I .GAME_PATH .getName ());
575- mpqEditor .insertFile (W3I .GAME_PATH .getName (), result .w3i );
576-
577- // Update manifest
578- ImportFile .CacheManifest manifest = manifestOpt .orElse (new ImportFile .CacheManifest ());
579- manifest .setW3iConfig (w3iHash );
580- ImportFile .saveManifest (mpqEditor , manifest );
581- }
582- }
583- }
584-
585- timeTaker .endPhase ();
586- }
587623
588624 private void injectExternalLuaFiles (File script ) {
589625 File luaDir ;
0 commit comments