Ejemplo n.º 1
0
        private static string SaveJson(List <DreamMapJson> maps, string interfaceFile, string outputFile)
        {
            DreamCompiledJson compiledDream = new DreamCompiledJson();

            compiledDream.Strings   = DMObjectTree.StringTable;
            compiledDream.Maps      = maps;
            compiledDream.Interface = interfaceFile;
            var jsonRep = DMObjectTree.CreateJsonRepresentation();

            compiledDream.Types = jsonRep.Item1;
            compiledDream.Procs = jsonRep.Item2;
            if (DMObjectTree.GlobalInitProc.Bytecode.Length > 0)
            {
                compiledDream.GlobalInitProc = DMObjectTree.GlobalInitProc.GetJsonRepresentation();
            }

            if (DMObjectTree.Globals.Count > 0)
            {
                GlobalListJson globalListJson = new GlobalListJson();
                globalListJson.GlobalCount = DMObjectTree.Globals.Count;

                // Approximate capacity (4/285 in tgstation, ~3%)
                globalListJson.Globals = new Dictionary <int, object>((int)(DMObjectTree.Globals.Count * 0.03));

                for (int i = 0; i < DMObjectTree.Globals.Count; i++)
                {
                    DMVariable global = DMObjectTree.Globals[i];
                    if (!global.TryAsJsonRepresentation(out var globalJson))
                    {
                        DMCompiler.Error(new CompilerError(global.Value.Location, $"Failed to serialize global {global.Name}"));
                    }

                    if (globalJson != null)
                    {
                        globalListJson.Globals.Add(i, globalJson);
                    }
                }
                compiledDream.Globals = globalListJson;
            }

            if (DMObjectTree.GlobalProcs.Count > 0)
            {
                compiledDream.GlobalProcs = DMObjectTree.GlobalProcs.Values.ToList();
            }

            string json = JsonSerializer.Serialize(compiledDream, new JsonSerializerOptions()
            {
                DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
            });

            // Successful serialization
            if (ErrorCount == 0)
            {
                File.WriteAllText(outputFile, json);
                return("Saved to " + outputFile);
            }
            return(string.Empty);
        }
Ejemplo n.º 2
0
        private static bool TryParseArguments(string[] args, out DMCompilerSettings settings)
        {
            settings       = new();
            settings.Files = new List <string>();

            bool skipBad = args.Contains("--skip-bad-args");

            foreach (string arg in args)
            {
                switch (arg)
                {
                case "--suppress-unimplemented": settings.SuppressUnimplementedWarnings = true; break;

                case "--dump-preprocessor": settings.DumpPreprocessor = true; break;

                case "--no-standard": settings.NoStandard = true; break;

                case "--verbose": settings.Verbose = true; break;

                case "--skip-bad-args": break;

                default: {
                    string extension = Path.GetExtension(arg);

                    if (!String.IsNullOrEmpty(extension) && (extension == ".dme" || extension == ".dm"))
                    {
                        settings.Files.Add(arg);
                        Console.WriteLine($"Compiling {Path.GetFileName(arg)}");
                    }
                    else
                    {
                        if (skipBad)
                        {
                            DMCompiler.Warning(new CompilerWarning(Location.Internal, $"Invalid compiler arg '{arg}', skipping"));
                        }
                        else
                        {
                            Console.WriteLine($"Invalid arg '{arg}'");
                            return(false);
                        }
                    }

                    break;
                }
                }
            }

            if (settings.Files.Count == 0)
            {
                Console.WriteLine("At least one DME or DM file must be provided as an argument");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            if (!TryParseArguments(args, out DMCompilerSettings settings))
            {
                return;
            }

            if (!DMCompiler.Compile(settings))
            {
                //Compile errors, exit with an error code
                Environment.Exit(1);
            }
        }