三国卡牌客户端基础资源仓库
hch
3 天以前 bea0780aedb3733e94dca26d9986a3adcfb1a693
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
using dnlib.DotNet;
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HybridCLR.Editor.HotUpdate
{
    public class MissingMetadataChecker
    {
        private readonly HashSet<string> _aotAssNames;
 
        private readonly HashSet<string> _hotUpdateAssNames;
 
        private readonly AssemblyCache _assCache;
 
        public MissingMetadataChecker(string aotDllDir, IEnumerable<string> hotUpdateAssNames)
        {
 
            _hotUpdateAssNames = new HashSet<string>(hotUpdateAssNames ?? new List<string>());
            _aotAssNames = new HashSet<string>();
            foreach (var aotFile in Directory.GetFiles(aotDllDir, "*.dll"))
            {
                string aotAssName = Path.GetFileNameWithoutExtension(aotFile);
                if (_hotUpdateAssNames.Contains(aotAssName))
                {
                    continue;
                }
                _aotAssNames.Add(aotAssName);
            }
            _assCache = new AssemblyCache(new PathAssemblyResolver(aotDllDir));
        }
 
        public bool Check(string hotUpdateDllPath)
        {
            bool anyMissing = false;
 
            ModuleDef mod = ModuleDefMD.Load(File.ReadAllBytes(hotUpdateDllPath), _assCache.ModCtx);
 
            foreach (var refass in mod.GetAssemblyRefs())
            {
                string refAssName = refass.Name;
                if (_aotAssNames.Contains(refAssName))
                {
                    _assCache.LoadModule(refass.Name, true);
                }
                else if (!_hotUpdateAssNames.Contains(refAssName))
                {
                    UnityEngine.Debug.LogError($"Missing AOT Assembly: {refAssName}");
                    anyMissing = true;
                }
            }
 
 
            foreach (TypeRef typeRef in mod.GetTypeRefs())
            {
                string defAssName = typeRef.DefinitionAssembly.Name;
                if (!_aotAssNames.Contains(defAssName))
                {
                    continue;
                }
                if (typeRef.ResolveTypeDef() == null)
                {
                    UnityEngine.Debug.LogError($"Missing Type: {typeRef.FullName}");
                    anyMissing = true;
                }
            }
 
            foreach (IMethodDefOrRef memberRef in mod.GetMemberRefs())
            {
                if (memberRef.DeclaringType.DefinitionAssembly == null)
                {
                    continue;
                }
                string defAssName = memberRef.DeclaringType.DefinitionAssembly.Name;
                if (!_aotAssNames.Contains(defAssName))
                {
                    continue;
                }
                if (memberRef.IsField)
                {
                    IField field = (IField)memberRef;
                    if (field.ResolveFieldDef() == null)
                    {
                        UnityEngine.Debug.LogError($"Missing Field: {memberRef.FullName}");
                        anyMissing = true;
                    }
                }
                else if (memberRef.IsMethod)
                {
                    TypeSig declaringTypeSig = memberRef.DeclaringType.ToTypeSig();
                    if (memberRef.ResolveMethodDef() == null)
                    {
                        if (declaringTypeSig.ElementType == ElementType.Array || declaringTypeSig.ElementType == ElementType.SZArray)
                        {
                            continue;
                        }
                        UnityEngine.Debug.LogError($"Missing Method: {memberRef.FullName}");
                        anyMissing = true;
                    }
                }
            }
            return !anyMissing;
        }
    }
}