提交 bd95de1e 编写于 作者: T tanghai

整理了客户端代码,以方便做ilruntime兼容

上级 bd7c17d7
......@@ -11,12 +11,12 @@ namespace Hotfix
R2C_Login response = new R2C_Login();
try
{
if (message.Account != "abcdef" || message.Password != "111111")
{
response.Error = ErrorCode.ERR_AccountOrPasswordError;
reply(response);
return;
}
//if (message.Account != "abcdef" || message.Password != "111111")
//{
// response.Error = ErrorCode.ERR_AccountOrPasswordError;
// reply(response);
// return;
//}
// 随机分配一个Gate
StartConfig config = Game.Scene.GetComponent<RealmGateAddressComponent>().GetAddress();
......
fileFormatVersion: 2
guid: ff8cd026c365b524ca9429d49f88f14d
timeCreated: 1487236049
licenseType: Pro
guid: 9f6d9adc6f537764fa0fea29671e77bf
timeCreated: 1504164637
licenseType: Free
NativeFormatImporter:
mainObjectFileID: 100100000
userData:
assetBundleName: uilobby.unity3d
assetBundleName: uilogin.unity3d
assetBundleVariant:
......@@ -2,7 +2,7 @@
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeAfterChangeNodeType)]
[Event((int)EventIdType.BehaviorTreeAfterChangeNodeType)]
public class BehaviorTreeAfterChangeNodeTypeEvent_SelectNode: IEvent
{
public void Run()
......
......@@ -2,7 +2,7 @@
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeClickNode)]
[Event((int)EventIdType.BehaviorTreeClickNode)]
public class BehaviorTreeClickNodeEvent_SelectNode: IEvent<NodeDesigner>
{
public void Run(NodeDesigner dstNode)
......
......@@ -2,7 +2,7 @@
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeConnectState)]
[Event((int)EventIdType.BehaviorTreeConnectState)]
public class BehaviorTreeConnectStateEvent_HandleConnectLines: IEvent<NodeDesigner, State>
{
public void Run(NodeDesigner nodeDesigner, State state)
......
......@@ -2,7 +2,7 @@
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeCreateNode)]
[Event((int)EventIdType.BehaviorTreeCreateNode)]
public class BehaviorTreeCreateNodeEvent_SelectNode: IEvent<NodeDesigner>
{
public void Run(NodeDesigner dstNode)
......
......@@ -2,7 +2,7 @@
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeMouseInNode)]
[Event((int)EventIdType.BehaviorTreeMouseInNode)]
public class BehaviorTreeMouseInNodeEvent_HandleOperate: IEvent<BehaviorNodeData, NodeDesigner>
{
public void Run(BehaviorNodeData nodeData, NodeDesigner nodeDesigner)
......
......@@ -3,7 +3,7 @@ using UnityEngine;
namespace MyEditor
{
[Event(EventIdType.BehaviorTreePropertyDesignerNewCreateClick)]
[Event((int)EventIdType.BehaviorTreePropertyDesignerNewCreateClick)]
public class BehaviorTreeNewCreateClickEvent_CreateNode: IEvent<string, Vector2>
{
public void Run(string name, Vector2 pos)
......
......@@ -3,7 +3,7 @@ using UnityEngine;
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeOpenEditor)]
[Event((int)EventIdType.BehaviorTreeOpenEditor)]
public class BehaviorTreeOpenEditorEvent_SelectNode: IEvent
{
public void Run()
......
......@@ -2,7 +2,7 @@
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeOpenEditor)]
[Event((int)EventIdType.BehaviorTreeOpenEditor)]
public class BehaviorTreeOpenEditorEvent_UpdatePropList: IEvent
{
public void Run()
......
......@@ -3,7 +3,7 @@ using UnityEngine;
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeReplaceClick)]
[Event((int)EventIdType.BehaviorTreeReplaceClick)]
public class BehaviorTreeReplaceClickEvent_ReplaceNode: IEvent<string, Vector2>
{
public void Run(string name, Vector2 pos)
......
......@@ -2,7 +2,7 @@
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeRightDesignerDrag)]
[Event((int)EventIdType.BehaviorTreeRightDesignerDrag)]
public class BehaviorTreeRightDesignerDragEvent_ModifyRightBorder: IEvent<float>
{
public void Run(float deltaX)
......
......@@ -3,7 +3,7 @@ using Model;
namespace MyEditor
{
[Event(EventIdType.BehaviorTreeRunTreeEvent)]
[Event((int)EventIdType.BehaviorTreeRunTreeEvent)]
public class BehaviorTreeRunTreeEvent_ShowDebugInfo: IEvent<BehaviorTree, List<long>>
{
public void Run(BehaviorTree tree, List<long> pathList)
......
fileFormatVersion: 2
guid: f5aff6e47e6bc6f4ab8d7a79a41b9a74
timeCreated: 1474946800
licenseType: Pro
NativeFormatImporter:
guid: fc3c76c649ab2b648ba4a4492ef73703
timeCreated: 1504145873
licenseType: Free
TextScriptImporter:
userData:
assetBundleName:
assetBundleVariant:
......@@ -5,10 +5,10 @@ GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
serializedVersion: 4
serializedVersion: 5
m_Component:
- 4: {fileID: 425028}
- 114: {fileID: 114000012985339954}
- component: {fileID: 425028}
- component: {fileID: 114000012985339954}
m_Layer: 0
m_Name: Code
m_TagString: Untagged
......@@ -25,10 +25,10 @@ Transform:
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &100100000
Prefab:
m_ObjectHideFlags: 1
......@@ -56,5 +56,3 @@ MonoBehaviour:
gameObject: {fileID: 4900000, guid: ae44b218243cfc347af99a6875e9e11c, type: 3}
- key: Hotfix.pdb
gameObject: {fileID: 4900000, guid: dd08ff64f788e7a4ba0f918632ff8d0f, type: 3}
- key: Hotfix.dll.mdb
gameObject: {fileID: 4900000, guid: 23e92275d463b2145ba2a28b5f1bb7dc, type: 3}
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &184536
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
serializedVersion: 4
m_Component:
- 4: {fileID: 425028}
- 114: {fileID: 114000012016491872}
m_Layer: 0
m_Name: Config
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &425028
Transform:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 184536}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
--- !u!1001 &100100000
Prefab:
m_ObjectHideFlags: 1
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications: []
m_RemovedComponents: []
m_ParentPrefab: {fileID: 0}
m_RootGameObject: {fileID: 184536}
m_IsPrefabParent: 1
--- !u!114 &114000012016491872
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 184536}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 502d8cafd6a5a0447ab1db9a24cdcb10, type: 3}
m_Name:
m_EditorClassIdentifier:
data: []
......@@ -326,6 +326,7 @@ GameObject:
serializedVersion: 5
m_Component:
- component: {fileID: 1472883029}
- component: {fileID: 1472883030}
m_Layer: 0
m_Name: UI
m_TagString: Untagged
......@@ -347,6 +348,20 @@ Transform:
m_Father: {fileID: 575235020}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1472883030
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1472883028}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 502d8cafd6a5a0447ab1db9a24cdcb10, type: 3}
m_Name:
m_EditorClassIdentifier:
data:
- key: LoginCanvas
gameObject: {fileID: 1848209001}
--- !u!1 &1518071806
GameObject:
m_ObjectHideFlags: 0
......@@ -388,7 +403,7 @@ GameObject:
- component: {fileID: 1848209004}
- component: {fileID: 1848209003}
m_Layer: 5
m_Name: UICanvas
m_Name: LoginCanvas
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
......
namespace Model
{
public static class EventIdType
public enum EventIdType
{
public const int InitSceneStart = 1;
InitSceneStart,
public const int BehaviorTreeRunTreeEvent = 2;
public const int BehaviorTreeOpenEditor = 3;
public const int BehaviorTreeClickNode = 4;
public const int BehaviorTreeAfterChangeNodeType = 5;
public const int BehaviorTreeCreateNode = 6;
public const int BehaviorTreePropertyDesignerNewCreateClick = 7;
public const int BehaviorTreeMouseInNode = 8;
public const int BehaviorTreeConnectState = 9;
public const int BehaviorTreeReplaceClick = 10;
public const int BehaviorTreeRightDesignerDrag = 11;
BehaviorTreeRunTreeEvent,
BehaviorTreeOpenEditor,
BehaviorTreeClickNode,
BehaviorTreeAfterChangeNodeType,
BehaviorTreeCreateNode,
BehaviorTreePropertyDesignerNewCreateClick,
BehaviorTreeMouseInNode,
BehaviorTreeConnectState,
BehaviorTreeReplaceClick,
BehaviorTreeRightDesignerDrag,
public const int SessionRecvMessage = 12;
SessionRecvMessage,
}
}
\ No newline at end of file
......@@ -31,5 +31,53 @@ namespace Model
return __ret;
}
public static unsafe StackObject* LogInfo(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)
{
//ILRuntime的调用约定为被调用者清理堆栈,因此执行这个函数后需要将参数从堆栈清理干净,并把返回值放在栈顶,具体请看ILRuntime实现原理文档
ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
StackObject* ptr_of_this_method;
//这个是最后方法返回后esp栈指针的值,应该返回清理完参数并指向返回值,这里是只需要返回清理完参数的值即可
StackObject* __ret = ILIntepreter.Minus(__esp, 1);
//取Log方法的参数,如果有两个参数的话,第一个参数是esp - 2,第二个参数是esp -1, 因为Mono的bug,直接-2值会错误,所以要调用ILIntepreter.Minus
ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
//这里是将栈指针上的值转换成object,如果是基础类型可直接通过ptr->Value和ptr->ValueLow访问到值,具体请看ILRuntime实现原理文档
object message = typeof(object).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
//所有非基础类型都得调用Free来释放托管堆栈
__intp.Free(ptr_of_this_method);
//在真实调用Debug.Log前,我们先获取DLL内的堆栈
var stacktrace = __domain.DebugService.GetStackTrance(__intp);
//我们在输出信息后面加上DLL堆栈
Log.Info(message + "\n" + stacktrace);
return __ret;
}
public static unsafe StackObject* LogError(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)
{
//ILRuntime的调用约定为被调用者清理堆栈,因此执行这个函数后需要将参数从堆栈清理干净,并把返回值放在栈顶,具体请看ILRuntime实现原理文档
ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
StackObject* ptr_of_this_method;
//这个是最后方法返回后esp栈指针的值,应该返回清理完参数并指向返回值,这里是只需要返回清理完参数的值即可
StackObject* __ret = ILIntepreter.Minus(__esp, 1);
//取Log方法的参数,如果有两个参数的话,第一个参数是esp - 2,第二个参数是esp -1, 因为Mono的bug,直接-2值会错误,所以要调用ILIntepreter.Minus
ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
//这里是将栈指针上的值转换成object,如果是基础类型可直接通过ptr->Value和ptr->ValueLow访问到值,具体请看ILRuntime实现原理文档
object message = typeof(object).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
//所有非基础类型都得调用Free来释放托管堆栈
__intp.Free(ptr_of_this_method);
//在真实调用Debug.Log前,我们先获取DLL内的堆栈
var stacktrace = __domain.DebugService.GetStackTrance(__intp);
//我们在输出信息后面加上DLL堆栈
Log.Error(message + "\n" + stacktrace);
return __ret;
}
}
}
namespace Model
{
public static class EntityEventId
public enum EntityEventId
{
public const int TimerComponent = 1;
public const int ClientConfigComponent = 2;
public const int EventComponent = 3;
public const int GameObjectComponent = 4;
public const int ILEventComponent = 5;
public const int MessageDispatherComponent = 6;
public const int NetInnerComponent = 7;
public const int NetOuterComponent = 8;
public const int RobotComponent = 9;
public const int UIComponent = 10;
public const int WWWAsync = 11;
public const int ILRuntimeComponent = 12;
public const int ResourcesComponent = 13;
public const int UnitComponent = 14;
TimerComponent,
ClientConfigComponent,
EventComponent,
GameObjectComponent,
ILEventComponent,
MessageDispatherComponent,
NetInnerComponent,
NetOuterComponent,
RobotComponent,
UIComponent,
WWWAsync,
ILRuntimeComponent,
ResourcesComponent,
UnitComponent,
public const int AppManagerComponent = 15;
public const int GateSessionKeyComponent = 16;
public const int OptionComponent = 17;
public const int StartConfigComponent = 18;
public const int LockComponent = 19;
public const int MasterComponent = 20;
public const int RealmGateAddressComponent = 21;
public const int UILobbyComponent = 22;
public const int BehaviorTreeComponent = 23;
public const int CrossComponent = 24;
AppManagerComponent,
GateSessionKeyComponent,
OptionComponent,
StartConfigComponent,
LockComponent,
MasterComponent,
RealmGateAddressComponent,
UILoginComponent,
BehaviorTreeComponent,
CrossComponent,
}
}
namespace Model
{
public interface IStart
{
void Start();
}
}
fileFormatVersion: 2
guid: 5a6304f22c9f47b4b5992372e8080252
timeCreated: 1504145679
\ No newline at end of file
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Model
{
public sealed class ObjectEvents
{
private static ObjectEvents instance;
private Queue<Disposer> loads = new Queue<Disposer>();
private Queue<Disposer> loads2 = new Queue<Disposer>();
private Queue<Disposer> starts = new Queue<Disposer>();
private Queue<Disposer> updates = new Queue<Disposer>();
private Queue<Disposer> updates2 = new Queue<Disposer>();
private Queue<Disposer> lateUpdates = new Queue<Disposer>();
private Queue<Disposer> lateUpdates2 = new Queue<Disposer>();
public static ObjectEvents Instance
{
get
......@@ -23,21 +28,54 @@ namespace Model
}
}
public static void Close()
{
instance = null;
}
private readonly Dictionary<string, Assembly> dictionary = new Dictionary<string, Assembly>();
public void Add(string name, Assembly assembly)
{
this.dictionary[name] = assembly;
this.Load();
}
public void Remove(string name)
{
this.dictionary.Remove(name);
}
public Assembly[] GetAll()
{
return this.dictionary.Values.ToArray();
}
public Assembly Get(string name)
{
return this.dictionary[name];
}
public void Add(Disposer disposer)
{
if (disposer is ILoad)
{
loads.Enqueue(disposer);
this.loads.Enqueue(disposer);
}
if (disposer is IStart)
{
this.starts.Enqueue(disposer);
}
if (disposer is IUpdate)
{
updates.Enqueue(disposer);
this.updates.Enqueue(disposer);
}
if (disposer is ILateUpdate)
{
lateUpdates.Enqueue(disposer);
this.lateUpdates.Enqueue(disposer);
}
}
......@@ -59,8 +97,24 @@ namespace Model
ObjectHelper.Swap(ref this.loads, ref this.loads2);
}
private void Start()
{
while (this.starts.Count > 0)
{
Disposer disposer = this.starts.Dequeue();
if (disposer.Id == 0)
{
continue;
}
((IStart)disposer).Start();
}
}
public void Update()
{
this.Start();
while (this.updates.Count > 0)
{
Disposer disposer = this.updates.Dequeue();
......
fileFormatVersion: 2
guid: e64b29d0fee5409889d0f320e76d9920
timeCreated: 1504163155
\ No newline at end of file
using UnityEngine;
namespace Hotfix
namespace Model
{
public static class LayerNames
{
......
fileFormatVersion: 2
guid: 0313ab7e20994a3eabf5ac4af2649ae9
timeCreated: 1504163170
\ No newline at end of file
using System;
namespace Hotfix
namespace Model
{
[AttributeUsage(AttributeTargets.Class)]
public class UIFactoryAttribute: Attribute
......
fileFormatVersion: 2
guid: 7cf320727b5c499d8c1f646cf54691d6
timeCreated: 1504163170
\ No newline at end of file
namespace Model
{
public enum UIType
{
Root = 0,
Login,
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 2541f74a1ec0436b8e812a69258e473e
timeCreated: 1504163170
\ No newline at end of file
......@@ -5,7 +5,7 @@ using UnityEngine;
namespace Model
{
[EntityEvent(EntityEventId.BehaviorTreeComponent)]
[EntityEvent((int)EntityEventId.BehaviorTreeComponent)]
public class BehaviorTreeComponent : Component
{
private Dictionary<string, Func<NodeProto, Node>> dictionary;
......
......@@ -6,7 +6,7 @@ namespace Model
/// <summary>
/// 事件分发,可以将事件分发到IL层
/// </summary>
[EntityEvent(EntityEventId.CrossComponent)]
[EntityEvent((int)EntityEventId.CrossComponent)]
public class CrossComponent : Component, IAwake
{
private static object[] args0 = new object[0];
......
......@@ -3,19 +3,22 @@ using System.Collections.Generic;
namespace Model
{
[ObjectEvent(EntityEventId.EventComponent)]
public class EventComponent : Component, IAwake
[ObjectEvent((int)EntityEventId.EventComponent)]
public class EventComponent : Component, IAwake, ILoad
{
private Dictionary<int, List<object>> allEvents;
public static EventComponent Instance;
private Dictionary<EventIdType, List<object>> allEvents;
public void Awake()
{
Instance = this;
this.Load();
}
public void Load()
{
this.allEvents = new Dictionary<int, List<object>>();
this.allEvents = new Dictionary<EventIdType, List<object>>();
Type[] types = DllHelper.GetMonoTypes();
foreach (Type type in types)
......@@ -26,16 +29,16 @@ namespace Model
{
EventAttribute aEventAttribute = (EventAttribute)attr;
object obj = Activator.CreateInstance(type);
if (!this.allEvents.ContainsKey(aEventAttribute.Type))
if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
{
this.allEvents.Add(aEventAttribute.Type, new List<object>());
this.allEvents.Add((EventIdType)aEventAttribute.Type, new List<object>());
}
this.allEvents[aEventAttribute.Type].Add(obj);
this.allEvents[(EventIdType)aEventAttribute.Type].Add(obj);
}
}
}
public void Run(int type)
public void Run(EventIdType type)
{
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
......@@ -56,7 +59,7 @@ namespace Model
}
}
public void Run<A>(int type, A a)
public void Run<A>(EventIdType type, A a)
{
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
......@@ -78,7 +81,7 @@ namespace Model
}
}
public void Run<A, B>(int type, A a, B b)
public void Run<A, B>(EventIdType type, A a, B b)
{
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
......@@ -100,7 +103,7 @@ namespace Model
}
}
public void Run<A, B, C>(int type, A a, B b, C c)
public void Run<A, B, C>(EventIdType type, A a, B b, C c)
{
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
......
......@@ -6,7 +6,7 @@ namespace Model
/// <summary>
/// 消息分发组件
/// </summary>
[ObjectEvent(EntityEventId.MessageDispatherComponent)]
[ObjectEvent((int)EntityEventId.MessageDispatherComponent)]
public class MessageDispatherComponent : Component, IAwake, ILoad
{
private Dictionary<ushort, List<IMHandler>> handlers;
......
......@@ -5,7 +5,7 @@ using UnityEngine;
namespace Model
{
[ObjectEvent(EntityEventId.ResourcesComponent)]
[ObjectEvent((int)EntityEventId.ResourcesComponent)]
public class ResourcesComponent: Component
{
public static AssetBundleManifest AssetBundleManifestObject { get; set; }
......
......@@ -12,7 +12,7 @@ namespace Model
public TaskCompletionSource<bool> tcs;
}
[EntityEvent(EntityEventId.TimerComponent)]
[EntityEvent((int)EntityEventId.TimerComponent)]
public class TimerComponent: Component, IUpdate
{
private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();
......
fileFormatVersion: 2
guid: c6e0e4d83fad46c3b1306e0c8fd35033
timeCreated: 1504162083
\ No newline at end of file
using UnityEngine;
namespace Model
{
public class CanvasConfig: MonoBehaviour
{
public string CanvasName;
}
}
fileFormatVersion: 2
guid: 8347f75bbf20473292de676504ad7398
timeCreated: 1504162087
\ No newline at end of file
......@@ -5,7 +5,7 @@ using UnityEngine;
namespace Model
{
[EntityEvent(EntityEventId.WWWAsync)]
[EntityEvent((int)EntityEventId.WWWAsync)]
public class WWWAsync: Entity
{
public WWW www;
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using ILRuntime.CLR.Method;
......@@ -10,41 +11,66 @@ namespace Model
{
public static class DllHelper
{
#if ILRuntime
public static void LoadHotfixAssembly()
{
GameObject code = (GameObject)Resources.Load("Code");
byte[] assBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.dll").bytes;
byte[] mdbBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.pdb").bytes;
using (MemoryStream fs = new MemoryStream(assBytes))
using (MemoryStream p = new MemoryStream(mdbBytes))
{
Init.Instance.AppDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
}
}
#else
public static Assembly LoadHotfixAssembly()
{
GameObject code = (GameObject)Resources.Load("Code");
byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
byte[] mdbBytes = code.Get<TextAsset>("Hotfix.dll.mdb").bytes;
byte[] mdbBytes = code.Get<TextAsset>("Hotfix.pdb").bytes;
Assembly assembly = Assembly.Load(assBytes, mdbBytes);
return assembly;
}
#endif
private static List<Type> _typeBuffer = new List<Type>();
public static Type[] GetHotfixTypes()
{
#if ILRuntime
ILRuntime.Runtime.Enviorment.AppDomain appDomain = Init.Instance.AppDomain;
if (appDomain == null)
{
return new Type[0];
}
List<Type> types = new List<Type>();
foreach (IType type in appDomain.LoadedTypes.Values.ToArray())
{
types.Add(type.ReflectionType);
if (!_typeBuffer.Contains(type.ReflectionType))
{
_typeBuffer.Add(type.ReflectionType);
}
}
return types.ToArray();
return _typeBuffer.ToArray();
#else
return ObjectEvents.Instance.Get("Hotfix").GetTypes();
#endif
}
public static Type[] GetMonoTypes()
{
List<Type> types = new List<Type>();
foreach (Assembly assembly in AssemblyManager.Instance.GetAll())
foreach (Assembly assembly in ObjectEvents.Instance.GetAll())
{
types.AddRange(assembly.GetTypes());
}
return types.ToArray();
}
#if ILRuntime
public static IMethod[] GetMethodInfo(string typeName)
{
ILRuntime.Runtime.Enviorment.AppDomain appDomain = Init.Instance.AppDomain;
......@@ -60,5 +86,6 @@ namespace Model
{
return Init.Instance.AppDomain.GetType(typeName);
}
#endif
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using ILRuntime.CLR.Method;
using ILRuntime.CLR.TypeSystem;
using ILRuntime.Runtime.Enviorment;
using UnityEngine;
using AppDomain = System.AppDomain;
namespace Model
{
public static class ILHelper
{
public static unsafe void RegisterRedirection()
{
MethodInfo mi = typeof(Log).GetMethod("Debug", new Type[] { typeof(string) });
Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
MethodInfo mi2 = typeof(Log).GetMethod("Info", new Type[] { typeof(string) });
Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi2, ILRedirection.LogInfo);
MethodInfo mi3 = typeof(Log).GetMethod("Error", new Type[] { typeof(string) });
Init.Instance.AppDomain.RegisterCLRMethodRedirection(mi3, ILRedirection.LogError);
}
public static unsafe void RegisterDelegate()
{
Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
Init.Instance.AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
}
public static unsafe void RegisterILAdapter()
{
Assembly assembly = typeof(Init).Assembly;
foreach (Type type in assembly.GetTypes())
{
object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
if (attrs.Length == 0)
{
continue;
}
object obj = Activator.CreateInstance(type);
CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
if (adaptor == null)
{
continue;
}
Init.Instance.AppDomain.RegisterCrossBindingAdaptor(adaptor);
}
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: db76e26ce70b40d1823db1466a26fc83
timeCreated: 1504164125
\ No newline at end of file
namespace Model
{
public abstract class IInstanceMethod
{
public string Name { get; protected set; }
public abstract void Run(params object[] param);
}
public abstract class IStaticMethod
{
public string Name { get; protected set; }
public abstract void Run(object instance, params object[] param);
}
}
using System;
using System.IO;
using System.Reflection;
using Model;
using System.Threading;
using ILRuntime.CLR.Method;
using ILRuntime.CLR.TypeSystem;
using ILRuntime.Runtime.Enviorment;
......@@ -9,40 +9,61 @@ using UnityEngine;
namespace Model
{
public class Init: MonoBehaviour
public class Init : MonoBehaviour
{
public static Init Instance;
public ILRuntime.Runtime.Enviorment.AppDomain AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
public ILRuntime.Runtime.Enviorment.AppDomain AppDomain;
private readonly object[] param0 = new object[0];
private IMethod start;
private IMethod update;
private IMethod onApplicationQuit;
private IStaticMethod start;
private IStaticMethod update;
private IStaticMethod lateUpdate;
private IStaticMethod onApplicationQuit;
private void Start()
{
try
{
Instance = this;
AssemblyManager.Instance.Add("Model", typeof(Model.Init).Assembly);
if (Application.unityVersion != "2017.1.0f3")
{
Log.Error("请使用Unity2017正式版");
return;
}
this.RegisterAssembly();
this.RegisterILAdapter();
this.RegisterDelegate();
this.RegisterRedirection();
DontDestroyOnLoad(gameObject);
Instance = this;
IType hotfixInitType = AppDomain.LoadedTypes["Hotfix.Init"];
start = hotfixInitType.GetMethod("Start", 0);
update = hotfixInitType.GetMethod("Update", 0);
onApplicationQuit = hotfixInitType.GetMethod("OnApplicationQuit", 0);
ObjectEvents.Instance.Add("Model", typeof(Init).Assembly);
#if ILRuntime
Log.Debug("run in ilruntime mode");
this.AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
DllHelper.LoadHotfixAssembly();
ILHelper.RegisterILAdapter();
ILHelper.RegisterDelegate();
ILHelper.RegisterRedirection();
this.start = new ILStaticMethod("Hotfix.Init", "Start", 0);
this.update = new ILStaticMethod("Hotfix.Init", "Update", 0);
this.lateUpdate = new ILStaticMethod("Hotfix.Init", "LateUpdate", 0);
this.onApplicationQuit = new ILStaticMethod("Hotfix.Init", "OnApplicationQuit", 0);
#else
Log.Debug("run in mono mode");
ObjectEvents.Instance.Add("Model", typeof(Init).Assembly);
Assembly hotfix = DllHelper.LoadHotfixAssembly();
ObjectEvents.Instance.Add("Hotfix", hotfix);
Type hotfixInit = hotfix.GetType("Hotfix.Init");
this.start = new MonoStaticMethod(hotfixInit, "Start");
this.update = new MonoStaticMethod(hotfixInit, "Update");
this.lateUpdate = new MonoStaticMethod(hotfixInit, "LateUpdate");
this.onApplicationQuit = new MonoStaticMethod(hotfixInit, "OnApplicationQuit");
#endif
// 进入热更新层
this.AppDomain.Invoke(this.start, null, param0);
this.start.Run();
}
catch (Exception e)
{
......@@ -52,61 +73,22 @@ namespace Model
private void Update()
{
this.AppDomain.Invoke(this.update, null, this.param0);
this.update.Run();
ObjectEvents.Instance.Update();
}
private void OnApplicationQuit()
{
this.AppDomain.Invoke(this.onApplicationQuit, null, this.param0);
}
public void RegisterAssembly()
private void LateUpdate()
{
GameObject code = (GameObject)Resources.Load("Code");
byte[] assBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.dll").bytes;
byte[] mdbBytes = code.GetComponent<ReferenceCollector>().Get<TextAsset>("Hotfix.pdb").bytes;
using (MemoryStream fs = new MemoryStream(assBytes))
using (MemoryStream p = new MemoryStream(mdbBytes))
{
AppDomain.LoadAssembly(fs, p, new Mono.Cecil.Pdb.PdbReaderProvider());
}
this.lateUpdate.Run();
ObjectEvents.Instance.LateUpdate();
}
public unsafe void RegisterRedirection()
{
MethodInfo mi = typeof(Log).GetMethod("Debug", new Type[] { typeof(string) });
this.AppDomain.RegisterCLRMethodRedirection(mi, ILRedirection.LogDebug);
}
public void RegisterDelegate()
{
AppDomain.DelegateManager.RegisterMethodDelegate<AChannel, System.Net.Sockets.SocketError>();
AppDomain.DelegateManager.RegisterMethodDelegate<byte[], int, int>();
}
public void RegisterILAdapter()
private void OnApplicationQuit()
{
Assembly assembly = typeof(Init).Assembly;
foreach (Type type in assembly.GetTypes())
{
object[] attrs = type.GetCustomAttributes(typeof(ILAdapterAttribute), false);
if (attrs.Length == 0)
{
continue;
}
object obj = Activator.CreateInstance(type);
CrossBindingAdaptor adaptor = obj as CrossBindingAdaptor;
if (adaptor == null)
{
continue;
}
AppDomain.RegisterCrossBindingAdaptor(adaptor);
}
Instance = null;
Game.Close();
ObjectEvents.Close();
this.onApplicationQuit.Run();
}
}
}
\ No newline at end of file
using System;
using System.Reflection;
using Model;
namespace Model
{
public class MonoInstanceMethod : IInstanceMethod
{
private readonly object instance;
private readonly MethodInfo methodInfo;
public MonoInstanceMethod(Type type, string methodName)
{
this.Name = methodName;
this.instance = Activator.CreateInstance(type);
this.methodInfo = type.GetMethod(methodName);
}
public override void Run(params object[] param)
{
this.methodInfo.Invoke(this.instance, param);
}
}
public class MonoStaticMethod : IStaticMethod
{
private readonly MethodInfo methodInfo;
public MonoStaticMethod(MethodInfo methodInfo)
{
this.methodInfo = methodInfo;
this.Name = methodInfo.Name;
}
public override void Run(object instance, params object[] param)
{
this.methodInfo.Run(instance, param);
}
}
}
fileFormatVersion: 2
guid: a7ff51f926ae4716829bec2b134d8d89
timeCreated: 1504163325
\ No newline at end of file
namespace Model
{
public abstract class IInstanceMethod
{
public abstract void Run();
public abstract void Run(object a);
public abstract void Run(object a, object b);
public abstract void Run(object a, object b, object c);
}
public abstract class IStaticMethod
{
public abstract void Run();
public abstract void Run(object a);
public abstract void Run(object a, object b);
public abstract void Run(object a, object b, object c);
}
}
......@@ -9,17 +9,40 @@ namespace Model
private readonly ILRuntime.Runtime.Enviorment.AppDomain appDomain;
private readonly ILTypeInstance instance;
private readonly IMethod method;
private readonly object[] param;
public ILInstanceMethod(Type type, string methodName)
{
this.Name = methodName;
appDomain = Init.Instance.AppDomain;
this.instance = this.appDomain.Instantiate(type.FullName);
this.method = this.instance.Type.GetMethod(methodName);
int n = this.method.ParameterCount;
this.param = new object[n];
}
public override void Run()
{
this.appDomain.Invoke(this.method, this.instance, param);
}
public override void Run(object a)
{
this.param[0] = a;
this.appDomain.Invoke(this.method, this.instance, param);
}
public override void Run(object a, object b)
{
this.param[0] = a;
this.param[1] = b;
this.appDomain.Invoke(this.method, this.instance, param);
}
public override void Run(params object[] param)
public override void Run(object a, object b, object c)
{
this.param[0] = a;
this.param[1] = b;
this.param[2] = c;
this.appDomain.Invoke(this.method, this.instance, param);
}
}
......@@ -30,27 +53,37 @@ namespace Model
private readonly IMethod method;
private readonly object[] param;
public ILStaticMethod(IMethod method, int paramsCount)
public ILStaticMethod(string typeName, string methodName, int paramsCount)
{
this.param = new object[paramsCount + 1];
this.Name = method.Name;
appDomain = Init.Instance.AppDomain;
this.method = method;
}
public override void Run(object instance, params object[] p)
{
if (this.method.IsStatic)
{
this.param[0] = instance;
for (int i = 0; i < p.Length; ++i)
{
this.param[1 + i] = p[i];
}
this.appDomain.Invoke(this.method, null, this.param);
return;
}
this.appDomain.Invoke(this.method, instance, p);
this.method = appDomain.GetType(typeName).GetMethod(methodName, paramsCount);
this.param = new object[paramsCount];
}
public override void Run()
{
this.appDomain.Invoke(this.method, null, this.param);
}
public override void Run(object a)
{
this.param[0] = a;
this.appDomain.Invoke(this.method, null, param);
}
public override void Run(object a, object b)
{
this.param[0] = a;
this.param[1] = b;
this.appDomain.Invoke(this.method, null, param);
}
public override void Run(object a, object b, object c)
{
this.param[0] = a;
this.param[1] = b;
this.param[2] = c;
this.appDomain.Invoke(this.method, null, param);
}
}
}
using System;
using System.Reflection;
namespace Model
{
public class MonoInstanceMethod : IInstanceMethod
{
private readonly object obj;
private readonly MethodInfo methodInfo;
private readonly object[] param;
public MonoInstanceMethod(Type type, string methodName)
{
this.obj = Activator.CreateInstance(type);
this.methodInfo = type.GetMethod(methodName);
this.param = new object[this.methodInfo.GetParameters().Length];
}
public override void Run()
{
this.methodInfo.Invoke(this.obj, param);
}
public override void Run(object a)
{
this.methodInfo.Invoke(this.obj, param);
}
public override void Run(object a, object b)
{
this.param[0] = a;
this.param[1] = b;
this.methodInfo.Invoke(this.obj, param);
}
public override void Run(object a, object b, object c)
{
this.param[0] = a;
this.param[1] = b;
this.param[2] = c;
this.methodInfo.Invoke(this.obj, param);
}
}
public class MonoStaticMethod : IStaticMethod
{
private readonly MethodInfo methodInfo;
private readonly object[] param;
public MonoStaticMethod(Type type, string methodName)
{
this.methodInfo = type.GetMethod(methodName);
this.param = new object[this.methodInfo.GetParameters().Length];
}
public override void Run()
{
this.methodInfo.Invoke(null, param);
}
public override void Run(object a)
{
this.param[0] = a;
this.methodInfo.Invoke(null, param);
}
public override void Run(object a, object b)
{
this.param[0] = a;
this.param[1] = b;
this.methodInfo.Invoke(null, param);
}
public override void Run(object a, object b, object c)
{
this.param[0] = a;
this.param[1] = b;
this.param[2] = c;
this.methodInfo.Invoke(null, param);
}
}
}
namespace Hotfix
{
public static class AssetBundleHelper
{
public static string GetBundleNameById(string id, EntityType entityType = EntityType.None)
{
string subString = id.Substring(0, 3);
switch (subString[0])
{
case '1':
case '2':
case '3':
return "3000";
case '4':
case '5':
case '6':
case '8':
return subString;
case '9':
return "900";
}
return subString;
}
}
}
\ No newline at end of file
using System;
namespace Hotfix
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public abstract class AEventAttribute: Attribute
{
public int Type { get; private set; }
protected AEventAttribute(int type)
{
this.Type = type;
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 288485f014836824b89d9a901bc5dcca
timeCreated: 1477377275
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections;
using System.Collections.Generic;
namespace Hotfix
{
public class Env
{
private Dictionary<EnvKey, object> values = new Dictionary<EnvKey, object>();
public object this[EnvKey key]
{
get
{
return this.values[key];
}
set
{
if (this.values == null)
{
this.values = new Dictionary<EnvKey, object>();
}
this.values[key] = value;
}
}
public T Get<T>(EnvKey key)
{
if (this.values == null || !this.values.ContainsKey(key))
{
return default(T);
}
object value = values[key];
try
{
return (T) value;
}
catch (InvalidCastException e)
{
throw new Exception($"不能把{value.GetType()}转换为{typeof (T)}", e);
}
}
public void Set(EnvKey key, object obj)
{
if (this.values == null)
{
this.values = new Dictionary<EnvKey, object>();
}
this.values[key] = obj;
}
public bool ContainKey(EnvKey key)
{
if (this.values == null)
{
return false;
}
return this.values.ContainsKey(key);
}
public void Remove(EnvKey key)
{
if (this.values == null)
{
return;
}
this.values.Remove(key);
if (this.values.Count == 0)
{
this.values = null;
}
}
public void Add(EnvKey key, object value)
{
if (this.values == null)
{
this.values = new Dictionary<EnvKey, object>();
}
this.values[key] = value;
}
public IEnumerator GetEnumerator()
{
return this.values.GetEnumerator();
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 414e43d5bdd3d75458b86b1cf07ad175
timeCreated: 1477377275
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
namespace Hotfix
{
/// <summary>
/// 一般使用事件名+变量名
/// </summary>
public enum EnvKey
{
ChannelError
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: c6db3274f6a56f44eaaa14e0f2eecf54
timeCreated: 1476427109
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
namespace Hotfix
{
public class EventAttribute: AEventAttribute
{
public EventAttribute(int type): base(type)
{
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 46c83a3a046e502409ed650e1f3bc238
timeCreated: 1477377275
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
namespace Hotfix
{
public static class EventIdType
{
public const int InitSceneStart = 1;
public const int BehaviorTreeRunTreeEvent = 2;
public const int BehaviorTreeOpenEditor = 3;
public const int BehaviorTreeClickNode = 4;
public const int BehaviorTreeAfterChangeNodeType = 5;
public const int BehaviorTreeCreateNode = 6;
public const int BehaviorTreePropertyDesignerNewCreateClick = 7;
public const int BehaviorTreeMouseInNode = 8;
public const int BehaviorTreeConnectState = 9;
public const int BehaviorTreeReplaceClick = 10;
public const int BehaviorTreeRightDesignerDrag = 11;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 50aff527fd963e44fbce24e2ef3c1287
timeCreated: 1476421726
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: dab16cc3acc616f4880a9cd714ba9be0
timeCreated: 1477377275
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -3,7 +3,7 @@ using Model;
namespace Hotfix
{
public abstract class AMHandler<Message> : IMHandler where Message : AMessage
public abstract class AMHandler<Message> : IMHandler where Message: AMessage
{
protected abstract void Run(Message message);
......
namespace Hotfix
{
public abstract class AMessage
{
}
public abstract class ARequest: AMessage
{
public uint RpcId;
}
/// <summary>
/// 服务端回的RPC消息需要继承这个抽象类
/// </summary>
public abstract class AResponse
{
public uint RpcId;
public int Error = 0;
public string Message = "";
}
public abstract class AFrameMessage : AMessage
{
public long Id;
}
}
\ No newline at end of file
......@@ -137,7 +137,8 @@ namespace Hotfix
public void RemoveComponent<K>() where K : Component
{
if (!this.componentDict.TryGetValue(typeof(K), out Component component))
Component component;
if (!this.componentDict.TryGetValue(typeof(K), out component))
{
return;
}
......@@ -153,7 +154,8 @@ namespace Hotfix
public void RemoveComponent(Type type)
{
if (!this.componentDict.TryGetValue(type, out Component component))
Component component;
if (!this.componentDict.TryGetValue(type, out component))
{
return;
}
......@@ -169,7 +171,8 @@ namespace Hotfix
public K GetComponent<K>() where K : Component
{
if (!this.componentDict.TryGetValue(typeof(K), out Component component))
Component component;
if (!this.componentDict.TryGetValue(typeof(K), out component))
{
return default(K);
}
......
using System;
namespace Hotfix
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ObjectEventAttribute: Attribute
{
public int ClassType;
public ObjectEventAttribute(int classType)
{
this.ClassType = classType;
}
}
}
\ No newline at end of file
......@@ -15,6 +15,9 @@ namespace Hotfix
private Queue<Disposer> lateUpdates = new Queue<Disposer>();
private Queue<Disposer> lateUpdates2 = new Queue<Disposer>();
private Queue<Disposer> frameUpdates = new Queue<Disposer>();
private Queue<Disposer> frameUpdates2 = new Queue<Disposer>();
public static ObjectEvents Instance
{
get
......
namespace Hotfix
{
public static class EntityEventId
{
public const int TimerComponent = 1;
public const int ClientConfigComponent = 2;
public const int EventComponent = 3;
public const int GameObjectComponent = 4;
public const int ILEventComponent = 5;
public const int MessageDispatherComponent = 6;
public const int NetInnerComponent = 7;
public const int NetOuterComponent = 8;
public const int RobotComponent = 9;
public const int UIComponent = 10;
public const int WWWAsync = 11;
public const int ILRuntimeComponent = 12;
public const int ResourcesComponent = 13;
public const int UnitComponent = 14;
public const int AppManagerComponent = 15;
public const int GateSessionKeyComponent = 16;
public const int OptionComponent = 17;
public const int StartConfigComponent = 18;
public const int LockComponent = 19;
public const int MasterComponent = 20;
public const int RealmGateAddressComponent = 21;
public const int UILobbyComponent = 22;
public const int BehaviorTreeComponent = 23;
}
}
namespace Hotfix
using Model;
using UnityEngine;
namespace Hotfix
{
public interface IUIFactory
{
UI Create(Scene scene, int type, UI parent);
UI Create(Scene scene, UIType type, GameObject parent);
}
}
\ No newline at end of file
namespace Hotfix
{
public static class UIType
{
public const int Root = 1;
public const int Lobby = 2;
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using Model;
namespace Hotfix
{
public class ConfigComponent: Component
{
private Dictionary<Type, ICategory> allConfig;
private void Load()
{
this.allConfig = new Dictionary<Type, ICategory>();
Type[] types = DllHelper.GetHotfixTypes();
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof (ConfigAttribute), false);
if (attrs.Length == 0)
{
continue;
}
object obj = Activator.CreateInstance(type);
ICategory iCategory = obj as ICategory;
if (iCategory == null)
{
throw new Exception($"class: {type.Name} not inherit from ACategory");
}
iCategory.BeginInit();
iCategory.EndInit();
this.allConfig[iCategory.ConfigType] = iCategory;
}
}
public T GetOne<T>() where T : AConfig
{
Type type = typeof (T);
if (!this.allConfig.TryGetValue(type, out ICategory configCategory))
{
throw new Exception($"ConfigComponent not found key: {type.FullName}");
}
return ((ACategory<T>) configCategory).GetOne();
}
public T Get<T>(long id) where T : AConfig
{
Type type = typeof (T);
if (!this.allConfig.TryGetValue(type, out ICategory configCategory))
{
throw new Exception($"ConfigComponent not found key: {type.FullName}");
}
return ((ACategory<T>) configCategory)[id];
}
public T TryGet<T>(int id) where T : AConfig
{
Type type = typeof (T);
if (!this.allConfig.TryGetValue(type, out ICategory configCategory))
{
return default(T);
}
return ((ACategory<T>) configCategory).TryGet(id);
}
public T[] GetAll<T>() where T : AConfig
{
Type type = typeof (T);
if (!this.allConfig.TryGetValue(type, out ICategory configCategory))
{
throw new Exception($"ConfigComponent not found key: {type.FullName}");
}
return ((ACategory<T>) configCategory).GetAll();
}
public T GetCategory<T>() where T : class, ICategory, new()
{
T t = new T();
Type type = t.ConfigType;
bool ret = this.allConfig.TryGetValue(type, out ICategory category);
return ret? (T) category : null;
}
}
}
\ No newline at end of file
......@@ -4,10 +4,10 @@ using Model;
namespace Hotfix
{
[ObjectEvent(EntityEventId.EventComponent)]
[ObjectEvent((int)EntityEventId.EventComponent)]
public class EventComponent : Component, IAwake
{
private Dictionary<int, List<object>> allEvents;
private Dictionary<EventIdType, List<object>> allEvents;
public void Awake()
{
......@@ -16,29 +16,29 @@ namespace Hotfix
public void Load()
{
this.allEvents = new Dictionary<int, List<object>>();
this.allEvents = new Dictionary<EventIdType, List<object>>();
Type[] types = DllHelper.GetHotfixTypes();
foreach (Type type in types)
{
object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
foreach (object attr in attrs)
{
EventAttribute aEventAttribute = (EventAttribute)attr;
{
EventAttribute aEventAttribute = (EventAttribute)attr;
object obj = Activator.CreateInstance(type);
if (!this.allEvents.ContainsKey(aEventAttribute.Type))
if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
{
this.allEvents.Add(aEventAttribute.Type, new List<object>());
this.allEvents.Add((EventIdType)aEventAttribute.Type, new List<object>());
}
this.allEvents[aEventAttribute.Type].Add(obj);
this.allEvents[(EventIdType)aEventAttribute.Type].Add(obj);
}
}
}
public void Run(int type)
public void Run(EventIdType type)
{
if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
{
return;
}
......@@ -51,14 +51,15 @@ namespace Hotfix
}
catch (Exception e)
{
Log.Error(e.ToString());
Log.Error(e.ToStr());
}
}
}
public void Run<A>(int type, A a)
public void Run<A>(EventIdType type, A a)
{
if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
{
return;
}
......@@ -77,9 +78,10 @@ namespace Hotfix
}
}
public void Run<A, B>(int type, A a, B b)
public void Run<A, B>(EventIdType type, A a, B b)
{
if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
{
return;
}
......@@ -98,9 +100,10 @@ namespace Hotfix
}
}
public void Run<A, B, C>(int type, A a, B b, C c)
public void Run<A, B, C>(EventIdType type, A a, B b, C c)
{
if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
List<object> iEvents;
if (!this.allEvents.TryGetValue(type, out iEvents))
{
return;
}
......
using UnityEngine;
using Model;
using UnityEngine;
namespace Hotfix
{
[ObjectEvent(EntityEventId.GameObjectComponent)]
[ObjectEvent((int)EntityEventId.GameObjectComponent)]
public class GameObjectComponent: Component
{
public GameObject GameObject { get; private set; }
......
......@@ -21,7 +21,8 @@ namespace Hotfix
public T Get<T>(string key)
{
if (!this.kv.TryGetValue(key, out object k))
object k;
if (!this.kv.TryGetValue(key, out k))
{
return default(T);
}
......
......@@ -8,10 +8,10 @@ namespace Hotfix
/// 消息分发组件
/// </summary>
[ObjectEvent((int)EntityEventId.MessageDispatherComponent)]
public class MessageDispatherComponent : Component, IAwake, ILoad
public class MessageDispatherComponent: Component, IAwake, ILoad
{
private Dictionary<ushort, List<IMHandler>> handlers;
public void Awake()
{
......@@ -20,9 +20,9 @@ namespace Hotfix
public void Load()
{
handlers = new Dictionary<ushort, List<IMHandler>>();
Type[] types = DllHelper.GetHotfixTypes();
handlers = new Dictionary<ushort, List<IMHandler>>();
Type[] types = DllHelper.GetHotfixTypes();
foreach (Type type in types)
{
......@@ -43,7 +43,8 @@ namespace Hotfix
public void Handle(MessageInfo messageInfo)
{
if (!this.handlers.TryGetValue(messageInfo.Opcode, out List<IMHandler> actions))
List<IMHandler> actions;
if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
{
Log.Error($"消息 {messageInfo.Opcode} 没有处理");
return;
......
namespace Hotfix
using Model;
namespace Hotfix
{
[ObjectEvent(EntityEventId.RobotComponent)]
[ObjectEvent((int)EntityEventId.RobotComponent)]
public class RobotComponent: Component
{
public override void Dispose()
......
......@@ -5,14 +5,14 @@ using Model;
namespace Hotfix
{
public class Timer
public struct Timer
{
public long Id { get; set; }
public long Time { get; set; }
public TaskCompletionSource<bool> tcs;
}
[ObjectEvent(EntityEventId.TimerComponent)]
[ObjectEvent((int)EntityEventId.TimerComponent)]
public class TimerComponent: Component, IUpdate
{
private readonly Dictionary<long, Timer> timers = new Dictionary<long, Timer>();
......@@ -42,7 +42,8 @@ namespace Hotfix
long[] timeOutId = this.timeId.GetAll(key);
foreach (long id in timeOutId)
{
if (!this.timers.TryGetValue(id, out Timer timer))
Timer timer;
if (!this.timers.TryGetValue(id, out timer))
{
continue;
}
......@@ -54,7 +55,8 @@ namespace Hotfix
private void Remove(long id)
{
if (!this.timers.TryGetValue(id, out Timer timer))
Timer timer;
if (!this.timers.TryGetValue(id, out timer))
{
return;
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ILRuntime.CLR.Method;
using Model;
using UnityEngine;
namespace Hotfix
{
#if ILRuntime
public class IILUIFactoryMethod : IUIFactory
{
private readonly object instance;
......@@ -18,37 +20,59 @@ namespace Hotfix
this.methodInfo = DllHelper.GetType(type.FullName).GetMethod("Create", 3);
}
public UI Create(Scene scene, int type, UI parent)
public UI Create(Scene scene, UIType type, GameObject parent)
{
this.params3[0] = scene;
this.params3[1] = type;
this.params3[2] = parent;
return (UI)Model.Init.Instance.AppDomain.Invoke(this.methodInfo, this.instance, this.params3);
return (UI)Model.Init.Instance.AppDomain.Invoke(methodInfo, instance, params3);
}
}
#else
public class IMonoUIFactoryMethod : IUIFactory
{
private readonly object instance;
private readonly MethodInfo methodInfo;
private readonly object[] params3 = new object[3];
public IMonoUIFactoryMethod(Type type)
{
this.instance = Activator.CreateInstance(type);
this.methodInfo = type.GetMethod("Create");
}
public UI Create(Scene scene, UIType type, GameObject parent)
{
this.params3[0] = scene;
this.params3[1] = type;
this.params3[2] = parent;
return (UI)this.methodInfo.Invoke(this.instance, this.params3);
}
}
#endif
/// <summary>
/// 管理所有UI
/// </summary>
[ObjectEvent(EntityEventId.UIComponent)]
[ObjectEvent((int)EntityEventId.UIComponent)]
public class UIComponent: Component, IAwake, ILoad
{
private UI Root;
private Dictionary<int, IUIFactory> UiTypes;
private readonly Dictionary<int, UI> uis = new Dictionary<int, UI>();
private GameObject Root;
private Dictionary<UIType, IUIFactory> UiTypes;
private readonly Dictionary<UIType, UI> uis = new Dictionary<UIType, UI>();
public override void Dispose()
{
if (this.Id == 0)
if (Id == 0)
{
return;
}
base.Dispose();
foreach (int type in uis.Keys.ToArray())
foreach (UIType type in uis.Keys.ToArray())
{
if (!uis.TryGetValue(type, out UI ui))
UI ui;
if (!uis.TryGetValue(type, out ui))
{
continue;
}
......@@ -59,16 +83,15 @@ namespace Hotfix
public void Awake()
{
GameObject uiCanvas = GameObject.Find("Global/UI/UICanvas");
this.Root = new UI(this.GetOwner<Scene>(), UIType.Root, null, uiCanvas);
this.Root = GameObject.Find("Global/UI/");
this.Load();
}
public void Load()
{
this.UiTypes = new Dictionary<int, IUIFactory>();
Type[] types = DllHelper.GetHotfixTypes();
UiTypes = new Dictionary<UIType, IUIFactory>();
Type[] types = DllHelper.GetHotfixTypes();
foreach (Type type in types)
{
......@@ -79,69 +102,79 @@ namespace Hotfix
}
UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;
if (this.UiTypes.ContainsKey(attribute.Type))
if (UiTypes.ContainsKey((UIType)attribute.Type))
{
Log.Debug($"已经存在同类UI Factory: {attribute.Type}");
throw new Exception($"已经存在同类UI Factory: {attribute.Type}");
}
#if ILRuntime
IUIFactory iuiFactory = new IILUIFactoryMethod(type);
#else
IUIFactory iuiFactory = new IMonoUIFactoryMethod(type);
#endif
this.UiTypes.Add(attribute.Type, iuiFactory);
this.UiTypes.Add((UIType)attribute.Type, iuiFactory);
}
}
public UI Create(int type)
public UI Create(UIType type)
{
try
{
UI ui = this.UiTypes[type].Create(this.GetOwner<Scene>(), type, this.Root);
this.uis.Add(type, ui);
UI ui = UiTypes[type].Create(GetOwner<Scene>(), type, Root);
uis.Add(type, ui);
// 设置canvas
string cavasName = ui.GameObject.GetComponent<CanvasConfig>().CanvasName;
ui.GameObject.transform.SetParent(this.Root.Get<GameObject>(cavasName).transform, false);
return ui;
}
catch (Exception e)
{
throw new Exception($"{type} UI 错误: {e}");
throw new Exception($"{type} UI 错误: {e.ToStr()}");
}
}
public void Add(int type, UI ui)
public void Add(UIType type, UI ui)
{
this.uis.Add(type, ui);
}
public void Remove(int type)
public void Remove(UIType type)
{
if (!this.uis.TryGetValue(type, out UI ui))
UI ui;
if (!uis.TryGetValue(type, out ui))
{
return;
}
this.uis.Remove(type);
uis.Remove(type);
ui.Dispose();
}
public void RemoveAll()
{
foreach (int type in this.uis.Keys.ToArray())
foreach (UIType type in this.uis.Keys.ToArray())
{
if (!this.uis.TryGetValue(type, out UI ui))
UI ui;
if (!this.uis.TryGetValue(type, out ui))
{
continue;
}
this.uis.Remove(type);
}
this.uis.Remove(type);
ui.Dispose();
}
}
public UI Get(int type)
public UI Get(UIType type)
{
this.uis.TryGetValue(type, out UI ui);
UI ui;
this.uis.TryGetValue(type, out ui);
return ui;
}
public List<int> GetUITypeList()
public List<UIType> GetUITypeList()
{
return new List<int>(this.uis.Keys);
return new List<UIType>(this.uis.Keys);
}
}
}
\ No newline at end of file
using System.Collections.Generic;
namespace Hotfix
{
[ObjectEvent(EntityEventId.UnitComponent)]
public class UnitComponent: Component
{
private readonly Dictionary<long, Unit> idUnits = new Dictionary<long, Unit>();
public void Add(Unit unit)
{
this.idUnits.Add(unit.Id, unit);
}
public Unit Get(long id)
{
this.idUnits.TryGetValue(id, out Unit unit);
return unit;
}
public void Remove(long id)
{
this.idUnits.Remove(id);
}
public override void Dispose()
{
if (this.Id == 0)
{
return;
}
base.Dispose();
foreach (Unit unit in this.idUnits.Values)
{
unit.Dispose();
}
}
}
}
\ No newline at end of file
using System.Collections.Generic;
using Model;
using UnityEngine;
namespace Hotfix
......@@ -7,7 +8,7 @@ namespace Hotfix
{
public Scene Scene { get; set; }
public int UIType { get; }
public UIType UIType { get; }
public string Name
{
......@@ -36,7 +37,7 @@ namespace Hotfix
this.GameObject.transform.SetAsFirstSibling();
}
public UI(Scene scene, int uiType, UI parent, GameObject gameObject): base(EntityType.UI)
public UI(Scene scene, UIType uiType, UI parent, GameObject gameObject): base(EntityType.UI)
{
this.Scene = scene;
this.UIType = uiType;
......
using System;
using Base;
using Model;
namespace Hotfix
{
public static class HotfixEntry
{
public static void Start()
{
Login();
}
public static async void Login()
{
try
{
Game.Scene.AddComponent<MessageDispatherComponent, AppType>(AppType.Client);
ClientConfig clientConfig = Game.Scene.AddComponent<ClientConfigComponent>().Config.GetComponent<ClientConfig>();
NetOuterComponent networkComponent = Game.Scene.AddComponent<NetOuterComponent>();
using (Session session = networkComponent.Create(clientConfig.Address))
{
R2C_Login s2CLogin = await session.Call<C2R_Login, R2C_Login>(new C2R_Login { Account = "abcdef", Password = "111111" });
networkComponent.Remove(session.Id);
// 连接Gate
using (Session gateSession = networkComponent.Create(s2CLogin.Address))
{
await gateSession.Call<C2G_LoginGate, G2C_LoginGate>(new C2G_LoginGate(s2CLogin.Key));
}
Log.Info("连接Gate验证成功!");
}
}
catch (Exception e)
{
Log.Error(e.ToString());
}
}
}
}
......@@ -5,7 +5,7 @@ namespace Hotfix
{
public static class Init
{
private static void Start()
public static void Start()
{
try
{
......@@ -16,16 +16,15 @@ namespace Hotfix
Hotfix.Scene.ModelScene.AddComponent<ResourcesComponent>();
Hotfix.Scene.ModelScene.AddComponent<BehaviorTreeComponent>();
Hotfix.Scene.AddComponent<UIComponent>();
Hotfix.Scene.AddComponent<UnitComponent>();
Hotfix.Scene.GetComponent<EventComponent>().Run(EventIdType.InitSceneStart);
}
catch (Exception e)
{
Log.Error(e.ToString());
Log.Error(e.ToStr());
}
}
private static void Update()
public static void Update()
{
try
{
......@@ -37,7 +36,7 @@ namespace Hotfix
}
}
private static void OnApplicationQuit()
public static void OnApplicationQuit()
{
Hotfix.Close();
}
......
using Model;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Controller
{
[UIFactory(UIType.UILogin)]
public class UILoginFactory : IUIFactory
{
public UI Create(Scene scene, UIType type, UI parent)
{
GameObject mainPrefab = Resources.Load<GameObject>("UI/LoginPanel");
mainPrefab = Object.Instantiate(mainPrefab);
mainPrefab.layer = LayerMask.NameToLayer(LayerNames.UI);
UI ui = new UI(scene, type, parent, mainPrefab);
parent.AddChild(ui);
return ui;
}
}
}
\ No newline at end of file
......@@ -5,25 +5,29 @@ using UnityEngine.UI;
namespace Hotfix
{
[ObjectEvent(EntityEventId.UILobbyComponent)]
public class UILobbyComponent: Component, IAwake
[ObjectEvent((int)EntityEventId.UILoginComponent)]
public class UILoginComponent: Component, IAwake
{
private GameObject account;
private GameObject loginBtn;
public void Awake()
{
ReferenceCollector rc = this.GetOwner<UI>().GameObject.GetComponent<ReferenceCollector>();
GameObject createRoom = rc.Get<GameObject>("CreateRoom");
GameObject joinRoom = rc.Get<GameObject>("JoinRoom");
createRoom.GetComponent<Button>().onClick.Add(OnCreateRoom);
joinRoom.GetComponent<Button>().onClick.Add(OnJoinRoom);
loginBtn = rc.Get<GameObject>("LoginBtn");
loginBtn.GetComponent<Button>().onClick.Add(OnLogin);
this.account = rc.Get<GameObject>("Account");
}
private static async void OnCreateRoom()
private async void OnLogin()
{
Session session = null;
try
{
session = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create("127.0.0.1:10002");
R2C_Login r2CLogin = await session.Call<R2C_Login>(new C2R_Login() { Account = "abcdef", Password = "111111" });
string account = this.account.GetComponent<InputField>().text;
R2C_Login r2CLogin = await session.Call<R2C_Login>(new C2R_Login() { Account = account, Password = "111111" });
Session gateSession = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
G2C_LoginGate g2CLoginGate = await gateSession.Call<G2C_LoginGate>(new C2G_LoginGate(r2CLogin.Key));
......@@ -47,10 +51,5 @@ namespace Hotfix
session?.Dispose();
}
}
private static void OnJoinRoom()
{
}
}
}
......@@ -2,12 +2,12 @@
namespace Hotfix
{
[Event(EventIdType.InitSceneStart)]
public class InitSceneStart_CreateLobbyUI: IEvent
[Event((int)EventIdType.InitSceneStart)]
public class InitSceneStart_CreateLoginUI: IEvent
{
public void Run()
{
UI ui = Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.Lobby);
UI ui = Hotfix.Scene.GetComponent<UIComponent>().Create(UIType.Login);
}
}
}
......@@ -3,18 +3,17 @@ using UnityEngine;
namespace Hotfix
{
[UIFactory(UIType.Lobby)]
public class UILobbyFactory : IUIFactory
[UIFactory((int)UIType.Login)]
public class UILoginFactory : IUIFactory
{
public UI Create(Scene scene, int type, UI parent)
public UI Create(Scene scene, UIType type, GameObject gameObject)
{
GameObject bundleGameObject = scene.ModelScene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("uilobby", "Lobby");
GameObject bundleGameObject = scene.ModelScene.GetComponent<ResourcesComponent>().GetAsset<GameObject>("uilogin", "UILogin");
GameObject lobby = UnityEngine.Object.Instantiate(bundleGameObject);
lobby.layer = LayerMask.NameToLayer(LayerNames.UI);
UI ui = new UI(scene, type, parent, lobby);
parent.Add(ui);
UI ui = new UI(scene, type, null, lobby);
ui.AddComponent<UILobbyComponent>();
ui.AddComponent<UILoginComponent>();
return ui;
}
}
......
......@@ -55,54 +55,41 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Base\Event\IEvent.cs" />
<Compile Include="Base\Helper\ArrayHelper.cs" />
<Compile Include="Base\Helper\AssetBundleHelper.cs" />
<Compile Include="Base\Helper\ExceptionHelper.cs" />
<Compile Include="Base\Message\AMessage.cs" />
<Compile Include="Base\Message\AMHandler.cs" />
<Compile Include="Base\Message\IMHandler.cs" />
<Compile Include="Component\MessageDispatherComponent.cs" />
<Compile Include="Base\Helper\ArrayHelper.cs" />
<Compile Include="Event\SessionRecvMessage_Dispatch.cs" />
<Compile Include="Init.cs" />
<Compile Include="AssetBundleHelper.cs" />
<Compile Include="Component\ConfigComponent.cs" />
<Compile Include="Base\Object\Component.cs" />
<Compile Include="Base\Object\Disposer.cs" />
<Compile Include="Base\Object\Entity.cs" />
<Compile Include="Base\Object\EntityType.cs" />
<Compile Include="Base\Object\IAwake.cs" />
<Compile Include="Base\Object\ILateUpdate.cs" />
<Compile Include="Base\Object\ILoad.cs" />
<Compile Include="Base\Object\IUpdate.cs" />
<Compile Include="Base\Object\Object.cs" />
<Compile Include="Base\Object\ObjectEvents.cs" />
<Compile Include="Base\Other\IUIFactory.cs" />
<Compile Include="Component\EventComponent.cs" />
<Compile Include="Component\GameObjectComponent.cs" />
<Compile Include="Component\KVComponent.cs" />
<Compile Include="Component\MessageDispatherComponent.cs" />
<Compile Include="Component\RobotComponent.cs" />
<Compile Include="Component\TimeComponent.cs" />
<Compile Include="Component\TimerComponent.cs" />
<Compile Include="Component\UIComponent.cs" />
<Compile Include="Base\Object\IAwake.cs" />
<Compile Include="Base\Object\ILoad.cs" />
<Compile Include="Base\Object\ILateUpdate.cs" />
<Compile Include="Base\Object\IUpdate.cs" />
<Compile Include="UI\UILobby\Component\UILobbyComponent.cs" />
<Compile Include="Component\UnitComponent.cs" />
<Compile Include="Entity\Hotfix.cs" />
<Compile Include="Entity\Scene.cs" />
<Compile Include="Entity\UI.cs" />
<Compile Include="Entity\Unit.cs" />
<Compile Include="Base\Event\AEventAttribute.cs" />
<Compile Include="Base\Event\Env.cs" />
<Compile Include="Base\Event\EnvKey.cs" />
<Compile Include="Base\Event\EventAttribute.cs" />
<Compile Include="Base\Event\EventIdType.cs" />
<Compile Include="Base\Event\IEvent.cs" />
<Compile Include="Base\Object\Component.cs" />
<Compile Include="Base\Object\Disposer.cs" />
<Compile Include="Base\Object\Entity.cs" />
<Compile Include="Base\Object\ObjectEventAttribute.cs" />
<Compile Include="Base\Object\ObjectEvents.cs" />
<Compile Include="Base\Object\EntityType.cs" />
<Compile Include="Base\Object\Object.cs" />
<Compile Include="Base\Other\EntityEventId.cs" />
<Compile Include="Base\Other\IUIFactory.cs" />
<Compile Include="Base\Other\LayerNames.cs" />
<Compile Include="Base\Other\UIFactoryAttribute.cs" />
<Compile Include="Base\Other\UIType.cs" />
<Compile Include="UI\UILobby\Event\InitSceneStart_CreateLobbyUI.cs" />
<Compile Include="Event\SessionRecvMessage_Dispatch.cs" />
<Compile Include="Init.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UI\UILobby\Factory\UILobbyFactory.cs" />
<Compile Include="UI\UILogin\Component\UILoginComponent.cs" />
<Compile Include="UI\UILogin\Event\InitSceneStart_CreateLoginUI.cs" />
<Compile Include="UI\UILogin\Factory\UILoginFactory.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Unity.csproj">
......
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
</Project>
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册