Browse Source

删减了QFrame部分内容

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
8fe7700850
  1. 34
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs
  2. 8
      Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs
  3. 8
      Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs
  4. 26
      Assets/MsgTransmitTools/UDPClient/UDPUtility.cs
  5. 14
      Assets/MsgTransmitTools/src/QFrameCopy.cs
  6. 925
      Assets/MsgTransmitTools/src/QFramework.cs
  7. 11
      Assets/MsgTransmitTools/src/QFramework.cs.meta

34
Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs

@ -16,13 +16,13 @@ namespace TCPClientTools
public int tcpPort; public int tcpPort;
public TcpClient tcpClient; public TcpClient tcpClient;
public NetworkStream sendStream; public NetworkStream sendStream;
public BindableProperty<bool> isOpenTCP = new BindableProperty<bool>(false); public bool isOpenTCP = false;
public bool isReceivedValue = false; public bool isReceivedValue = false;
public BindableProperty<string> receivedData = new BindableProperty<string>(""); public string receivedData = "";
public BindableProperty<string> exceptionData = new BindableProperty<string>(""); public string exceptionData = "";
public Thread reciveT; public Thread reciveT;
public BindableProperty<bool> isTimeOut = new BindableProperty<bool>(false); public bool isTimeOut = false;
public TCPUtility() { public TCPUtility() {
Debug.LogWarning("使用无参数构造tcp时,需要手动开启tcp服务"); Debug.LogWarning("使用无参数构造tcp时,需要手动开启tcp服务");
@ -32,12 +32,12 @@ namespace TCPClientTools
{ {
this.tcpAddress = tcpAddress; this.tcpAddress = tcpAddress;
this.tcpPort = tcpPort; this.tcpPort = tcpPort;
this.isTimeOut.Value = false; this.isTimeOut = false;
StartTCPClient(); StartTCPClient();
} }
public bool StartTCPClient() public bool StartTCPClient()
{ {
isTimeOut.Value = false; isTimeOut = false;
if (!isOpenTCP) if (!isOpenTCP)
{ {
try { try {
@ -46,10 +46,10 @@ namespace TCPClientTools
} }
catch (Exception e) catch (Exception e)
{ {
exceptionData.Value = e.ToString(); exceptionData = e.ToString();
return false; return false;
} }
isOpenTCP.Value = true; isOpenTCP = true;
reciveT = new Thread(RecciveMsg); reciveT = new Thread(RecciveMsg);
reciveT.IsBackground = true; reciveT.IsBackground = true;
reciveT.Start(); reciveT.Start();
@ -58,7 +58,7 @@ namespace TCPClientTools
return false; return false;
} }
public bool StartTCPClient(string ip, int port) { public bool StartTCPClient(string ip, int port) {
isTimeOut.Value = false; isTimeOut = false;
if (!isOpenTCP) { if (!isOpenTCP) {
tcpAddress = ip; tcpAddress = ip;
tcpPort = port; tcpPort = port;
@ -69,10 +69,10 @@ namespace TCPClientTools
} }
catch (Exception e) catch (Exception e)
{ {
exceptionData.Value = e.ToString(); exceptionData = e.ToString();
return false; return false;
} }
isOpenTCP.Value = true; isOpenTCP = true;
reciveT = new Thread(RecciveMsg); reciveT = new Thread(RecciveMsg);
reciveT.IsBackground = true; reciveT.IsBackground = true;
reciveT.Start(); reciveT.Start();
@ -85,7 +85,7 @@ namespace TCPClientTools
if (tcpClient.Connected) { if (tcpClient.Connected) {
sendStream.Close(); sendStream.Close();
tcpClient.Close(); tcpClient.Close();
isOpenTCP.Value = false; isOpenTCP = false;
reciveT.Abort(); reciveT.Abort();
} }
} }
@ -103,7 +103,7 @@ namespace TCPClientTools
byte[] receiveBuff = new byte[1024]; byte[] receiveBuff = new byte[1024];
int reviceLength = 0; int reviceLength = 0;
string msg = ""; string msg = "";
while (isOpenTCP.Value) while (isOpenTCP)
{ {
try try
{ {
@ -111,7 +111,7 @@ namespace TCPClientTools
msg = Encoding.UTF8.GetString(receiveBuff, 0, reviceLength); msg = Encoding.UTF8.GetString(receiveBuff, 0, reviceLength);
if (msg != "") if (msg != "")
{ {
receivedData.Value = msg; receivedData = msg;
isReceivedValue = true; isReceivedValue = true;
} }
} }
@ -119,8 +119,8 @@ namespace TCPClientTools
{ {
Debug.LogWarning(e); Debug.LogWarning(e);
//断线发送异常 //断线发送异常
isTimeOut.Value = true; isTimeOut = true;
exceptionData.Value = e.ToString(); exceptionData = e.ToString();
break; break;
} }
} }
@ -135,7 +135,7 @@ namespace TCPClientTools
if (isReceivedValue) if (isReceivedValue)
{ {
isReceivedValue = false; isReceivedValue = false;
return receivedData.Value; return receivedData;
} }
else { else {
return ""; return "";

8
Assets/MsgTransmitTools/TCPClient/Script/View/TCPPrinter.cs

@ -25,12 +25,12 @@ namespace TCPClientTools
//Debug.Log(tcpUtil.Read_TCPClient()); //Debug.Log(tcpUtil.Read_TCPClient());
if (tcpUtil != null && !"".Equals(tcpUtil.getReceivedValue())) { if (tcpUtil != null && !"".Equals(tcpUtil.getReceivedValue())) {
GameObject item = Instantiate(tcpMsgItem, tcpMsgContent); GameObject item = Instantiate(tcpMsgItem, tcpMsgContent);
item.GetComponentInChildren<Text>().text = tcpUtil.receivedData.Value; item.GetComponentInChildren<Text>().text = tcpUtil.receivedData;
this.GetModel<TCPEventModel>().onDataRecived.Invoke(tcpUtil.receivedData.Value); this.GetModel<TCPEventModel>().onDataRecived.Invoke(tcpUtil.receivedData);
} }
if (tcpUtil.getTimeOutState() && tcpUtil.isOpenTCP) { if (tcpUtil.getTimeOutState() && tcpUtil.isOpenTCP) {
this.SendEvent(new onLinkException(tcpUtil.exceptionData.Value)); this.SendEvent(new onLinkException(tcpUtil.exceptionData));
tcpUtil.isOpenTCP.Value = false; tcpUtil.isOpenTCP = false;
} }
} }
private void OnDestroy() private void OnDestroy()

8
Assets/MsgTransmitTools/UDPClient/UDPPrinter.cs

@ -29,14 +29,14 @@ public class UDPPrinter : MonoBehaviour, IController, ICanSendEvent, ICanGetUtil
if (udpUtil != null && !"".Equals(udpUtil.getReceivedValue())) if (udpUtil != null && !"".Equals(udpUtil.getReceivedValue()))
{ {
GameObject item = Instantiate(udpMsgItem, udpMsgContent); GameObject item = Instantiate(udpMsgItem, udpMsgContent);
item.GetComponentInChildren<Text>().text = udpUtil.receivedData.Value; item.GetComponentInChildren<Text>().text = udpUtil.receivedData;
this.GetModel<UDPEventModel>().onDataRecived.Invoke(udpUtil.receivedData.Value); this.GetModel<UDPEventModel>().onDataRecived.Invoke(udpUtil.receivedData);
} }
if (udpUtil.getTimeOutState() && udpUtil.isOpenUDP) if (udpUtil.getTimeOutState() && udpUtil.isOpenUDP)
{ {
this.SendEvent(new onUDPLinkException(udpUtil.exceptionData.Value)); this.SendEvent(new onUDPLinkException(udpUtil.exceptionData));
udpUtil.isOpenUDP.Value = false; udpUtil.isOpenUDP = false;
} }
} }
private void OnDestroy() private void OnDestroy()

26
Assets/MsgTransmitTools/UDPClient/UDPUtility.cs

@ -13,12 +13,12 @@ public class UDPUtility : IUtility
public int udpPort; public int udpPort;
public UdpClient udpClient; public UdpClient udpClient;
public NetworkStream sendStream; public NetworkStream sendStream;
public BindableProperty<bool> isOpenUDP = new BindableProperty<bool>(false); public bool isOpenUDP = false;
public bool isReceivedValue = false; public bool isReceivedValue = false;
public BindableProperty<string> receivedData = new BindableProperty<string>(""); public string receivedData = "";
public BindableProperty<string> exceptionData = new BindableProperty<string>(""); public string exceptionData = "";
public Thread reciveT; public Thread reciveT;
public BindableProperty<bool> isTimeOut = new BindableProperty<bool>(false); public bool isTimeOut = false;
public UDPUtility() public UDPUtility()
{ {
@ -34,7 +34,7 @@ public class UDPUtility : IUtility
public bool StartUDPClient(string ip, int port) public bool StartUDPClient(string ip, int port)
{ {
isTimeOut.Value = false; isTimeOut = false;
if (!isOpenUDP) if (!isOpenUDP)
{ {
udpAddress = ip; udpAddress = ip;
@ -45,10 +45,10 @@ public class UDPUtility : IUtility
} }
catch (Exception e) catch (Exception e)
{ {
exceptionData.Value = e.ToString(); exceptionData = e.ToString();
return false; return false;
} }
isOpenUDP.Value = true; isOpenUDP = true;
reciveT = new Thread(RecciveMsg); reciveT = new Thread(RecciveMsg);
reciveT.IsBackground = true; reciveT.IsBackground = true;
reciveT.Start(); reciveT.Start();
@ -60,7 +60,7 @@ public class UDPUtility : IUtility
public void CloseUDPClient() public void CloseUDPClient()
{ {
udpClient.Dispose(); udpClient.Dispose();
isOpenUDP.Value = false; isOpenUDP = false;
reciveT.Abort(); reciveT.Abort();
} }
@ -76,7 +76,7 @@ public class UDPUtility : IUtility
{ {
string msg = ""; string msg = "";
//Debug.Log("StartReceiving!"); //Debug.Log("StartReceiving!");
while (isOpenUDP.Value) while (isOpenUDP)
{ {
try try
{ {
@ -85,7 +85,7 @@ public class UDPUtility : IUtility
msg = Encoding.UTF8.GetString(receiveBytes); msg = Encoding.UTF8.GetString(receiveBytes);
if (msg != "") if (msg != "")
{ {
receivedData.Value = msg; receivedData = msg;
isReceivedValue = true; isReceivedValue = true;
} }
} }
@ -93,8 +93,8 @@ public class UDPUtility : IUtility
{ {
Debug.LogWarning(e); Debug.LogWarning(e);
//断线发送异常 //断线发送异常
isTimeOut.Value = true; isTimeOut = true;
exceptionData.Value = e.ToString(); exceptionData = e.ToString();
break; break;
} }
} }
@ -110,7 +110,7 @@ public class UDPUtility : IUtility
if (isReceivedValue) if (isReceivedValue)
{ {
isReceivedValue = false; isReceivedValue = false;
return receivedData.Value; return receivedData;
} }
else else
{ {

14
Assets/MsgTransmitTools/src/QFrameCopy.cs

@ -25,7 +25,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace QFrameworkCP namespace QFramework
{ {
#region Architecture #region Architecture
@ -204,8 +204,8 @@ namespace QFrameworkCP
#region Controller #region Controller
public interface IController : IBelongToArchitecture, ICanSendCommand, ICanGetSystem, ICanGetModel, public interface IController : IBelongToArchitecture, ICanSendCommand, ICanGetModel,
ICanRegisterEvent, ICanSendQuery ICanRegisterEvent, ICanSendQuery, ICanSendEvent
{ {
} }
@ -252,7 +252,7 @@ namespace QFrameworkCP
#region Command #region Command
public interface ICommand : IBelongToArchitecture, ICanSetArchitecture, ICanGetSystem, ICanGetModel, ICanGetUtility, public interface ICommand : IBelongToArchitecture, ICanSetArchitecture, ICanGetModel, ICanGetUtility,
ICanSendEvent, ICanSendCommand, ICanSendQuery ICanSendEvent, ICanSendCommand, ICanSendQuery
{ {
void Execute(); void Execute();
@ -284,7 +284,7 @@ namespace QFrameworkCP
#region Query #region Query
public interface IQuery<TResult> : IBelongToArchitecture, ICanSetArchitecture, ICanGetModel, ICanGetSystem, public interface IQuery<TResult> : IBelongToArchitecture, ICanSetArchitecture, ICanGetModel,
ICanSendQuery ICanSendQuery
{ {
TResult Do(); TResult Do();
@ -339,10 +339,6 @@ namespace QFrameworkCP
} }
} }
public interface ICanGetSystem : IBelongToArchitecture
{
}
public interface ICanGetUtility : IBelongToArchitecture public interface ICanGetUtility : IBelongToArchitecture
{ {
} }

925
Assets/MsgTransmitTools/src/QFramework.cs

@ -1,925 +0,0 @@
/****************************************************************************
* Copyright (c) 2015 ~ 2022 liangxiegame MIT License
*
* QFramework v1.0
*
* https://qframework.cn
* https://github.com/liangxiegame/QFramework
* https://gitee.com/liangxiegame/QFramework
*
* Author:
* liangxie https://github.com/liangxie
* soso https://github.com/so-sos-so
*
* Contributor
* TastSong https://github.com/TastSong
* https://gitee.com/JingChanChangFan/hk_-unity-tools
* () https://space.bilibili.com/656352/
*
* Community
* QQ Group: 623597263
* Latest Update: 2022.8.8 10:24 List=>HashSet
****************************************************************************/
using System;
using System.Collections.Generic;
using UnityEngine;
namespace QFramework
{
#region Architecture
public interface IArchitecture
{
void RegisterSystem<T>(T system) where T : ISystem;
void RegisterModel<T>(T model) where T : IModel;
void RegisterUtility<T>(T utility) where T : IUtility;
T GetSystem<T>() where T : class, ISystem;
T GetModel<T>() where T : class, IModel;
T GetUtility<T>() where T : class, IUtility;
void SendCommand<T>() where T : ICommand, new();
void SendCommand<T>(T command) where T : ICommand;
TResult SendQuery<TResult>(IQuery<TResult> query);
void SendEvent<T>() where T : new();
void SendEvent<T>(T e);
IUnRegister RegisterEvent<T>(Action<T> onEvent);
void UnRegisterEvent<T>(Action<T> onEvent);
}
public abstract class Architecture<T> : IArchitecture where T : Architecture<T>, new()
{
private bool mInited = false;
private HashSet<ISystem> mSystems = new HashSet<ISystem>();
private HashSet<IModel> mModels = new HashSet<IModel>();
public static Action<T> OnRegisterPatch = architecture => { };
private static T mArchitecture;
public static IArchitecture Interface
{
get
{
if (mArchitecture == null)
{
MakeSureArchitecture();
}
return mArchitecture;
}
}
static void MakeSureArchitecture()
{
if (mArchitecture == null)
{
mArchitecture = new T();
mArchitecture.Init();
OnRegisterPatch?.Invoke(mArchitecture);
foreach (var architectureModel in mArchitecture.mModels)
{
architectureModel.Init();
}
mArchitecture.mModels.Clear();
foreach (var architectureSystem in mArchitecture.mSystems)
{
architectureSystem.Init();
}
mArchitecture.mSystems.Clear();
mArchitecture.mInited = true;
}
}
protected abstract void Init();
private IOCContainer mContainer = new IOCContainer();
public void RegisterSystem<TSystem>(TSystem system) where TSystem : ISystem
{
system.SetArchitecture(this);
mContainer.Register<TSystem>(system);
if (!mInited)
{
mSystems.Add(system);
}
else
{
system.Init();
}
}
public void RegisterModel<TModel>(TModel model) where TModel : IModel
{
model.SetArchitecture(this);
mContainer.Register<TModel>(model);
if (!mInited)
{
mModels.Add(model);
}
else
{
model.Init();
}
}
public void RegisterUtility<TUtility>(TUtility utility) where TUtility : IUtility
{
mContainer.Register<TUtility>(utility);
}
public TSystem GetSystem<TSystem>() where TSystem : class, ISystem
{
return mContainer.Get<TSystem>();
}
public TModel GetModel<TModel>() where TModel : class, IModel
{
return mContainer.Get<TModel>();
}
public TUtility GetUtility<TUtility>() where TUtility : class, IUtility
{
return mContainer.Get<TUtility>();
}
public void SendCommand<TCommand>() where TCommand : ICommand, new()
{
var command = new TCommand();
ExecuteCommand(command);
}
public void SendCommand<TCommand>(TCommand command) where TCommand : ICommand
{
ExecuteCommand(command);
}
protected virtual void ExecuteCommand(ICommand command)
{
command.SetArchitecture(this);
command.Execute();
}
public TResult SendQuery<TResult>(IQuery<TResult> query)
{
return DoQuery<TResult>(query);
}
protected virtual TResult DoQuery<TResult>(IQuery<TResult> query)
{
query.SetArchitecture(this);
return query.Do();
}
private TypeEventSystem mTypeEventSystem = new TypeEventSystem();
public void SendEvent<TEvent>() where TEvent : new()
{
mTypeEventSystem.Send<TEvent>();
}
public void SendEvent<TEvent>(TEvent e)
{
mTypeEventSystem.Send<TEvent>(e);
}
public IUnRegister RegisterEvent<TEvent>(Action<TEvent> onEvent)
{
return mTypeEventSystem.Register<TEvent>(onEvent);
}
public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent)
{
mTypeEventSystem.UnRegister<TEvent>(onEvent);
}
}
public interface IOnEvent<T>
{
void OnEvent(T e);
}
public static class OnGlobalEventExtension
{
public static IUnRegister RegisterEvent<T>(this IOnEvent<T> self) where T : struct
{
return TypeEventSystem.Global.Register<T>(self.OnEvent);
}
public static void UnRegisterEvent<T>(this IOnEvent<T> self) where T : struct
{
TypeEventSystem.Global.UnRegister<T>(self.OnEvent);
}
}
#endregion
#region Controller
public interface IController : IBelongToArchitecture, ICanSendCommand, ICanGetSystem, ICanGetModel,
ICanRegisterEvent, ICanSendQuery
{
}
#endregion
#region System
public interface ISystem : IBelongToArchitecture, ICanSetArchitecture, ICanGetModel, ICanGetUtility,
ICanRegisterEvent, ICanSendEvent, ICanGetSystem
{
void Init();
}
public abstract class AbstractSystem : ISystem
{
private IArchitecture mArchitecture;
IArchitecture IBelongToArchitecture.GetArchitecture()
{
return mArchitecture;
}
void ICanSetArchitecture.SetArchitecture(IArchitecture architecture)
{
mArchitecture = architecture;
}
void ISystem.Init()
{
OnInit();
}
protected abstract void OnInit();
}
#endregion
#region Model
public interface IModel : IBelongToArchitecture, ICanSetArchitecture, ICanGetUtility, ICanSendEvent
{
void Init();
}
public abstract class AbstractModel : IModel
{
private IArchitecture mArchitecturel;
IArchitecture IBelongToArchitecture.GetArchitecture()
{
return mArchitecturel;
}
void ICanSetArchitecture.SetArchitecture(IArchitecture architecture)
{
mArchitecturel = architecture;
}
void IModel.Init()
{
OnInit();
}
protected abstract void OnInit();
}
#endregion
#region Utility
public interface IUtility
{
}
#endregion
#region Command
public interface ICommand : IBelongToArchitecture, ICanSetArchitecture, ICanGetSystem, ICanGetModel, ICanGetUtility,
ICanSendEvent, ICanSendCommand, ICanSendQuery
{
void Execute();
}
public abstract class AbstractCommand : ICommand
{
private IArchitecture mArchitecture;
IArchitecture IBelongToArchitecture.GetArchitecture()
{
return mArchitecture;
}
void ICanSetArchitecture.SetArchitecture(IArchitecture architecture)
{
mArchitecture = architecture;
}
void ICommand.Execute()
{
OnExecute();
}
protected abstract void OnExecute();
}
#endregion
#region Query
public interface IQuery<TResult> : IBelongToArchitecture, ICanSetArchitecture, ICanGetModel, ICanGetSystem,
ICanSendQuery
{
TResult Do();
}
public abstract class AbstractQuery<T> : IQuery<T>
{
public T Do()
{
return OnDo();
}
protected abstract T OnDo();
private IArchitecture mArchitecture;
public IArchitecture GetArchitecture()
{
return mArchitecture;
}
public void SetArchitecture(IArchitecture architecture)
{
mArchitecture = architecture;
}
}
#endregion
#region Rule
public interface IBelongToArchitecture
{
IArchitecture GetArchitecture();
}
public interface ICanSetArchitecture
{
void SetArchitecture(IArchitecture architecture);
}
public interface ICanGetModel : IBelongToArchitecture
{
}
public static class CanGetModelExtension
{
public static T GetModel<T>(this ICanGetModel self) where T : class, IModel
{
return self.GetArchitecture().GetModel<T>();
}
}
public interface ICanGetSystem : IBelongToArchitecture
{
}
public static class CanGetSystemExtension
{
public static T GetSystem<T>(this ICanGetSystem self) where T : class, ISystem
{
return self.GetArchitecture().GetSystem<T>();
}
}
public interface ICanGetUtility : IBelongToArchitecture
{
}
public static class CanGetUtilityExtension
{
public static T GetUtility<T>(this ICanGetUtility self) where T : class, IUtility
{
return self.GetArchitecture().GetUtility<T>();
}
}
public interface ICanRegisterEvent : IBelongToArchitecture
{
}
public static class CanRegisterEventExtension
{
public static IUnRegister RegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent)
{
return self.GetArchitecture().RegisterEvent<T>(onEvent);
}
public static void UnRegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent)
{
self.GetArchitecture().UnRegisterEvent<T>(onEvent);
}
}
public interface ICanSendCommand : IBelongToArchitecture
{
}
public static class CanSendCommandExtension
{
public static void SendCommand<T>(this ICanSendCommand self) where T : ICommand, new()
{
self.GetArchitecture().SendCommand<T>();
}
public static void SendCommand<T>(this ICanSendCommand self, T command) where T : ICommand
{
self.GetArchitecture().SendCommand<T>(command);
}
}
public interface ICanSendEvent : IBelongToArchitecture
{
}
public static class CanSendEventExtension
{
public static void SendEvent<T>(this ICanSendEvent self) where T : new()
{
self.GetArchitecture().SendEvent<T>();
}
public static void SendEvent<T>(this ICanSendEvent self, T e)
{
self.GetArchitecture().SendEvent<T>(e);
}
}
public interface ICanSendQuery : IBelongToArchitecture
{
}
public static class CanSendQueryExtension
{
public static TResult SendQuery<TResult>(this ICanSendQuery self, IQuery<TResult> query)
{
return self.GetArchitecture().SendQuery(query);
}
}
#endregion
#region TypeEventSystem
public interface IUnRegister
{
void UnRegister();
}
public interface IUnRegisterList
{
List<IUnRegister> UnregisterList { get; }
}
public static class IUnRegisterListExtension
{
public static void AddToUnregisterList(this IUnRegister self, IUnRegisterList unRegisterList)
{
unRegisterList.UnregisterList.Add(self);
}
public static void UnRegisterAll(this IUnRegisterList self)
{
foreach (var unRegister in self.UnregisterList)
{
unRegister.UnRegister();
}
self.UnregisterList.Clear();
}
}
/// <summary>
/// 自定义可注销的类
/// </summary>
public struct CustomUnRegister : IUnRegister
{
/// <summary>
/// 委托对象
/// </summary>
private Action mOnUnRegister { get; set; }
/// <summary>
/// 带参构造函数
/// </summary>
/// <param name="onDispose"></param>
public CustomUnRegister(Action onUnRegsiter)
{
mOnUnRegister = onUnRegsiter;
}
/// <summary>
/// 资源释放
/// </summary>
public void UnRegister()
{
mOnUnRegister.Invoke();
mOnUnRegister = null;
}
}
public class UnRegisterOnDestroyTrigger : MonoBehaviour
{
private readonly HashSet<IUnRegister> mUnRegisters = new HashSet<IUnRegister>();
public void AddUnRegister(IUnRegister unRegister)
{
mUnRegisters.Add(unRegister);
}
public void RemoveUnRegister(IUnRegister unRegister)
{
mUnRegisters.Remove(unRegister);
}
private void OnDestroy()
{
foreach (var unRegister in mUnRegisters)
{
unRegister.UnRegister();
}
mUnRegisters.Clear();
}
}
public static class UnRegisterExtension
{
public static IUnRegister UnRegisterWhenGameObjectDestroyed(this IUnRegister unRegister, GameObject gameObject)
{
var trigger = gameObject.GetComponent<UnRegisterOnDestroyTrigger>();
if (!trigger)
{
trigger = gameObject.AddComponent<UnRegisterOnDestroyTrigger>();
}
trigger.AddUnRegister(unRegister);
return unRegister;
}
}
public class TypeEventSystem
{
private readonly EasyEvents mEvents = new EasyEvents();
public static readonly TypeEventSystem Global = new TypeEventSystem();
public void Send<T>() where T : new()
{
mEvents.GetEvent<EasyEvent<T>>()?.Trigger(new T());
}
public void Send<T>(T e)
{
mEvents.GetEvent<EasyEvent<T>>()?.Trigger(e);
}
public IUnRegister Register<T>(Action<T> onEvent)
{
var e = mEvents.GetOrAddEvent<EasyEvent<T>>();
return e.Register(onEvent);
}
public void UnRegister<T>(Action<T> onEvent)
{
var e = mEvents.GetEvent<EasyEvent<T>>();
if (e != null)
{
e.UnRegister(onEvent);
}
}
}
#endregion
#region IOC
public class IOCContainer
{
private Dictionary<Type, object> mInstances = new Dictionary<Type, object>();
public void Register<T>(T instance)
{
var key = typeof(T);
if (mInstances.ContainsKey(key))
{
mInstances[key] = instance;
}
else
{
mInstances.Add(key, instance);
}
}
public T Get<T>() where T : class
{
var key = typeof(T);
if (mInstances.TryGetValue(key, out var retInstance))
{
return retInstance as T;
}
return null;
}
}
#endregion
#region BindableProperty
public interface IBindableProperty<T> : IReadonlyBindableProperty<T>
{
new T Value { get; set; }
void SetValueWithoutEvent(T newValue);
}
public interface IReadonlyBindableProperty<T>
{
T Value { get; }
IUnRegister RegisterWithInitValue(Action<T> action);
void UnRegister(Action<T> onValueChanged);
IUnRegister Register(Action<T> onValueChanged);
}
public class BindableProperty<T> : IBindableProperty<T>
{
public BindableProperty(T defaultValue = default)
{
mValue = defaultValue;
}
protected T mValue;
public T Value
{
get => GetValue();
set
{
if (value == null && mValue == null) return;
if (value != null && value.Equals(mValue)) return;
SetValue(value);
mOnValueChanged?.Invoke(value);
}
}
protected virtual void SetValue(T newValue)
{
mValue = newValue;
}
protected virtual T GetValue()
{
return mValue;
}
public void SetValueWithoutEvent(T newValue)
{
mValue = newValue;
}
private Action<T> mOnValueChanged = (v) => { };
public IUnRegister Register(Action<T> onValueChanged)
{
mOnValueChanged += onValueChanged;
return new BindablePropertyUnRegister<T>()
{
BindableProperty = this,
OnValueChanged = onValueChanged
};
}
public IUnRegister RegisterWithInitValue(Action<T> onValueChanged)
{
onValueChanged(mValue);
return Register(onValueChanged);
}
public static implicit operator T(BindableProperty<T> property)
{
return property.Value;
}
public override string ToString()
{
return Value.ToString();
}
public void UnRegister(Action<T> onValueChanged)
{
mOnValueChanged -= onValueChanged;
}
public string Trim()
{
throw new NotImplementedException();
}
}
public class BindablePropertyUnRegister<T> : IUnRegister
{
public BindableProperty<T> BindableProperty { get; set; }
public Action<T> OnValueChanged { get; set; }
public void UnRegister()
{
BindableProperty.UnRegister(OnValueChanged);
BindableProperty = null;
OnValueChanged = null;
}
}
#endregion
#region EasyEvent
public interface IEasyEvent
{
}
public class EasyEvent : IEasyEvent
{
private Action mOnEvent = () => { };
public IUnRegister Register(Action onEvent)
{
mOnEvent += onEvent;
return new CustomUnRegister(() => { UnRegister(onEvent); });
}
public void UnRegister(Action onEvent)
{
mOnEvent -= onEvent;
}
public void Trigger()
{
mOnEvent?.Invoke();
}
}
public class EasyEvent<T> : IEasyEvent
{
private Action<T> mOnEvent = e => { };
public IUnRegister Register(Action<T> onEvent)
{
mOnEvent += onEvent;
return new CustomUnRegister(() => { UnRegister(onEvent); });
}
public void UnRegister(Action<T> onEvent)
{
mOnEvent -= onEvent;
}
public void Trigger(T t)
{
mOnEvent?.Invoke(t);
}
}
public class EasyEvent<T, K> : IEasyEvent
{
private Action<T, K> mOnEvent = (t, k) => { };
public IUnRegister Register(Action<T, K> onEvent)
{
mOnEvent += onEvent;
return new CustomUnRegister(() => { UnRegister(onEvent); });
}
public void UnRegister(Action<T, K> onEvent)
{
mOnEvent -= onEvent;
}
public void Trigger(T t, K k)
{
mOnEvent?.Invoke(t, k);
}
}
public class EasyEvent<T, K, S> : IEasyEvent
{
private Action<T, K, S> mOnEvent = (t, k, s) => { };
public IUnRegister Register(Action<T, K, S> onEvent)
{
mOnEvent += onEvent;
return new CustomUnRegister(() => { UnRegister(onEvent); });
}
public void UnRegister(Action<T, K, S> onEvent)
{
mOnEvent -= onEvent;
}
public void Trigger(T t, K k, S s)
{
mOnEvent?.Invoke(t, k, s);
}
}
public class EasyEvents
{
private static EasyEvents mGlobalEvents = new EasyEvents();
public static T Get<T>() where T : IEasyEvent
{
return mGlobalEvents.GetEvent<T>();
}
public static void Register<T>() where T : IEasyEvent, new()
{
mGlobalEvents.AddEvent<T>();
}
private Dictionary<Type, IEasyEvent> mTypeEvents = new Dictionary<Type, IEasyEvent>();
public void AddEvent<T>() where T : IEasyEvent, new()
{
mTypeEvents.Add(typeof(T), new T());
}
public T GetEvent<T>() where T : IEasyEvent
{
IEasyEvent e;
if (mTypeEvents.TryGetValue(typeof(T), out e))
{
return (T)e;
}
return default;
}
public T GetOrAddEvent<T>() where T : IEasyEvent, new()
{
var eType = typeof(T);
if (mTypeEvents.TryGetValue(eType, out var e))
{
return (T)e;
}
var t = new T();
mTypeEvents.Add(eType, t);
return t;
}
}
#endregion
#if UNITY_EDITOR
internal class EditorMenus
{
[UnityEditor.MenuItem("QFramework/Install QFrameworkWithToolKits")]
public static void InstallPackageKit()
{
Application.OpenURL("https://qframework.cn/qf");
}
}
#endif
}

11
Assets/MsgTransmitTools/src/QFramework.cs.meta

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 109e532e5f905684597f56d42cd0bbce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save