Skip to content

Commit

Permalink
Implement WriteJson for ModInstruction
Browse files Browse the repository at this point in the history
  • Loading branch information
behindcurtain3 committed Jun 5, 2024
1 parent 4e75311 commit fa3e354
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
16 changes: 15 additions & 1 deletion Pulsar4X/GameEngine/Modding/ModInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,21 @@ public override bool CanConvert(Type objectType)

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
throw new NotImplementedException();
if (value == null)
{
writer.WriteNull();
return;
}

ModInstruction modInstruction = (ModInstruction)value;

JObject jObject = new JObject
{
{ "Type", modInstruction.Type.ToString() },
{ "Payload", JObject.FromObject(modInstruction.Data) }
};

jObject.WriteTo(writer);
}
}

Expand Down
20 changes: 13 additions & 7 deletions Pulsar4X/Pulsar4X.Client/ModFileEditing/BluePrintsUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ImGuiNET;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Pulsar4X.Blueprints;
using Pulsar4X.DataStructures;
using Pulsar4X.Interfaces;
Expand Down Expand Up @@ -187,16 +188,21 @@ protected override void Save()
{
using (StreamWriter outputFile = new StreamWriter(Path.Combine(_fileDialogPath, _fileName)))
{

var tcb = new Tcb() {};
tcb.Payload = new List<TechCategoryBlueprint>();
JArray output = new JArray();
foreach (TechCategoryBlueprint bpt in _itemBlueprints)
{
tcb.Payload.Add(bpt);
ModInstruction modInstruction = new ModInstruction();
modInstruction.Type = ModInstruction.DataType.TechCategory;
modInstruction.Data = bpt;

var json = JsonConvert.SerializeObject(
modInstruction,
Formatting.Indented,
new JsonSerializerSettings { Converters = new List<JsonConverter> { new ModInstructionJsonConverter() } });
output.Add(json);
}
var json = JsonConvert.SerializeObject(tcb, Formatting.Indented);
outputFile.WriteLine(json);


outputFile.Write(output);

/*
Dictionary<ModInstruction.DataType, List<TechCategoryBlueprint>> dic = new Dictionary<ModInstruction.DataType, List<TechCategoryBlueprint>>();
Expand Down

0 comments on commit fa3e354

Please sign in to comment.