Slime Stuff & DB Prep
Put the finishing touches on the microfauna Prepared the repository for database extraction
This commit is contained in:
parent
ad499a09ac
commit
48759380c5
773 changed files with 5278 additions and 805 deletions
1190
Assets/Scripts/CultCache/Editor/DatabaseInspector.cs
Normal file
1190
Assets/Scripts/CultCache/Editor/DatabaseInspector.cs
Normal file
File diff suppressed because it is too large
Load diff
11
Assets/Scripts/CultCache/Editor/DatabaseInspector.cs.meta
Normal file
11
Assets/Scripts/CultCache/Editor/DatabaseInspector.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 30b612dc279f724418b8a7a5426287c2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
451
Assets/Scripts/CultCache/Editor/DatabaseView.cs
Normal file
451
Assets/Scripts/CultCache/Editor/DatabaseView.cs
Normal file
|
|
@ -0,0 +1,451 @@
|
|||
/* 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.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using JsonKnownTypes;
|
||||
using RethinkDb.Driver;
|
||||
using RethinkDb.Driver.Net;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using static UnityEditor.EditorGUILayout;
|
||||
|
||||
public class DatabaseListView : EditorWindow
|
||||
{
|
||||
// Singleton to avoid multiple instances of window.
|
||||
private static DatabaseListView _instance;
|
||||
public static DatabaseListView Instance => _instance ? _instance : GetWindow<DatabaseListView>();
|
||||
[MenuItem("Window/GameCult/Database Tools")]
|
||||
static void Init() => Instance.Show();
|
||||
private void Awake()
|
||||
{
|
||||
_instance = this;
|
||||
}
|
||||
|
||||
public Guid SelectedItem;
|
||||
public GUIStyle SelectedStyle;
|
||||
|
||||
public static RethinkDB R = RethinkDB.R;
|
||||
|
||||
private DatabaseCache _databaseCache;
|
||||
private bool[] _itemTypeFoldouts;
|
||||
private bool _itemsFoldout;
|
||||
private bool _compoundFoldout;
|
||||
private bool _simpleFoldout;
|
||||
private bool _commoditiesFoldout;
|
||||
//private bool _consumablesFoldout;
|
||||
private Type[] _itemTypes;
|
||||
private bool _listItemStyle;
|
||||
private GUIStyle _listStyleOdd;
|
||||
private GUIStyle _listStyleEven;
|
||||
private static DatabaseInspector _inspector;
|
||||
private Type[] _entryTypes;
|
||||
private bool[] _entryFoldouts;
|
||||
private string _connectionString;
|
||||
private const float LineHeight = 20;
|
||||
private Vector2 _view;
|
||||
private RethinkQueryStatus _queryStatus;
|
||||
private HashSet<string> _itemGroupFoldouts = new HashSet<string>();
|
||||
private HashSet<Guid> _itemManufacturerFoldouts = new HashSet<Guid>();
|
||||
private HashSet<(Guid, HardpointType)> _itemManufacturerTypeFoldouts = new HashSet<(Guid, HardpointType)>();
|
||||
private string _filePath => Path.Combine(new DirectoryInfo(Application.dataPath).Parent.CreateSubdirectory("GameData").FullName, _fileName);
|
||||
private string _fileName = "AetherDB.msgpack";
|
||||
|
||||
public Color LabelColor => EditorGUIUtility.isProSkin ? Color.white : Color.black;
|
||||
|
||||
public GUIStyle ListItemStyle =>
|
||||
(_listItemStyle = !_listItemStyle) ? _listStyleEven : _listStyleOdd;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
|
||||
//_filePath = ;
|
||||
// Create database cache
|
||||
_databaseCache = new DatabaseCache(_filePath);
|
||||
|
||||
// var onInsert = new Action<DatabaseEntry>(entry =>
|
||||
// {
|
||||
// });
|
||||
// _databaseCache.OnDataInsertLocal += onInsert;
|
||||
// _databaseCache.OnDataInsertRemote += onInsert;
|
||||
|
||||
// var onDelete = new Action<DatabaseEntry>(entry =>
|
||||
// {
|
||||
// });
|
||||
// _databaseCache.OnDataDeleteLocal += onDelete;
|
||||
// _databaseCache.OnDataDeleteRemote += onDelete;
|
||||
|
||||
_databaseCache.OnDataUpdateLocal += _ => Repaint();
|
||||
_databaseCache.OnDataInsertLocal += _ => Repaint();
|
||||
_databaseCache.OnDataDeleteLocal += _ => Repaint();
|
||||
_databaseCache.OnDataUpdateRemote += _ => EditorDispatcher.Dispatch(Repaint);
|
||||
_databaseCache.OnDataInsertRemote += _ => EditorDispatcher.Dispatch(Repaint);
|
||||
_databaseCache.OnDataDeleteRemote += _ => EditorDispatcher.Dispatch(Repaint);
|
||||
|
||||
DatabaseInspector.DatabaseCache = _databaseCache;
|
||||
_inspector = DatabaseInspector.Instance;
|
||||
_inspector.Show();
|
||||
|
||||
_itemTypes = typeof(ItemData).GetAllChildClasses()
|
||||
.Where(t => t.GetCustomAttribute<InspectableAttribute>() != null).ToArray();
|
||||
_entryTypes = typeof(DatabaseEntry).GetAllChildClasses()
|
||||
.Where(t => t.GetCustomAttribute<InspectableAttribute>() != null && !typeof(ItemData).IsAssignableFrom(t)).ToArray();
|
||||
|
||||
_itemTypeFoldouts = new bool[_itemTypes.Length];
|
||||
_entryFoldouts = new bool[_entryTypes.Length];
|
||||
|
||||
if (EditorPrefs.HasKey("RethinkDB.URL"))
|
||||
_connectionString = EditorPrefs.GetString("RethinkDB.URL");
|
||||
else
|
||||
_connectionString = "Enter DB URL";
|
||||
|
||||
InitStyles();
|
||||
}
|
||||
|
||||
private void InitStyles()
|
||||
{
|
||||
var isDark = EditorGUIUtility.isProSkin;
|
||||
_listStyleOdd = new GUIStyle
|
||||
{
|
||||
normal = {background = isDark ? XKCDColors.DarkGrey.ToTexture() : XKCDColors.LightGrey.ToTexture()},
|
||||
margin = new RectOffset(0, 0, 0, 0)
|
||||
};
|
||||
_listStyleEven = new GUIStyle
|
||||
{
|
||||
normal = {background = isDark ? Color.Lerp(XKCDColors.DarkGrey,Color.black,.5f).ToTexture() : Color.Lerp(XKCDColors.LightGrey,Color.white,.5f).ToTexture()},
|
||||
margin = new RectOffset(0, 0, 0, 0)
|
||||
};
|
||||
SelectedStyle = new GUIStyle
|
||||
{
|
||||
normal = {background = isDark ? XKCDColors.DarkGreyBlue.ToTexture() : XKCDColors.Greyblue.ToTexture()},
|
||||
margin = new RectOffset(0, 0, 0, 0)
|
||||
};
|
||||
}
|
||||
|
||||
public void OnGUI()
|
||||
{
|
||||
Event currentEvent = Event.current;
|
||||
EventType currentEventType = currentEvent.type;
|
||||
|
||||
if (currentEventType == EventType.DragUpdated)
|
||||
// Indicate that we don't accept drags ourselves
|
||||
DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
|
||||
|
||||
using (var h = new HorizontalScope())
|
||||
{
|
||||
_connectionString = TextField(_connectionString);
|
||||
if (GUILayout.Button("Connect"))
|
||||
{
|
||||
EditorPrefs.SetString("RethinkDB.URL", _connectionString);
|
||||
JsonKnownTypesSettingsManager.RegisterTypeAssembly<ItemData>();
|
||||
_queryStatus = RethinkConnection.RethinkConnect(_databaseCache, _connectionString);
|
||||
}
|
||||
// if (GUILayout.Button("Connect All"))
|
||||
// {
|
||||
// EditorPrefs.SetString("RethinkDB.URL", _connectionString);
|
||||
// _queryStatus = RethinkConnection.RethinkConnect(_databaseCache, _connectionString, true, false);
|
||||
// }
|
||||
}
|
||||
using (var h = new HorizontalScope())
|
||||
{
|
||||
//_fileName = TextField(_fileName);
|
||||
if (GUILayout.Button("Save"))
|
||||
{
|
||||
_databaseCache.Save();
|
||||
Debug.Log("Local DB Cache Saved!");
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Load"))
|
||||
{
|
||||
_databaseCache.Load();
|
||||
Debug.Log("Loaded DB From Local Cache!");
|
||||
}
|
||||
}
|
||||
|
||||
if (_queryStatus != null && _queryStatus.RetrievedItems < _queryStatus.GalaxyEntries + _queryStatus.ItemsEntries)
|
||||
{
|
||||
var progressRect = GetControlRect(false, 20);
|
||||
EditorGUI.ProgressBar(progressRect, (float)_queryStatus.RetrievedItems/(_queryStatus.GalaxyEntries + _queryStatus.ItemsEntries), "Sync Progress");
|
||||
}
|
||||
GUILayout.Space(5);
|
||||
|
||||
_view = BeginScrollView(
|
||||
_view,
|
||||
false,
|
||||
false,
|
||||
GUIStyle.none,
|
||||
GUI.skin.verticalScrollbar,
|
||||
GUI.skin.scrollView,
|
||||
GUILayout.Width(EditorGUIUtility.currentViewWidth),
|
||||
GUILayout.ExpandHeight(true));
|
||||
|
||||
// if(GUILayout.Button("Log Cache Count"))
|
||||
// Debug.Log($"Cache contains {_databaseCache.AllEntries.Count()} elements");
|
||||
|
||||
using (var h = new HorizontalScope(ListItemStyle))
|
||||
{
|
||||
_itemsFoldout = Foldout(_itemsFoldout, "Items", true);
|
||||
}
|
||||
if (_itemsFoldout)
|
||||
{
|
||||
var items = _databaseCache.AllEntries.Where(item => item is ItemData).Cast<ItemData>().ToArray();
|
||||
for (var i = 0; i < _itemTypes.Length; i++)
|
||||
{
|
||||
using (var h = new HorizontalScope(ListItemStyle))
|
||||
{
|
||||
GUILayout.Space(10);
|
||||
_itemTypeFoldouts[i] = Foldout(_itemTypeFoldouts[i],
|
||||
((NameAttribute) _itemTypes[i].GetCustomAttribute(typeof(NameAttribute)))?.Name ?? FormatTypeName(_itemTypes[i].Name),
|
||||
true);
|
||||
}
|
||||
if (_itemTypeFoldouts[i])
|
||||
{
|
||||
if (_itemTypes[i] == typeof(SimpleCommodityData) ||
|
||||
_itemTypes[i] == typeof(CompoundCommodityData) ||
|
||||
_itemTypes[i] == typeof(HullData))
|
||||
{
|
||||
var typedItems = items
|
||||
.Where(e => e.GetType() == _itemTypes[i])
|
||||
.OrderBy(item => item.Name);
|
||||
IEnumerable<IGrouping<string, ItemData>> itemGroups = new List<IGrouping<string, ItemData>>();
|
||||
if(_itemTypes[i] == typeof(SimpleCommodityData))
|
||||
itemGroups = typedItems.GroupBy(item => Enum.GetName(typeof(SimpleCommodityCategory), (item as SimpleCommodityData).Category));
|
||||
else if(_itemTypes[i] == typeof(CompoundCommodityData))
|
||||
itemGroups = typedItems.GroupBy(item => Enum.GetName(typeof(CompoundCommodityCategory), (item as CompoundCommodityData).Category));
|
||||
else if (_itemTypes[i] == typeof(HullData))
|
||||
itemGroups = typedItems.GroupBy(item => Enum.GetName(typeof(HullType), (item as HullData).HullType));
|
||||
else itemGroups = typedItems.GroupBy(item => "All");
|
||||
|
||||
foreach (var itemGroup in itemGroups)
|
||||
{
|
||||
using (var h = new HorizontalScope(ListItemStyle))
|
||||
{
|
||||
GUILayout.Space(20);
|
||||
if (Foldout(_itemGroupFoldouts.Contains(itemGroup.Key), FormatTypeName(itemGroup.Key), true))
|
||||
_itemGroupFoldouts.Add(itemGroup.Key);
|
||||
else _itemGroupFoldouts.Remove(itemGroup.Key);
|
||||
}
|
||||
|
||||
if (_itemGroupFoldouts.Contains(itemGroup.Key))
|
||||
{
|
||||
foreach (var item in itemGroup)
|
||||
{
|
||||
var style = ListItemStyle;
|
||||
var selected = SelectedItem == item.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", item.ID);
|
||||
DragAndDrop.StartDrag("Database Item");
|
||||
}
|
||||
else if (currentEventType == EventType.MouseUp)
|
||||
Select(item);
|
||||
}
|
||||
|
||||
GUILayout.Space(30);
|
||||
GUILayout.Label(item.Name, selected ? EditorStyles.whiteLabel : GUI.skin.label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!typeof(EquippableItemData).IsAssignableFrom(_itemTypes[i]))
|
||||
throw new InvalidCastException($"Type {_itemTypes[i].Name} is invalid! Expected equippable item.");
|
||||
var typedItems = items
|
||||
.Where(e => e.GetType() == _itemTypes[i])
|
||||
.Cast<EquippableItemData>()
|
||||
.OrderBy(item => item.Name);
|
||||
var manufacturerGroups = typedItems.GroupBy(item => item.Manufacturer);
|
||||
foreach (var manufacturerGroup in manufacturerGroups)
|
||||
{
|
||||
var manufacturer = _databaseCache.Get<Faction>(manufacturerGroup.Key);
|
||||
var manufacturerName = manufacturer?.Name ?? "None";
|
||||
using (var h = new HorizontalScope(ListItemStyle))
|
||||
{
|
||||
GUILayout.Space(20);
|
||||
if (Foldout(_itemManufacturerFoldouts.Contains(manufacturerGroup.Key), manufacturerName, true))
|
||||
_itemManufacturerFoldouts.Add(manufacturerGroup.Key);
|
||||
else _itemManufacturerFoldouts.Remove(manufacturerGroup.Key);
|
||||
}
|
||||
|
||||
if (_itemManufacturerFoldouts.Contains(manufacturerGroup.Key))
|
||||
{
|
||||
if (_itemTypes[i] == typeof(GearData) || _itemTypes[i] == typeof(WeaponItemData))
|
||||
{
|
||||
var itemGroups = manufacturerGroup.GroupBy(item => item.HardpointType);
|
||||
|
||||
foreach (var itemGroup in itemGroups)
|
||||
{
|
||||
using (var h = new HorizontalScope(ListItemStyle))
|
||||
{
|
||||
GUILayout.Space(30);
|
||||
if (Foldout(_itemManufacturerTypeFoldouts.Contains((manufacturerGroup.Key, itemGroup.Key)), Enum.GetName(typeof(HardpointType), itemGroup.Key), true))
|
||||
_itemManufacturerTypeFoldouts.Add((manufacturerGroup.Key, itemGroup.Key));
|
||||
else _itemManufacturerTypeFoldouts.Remove((manufacturerGroup.Key, itemGroup.Key));
|
||||
}
|
||||
|
||||
if (_itemManufacturerTypeFoldouts.Contains((manufacturerGroup.Key, itemGroup.Key)))
|
||||
{
|
||||
foreach (var item in itemGroup)
|
||||
{
|
||||
var style = ListItemStyle;
|
||||
var selected = SelectedItem == item.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", item.ID);
|
||||
DragAndDrop.StartDrag("Database Item");
|
||||
}
|
||||
else if (currentEventType == EventType.MouseUp)
|
||||
Select(item);
|
||||
}
|
||||
|
||||
GUILayout.Space(40);
|
||||
GUILayout.Label(item.Name, selected ? EditorStyles.whiteLabel : GUI.skin.label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in manufacturerGroup)
|
||||
{
|
||||
var style = ListItemStyle;
|
||||
var selected = SelectedItem == item.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", item.ID);
|
||||
DragAndDrop.StartDrag("Database Item");
|
||||
}
|
||||
else if (currentEventType == EventType.MouseUp)
|
||||
Select(item);
|
||||
}
|
||||
|
||||
GUILayout.Space(30);
|
||||
GUILayout.Label(item.Name, selected ? EditorStyles.whiteLabel : GUI.skin.label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using (var h = new HorizontalScope(ListItemStyle))
|
||||
{
|
||||
if(GUI.Button(h.rect, GUIContent.none, GUIStyle.none))
|
||||
CreateItem(_itemTypes[i]);
|
||||
GUILayout.Space(20);
|
||||
GUILayout.Label("New " + FormatTypeName(_itemTypes[i].Name));
|
||||
// var rect = GetControlRect(false, GUILayout.Width(EditorGUIUtility.singleLineHeight));
|
||||
// GUI.DrawTexture(rect, Icons.Instance.plus, ScaleMode.StretchToFill, true, 1, LabelColor, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//var entries = _databaseCache.AllEntries.Where(item => !(item is ItemData)).ToArray();
|
||||
for (var i = 0; i < _entryTypes.Length; i++)
|
||||
{
|
||||
using (var h = new HorizontalScope(ListItemStyle))
|
||||
{
|
||||
_entryFoldouts[i] = Foldout(_entryFoldouts[i],
|
||||
((NameAttribute) _entryTypes[i].GetCustomAttribute(typeof(NameAttribute)))?.Name ?? FormatTypeName(_entryTypes[i].Name),
|
||||
true);
|
||||
}
|
||||
|
||||
if (_entryFoldouts[i])
|
||||
{
|
||||
int index = 0;
|
||||
foreach (var entry in _databaseCache.GetAll(_entryTypes[i]).OrderBy(entry=>entry is INamedEntry namedEntry ? namedEntry.EntryName : entry.ID.ToString()))
|
||||
{
|
||||
index++;
|
||||
var style = ListItemStyle;
|
||||
var selected = SelectedItem == entry.ID;
|
||||
using (var h = new HorizontalScope(selected?SelectedStyle:style))
|
||||
{
|
||||
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(10);
|
||||
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]);
|
||||
GUILayout.Space(10);
|
||||
GUILayout.Label("New " + _entryTypes[i].Name);
|
||||
// var rect = GetControlRect(false, GUILayout.Width(EditorGUIUtility.singleLineHeight));
|
||||
// GUI.DrawTexture(rect, Icons.Instance.plus, ScaleMode.StretchToFill, true, 1, LabelColor, 0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EndScrollView();
|
||||
}
|
||||
|
||||
private string FormatTypeName(string typeName)
|
||||
{
|
||||
return (typeName.EndsWith("Data")
|
||||
? typeName.Substring(0, typeName.Length - 4)
|
||||
: typeName).SplitCamelCase();
|
||||
}
|
||||
|
||||
void Select(DatabaseEntry item)
|
||||
{
|
||||
SelectedItem = item.ID;
|
||||
Repaint();
|
||||
_inspector.entry = _databaseCache.Get(SelectedItem);
|
||||
_inspector.Repaint();
|
||||
}
|
||||
|
||||
private DatabaseEntry CreateItem(Type type, Action<DatabaseEntry> onCreate = null)
|
||||
{
|
||||
var newEntry = (DatabaseEntry) Activator.CreateInstance(type);
|
||||
if(newEntry is INamedEntry entry)
|
||||
entry.EntryName = $"New {FormatTypeName(type.Name)}";
|
||||
onCreate?.Invoke(newEntry);
|
||||
_databaseCache.Add(newEntry);
|
||||
Select(newEntry);
|
||||
return newEntry;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CultCache/Editor/DatabaseView.cs.meta
Normal file
11
Assets/Scripts/CultCache/Editor/DatabaseView.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b514fcab25f3b914b89824efbe244591
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
81
Assets/Scripts/CultCache/Editor/EditorDispatch.cs
Normal file
81
Assets/Scripts/CultCache/Editor/EditorDispatch.cs
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
public static class EditorDispatcher
|
||||
{
|
||||
private static readonly Queue<Action> dispatchQueue = new Queue<Action>();
|
||||
private static double timeSliceLimit = 10.0; // in miliseconds
|
||||
private static Stopwatch timer;
|
||||
|
||||
static EditorDispatcher()
|
||||
{
|
||||
EditorApplication.update += Update;
|
||||
timer = new Stopwatch();
|
||||
}
|
||||
|
||||
private static void Update ()
|
||||
{
|
||||
lock (dispatchQueue)
|
||||
{
|
||||
int dispatchCount = 0;
|
||||
|
||||
timer.Reset();
|
||||
timer.Start();
|
||||
|
||||
while (dispatchQueue.Count > 0 && (timer.Elapsed.TotalMilliseconds <= timeSliceLimit))
|
||||
{
|
||||
dispatchQueue.Dequeue().Invoke();
|
||||
|
||||
dispatchCount++;
|
||||
}
|
||||
|
||||
timer.Stop();
|
||||
|
||||
// if (dispatchCount > 0)
|
||||
// UnityEngine.Debug.Log(string.Format("[EditorDispatcher] Dispatched {0} calls in {1}ms", dispatchCount, timer.Elapsed.TotalMilliseconds));
|
||||
|
||||
// todo some logic for disconnecting update when the queue is empty
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send an Action Delegate to be run on the main thread. See EditorDispatchActions for some common usecases.
|
||||
/// </summary>
|
||||
/// <param name="task">An action delegate to run on the main thread</param>
|
||||
/// <returns>An AsyncDispatch that can be used to track if the dispatch has completed.</returns>
|
||||
public static AsyncDispatch Dispatch(Action task)
|
||||
{
|
||||
lock (dispatchQueue)
|
||||
{
|
||||
AsyncDispatch dispatch = new AsyncDispatch();
|
||||
|
||||
// enqueue a new task that runs the supplied task and completes the dispatcher
|
||||
dispatchQueue.Enqueue(() => { task(); dispatch.FinishedDispatch(); });
|
||||
|
||||
return dispatch;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents the progress of the dispatched action. Can be yielded to in a coroutine.
|
||||
/// If not using coroutines, look at the IsDone property to find out when its okay to proceed.
|
||||
/// </summary>
|
||||
public class AsyncDispatch : CustomYieldInstruction
|
||||
{
|
||||
public bool IsDone { get; private set; }
|
||||
public override bool keepWaiting { get { return !IsDone; } }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Flags this dispatch as completed.
|
||||
/// </summary>
|
||||
internal void FinishedDispatch()
|
||||
{
|
||||
IsDone = true;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CultCache/Editor/EditorDispatch.cs.meta
Normal file
11
Assets/Scripts/CultCache/Editor/EditorDispatch.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b7269ba738c469a4ea5b50811ef07f11
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
9
Assets/Scripts/CultCache/Editor/InspectorBase.cs
Normal file
9
Assets/Scripts/CultCache/Editor/InspectorBase.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
public abstract class InspectorBase<T>
|
||||
{
|
||||
protected const int width = 150;
|
||||
protected const int labelWidth = 20;
|
||||
protected const int toggleWidth = 18;
|
||||
protected const int arrowWidth = 22;
|
||||
|
||||
public abstract T Inspect(string label, T value);
|
||||
}
|
||||
3
Assets/Scripts/CultCache/Editor/InspectorBase.cs.meta
Normal file
3
Assets/Scripts/CultCache/Editor/InspectorBase.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f3cde78766994141a3da6ab88d09118f
|
||||
timeCreated: 1617052142
|
||||
8
Assets/Scripts/CultCache/Editor/Inspectors.meta
Normal file
8
Assets/Scripts/CultCache/Editor/Inspectors.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b62c0242f5704a96829889e1d4c44900
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
using Unity.Mathematics;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using static UnityEditor.EditorGUILayout;
|
||||
|
||||
public class Float2Inspector : InspectorBase<float2>
|
||||
{
|
||||
public override float2 Inspect(string label, float2 value)
|
||||
{
|
||||
using (var h = new EditorGUILayout.HorizontalScope())
|
||||
{
|
||||
GUILayout.Label(label, GUILayout.Width(width));
|
||||
GUILayout.Label("X", GUILayout.Width(labelWidth));
|
||||
value.x = DelayedFloatField(value.x);
|
||||
GUILayout.Label("Y", GUILayout.Width(labelWidth));
|
||||
value.y = DelayedFloatField(value.y);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Float3Inspector : InspectorBase<float3>
|
||||
{
|
||||
public override float3 Inspect(string label, float3 value)
|
||||
{
|
||||
using (var h = new EditorGUILayout.HorizontalScope())
|
||||
{
|
||||
GUILayout.Label(label, GUILayout.Width(width));
|
||||
GUILayout.Label("X", GUILayout.Width(labelWidth));
|
||||
value.x = DelayedFloatField(value.x);
|
||||
GUILayout.Label("Y", GUILayout.Width(labelWidth));
|
||||
value.y = DelayedFloatField(value.y);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Int2Inspector : InspectorBase<int2>
|
||||
{
|
||||
public override int2 Inspect(string label, int2 value)
|
||||
{
|
||||
using (var h = new EditorGUILayout.HorizontalScope())
|
||||
{
|
||||
GUILayout.Label(label, GUILayout.Width(width));
|
||||
GUILayout.Label("X", GUILayout.Width(labelWidth));
|
||||
value.x = DelayedIntField(value.x);
|
||||
GUILayout.Label("Y", GUILayout.Width(labelWidth));
|
||||
value.y = DelayedIntField(value.y);
|
||||
if (GUILayout.Button("\u2190", GUILayout.Width(arrowWidth)))
|
||||
value.x--;
|
||||
if (GUILayout.Button("\u2192", GUILayout.Width(arrowWidth)))
|
||||
value.x++;
|
||||
if (GUILayout.Button("\u2191", GUILayout.Width(arrowWidth)))
|
||||
value.y++;
|
||||
if (GUILayout.Button("\u2193", GUILayout.Width(arrowWidth)))
|
||||
value.y--;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ae5bba94d1f1a7144944aebf833497b3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
155
Assets/Scripts/CultCache/Editor/RethinkConnection.cs
Normal file
155
Assets/Scripts/CultCache/Editor/RethinkConnection.cs
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
/* 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.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(DatabaseCache cache, string connectionString, bool syncLocalChanges = true)
|
||||
{
|
||||
// Add Unity.Mathematics serialization support to RethinkDB Driver
|
||||
//Converter.Serializer.Converters.Add(new MathJsonConverter());
|
||||
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
||||
{
|
||||
Converters = new List<JsonConverter>
|
||||
{
|
||||
new MathJsonConverter(),
|
||||
Converter.DateTimeConverter,
|
||||
Converter.BinaryConverter,
|
||||
Converter.GroupingConverter,
|
||||
Converter.PocoExprConverter
|
||||
}
|
||||
};
|
||||
|
||||
var connection = R.Connection().Hostname(connectionString).Port(RethinkDBConstants.DefaultPort+1).Timeout(60).Connect();
|
||||
Debug.Log("Connected to RethinkDB");
|
||||
|
||||
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();
|
||||
|
||||
// Get all item data from RethinkDB
|
||||
Task.Run(async () =>
|
||||
{
|
||||
status.ItemsEntries = R
|
||||
.Db("Aetheria")
|
||||
.Table("Items")
|
||||
.Count().RunAtom<int>(connection);
|
||||
|
||||
var result = await R
|
||||
.Db("Aetheria")
|
||||
.Table("Items")
|
||||
.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.RetrievedItems++;
|
||||
}
|
||||
}).WrapAwait();
|
||||
|
||||
// Get globaldata and all galaxy map layer data from RethinkDB
|
||||
Task.Run(async () =>
|
||||
{
|
||||
ReqlAst operation = R
|
||||
.Db("Aetheria")
|
||||
.Table("Galaxy");
|
||||
status.GalaxyEntries = ((Table) operation).Count().RunAtom<int>(connection);
|
||||
|
||||
var result = await operation
|
||||
.RunCursorAsync<DatabaseEntry>(connection);
|
||||
while (await result.MoveNextAsync())
|
||||
{
|
||||
var entry = result.Current;
|
||||
//Debug.Log($"Received Galaxy entry from RethinkDB: {entry.GetType()} {(entry as INamedEntry)?.EntryName ?? ""}:{entry.ID}");
|
||||
cache.Add(entry, true);
|
||||
status.RetrievedItems++;
|
||||
}
|
||||
}).WrapAwait();
|
||||
|
||||
// Subscribe to changes from RethinkDB
|
||||
Task.Run(async () =>
|
||||
{
|
||||
var result = await R
|
||||
.Db("Aetheria")
|
||||
.Table("Items")
|
||||
.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 RetrievedItems;
|
||||
public int GalaxyEntries;
|
||||
public int ItemsEntries;
|
||||
}
|
||||
11
Assets/Scripts/CultCache/Editor/RethinkConnection.cs.meta
Normal file
11
Assets/Scripts/CultCache/Editor/RethinkConnection.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a8fce60db4f3e614c8d19c3da4cede0e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue