Drake53/War3Net

Access to compiled script before it is added to mpq

Closed this issue · 2 comments

I'd like to be able to grab map script at the time when it has been compiled, but the mpq archive has not yet been built.
Right now you suggest the following code structure:

BuildResult br = mapBuilder.Build(scriptCompilerOptions, buildInfo.GetMapFolder);

if (br.Success) <.....>

I'd very much like there would be another step, so I can run lua obfuscator/minifier in this intermediate step.
For example something like

MapBuilder mapBuilder = new MapBuilder("compiled_war3map.w3x")

mapBuilder.compiledScript = NodeBridge.Providers.Execute<string>("LuaMinify", mapBuilder.compiledMapScript)

BuildResult br = mapBuilder.Build(scriptCompilerOptions, buildInfo.GetMapFolder);

if (br.Success) <.....>

I added an event in v1.3.10 that allows you to modify the mpqfiles that will be used to build the map archive.
Example how to use this:

            mapBuilder.OnArchiveBuilding += (s, e) =>
            {
                const string MapScriptFileName = "war3map.lua";
                var oldMapScriptFilePath = Path.Combine(OutputFolderPath, MapScriptFileName);
                var newMapScriptFilePath = Path.Combine(OutputFolderPath, "war3mapMinified.lua");

                // Minify(inputFile: oldMapScriptFilePath, outputFile: newMapScriptFilePath);

                var oldMpqFile = e.MpqFiles.Single(file => file.Name == MpqHash.GetHashedFileName(MapScriptFileName));
                var newMpqFile = MpqFile.New(File.OpenRead(newMapScriptFilePath), MapScriptFileName);

                e.MpqFiles.Remove(oldMpqFile);
                e.MpqFiles.Add(newMpqFile);

                oldMpqFile.Dispose();
            };

            var buildResult = mapBuilder.Build(...);

Amazing! That's eeeeeeeeeeeeeexactly what's needed <3
Now the checklist is almost complete!

  • Modify files before they are built
  • Summon a Ziggurat