Select Git revision
IJSONsavable.cs
-
Marco Zimmer authoredMarco Zimmer authored
IJSONsavable.cs 15.24 KiB
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.IO;
using System;
using Newtonsoft.Json;
using UnityEngine;
using static CommunicationEvents;
// I would go for static virtual methods, but C#9 does not allow me...
// static methods cannot be overwritten -> virtual
public interface IJSONsavable<T> where T : IJSONsavable<T>, new()
{
// stand-in for non static methods
public static readonly IJSONsavable<T> Instance = new T();
public static readonly FieldInfo[] JsonSaveableFields =
#region one-time-initialisation
typeof(T)
.GetFields(
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static)
.Where((field)
=> !field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JsonIgnoreAttribute))
&& field.FieldType.GetInterfaces().Any((inter)
=> inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IJSONsavable<>)))
.ToArray();
#endregion one-time-initialisation
public static readonly FieldInfo[] JsonAutoPreProcessFields =
#region one-time-initialisation
typeof(T)
.GetFields(
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static)
.Where((field)
=> !field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JsonIgnoreAttribute))
&& field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JSONsavable.JsonAutoPreProcessAttribute))
&& field.FieldType.GetInterfaces().Any((inter)
=> inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IJSONsavable<>)))
.ToArray();
#endregion one-time-initialisation
public static readonly FieldInfo[] JsonAutoPostProcessFields =
#region one-time-initialisation
typeof(T)
.GetFields(
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Static)
.Where((field)
=> !field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JsonIgnoreAttribute))
&& field.GetCustomAttributes().Any((attribute)
=> attribute.GetType() == typeof(JSONsavable.JsonAutoPostProcessAttribute))
&& field.FieldType.GetInterfaces().Any((inter)
=> inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IJSONsavable<>)))
.ToArray();
#endregion one-time-initialisation
public static readonly FieldInfo[] JsonSeperateFields =