少年修仙传客户端基础资源
dabaoji
2023-11-13 0a23e1b83f8fda8ab9f9e32fe71c26c431faf63a
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using ILCrossBinding;
using Snxxz.UI;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
 
[System.Reflection.Obfuscation(Exclude = true)]
public class ILRuntimeCLRBinding
{
    const string genPath = "Assets/ILRuntime/Generated";
 
    //需要生成绑定代码的类, litjson 和 c# system 下的代码不要在这生成,会有问题
    //此处配置的类,生成代码会覆盖自动生成的。
    static List<Type> types = new List<Type>
    {
        //Unity相关
        typeof(Transform),
        typeof(RectTransform),
        typeof(GameObject),
        typeof(Component),
        typeof(UnityEngine.Object),
        typeof(MonoBehaviour),
        typeof(Behaviour),
        typeof(Text),
        typeof(Button),
        typeof(Image),
        typeof(Toggle),
        //主工程相关
        typeof(Redpoint),
        typeof(Int2),
        typeof(Int3),
        typeof(UILoader),
        typeof(ComponentExtersion),
        typeof(TransformExtension),
        typeof(LocalSave),
        typeof(ILBehaviourProxy),
        typeof(WidgetBehavior),
        typeof(ILWindowProxy),
        typeof(ILOneLevelWindowProxy),
        typeof(ModelCenter),
        typeof(Singleton<ModelCenter>),
        typeof(WindowCenter),
        typeof(SingletonMonobehaviour<WindowCenter>),
        typeof(WindowJumpMgr),
        typeof(Singleton<WindowJumpMgr>),
        typeof(DebugEx),
        typeof(TextEx),
        typeof(ButtonEx),
        typeof(ImageEx),
        typeof(TimeUtility),
        typeof(HttpRequest),
        typeof(SingletonMonobehaviour<HttpRequest>),
        typeof(SysNotifyMgr),
        typeof(SingletonMonobehaviour<SysNotifyMgr>),
        typeof(PackageRegedit),
        typeof(GameNetSystem),
        typeof(Singleton<GameNetSystem>),
        typeof(GameNetPackBasic),
        typeof(OperationTimeHepler),
        typeof(Singleton<OperationTimeHepler>),
        typeof(DtcBasic),
        typeof(PlayerDatas),
        typeof(Singleton<PlayerDatas>),
        typeof(PlayerBaseData),
        typeof(Language),
        typeof(TimeMgr),
        typeof(SingletonMonobehaviour<TimeMgr>),
        typeof(CommonItemBaisc),
        typeof(ItemCell),
        typeof(ItemTipUtility),
        typeof(SkillModel),
        typeof(VipModel),
        typeof(ItemCellModel),
        typeof(ItemModel),
        typeof(PackModel),
        typeof(EquipModel),
        typeof(RedpointCenter),
        typeof(Singleton<RedpointCenter>),
        typeof(ItemLogicUtility),
        typeof(Singleton<ItemLogicUtility>),
        typeof(ItemOperateUtility),
        typeof(Singleton<ItemOperateUtility>),
        typeof(Equation),
        typeof(Singleton<Equation>),
        typeof(SkillHelper),
        typeof(Singleton<SkillHelper>),
        typeof(OpenServerActivityCenter),
        typeof(Singleton<OpenServerActivityCenter>),
        typeof(GlobalTimeEvent),
        typeof(SingletonMonobehaviour<GlobalTimeEvent>),
        typeof(UIHelper),
        typeof(FindPreciousModel),
        typeof(DungeonModel),
 
        //表
        typeof(ItemConfig),
        typeof(FuncConfigConfig),
        typeof(SysInfoConfig),
        typeof(InvestConfig),
        typeof(VipPrivilegeConfig),
        typeof(DungeonOpenTimeConfig),
        typeof(RealmConfig),
        typeof(MapConfig),
        typeof(OrderInfoConfig),
        typeof(CTGConfig),
        typeof(SkillConfig),
        typeof(PetInfoConfig),
        typeof(HorseConfig),
        typeof(NPCConfig),
        typeof(PlayerPropertyConfig),
        typeof(SpiritWeaponConfig),
        typeof(FuncOpenLVConfig),
        typeof(FightPowerParamConfig),
        typeof(EquipGSParamConfig),
    };
 
