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); }
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); }