少年修仙传客户端基础资源
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
using UnityEngine;
using System.Collections.Generic;
using ILRuntime.Other;
using System;
using System.Collections;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;
using ILRuntime.CLR.Method;
 
namespace ILCrossBinding
{
    public class CoroutineAdapter : CrossBindingAdaptor
    {
        public override Type BaseCLRType
        {
            get
            {
                return null;
            }
        }
 
        public override Type[] BaseCLRTypes
        {
            get
            {
                //跨域继承只能有1个Adapter,因此应该尽量避免一个类同时实现多个外部接口,对于coroutine来说是IEnumerator<object>,IEnumerator和IDisposable,
                //ILRuntime虽然支持,但是一定要小心这种用法,使用不当很容易造成不可预期的问题
                //日常开发如果需要实现多个DLL外部接口,请在Unity这边先做一个基类实现那些个接口,然后继承那个基类
                return new Type[] { typeof(IEnumerator<object>), typeof(IEnumerator), typeof(IDisposable) };
            }
        }
 
        public override Type AdaptorType
        {
            get
            {
                return typeof(Adaptor);
            }
        }
 
        public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
        {
            return new Adaptor(appdomain, instance);
        }
        //Coroutine生成的类实现了IEnumerator<System.Object>, IEnumerator, IDisposable,所以都要实现,这个可以通过reflector之类的IL反编译软件得知
        internal class Adaptor : IEnumerator<System.Object>, IEnumerator, IDisposable, CrossBindingAdaptorType
        {
            ILTypeInstance instance;
            ILRuntime.Runtime.Enviorment.AppDomain appdomain;
 
            public Adaptor()
            {
 
            }
 
            public Adaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance)
            {
                this.appdomain = appdomain;
                this.instance = instance;
            }
 
            public ILTypeInstance ILInstance { get { return instance; } }
 
            IMethod mCurrentMethod;
            bool mCurrentMethodGot;
            public object Current
            {
                get
                {
                    if (!mCurrentMethodGot)
                    {
                        mCurrentMethod = instance.Type.GetMethod("get_Current", 0);
                        if (mCurrentMethod == null)
                        {
                            //这里写System.Collections.IEnumerator.get_Current而不是直接get_Current是因为coroutine生成的类是显式实现这个接口的,通过Reflector等反编译软件可得知
                            //为了兼容其他只实现了单一Current属性的,所以上面先直接取了get_Current
                            mCurrentMethod = instance.Type.GetMethod("System.Collections.IEnumerator.get_Current", 0);
                        }
                        mCurrentMethodGot = true;
                    }
 
                    if (mCurrentMethod != null)
                    {
                        var res = appdomain.Invoke(mCurrentMethod, instance, null);
                        return res;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
 
            IMethod mDisposeMethod;
            bool mDisposeMethodGot;
            public void Dispose()
            {
                if (!mDisposeMethodGot)
                {
                    mDisposeMethod = instance.Type.GetMethod("Dispose", 0);
                    if (mDisposeMethod == null)
                    {
                        mDisposeMethod = instance.Type.GetMethod("System.IDisposable.Dispose", 0);
                    }
                    mDisposeMethodGot = true;
                }
 
                if (mDisposeMethod != null)
                {
                    appdomain.Invoke(mDisposeMethod, instance, null);
                }
            }
 
            IMethod mMoveNextMethod;
            bool mMoveNextMethodGot;
            public bool MoveNext()
            {
                if (!mMoveNextMethodGot)
                {
                    mMoveNextMethod = instance.Type.GetMethod("MoveNext", 0);
                    mMoveNextMethodGot = true;
                }
 
                if (mMoveNextMethod != null)
                {
                    return (bool)appdomain.Invoke(mMoveNextMethod, instance, null);
                }
                else
                {
                    return false;
                }
            }
 
            IMethod mResetMethod;
            bool mResetMethodGot;
            public void Reset()
            {
                if (!mResetMethodGot)
                {
                    mResetMethod = instance.Type.GetMethod("Reset", 0);
                    mResetMethodGot = true;
                }
 
                if (mResetMethod != null)
                {
                    appdomain.Invoke(mResetMethod, instance, null);
                }
            }
 
            public override string ToString()
            {
                IMethod m = appdomain.ObjectType.GetMethod("ToString", 0);
                m = instance.Type.GetVirtualMethod(m);
                if (m == null || m is ILMethod)
                {
                    return instance.ToString();
                }
                else
                    return instance.Type.FullName;
            }
        }
    }
}