Esempio n. 1
0
        public static CompileResult CompileScript(this Map map, Compiler compiler, IEnumerable <string> luaSystemLibs, string commonJPath, string blizzardJPath)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            if (compiler is null)
            {
                throw new ArgumentNullException(nameof(compiler));
            }

            if (map.Info.ScriptLanguage != ScriptLanguage.Lua)
            {
                throw new InvalidOperationException($"The map's script language must be set to lua in order to use the C# compiler.");
            }

            using var stream = new MemoryStream();

            try
            {
                compiler.CompileSingleFile(stream, luaSystemLibs);
            }
            catch (CompilationErrorException e)
            {
                return(new CompileResult(e.EmitResult));
            }

            var transpiler = new JassToLuaTranspiler();

            transpiler.IgnoreComments          = true;
            transpiler.IgnoreEmptyDeclarations = true;
            transpiler.IgnoreEmptyStatements   = true;
            transpiler.KeepFunctionsSeparated  = true;

            transpiler.RegisterJassFile(JassSyntaxFactory.ParseCompilationUnit(File.ReadAllText(commonJPath)));
            transpiler.RegisterJassFile(JassSyntaxFactory.ParseCompilationUnit(File.ReadAllText(blizzardJPath)));

            var mapScriptBuilder = new MapScriptBuilder();

            mapScriptBuilder.SetDefaultOptionsForCSharpLua();

            var luaCompilationUnit = transpiler.Transpile(mapScriptBuilder.Build(map));

            using (var writer = new StreamWriter(stream, _defaultEncoding, leaveOpen: true))
            {
                var luaRenderOptions = new LuaSyntaxGenerator.SettingInfo
                {
                    Indent = 4,
                };

                var luaRenderer = new LuaRenderer(luaRenderOptions, writer);
                luaRenderer.RenderCompilationUnit(luaCompilationUnit);
            }

            stream.Position = 0;
            map.SetScriptFile(stream);

            return(new CompileResult(true, null));
        }
Esempio n. 2
0
        private static void RenderFunctionToFile(string path, LuaCompilationUnitSyntax luaCompilationUnit)
        {
            using (var fileStream = File.Create(path))
            {
                using (var writer = new StreamWriter(fileStream, new UTF8Encoding(false, true)))
                {
                    var renderSettings = new LuaSyntaxGenerator.SettingInfo();
                    renderSettings.Indent = 4;

                    var renderer = new LuaRenderer(renderSettings, writer);
                    renderer.RenderCompilationUnit(luaCompilationUnit);
                }
            }
        }
Esempio n. 3
0
        private void RenderToFile(string path, IEnumerable <LuaVariableListDeclarationSyntax> functions)
        {
            using (var fileStream = FileProvider.OpenNewWrite(path))
            {
                using (var writer = new StreamWriter(fileStream, new UTF8Encoding(false, true)))
                {
                    var renderer = new LuaRenderer(_rendererOptions, writer);

                    var compilationUnitSyntax = new LuaCompilationUnitSyntax();
                    foreach (var function in functions)
                    {
                        compilationUnitSyntax.AddStatement(function);
                    }

                    renderer.RenderCompilationUnit(compilationUnitSyntax);
                }
            }
        }
