少年修仙传客户端基础资源
lwb
2021-02-07 e5623e7b3111aac69db65987ea16411ac9c80d15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.IO;
using ILCrossBinding;
using ILRuntime.Mono.Cecil.Pdb;
using Snxxz.UI;
using UnityEngine;
 
public class ILRuntimeLoader : SingletonMonobehaviour<ILRuntimeLoader>
{
    FileStream dllFS, pdbFS;
 
    private ILRuntime.Runtime.Enviorment.AppDomain _appdomain;
 
    public ILRuntime.Runtime.Enviorment.AppDomain appDomain
    {
        get
        {
            if (_appdomain == null)
                DebugEx.Log("AppDomain 调用时为空!");
            return _appdomain;
        }
    }
 
    public void Init()
    {
        LoadILRuntime();
        //由于Unity的Profiler接口只允许在主线程使用,为了避免出异常,需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profiler
#if UNITY_EDITOR
        _appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
#endif
        //对LitJson进行注册
        LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(_appdomain);
        RegisterCrossBindingAdaptor();
        RegisterValueTypeBinder();
        RegisterDelegate();
#if UNITY_EDITOR
        // CLRBindings 是动态生成出来的,editor下可能不存在,所以使用反射方式调用
        var type = Type.GetType("ILRuntime.Runtime.Generated.CLRBindings");
        if (type != null)
        {
            var m = type.GetMethod("Initialize");
            m.Invoke(null, new object[] { appDomain });
        }
#else
        ILRuntime.Runtime.Generated.CLRBindings.Initialize(_appdomain);
#endif
    }
 
    void LoadILRuntime()
    {
        _appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
        var dllPath = string.Empty;
        var pdbPath = string.Empty;
        if (AssetSource.logicFromEditor)
        {
            dllPath = ResourcesPath.ResourcesOutAssetPath + "logic/Logic.dll.bytes";
            pdbPath = ResourcesPath.ResourcesOutAssetPath + "logic/Logic.pdb";
        }
        else
        {
            dllPath = AssetVersionUtility.GetAssetFilePath("logic/Logic.dll.bytes");
            pdbPath = AssetVersionUtility.GetAssetFilePath("logic/Logic.pdb");
        }
        if (!File.Exists(dllPath))
        {
            DebugEx.LogErrorFormat("找不到热更代码:{0}", dllPath);
            return;
        }
 
        dllFS = new FileStream(dllPath, FileMode.Open);
        if (VersionConfig.Get().debugVersion && File.Exists(pdbPath))
            pdbFS = new FileStream(pdbPath, FileMode.Open);
        _appdomain.LoadAssembly(dllFS, pdbFS, new PdbReaderProvider());
    }
 
    //注册跨域继承适配器
    private void RegisterCrossBindingAdaptor()
    {
        appDomain.RegisterCrossBindingAdaptor(new CoroutineAdapter());
        appDomain.RegisterCrossBindingAdaptor(new DtcBasicAdapter());
        appDomain.RegisterCrossBindingAdaptor(new GameNetPackBasicAdapter());
    }
 
    //注册值类型绑定
    private void RegisterValueTypeBinder()
    {
        appDomain.RegisterValueTypeBinder(typeof(Vector3), new Vector3Binder());
        appDomain.RegisterValueTypeBinder(typeof(Quaternion), new QuaternionBinder());
        appDomain.RegisterValueTypeBinder(typeof(Vector2), new Vector2Binder());
    }
 
