mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
升级拓展GameEvent,支持基于Interface的方法调用抛出事件,以及自动化根据声明的Interface来生成实现代码。
升级拓展GameEvent,支持基于Interface的方法调用抛出事件,以及自动化根据声明的Interface来生成实现代码。
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 40a878a415f34e7a855fc4916bbb8e6b
|
||||
timeCreated: 1702479104
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1dcaa491f139438dbd963d8bbf0dba85
|
||||
timeCreated: 1702385397
|
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class BaseAttribute: Attribute
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 819c4eaddddd4646a100da2e3f19c3c7
|
||||
timeCreated: 1702385397
|
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
public class CodeTypes
|
||||
{
|
||||
private static CodeTypes _instance;
|
||||
public static CodeTypes Instance => _instance ??= new CodeTypes();
|
||||
|
||||
private readonly Dictionary<string, Type> _allTypes = new();
|
||||
private readonly UnOrderMultiMapSet<Type, Type> _types = new();
|
||||
|
||||
public void Init(Assembly[] assemblies)
|
||||
{
|
||||
Dictionary<string, Type> addTypes = GetAssemblyTypes(assemblies);
|
||||
foreach ((string fullName, Type type) in addTypes)
|
||||
{
|
||||
_allTypes[fullName] = type;
|
||||
|
||||
if (type.IsAbstract)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 记录所有的有BaseAttribute标记的的类型
|
||||
object[] objects = type.GetCustomAttributes(typeof(BaseAttribute), true);
|
||||
|
||||
foreach (object o in objects)
|
||||
{
|
||||
_types.Add(o.GetType(), type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HashSet<Type> GetTypes(Type systemAttributeType)
|
||||
{
|
||||
if (!_types.ContainsKey(systemAttributeType))
|
||||
{
|
||||
return new HashSet<Type>();
|
||||
}
|
||||
|
||||
return _types[systemAttributeType];
|
||||
}
|
||||
|
||||
public Dictionary<string, Type> GetTypes()
|
||||
{
|
||||
return _allTypes;
|
||||
}
|
||||
|
||||
public Type GetType(string typeName)
|
||||
{
|
||||
return _allTypes[typeName];
|
||||
}
|
||||
|
||||
public static Dictionary<string, Type> GetAssemblyTypes(params Assembly[] args)
|
||||
{
|
||||
Dictionary<string, Type> types = new Dictionary<string, Type>();
|
||||
|
||||
foreach (Assembly ass in args)
|
||||
{
|
||||
foreach (Type type in ass.GetTypes())
|
||||
{
|
||||
if (type.FullName != null)
|
||||
{
|
||||
types[type.FullName] = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 01fdfc4515314c579523ac3716005210
|
||||
timeCreated: 1702385429
|
@@ -0,0 +1,80 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
public class UnOrderMultiMapSet<TKey, TValue>: Dictionary<TKey, HashSet<TValue>>
|
||||
{
|
||||
public new HashSet<TValue> this[TKey t]
|
||||
{
|
||||
get
|
||||
{
|
||||
HashSet<TValue> set;
|
||||
if (!TryGetValue(t, out set))
|
||||
{
|
||||
set = new HashSet<TValue>();
|
||||
}
|
||||
return set;
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<TKey, HashSet<TValue>> GetDictionary()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Add(TKey t, TValue k)
|
||||
{
|
||||
HashSet<TValue> set;
|
||||
TryGetValue(t, out set);
|
||||
if (set == null)
|
||||
{
|
||||
set = new HashSet<TValue>();
|
||||
base[t] = set;
|
||||
}
|
||||
set.Add(k);
|
||||
}
|
||||
|
||||
public bool Remove(TKey t, TValue k)
|
||||
{
|
||||
HashSet<TValue> set;
|
||||
TryGetValue(t, out set);
|
||||
if (set == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!set.Remove(k))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (set.Count == 0)
|
||||
{
|
||||
Remove(t);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Contains(TKey t, TValue k)
|
||||
{
|
||||
HashSet<TValue> set;
|
||||
TryGetValue(t, out set);
|
||||
if (set == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return set.Contains(k);
|
||||
}
|
||||
|
||||
public new int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
int count = 0;
|
||||
foreach (KeyValuePair<TKey,HashSet<TValue>> kv in this)
|
||||
{
|
||||
count += kv.Value.Count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b798f0c1317c4caf9ace168f07b51d4f
|
||||
timeCreated: 1702385485
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d1f693ff76ae490fbe194855d94e8266
|
||||
timeCreated: 1702479172
|
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件接口帮助类。
|
||||
/// </summary>
|
||||
internal class EventInterfaceHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 初始化。
|
||||
/// </summary>
|
||||
public static void Init()
|
||||
{
|
||||
RegisterEventInterface_Logic.Register(GameEvent.EventMgr);
|
||||
RegisterEventInterface_UI.Register(GameEvent.EventMgr);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9afaf331ee7249adb5cc0953dfd3413c
|
||||
timeCreated: 1702379658
|
@@ -0,0 +1,16 @@
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
[System.AttributeUsage(System.AttributeTargets.Class)]
|
||||
internal class EventInterfaceImpAttribute : BaseAttribute
|
||||
{
|
||||
private EEventGroup _eGroup;
|
||||
public EEventGroup EventGroup => _eGroup;
|
||||
|
||||
public EventInterfaceImpAttribute(EEventGroup group)
|
||||
{
|
||||
_eGroup = group;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8bbf40942b0e4470bb8d8a82577f713c
|
||||
timeCreated: 1702479403
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: de49bf2e9f0a4fac85851a582e2fb4ed
|
||||
timeCreated: 1702379835
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bff68b49afffbe54b9d5ff4e4cad4f23
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,70 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by autoBindTool.
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
public partial class IActorLogicEvent_Event
|
||||
{
|
||||
public static readonly int OnMainPlayerDataChange = RuntimeId.ToRuntimeId("IActorLogicEvent_Event.OnMainPlayerDataChange");
|
||||
public static readonly int OnMainPlayerLevelChange = RuntimeId.ToRuntimeId("IActorLogicEvent_Event.OnMainPlayerLevelChange");
|
||||
public static readonly int OnMainPlayerGoldChange = RuntimeId.ToRuntimeId("IActorLogicEvent_Event.OnMainPlayerGoldChange");
|
||||
public static readonly int OnMainPlayerDiamondChange = RuntimeId.ToRuntimeId("IActorLogicEvent_Event.OnMainPlayerDiamondChange");
|
||||
public static readonly int OnMainPlayerBindDiamondChange = RuntimeId.ToRuntimeId("IActorLogicEvent_Event.OnMainPlayerBindDiamondChange");
|
||||
public static readonly int OnMainPlayerCurrencyChange = RuntimeId.ToRuntimeId("IActorLogicEvent_Event.OnMainPlayerCurrencyChange");
|
||||
public static readonly int OnMainPlayerExpChange = RuntimeId.ToRuntimeId("IActorLogicEvent_Event.OnMainPlayerExpChange");
|
||||
}
|
||||
|
||||
[EventInterfaceImp(EEventGroup.GroupLogic)]
|
||||
public partial class IActorLogicEvent_Gen : IActorLogicEvent
|
||||
{
|
||||
private EventDispatcher _dispatcher;
|
||||
public IActorLogicEvent_Gen(EventDispatcher dispatcher)
|
||||
{
|
||||
_dispatcher = dispatcher;
|
||||
}
|
||||
|
||||
public void OnMainPlayerDataChange()
|
||||
{
|
||||
_dispatcher.Send(IActorLogicEvent_Event.OnMainPlayerDataChange);
|
||||
}
|
||||
|
||||
public void OnMainPlayerLevelChange()
|
||||
{
|
||||
_dispatcher.Send(IActorLogicEvent_Event.OnMainPlayerLevelChange);
|
||||
}
|
||||
|
||||
public void OnMainPlayerGoldChange(System.UInt32 oldVal,System.UInt32 newVal)
|
||||
{
|
||||
_dispatcher.Send(IActorLogicEvent_Event.OnMainPlayerGoldChange,oldVal,newVal);
|
||||
}
|
||||
|
||||
public void OnMainPlayerDiamondChange(System.UInt32 oldVal,System.UInt32 newVal)
|
||||
{
|
||||
_dispatcher.Send(IActorLogicEvent_Event.OnMainPlayerDiamondChange,oldVal,newVal);
|
||||
}
|
||||
|
||||
public void OnMainPlayerBindDiamondChange(System.UInt32 oldVal,System.UInt32 newVal)
|
||||
{
|
||||
_dispatcher.Send(IActorLogicEvent_Event.OnMainPlayerBindDiamondChange,oldVal,newVal);
|
||||
}
|
||||
|
||||
public void OnMainPlayerCurrencyChange(GameLogic.CurrencyType type,System.UInt32 oldVal,System.UInt32 newVal)
|
||||
{
|
||||
_dispatcher.Send(IActorLogicEvent_Event.OnMainPlayerCurrencyChange,type,oldVal,newVal);
|
||||
}
|
||||
|
||||
public void OnMainPlayerExpChange(System.UInt64 oldVal,System.UInt64 newVal)
|
||||
{
|
||||
_dispatcher.Send(IActorLogicEvent_Event.OnMainPlayerExpChange,oldVal,newVal);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 12d7d4edd7d06bc4286ea4af153380c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 615ca01c7a524654c91935631f39f570
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,40 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by autoBindTool.
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
public partial class ILoginUI_Event
|
||||
{
|
||||
public static readonly int OnRoleLogin = RuntimeId.ToRuntimeId("ILoginUI_Event.OnRoleLogin");
|
||||
public static readonly int OnRoleLoginOut = RuntimeId.ToRuntimeId("ILoginUI_Event.OnRoleLoginOut");
|
||||
}
|
||||
|
||||
[EventInterfaceImp(EEventGroup.GroupUI)]
|
||||
public partial class ILoginUI_Gen : ILoginUI
|
||||
{
|
||||
private EventDispatcher _dispatcher;
|
||||
public ILoginUI_Gen(EventDispatcher dispatcher)
|
||||
{
|
||||
_dispatcher = dispatcher;
|
||||
}
|
||||
|
||||
public void OnRoleLogin(System.Boolean isReconnect)
|
||||
{
|
||||
_dispatcher.Send(ILoginUI_Event.OnRoleLogin,isReconnect);
|
||||
}
|
||||
|
||||
public void OnRoleLoginOut()
|
||||
{
|
||||
_dispatcher.Send(ILoginUI_Event.OnRoleLoginOut);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bde7ed0ea10cf29448370b39ecd69a97
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7cf3381dedbf4daeb53e710a5c544204
|
||||
timeCreated: 1702433587
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 610229edeca4417685ffd07f18b2b9f1
|
||||
timeCreated: 1702379817
|
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// 示例货币枚举。
|
||||
/// </summary>
|
||||
public enum CurrencyType
|
||||
{
|
||||
None,
|
||||
Gold,
|
||||
Diamond,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 示意逻辑层事件。
|
||||
/// <remarks> 优化抛出事件,通过接口约束事件参数。</remarks>
|
||||
/// <remarks> example: GameEvent.Get<IActorLogicEvent>().OnMainPlayerCurrencyChange(CurrencyType.Gold,oldVal,newVal); </remarks>
|
||||
/// </summary>
|
||||
[EventInterface(EEventGroup.GroupLogic)]
|
||||
interface IActorLogicEvent
|
||||
{
|
||||
void OnMainPlayerDataChange();
|
||||
|
||||
void OnMainPlayerLevelChange();
|
||||
|
||||
void OnMainPlayerGoldChange(uint oldVal, uint newVal);
|
||||
|
||||
void OnMainPlayerDiamondChange(uint oldVal, uint newVal);
|
||||
|
||||
void OnMainPlayerBindDiamondChange(uint oldVal, uint newVal);
|
||||
|
||||
void OnMainPlayerCurrencyChange(CurrencyType type, uint oldVal, uint newVal);
|
||||
|
||||
void OnMainPlayerExpChange(ulong oldVal, ulong newVal);
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8d4558cb74e8462a86f0ee3461f6b7c9
|
||||
timeCreated: 1702383645
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 90e13cc92c5d42f28b4f5fab599f472a
|
||||
timeCreated: 1702379805
|
@@ -0,0 +1,17 @@
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// 示意UI层事件。
|
||||
/// <remarks> 优化抛出事件,通过接口约束事件参数。</remarks>
|
||||
/// <remarks> example: GameEvent.Get<ILoginUI>().OnRoleLogin(isReconnect); </remarks>
|
||||
/// </summary>
|
||||
[EventInterface(EEventGroup.GroupUI)]
|
||||
public interface ILoginUI
|
||||
{
|
||||
public void OnRoleLogin(bool isReconnect);
|
||||
|
||||
public void OnRoleLoginOut();
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33b45e62bd3447498acfe874017b9a35
|
||||
timeCreated: 1702433755
|
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// 逻辑层事件接口。
|
||||
/// </summary>
|
||||
internal class RegisterEventInterface_Logic
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册逻辑层事件接口。
|
||||
/// </summary>
|
||||
/// <param name="mgr">事件管理器。</param>
|
||||
public static void Register(EventMgr mgr)
|
||||
{
|
||||
HashSet<Type> types = CodeTypes.Instance.GetTypes(typeof(EventInterfaceImpAttribute));
|
||||
|
||||
foreach (Type type in types)
|
||||
{
|
||||
object[] attrs = type.GetCustomAttributes(typeof(EventInterfaceImpAttribute), false);
|
||||
if (attrs.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
EventInterfaceImpAttribute httpHandlerAttribute = (EventInterfaceImpAttribute)attrs[0];
|
||||
|
||||
if (httpHandlerAttribute.EventGroup != EEventGroup.GroupLogic)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
object obj = Activator.CreateInstance(type, mgr.Dispatcher);
|
||||
|
||||
mgr.RegWrapInterface(obj.GetType().GetInterfaces()[0]?.FullName, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8bdf6c139b44758aa16db2e1837f5d9
|
||||
timeCreated: 1702379518
|
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// UI层事件接口。
|
||||
/// </summary>
|
||||
internal class RegisterEventInterface_UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册UI层事件接口。
|
||||
/// </summary>
|
||||
/// <param name="mgr">事件管理器。</param>
|
||||
public static void Register(EventMgr mgr)
|
||||
{
|
||||
HashSet<Type> types = CodeTypes.Instance.GetTypes(typeof(EventInterfaceImpAttribute));
|
||||
|
||||
foreach (Type type in types)
|
||||
{
|
||||
object[] attrs = type.GetCustomAttributes(typeof(EventInterfaceImpAttribute), false);
|
||||
if (attrs.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
EventInterfaceImpAttribute httpHandlerAttribute = (EventInterfaceImpAttribute)attrs[0];
|
||||
|
||||
if (httpHandlerAttribute.EventGroup != EEventGroup.GroupUI)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
object obj = Activator.CreateInstance(type, mgr.Dispatcher);
|
||||
|
||||
mgr.RegWrapInterface(obj.GetType().GetInterfaces()[0]?.FullName, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f53b67f2cfbe4912bffee9593cd60970
|
||||
timeCreated: 1702379505
|
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using GameLogic;
|
||||
using TEngine;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -8,6 +9,8 @@ public partial class GameApp
|
||||
|
||||
private void Init()
|
||||
{
|
||||
CodeTypes.Instance.Init(_hotfixAssembly.ToArray());
|
||||
EventInterfaceHelper.Init();
|
||||
_listLogicMgr = new List<ILogicSys>();
|
||||
RegisterAllSystem();
|
||||
InitSystemSetting();
|
||||
|
8
UnityProject/Assets/TEngine/Editor/EventInterface.meta
Normal file
8
UnityProject/Assets/TEngine/Editor/EventInterface.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86094b7c7ca31ab4da174cc7eae14a3c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,199 @@
|
||||
#region Class Documentation
|
||||
/************************************************************************************************************
|
||||
Class Name: EventInterfaceGenerate.cs
|
||||
Type: Editor, Generator, Util, Static
|
||||
Definition:
|
||||
用法,在目录"Assets/GameScripts/HotFix/GameLogic/Event/Interface/"下分组照示例声明Interface 模块待抛出事件的接口。编译后自动生成接口实现抛出的脚本。
|
||||
Example:
|
||||
|
||||
旧版抛出事件方式: GameEvent.Send(RuntimeId.ToRuntimeId("OnMainPlayerCurrencyChange"),CurrencyType.Gold,oldVal,newVal);
|
||||
|
||||
新版抛出事件方式 : GameEvent.Get<IActorLogicEvent>().OnMainPlayerCurrencyChange(CurrencyType.Gold,oldVal,newVal);
|
||||
|
||||
************************************************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using TEngine;
|
||||
using Unity.EditorCoroutines.Editor;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
[InitializeOnLoad]
|
||||
public static class EventInterfaceGenerate
|
||||
{
|
||||
public static string NameSpace = @"GameLogic";
|
||||
|
||||
|
||||
public const string EventInterfacePath = "Assets/GameScripts/HotFix/GameLogic/Event/Interface/";
|
||||
|
||||
static EventInterfaceGenerate()
|
||||
{
|
||||
Generate();
|
||||
}
|
||||
|
||||
[MenuItem("TEngine/Generate EventInterface", false, 300)]
|
||||
public static void Generate()
|
||||
{
|
||||
if (EventInterfaceGenerateTag.HadGenerate)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
EventInterfaceGenerateTag.HadGenerate = true;
|
||||
|
||||
// 加载程序集
|
||||
Assembly assembly = typeof(GameApp).Assembly;
|
||||
|
||||
// 获取程序集中的所有类型
|
||||
Type[] types = assembly.GetTypes();
|
||||
|
||||
// 遍历每个类型
|
||||
foreach (Type type in types)
|
||||
{
|
||||
// 检查类型是否是接口
|
||||
if (!type.IsInterface)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var attribute = type.GetCustomAttributes(typeof(EventInterfaceAttribute), false).FirstOrDefault();
|
||||
|
||||
if (attribute != null)
|
||||
{
|
||||
EventInterfaceAttribute eventInterfaceAttribute = attribute as EventInterfaceAttribute;
|
||||
|
||||
GenAutoBindCode(type, eventInterfaceAttribute);
|
||||
}
|
||||
}
|
||||
|
||||
AssetDatabase.Refresh();
|
||||
Debug.Log("Generate EventInterface Complete");
|
||||
// EditorUtility.DisplayDialog("提示", "代码生成完毕", "OK");
|
||||
|
||||
EditorCoroutineUtility.StartCoroutine(EventInterfaceGenerateTag.Reset(), null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成自动绑定代码
|
||||
/// </summary>
|
||||
private static void GenAutoBindCode(Type interfaceType, EventInterfaceAttribute eventInterfaceAttribute)
|
||||
{
|
||||
string interfaceName = interfaceType.Name;
|
||||
string className = $"{interfaceName}_Gen";
|
||||
string codePath = $"{Application.dataPath}/GameScripts/HotFix/GameLogic/Event/Gen/{eventInterfaceAttribute.EventGroup}";
|
||||
|
||||
if (!Directory.Exists(codePath))
|
||||
{
|
||||
Directory.CreateDirectory(codePath);
|
||||
}
|
||||
|
||||
using (StreamWriter sw = new StreamWriter($"{codePath}/{className}.cs"))
|
||||
{
|
||||
sw.WriteLine(
|
||||
$"//------------------------------------------------------------------------------\n//\t<auto-generated>\n//\t\tThis code was generated by autoBindTool.\n//\t\tChanges to this file may cause incorrect behavior and will be lost if\n//\t\tthe code is regenerated.\n//\t</auto-generated>\n//------------------------------------------------------------------------------");
|
||||
sw.WriteLine("using UnityEngine;");
|
||||
sw.WriteLine("using UnityEngine.UI;");
|
||||
sw.WriteLine("using TEngine;");
|
||||
sw.WriteLine("");
|
||||
|
||||
if (!string.IsNullOrEmpty(NameSpace))
|
||||
{
|
||||
//命名空间
|
||||
sw.WriteLine("namespace " + NameSpace);
|
||||
sw.WriteLine("{");
|
||||
}
|
||||
|
||||
#region EventId生成
|
||||
|
||||
sw.WriteLine($"\tpublic partial class {interfaceName}_Event");
|
||||
sw.WriteLine("\t{");
|
||||
|
||||
// 获取接口中的所有方法
|
||||
MethodInfo[] methods = interfaceType.GetMethods();
|
||||
|
||||
//组件字段
|
||||
foreach (MethodInfo method in methods)
|
||||
{
|
||||
sw.WriteLine($"\t\tpublic static readonly int {method.Name} = RuntimeId.ToRuntimeId(\"{interfaceName}_Event.{method.Name}\");");
|
||||
}
|
||||
|
||||
sw.WriteLine("\t}");
|
||||
sw.WriteLine("");
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
//类名
|
||||
sw.WriteLine($"\t[EventInterfaceImp(EEventGroup.{eventInterfaceAttribute.EventGroup})]");
|
||||
sw.WriteLine($"\tpublic partial class {className} : {interfaceName}");
|
||||
sw.WriteLine("\t{");
|
||||
|
||||
sw.WriteLine("\t\tprivate EventDispatcher _dispatcher;");
|
||||
sw.WriteLine($"\t\tpublic {className}(EventDispatcher dispatcher)");
|
||||
sw.WriteLine("\t\t{");
|
||||
sw.WriteLine($"\t\t\t_dispatcher = dispatcher;");
|
||||
sw.WriteLine("\t\t}");
|
||||
sw.WriteLine("");
|
||||
|
||||
//组件字段
|
||||
foreach (MethodInfo methodInfo in methods)
|
||||
{
|
||||
ParameterInfo[] parameterInfos = methodInfo.GetParameters(); //得到指定方法的参数列表
|
||||
if (parameterInfos.Length <= 0)
|
||||
{
|
||||
sw.WriteLine(
|
||||
$" public void {methodInfo.Name}()\n {{\n _dispatcher.Send({interfaceName}_Event.{methodInfo.Name});\n }}");
|
||||
}
|
||||
else
|
||||
{
|
||||
string paramStr = "";
|
||||
string paramStr2 = "";
|
||||
for (int i = 0; i < parameterInfos.Length; i++)
|
||||
{
|
||||
var parameterInfo = parameterInfos[i];
|
||||
Type type = parameterInfo.ParameterType;
|
||||
string paramName = parameterInfo.Name;
|
||||
if (i == parameterInfos.Length - 1)
|
||||
{
|
||||
paramStr += $"{type.FullName} {paramName}";
|
||||
paramStr2 += $"{paramName}";
|
||||
}
|
||||
else
|
||||
{
|
||||
paramStr += $"{type.FullName} {paramName},";
|
||||
paramStr2 += $"{paramName},";
|
||||
}
|
||||
}
|
||||
|
||||
sw.WriteLine(
|
||||
$" public void {methodInfo.Name}({paramStr})\n {{\n _dispatcher.Send({interfaceName}_Event.{methodInfo.Name},{paramStr2});\n }}");
|
||||
}
|
||||
|
||||
sw.WriteLine("");
|
||||
}
|
||||
|
||||
sw.WriteLine("\t}");
|
||||
|
||||
if (!string.IsNullOrEmpty(NameSpace))
|
||||
{
|
||||
sw.WriteLine("}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class EventInterfaceGenerateTag
|
||||
{
|
||||
public static bool HadGenerate = false;
|
||||
|
||||
public static IEnumerator Reset()
|
||||
{
|
||||
yield return new WaitForSeconds(10f);
|
||||
HadGenerate = false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3e3af172e88343e4cb49c9e870518ede
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -5,7 +5,9 @@
|
||||
"GUID:24c092aee38482f4e80715eaa8148782",
|
||||
"GUID:e34a5702dd353724aa315fb8011f08c3",
|
||||
"GUID:4d1926c9df5b052469a1c63448b7609a",
|
||||
"GUID:2373f786d14518f44b0f475db77ba4de"
|
||||
"GUID:2373f786d14518f44b0f475db77ba4de",
|
||||
"GUID:6e76b07590314a543b982daed6af2509",
|
||||
"GUID:478a2357cc57436488a56e564b08d223"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
|
@@ -19,10 +19,11 @@ namespace TEngine
|
||||
}
|
||||
|
||||
[System.AttributeUsage(System.AttributeTargets.Interface)]
|
||||
public class EventInterface : Attribute
|
||||
public class EventInterfaceAttribute : Attribute
|
||||
{
|
||||
private EEventGroup _eGroup;
|
||||
public EventInterface(EEventGroup group)
|
||||
public EEventGroup EventGroup => _eGroup;
|
||||
public EventInterfaceAttribute(EEventGroup group)
|
||||
{
|
||||
_eGroup = group;
|
||||
}
|
||||
|
@@ -51,6 +51,21 @@ namespace TEngine
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册wrap的函数。
|
||||
/// </summary>
|
||||
/// <param name="typeName">类型名称。</param>
|
||||
/// <param name="callerWrap">调用接口名。</param>
|
||||
public void RegWrapInterface(string typeName,object callerWrap)
|
||||
{
|
||||
var entry = new EventEntryData();
|
||||
entry.InterfaceWrap = callerWrap;
|
||||
if (typeName != null)
|
||||
{
|
||||
_eventEntryMap.Add(typeName, entry);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分发注册器。
|
||||
/// </summary>
|
||||
|
@@ -12,6 +12,7 @@ namespace TEngine
|
||||
/// </summary>
|
||||
private static readonly EventMgr _eventMgr = new EventMgr();
|
||||
|
||||
public static EventMgr EventMgr => _eventMgr;
|
||||
#region 细分的注册接口
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user