Modular Database Tools

Extracted all inspector code for database inspector into a single class per supported field type and wrote reflection code to invoke them automatically
Removed all Aetheria-specific code from database tools
Database View editor is now abstract and supports arbitrary grouping of entry types; specific users must implement their own database view
This commit is contained in:
Reinier Vens 2021-03-31 04:07:11 +01:00
commit 526a737a4d
72 changed files with 1674 additions and 1624 deletions

View file

@ -1,28 +1,14 @@
using System;
[AttributeUsage(AttributeTargets.Class)]
public class InspectableAttribute : Attribute { }
public abstract class PreferredInspectorAttribute : InspectableAttribute
{
public Type TargetType;
public PreferredInspectorAttribute(Type targetType)
{
TargetType = targetType;
}
}
[AttributeUsage(AttributeTargets.Class)]
public class ExternalEntryAttribute : Attribute { }
public class InspectableFieldAttribute : Attribute { }
public class InspectableAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Field)]
public class InspectableTextAttribute : InspectableFieldAttribute { }
public abstract class PreferredInspectorAttribute : InspectableAttribute { }
[AttributeUsage(AttributeTargets.Field)]
public class InspectableUnityObjectAttribute : InspectableFieldAttribute
public class InspectableUnityObjectAttribute : PreferredInspectorAttribute
{
public Type ObjectType;
@ -31,27 +17,15 @@ public class InspectableUnityObjectAttribute : InspectableFieldAttribute
ObjectType = type;
}
}
public class InspectableTextAttribute : PreferredInspectorAttribute { }
public class InspectablePrefabAttribute : PreferredInspectorAttribute { }
public class InspectableTextureAttribute : PreferredInspectorAttribute { }
public class InspectableTextAssetAttribute : PreferredInspectorAttribute { }
public class InspectableTemperatureAttribute : PreferredInspectorAttribute { }
public class InspectableAnimationCurveAttribute : PreferredInspectorAttribute { }
public class InspectableColorAttribute : PreferredInspectorAttribute { }
[AttributeUsage(AttributeTargets.Field)]
public class InspectablePrefabAttribute : InspectableFieldAttribute { }
[AttributeUsage(AttributeTargets.Field)]
public class InspectableTextureAttribute : InspectableFieldAttribute { }
[AttributeUsage(AttributeTargets.Field)]
public class InspectableTextAssetAttribute : InspectableFieldAttribute { }
[AttributeUsage(AttributeTargets.Field)]
public class InspectableTemperatureAttribute : InspectableFieldAttribute { }
[AttributeUsage(AttributeTargets.Field)]
public class InspectableAnimationCurveAttribute : InspectableFieldAttribute { }
[AttributeUsage(AttributeTargets.Field)]
public class InspectableColorAttribute : InspectableFieldAttribute { }
[AttributeUsage(AttributeTargets.Field)]
public class InspectableDatabaseLinkAttribute : InspectableFieldAttribute
public class InspectableDatabaseLinkAttribute : PreferredInspectorAttribute
{
public readonly Type EntryType;
@ -61,8 +35,7 @@ public class InspectableDatabaseLinkAttribute : InspectableFieldAttribute
}
}
[AttributeUsage(AttributeTargets.Field)]
public class RangedFloatInspectableAttribute : InspectableFieldAttribute
public class RangedFloatInspectableAttribute : PreferredInspectorAttribute
{
public readonly float Min, Max;
@ -73,6 +46,27 @@ public class RangedFloatInspectableAttribute : InspectableFieldAttribute
}
}
public class RangedIntInspectableAttribute : PreferredInspectorAttribute
{
public readonly int Min, Max;
public RangedIntInspectableAttribute(int min, int max)
{
Min = min;
Max = max;
}
}
public class InspectableTypeAttribute : PreferredInspectorAttribute
{
public readonly Type Type;
public InspectableTypeAttribute(Type type)
{
Type = type;
}
}
[AttributeUsage(AttributeTargets.Field)]
public class RangedFloatAttribute : Attribute
{
@ -85,30 +79,6 @@ public class RangedFloatAttribute : Attribute
}
}
[AttributeUsage(AttributeTargets.Field)]
public class RangedIntInspectableAttribute : InspectableFieldAttribute
{
public readonly int Min, Max;
public RangedIntInspectableAttribute(int min, int max)
{
Min = min;
Max = max;
}
}
public class InspectableTypeAttribute : InspectableFieldAttribute
{
public readonly Type Type;
public InspectableTypeAttribute(Type type)
{
Type = type;
}
}
[AttributeUsage(AttributeTargets.Field)]
public class SimplePerformanceStatAttribute : Attribute { }
/// <summary>
/// <para>Specify a tooltip for a field in the Inspector window.</para>

View file