Esempio n. 4
0
        public void TranspileAndSaveTest(string inputFile, string outputFile, ScriptLanguage targetLanguage)
        {
            var targetFileName = targetLanguage switch
            {
                ScriptLanguage.Jass => "war3map.j",
                ScriptLanguage.Lua => "war3map.lua",
                _ => throw new InvalidEnumArgumentException(nameof(targetLanguage), (int)targetLanguage, typeof(ScriptLanguage)),
            };

            using var mapArchive = MpqArchive.Open(inputFile, true);

            var map               = Map.Open(mapArchive);
            var sourceLanguage    = map.Info.ScriptLanguage;
            var mpqArchiveBuilder = new MpqArchiveBuilder(mapArchive);

            using var mapInfoStream = new MemoryStream();
            using var mapInfoWriter = new BinaryWriter(mapInfoStream);

            var mapInfo = map.Info;

            mapInfo.ScriptLanguage = targetLanguage;
            if (mapInfo.FormatVersion < MapInfoFormatVersion.Lua)
            {
                mapInfo.FormatVersion = MapInfoFormatVersion.Lua;
                if (mapInfo.GameVersion is null)
                {
                    mapInfo.GameVersion = GamePatchVersionProvider.GetGameVersion(War3Net.Build.Common.GamePatch.v1_31_1);
                }
            }

            mapInfoWriter.Write(mapInfo);
            mapInfoStream.Position = 0;
            mpqArchiveBuilder.AddFile(MpqFile.New(mapInfoStream, MapInfo.FileName));

            if (sourceLanguage == ScriptLanguage.Jass)
            {
                if (targetLanguage != ScriptLanguage.Lua)
                {
                    throw new NotSupportedException($"Unable to transpile from {sourceLanguage} to {targetLanguage}.");
                }

                mpqArchiveBuilder.RemoveFile("war3map.j");
                mpqArchiveBuilder.RemoveFile(@"Scripts\war3map.j");

                using var reader = new StreamReader(@"C:\Users\Stan\Google Drive\PHP Projects\Files\common.j");
                var commonJ = JassSyntaxFactory.ParseCompilationUnit(reader.ReadToEnd());

                using var reader2 = new StreamReader(@"C:\Users\Stan\Google Drive\PHP Projects\Files\blizzard.j");
                var blizzardJ = JassSyntaxFactory.ParseCompilationUnit(reader2.ReadToEnd());

                var transpiler = new JassToLuaTranspiler();
                transpiler.RegisterJassFile(commonJ);
                transpiler.RegisterJassFile(blizzardJ);

                var script = mapArchive.OpenFile("war3map.j");

                using var reader3 = new StreamReader(script);
                var luaCompilationUnit = transpiler.Transpile(JassSyntaxFactory.ParseCompilationUnit(reader3.ReadToEnd()));
                script.Close();

                var tempFileName = Path.GetTempFileName();
                try
                {
                    using (var writer = new StreamWriter(tempFileName))
                    {
                        var luaRenderOptions = new LuaSyntaxGenerator.SettingInfo
                        {
                            // Indent = 4,
                            // IsCommentsDisabled = true,
                        };

                        var luaRenderer = new LuaRenderer(luaRenderOptions, writer);
                        luaRenderer.RenderCompilationUnit(luaCompilationUnit);
                    }

                    using var fileStream = File.OpenRead(tempFileName);
                    mpqArchiveBuilder.AddFile(MpqFile.New(fileStream, targetFileName));

                    var mpqArchiveCreateOptions = new MpqArchiveCreateOptions
                    {
                        AttributesCreateMode = MpqFileCreateMode.Prune,
                    };

                    mpqArchiveBuilder.SaveTo(outputFile, mpqArchiveCreateOptions);
                }
                finally
                {
                    File.Delete(tempFileName);
                }
            }
            else if (sourceLanguage == ScriptLanguage.Lua)
            {
                throw new NotSupportedException($"Unable to transpile from {sourceLanguage} to {targetLanguage}.");
            }
            else
            {
                throw new NotSupportedException($"Unable to transpile from {sourceLanguage} to {targetLanguage}.");
            }
        }
