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); }
private static void SaveJson(List <DreamMapJson> maps, string interfaceFile, string outputFile) { DreamCompiledJson compiledDream = new DreamCompiledJson(); compiledDream.Strings = DMObjectTree.StringTable; compiledDream.Maps = maps; compiledDream.Interface = interfaceFile; compiledDream.Types = DMObjectTree.CreateJsonRepresentation(); 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)) { throw new Exception($"Failed to serialize global {global.Name}"); } if (globalJson != null) { globalListJson.Globals.Add(i, globalJson); } } compiledDream.Globals = globalListJson; } if (DMObjectTree.GlobalProcs.Count > 0) { compiledDream.GlobalProcs = new(DMObjectTree.GlobalProcs.Count); foreach (KeyValuePair <string, DMProc> globalProc in DMObjectTree.GlobalProcs) { string name = globalProc.Key; DMProc proc = globalProc.Value; compiledDream.GlobalProcs[name] = proc.GetJsonRepresentation(); } } string json = JsonSerializer.Serialize(compiledDream, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); File.WriteAllText(outputFile, json); Console.WriteLine("Saved to " + outputFile); }
static void Main(string[] args) { if (args.Length == 0 || Path.GetExtension(args[0]) != ".json") { Console.WriteLine("The json output of DMCompiler must be provided as an argument"); return; } string compiledJsonText = File.ReadAllText(args[0]); CompiledJson = JsonSerializer.Deserialize <DreamCompiledJson>(compiledJsonText); if (CompiledJson.GlobalInitProc != null) { GlobalInitProc = new DMProc(CompiledJson.GlobalInitProc); } LoadAllProcs(); LoadAllTypes(); bool acceptingCommands = true; while (acceptingCommands) { if (_selectedType != null) { Console.Write(_selectedType.Path); } Console.Write("> "); string input = Console.ReadLine(); string[] split = input.Split(" "); string command = split[0].ToLower(); switch (command) { case "q": acceptingCommands = false; break; case "search": Search(split); break; case "sel": case "select": Select(split); break; case "list": List(split); break; case "d": case "decompile": Decompile(split); break; default: Console.WriteLine("Invalid command \"" + command + "\""); break; } } }