Skip to content

Commit e13b558

Browse files
committed
python 增加对 NestedTypes 的支持
1 parent 5d52a4b commit e13b558

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

unity/Assets/core/upm/Runtime/Src/Backends/BackendPython.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ from importlib import machinery
7171
_csTypeCache_ = dict()
7272
_p_loader = scriptEnv.GetLoader()
7373
74+
_p_BindingFlags = loadType(scriptEnv.GetTypeByString('System.Reflection.BindingFlags'))
75+
_csTypeCache_['System.Reflection.BindingFlags'] = _p_BindingFlags
76+
_p_GET_MEMBER_FLAGS = _p_BindingFlags.get_Public() | _p_BindingFlags.get_NonPublic() | _p_BindingFlags.get_Instance() | _p_BindingFlags.get_Static()
7477
class PesapiLoader(importlib.abc.Loader):
7578
7679
def exec_module(self, mod):
@@ -141,14 +144,28 @@ def load_type(type_name: str):
141144
print('Failed to load type: ' + type_name)
142145
return None
143146
cs_class._p_innerType = cs_type
147+
nestedTypes = puerts.get_nested_types(cs_type)
148+
if nestedTypes:
149+
for i in range(nestedTypes.Length):
150+
ntype = nestedTypes.get_Item(i)
151+
if ntype.IsGenericTypeDefinition:
152+
# convert name (T`1) to (T__T1) for sytax compatibility
153+
nName = ntype.Name
154+
tick_index = nName.find('`')
155+
nName = nName[:tick_index] + '__T' + nName[tick_index + 1:]
156+
setattr(cs_class, nName, puerts.load_type(ntype.FullName))
157+
pass # skip generic type definitions, use puerts.generic to instantiate them
158+
else:
159+
try:
160+
setattr(cs_class, ntype.Name, puerts.load_type(ntype.FullName))
161+
except Exception as e:
162+
print(f'load nestedtype [{ntype.Name or ntype}] of {cs_type.Name or cs_type} fail: {e}')
144163
_csTypeCache_[type_name] = cs_class
145164
return cs_class
146165
147166
@staticmethod
148167
def get_nested_types(cs_type):
149-
BindingFlags = puerts.load_type('System.Reflection.BindingFlags')
150-
GET_MEMBER_FLAGS = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public
151-
return cs_type.GetNestedTypes(GET_MEMBER_FLAGS)
168+
return cs_type.GetNestedTypes(_p_GET_MEMBER_FLAGS)
152169
153170
@staticmethod
154171
def gen_iterator(obj):
@@ -198,10 +215,22 @@ def generic(cs_type, *args):
198215
199216
if puerts.typeof(puerts.load_type('System.Collections.IEnumerable')).IsAssignableFrom(cs_type):
200217
cs_class.__iter__ = puerts.gen_iterator
218+
219+
nestedTypes = puerts.get_nested_types(cs_type)
220+
if nestedTypes:
221+
for i in range(nestedTypes.Length):
222+
ntype = nestedTypes.get_Item(i)
223+
if ntype.IsGenericTypeDefinition:
224+
cs_class.__dict__[ntype.Name] = ntype
225+
pass # skip generic type definitions, use puerts.generic to instantiate them
226+
else:
227+
try:
228+
cs_class.__dict__[ntype.Name] = puerts.load_type(ntype.FullName)
229+
except Exception as e:
230+
print(f'load nestedtype [{ntype.Name or ntype}] of {cs_type.Name or cs_type} fail: {e}')
201231
_csTypeCache_[cs_type.FullName] = cs_class
202232
return cs_class
203233
204-
205234
sys.meta_path.append(PesapiFinder())
206235
''')");
207236
}

0 commit comments

Comments
 (0)