Esempio n. 5
0
        public static void TranspileAndSave(string inputFile, string outputFile, string?commonJPath, string?blizzardJPath, ScriptLanguage targetLanguage, BackgroundWorker?worker)
        {
            var targetFileName = targetLanguage switch
            {
                ScriptLanguage.Jass => "war3map.j",
                ScriptLanguage.Lua => "war3map.lua",
                _ => throw new InvalidEnumArgumentException(nameof(targetLanguage), (int)targetLanguage, typeof(ScriptLanguage)),
            };

            using var mapArchive = MpqArchive.Open(inputFile, true);

            var map               = Map.Open(mapArchive);
            var sourceLanguage    = map.Info.ScriptLanguage;
            var mpqArchiveBuilder = new MpqArchiveBuilder(mapArchive);

            using var mapInfoStream = new MemoryStream();
            using var mapInfoWriter = new BinaryWriter(mapInfoStream);

            var mapInfo = map.Info;

            mapInfo.ScriptLanguage = targetLanguage;
            if (mapInfo.FormatVersion < MapInfoFormatVersion.Lua)
            {
                mapInfo.FormatVersion = MapInfoFormatVersion.Lua;
                if (mapInfo.GameVersion is null)
                {
                    mapInfo.GameVersion = GamePatchVersionProvider.GetGameVersion(GamePatch.v1_31_1);
                }
            }

            mapInfoWriter.Write(mapInfo);
            mapInfoStream.Position = 0;
            mpqArchiveBuilder.AddFile(MpqFile.New(mapInfoStream, MapInfo.FileName));

            if (sourceLanguage == ScriptLanguage.Jass)
            {
                if (targetLanguage != ScriptLanguage.Lua)
                {
                    throw new NotSupportedException($"Unable to transpile from {sourceLanguage} to {targetLanguage}.");
                }

                mpqArchiveBuilder.RemoveFile("war3map.j");
                mpqArchiveBuilder.RemoveFile(@"Scripts\war3map.j");

                var jassHelperFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Warcraft III", "JassHelper");
                if (string.IsNullOrEmpty(commonJPath))
                {
                    if (Directory.Exists(jassHelperFolder))
                    {
                        commonJPath = Path.Combine(jassHelperFolder, "common.j");
                        if (string.IsNullOrEmpty(blizzardJPath))
                        {
                            blizzardJPath = Path.Combine(jassHelperFolder, "Blizzard.j");
                        }
                    }
                    else
                    {
                        throw new DirectoryNotFoundException("Unable to automatically find common.j and Blizzard.j folder.");
                    }
                }

                var mapHasCustomBlizzardJ = mapArchive.FileExists(@"Scripts\Blizzard.j");
                if (string.IsNullOrEmpty(blizzardJPath) && !mapHasCustomBlizzardJ)
                {
                    if (Directory.Exists(jassHelperFolder))
                    {
                        blizzardJPath = Path.Combine(jassHelperFolder, "Blizzard.j");
                    }
                    else
                    {
                        throw new DirectoryNotFoundException("Unable to automatically find common.j and Blizzard.j folder.");
                    }
                }

                var estimatedWorkToTranspile = map.Script.Length + 700000;
                var estimatedWorkToRegisterCommonAndBlizzard = 35000000 / estimatedWorkToTranspile;
                if (estimatedWorkToRegisterCommonAndBlizzard == 0)
                {
                    estimatedWorkToRegisterCommonAndBlizzard = 1;
                }

                var transpiler = new JassToLuaTranspiler();
                transpiler.RegisterJassFile(JassSyntaxFactory.ParseCompilationUnit(File.ReadAllText(commonJPath)));

                worker?.ReportProgress(estimatedWorkToRegisterCommonAndBlizzard);

                if (mapHasCustomBlizzardJ)
                {
                    throw new NotImplementedException("Custom Blizzard.j files are currently not supported.");
                }
                else
                {
                    transpiler.RegisterJassFile(JassSyntaxFactory.ParseCompilationUnit(File.ReadAllText(blizzardJPath)));

                    worker?.ReportProgress(estimatedWorkToRegisterCommonAndBlizzard * 2);
                }

                var luaCompilationUnit = transpiler.Transpile(JassSyntaxFactory.ParseCompilationUnit(map.Script));

                using var stream = new MemoryStream();
                using (var writer = new StreamWriter(stream, leaveOpen: true))
                {
                    var luaRenderOptions = new LuaSyntaxGenerator.SettingInfo
                    {
                        // Indent = 4,
                        // IsCommentsDisabled = true,
                    };

                    var luaRenderer = new LuaRenderer(luaRenderOptions, writer);
                    luaRenderer.RenderCompilationUnit(luaCompilationUnit);
                    writer.Flush();
                }

                stream.Position = 0;
                mpqArchiveBuilder.AddFile(MpqFile.New(stream, targetFileName));

                worker?.ReportProgress(100);

                var mpqArchiveCreateOptions = new MpqArchiveCreateOptions
                {
                };

                mpqArchiveBuilder.SaveTo(outputFile, mpqArchiveCreateOptions);
            }
            else if (sourceLanguage == ScriptLanguage.Lua)
            {
                mpqArchiveBuilder.RemoveFile("war3map.lua");
                mpqArchiveBuilder.RemoveFile(@"Scripts\war3map.lua");

                throw new NotSupportedException($"Unable to transpile from {sourceLanguage} to {targetLanguage}.");
            }
            else
            {
                throw new NotSupportedException($"Unable to transpile from {sourceLanguage} to {targetLanguage}.");
            }
        }