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:
@@ -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