public Adaptor(AppDomain appdomain, ILTypeInstance instance) : base(appdomain, instance) { this.m_appdomain = appdomain; this.m_instance = instance; }
public override object CreateCLRInstance(AppDomain appdomain, ILTypeInstance instance) { return(new Adaptor(appdomain, instance)); }
protected MyAdaptor(AppDomain appdomain, ILTypeInstance instance) { AppDomain = appdomain; _instance = instance; }
//IL2CPP can't process esp->ToObject() properly, so I can only use static function for this public static unsafe object ToObject(StackObject* esp, ILRuntime.Runtime.Enviorment.AppDomain appdomain, IList<object> mStack) { switch (esp->ObjectType) { case ObjectTypes.Integer: return esp->Value; case ObjectTypes.Long: { return *(long*)&esp->Value; } case ObjectTypes.Float: { return *(float*)&esp->Value; } case ObjectTypes.Double: { return *(double*)&esp->Value; } case ObjectTypes.Object: return mStack[esp->Value]; case ObjectTypes.FieldReference: { ILTypeInstance instance = mStack[esp->Value] as ILTypeInstance; if (instance != null) { return instance[esp->ValueLow]; } else { var obj = mStack[esp->Value]; IType t = null; if (obj is CrossBindingAdaptorType) { t = appdomain.GetType(((CrossBindingAdaptor)((CrossBindingAdaptorType)obj).ILInstance.Type.FirstCLRBaseType).BaseCLRType); } else t = appdomain.GetType(obj.GetType()); return ((CLRType)t).GetFieldValue(esp->ValueLow, obj); } } case ObjectTypes.ArrayReference: { Array instance = mStack[esp->Value] as Array; return instance.GetValue(esp->ValueLow); } case ObjectTypes.StaticFieldReference: { var t = appdomain.GetType(esp->Value); if (t is CLR.TypeSystem.ILType) { CLR.TypeSystem.ILType type = (CLR.TypeSystem.ILType)t; return type.StaticInstance[esp->ValueLow]; } else { CLR.TypeSystem.CLRType type = (CLR.TypeSystem.CLRType)t; return type.GetFieldValue(esp->ValueLow, null); } } case ObjectTypes.StackObjectReference: { return ToObject((ILIntepreter.ResolveReference(esp)), appdomain, mStack); } case ObjectTypes.ValueTypeObjectReference: { StackObject* dst = ILIntepreter.ResolveReference(esp); IType type = appdomain.GetTypeByIndex(dst->Value); if (type is ILType) { ILType iltype = (ILType)type; var ins = iltype.Instantiate(false); for (int i = 0; i < dst->ValueLow; i++) { var addr = ILIntepreter.Minus(dst, i + 1); ins.AssignFromStack(i, addr, appdomain, mStack); } return ins; } else { return ((CLRType)type).ValueTypeBinder.ToObject(dst, mStack); } } case ObjectTypes.Null: return null; default: throw new NotImplementedException(); } }
public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) { return(new Adaptor(appdomain, instance)); }
public Adapter(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) { m_Appdomain = appdomain; m_Instance = instance; }
public override void Invoke(ILTypeInstance instance) { throw new NotSupportedException(); }
public IAsyncStateMachineAdaptor(ILRuntime.Runtime.Enviorment.AppDomain appDomain, ILTypeInstance instance) { this.appDomain = appDomain; this.instance = instance; }
public Adapter(AppDomain appdomain, ILTypeInstance instance) { m_Appdomain = appdomain; m_Instance = instance; }
public void Reset() { mAwakeMethodGot = false; mStartMethodGot = false; mUpdateMethodGot = false; mFixedUpdateMethodGot = false; mLateUpdateMethodGot = false; mOnEnableMethodGot = false; mOnDisableMethodGot = false; mDestroyMethodGot = false; mOnTriggerEnterMethodGot = false; mOnTriggerStayMethodGot = false; mOnTriggerExitMethodGot = false; mOnCollisionEnterMethodGot = false; mOnCollisionStayMethodGot = false; mOnCollisionExitMethodGot = false; mOnValidateMethodGot = false; mOnAnimatorMoveMethodGot = false; mOnApplicationFocusMethodGot = false; mOnApplicationPauseMethodGot = false; mOnApplicationQuitMethodGot = false; mOnBecameInvisibleMethodGot = false; mOnBecameVisibleMethodGot = false; mOnDrawGizmosMethodGot = false; mOnJointBreakMethodGot = false; mOnMouseDownMethodGot = false; mOnMouseDragMethodGot = false; mOnMouseEnterMethodGot = false; mOnMouseExitMethodGot = false; mOnMouseOverMethodGot = false; mOnMouseUpMethodGot = false; mOnParticleCollisionMethodGot = false; mOnParticleTriggerMethodGot = false; mOnPostRenderMethodGot = false; mOnPreCullMethodGot = false; mOnPreRenderMethodGot = false; mOnRenderImageMethodGot = false; mOnRenderObjectMethodGot = false; mOnServerInitializedMethodGot = false; mOnAnimatorIKMethodGot = false; mOnAudioFilterReadMethodGot = false; mOnCanvasGroupChangedMethodGot = false; mOnCanvasHierarchyChangedMethodGot = false; mOnCollisionEnter2DMethodGot = false; mOnCollisionExit2DMethodGot = false; mOnCollisionStay2DMethodGot = false; mOnConnectedToServerMethodGot = false; mOnControllerColliderHitMethodGot = false; mOnDrawGizmosSelectedMethodGot = false; mOnGUIMethodGot = false; mOnJointBreak2DMethodGot = false; mOnParticleSystemStoppedMethodGot = false; mOnTransformChildrenChangedMethodGot = false; mOnTransformParentChangedMethodGot = false; mOnTriggerEnter2DMethodGot = false; mOnTriggerExit2DMethodGot = false; mOnTriggerStay2DMethodGot = false; mOnWillRenderObjectMethodGot = false; mOnBeforeTransformParentChangedMethodGot = false; mOnDidApplyAnimationPropertiesMethodGot = false; mOnMouseUpAsButtonMethodGot = false; mOnParticleUpdateJobScheduledMethodGot = false; mOnRectTransformDimensionsChangeMethodGot = false; instance = null; appdomain = null; destoryed = false; awaked = false; isAwaking = false; }
public ITargetProxy CreateProxy(object target, BindingDescription description) { if (target == null || !(target is ILTypeInstance || target is CrossBindingAdaptorType)) { return(null); } ILTypeInstance typeInstance = target as ILTypeInstance; if (typeInstance == null) { typeInstance = (target as CrossBindingAdaptorType).ILInstance; } IProxyType type = typeInstance.Type.ReflectionType.AsProxy(); IProxyMemberInfo memberInfo = type.GetMember(description.TargetName); if (memberInfo == null) { memberInfo = type.GetMember(description.TargetName, BindingFlags.Instance | BindingFlags.NonPublic); } if (memberInfo == null) { throw new MissingMemberException(type.Type.FullName, description.TargetName); } var propertyInfo = memberInfo as IProxyPropertyInfo; if (propertyInfo != null) { var valueType = propertyInfo.ValueType; if (typeof(IObservableProperty).IsAssignableFrom(valueType)) { object observableValue = propertyInfo.GetValue(target); if (observableValue == null) { throw new NullReferenceException(string.Format("The \"{0}\" property is null in class \"{1}\".", propertyInfo.Name, propertyInfo.DeclaringType.Name)); } return(new ObservableTargetProxy(target, (IObservableProperty)observableValue)); } if (typeof(IInteractionAction).IsAssignableFrom(valueType)) { object interactionAction = propertyInfo.GetValue(target); if (interactionAction == null) { return(null); } return(new InteractionTargetProxy(target, (IInteractionAction)interactionAction)); } return(new PropertyTargetProxy(target, propertyInfo)); } var fieldInfo = memberInfo as IProxyFieldInfo; if (fieldInfo != null) { var valueType = fieldInfo.ValueType; if (typeof(IObservableProperty).IsAssignableFrom(valueType)) { object observableValue = fieldInfo.GetValue(target); if (observableValue == null) { throw new NullReferenceException(string.Format("The \"{0}\" field is null in class \"{1}\".", fieldInfo.Name, fieldInfo.DeclaringType.Name)); } return(new ObservableTargetProxy(target, (IObservableProperty)observableValue)); } if (typeof(IInteractionAction).IsAssignableFrom(valueType)) { object interactionAction = fieldInfo.GetValue(target); if (interactionAction == null) { return(null); } return(new InteractionTargetProxy(target, (IInteractionAction)interactionAction)); } return(new FieldTargetProxy(target, fieldInfo)); } var eventInfo = memberInfo as IProxyEventInfo; if (eventInfo != null) { return(new EventTargetProxy(target, eventInfo)); } var methodInfo = memberInfo as IProxyMethodInfo; if (methodInfo != null) { return(new MethodTargetProxy(target, methodInfo)); } return(null); }
VariableInfo[] EnumILTypeInstance(ILTypeInstance obj, ILIntepreter intepreter) { return EnumObject(obj, obj.Type.ReflectionType); }
public static bool SerializeJBehaviourType(AnimBool[] fadeGroup, ILTypeInstance instance) { //如果JBehaviour var jBehaviourType = Init.Appdomain.LoadedTypes["JEngine.Core.JBehaviour"]; var t = instance.Type.ReflectionType; if (t.IsSubclassOf(jBehaviourType.ReflectionType)) { var f = t.GetField("_instanceID", BindingFlags.NonPublic); if (!(f is null)) { GUI.enabled = false; var id = f.GetValue(instance).ToString(); EditorGUILayout.TextField("InstanceID", id); GUI.enabled = true; } string frameModeStr = "FrameMode"; string frequencyStr = "Frequency"; string pausedStr = "Paused"; string totalTimeStr = "TotalTime"; string loopDeltaTimeStr = "LoopDeltaTime"; string loopCountsStr = "LoopCounts"; string timeScaleStr = "TimeScale"; fadeGroup[0].target = EditorGUILayout.Foldout(fadeGroup[0].target, "JBehaviour Stats", true); if (EditorGUILayout.BeginFadeGroup(fadeGroup[0].faded)) { var fm = t.GetField(frameModeStr, BindingFlags.Public); bool frameMode = !(fm is null) && EditorGUILayout.Toggle(frameModeStr, (bool)fm.GetValue(instance)); fm?.SetValue(instance, frameMode); var fq = t.GetField(frequencyStr, BindingFlags.Public); if (!(fq is null)) { int frequency = EditorGUILayout.IntField(frequencyStr, (int)fq.GetValue(instance)); fq.SetValue(instance, frequency); } GUI.enabled = false; var paused = t.GetField(pausedStr, BindingFlags.NonPublic); if (!(paused is null)) { EditorGUILayout.Toggle(pausedStr, (bool)paused.GetValue(instance)); } var totalTime = t.GetField(totalTimeStr, BindingFlags.Public); if (!(totalTime is null)) { EditorGUILayout.FloatField(totalTimeStr, (float)totalTime.GetValue(instance)); } var loopDeltaTime = t.GetField(loopDeltaTimeStr, BindingFlags.Public); if (!(loopDeltaTime is null)) { EditorGUILayout.FloatField(loopDeltaTimeStr, (float)loopDeltaTime.GetValue(instance)); } var loopCounts = t.GetField(loopCountsStr, BindingFlags.Public); if (!(loopCounts is null)) { EditorGUILayout.LongField(loopCountsStr, (long)loopCounts.GetValue(instance)); } GUI.enabled = true; var timeScale = t.GetField(timeScaleStr, BindingFlags.Public); if (!(timeScale is null)) { var ts = EditorGUILayout.FloatField(timeScaleStr, (float)timeScale.GetValue(instance)); timeScale.SetValue(instance, ts); } } EditorGUILayout.EndFadeGroup(); if (instance.Type.FieldMapping.Count > 0) { EditorGUILayout.Space(10); EditorGUILayout.HelpBox(String.Format(Setting.GetString(SettingString.MemberVariables), t.Name), MessageType.Info); } return(true); } return(false); }
public IAsyncStateMachineAdaptor(AppDomain appDomain, ILTypeInstance instance) { this.appDomain = appDomain; this.instance = instance; }
public Adaptor(AppDomain appdomain, ILTypeInstance instance) : base(appdomain, instance) { }
public Adapter(AppDomain appDomain, ILTypeInstance instance) { _appDomain = appDomain; _instance = instance; }
//IL2CPP can't process esp->ToObject() properly, so I can only use static function for this public static unsafe object ToObject(StackObject *esp, ILRuntime.Runtime.Enviorment.AppDomain appdomain, List <object> mStack) { switch (esp->ObjectType) { case ObjectTypes.Integer: return(esp->Value); case ObjectTypes.Long: { return(*(long *)&esp->Value); } case ObjectTypes.Float: { return(*(float *)&esp->Value); } case ObjectTypes.Double: { return(*(double *)&esp->Value); } case ObjectTypes.Object: return(mStack[esp->Value]); case ObjectTypes.FieldReference: { ILTypeInstance instance = mStack[esp->Value] as ILTypeInstance; if (instance != null) { var tmp = instance.Fields[esp->ValueLow]; return(ToObject(&tmp, appdomain, instance.ManagedObjects)); } else { var obj = mStack[esp->Value]; IType t = null; if (obj is CrossBindingAdaptorType) { t = appdomain.GetType(((CrossBindingAdaptor)((CrossBindingAdaptorType)obj).ILInstance.Type.BaseType).BaseCLRType); } else { t = appdomain.GetType(obj.GetType()); } var fi = ((CLRType)t).Fields[esp->ValueLow]; return(fi.GetValue(obj)); } } case ObjectTypes.ArrayReference: { Array instance = mStack[esp->Value] as Array; return(instance.GetValue(esp->ValueLow)); } case ObjectTypes.StaticFieldReference: { var t = appdomain.GetType(esp->Value); if (t is CLR.TypeSystem.ILType) { CLR.TypeSystem.ILType type = (CLR.TypeSystem.ILType)t; var tmp = type.StaticInstance.Fields[esp->ValueLow]; return(ToObject(&tmp, appdomain, type.StaticInstance.ManagedObjects)); } else { CLR.TypeSystem.CLRType type = (CLR.TypeSystem.CLRType)t; var fi = type.Fields[esp->ValueLow]; return(fi.GetValue(null)); } } case ObjectTypes.StackObjectReference: { return(ToObject((*(StackObject **)&esp->Value), appdomain, mStack)); } case ObjectTypes.Null: return(null); default: throw new NotImplementedException(); } }
/// <summary> /// ilMethod代表的delegate会赋值给method对应的delegate,一般两者参数类型都一致, /// 但新版本的支持泛型协变之后,有些时候会不一致,所以此处判断是用method判断,而不是用ilMethod判断 /// </summary> /// <param name="instance"></param> /// <param name="ilMethod"></param> /// <param name="method"></param> /// <returns></returns> internal IDelegateAdapter FindDelegateAdapter(ILTypeInstance instance, ILMethod ilMethod, IMethod method) { IDelegateAdapter res; var parameterCount = method.ParameterCount; var returnTypeForCLR = method.ReturnType.TypeForCLR; if (method.ReturnType == appdomain.VoidType) { if (parameterCount == 0) { res = zeroParamMethodAdapter.Instantiate(appdomain, instance, ilMethod); if (instance != null) { instance.SetDelegateAdapter(ilMethod, res); } return(res); } foreach (var i in methods) { var parameterTypes = i.ParameterTypes; if (parameterTypes.Length == parameterCount) { bool match = true; for (int j = 0; j < parameterCount; j++) { if (parameterTypes[j] != method.Parameters[j].TypeForCLR) { match = false; break; } } if (match) { res = i.Adapter.Instantiate(appdomain, instance, ilMethod); if (instance != null) { instance.SetDelegateAdapter(ilMethod, res); } return(res); } } } } else { foreach (var i in functions) { var parameterTypes = i.ParameterTypes; if (parameterTypes.Length == parameterCount + 1) { bool match = true; for (int j = 0; j < parameterCount; j++) { if (parameterTypes[j] != method.Parameters[j].TypeForCLR) { match = false; break; } } if (match) { if (returnTypeForCLR == parameterTypes[parameterCount]) { res = i.Adapter.Instantiate(appdomain, instance, ilMethod); if (instance != null) { instance.SetDelegateAdapter(ilMethod, res); } return(res); } } } } } res = dummyAdapter.Instantiate(appdomain, instance, ilMethod); if (instance != null) { instance.SetDelegateAdapter(ilMethod, res); } return(res); }
public abstract object CreateCLRInstance(Enviorment.AppDomain appdomain, ILTypeInstance instance);
public override object CreateCLRInstance(AppDomain appdomain, ILTypeInstance instance) { return(new MyUIBehaviour(appdomain, instance)); }
internal IDelegateAdapter FindDelegateAdapter(ILTypeInstance instance, ILMethod method) { if (method.ReturnType == appdomain.VoidType) { if (method.ParameterCount == 0) { return(zeroParamMethodAdapter.Instantiate(appdomain, instance, method)); } foreach (var i in methods) { if (i.ParameterTypes.Length == method.ParameterCount) { bool match = true; for (int j = 0; j < method.ParameterCount; j++) { if (i.ParameterTypes[j] != method.Parameters[j].TypeForCLR) { match = false; break; } } if (match) { var res = i.Adapter.Instantiate(appdomain, instance, method); if (instance != null) { instance.SetDelegateAdapter(method, res); } return(res); } } } } else { foreach (var i in functions) { if (i.ParameterTypes.Length == method.ParameterCount + 1) { bool match = true; for (int j = 0; j < method.ParameterCount; j++) { if (i.ParameterTypes[j] != method.Parameters[j].TypeForCLR) { match = false; break; } } if (match) { if (method.ReturnType.TypeForCLR == i.ParameterTypes[method.ParameterCount]) { var res = i.Adapter.Instantiate(appdomain, instance, method); if (instance != null) { instance.SetDelegateAdapter(method, res); } return(res); } } } } } throw new KeyNotFoundException("Cannot find Delegate Adapter for:" + method); }
public MyUIBehaviour(AppDomain appdomain, ILTypeInstance instance) { this.appdomain = appdomain; this.instance = instance; }
/// <summary> /// Add class /// </summary> /// <param name="classData"></param> /// <returns></returns> public string AddClass(ClassData classData) { //添加脚本 string classType = $"{classData.classNamespace + (classData.classNamespace == "" ? "" : ".")}{classData.className}"; if (!InitJEngine.Appdomain.LoadedTypes.ContainsKey(classType)) { Log.PrintError($"自动绑定{name}出错:{classType}不存在,已跳过"); return(null); } IType type = InitJEngine.Appdomain.LoadedTypes[classType]; Type t = type.ReflectionType; //获取实际属性 //JBehaviour需自动赋值一个值 var jBehaviourType = InitJEngine.Appdomain.LoadedTypes["JEngine.Core.JBehaviour"]; bool isJBehaviour = t.IsSubclassOf(jBehaviourType.ReflectionType); bool isMono = t.IsSubclassOf(typeof(MonoBehaviour)); bool needAdapter = t.BaseType != null && t.BaseType.GetInterfaces().Contains(typeof(CrossBindingAdaptorType)); ILTypeInstance instance; if (classData.useConstructor) { instance = isMono ? new ILTypeInstance(type as ILType, false) : InitJEngine.Appdomain.Instantiate(classType); } else { instance = new ILTypeInstance(type as ILType, !isMono); } instance.CLRInstance = instance; //这里是ClassBind的灵魂,我都佩服我自己这么写,所以别乱改这块 //非mono的跨域继承用特殊的,就是用JEngine提供的一个mono脚本,来显示字段,里面存ILTypeInstance //总之JEngine牛逼 //是继承Mono封装的基类,用自动生成的 if (needAdapter && isMono && t.BaseType?.FullName != typeof(MonoBehaviourAdapter.Adaptor).FullName && Type.GetType(t.BaseType?.FullName ?? string.Empty) != null) { Type adapterType = Type.GetType(t.BaseType?.FullName ?? string.Empty); if (adapterType == null) { Log.PrintError($"{t.FullName}, need to generate adapter"); return(null); } //直接反射赋值一波了 var clrInstance = gameObject.AddComponent(adapterType); var clrEnabled = t.GetProperty("enabled", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); var clrILInstance = t.GetField("instance", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); var clrAppDomain = t.GetField("appdomain", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); clrEnabled?.SetValue(clrInstance, false); clrILInstance?.SetValue(clrInstance, instance); clrAppDomain?.SetValue(clrInstance, InitJEngine.Appdomain); instance.CLRInstance = clrInstance; } //直接继承Mono的,非继承mono的,或不需要继承的,用这个 else { //挂个适配器到编辑器(直接继承mono,非继承mono,无需继承,都可以用这个) var clrInstance = gameObject.AddComponent <MonoBehaviourAdapter.Adaptor>(); clrInstance.enabled = false; clrInstance.ILInstance = instance; clrInstance.AppDomain = InitJEngine.Appdomain; //是MonoBehaviour继承,需要指定CLRInstance if (isMono && needAdapter) { instance.CLRInstance = clrInstance; } //判断类型 clrInstance.isMonoBehaviour = isMono; classData.Added = true; //JBehaviour额外处理 if (isJBehaviour) { clrInstance.isJBehaviour = true; var go = t.GetField("_gameObject", BindingFlags.Public); if (!(go is null)) { go.SetValue(clrInstance.ILInstance, gameObject); } } //JBehaviour返回实例ID if (isJBehaviour) { var f = t.GetField("_instanceID", BindingFlags.NonPublic); if (!(f is null)) { var id = f.GetValue(clrInstance.ILInstance).ToString(); return(id); } } } if (classData.useConstructor && isMono) { var m = type.GetConstructor(Extensions.EmptyParamList); if (m != null) { InitJEngine.Appdomain.Invoke(m, instance, null); } } return(null); }
void Awake() { IType itype = ILRuntimeManager.GetScriptType(script); if (itype != null) { ILTypeInstance instance = new ILTypeInstance(itype as ILType, false);; if (instance != null) { for (int i = 0; i < fields.Count; ++i) { int index; var type = instance.Type.GetField(fields[i].name, out index); if (type != null) { var cType = type.TypeForCLR; if (cType.IsPrimitive) { if (cType == typeof(sbyte)) { sbyte val; if (!sbyte.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(short)) { short val; if (!short.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(int)) { int val; if (!int.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(long)) { long val; if (!long.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(byte)) { byte val; if (!byte.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(ushort)) { ushort val; if (!ushort.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(uint)) { uint val; if (!uint.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(ulong)) { ulong val; if (!ulong.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(float)) { float val; if (!float.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(double)) { double val; if (!double.TryParse(fields[i].value, out val)) { val = 0; } instance[index] = val; } else if (cType == typeof(bool)) { bool val; if (!bool.TryParse(fields[i].value, out val)) { val = false; } instance[index] = val; } else if (cType == typeof(char)) { char val; if (!char.TryParse(fields[i].value, out val)) { val = ' '; } instance[index] = val; } //else //throw new System.NotImplementedException(); } else { if (!typeof(UnityEngine.Object).IsAssignableFrom(cType)) { if (cType == typeof(string)) { instance[index] = fields[i].value; } else if (cType == typeof(UnityEngine.Vector2)) { instance[index] = UnityHelper.ParseVector2(fields[i].value); } else if (cType == typeof(UnityEngine.Vector3)) { instance[index] = UnityHelper.ParseVector3(fields[i].value); } else if (cType == typeof(UnityEngine.Vector4)) { instance[index] = UnityHelper.ParseVector4(fields[i].value); } } } } } for (int i = 0; i < objFields.Count; ++i) { int index; var type = instance.Type.GetField(objFields[i].name, out index); if (type != null) { var cType = type.TypeForCLR; if (!cType.IsPrimitive) { if (typeof(UnityEngine.Object).IsAssignableFrom(cType)) { instance[index] = objFields[i]; } } } } var adptor = gameObject.AddComponent <MonoBehaviourAdapter.Adaptor>(); adptor.enabled = enabled; adptor.AppDomain = ILRuntimeManager.app; adptor.ILInstance = instance; if (gameObject.activeInHierarchy) { adptor.Awake(); if (adptor.enabled) { adptor.OnEnable(); } } } } TimerManager.Instance.AddFarmeTimer(1, delayDestory); }
public Adaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) { this.appdomain = appdomain; this.instance = instance; }
/// <summary> /// Create a instance of the specified type, which is inherited from a CLR Type /// </summary> /// <typeparam name="T">CLR Type</typeparam> /// <param name="type">Full Name of the type</param> /// <param name="args">Arguments for the constructor</param> /// <returns></returns> public T Instantiate <T>(string type, object[] args = null) { ILTypeInstance ins = Instantiate(type, args); return((T)ins.CLRInstance); }
public Adaptor(AppDomain appdomain, ILTypeInstance instance) { this.appdomain = appdomain; this.instance = instance; }
private void Bind() { var cb = this; foreach (var _class in cb.ScriptsToBind) { //添加脚本 string classType = $"{_class.Namespace + (_class.Namespace == "" ? "" : ".")}{_class.Class}"; if (!InitILrt.appDomain.LoadedTypes.ContainsKey(classType)) { Log.PrintError($"{classType}不存在,已跳过"); continue; } IType type = InitILrt.appDomain.LoadedTypes[classType]; var instance = new ILTypeInstance(type as ILType, false); var clrInstance = cb.gameObject.AddComponent <MonoBehaviourAdapter.Adaptor>(); clrInstance.enabled = false; clrInstance.ILInstance = instance; clrInstance.AppDomain = InitILrt.appDomain; instance.CLRInstance = clrInstance; //绑定数据 if (_class.RequireBindFields) { _class.BoundData = false; //获取实际属性 Type t = type.ReflectionType; foreach (var field in _class.Fields) { object obj = new object(); try { if (field.fieldType == _ClassField.FieldType.Short) { obj = int.Parse(field.value); _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.UShort) { obj = ushort.Parse(field.value); _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.Int) { obj = short.Parse(field.value); _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.UInt) { obj = uint.Parse(field.value); _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.Long) { obj = long.Parse(field.value); _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.ULong) { obj = ulong.Parse(field.value); _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.Float) { obj = float.Parse(field.value); _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.Decimal) { obj = decimal.Parse(field.value); _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.Double) { obj = Double.Parse(field.value); _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.String) { obj = field.value; _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.Bool) { obj = field.value == "true"; _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.GameObject) { GameObject go = null; try { go = field.value.Substring(0, 7) == "${this}" ? this.gameObject : GameObject.Find(field.value.Substring(0, field.value.LastIndexOf('.'))); if (go == null) { if (field.value.Contains("/")) //如果有父级 { try { var parent = GameObject.Find(field.value.Substring(0, field.value.IndexOf('/'))); //寻找父物体 go = parent.transform .Find(field.value.Substring(field.value.IndexOf('/') + 1)) .gameObject; } catch { Log.PrintError($"{field.value}对象被隐藏或不存在,无法获取,已跳过"); continue; } } else { Log.PrintError($"{field.value}对象被隐藏或不存在,无法获取,已跳过"); continue; } } } catch (Exception ex) { if (field.value.Contains("/")) //如果有父级 { try { var parent = GameObject.Find(field.value.Substring(0, field.value.IndexOf('/'))); //寻找父物体 go = parent.transform .Find(field.value.Substring(field.value.IndexOf('/') + 1)) .gameObject; } catch { Log.PrintError($"{field.value}对象被隐藏或不存在,无法获取,已跳过"); continue; } } else { Log.PrintError($"{field.value}对象被隐藏或不存在,无法获取,已跳过"); continue; } } obj = go; _class.BoundData = true; } else if (field.fieldType == _ClassField.FieldType.UnityComponent) { GameObject go = null; try { go = field.value.Substring(0, 7) == "${this}" ? this.gameObject : GameObject.Find(field.value.Substring(0, field.value.LastIndexOf('.'))); if (go == null) { if (field.value.Contains("/")) //如果有父级 { try { var parent = GameObject.Find(field.value.Substring(0, field.value.IndexOf('/'))); //寻找父物体 try { var newPath = field.value.Substring(field.value.IndexOf('/') + 1); go = parent.transform .Find(newPath.Substring(0, newPath.LastIndexOf('.'))) .gameObject; } catch (Exception exx) { Log.Print(exx); } } catch { Log.PrintError($"{field.value}对象被隐藏或不存在,无法获取,已跳过"); continue; } } else { Log.PrintError($"{field.value}对象被隐藏或不存在,无法获取,已跳过"); continue; } } } catch (Exception ex) { if (field.value.Contains("/")) //如果有父级 { try { var parent = GameObject.Find(field.value.Substring(0, field.value.IndexOf('/'))); //寻找父物体 try { var newPath = field.value.Substring(field.value.IndexOf('/') + 1); go = parent.transform .Find(newPath.Substring(0, newPath.LastIndexOf('.'))) .gameObject; } catch (Exception exx) { Log.Print(exx); } } catch { Log.PrintError($"{field.value}对象被隐藏或不存在,无法获取,已跳过"); continue; } } else { Log.PrintError($"{field.value}对象被隐藏或不存在,无法获取,已跳过"); continue; } } foreach (var component in go.GetComponents <Component>()) { string scriptName = field.value.Substring(field.value.LastIndexOf('.')); if (component.GetType() == typeof(MonoBehaviourAdapter.Adaptor)) { MonoBehaviourAdapter.Adaptor c = (MonoBehaviourAdapter.Adaptor)component; if (c.ILInstance.Type.ToString().Contains(scriptName)) { obj = c.ILInstance; _class.BoundData = true; break; } } if (component.GetType().ToString().Contains(scriptName)) { obj = component; _class.BoundData = true; break; } } } else if (field.fieldType == _ClassField.FieldType.HotUpdateResource) { obj = Assets.LoadAsset(field.value, typeof(UnityEngine.Object)).asset; _class.BoundData = true; } } catch (Exception except) { Log.PrintError($"{classType}.{field.fieldName}获取值{field.value}出错:{except.Message},已跳过"); } //如果有数据再绑定 if (_class.BoundData) { if (t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static).Contains(t.GetField(field.fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))) { try { t.GetField(field.fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static).SetValue(clrInstance.ILInstance, obj); } catch (Exception e) { Log.PrintError($"{classType}.{field.fieldName}赋值出错:{e.Message},已跳过"); } } else { Log.PrintError($"{classType}不存在{field.fieldName},已跳过"); } } } } //是否激活 if (_class.ActiveAfter) { if (_class.BoundData == false && _class.RequireBindFields) { Log.PrintError($"{classType}没有成功绑定数据,无法自动激活,请手动!"); continue; } clrInstance.enabled = true; clrInstance.Awake(); } Destroy(cb); } }