Phase 0.1: Resource extraction pipeline
- Add OrderedDictionary JSON converter for round-trip support - Add ObjLoadRaw() for GDI+-free binary resource loading - Simplify Ser.cs JSON methods (consistent settings, JsonConvert API) - Create SlaveMatrix.Extract CLI tool for resource extraction - Manual export walks Obj/Difs/Dif/Pars/Par hierarchy - Exports all 13 Obj resources to structured JSON with shape data - Update .gitignore for build artifacts and extraction output
This commit is contained in:
47
2DGAMELIB/_2DGAMELIB/OrderedDictionaryConverter.cs
Normal file
47
2DGAMELIB/_2DGAMELIB/OrderedDictionaryConverter.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace _2DGAMELIB
|
||||
{
|
||||
public class OrderedDictionaryConverter<T1, T2> : JsonConverter<OrderedDictionary<T1, T2>>
|
||||
where T1 : notnull
|
||||
{
|
||||
public override OrderedDictionary<T1, T2> ReadJson(JsonReader reader, Type objectType, OrderedDictionary<T1, T2> existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
{
|
||||
var dict = new OrderedDictionary<T1, T2>();
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
return dict;
|
||||
|
||||
var array = JArray.Load(reader);
|
||||
|
||||
foreach (var item in array)
|
||||
{
|
||||
var key = item["Key"].ToObject<T1>(serializer);
|
||||
var value = item["Value"].ToObject<T2>(serializer);
|
||||
dict.Add(key, value);
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, OrderedDictionary<T1, T2> value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
|
||||
foreach (var key in value.Keys)
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Key");
|
||||
serializer.Serialize(writer, key);
|
||||
writer.WritePropertyName("Value");
|
||||
serializer.Serialize(writer, value[key]);
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,11 +156,23 @@ namespace _2DGAMELIB
|
||||
GetMinMaxY(Par, ref MM[1].Y, ref MM[0].Y);
|
||||
}
|
||||
|
||||
public static Obj ObjLoad(this byte[] bd)
|
||||
{
|
||||
public static Obj ObjLoad(this byte[] bd)
|
||||
{
|
||||
return bd.Load<byte[]>().ToDeserialObject<Obj>().SetDefaultR();
|
||||
}
|
||||
|
||||
public static Obj? ObjLoadRaw(this byte[] bd)
|
||||
{
|
||||
try
|
||||
{
|
||||
return bd.Load<byte[]>().ToDeserialObject<Obj>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static bool Lot(this double p)
|
||||
{
|
||||
|
||||
@@ -1,155 +1,107 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace _2DGAMELIB
|
||||
{
|
||||
|
||||
//serialization stuff
|
||||
public static class Ser
|
||||
{
|
||||
private static SerializableAttribute s = new SerializableAttribute();
|
||||
private static SerializableAttribute s = new SerializableAttribute();
|
||||
|
||||
public static T DeepCopy<T>(this T Object)
|
||||
{
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
||||
using MemoryStream memoryStream = new MemoryStream();
|
||||
binaryFormatter.Serialize(memoryStream, Object);
|
||||
memoryStream.Position = 0L;
|
||||
return (T)binaryFormatter.Deserialize(memoryStream);
|
||||
}
|
||||
|
||||
public static byte[] ToSerialBytes<T>(this T Object)
|
||||
{
|
||||
using MemoryStream memoryStream = new MemoryStream();
|
||||
new BinaryFormatter().Serialize(memoryStream, Object);
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
|
||||
public static T ToDeserialObject<T>(this byte[] Bytes)
|
||||
{
|
||||
using MemoryStream serializationStream = new MemoryStream(Bytes);
|
||||
return (T)new BinaryFormatter().Deserialize(serializationStream);
|
||||
}
|
||||
|
||||
public static void Save<T>(this T Object, string Path)
|
||||
{
|
||||
using FileStream serializationStream = new FileStream(Path, FileMode.Create, FileAccess.Write);
|
||||
new BinaryFormatter().Serialize(serializationStream, Object);
|
||||
}
|
||||
|
||||
public static T Load<T>(this string Path)
|
||||
{
|
||||
using FileStream serializationStream = new FileStream(Path, FileMode.Open, FileAccess.Read);
|
||||
return (T)new BinaryFormatter().Deserialize(serializationStream);
|
||||
}
|
||||
|
||||
public static T Load<T>(this byte[] bd)
|
||||
{
|
||||
using MemoryStream serializationStream = new MemoryStream(bd);
|
||||
return (T)new BinaryFormatter().Deserialize(serializationStream);
|
||||
}
|
||||
/*
|
||||
public static void ToXml<T>(this T Object, string Path)
|
||||
{
|
||||
using FileStream output = new FileStream(Path, FileMode.Create, FileAccess.Write);
|
||||
using XmlWriter writer = XmlWriter.Create(output, new XmlWriterSettings
|
||||
{
|
||||
Indent = true
|
||||
});
|
||||
new DataContractSerializer(typeof(T)).WriteObject(writer, Object);
|
||||
}
|
||||
|
||||
public static T FromXml<T>(this string Path)
|
||||
{
|
||||
using FileStream input = new FileStream(Path, FileMode.Open, FileAccess.Read);
|
||||
using XmlReader reader = XmlReader.Create(input);
|
||||
return (T)new DataContractSerializer(typeof(T)).ReadObject(reader);
|
||||
}
|
||||
*/
|
||||
static Ser(){}
|
||||
|
||||
public static T JsonDeepCopy<T>(this T Object)
|
||||
private static JsonSerializerSettings CreateSettings()
|
||||
{
|
||||
using MemoryStream memoryStream = new MemoryStream();
|
||||
|
||||
|
||||
JsonSerializer jsonSerializer = new JsonSerializer
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
Formatting = Newtonsoft.Json.Formatting.Indented,
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
};
|
||||
settings.Converters.Add(new OrderedDictionaryConverter<string, Difs>());
|
||||
settings.Converters.Add(new OrderedDictionaryConverter<string, object>());
|
||||
settings.Converters.Add(new OrderedDictionaryConverter<string, But>());
|
||||
settings.Converters.Add(new OrderedDictionaryConverter<string, Lab>());
|
||||
return settings;
|
||||
}
|
||||
|
||||
jsonSerializer.Serialize(new StreamWriter(memoryStream, Encoding.UTF8), Object);
|
||||
public static T DeepCopy<T>(this T Object)
|
||||
{
|
||||
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
||||
using MemoryStream memoryStream = new MemoryStream();
|
||||
binaryFormatter.Serialize(memoryStream, Object);
|
||||
memoryStream.Position = 0L;
|
||||
return (T)binaryFormatter.Deserialize(memoryStream);
|
||||
}
|
||||
|
||||
public static byte[] ToSerialBytes<T>(this T Object)
|
||||
{
|
||||
using MemoryStream memoryStream = new MemoryStream();
|
||||
new BinaryFormatter().Serialize(memoryStream, Object);
|
||||
return memoryStream.ToArray();
|
||||
}
|
||||
|
||||
return (T)new JsonSerializer
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
Formatting = Newtonsoft.Json.Formatting.Indented,
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
}.Deserialize(new StreamReader(memoryStream), typeof(T));
|
||||
public static T ToDeserialObject<T>(this byte[] Bytes)
|
||||
{
|
||||
using MemoryStream serializationStream = new MemoryStream(Bytes);
|
||||
return (T)new BinaryFormatter().Deserialize(serializationStream);
|
||||
}
|
||||
|
||||
public static void Save<T>(this T Object, string Path)
|
||||
{
|
||||
using FileStream serializationStream = new FileStream(Path, FileMode.Create, FileAccess.Write);
|
||||
new BinaryFormatter().Serialize(serializationStream, Object);
|
||||
}
|
||||
|
||||
public static T Load<T>(this string Path)
|
||||
{
|
||||
using FileStream serializationStream = new FileStream(Path, FileMode.Open, FileAccess.Read);
|
||||
return (T)new BinaryFormatter().Deserialize(serializationStream);
|
||||
}
|
||||
|
||||
public static T Load<T>(this byte[] bd)
|
||||
{
|
||||
using MemoryStream serializationStream = new MemoryStream(bd);
|
||||
return (T)new BinaryFormatter().Deserialize(serializationStream);
|
||||
}
|
||||
|
||||
static Ser() { }
|
||||
|
||||
public static T JsonDeepCopy<T>(this T Object)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(Object, CreateSettings());
|
||||
return JsonConvert.DeserializeObject<T>(json, CreateSettings());
|
||||
}
|
||||
|
||||
public static byte[] ToJsonBytes<T>(this T Object)
|
||||
{
|
||||
MemoryStream textWriter = new MemoryStream();
|
||||
|
||||
JsonSerializer jsonSerializer = new JsonSerializer
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
Formatting = Newtonsoft.Json.Formatting.Indented,
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
};
|
||||
|
||||
jsonSerializer.Serialize(new StreamWriter(textWriter, Encoding.UTF8), Object);
|
||||
|
||||
return textWriter.ToArray();
|
||||
var json = JsonConvert.SerializeObject(Object, CreateSettings());
|
||||
return Encoding.UTF8.GetBytes(json);
|
||||
}
|
||||
|
||||
public static T ToUnJsonObject<T>(this byte[] Bytes)
|
||||
{
|
||||
using StreamReader reader = new StreamReader(new MemoryStream(Bytes), Encoding.UTF8);
|
||||
return (T)new JsonSerializer
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
Formatting = Newtonsoft.Json.Formatting.Indented,
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
}.Deserialize(reader, typeof(T));
|
||||
var json = Encoding.UTF8.GetString(Bytes);
|
||||
return JsonConvert.DeserializeObject<T>(json, CreateSettings());
|
||||
}
|
||||
|
||||
|
||||
public static void ToJson<T>(this T Object, string Path)
|
||||
{
|
||||
using StreamWriter textWriter = File.CreateText(Path);
|
||||
JsonSerializer jsonSerializer = new JsonSerializer
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
Formatting = Newtonsoft.Json.Formatting.Indented,
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
};
|
||||
jsonSerializer.Serialize(textWriter, Object);
|
||||
}
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(Object, CreateSettings());
|
||||
File.WriteAllText(Path, json);
|
||||
}
|
||||
|
||||
public static T UnJson<T>(string Path)
|
||||
{
|
||||
using StreamReader reader = File.OpenText(Path);
|
||||
return (T)new JsonSerializer
|
||||
{
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
Formatting = Newtonsoft.Json.Formatting.Indented,
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
}.Deserialize(reader, typeof(T));
|
||||
}
|
||||
public static T UnJson<T>(string Path)
|
||||
{
|
||||
var json = File.ReadAllText(Path);
|
||||
return JsonConvert.DeserializeObject<T>(json, CreateSettings());
|
||||
}
|
||||
|
||||
public static JsonSerializerSettings GetSettings() => CreateSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user