    //注册委托
    private void RegisterDelegate()
    {
        //无返回值
        appDomain.DelegateManager.RegisterMethodDelegate<int>();
        appDomain.DelegateManager.RegisterMethodDelegate<long>();
        appDomain.DelegateManager.RegisterMethodDelegate<string>();
        appDomain.DelegateManager.RegisterMethodDelegate<float>();
        appDomain.DelegateManager.RegisterMethodDelegate<double>();
        appDomain.DelegateManager.RegisterMethodDelegate<bool>();
        appDomain.DelegateManager.RegisterMethodDelegate<byte[]>();
        appDomain.DelegateManager.RegisterMethodDelegate<bool, bool>();
        appDomain.DelegateManager.RegisterMethodDelegate<bool, string>();
        appDomain.DelegateManager.RegisterMethodDelegate<int, string>();
        appDomain.DelegateManager.RegisterMethodDelegate<int, float>();
        appDomain.DelegateManager.RegisterMethodDelegate<string, int>();
        appDomain.DelegateManager.RegisterMethodDelegate<int, bool, string>();
        appDomain.DelegateManager.RegisterMethodDelegate<int, int>();
        appDomain.DelegateManager.RegisterMethodDelegate<int, bool>();
        appDomain.DelegateManager.RegisterMethodDelegate<bool, int>();
        appDomain.DelegateManager.RegisterMethodDelegate<ulong>();
        appDomain.DelegateManager.RegisterMethodDelegate<uint>();
        appDomain.DelegateManager.RegisterMethodDelegate<Snxxz.UI.Operation>();
        appDomain.DelegateManager.RegisterMethodDelegate<float, Vector2>();
        appDomain.DelegateManager.RegisterMethodDelegate<int, Transform>();
        appDomain.DelegateManager.RegisterMethodDelegate<Vector2>();
        appDomain.DelegateManager.RegisterMethodDelegate<Vector3>();
        appDomain.DelegateManager.RegisterMethodDelegate<Transform>();
        appDomain.DelegateManager.RegisterMethodDelegate<GameObject>();
        appDomain.DelegateManager.RegisterMethodDelegate<Component>();
        appDomain.DelegateManager.RegisterMethodDelegate<E_AttackMode>();
        appDomain.DelegateManager.RegisterMethodDelegate<PlayerDataType>();
        appDomain.DelegateManager.RegisterMethodDelegate<PlayerDataType, int>();
        appDomain.DelegateManager.RegisterMethodDelegate<List<Vector3>>();
        appDomain.DelegateManager.RegisterMethodDelegate<ChatData>();
        appDomain.DelegateManager.RegisterMethodDelegate<VoiceHttpRequest.VoiceDecodec>();
        appDomain.DelegateManager.RegisterMethodDelegate<Snxxz.UI.ComposeMatCell, NeedMatType>();
        appDomain.DelegateManager.RegisterMethodDelegate<ItemCompoundConfig, int>();
        appDomain.DelegateManager.RegisterMethodDelegate<Snxxz.UI.ComposeMatCell, int, SelectItemType>();
        appDomain.DelegateManager.RegisterMethodDelegate<GroupType, bool>();
        appDomain.DelegateManager.RegisterMethodDelegate<PackType, int>();
        appDomain.DelegateManager.RegisterMethodDelegate<PackType, string>();
        appDomain.DelegateManager.RegisterMethodDelegate<PackType, int, int>();
        appDomain.DelegateManager.RegisterMethodDelegate<SystemSwitch, bool>();
        appDomain.DelegateManager.RegisterMethodDelegate<ChatBoolType, bool>();
        appDomain.DelegateManager.RegisterMethodDelegate<E_NpcType, int, uint>();
        appDomain.DelegateManager.RegisterMethodDelegate<TeamInviteType>();
        appDomain.DelegateManager.RegisterMethodDelegate<Snxxz.UI.PlayerDetails.DetailType>();
        appDomain.DelegateManager.RegisterMethodDelegate<FunctionUnlockType>();
        appDomain.DelegateManager.RegisterMethodDelegate<TreasureCategory>();
        appDomain.DelegateManager.RegisterMethodDelegate<Window>();
        appDomain.DelegateManager.RegisterMethodDelegate<TimeMgr.SyntonyType>();
        appDomain.DelegateManager.RegisterMethodDelegate<DateTime>();
 
        appDomain.DelegateManager.RegisterDelegateConvertor<UnityEngine.Events.UnityAction>((action) =>
        {
            return new UnityEngine.Events.UnityAction(() =>
            {
                ((System.Action)action)();
            });
        });
 
        //有返回值
        appDomain.DelegateManager.RegisterFunctionDelegate<int>();
        appDomain.DelegateManager.RegisterFunctionDelegate<bool>();
        appDomain.DelegateManager.RegisterFunctionDelegate<long>();
        appDomain.DelegateManager.RegisterFunctionDelegate<string>();
        appDomain.DelegateManager.RegisterFunctionDelegate<float>();
        appDomain.DelegateManager.RegisterFunctionDelegate<double>();
        appDomain.DelegateManager.RegisterFunctionDelegate<int, bool>();
    }
 
    public void LaunchStart()
    {
        _appdomain.Invoke("GameLogicMgr", "Init", null, null);
    }
 
    protected override void OnDestroy()
    {
        base.OnDestroy();
#if UNITY_EDITOR
        var type = Type.GetType("ILRuntime.Runtime.Generated.CLRBindings");
        if (type != null)
        {
            var m = type.GetMethod("Shutdown");
            m.Invoke(null, new object[] { appDomain });
        }
#else
        ILRuntime.Runtime.Generated.CLRBindings.Shutdown(appDomain);
#endif
        dllFS?.Close();
        pdbFS?.Close();
    }
}