2020-03-22 19:05:26 +00:00
|
|
|
|
using System;
|
2020-03-27 16:46:46 +00:00
|
|
|
|
using System.Collections;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using MessagePack;
|
|
|
|
|
|
|
|
|
|
|
|
public class DatabaseCache
|
|
|
|
|
|
{
|
2020-03-24 14:36:22 +00:00
|
|
|
|
public event Action<DatabaseEntry> OnDataInsertLocal;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
public event Action<DatabaseEntry> OnDataUpdateLocal;
|
2020-03-24 00:02:23 +00:00
|
|
|
|
public event Action<DatabaseEntry> OnDataDeleteLocal;
|
2020-05-22 22:34:08 +01:00
|
|
|
|
public event Action<DatabaseEntry> OnDataInsertRemote;
|
2020-03-24 14:36:22 +00:00
|
|
|
|
public event Action<DatabaseEntry> OnDataUpdateRemote;
|
2020-05-22 22:34:08 +01:00
|
|
|
|
public event Action<DatabaseEntry> OnDataDeleteRemote;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
|
|
|
|
|
|
public Action<string> Logger = Console.WriteLine;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<Guid, DatabaseEntry> _entries = new Dictionary<Guid, DatabaseEntry>();
|
|
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<Type, Dictionary<Guid, DatabaseEntry>> _types = new Dictionary<Type, Dictionary<Guid, DatabaseEntry>>();
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<DatabaseEntry> AllEntries => _entries.Values;
|
|
|
|
|
|
|
|
|
|
|
|
public void Add(DatabaseEntry entry, bool remote = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entry != null)
|
|
|
|
|
|
{
|
2020-03-24 14:36:22 +00:00
|
|
|
|
var exists = _entries.ContainsKey(entry.ID);
|
2020-03-22 19:05:26 +00:00
|
|
|
|
_entries[entry.ID] = entry;
|
|
|
|
|
|
var type = entry.GetType();
|
2020-03-28 03:43:07 +00:00
|
|
|
|
var types = type.GetParentTypes();
|
2020-03-22 19:05:26 +00:00
|
|
|
|
foreach (var t in types)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!_types.ContainsKey(t))
|
|
|
|
|
|
_types[t] = new Dictionary<Guid, DatabaseEntry>();
|
|
|
|
|
|
_types[t][entry.ID] = entry;
|
|
|
|
|
|
}
|
2020-03-24 14:36:22 +00:00
|
|
|
|
|
|
|
|
|
|
if (remote)
|
2020-05-22 22:34:08 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (exists)
|
|
|
|
|
|
OnDataUpdateRemote?.Invoke(entry);
|
|
|
|
|
|
else
|
|
|
|
|
|
OnDataInsertRemote?.Invoke(entry);
|
|
|
|
|
|
}
|
2020-03-22 19:05:26 +00:00
|
|
|
|
else
|
2020-03-24 14:36:22 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (exists)
|
|
|
|
|
|
OnDataUpdateLocal?.Invoke(entry);
|
|
|
|
|
|
else
|
|
|
|
|
|
OnDataInsertLocal?.Invoke(entry);
|
|
|
|
|
|
}
|
2020-03-22 19:05:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AddAll(IEnumerable<DatabaseEntry> entries, bool remote = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var entry in entries)
|
|
|
|
|
|
{
|
|
|
|
|
|
Add(entry, remote);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DatabaseEntry Get(Guid guid)
|
|
|
|
|
|
{
|
|
|
|
|
|
DatabaseEntry entry;
|
|
|
|
|
|
_entries.TryGetValue(guid, out entry);
|
|
|
|
|
|
return entry;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public T Get<T>(Guid guid) where T : DatabaseEntry
|
|
|
|
|
|
{
|
2020-03-27 16:46:46 +00:00
|
|
|
|
if (!_types.ContainsKey(typeof(T)))
|
|
|
|
|
|
return null;
|
2020-03-22 19:05:26 +00:00
|
|
|
|
DatabaseEntry entry;
|
|
|
|
|
|
_types[typeof(T)].TryGetValue(guid, out entry);
|
|
|
|
|
|
return (T) entry;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<T> GetAll<T>()
|
|
|
|
|
|
{
|
2020-03-27 16:46:46 +00:00
|
|
|
|
return !_types.ContainsKey(typeof(T)) ? Enumerable.Empty<T>() : _types[typeof(T)].Values.Cast<T>();
|
2020-03-22 19:05:26 +00:00
|
|
|
|
}
|
2020-03-24 00:02:23 +00:00
|
|
|
|
|
2020-03-27 01:53:03 +00:00
|
|
|
|
public void Delete(DatabaseEntry entry, bool remote = false)
|
2020-03-24 00:02:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
_entries.Remove(entry.ID);
|
|
|
|
|
|
foreach (var type in _types.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
type.Remove(entry.ID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-22 22:34:08 +01:00
|
|
|
|
if (remote)
|
|
|
|
|
|
OnDataDeleteRemote?.Invoke(entry);
|
|
|
|
|
|
else
|
2020-03-27 01:53:03 +00:00
|
|
|
|
OnDataDeleteLocal?.Invoke(entry);
|
2020-03-24 00:02:23 +00:00
|
|
|
|
}
|
2020-04-23 03:31:17 +01:00
|
|
|
|
|
|
|
|
|
|
public void Load(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
RegisterResolver.Register();
|
|
|
|
|
|
var bytes = File.ReadAllBytes(Path.Combine(path, "AetherDB.msgpack"));
|
|
|
|
|
|
var entries = MessagePackSerializer.Deserialize<DatabaseEntry[]>(bytes);
|
|
|
|
|
|
AddAll(entries);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Save(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
RegisterResolver.Register();
|
|
|
|
|
|
var entries = AllEntries.ToArray();
|
|
|
|
|
|
File.WriteAllBytes(Path.Combine(path, "AetherDB.msgpack"), MessagePackSerializer.Serialize(entries));
|
|
|
|
|
|
}
|
2020-03-22 19:05:26 +00:00
|
|
|
|
}
|