Ejemplo 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));
        }
Ejemplo n.º 2
0
        public static CompileResult CompileScript(this Map map, Compiler compiler, IEnumerable <string> luaSystemLibs, string commonJPath, string blizzardJPath)
        {
            var mapScriptBuilder = new MapScriptBuilder();

            mapScriptBuilder.SetDefaultOptionsForCSharpLua();

            return(map.CompileScript(compiler, mapScriptBuilder, luaSystemLibs, commonJPath, blizzardJPath));
        }