存档功能
This commit is contained in:
@@ -1,37 +1,27 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AibisDream.Utility;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream.Framework
|
||||
{
|
||||
public interface IModule
|
||||
{
|
||||
void Init();
|
||||
void Init(string dataKey);
|
||||
void Deinit();
|
||||
}
|
||||
|
||||
public interface ISystem
|
||||
{
|
||||
void Init();
|
||||
void Deinit();
|
||||
}
|
||||
|
||||
public interface ISystemHasData
|
||||
{
|
||||
string CurDataKey { get; set; }
|
||||
string[] GetDataKeyOptions();
|
||||
void Load();
|
||||
void Save();
|
||||
string GetConfigPath();
|
||||
void LoadLevel();
|
||||
void SaveLevel();
|
||||
}
|
||||
|
||||
public abstract class SystemHasData : MonoBehaviour, ISystemHasData
|
||||
{
|
||||
public abstract string CurDataKey { get; set; }
|
||||
public abstract string[] GetDataKeyOptions();
|
||||
public abstract void Load();
|
||||
public abstract void Save();
|
||||
public abstract string GetConfigPath();
|
||||
public abstract void LoadLevel();
|
||||
public abstract void SaveLevel();
|
||||
}
|
||||
|
||||
public interface IHasData
|
||||
@@ -39,7 +29,12 @@ namespace AibisDream.Framework
|
||||
void Save(string dataKey);
|
||||
void Load(string dataKey);
|
||||
}
|
||||
|
||||
|
||||
public interface IData
|
||||
{
|
||||
void Load();
|
||||
}
|
||||
|
||||
#region IOC容器
|
||||
|
||||
public class IOCContainer
|
||||
@@ -85,5 +80,234 @@ namespace AibisDream.Framework
|
||||
public void Clear() => _instances.Clear();
|
||||
}
|
||||
|
||||
public class DataContainer
|
||||
{
|
||||
private Dictionary<string, IData> _instances = new();
|
||||
|
||||
public void Register<T>(T instance) where T : IData
|
||||
{
|
||||
var key = typeof(T).AssemblyQualifiedName;
|
||||
if (key != null) _instances[key] = instance;
|
||||
}
|
||||
|
||||
public T Get<T>() where T : class, IData
|
||||
{
|
||||
var key = typeof(T).AssemblyQualifiedName;
|
||||
if (key != null && _instances.TryGetValue(key, out var retInstance))
|
||||
{
|
||||
return retInstance as T;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public object Get(Type type)
|
||||
{
|
||||
var key = type.AssemblyQualifiedName;
|
||||
if (key != null && _instances.TryGetValue(key, out var retInstance))
|
||||
{
|
||||
return retInstance;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public T Unregister<T>() where T : class, IData
|
||||
{
|
||||
var res = Get<T>();
|
||||
var assemblyQualifiedName = typeof(T).AssemblyQualifiedName;
|
||||
if (res != null && assemblyQualifiedName != null)
|
||||
{
|
||||
_instances.Remove(assemblyQualifiedName);
|
||||
return res;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Clear() => _instances.Clear();
|
||||
|
||||
public JObject SaveAsJson()
|
||||
{
|
||||
return JObject.FromObject(_instances);
|
||||
}
|
||||
|
||||
public void LoadByJson(JObject dataJson)
|
||||
{
|
||||
foreach (var fixDataItemPair in _instances)
|
||||
{
|
||||
if (!dataJson.TryGetValue(fixDataItemPair.Key, out var fixData))
|
||||
{
|
||||
Debug.Log(fixDataItemPair.Key + "未注册");
|
||||
continue;
|
||||
}
|
||||
|
||||
var fixDataType = Type.GetType(fixDataItemPair.Key);
|
||||
if (fixDataType == null)
|
||||
{
|
||||
Debug.Log(fixDataItemPair.Key + "类型不存在");
|
||||
continue;
|
||||
}
|
||||
|
||||
// 把Json中读到的参数填回Data
|
||||
var tempObj = fixData.ToObject(fixDataType);
|
||||
CommonUtil.CopyProperties(tempObj, fixDataItemPair.Value);
|
||||
}
|
||||
|
||||
foreach (var data in _instances.Values)
|
||||
{
|
||||
data.Load();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 事件相关
|
||||
|
||||
public interface IEasyEvent
|
||||
{
|
||||
IUnRegister Register(Action onEvent);
|
||||
public void UnRegisterAll();
|
||||
}
|
||||
|
||||
public class EasyEvent : IEasyEvent
|
||||
{
|
||||
private event Action OnEvent = () => { };
|
||||
|
||||
public IUnRegister Register(Action onEvent)
|
||||
{
|
||||
OnEvent += onEvent;
|
||||
return new CustomUnRegister(() => { UnRegister(onEvent); });
|
||||
}
|
||||
|
||||
public void UnRegister(Action onEvent) => OnEvent -= onEvent;
|
||||
|
||||
public void Trigger() => OnEvent?.Invoke();
|
||||
|
||||
public void UnRegisterAll()
|
||||
{
|
||||
OnEvent = null;
|
||||
}
|
||||
}
|
||||
|
||||
public class EasyEvent<T> : IEasyEvent
|
||||
{
|
||||
private event Action<T> OnEvent = e => { };
|
||||
|
||||
public IUnRegister Register(Action<T> onEvent)
|
||||
{
|
||||
OnEvent += onEvent;
|
||||
return new CustomUnRegister(() => { UnRegister(onEvent); });
|
||||
}
|
||||
|
||||
public void UnRegister(Action<T> onEvent) => OnEvent -= onEvent;
|
||||
|
||||
public void Trigger(T t) => OnEvent?.Invoke(t);
|
||||
|
||||
IUnRegister IEasyEvent.Register(Action onEvent)
|
||||
{
|
||||
return Register(Action);
|
||||
void Action(T _) => onEvent();
|
||||
}
|
||||
|
||||
public void UnRegisterAll()
|
||||
{
|
||||
OnEvent = null;
|
||||
}
|
||||
}
|
||||
|
||||
public class EasyEvent<T, TK> : IEasyEvent
|
||||
{
|
||||
private event Action<T, TK> OnEvent = (_, _) => { };
|
||||
|
||||
public IUnRegister Register(Action<T, TK> onEvent)
|
||||
{
|
||||
OnEvent += onEvent;
|
||||
return new CustomUnRegister(() => { UnRegister(onEvent); });
|
||||
}
|
||||
|
||||
public void UnRegister(Action<T, TK> onEvent) => OnEvent -= onEvent;
|
||||
|
||||
public void Trigger(T t, TK k) => OnEvent?.Invoke(t, k);
|
||||
|
||||
IUnRegister IEasyEvent.Register(Action onEvent)
|
||||
{
|
||||
return Register(Action);
|
||||
void Action(T _, TK __) => onEvent();
|
||||
}
|
||||
|
||||
public void UnRegisterAll()
|
||||
{
|
||||
OnEvent = null;
|
||||
}
|
||||
}
|
||||
|
||||
public interface IUnRegister
|
||||
{
|
||||
void UnRegister();
|
||||
}
|
||||
|
||||
public struct CustomUnRegister : IUnRegister
|
||||
{
|
||||
private Action OnUnRegister { get; set; }
|
||||
public CustomUnRegister(Action onUnRegister) => OnUnRegister = onUnRegister;
|
||||
|
||||
public void UnRegister()
|
||||
{
|
||||
OnUnRegister.Invoke();
|
||||
OnUnRegister = null;
|
||||
}
|
||||
}
|
||||
|
||||
public class BindProperty<T>
|
||||
{
|
||||
private T _value;
|
||||
private EasyEvent<T> _onValueChanged;
|
||||
|
||||
public BindProperty(T defaultValue = default) => _value = defaultValue;
|
||||
|
||||
public static Func<T, T, bool> Comparer { get; set; } = (a, b) => a.Equals(b);
|
||||
|
||||
public BindProperty<T> WithComparer(Func<T, T, bool> comparer)
|
||||
{
|
||||
Comparer = comparer;
|
||||
return this;
|
||||
}
|
||||
|
||||
public T Value
|
||||
{
|
||||
get => GetValue();
|
||||
set
|
||||
{
|
||||
if (value == null && _value == null) return;
|
||||
if (value != null && Comparer(value, _value)) return;
|
||||
|
||||
SetValue(value);
|
||||
_onValueChanged.Trigger(value);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetValue(T newValue) => _value = newValue;
|
||||
|
||||
private T GetValue() => _value;
|
||||
|
||||
public void SetValueWithoutEvent(T newValue) => _value = newValue;
|
||||
|
||||
public IUnRegister Register(Action<T> onValueChanged)
|
||||
{
|
||||
return _onValueChanged.Register(onValueChanged);
|
||||
}
|
||||
|
||||
public void UnRegister(Action<T> onValueChanged) => _onValueChanged.UnRegister(onValueChanged);
|
||||
|
||||
public override string ToString() => Value.ToString();
|
||||
|
||||
public void RemoveAll()
|
||||
{
|
||||
_onValueChanged?.UnRegisterAll();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AibisDream.Utility;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AibisDream.Framework
|
||||
{
|
||||
public class EnumEventSystem
|
||||
{
|
||||
public static readonly EnumEventSystem Global = new();
|
||||
|
||||
private readonly Dictionary<Type, IEasyEvent[]> _eventDict = new(50);
|
||||
|
||||
protected EnumEventSystem()
|
||||
{
|
||||
}
|
||||
|
||||
public IUnRegister Register<T>(T key, Action onEvent) where T : IConvertible
|
||||
{
|
||||
var kv = key.ToInt32(null);
|
||||
|
||||
if (_eventDict.TryGetValue(typeof(T), out var events))
|
||||
{
|
||||
if (events[kv] == null)
|
||||
{
|
||||
var tempEvent = new EasyEvent();
|
||||
events[kv] = tempEvent;
|
||||
return tempEvent.Register(onEvent);
|
||||
}
|
||||
|
||||
if (events[kv] is EasyEvent targetEvent)
|
||||
{
|
||||
return targetEvent.Register(onEvent);
|
||||
}
|
||||
|
||||
Debug.Log($"{key.ToString()} 注册参数错误");
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
events = new IEasyEvent[Enum.GetValues(typeof(T)).Length];
|
||||
_eventDict.Add(typeof(T), events);
|
||||
|
||||
var tempEvent = new EasyEvent();
|
||||
events[kv] = tempEvent;
|
||||
|
||||
return tempEvent.Register(onEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public IUnRegister Register<T, TE>(T key, Action<TE> onEvent) where T : IConvertible where TE : struct
|
||||
{
|
||||
var kv = key.ToInt32(null);
|
||||
|
||||
if (_eventDict.TryGetValue(typeof(T), out var events))
|
||||
{
|
||||
if (events[kv] == null)
|
||||
{
|
||||
var tempEvent = new EasyEvent<TE>();
|
||||
events[kv] = tempEvent;
|
||||
return tempEvent.Register(onEvent);
|
||||
}
|
||||
|
||||
if (events[kv] is EasyEvent<TE> targetEvent)
|
||||
{
|
||||
return targetEvent.Register(onEvent);
|
||||
}
|
||||
|
||||
Debug.Log($"{key.ToString()} 注册参数错误");
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
events = new IEasyEvent[Enum.GetValues(typeof(T)).Length];
|
||||
_eventDict.Add(typeof(T), events);
|
||||
|
||||
var tempEvent = new EasyEvent<TE>();
|
||||
events[kv] = tempEvent;
|
||||
|
||||
return tempEvent.Register(onEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public void UnRegister<T>(T key, Action onEvent) where T : IConvertible
|
||||
{
|
||||
var kv = key.ToInt32(null);
|
||||
|
||||
if (!_eventDict.TryGetValue(typeof(T), out var e)) return;
|
||||
if (e.Length >= kv || e[kv] == null) return;
|
||||
|
||||
if (e[kv] is EasyEvent tempEvent)
|
||||
{
|
||||
tempEvent.UnRegister(onEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public void UnRegister<T>(T key) where T : IConvertible
|
||||
{
|
||||
var kv = key.ToInt32(null);
|
||||
|
||||
if (!_eventDict.TryGetValue(typeof(T), out var e)) return;
|
||||
if (e.Length >= kv || e[kv] == null) return;
|
||||
|
||||
e[kv].UnRegisterAll();
|
||||
}
|
||||
|
||||
public void UnRegisterAll()
|
||||
{
|
||||
foreach (var eventItem in _eventDict.Values.SelectMany(eventArr => eventArr))
|
||||
{
|
||||
eventItem?.UnRegisterAll();
|
||||
}
|
||||
|
||||
_eventDict.Clear();
|
||||
}
|
||||
|
||||
public void Send<T>(T key) where T : IConvertible
|
||||
{
|
||||
var kv = key.ToInt32(null);
|
||||
|
||||
if (!_eventDict.TryGetValue(typeof(T), out var e)) return;
|
||||
if (e.Length >= kv || e[kv] == null) return;
|
||||
|
||||
if (e[kv] is EasyEvent tempEvent)
|
||||
{
|
||||
tempEvent.Trigger();
|
||||
}
|
||||
}
|
||||
|
||||
public void Send<T, TE>(T key, TE param) where T : IConvertible
|
||||
{
|
||||
var kv = key.ToInt32(null);
|
||||
|
||||
if (!_eventDict.TryGetValue(typeof(T), out var e)) return;
|
||||
if (e.Length >= kv || e[kv] == null) return;
|
||||
|
||||
if (e[kv] is EasyEvent<TE> tempEvent)
|
||||
{
|
||||
tempEvent.Trigger(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class StringEventSystem
|
||||
{
|
||||
public static readonly StringEventSystem Global = new();
|
||||
|
||||
private readonly Dictionary<string, IEasyEvent> _events = new();
|
||||
|
||||
public IUnRegister Register(string key, Action onEvent)
|
||||
{
|
||||
if (_events.TryGetValue(key, out var e))
|
||||
{
|
||||
var easyEvent = e.As<EasyEvent>();
|
||||
return easyEvent.Register(onEvent);
|
||||
}
|
||||
else
|
||||
{
|
||||
var easyEvent = new EasyEvent();
|
||||
_events.Add(key, easyEvent);
|
||||
return easyEvent.Register(onEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public void UnRegister(string key, Action onEvent)
|
||||
{
|
||||
if (_events.TryGetValue(key, out var e))
|
||||
{
|
||||
var easyEvent = e.As<EasyEvent>();
|
||||
easyEvent?.UnRegister(onEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public void Send(string key)
|
||||
{
|
||||
if (_events.TryGetValue(key, out var e))
|
||||
{
|
||||
var easyEvent = e.As<EasyEvent>();
|
||||
easyEvent?.Trigger();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public IUnRegister Register<T>(string key, Action<T> onEvent)
|
||||
{
|
||||
if (_events.TryGetValue(key, out var e))
|
||||
{
|
||||
var easyEvent = e.As<EasyEvent<T>>();
|
||||
return easyEvent.Register(onEvent);
|
||||
}
|
||||
else
|
||||
{
|
||||
var easyEvent = new EasyEvent<T>();
|
||||
_events.Add(key, easyEvent);
|
||||
return easyEvent.Register(onEvent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void UnRegister<T>(string key, Action<T> onEvent)
|
||||
{
|
||||
if (_events.TryGetValue(key, out var e))
|
||||
{
|
||||
var easyEvent = e.As<EasyEvent<T>>();
|
||||
easyEvent?.UnRegister(onEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public void Send<T>(string key, T data)
|
||||
{
|
||||
if (_events.TryGetValue(key, out var e))
|
||||
{
|
||||
var easyEvent = e.As<EasyEvent<T>>();
|
||||
easyEvent?.Trigger(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 21974c647d3c4682b2c9dc6b8c23b4ef
|
||||
timeCreated: 1736232227
|
||||
@@ -29,6 +29,11 @@ namespace AibisDream.Kit
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
OnSingletonDestroy();
|
||||
}
|
||||
|
||||
public virtual void OnSingletonDestroy()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user