private static void Main(string[] args) { #if false var mapRegions = new MapRegions(MapRegionsFormatVersion.Normal); mapRegions.Regions.Add(new Region { Left = -2594f, Right = -2016f, Bottom = 992f, Top = 1568f, Name = "Spawn1Reg", }); #else var mapRegionsPath = ""; // TODO using var mapRegionsFileStream = File.OpenRead(mapRegionsPath); // var mapArchivePath = ""; // TODO // using var mapRegionsFileStream = MpqFile.OpenRead(Path.Combine(mapArchivePath, MapRegions.FileName)); using var mapRegionsReader = new BinaryReader(mapRegionsFileStream); var mapRegions = mapRegionsReader.ReadMapRegions(); #endif var members = new List <MemberDeclarationSyntax>(); var luamembers = new List <MemberDeclarationSyntax>(); foreach (var region in mapRegions.Regions) { if (string.IsNullOrEmpty(region.AmbientSound) && region.WeatherType == 0) { var decl = new GlobalVariableDeclarationSyntax( new War3Net.CodeAnalysis.Jass.Syntax.VariableDeclarationSyntax( new VariableDefinitionSyntax( JassSyntaxFactory.ParseTypeName("rect"), JassSyntaxFactory.Token(SyntaxTokenType.AlphanumericIdentifier, $"gg_rct_{region.Name.Replace(' ', '_')}"), JassSyntaxFactory.EqualsValueClause(JassSyntaxFactory.InvocationExpression( "Rect", JassSyntaxFactory.ConstantExpression(region.Left), JassSyntaxFactory.ConstantExpression(region.Bottom), JassSyntaxFactory.ConstantExpression(region.Right), JassSyntaxFactory.ConstantExpression(region.Top))))), JassSyntaxFactory.Newlines()); members.Add(JassToCSharpTranspiler.Transpile(decl)); } else { var decl = new GlobalVariableDeclarationSyntax( new War3Net.CodeAnalysis.Jass.Syntax.VariableDeclarationSyntax( new VariableDefinitionSyntax( JassSyntaxFactory.ParseTypeName("rect"), JassSyntaxFactory.Token(SyntaxTokenType.AlphanumericIdentifier, $"gg_rct_{region.Name.Replace(' ', '_')}"), JassSyntaxFactory.Empty())), JassSyntaxFactory.Newlines()); luamembers.Add(JassToCSharpTranspiler.Transpile(decl)); } } var class1 = JassTranspilerHelper.GetClassDeclaration("Regions", members, false); var class2 = JassTranspilerHelper.GetClassDeclaration("LuaRegions", luamembers, true); var usingDirectives = new UsingDirectiveSyntax[] { SyntaxFactory.UsingDirective(SyntaxFactory.Token(SyntaxKind.StaticKeyword), null, SyntaxFactory.ParseName("War3Api.Common")), }; var compilationUnit = JassTranspilerHelper.GetCompilationUnit( new SyntaxList <UsingDirectiveSyntax>(usingDirectives), JassTranspilerHelper.GetNamespaceDeclaration("War3Map.Template.Generated", class1.Concat(class2).ToArray())); var path = Path.Combine(GeneratedCodeProjectFolderPath, "Regions.cs"); using var fileStream = File.Create(path); CompilationHelper.SerializeTo(compilationUnit, fileStream); }
public static bool CompileCSharpFromJass( string filePath, string assemblyName, string namespaceName, string className, MetadataReference[] metadataReferences, UsingDirectiveSyntax[] usingDirectives, out MetadataReference outReference, out UsingDirectiveSyntax outDirective, out EmitResult emitResult, OutputKind outputKind, bool applyNativeMemberAttributes = false, string outputSource = null, string outputEmit = null, string outputLuaTypes = null) { var directiveName = $"{namespaceName}.{className}"; outDirective = SyntaxFactory.UsingDirective(SyntaxFactory.Token(SyntaxKind.StaticKeyword), null, SyntaxFactory.ParseName(directiveName)); if (outputLuaTypes != null) { // These two are incompatible, because enums can't freely inherit from other types (example: igamestate and fgamestate enums inherit from gamestate). TranspileToEnumHandler.Reset(); } var fileSyntax = JassParser.ParseFile(filePath); if (outputLuaTypes != null) { // Output lua source code for JASS type definitions. TranspileTypesToLua(fileSyntax, $"{namespaceName}{className}", outputLuaTypes); } var compilationUnit = Transpile( fileSyntax, namespaceName, className, applyNativeMemberAttributes, usingDirectives).NormalizeWhitespace(); if (outputSource != null) { new FileInfo(outputSource).Directory.Create(); // Output C# source code. #pragma warning disable CA2000 // Dispose objects before losing scope CompilationHelper.SerializeTo(compilationUnit, File.OpenWrite(outputSource), false); #pragma warning restore CA2000 // Dispose objects before losing scope } var compilation = CompilationHelper.PrepareCompilation( compilationUnit, outputKind, assemblyName ?? directiveName, metadataReferences); if (outputEmit is null) { var peStream = new MemoryStream(); emitResult = compilation.Emit(peStream, options: new EmitOptions(metadataOnly: true)); // TODO: set metadataOnly to applyNativeMemberAttributes? peStream.Seek(0, SeekOrigin.Begin); if (emitResult.Success) { outReference = MetadataReference.CreateFromStream(peStream); return(true); } peStream.Dispose(); } else { new FileInfo(outputEmit).Directory.Create(); // Output .dll file. emitResult = compilation.Emit(outputEmit); if (emitResult.Success) { outReference = MetadataReference.CreateFromFile(outputEmit); return(true); } } outReference = null; return(false); }