@ -10,7 +10,7 @@ using System.Linq;
using System.Reflection;
using MessagePack;
public class DatabaseCache
public class CultCache
{
public event Action<DatabaseEntry> OnDataInsertLocal;
public event Action<DatabaseEntry> OnDataUpdateLocal;
@ -42,10 +42,11 @@ public class DatabaseCache
}
}
private readonly Dictionary<Guid, ExternalEntry> _externalEntries = new Dictionary<Guid, ExternalEntry>();
private readonly Dictionary<Type, FieldInfo[]> _linkFields = new Dictionary<Type, FieldInfo[]>();
public IEnumerable<DatabaseEntry> AllEntries => _entries.Values;
public DatabaseCache(string filePath)
public CultCache(string filePath)
{
RegisterResolver.Register();
_filePath = filePath;
@ -57,6 +58,9 @@ public class DatabaseCache
// This is mainly to accomodate large files like datasets
foreach (var type in typeof(DatabaseEntry).GetAllChildClasses())
{
var linkFields = type.GetFields().Where(f => f.FieldType.IsAssignableToGenericType(typeof(DatabaseLink<>))).ToArray();
if(linkFields.Length > 0)
_linkFields[type] = linkFields;
if (type.GetCustomAttribute<ExternalEntryAttribute>() != null)
{
var typeDirectory = dataDirectory.CreateSubdirectory(type.Name);
@ -88,6 +92,17 @@ public class DatabaseCache
// If an external entry is added, store it separately
var type = entry.GetType();
if (_linkFields.ContainsKey(type))
{
foreach (var linkField in _linkFields[type])
{
var link = (DatabaseLinkBase) linkField.GetValue(entry);
if (link != null)
{
link.Cache = this;
}
}
}
if (_externalTypes.ContainsKey(type))
{
if (_externalEntries.ContainsKey(entry.ID))

View file

@ -17,9 +17,7 @@ public interface INamedEntry
string EntryName { get; set; }
}
[InspectableField,
MessagePackObject,
//MessagePackFormatter(typeof(TypelessFormatter)),
[MessagePackObject,
JsonObject(MemberSerialization.OptIn),
JsonConverter(typeof(JsonKnownTypesConverter<DatabaseEntry>)),
Union(0, typeof(SimpleCommodityData)),
@ -33,7 +31,7 @@ public interface INamedEntry
Union(9, typeof(NameFile)),
//Union(10, typeof(ZoneData)),
Union(11, typeof(PlayerData)),
Union(12, typeof(Corporation)),
// Union(12, typeof(Corporation)),
Union(13, typeof(Faction)),
Union(14, typeof(OrbitalEntity)),
Union(15, typeof(OrbitData)),
@ -41,8 +39,8 @@ public interface INamedEntry
Union(17, typeof(PersonalityAttribute)),
Union(18, typeof(AgentTask)),
// Union(19, typeof(LoadoutData)),
Union(20, typeof(Ship)),
Union(21, typeof(Mining)),
Union(20, typeof(Ship)),
Union(21, typeof(Mining)),
Union(22, typeof(StationTowing)),
Union(23, typeof(Survey)),
Union(24, typeof(HaulingTask)),
@ -57,6 +55,21 @@ public abstract class DatabaseEntry
{
[JsonProperty("id"), Key(0)]
public Guid ID = Guid.NewGuid();
}
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class DatabaseLink<T> : DatabaseLinkBase where T : DatabaseEntry
{
[IgnoreMember]
public T Value => Cache.Get<T>(LinkID);
}
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class DatabaseLinkBase
{
[Key(0), JsonProperty("linkID")]
public Guid LinkID;
public DatabaseEntry(){}
[IgnoreMember]
public CultCache Cache;
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
public static class CultCacheExtensions
@ -56,7 +57,35 @@ public static class CultCacheExtensions
return ChildClasses[type] = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(ass => ass.GetTypes()).Where(type.IsAssignableFrom).ToArray();
}
private static Dictionary<Type,Type[]> GenericChildClasses = new Dictionary<Type, Type[]>();
public static Type[] GetAllGenericChildClasses(this Type genericType)
{
if (GenericChildClasses.ContainsKey(genericType))
return ChildClasses[genericType];
return ChildClasses[genericType] = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(ass => ass.GetTypes()).Where(type=>type.IsAssignableToGenericType(genericType)).ToArray();
}
public static bool IsAssignableToGenericType(this Type givenType, Type genericType)
{
var interfaceTypes = givenType.GetInterfaces();
foreach (var it in interfaceTypes)
{
if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)
return true;
}
if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
return true;
Type baseType = givenType.BaseType;
if (baseType == null) return false;
return IsAssignableToGenericType(baseType, genericType);
}
public static async void WrapAwait(this Task task)
{
await task;