2020-03-26 12:28:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using System.ComponentModel;
|
2020-03-26 12:28:03 +00:00
|
|
|
|
using System.IO;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
2020-03-26 12:28:03 +00:00
|
|
|
|
using System.Text;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-03-26 12:28:03 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
using Wabbajack.Common.Serialization.Json;
|
2020-03-26 12:28:03 +00:00
|
|
|
|
using File = Alphaleonis.Win32.Filesystem.File;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
|
|
|
|
public static partial class Utils
|
|
|
|
|
{
|
|
|
|
|
public static List<JsonConverter> Converters = new List<JsonConverter>
|
|
|
|
|
{
|
|
|
|
|
new HashJsonConverter(),
|
|
|
|
|
new RelativePathConverter(),
|
|
|
|
|
new AbolutePathConverter(),
|
|
|
|
|
new HashRelativePathConverter(),
|
2020-04-01 12:17:38 +00:00
|
|
|
|
new FullPathConverter(),
|
2020-04-01 19:15:23 +00:00
|
|
|
|
new GameConverter(),
|
|
|
|
|
new PercentConverter(),
|
2020-03-26 12:28:03 +00:00
|
|
|
|
};
|
2020-04-06 20:48:54 +00:00
|
|
|
|
|
|
|
|
|
public static JsonSerializerSettings JsonSettings =>
|
|
|
|
|
new JsonSerializerSettings {
|
|
|
|
|
TypeNameHandling = TypeNameHandling.Objects,
|
|
|
|
|
SerializationBinder = new JsonNameSerializationBinder(),
|
|
|
|
|
Converters = Converters};
|
|
|
|
|
|
2020-04-01 12:17:38 +00:00
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
public static void ToJson<T>(this T obj, string filename)
|
2020-03-26 12:28:03 +00:00
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filename))
|
|
|
|
|
File.Delete(filename);
|
2020-04-06 20:48:54 +00:00
|
|
|
|
File.WriteAllText(filename, JsonConvert.SerializeObject(obj, Formatting.Indented, JsonSettings));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ToJson<T>(this T obj, Stream stream)
|
|
|
|
|
{
|
|
|
|
|
using var tw = new StreamWriter(stream, Encoding.UTF8, leaveOpen: true);
|
|
|
|
|
using var writer = new JsonTextWriter(tw);
|
|
|
|
|
var ser = JsonSerializer.Create(JsonSettings);
|
|
|
|
|
ser.Serialize(writer, obj);
|
2020-03-26 12:28:03 +00:00
|
|
|
|
}
|
2020-04-08 04:19:36 +00:00
|
|
|
|
|
|
|
|
|
public static void ToJson<T>(this T obj, AbsolutePath path)
|
|
|
|
|
{
|
|
|
|
|
using var fs = path.Create();
|
|
|
|
|
obj.ToJson(fs);
|
|
|
|
|
}
|
2020-03-26 12:28:03 +00:00
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
public static string ToJson<T>(this T obj)
|
2020-03-26 12:28:03 +00:00
|
|
|
|
{
|
2020-04-06 20:48:54 +00:00
|
|
|
|
return JsonConvert.SerializeObject(obj, JsonSettings);
|
2020-03-26 12:28:03 +00:00
|
|
|
|
}
|
2020-04-01 12:17:38 +00:00
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
public static T FromJson<T>(this AbsolutePath filename,
|
2020-04-01 12:17:38 +00:00
|
|
|
|
TypeNameHandling handling = TypeNameHandling.All,
|
2020-03-26 12:28:03 +00:00
|
|
|
|
TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
|
|
|
|
|
{
|
2020-04-06 20:48:54 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<T>(filename.ReadAllText(), JsonSettings)!;
|
2020-03-26 12:28:03 +00:00
|
|
|
|
}
|
2020-04-01 12:17:38 +00:00
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
public static T FromJsonString<T>(this string data,
|
|
|
|
|
TypeNameHandling handling = TypeNameHandling.Objects,
|
2020-03-26 12:28:03 +00:00
|
|
|
|
TypeNameAssemblyFormatHandling format = TypeNameAssemblyFormatHandling.Full)
|
|
|
|
|
{
|
2020-04-06 20:48:54 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<T>(data, JsonSettings)!;
|
2020-03-26 12:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
public static T FromJson<T>(this Stream stream)
|
2020-03-26 12:28:03 +00:00
|
|
|
|
{
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using var tr = new StreamReader(stream, Encoding.UTF8, leaveOpen: true);
|
|
|
|
|
using var reader = new JsonTextReader(tr);
|
|
|
|
|
var ser = JsonSerializer.Create(JsonSettings);
|
|
|
|
|
return ser.Deserialize<T>(reader);
|
2020-03-26 12:28:03 +00:00
|
|
|
|
}
|
2020-04-06 20:48:54 +00:00
|
|
|
|
|
2020-04-01 12:17:38 +00:00
|
|
|
|
|
|
|
|
|
|
2020-03-26 12:28:03 +00:00
|
|
|
|
private class HashJsonConverter : JsonConverter<Hash>
|
|
|
|
|
{
|
|
|
|
|
public override void WriteJson(JsonWriter writer, Hash value, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteValue(value.ToBase64());
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 12:17:38 +00:00
|
|
|
|
public override Hash ReadJson(JsonReader reader, Type objectType, Hash existingValue, bool hasExistingValue,
|
|
|
|
|
JsonSerializer serializer)
|
2020-03-26 12:28:03 +00:00
|
|
|
|
{
|
2020-04-03 23:23:13 +00:00
|
|
|
|
return Hash.FromBase64((string)reader.Value!);
|
2020-03-26 12:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-01 12:17:38 +00:00
|
|
|
|
|
2020-03-26 12:28:03 +00:00
|
|
|
|
private class RelativePathConverter : JsonConverter<RelativePath>
|
|
|
|
|
{
|
|
|
|
|
public override void WriteJson(JsonWriter writer, RelativePath value, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteValue((string)value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 12:17:38 +00:00
|
|
|
|
public override RelativePath ReadJson(JsonReader reader, Type objectType, RelativePath existingValue,
|
|
|
|
|
bool hasExistingValue,
|
2020-03-26 12:28:03 +00:00
|
|
|
|
JsonSerializer serializer)
|
|
|
|
|
{
|
2020-04-03 23:23:13 +00:00
|
|
|
|
return (RelativePath)(string)reader.Value!;
|
2020-03-26 12:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-01 12:17:38 +00:00
|
|
|
|
|
2020-03-26 12:28:03 +00:00
|
|
|
|
private class AbolutePathConverter : JsonConverter<AbsolutePath>
|
|
|
|
|
{
|
|
|
|
|
public override void WriteJson(JsonWriter writer, AbsolutePath value, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteValue((string)value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 12:17:38 +00:00
|
|
|
|
public override AbsolutePath ReadJson(JsonReader reader, Type objectType, AbsolutePath existingValue,
|
|
|
|
|
bool hasExistingValue,
|
2020-03-26 12:28:03 +00:00
|
|
|
|
JsonSerializer serializer)
|
|
|
|
|
{
|
2020-04-03 23:23:13 +00:00
|
|
|
|
return (AbsolutePath)(string)reader.Value!;
|
2020-03-26 12:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-01 12:17:38 +00:00
|
|
|
|
|
2020-04-01 19:15:23 +00:00
|
|
|
|
private class PercentConverter : JsonConverter<Percent>
|
|
|
|
|
{
|
|
|
|
|
public override Percent ReadJson(JsonReader reader, Type objectType, Percent existingValue, bool hasExistingValue, JsonSerializer serializer)
|
|
|
|
|
{
|
2020-04-03 23:23:13 +00:00
|
|
|
|
double d = (double)reader.Value!;
|
2020-04-01 19:15:23 +00:00
|
|
|
|
return Percent.FactoryPutInRange(d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void WriteJson(JsonWriter writer, Percent value, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteValue(value.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:28:03 +00:00
|
|
|
|
private class HashRelativePathConverter : JsonConverter<HashRelativePath>
|
|
|
|
|
{
|
|
|
|
|
public override void WriteJson(JsonWriter writer, HashRelativePath value, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteStartArray();
|
|
|
|
|
writer.WriteValue(value.BaseHash.ToBase64());
|
|
|
|
|
foreach (var itm in value.Paths)
|
|
|
|
|
writer.WriteValue((string)itm);
|
|
|
|
|
writer.WriteEndArray();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 12:17:38 +00:00
|
|
|
|
public override HashRelativePath ReadJson(JsonReader reader, Type objectType,
|
|
|
|
|
HashRelativePath existingValue, bool hasExistingValue,
|
2020-03-26 12:28:03 +00:00
|
|
|
|
JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
if (reader.TokenType != JsonToken.StartArray)
|
|
|
|
|
throw new JsonException("Invalid JSON state while reading Hash Relative Path");
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
2020-04-03 23:23:13 +00:00
|
|
|
|
var hash = Hash.FromBase64((string)reader.Value!);
|
2020-03-26 12:28:03 +00:00
|
|
|
|
var paths = new List<RelativePath>();
|
|
|
|
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
while (reader.TokenType != JsonToken.EndArray)
|
|
|
|
|
{
|
2020-04-03 23:23:13 +00:00
|
|
|
|
paths.Add((RelativePath)(string)reader.Value!);
|
2020-03-26 12:28:03 +00:00
|
|
|
|
reader.Read();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HashRelativePath(hash, paths.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-01 12:17:38 +00:00
|
|
|
|
|
2020-03-26 12:28:03 +00:00
|
|
|
|
private class FullPathConverter : JsonConverter<FullPath>
|
|
|
|
|
{
|
|
|
|
|
public override void WriteJson(JsonWriter writer, FullPath value, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteStartArray();
|
|
|
|
|
writer.WriteValue((string)value.Base);
|
|
|
|
|
foreach (var itm in value.Paths)
|
|
|
|
|
writer.WriteValue((string)itm);
|
|
|
|
|
writer.WriteEndArray();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 12:17:38 +00:00
|
|
|
|
public override FullPath ReadJson(JsonReader reader, Type objectType, FullPath existingValue,
|
|
|
|
|
bool hasExistingValue,
|
2020-03-26 12:28:03 +00:00
|
|
|
|
JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
if (reader.TokenType != JsonToken.StartArray)
|
|
|
|
|
throw new JsonException("Invalid JSON state while reading Hash Relative Path");
|
|
|
|
|
reader.Read();
|
|
|
|
|
|
2020-04-03 23:23:13 +00:00
|
|
|
|
var abs = (AbsolutePath)(string)reader.Value!;
|
2020-03-26 12:28:03 +00:00
|
|
|
|
var paths = new List<RelativePath>();
|
|
|
|
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
while (reader.TokenType != JsonToken.EndArray)
|
|
|
|
|
{
|
2020-04-03 23:23:13 +00:00
|
|
|
|
paths.Add((RelativePath)(string)reader.Value!);
|
2020-03-26 12:28:03 +00:00
|
|
|
|
reader.Read();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new FullPath(abs, paths.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 12:17:38 +00:00
|
|
|
|
public class GameConverter : JsonConverter<Game>
|
|
|
|
|
{
|
|
|
|
|
public override void WriteJson(JsonWriter writer, Game value, JsonSerializer serializer)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteValue(Enum.GetName(typeof(Game), value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Game ReadJson(JsonReader reader, Type objectType, Game existingValue,
|
|
|
|
|
bool hasExistingValue,
|
|
|
|
|
JsonSerializer serializer)
|
|
|
|
|
{
|
2020-04-01 18:54:08 +00:00
|
|
|
|
// Backwards compatibility support
|
|
|
|
|
var str = reader.Value?.ToString();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(str)) return default;
|
|
|
|
|
if (int.TryParse(str, out var i))
|
|
|
|
|
{
|
|
|
|
|
return (Game)i;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 23:23:13 +00:00
|
|
|
|
GameMetaData? game = GameRegistry.GetByFuzzyName(str);
|
|
|
|
|
if (game == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Could not convert {str} to a Game type.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return game.Game;
|
2020-04-01 12:17:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-06 20:48:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class JsonNameSerializationBinder : ISerializationBinder
|
|
|
|
|
{
|
2020-04-06 22:02:01 +00:00
|
|
|
|
private static Dictionary<string, Type> _nameToType = new Dictionary<string, Type>();
|
|
|
|
|
private static Dictionary<Type, string> _typeToName = new Dictionary<Type, string>();
|
|
|
|
|
private static bool _inited = false;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
|
|
|
|
|
public JsonNameSerializationBinder()
|
|
|
|
|
{
|
2020-04-06 22:02:01 +00:00
|
|
|
|
if (_inited)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
var customDisplayNameTypes =
|
|
|
|
|
AppDomain.CurrentDomain
|
|
|
|
|
.GetAssemblies()
|
2020-04-06 22:02:01 +00:00
|
|
|
|
.Where(a => a.FullName != null && !a.FullName.StartsWith("System") && !a.FullName.StartsWith("Microsoft"))
|
2020-04-06 20:48:54 +00:00
|
|
|
|
.SelectMany(a =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return a.GetTypes();
|
|
|
|
|
}
|
|
|
|
|
catch (ReflectionTypeLoadException)
|
|
|
|
|
{
|
|
|
|
|
return new Type[0];
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
//concat with references if desired
|
|
|
|
|
.Where(x => x
|
|
|
|
|
.GetCustomAttributes(false)
|
|
|
|
|
.Any(y => y is JsonNameAttribute));
|
|
|
|
|
|
|
|
|
|
_nameToType = customDisplayNameTypes.ToDictionary(
|
|
|
|
|
t => t.GetCustomAttributes(false).OfType<JsonNameAttribute>().First().Name,
|
|
|
|
|
t => t);
|
|
|
|
|
|
|
|
|
|
_typeToName = _nameToType.ToDictionary(
|
|
|
|
|
t => t.Value,
|
|
|
|
|
t => t.Key);
|
2020-04-06 22:02:01 +00:00
|
|
|
|
_inited = true;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Type BindToType(string? assemblyName, string typeName)
|
|
|
|
|
{
|
|
|
|
|
if (typeName.EndsWith("[]"))
|
|
|
|
|
{
|
|
|
|
|
var result = BindToType(assemblyName, typeName.Substring(0, typeName.Length - 2));
|
|
|
|
|
return result.MakeArrayType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_nameToType.ContainsKey(typeName))
|
|
|
|
|
return _nameToType[typeName];
|
|
|
|
|
|
|
|
|
|
var val = Type.GetType(typeName);
|
|
|
|
|
if (val != null)
|
|
|
|
|
return val;
|
|
|
|
|
|
|
|
|
|
if (assemblyName != null)
|
|
|
|
|
{
|
|
|
|
|
var assembly = AppDomain.CurrentDomain.Load(assemblyName);
|
|
|
|
|
if (assembly != null)
|
|
|
|
|
{
|
|
|
|
|
var result = assembly.GetType(typeName);
|
|
|
|
|
if (result != null) return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new InvalidDataException($"No Binding name for {typeName}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BindToName(Type serializedType, out string? assemblyName, out string? typeName)
|
|
|
|
|
{
|
|
|
|
|
if (!_typeToName.ContainsKey(serializedType))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidDataException($"No Binding name for {serializedType}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var name = _typeToName[serializedType];
|
|
|
|
|
|
|
|
|
|
assemblyName = null;
|
|
|
|
|
typeName = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-26 12:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|