copy from github

This commit is contained in:
Sebastian Bularca
2026-03-27 15:14:08 +01:00
parent 4aefcfd47f
commit b5d13e86d9
63 changed files with 1706 additions and 2 deletions

View File

@@ -0,0 +1,75 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using Newtonsoft.Json;
namespace Jovian.SaveSystem {
/// <summary>
/// Serializes data to an obfuscated binary format.
/// Pipeline: JSON string → UTF-8 bytes → XOR obfuscation → DeflateStream compression.
/// </summary>
public sealed class BinarySaveSerializer : ISaveSerializer {
private readonly byte[] keyBytes;
private readonly JsonSerializerSettings serializerSettings;
public BinarySaveSerializer(string obfuscationKey) {
if(string.IsNullOrEmpty(obfuscationKey)) {
throw new ArgumentException("Obfuscation key must not be null or empty.", nameof(obfuscationKey));
}
keyBytes = Encoding.UTF8.GetBytes(obfuscationKey);
serializerSettings = new JsonSerializerSettings {
TypeNameHandling = TypeNameHandling.None,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.None
};
}
public byte[] Serialize<TData>(TData data) {
string json = JsonConvert.SerializeObject(data, serializerSettings);
byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
byte[] obfuscated = ApplyXor(jsonBytes);
return Compress(obfuscated);
}
public TData Deserialize<TData>(byte[] payload) {
if(payload == null || payload.Length == 0) {
throw new ArgumentException("Payload is null or empty.", nameof(payload));
}
byte[] decompressed = Decompress(payload);
byte[] deobfuscated = ApplyXor(decompressed);
string json = Encoding.UTF8.GetString(deobfuscated);
return JsonConvert.DeserializeObject<TData>(json, serializerSettings);
}
private byte[] ApplyXor(byte[] data) {
byte[] result = new byte[data.Length];
for(int i = 0; i < data.Length; i++) {
result[i] = (byte)(data[i] ^ keyBytes[i % keyBytes.Length]);
}
return result;
}
private static byte[] Compress(byte[] data) {
using(MemoryStream output = new MemoryStream()) {
using(DeflateStream deflate = new DeflateStream(output, CompressionLevel.Fastest, leaveOpen: true)) {
deflate.Write(data, 0, data.Length);
}
return output.ToArray();
}
}
private static byte[] Decompress(byte[] data) {
using(MemoryStream input = new MemoryStream(data)) {
using(DeflateStream deflate = new DeflateStream(input, CompressionMode.Decompress)) {
using(MemoryStream output = new MemoryStream()) {
deflate.CopyTo(output);
return output.ToArray();
}
}
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4f7af3b3f06e67b408b0f52089acdeea

View File

@@ -0,0 +1,34 @@
using System;
using System.Text;
using Newtonsoft.Json;
namespace Jovian.SaveSystem {
/// <summary>
/// Serializes data to/from JSON using Newtonsoft.Json.
/// </summary>
public sealed class JsonSaveSerializer : ISaveSerializer {
private readonly JsonSerializerSettings serializerSettings;
public JsonSaveSerializer() {
serializerSettings = new JsonSerializerSettings {
TypeNameHandling = TypeNameHandling.None,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented
};
}
public byte[] Serialize<TData>(TData data) {
string json = JsonConvert.SerializeObject(data, serializerSettings);
return Encoding.UTF8.GetBytes(json);
}
public TData Deserialize<TData>(byte[] payload) {
if(payload == null || payload.Length == 0) {
throw new ArgumentException("Payload is null or empty.", nameof(payload));
}
string json = Encoding.UTF8.GetString(payload);
return JsonConvert.DeserializeObject<TData>(json, serializerSettings);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 2edaf26270d17a145a2c7f1fa7080d3a