    //需要排除的方法
    static List<object[]> excludeMethods = new List<object[]>()
    {
        new object[]{typeof(Text),"OnRebuildRequested"},
        new object[]{typeof(MonoBehaviour),"get_runInEditMode"},
        new object[]{typeof(MonoBehaviour),"set_runInEditMode"},
    };
 
    static HashSet<MethodBase> GetExcludeMethod()
    {
        var set = new HashSet<MethodBase>();
        foreach (var objs in excludeMethods)
        {
            var type = (Type)objs[0];
            var m = type.GetMethod((string)objs[1]);
            set.Add(m);
        }
        return set;
    }
 
    [MenuItem("ILRuntime/清除CLR绑定")]
    static void ClearCLRBinding()
    {
        if (Directory.Exists(genPath))
        {
            Directory.Delete(genPath, true);
        }
        AssetDatabase.Refresh();
    }
 
    [MenuItem("ILRuntime/生成CLR绑定")]
    public static void GenerateCLRBinding()
    {
        ClearCLRBinding();
        GenerateCLRBindingByAnalysis();
        GenerateCLRBindingByTypes();
        MergeGenerated();
        AssetDatabase.Refresh();
        DebugEx.Log("ILRuntime CLR绑定代码生成成功!");
    }
 
    // [MenuItem("ILRuntime/自动分析Dll生成CLR绑定")]
    static void GenerateCLRBindingByAnalysis()
    {
        var path = genPath + "/Analysis";
        //用热更dll调用引用来生成绑定代码
        ILRuntime.Runtime.Enviorment.AppDomain domain = new ILRuntime.Runtime.Enviorment.AppDomain();
        using (System.IO.FileStream fs = new System.IO.FileStream("Assets/ResourcesOut/logic/Logic.dll.bytes", System.IO.FileMode.Open, System.IO.FileAccess.Read))
        {
            domain.LoadAssembly(fs);
            //Crossbind Adapter is needed to generate the correct binding code
            InitILRuntime(domain);
            ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(domain, path);
        }
    }
 
    // [MenuItem("ILRuntime/根据配置列表生成CLR绑定")]
    static void GenerateCLRBindingByTypes()
    {
        var path = genPath + "/Types";
        //用自定义的列表来生成绑定代码
        ILRuntime.Runtime.CLRBinding.BindingCodeGenerator.GenerateBindingCode(types, path, GetExcludeMethod());
    }
 
    //合并生成的代码
    static void MergeGenerated()
    {
        var exist = new HashSet<string>();
        var added = new List<string>();
        var path1 = System.Environment.CurrentDirectory + "/" + genPath + "/Types";
        var dic1 = new DirectoryInfo(path1);
        //获取types生成的列表
        foreach (var file in dic1.GetFiles())
        {
            exist.Add(file.Name);
        }
        var path2 = System.Environment.CurrentDirectory + "/" + genPath + "/Analysis";
        var dic2 = new DirectoryInfo(path2);
        //dll存在和types一样的代码,就删除,其余的视为新增
        foreach (var file in dic2.GetFiles())
        {
            if (exist.Contains(file.Name))
                file.Delete();
            else
            {
                var className = GetClassName(file.FullName);
                if (!string.IsNullOrEmpty(className))
                    added.Add(className);
            }
        }
        //将dll新增代码写入到CLRBindings中注册
        var lines = File.ReadAllLines(path1 + "/CLRBindings.cs").ToList();
        int index = 0;
        for (int i = 0; i < lines.Count; i++)
        {
            if (lines[i].Contains("public static void Initialize"))
            {
                index = i;
                break;
            }
        }
        var space = "            ";
        lines.Insert(index + 2, space + "//配置列表的CLR绑定类");
        foreach (var name in added)
        {
            lines.Insert(index + 2, space + name + ".Register(app);");
        }
        lines.Insert(index + 2, space + "//自动分析dll新增的CLR绑定类");
        File.WriteAllLines(path1 + "/CLRBindings.cs", lines);
    }
 
    //获取文件的类名
    static string GetClassName(string path)
    {
        var lines = File.ReadAllLines(path).ToList();
        foreach (var line in lines)
        {
            if (line.Contains("class"))
            {
                var strs = line.Split(' ');
                return strs[strs.Length - 1];
            }
        }
        return null;
    }
 
 
    static void InitILRuntime(ILRuntime.Runtime.Enviorment.AppDomain domain)
    {
        //这里需要注册所有热更DLL中用到的跨域继承Adapter,否则无法正确抓取引用
        ILLauncherProxy.RegisterCrossBindingAdaptor(domain);
    }
 
}
#endif