12
12
using System . Collections . Generic ;
13
13
using System . IO ;
14
14
using System . Linq ;
15
+ using System . Reflection ;
15
16
using AppDomain = ILRuntime . Runtime . Enviorment . AppDomain ;
16
17
17
18
namespace GameFramework . Taurus
18
19
{
19
20
public sealed class HotFixManager : GameFrameworkModule , IUpdate , IFixedUpdate
20
21
{
21
- #region 属性
22
- /// <summary>
23
- /// ILRuntime的入口
24
- /// </summary>
25
- public AppDomain Appdomain { get ; private set ; }
26
- //热更新的开头函数
27
- private object _hotFixVrCoreEntity ;
22
+ #region 属性
23
+ #if ILRuntime
24
+ /// <summary>
25
+ /// ILRuntime的入口
26
+ /// </summary>
27
+ private AppDomain _appdomain ;
28
+ #else
29
+ /// <summary>
30
+ /// 反射程序集
31
+ /// </summary>
32
+ private Assembly _assembly ;
33
+ #endif
34
+ //热更新的开头函数
35
+ private object _hotFixEntity ;
28
36
29
37
//热更新里面的反射类型
30
- private List < Type > _hotFixReflectionTypes ;
38
+ private List < Type > _hotFixTypes ;
31
39
/// <summary>
32
40
/// 获取热更新的所有类型
33
41
/// </summary>
@@ -36,85 +44,133 @@ public List<Type> GetHotFixTypes
36
44
{
37
45
get
38
46
{
39
- if ( _hotFixReflectionTypes == null || _hotFixReflectionTypes . Count == 0 )
47
+ if ( _hotFixTypes == null || _hotFixTypes . Count == 0 )
40
48
{
41
- _hotFixReflectionTypes = new List < Type > ( ) ;
42
- if ( this . Appdomain == null )
43
- return _hotFixReflectionTypes ;
44
-
45
- foreach ( var item in Appdomain . LoadedTypes . Values )
46
- {
47
- _hotFixReflectionTypes . Add ( item . ReflectionType ) ;
48
- }
49
- }
50
- return _hotFixReflectionTypes ;
49
+ _hotFixTypes = new List < Type > ( ) ;
50
+ #if ILRuntime
51
+ if ( this . _appdomain != null )
52
+ {
53
+ foreach ( var item in _appdomain . LoadedTypes . Values )
54
+ {
55
+ _hotFixTypes . Add ( item . ReflectionType ) ;
56
+ }
57
+ }
58
+ #else
59
+ if ( _assembly != null )
60
+ {
61
+ _hotFixTypes = _assembly . GetTypes ( ) . ToList ( ) ;
62
+ }
63
+ #endif
64
+ }
65
+ return _hotFixTypes ;
51
66
}
52
67
}
53
68
54
- #region 函数
69
+ #region 函数
55
70
//渲染更新函数
56
71
public Action Update ;
57
72
//固定帧更新函数
58
73
public Action FixedUpdate ;
59
74
//结束函数
60
75
public Action Close ;
61
- #endregion
76
+ #endregion
62
77
63
- #endregion
78
+ #endregion
64
79
65
80
public HotFixManager ( )
66
81
{
67
- Appdomain = new AppDomain ( ) ;
82
+
68
83
}
69
-
70
-
84
+
71
85
/// <summary>
72
86
/// 加载
73
87
/// </summary>
74
88
/// <param name="dllDatas"></param>
75
89
/// <param name="pdbDatas"></param>
76
90
public void LoadHotfixAssembly ( byte [ ] dllDatas , byte [ ] pdbDatas = null )
77
91
{
78
- if ( pdbDatas != null )
92
+ #if ILRuntime
93
+ _appdomain = new AppDomain ( ) ;
94
+ if ( pdbDatas != null )
79
95
{
80
96
using ( System . IO . MemoryStream fs = new MemoryStream ( dllDatas ) )
81
97
{
82
98
using ( System . IO . MemoryStream p = new MemoryStream ( pdbDatas ) )
83
99
{
84
- Appdomain . LoadAssembly ( fs , p , new Mono . Cecil . Pdb . PdbReaderProvider ( ) ) ;
100
+ _appdomain . LoadAssembly ( fs , p , new Mono . Cecil . Pdb . PdbReaderProvider ( ) ) ;
85
101
}
86
102
}
87
103
}
88
104
else
89
105
using ( System . IO . MemoryStream fs = new MemoryStream ( dllDatas ) )
90
106
{
91
- Appdomain . LoadAssembly ( fs , null , new Mono . Cecil . Pdb . PdbReaderProvider ( ) ) ;
107
+ _appdomain . LoadAssembly ( fs , null , new Mono . Cecil . Pdb . PdbReaderProvider ( ) ) ;
92
108
}
93
-
94
-
109
+
95
110
InitializeILRuntime ( ) ;
111
+ #else
112
+ _assembly = Assembly . Load ( dllDatas , pdbDatas ) ;
113
+ #endif
114
+ //运行热更新的入口
115
+ RunHotFixInstantiate ( ) ;
116
+ }
117
+
118
+ public void OnUpdate ( )
119
+ {
120
+ Update ? . Invoke ( ) ;
121
+ }
96
122
97
- //运行热更新的入口
98
- RunHotFixVrCoreEntity ( ) ;
123
+ public void OnFixedUpdate ( )
124
+ {
125
+ FixedUpdate ? . Invoke ( ) ;
99
126
}
100
127
101
- void InitializeILRuntime ( )
128
+ public override void OnClose ( )
129
+ {
130
+ Close ? . Invoke ( ) ;
131
+
132
+ _hotFixEntity = null ;
133
+
134
+ #if ILRuntime
135
+ _appdomain = null ;
136
+ #else
137
+ _assembly = null ;
138
+ #endif
139
+
140
+ }
141
+
142
+ //运行更新的实例
143
+ private void RunHotFixInstantiate ( )
144
+ {
145
+ #if ILRuntime
146
+ _hotFixEntity = _appdomain ? . Instantiate ( "HotFix.Taurus.HotFixMode" ) ;
147
+ #else
148
+ _hotFixEntity = _assembly ? . CreateInstance ( "HotFix.Taurus.HotFixMode" ) ;
149
+ #endif
150
+ if ( _hotFixEntity == null )
151
+ throw new GamekException ( "热更新实例化失败:HotFix.Taurus.HotFixMode" ) ;
152
+ }
153
+
154
+ #region 内部函数
155
+
156
+ #if ILRuntime
157
+ void InitializeILRuntime ( )
102
158
{
103
159
//注册CLR绑定
104
- ILRuntime . Runtime . Generated . CLRBindings . Initialize ( Appdomain ) ;
160
+ ILRuntime . Runtime . Generated . CLRBindings . Initialize ( _appdomain ) ;
105
161
106
162
//跨域继承的基类
107
- Appdomain . RegisterCrossBindingAdaptor ( new Google . Protobuf . IMessageAdaptor ( ) ) ;
108
- Appdomain . RegisterCrossBindingAdaptor ( new IAsyncStateMachineClassInheritanceAdaptor ( ) ) ;
109
- Appdomain . DelegateManager . RegisterFunctionDelegate < Google . Protobuf . IMessageAdaptor . Adaptor > ( ) ;
110
- Appdomain . DelegateManager . RegisterMethodDelegate < System . Object > ( ) ;
163
+ _appdomain . RegisterCrossBindingAdaptor ( new Google . Protobuf . IMessageAdaptor ( ) ) ;
164
+ _appdomain . RegisterCrossBindingAdaptor ( new IAsyncStateMachineClassInheritanceAdaptor ( ) ) ;
165
+ _appdomain . DelegateManager . RegisterFunctionDelegate < Google . Protobuf . IMessageAdaptor . Adaptor > ( ) ;
166
+ _appdomain . DelegateManager . RegisterMethodDelegate < System . Object > ( ) ;
111
167
112
168
113
- Appdomain . DelegateManager . RegisterMethodDelegate < System . UInt16 , System . Byte [ ] > ( ) ;
169
+ _appdomain . DelegateManager . RegisterMethodDelegate < System . UInt16 , System . Byte [ ] > ( ) ;
114
170
115
171
//这里做一些ILRuntime的注册,HelloWorld示例暂时没有需要注册的
116
- Appdomain . DelegateManager . RegisterMethodDelegate < System . Object , ILRuntime . Runtime . Intepreter . ILTypeInstance > ( ) ;
117
- Appdomain . DelegateManager . RegisterDelegateConvertor < System . EventHandler < ILRuntime . Runtime . Intepreter . ILTypeInstance > > ( ( act ) =>
172
+ _appdomain . DelegateManager . RegisterMethodDelegate < System . Object , ILRuntime . Runtime . Intepreter . ILTypeInstance > ( ) ;
173
+ _appdomain . DelegateManager . RegisterDelegateConvertor < System . EventHandler < ILRuntime . Runtime . Intepreter . ILTypeInstance > > ( ( act ) =>
118
174
{
119
175
return new System . EventHandler < ILRuntime . Runtime . Intepreter . ILTypeInstance > ( ( sender , e ) =>
120
176
{
@@ -124,28 +180,9 @@ void InitializeILRuntime()
124
180
125
181
126
182
}
127
-
128
- public void OnUpdate ( )
129
- {
130
- Update ? . Invoke ( ) ;
131
- }
183
+ #endif
132
184
133
- public void OnFixedUpdate ( )
134
- {
135
- FixedUpdate ? . Invoke ( ) ;
136
- }
137
-
138
- public override void OnClose ( )
139
- {
140
- Close ? . Invoke ( ) ;
141
- Appdomain = null ;
142
- }
143
-
144
- //运行更新的实例
145
- private void RunHotFixVrCoreEntity ( )
146
- {
147
- _hotFixVrCoreEntity = Appdomain . Instantiate ( "HotFix.Taurus.HotFixMode" ) ;
148
- }
185
+ #endregion
149
186
150
- }
187
+ }
151
188
}
0 commit comments