Ejemplo n.º 1
0
        //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();
            }
        }