Database Improvements

Brought in all the myriad improvements to the database tools from the Stream Pixels project
Added configurable nested entry grouping
This commit is contained in:
Reinier Vens 2021-04-07 01:27:35 +01:00
commit 968783bf9c
37 changed files with 865 additions and 580 deletions

View file

@ -34,10 +34,10 @@ public class AetheriaDatabaseView : DatabaseListView
data => data.HardpointType,
type => Enum.GetName(typeof(HardpointType), type),
(data, type) => data.Hardpoint = type),
// new DatabaseEntryGroup<GearData, Guid>(
// data => data.Manufacturer,
// factionID => cultCache.Get<Faction>(factionID).ShortName,
// (data, faction) => data.Manufacturer = faction),
new DatabaseEntryGroup<GearData, Guid>(
data => data.Manufacturer,
factionID => factionID == Guid.Empty ? "Default" : cultCache.Get<Faction>(factionID).ShortName,
(data, faction) => data.Manufacturer = faction),
new DatabaseEntryGroup<HullData, HullType>(
data => data.HullType,
type => Enum.GetName(typeof(HullType), type),

View file

@ -25,16 +25,19 @@ public class DatabaseInspector : EditorWindow
// Singleton to avoid multiple instances of window.
private static DatabaseInspector _instance;
public static DatabaseInspector Instance => _instance ? _instance : GetWindow<DatabaseInspector>();
private Vector2 _view;
private const int width = 150;
private GUIStyle _warning;
private HashSet<int> _listItemFoldouts = new HashSet<int>();
private Material _schematicMat;
private Dictionary<(Type targetType, HashSet<Type> attributes), (object instance, Type type)> _inspectors =
new Dictionary<(Type targetType, HashSet<Type> attributes), (object instance, Type type)>();
private static bool _listItemStyle;
public static GUIStyle ListItemStyle => (_listItemStyle = !_listItemStyle) ? DatabaseListView.ListStyleEven : DatabaseListView.ListStyleOdd;
private Dictionary<Type, Type> _genericInspectors = new Dictionary<Type, Type>();
public const int Width = 150;
private Vector2 _view;
private GUIStyle _warning;
private List<(Type targetType, HashSet<Type> attributes, Type inspectorType, object instance)> _inspectors =
new List<(Type targetType, HashSet<Type> attributes, Type inspectorType, object instance)>();
private List<(Type targetType, HashSet<Type> attributes, Type inspectorType, Dictionary<Type, object> instances)> _genericInspectors =
new List<(Type targetType, HashSet<Type> attributes, Type inspectorType, Dictionary<Type, object> instances)>();
private HashSet<object> _foldouts = new HashSet<object>();
public Color LabelColor => EditorGUIUtility.isProSkin ? Color.white : Color.black;
@ -57,105 +60,138 @@ public class DatabaseInspector : EditorWindow
{
if (genericArguments[0].ContainsGenericParameters)
{
_genericInspectors.Add(genericArguments[0].GetGenericTypeDefinition(), type);
_genericInspectors.Add((
genericArguments[0].GetGenericTypeDefinition(),
new HashSet<Type>(genericArguments.Skip(1)),
type,
new Dictionary<Type, object>()
));
}
else
{
_inspectors.Add((genericArguments[0], new HashSet<Type>(genericArguments.Skip(1))), (Activator.CreateInstance(type), type));
_inspectors.Add((
genericArguments[0],
new HashSet<Type>(genericArguments.Skip(1)),
type,
Activator.CreateInstance(type)
));
}
}
}
}
public void Inspect(object obj, bool inspectablesOnly = false)
public void Inspect(object obj, bool inspectablesOnly = true)
{
foreach (var field in obj.GetType().GetFields().OrderBy(f=>f.GetCustomAttribute<KeyAttribute>()?.IntKey ?? 0))
{
Inspect(obj, field, inspectablesOnly);
}
// if (obj is EquippableItemData equippableItemData)
// {
// var restricted = false;
// var doubleRestricted = false;
// HullType type = HullType.Ship;
// foreach (var behavior in equippableItemData.Behaviors)
// {
// var restriction = behavior.GetType().GetCustomAttribute<EntityTypeRestrictionAttribute>();
// if (restriction != null)
// {
// if (restricted && restriction.Type != type)
// doubleRestricted = true;
// restricted = true;
// type = restriction.Type;
// }
// }
// if(doubleRestricted)
// GUILayout.Label("ITEM UNEQUIPPABLE: BEHAVIOR HULL TYPE CONFLICT", _warning);
// else if (restricted)
// GUILayout.Label($"Item restricted by behavior to hull type {Enum.GetName(typeof(HullType), type)}", EditorStyles.boldLabel);
// }
}
public void Inspect(object obj, FieldInfo field, bool inspectablesOnly = false)
public void Inspect(object obj, FieldInfo field, bool inspectablesOnly = true)
{
var inspectable = field.GetCustomAttribute<InspectableAttribute>();
if (inspectable == null && inspectablesOnly) return;
var attributes = field.GetCustomAttributes(false);
var type = field.FieldType;
var value = field.GetValue(obj);
field.SetValue(obj, InspectMember(field.Name.SplitCamelCase(), value, obj, type, false, attributes));
}
if (type.IsGenericType)
public object InspectMember(string label, object value, object parent, Type type, bool suppressContainer = false, params object[] attributes)
{
var style = _listItemStyle;
object GetAttribute(Type attributeType)
{
var genericTypeDefinition = type.GetGenericTypeDefinition();
if(_genericInspectors.ContainsKey(genericTypeDefinition))
{
var genericInspectorGenericType = _genericInspectors[genericTypeDefinition];
var genericInspectorType = genericInspectorGenericType.MakeGenericType(type.GenericTypeArguments[0]);
var genericInspector = Activator.CreateInstance(genericInspectorType);
var parameters = new[] {field.Name.SplitCamelCase(), field.GetValue(obj), obj, this};
field.SetValue(obj, genericInspectorType.GetMethod("Inspect").Invoke(genericInspector, parameters));
return;
}
return attributes.FirstOrDefault(attributeType.IsInstanceOfType);
}
var potentialInspectors = _inspectors.Keys
if (value == null && type != typeof(string) && !(type.IsInterface || type.IsAbstract)) value = Activator.CreateInstance(type);
var potentialInspectors = _inspectors
.Where(pi =>
pi.targetType == type &&
pi.attributes.All(attributeType => field.GetCustomAttribute(attributeType) != null));
pi.attributes.All(attributeType => GetAttribute(attributeType) != null));
if (potentialInspectors.Any())
{
var preferredInspector = potentialInspectors.MaxBy(i => i.attributes.Count);
var parameters = new[] {field.Name.SplitCamelCase(), field.GetValue(obj), obj, this}
.Concat(preferredInspector.attributes.Select<Type, object>(attributeType => field.GetCustomAttribute(attributeType)))
var parameters = new[] {label, value, parent, this}
.Concat(preferredInspector.attributes.Select(GetAttribute))
.ToArray();
field.SetValue(obj, _inspectors[preferredInspector].type.GetMethod("Inspect").Invoke(_inspectors[preferredInspector].instance, parameters));
value = preferredInspector.inspectorType.GetMethod("Inspect").Invoke(preferredInspector.instance, parameters);
_listItemStyle = style;
return value;
}
else if (type.IsEnum)
if (type.IsEnum)
{
var isflags = type.GetCustomAttributes<FlagsAttribute>().Any();
var names = Enum.GetNames(field.FieldType);
field.SetValue(obj,
InspectEnum(field.Name.SplitCamelCase(), (int) field.GetValue(obj), isflags ? names.Skip(1).ToArray() : names, isflags));
var names = Enum.GetNames(type);
value = InspectEnum(label, (int) value, isflags ? names.Skip(1).ToArray() : names, isflags);
_listItemStyle = style;
return value;
}
else if (type.GetCustomAttribute<InspectableAttribute>() != null)
if (type.GetCustomAttribute<InspectableAttribute>() != null)
{
Space();
if(!suppressContainer)
GUILayout.BeginVertical(GUI.skin.box);
using (new HorizontalScope())
{
LabelField(field.Name.SplitCamelCase(), EditorStyles.boldLabel);
if (type.IsInterface)
if(!suppressContainer)
{
var subTypes = type.GetAllInterfaceClasses();
if (Foldout(_foldouts.Contains(value), label, true, EditorStyles.boldLabel))
_foldouts.Add(value);
else _foldouts.Remove(value);
}
if (type.IsInterface || type.IsAbstract)
{
var subTypes = type.GetAllChildClasses().Where(t=>!(t.IsInterface || t.IsAbstract)).ToArray();
var selected = value != null ? Array.IndexOf(subTypes, value.GetType()) + 1 : 0;
var newSelection = Popup(selected,
subTypes.Select(t => t.Name.SplitCamelCase()).Prepend("None").ToArray());
if(newSelection != selected)
field.SetValue(obj, value = newSelection==0?null:Activator.CreateInstance(subTypes[newSelection-1]));
} else if(value == null) field.SetValue(obj, value = Activator.CreateInstance(type));
if (newSelection != selected)
value = newSelection == 0 ? null : Activator.CreateInstance(subTypes[newSelection - 1]);
}
else if (value == null) value = Activator.CreateInstance(type);
}
if(value != null) Inspect(field.GetValue(obj), inspectablesOnly);
if (value != null && (_foldouts.Contains(value) || suppressContainer)) Inspect(value);
if(!suppressContainer)
GUILayout.EndVertical();
_listItemStyle = style;
return value;
}
else Debug.Log($"Field \"{field.Name}\" has unknown type {field.FieldType.Name}. No inspector was generated.");
if (type.IsGenericType)
{
var genericTypeDefinition = type.GetGenericTypeDefinition();
var potentialGenericInspectors = _genericInspectors
.Where(pi =>
pi.targetType == genericTypeDefinition &&
pi.attributes.All(attributeType => GetAttribute(attributeType) != null));
if (potentialGenericInspectors.Any())
{
var genericTypeArguments = type.GenericTypeArguments;
var preferredInspector = potentialGenericInspectors.MaxBy(i => i.attributes.Count);
var genericInspectorType = preferredInspector.inspectorType.MakeGenericType(genericTypeArguments);
if (!preferredInspector.instances.ContainsKey(genericInspectorType))
preferredInspector.instances[genericInspectorType] =
Activator.CreateInstance(genericInspectorType);
var parameters = new[] {label, value, parent, this};
value = genericInspectorType.GetMethod("Inspect").Invoke(preferredInspector.instances[genericInspectorType], parameters);
_listItemStyle = style;
return value;
}
}
Debug.Log($"Field \"{label}\" has unknown type {type.Name}. No inspector was generated.");
_listItemStyle = style;
return value;
}
public int InspectEnum(string label, int value, string[] enumOptions, bool flags)
@ -164,13 +200,13 @@ public class DatabaseInspector : EditorWindow
{
Space();
LabelField(label, EditorStyles.boldLabel);
using (var v = new VerticalScope(GUI.skin.box))
using (new VerticalScope(GUI.skin.box))
{
for (var i = 0; i < enumOptions.Length; i++)
{
using (var h = new HorizontalScope())
{
GUILayout.Label(enumOptions[i], GUILayout.Width(width));
GUILayout.Label(enumOptions[i], GUILayout.Width(Width));
var set = Toggle(1 << i == (1 << i & value));
if (set)
value |= 1 << i;
@ -184,7 +220,7 @@ public class DatabaseInspector : EditorWindow
using (var h = new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label(label, GUILayout.Width(Width));
return Popup(value, enumOptions);
}
}
@ -212,7 +248,8 @@ public class DatabaseInspector : EditorWindow
// Serialize inspected object so we can check whether it has actually changed
RegisterResolver.Register();
var previousBytes = MessagePackSerializer.Serialize(entry);
var previousBytes = MessagePackSerializer.Serialize<DatabaseEntry>(entry);
var previousHash = previousBytes.GetHashSHA1();
EditorGUI.BeginChangeCheck();
@ -224,7 +261,7 @@ public class DatabaseInspector : EditorWindow
// Debug.Log(entry.ToJson());
if (GUILayout.Button("Delete Entry"))
{
CultCache.Delete(entry);
CultCache.Remove(entry);
entry = null;
return;
}
@ -243,11 +280,12 @@ public class DatabaseInspector : EditorWindow
LabelField(dataEntry.ID.ToString());
}
Inspect(entry, true);
_listItemStyle = false;
Inspect(entry);
if (EditorGUI.EndChangeCheck())
{
if(!previousBytes.ByteEquals(MessagePackSerializer.Serialize(entry)))
if(previousHash != MessagePackSerializer.Serialize(entry).GetHashSHA1())
CultCache.Add(entry);
}

View file

@ -26,8 +26,9 @@ public abstract class DatabaseListView : EditorWindow
protected CultCache cultCache;
private static DatabaseInspector _inspector;
private Type[] _entryTypes;
private bool[] _typeFoldouts;
private Dictionary<string, (bool foldout, (Type entryType, bool foldout)[] types)> _tables;
//private Type[] _entryTypes;
private Type[] _globalTypes;
private string _connectionString;
private Vector2 _view;
private RethinkQueryStatus _queryStatus;
@ -70,6 +71,7 @@ public abstract class DatabaseListView : EditorWindow
public override int HashGroup(object o)
{
return o.GetHashCode();
if (o is K k) return k.GetHashCode();
return 0;
}
@ -94,8 +96,7 @@ public abstract class DatabaseListView : EditorWindow
public Color LabelColor => EditorGUIUtility.isProSkin ? Color.white : Color.black;
private bool _listItemStyle;
public GUIStyle ListItemStyle =>
(_listItemStyle = !_listItemStyle) ? ListStyleEven : ListStyleOdd;
public GUIStyle ListItemStyle => (_listItemStyle = !_listItemStyle) ? ListStyleEven : ListStyleOdd;
void OnEnable()
{
@ -114,19 +115,22 @@ public abstract class DatabaseListView : EditorWindow
cultCache.OnDataUpdateLocal += _ => Repaint();
cultCache.OnDataInsertLocal += _ => Repaint();
cultCache.OnDataDeleteLocal += _ => Repaint();
cultCache.OnDataRemoveLocal += _ => Repaint();
cultCache.OnDataUpdateRemote += _ => EditorDispatcher.Dispatch(Repaint);
cultCache.OnDataInsertRemote += _ => EditorDispatcher.Dispatch(Repaint);
cultCache.OnDataDeleteRemote += _ => EditorDispatcher.Dispatch(Repaint);
cultCache.OnDataRemoveRemote += _ => EditorDispatcher.Dispatch(Repaint);
DatabaseInspector.CultCache = cultCache;
_inspector = DatabaseInspector.Instance;
_inspector.Show();
_entryTypes = typeof(DatabaseEntry).GetAllChildClasses()
.Where(t => t.GetCustomAttribute<InspectableAttribute>() != null).ToArray();
_typeFoldouts = new bool[_entryTypes.Length];
_globalTypes = typeof(DatabaseEntry).GetAllChildClasses()
.Where(t => t.GetCustomAttribute<InspectableAttribute>() != null && t.GetCustomAttribute<GlobalSettingsAttribute>() != null).ToArray();
_tables = typeof(DatabaseEntry).GetAllChildClasses()
.Where(t => t.GetCustomAttribute<InspectableAttribute>() != null && t.GetCustomAttribute<GlobalSettingsAttribute>() == null)
.GroupBy(t=>t.GetCustomAttribute<RethinkTableAttribute>()?.TableName ??
(t.GetCustomAttribute<ExternalEntryAttribute>() != null ? "External" : "Default"))
.ToDictionary(group=>group.Key, group=> (false, group.Select(t=>(t, false)).ToArray()));
if (EditorPrefs.HasKey("RethinkDB.URL"))
_connectionString = EditorPrefs.GetString("RethinkDB.URL");
@ -154,7 +158,7 @@ public abstract class DatabaseListView : EditorWindow
// Indicate that we don't accept drags ourselves
DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
using (var h = new HorizontalScope())
using (new HorizontalScope())
{
_connectionString = TextField(_connectionString);
if (GUILayout.Button("Connect"))
@ -163,13 +167,8 @@ public abstract class DatabaseListView : EditorWindow
JsonKnownTypesSettingsManager.RegisterTypeAssembly<ItemData>();
_queryStatus = RethinkConnection.RethinkConnect(cultCache, _connectionString, DatabaseName);
}
// if (GUILayout.Button("Connect All"))
// {
// EditorPrefs.SetString("RethinkDB.URL", _connectionString);
// _queryStatus = RethinkConnection.RethinkConnect(_databaseCache, _connectionString, true, false);
// }
}
using (var h = new HorizontalScope())
using (new HorizontalScope())
{
//_fileName = TextField(_fileName);
if (GUILayout.Button("Save"))
@ -201,110 +200,132 @@ public abstract class DatabaseListView : EditorWindow
GUI.skin.scrollView,
GUILayout.Width(EditorGUIUtility.currentViewWidth),
GUILayout.ExpandHeight(true));
for (var i = 0; i < _entryTypes.Length; i++)
{
using (var h = new HorizontalScope(ListItemStyle))
{
_typeFoldouts[i] = Foldout(_typeFoldouts[i],
((NameAttribute) _entryTypes[i].GetCustomAttribute(typeof(NameAttribute)))?.Name ?? FormatTypeName(_entryTypes[i].Name),
true);
}
if (_typeFoldouts[i])
foreach (var globalType in _globalTypes)
{
var globalEntry = cultCache.GetGlobal(globalType);
var style = ListItemStyle;
var selected = SelectedItem == globalEntry.ID;
using (new HorizontalScope(selected?SelectedStyle:style))
{
// TODO: NESTED GROUPS!
var entries = cultCache.GetAll(_entryTypes[i])
.OrderBy(entry => entry is INamedEntry namedEntry ? namedEntry.EntryName : entry.ID.ToString());
var grouper = Groupers.FirstOrDefault(g => g.CanGroup(_entryTypes[i]));
if (grouper != null)
using (var h = new HorizontalScope())
{
var groups = entries.GroupBy(o => grouper.SelectGroup(o));
foreach (var group in groups)
if (h.rect.Contains(currentEvent.mousePosition))
{
var groupHash = grouper.HashGroup(group.Key);
var groupName = grouper.GetGroupName(group.Key);
if (currentEventType == EventType.MouseUp)
Select(globalEntry);
}
GUILayout.Label(globalType.Name.SplitCamelCase(), selected ? EditorStyles.whiteLabel : GUI.skin.label);
}
}
}
void DisplayGroup(Type entryType, IEnumerable<DatabaseEntry> entries, IEnumerable<DatabaseEntryGroup> nestedGroups, int indent = 2, Action<DatabaseEntry> activatePreviousGroups = null, string previousGroupNames = "")
{
var grouper = nestedGroups.FirstOrDefault();
if (grouper != null)
{
var groups = entries.GroupBy(o => grouper.SelectGroup(o));
foreach (var group in groups)
{
//var groupHash = grouper.HashGroup(group.Key);
var groupName = grouper.GetGroupName(group.Key);
var groupHash = groupName.GetHashCode();
using (new HorizontalScope(ListItemStyle))
{
GUILayout.Space(indent*10);
if (Foldout(_groupFoldouts.Contains(groupHash), FormatTypeName(groupName), true))
_groupFoldouts.Add(groupHash);
else _groupFoldouts.Remove(groupHash);
}
if (_groupFoldouts.Contains(groupHash))
{
Action<DatabaseEntry> activate = entry =>
{
activatePreviousGroups?.Invoke(entry);
grouper.Activate(entry, group.Key);
};
DisplayGroup(entryType, group, nestedGroups.Skip(1), indent + 1, activate, $"{groupName} {previousGroupNames}");
using (var h = new HorizontalScope(ListItemStyle))
{
GUILayout.Space(10);
if (Foldout(_groupFoldouts.Contains(groupHash), FormatTypeName(groupName), true))
_groupFoldouts.Add(groupHash);
else _groupFoldouts.Remove(groupHash);
}
if (_groupFoldouts.Contains(groupHash))
{
int index = 0;
foreach (var entry in group)
{
index++;
var style = ListItemStyle;
var selected = SelectedItem == entry.ID;
using (new HorizontalScope(selected?SelectedStyle:style))
{
using (var h = new HorizontalScope())
{
if (h.rect.Contains(currentEvent.mousePosition))
{
if (currentEventType == EventType.MouseDrag)
{
DragAndDrop.PrepareStartDrag();
DragAndDrop.SetGenericData("Item", entry.ID);
DragAndDrop.StartDrag("Database Item");
}
else if (currentEventType == EventType.MouseUp)
Select(entry);
}
GUILayout.Space(20);
GUILayout.Label((entry as INamedEntry)?.EntryName ?? index.ToString(), selected ? EditorStyles.whiteLabel : GUI.skin.label);
}
}
}
using (var h = new HorizontalScope(ListItemStyle))
{
if(GUI.Button(h.rect, GUIContent.none, GUIStyle.none))
CreateItem(_entryTypes[i], entry => grouper.Activate(entry, group.Key));
GUILayout.Space(20);
GUILayout.Label($"New {groupName}");
}
if (GUI.Button(h.rect, GUIContent.none, GUIStyle.none))
CreateItem(entryType, activate);
GUILayout.Space((indent+1)*10);
GUILayout.Label($"Create {groupName} {previousGroupNames}");
}
}
}
else
}
else
{
int index = 0;
foreach (var entry in entries)
{
int index = 0;
foreach (var entry in entries)
index++;
var style = ListItemStyle;
var selected = SelectedItem == entry.ID;
using (var h = new HorizontalScope(selected ? SelectedStyle : style))
{
index++;
var style = ListItemStyle;
var selected = SelectedItem == entry.ID;
using (var h = new HorizontalScope(selected?SelectedStyle:style))
if (h.rect.Contains(currentEvent.mousePosition))
{
if (h.rect.Contains(currentEvent.mousePosition))
if (currentEventType == EventType.MouseDrag)
{
if (currentEventType == EventType.MouseDrag)
{
DragAndDrop.PrepareStartDrag();
DragAndDrop.SetGenericData("Item", entry.ID);
DragAndDrop.StartDrag("Database Item");
}
else if (currentEventType == EventType.MouseUp)
Select(entry);
DragAndDrop.PrepareStartDrag();
DragAndDrop.SetGenericData("Item", entry.ID);
DragAndDrop.StartDrag("Database Item");
}
GUILayout.Space(10);
GUILayout.Label((entry as INamedEntry)?.EntryName ?? index.ToString(), selected ? EditorStyles.whiteLabel : GUI.skin.label);
else if (currentEventType == EventType.MouseUp)
Select(entry);
}
GUILayout.Space(indent*10);
GUILayout.Label((entry as INamedEntry)?.EntryName ?? index.ToString(),
selected ? EditorStyles.whiteLabel : GUI.skin.label);
}
}
using (var h = new HorizontalScope(ListItemStyle))
}
}
foreach (var table in _tables.Keys.ToArray())
{
var (tableFoldout, _) = _tables[table];
using (new HorizontalScope(ListItemStyle))
{
_tables[table] = (Foldout(tableFoldout = _tables[table].foldout, table, true), _tables[table].types);
}
if (tableFoldout)
{
for (var i = 0; i < _tables[table].types.Length; i++)
{
if(GUI.Button(h.rect, GUIContent.none, GUIStyle.none))
CreateItem(_entryTypes[i]);
GUILayout.Space(10);
GUILayout.Label($"New {_entryTypes[i].Name}");
var (entryType, typeFoldout) = _tables[table].types[i];
using (new HorizontalScope(ListItemStyle))
{
GUILayout.Space(10);
_tables[table].types[i] = (entryType, typeFoldout = Foldout(typeFoldout,
((NameAttribute) entryType.GetCustomAttribute(typeof(NameAttribute)))?.Name ?? FormatTypeName(entryType.Name),
true));
}
if (typeFoldout)
{
var entries = cultCache.GetAll(entryType)
.OrderBy(entry => entry is INamedEntry namedEntry ? namedEntry.EntryName : entry.ID.ToString());
var groupers = Groupers.Where(g => g.CanGroup(entryType));
DisplayGroup(entryType, entries, groupers);
using (var h = new HorizontalScope(ListItemStyle))
{
if (GUI.Button(h.rect, GUIContent.none, GUIStyle.none))
CreateItem(entryType);
GUILayout.Space(20);
GUILayout.Label($"Create {FormatTypeName(entryType.Name)}");
}
}
}
}
}
@ -312,7 +333,7 @@ public abstract class DatabaseListView : EditorWindow
EndScrollView();
}
private string FormatTypeName(string typeName)
public static string FormatTypeName(string typeName)
{
return (typeName.EndsWith("Data")
? typeName.Substring(0, typeName.Length - 4)

View file

@ -12,7 +12,7 @@ public class AnimationCurveFloat4Inspector : BaseInspector<float4[]>
var val = value != null && value.Length > 0
? value.ToCurve()
: new AnimationCurve();
using (var h = new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
val = CurveField(val, Color.yellow, new Rect(0, 0, 1, 1));
@ -22,6 +22,25 @@ public class AnimationCurveFloat4Inspector : BaseInspector<float4[]>
}
}
public class BezierCurveInspector : BaseInspector<BezierCurve>
{
public override BezierCurve Inspect(string label, BezierCurve value, object parent, DatabaseInspector inspectorWindow)
{
var val = value != null && value.Keys != null && value.Keys.Length > 0
? value.ToCurve()
: new AnimationCurve();
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
val = CurveField(val, Color.yellow, new Rect(0, 0, 1, 1));
}
value.Keys = val.keys.Select(k => float4(k.time, k.value, k.inTangent, k.outTangent)).ToArray();
return value;
}
}
public class AnimationCurveInspect : BaseInspector<AnimationCurve>
{
public override AnimationCurve Inspect(string label, AnimationCurve value, object parent, DatabaseInspector inspectorWindow)

View file

@ -8,32 +8,35 @@ public class DatabaseLinkInspector : BaseInspector<Guid, InspectableDatabaseLink
{
public override Guid Inspect(string label, Guid value, object parent, DatabaseInspector inspectorWindow, InspectableDatabaseLinkAttribute link)
{
LabelField(label, EditorStyles.boldLabel);
var valueEntry = DatabaseInspector.CultCache.Get(value);
using (var h = new HorizontalScope(GUI.skin.box))
using (new HorizontalScope())
{
GUILayout.Label((valueEntry as INamedEntry)?.EntryName ?? valueEntry?.ID.ToString() ?? $"Assign a {link.EntryType.Name} by dragging from the list panel.");
if (h.rect.Contains(Event.current.mousePosition) && (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform))
GUILayout.Label(label, GUILayout.Width(width));
var valueEntry = DatabaseInspector.CultCache.Get(value);
using (var h = new HorizontalScope(GUI.skin.box))
{
var guid = (Guid) DragAndDrop.GetGenericData("Item");
var dragEntry = DatabaseInspector.CultCache.Get(guid);
var dragValid = dragEntry != null && link.EntryType.IsInstanceOfType(dragEntry) && dragEntry != valueEntry;
if(Event.current.type == EventType.DragUpdated)
DragAndDrop.visualMode = dragValid ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;
else if(Event.current.type == EventType.DragPerform)
GUILayout.Label((valueEntry as INamedEntry)?.EntryName ?? valueEntry?.ID.ToString() ?? $"Drag and drop: {DatabaseListView.FormatTypeName(link.EntryType.Name)}");
if (h.rect.Contains(Event.current.mousePosition) && (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform))
{
if (dragValid)
var guid = (Guid) DragAndDrop.GetGenericData("Item");
var dragEntry = DatabaseInspector.CultCache.Get(guid);
var dragValid = dragEntry != null && link.EntryType.IsInstanceOfType(dragEntry) && dragEntry != valueEntry;
if(Event.current.type == EventType.DragUpdated)
DragAndDrop.visualMode = dragValid ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;
else if(Event.current.type == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
GUI.changed = true;
return guid;
if (dragValid)
{
DragAndDrop.AcceptDrag();
GUI.changed = true;
return guid;
}
}
}
}
if (GUILayout.Button("-", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
{
return Guid.Empty;
if (GUILayout.Button("-", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
{
return Guid.Empty;
}
}
}
@ -46,13 +49,19 @@ public class DatabaseLinkObjectInspector<T> : BaseInspector<DatabaseLink<T>> whe
public override DatabaseLink<T> Inspect(string label, DatabaseLink<T> value, object parent, DatabaseInspector inspectorWindow)
{
if (value == null)
value = new DatabaseLink<T>{Cache = DatabaseInspector.CultCache};
LabelField(label, EditorStyles.boldLabel);
var valueEntry = value.Value;
using (var h = new HorizontalScope(GUI.skin.box))
value = new DatabaseLink<T>();
if(!string.IsNullOrEmpty(label))
{
GUILayout.Label((valueEntry as INamedEntry)?.EntryName ?? valueEntry?.ID.ToString() ?? $"Assign a {typeof(T).Name} by dragging from the list panel.");
GUILayout.BeginHorizontal();
GUILayout.Label(label, GUILayout.Width(width));
}
var valueEntry = value.Value;
using (var h = string.IsNullOrEmpty(label) ? new HorizontalScope() : new HorizontalScope(GUI.skin.box))
{
GUILayout.Label((valueEntry as INamedEntry)?.EntryName ?? valueEntry?.ID.ToString() ?? $"Drag and drop: {DatabaseListView.FormatTypeName(typeof(T).Name)}");
if (h.rect.Contains(Event.current.mousePosition) && (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform))
{
var guid = (Guid) DragAndDrop.GetGenericData("Item");
@ -70,11 +79,21 @@ public class DatabaseLinkObjectInspector<T> : BaseInspector<DatabaseLink<T>> whe
}
}
}
if (GUILayout.Button("-", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
if(value.LinkID != Guid.Empty)
{
value.LinkID = Guid.Empty;
if (GUILayout.Button("-",
GUILayout.Width((EditorGUIUtility.singleLineHeight - 3) * 2),
GUILayout.Height(EditorGUIUtility.singleLineHeight - 3)))
{
value.LinkID = Guid.Empty;
}
}
}
if(!string.IsNullOrEmpty(label))
{
GUILayout.EndHorizontal();
}
return value;
}
@ -92,11 +111,9 @@ public class DatabaseLinkListInspector : BaseInspector<List<Guid>, InspectableDa
DatabaseInspector inspectorWindow,
InspectableDatabaseLinkAttribute link)
{
Space();
LabelField(label, EditorStyles.boldLabel);
using (var v = new VerticalScope(GUI.skin.box))
{
GUILayout.Label(label, EditorStyles.boldLabel);
if (value.Count == 0)
{
using (new HorizontalScope(ListItemStyle))

View file

@ -7,10 +7,6 @@ using static UnityEditor.EditorGUILayout;
public class FloatDictionaryInspector : BaseInspector<Dictionary<Guid, float>, InspectableDatabaseLinkAttribute>
{
private bool _listItemStyle;
public GUIStyle ListItemStyle =>
(_listItemStyle = !_listItemStyle) ? DatabaseListView.ListStyleEven : DatabaseListView.ListStyleOdd;
public override Dictionary<Guid, float> Inspect(string label,
Dictionary<Guid, float> value,
object parent,
@ -24,14 +20,14 @@ public class FloatDictionaryInspector : BaseInspector<Dictionary<Guid, float>, I
{
if (value.Count == 0)
{
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
GUILayout.Label("Drag from list to add item");
}
}
foreach (var ingredient in value.ToArray())
{
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
var entry = DatabaseInspector.CultCache.Get(ingredient.Key);
if (entry == null)
@ -79,10 +75,6 @@ public class FloatDictionaryInspector : BaseInspector<Dictionary<Guid, float>, I
public class RangedFloatDictionaryInspector : BaseInspector<Dictionary<Guid, float>, InspectableDatabaseLinkAttribute, RangedFloatAttribute>
{
private bool _listItemStyle;
public GUIStyle ListItemStyle =>
(_listItemStyle = !_listItemStyle) ? DatabaseListView.ListStyleEven : DatabaseListView.ListStyleOdd;
public override Dictionary<Guid, float> Inspect(string label,
Dictionary<Guid, float> value,
object parent,
@ -97,14 +89,14 @@ public class RangedFloatDictionaryInspector : BaseInspector<Dictionary<Guid, flo
{
if (value.Count == 0)
{
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
GUILayout.Label("Drag from list to add item");
}
}
foreach (var ingredient in value.ToArray())
{
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
var entry = DatabaseInspector.CultCache.Get(ingredient.Key);
if (entry == null)
@ -152,10 +144,6 @@ public class RangedFloatDictionaryInspector : BaseInspector<Dictionary<Guid, flo
public class IntDictionaryInspector : BaseInspector<Dictionary<Guid, int>, InspectableDatabaseLinkAttribute>
{
private bool _listItemStyle;
public GUIStyle ListItemStyle =>
(_listItemStyle = !_listItemStyle) ? DatabaseListView.ListStyleEven : DatabaseListView.ListStyleOdd;
public override Dictionary<Guid, int> Inspect(string label,
Dictionary<Guid, int> value,
object parent,
@ -169,14 +157,14 @@ public class IntDictionaryInspector : BaseInspector<Dictionary<Guid, int>, Inspe
{
if (value.Count == 0)
{
using (var h = new HorizontalScope(ListItemStyle))
using (var h = new HorizontalScope(DatabaseInspector.ListItemStyle))
{
GUILayout.Label("Drag from list to add item");
}
}
foreach (var ingredient in value.ToArray())
{
using (var h = new HorizontalScope(ListItemStyle))
using (var h = new HorizontalScope(DatabaseInspector.ListItemStyle))
{
var entry = DatabaseInspector.CultCache.Get(ingredient.Key);
if (entry == null)

View file

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using static Unity.Mathematics.math;
using static UnityEditor.EditorGUILayout;
public class EnumInspector<T> : BaseInspector<List<T>, InspectableEnumValuesAttribute>
{
public override List<T> Inspect(string label, List<T> list, object parent, DatabaseInspector inspectorWindow, InspectableEnumValuesAttribute attribute)
{
var names = Enum.GetNames(attribute.EnumType);
list ??= new List<T>();
while(list.Count < names.Length) list.Add(Activator.CreateInstance<T>());
while(list.Count > names.Length) list.RemoveAt(list.Count-1);
for (var i = 0; i < names.Length; i++)
inspectorWindow.InspectMember(names[i], list[i], parent, typeof(T));
return list;
}
}
public class EnumDictionaryInspector<E, T> : BaseInspector<EnumDictionary<E, T>> where E : Enum
{
private static HashSet<object> _foldouts = new HashSet<object>();
public override EnumDictionary<E, T> Inspect(string label, EnumDictionary<E, T> dict, object parent, DatabaseInspector inspectorWindow)
{
var names = Enum.GetNames(typeof(E));
if (dict == null || dict.Values.Length != names.Length)
{
var newValue = new T[names.Length];
if(dict != null && dict.Values.Length > 0)
Array.Copy(dict.Values, newValue, min(dict.Values.Length, newValue.Length));
dict.Values = newValue;
}
using (new VerticalScope(GUI.skin.box))
{
if (Foldout(_foldouts.Contains(dict), label, true, EditorStyles.boldLabel))
_foldouts.Add(dict);
else _foldouts.Remove(dict);
if(_foldouts.Contains(dict))
{
for (var i = 0; i < names.Length; i++)
{
using (new VerticalScope(DatabaseInspector.ListItemStyle))
{
dict.Values[i] = (T) inspectorWindow.InspectMember(names[i], dict.Values[i], parent, typeof(T));
}
}
}
}
return dict;
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: da1b15e5564747958639c8f959226c74
timeCreated: 1617335525

View file

@ -14,13 +14,13 @@ public class FloatInspector : BaseInspector<float>
}
}
public class RangedFloatInspector : BaseInspector<float, RangedFloatInspectableAttribute>
public class RangedFloatInspector : BaseInspector<float, InspectableRangedFloatAttribute>
{
public override float Inspect(string label,
float value,
object parent,
DatabaseInspector inspectorWindow,
RangedFloatInspectableAttribute attribute)
InspectableRangedFloatAttribute attribute)
{
using (new HorizontalScope())
{

View file

@ -14,9 +14,9 @@ public class IntInspector : BaseInspector<int>
}
}
public class RangedIntInspector : BaseInspector<int, RangedIntInspectableAttribute>
public class RangedIntInspector : BaseInspector<int, InspectableRangedIntAttribute>
{
public override int Inspect(string label, int value, object parent, DatabaseInspector inspectorWindow, RangedIntInspectableAttribute attribute)
public override int Inspect(string label, int value, object parent, DatabaseInspector inspectorWindow, InspectableRangedIntAttribute attribute)
{
using (new HorizontalScope())
{

View file

@ -9,46 +9,49 @@ using static UnityEditor.EditorGUILayout;
public class ListInspector<T> : BaseInspector<List<T>>
{
private bool _listItemStyle;
private static HashSet<int> _listItemFoldouts = new HashSet<int>();
public GUIStyle ListItemStyle =>
(_listItemStyle = !_listItemStyle) ? DatabaseListView.ListStyleEven : DatabaseListView.ListStyleOdd;
private static HashSet<object> _foldouts = new HashSet<object>();
public override List<T> Inspect(string label, List<T> list, object parent, DatabaseInspector inspectorWindow)
{
var listType = typeof(T);
if (listType.GetCustomAttribute<InspectableAttribute>() != null)
var inspectable = listType.GetCustomAttribute<InspectableAttribute>();
// if (listType.GetCustomAttribute<InspectableAttribute>() != null)
// {
//Space();
using (new VerticalScope(GUI.skin.box))
{
Space();
using (new VerticalScope(GUI.skin.box))
if (list == null)
{
if (list == null)
list = new List<T>();
}
else
{
var sorted = true;
var order = int.MinValue;
foreach(var element in list)
{
list = new List<T>();
if (element == null)
{
list.Remove(element);
return list;
}
var elementType = element.GetType();
var elementOrder = elementType.GetCustomAttribute<OrderAttribute>()?.Order ?? 0;
if (elementOrder < order)
sorted = false;
order = elementOrder;
}
else
if (!sorted)
{
var sorted = true;
var order = int.MinValue;
foreach(var element in list)
{
if (element == null)
{
list.Remove(element);
return list;
}
var elementType = element.GetType();
var elementOrder = elementType.GetCustomAttribute<OrderAttribute>()?.Order ?? 0;
if (elementOrder < order)
sorted = false;
order = elementOrder;
}
if (!sorted)
{
list.Sort((x, y) => (x.GetType().GetCustomAttribute<OrderAttribute>()?.Order ?? 0).CompareTo(y.GetType().GetCustomAttribute<OrderAttribute>()?.Order ?? 0));
}
list.Sort((x, y) => (x.GetType().GetCustomAttribute<OrderAttribute>()?.Order ?? 0).CompareTo(y.GetType().GetCustomAttribute<OrderAttribute>()?.Order ?? 0));
}
}
if (Foldout(_foldouts.Contains(list), label, true, EditorStyles.boldLabel))
_foldouts.Add(list);
else _foldouts.Remove(list);
if (_foldouts.Contains(list))
{
foreach (var o in list)
{
if (o == null)
@ -64,78 +67,65 @@ public class ListInspector<T> : BaseInspector<List<T>>
GUI.backgroundColor = tint.TintColor.ToColor();
}
using (new VerticalScope(ListItemStyle))
using (new VerticalScope(DatabaseInspector.ListItemStyle))
{
using (new HorizontalScope())
{
//if (listType.IsInterface)
if(Foldout(_listItemFoldouts.Contains(o.GetHashCode()), o.ToString(), true))
_listItemFoldouts.Add(o.GetHashCode());
else
_listItemFoldouts.Remove(o.GetHashCode());
if (inspectable != null)
{
if (Foldout(_foldouts.Contains(o), o.ToString(), true, EditorStyles.boldLabel))
_foldouts.Add(o);
else
_foldouts.Remove(o);
}
else inspectorWindow.InspectMember("", o, parent, listType, true);
// var rect = GetControlRect(false,
// GUILayout.Width(EditorGUIUtility.singleLineHeight));
// GUI.DrawTexture(rect, Icons.Instance.minus, ScaleMode.StretchToFill, true, 1,
// LabelColor, 0, 0);
if (GUILayout.Button("-", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
{
list.Remove(o);
break;
}
}
if(_listItemFoldouts.Contains(o.GetHashCode()))
{
Space();
inspectorWindow.Inspect(o, true);
}
if(inspectable != null && _foldouts.Contains(o))
inspectorWindow.InspectMember("", o, parent, o.GetType(), true);
}
if(_tinted)
GUI.backgroundColor = originalColor;
}
using (new HorizontalScope(ListItemStyle))
using (new HorizontalScope(DatabaseInspector.ListItemStyle))
{
GUILayout.Label($"Add new {listType.Name}", GUILayout.ExpandWidth(true));
GUILayout.Label($"Add new {listType.GetFullName()}", GUILayout.ExpandWidth(true));
// var rect = GetControlRect(false, GUILayout.Width(EditorGUIUtility.singleLineHeight));
// GUI.DrawTexture(rect, Icons.Instance.plus, ScaleMode.StretchToFill, true, 1, LabelColor, 0, 0);
if (GUILayout.Button("+", GUILayout.Width((EditorGUIUtility.singleLineHeight - 3)*2), GUILayout.Height(EditorGUIUtility.singleLineHeight-3)))
{
if (listType.IsInterface)
if (listType.IsAbstract || listType.IsInterface)
{
var menu = new GenericMenu();
foreach (var childType in listType.GetAllInterfaceClasses())
foreach (var childType in listType.GetAllChildClasses().Where(t=>!t.IsAbstract && !t.IsInterface))
{
menu.AddItem(new GUIContent(childType.Name), false, () =>
{
list.Add((T)Activator.CreateInstance(childType));
});
}
menu.ShowAsContext();
}
else if (listType.IsAbstract)
{
var menu = new GenericMenu();
foreach (var childType in listType.GetAllChildClasses().Where(t=>!t.IsAbstract))
{
menu.AddItem(new GUIContent(childType.Name), false, () =>
{
list.Add((T)Activator.CreateInstance(childType));
var o = (T) Activator.CreateInstance(childType);
_foldouts.Add(o);
list.Add(o);
});
}
menu.ShowAsContext();
}
else
{
list.Add(Activator.CreateInstance<T>());
var o = Activator.CreateInstance<T>();
_foldouts.Add(o);
list.Add(o);
}
}
}
}
}
else Debug.Log($"Field \"{label}\" is a list of non-Inspectable type {listType.Name}. No inspector was generated.");
// }
// else Debug.Log($"Field \"{label}\" is a list of non-Inspectable type {listType.Name}. No inspector was generated.");
return list;
}

View file

@ -3,12 +3,11 @@ using UnityEditor;
using UnityEngine;
using static UnityEditor.EditorGUILayout;
public class Float2Inspector : BaseInspector<float2>
{
public override float2 Inspect(string label, float2 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -24,7 +23,7 @@ public class Float3Inspector : BaseInspector<float3>
{
public override float3 Inspect(string label, float3 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -38,11 +37,23 @@ public class Float3Inspector : BaseInspector<float3>
}
}
public class Float3ColorInspector : BaseInspector<float3, InspectableColorAttribute>
{
public override float3 Inspect(string label, float3 value, object parent, DatabaseInspector inspectorWindow, InspectableColorAttribute attribute)
{
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
return ColorField(value.ToColor()).ToFloat3();
}
}
}
public class Float4Inspector : BaseInspector<float4>
{
public override float4 Inspect(string label, float4 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -58,11 +69,23 @@ public class Float4Inspector : BaseInspector<float4>
}
}
public class Float4ColorInspector : BaseInspector<float4, InspectableColorAttribute>
{
public override float4 Inspect(string label, float4 value, object parent, DatabaseInspector inspectorWindow, InspectableColorAttribute attribute)
{
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
return ColorField(value.ToColor()).ToFloat4();
}
}
}
public class Int2Inspector : BaseInspector<int2>
{
public override int2 Inspect(string label, int2 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -86,7 +109,7 @@ public class Int3Inspector : BaseInspector<int3>
{
public override int3 Inspect(string label, int3 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));
@ -104,7 +127,7 @@ public class Int4Inspector : BaseInspector<int4>
{
public override int4 Inspect(string label, int4 value, object parent, DatabaseInspector inspectorWindow)
{
using (new EditorGUILayout.HorizontalScope())
using (new HorizontalScope())
{
GUILayout.Label(label, GUILayout.Width(width));
GUILayout.Label("X", GUILayout.Width(labelWidth));

View file

@ -1,157 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RethinkDb.Driver;
using RethinkDb.Driver.Ast;
using RethinkDb.Driver.Net;
using UnityEngine;
public static class RethinkConnection
{
public static RethinkDB R = RethinkDB.R;
public static RethinkQueryStatus RethinkConnect(CultCache cache, string connectionString, string dbName, bool syncLocalChanges = true)
{
// Add Unity.Mathematics serialization support to RethinkDB Driver
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
new MathJsonConverter(),
Converter.DateTimeConverter,
Converter.BinaryConverter,
Converter.GroupingConverter,
Converter.PocoExprConverter
}
};
var syncTables = typeof(DatabaseEntry).GetAllChildClasses()
.Select(t => t.GetCustomAttribute<RethinkTableAttribute>()?.TableName ?? "Default").Distinct();
var connectionStringDomainLength = connectionString.IndexOf(':');
if (connectionStringDomainLength < 1)
throw new ArgumentException("Illegal Connection String: must include port!");
var portString = connectionString.Substring(connectionStringDomainLength + 1);
if (!int.TryParse(portString, out var port))
throw new ArgumentException($"Illegal connection string! \"{portString}\" is not a valid port number!");
var connection = R.Connection()
.Hostname(connectionString.Substring(0,connectionStringDomainLength))
.Port(port).Timeout(60).Connect();
Debug.Log("Connected to RethinkDB");
var tables = R.Db(dbName).TableList().RunAtom<string[]>(connection);
foreach (var st in syncTables.Where(st => !tables.Contains(st)))
R.Db(dbName).TableCreate(st).RunNoReply(connection);
if (syncLocalChanges)
{
// When entries are changed locally, push the changes to RethinkDB
cache.OnDataUpdateLocal += async entry =>
{
var table = entry.GetType().GetCustomAttribute<RethinkTableAttribute>()?.TableName ?? "Other";
var result = await R
.Db("Aetheria")
.Table(table)
.Get(entry.ID)
.Replace(entry)
.RunAsync(connection);
Debug.Log($"Uploaded entry to RethinkDB: {entry.ID} result: {result}");
};
cache.OnDataInsertLocal += async entry =>
{
var table = entry.GetType().GetCustomAttribute<RethinkTableAttribute>()?.TableName ?? "Other";
var result = await R
.Db("Aetheria")
.Table(table)
.Insert(entry)
.RunAsync(connection);
Debug.Log($"Inserted entry to RethinkDB: {entry.ID} result: {result}");
};
cache.OnDataDeleteLocal += async entry =>
{
var table = entry.GetType().GetCustomAttribute<RethinkTableAttribute>()?.TableName ?? "Other";
var result = await R
.Db("Aetheria")
.Table(table)
.Get(entry.ID)
.Delete()
.RunAsync(connection);
Debug.Log($"Deleted entry from RethinkDB: {entry.ID} result: {result}");
};
}
var status = new RethinkQueryStatus();
foreach (var table in syncTables)
{
// Get entries from RethinkDB
Task.Run(async () =>
{
status.TotalEntries += R
.Db("Aetheria")
.Table(table)
.Count().RunAtom<int>(connection);
var result = await R
.Db("Aetheria")
.Table(table)
.RunCursorAsync<DatabaseEntry>(connection);
while (await result.MoveNextAsync())
{
var entry = result.Current;
//Debug.Log($"Received Items entry from RethinkDB: {entry.GetType()} {(entry as INamedEntry)?.EntryName ?? ""}:{entry.ID}");
cache.Add(entry, true);
status.RetrievedEntries++;
}
}).WrapAwait();
// Subscribe to changes from RethinkDB
Task.Run(async () =>
{
var result = await R
.Db("Aetheria")
.Table(table)
.Changes()
.RunChangesAsync<DatabaseEntry>(connection);
while (await result.MoveNextAsync())
{
var change = result.Current;
if(change.OldValue == null)
Debug.Log($"Received change from RethinkDB (Entry Created): {change.NewValue.ID}");
else if(change.NewValue == null)
Debug.Log($"Received change from RethinkDB (Entry Deleted): {change.OldValue.ID}");
else
Debug.Log($"Received change from RethinkDB: {change.NewValue.ID}");
cache.Add(change.NewValue, true);
}
}).WrapAwait();
}
// Observable.Timer(DateTimeOffset.Now, TimeSpan.FromSeconds(60)).Subscribe(_ =>
// {
// Debug.Log(R.Now().Run<DateTime>(connection).ToString() as string);
// });
return status;
}
}
public class RethinkQueryStatus
{
public int RetrievedEntries;
public int TotalEntries;
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: a8fce60db4f3e614c8d19c3da4cede0e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -39,7 +39,15 @@ public static class UnityExtensions
{
return new AnimationCurve(keys.Select(v => new Keyframe(v.x, v.y, v.z, v.w)).ToArray());
}
public static AnimationCurve ToCurve(this BezierCurve curve)
{
return new AnimationCurve(curve.Keys.Select(v => new Keyframe(v.x, v.y, v.z, v.w)).ToArray());
}
public static Color ToColor(this float3 v) => new Color(v.x,v.y,v.z);
public static float3 ToFloat3(this Color c) => float3(c.r,c.g,c.b);
public static Color ToColor(this float4 v) => new Color(v.x,v.y,v.z, v.w);
public static float4 ToFloat4(this Color c) => float4(c.r, c.g, c.b, c.a);
}