Exemple #1
0
 internal static XDRPCArgumentInfo GenerateArgumentInfo(
     Type t,
     object o,
     ArgumentType at)
 {
     return(XDRPCMarshaler.GenerateArgumentInfo(t, o, at, 0));
 }
Exemple #2
0
 private static XDRPCArgumentInfo[] GenerateArgumentInfoArray(
     params object[] args)
 {
     XDRPCArgumentInfo[] xdrpcArgumentInfoArray = new XDRPCArgumentInfo[args.Length];
     for (int idx = 0; idx < args.Length; ++idx)
     {
         object o = args[idx];
         if (o == null)
         {
             xdrpcArgumentInfoArray[idx] = (XDRPCArgumentInfo) new XDRPCNullArgumentInfo();
         }
         else
         {
             Type type = o.GetType();
             if (typeof(XDRPCArgumentInfo).IsAssignableFrom(type))
             {
                 xdrpcArgumentInfoArray[idx] = (XDRPCArgumentInfo)o;
             }
             else
             {
                 if (!XDRPCMarshaler.IsValidArgumentType(type))
                 {
                     throw new XDRPCInvalidArgumentTypeException(type, idx);
                 }
                 xdrpcArgumentInfoArray[idx] = XDRPCMarshaler.GenerateArgumentInfo(type, o);
             }
         }
     }
     return(xdrpcArgumentInfoArray);
 }
        private void ConstructArgumentInfoArray()
        {
            Type  elementType = typeof(T).GetElementType();
            Array array       = (object)this.Value as Array;

            this._arrayElementData = new XDRPCArrayArgumentInfo <T> .ArrayElementData[this.MaxArrayLength];
            int index           = 0;
            int referenceOffset = 0;

            for (; index < this._arrayLength; ++index)
            {
                XDRPCArgumentInfo argumentInfo = XDRPCMarshaler.GenerateArgumentInfo(elementType, array.GetValue(index), ArgumentType.ByRef);
                this._arrayElementData[index] = this.GenerateArrayBufferData(argumentInfo, ref referenceOffset);
            }
            for (; index < this.MaxArrayLength; ++index)
            {
                XDRPCArgumentInfo argumentInfo = XDRPCMarshaler.GenerateArgumentInfo(elementType, Activator.CreateInstance(elementType), ArgumentType.ByRef);
                this._arrayElementData[index] = this.GenerateArrayBufferData(argumentInfo, ref referenceOffset);
            }
            if (this.MaxArrayLength > 0)
            {
                this._referenceSize = referenceOffset;
            }
            else
            {
                this._referenceSize = 0;
            }
        }
Exemple #4
0
 internal static XDRPCArgumentInfo GenerateArgumentInfo(
     Type t,
     object o,
     ArgumentType at,
     int size)
 {
     return(XDRPCMarshaler.GenerateArgumentInfo(t, o, at, size, Encoding.ASCII));
 }
Exemple #5
0
        public void Set <T>(T data)
        {
            this.ValidatePointer(nameof(Set));
            Type type = typeof(T);

            this.ValidateType(type, nameof(Set));
            int arrayElementCount = this.GetArrayElementCount(type);

            this.Set(XDRPCMarshaler.GenerateArgumentInfo(type, (object)data, ArgumentType.ByRef, arrayElementCount));
        }
Exemple #6
0
        public T Get <T>()
        {
            this.ValidatePointer(nameof(Get));
            Type type = typeof(T);

            this.ValidateType(type, nameof(Get));
            int arrayElementCount          = this.GetArrayElementCount(type);
            XDRPCArgumentInfo argumentInfo = XDRPCMarshaler.GenerateArgumentInfo(type, (object)default(T), ArgumentType.Out, arrayElementCount);

            this.Get(argumentInfo);
            return((T)XDRPCMarshaler.GetArgumentInfoValue(type, argumentInfo));
        }
        public static XDRPCReference AllocateRPC <T>(
            this IXboxConsole console,
            XDRPCMode mode)
            where T : struct
        {
            Type t = typeof(T);

            if (!XDRPCMarshaler.IsValidArgumentType(t))
            {
                throw new XDRPCInvalidTypeException(t, string.Format("Invalid type {0}: Cannot allocate type not supported by XDRPC.", (object)t.Name));
            }
            XDRPCArgumentInfo argumentInfo = XDRPCMarshaler.GenerateArgumentInfo(t, (object)default(T), ArgumentType.ByRef);

            return(console.AllocateRPC(argumentInfo, mode));
        }
Exemple #8
0
 internal static XDRPCArgumentInfo GenerateArgumentInfo(Type t, object o)
 {
     return(XDRPCMarshaler.GenerateArgumentInfo(t, o, ArgumentType.ByValue));
 }
Exemple #9
0
        private int PopulateStructBufferRecursive(
            Type structType,
            object structToMarshal,
            List <XDRPCStructArgumentInfo <T> .StructBufferData> bufferDataList,
            int depth,
            string unionWritingField)
        {
            if (depth > 3)
            {
                throw new XDRPCInvalidTypeException(structType, string.Format("Struct of type {0} has too many nested structs (more than {1} deep) which is not supported.", (object)typeof(T).Name, (object)3));
            }
            if (!((IEnumerable <Type>)XDRPCStructArgumentInfo <T> .builtInStructAllowlist).Contains <Type>(structType))
            {
                object[] customAttributes = structType.GetCustomAttributes(typeof(XDRPCStructAttribute), false);
                if (customAttributes == null || customAttributes.Length == 0)
                {
                    throw new XDRPCInvalidTypeException(structType, string.Format("Struct of type {0} doesn't have the XDRPCStruct attribute.", (object)structType.Name));
                }
            }
            FieldInfo[] fields        = structType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            int         packAttribute = structType.StructLayoutAttribute.Value == LayoutKind.Sequential ? 8 : structType.StructLayoutAttribute.Pack;
            int         num1          = 0;
            int         num2          = 0;
            bool        flag1         = false;

            foreach (FieldInfo fieldInfo in fields)
            {
                Type fieldType = fieldInfo.FieldType;
                MarshalAsAttribute marshalAsAttribute = (MarshalAsAttribute)null;
                XDRPCStructArgumentInfo <T> .StructBufferData data = new XDRPCStructArgumentInfo <T> .StructBufferData();

                object[] customAttributes1 = fieldInfo.GetCustomAttributes(typeof(MarshalAsAttribute), false);
                if (customAttributes1 != null && customAttributes1.Length != 0)
                {
                    marshalAsAttribute = (MarshalAsAttribute)customAttributes1[0];
                }
                if (fieldType == typeof(string) || fieldType.IsArray)
                {
                    if (marshalAsAttribute == null)
                    {
                        throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} of type {1} in struct type {2} doesn't have the required MarshalAsAttribute.", (object)fieldInfo.Name, (object)fieldType.Name, (object)structType.Name));
                    }
                    if (marshalAsAttribute.Value != UnmanagedType.ByValArray && marshalAsAttribute.Value != UnmanagedType.LPArray)
                    {
                        throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} of type {1} in struct type {2} with its MarshalAs attribute not using the required UnmanagedType.ByValArray or UnmanagedType.LPArray.", (object)fieldInfo.Name, (object)fieldType.Name, (object)structType.Name));
                    }
                    bool flag2 = marshalAsAttribute.Value == UnmanagedType.LPArray;
                    int  size  = marshalAsAttribute.SizeConst;
                    int  num3;
                    int  cMax;
                    if (fieldType.IsArray)
                    {
                        if (fieldType.GetArrayRank() > 1)
                        {
                            throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is a multidimensional array which is not supported by XDRPCStructArgumentInfo.", (object)fieldInfo.Name, (object)structType.Name));
                        }
                        Type elementType = fieldType.GetElementType();
                        if (!XDRPCMarshaler.IsValidValueType(elementType))
                        {
                            throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is an array of type {2} which is not supported by XDRPCStructArgumentInfo.", (object)fieldInfo.Name, (object)structType.Name, (object)elementType.Name));
                        }
                        int num4 = MarshalingUtils.SizeOf(elementType);
                        if (num4 == 0)
                        {
                            throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is an array of type {2} which is not supported by XDRPCStructArgumentInfo.", (object)fieldInfo.Name, (object)structType.Name, (object)elementType.Name));
                        }
                        num3 = num4;
                        if (XDRPCMarshaler.IsValidStructType(elementType))
                        {
                            num3 = MarshalingUtils.AlignmentOf(elementType);
                        }
                        Array array = (Array)fieldInfo.GetValue(structToMarshal);
                        if (flag2)
                        {
                            if (array != null && size < array.Length)
                            {
                                size = array.Length;
                            }
                        }
                        else if (size == 0)
                        {
                            throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is an array being passed by value that doesn't have the required SizeConst part of the attribute defined.", (object)fieldInfo.Name, (object)structType.Name));
                        }
                        cMax      = num4 * size;
                        data.Info = XDRPCMarshaler.GenerateArgumentInfo(fieldType, (object)array, ArgumentType.ByRef, size);
                    }
                    else
                    {
                        if (!XDRPCStructArgumentInfo <T> .EncodingMap.ContainsKey(marshalAsAttribute.ArraySubType))
                        {
                            throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is a string with a MarshalAs attribute without a string UnmanagedType for its ArraySubType.", (object)fieldInfo.Name, (object)structType.Name));
                        }
                        Encoding encoding = XDRPCStructArgumentInfo <T> .EncodingMap[marshalAsAttribute.ArraySubType];
                        int      num4     = encoding == Encoding.Unicode ? 2 : 1;
                        num3 = num4;
                        cMax = size * num4;
                        string v = (string)fieldInfo.GetValue(structToMarshal);
                        if (flag2)
                        {
                            int num5 = 0;
                            if (v != null)
                            {
                                num5 = encoding.GetByteCount(v + "\0");
                            }
                            if (cMax < num5)
                            {
                                cMax = num5;
                            }
                        }
                        else if (cMax == 0)
                        {
                            throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is a string being passed by value that doesn't have the required SizeConst part of the attribute defined.", (object)fieldInfo.Name, (object)structType.Name));
                        }
                        data.Info = (XDRPCArgumentInfo) new XDRPCStringArgumentInfo(v, encoding, ArgumentType.ByRef, cMax, CountType.Byte);
                    }
                    int requiredReferenceSize = data.Info.GetRequiredReferenceSize();
                    if (requiredReferenceSize > 0)
                    {
                        XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = new XDRPCStructArgumentInfo <T> .StructBufferData();

                        structBufferData.BufferOffset   = this.GetCurrentOffset(this._referenceBufferDataList, 8);
                        structBufferData.BufferSize     = requiredReferenceSize;
                        structBufferData.ReferenceIndex = -1;
                        structBufferData.Info           = (XDRPCArgumentInfo)null;
                        data.ReferenceIndex             = this._referenceBufferDataList.Count;
                        this._referenceBufferDataList.Add(structBufferData);
                    }
                    else
                    {
                        data.ReferenceIndex = -1;
                    }
                    data.BufferSize = cMax;
                    if (flag2)
                    {
                        if (cMax > 0)
                        {
                            XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = data;
                            structBufferData.BufferOffset = this.GetCurrentOffset(this._referenceBufferDataList, num3);
                            this.FillPointerBufferData(out data, bufferDataList, packAttribute);
                            this._referenceBufferDataList.Add(structBufferData);
                        }
                        else
                        {
                            this.FillPointerBufferData(out data, bufferDataList, packAttribute);
                            data.Info           = (XDRPCArgumentInfo) new XDRPCArgumentInfo <uint>(0U, ArgumentType.ByRef);
                            data.ReferenceIndex = -1;
                        }
                        num3 = data.BufferSize;
                    }
                    else
                    {
                        int packAlignment = this.CalculatePackAlignment(packAttribute, num3);
                        data.BufferOffset = this.GetCurrentOffset(bufferDataList, packAlignment);
                    }
                    num1 = Math.Max(num1, num3);
                    num2 = Math.Max(num2, data.BufferSize);
                    if (this.FillUnionBufferData(ref data, fieldInfo, unionWritingField))
                    {
                        flag1 = true;
                    }
                    bufferDataList.Add(data);
                }
                else if (fieldType.IsPrimitive)
                {
                    int num3 = MarshalingUtils.SizeOf(fieldType);
                    if (num3 == 0)
                    {
                        throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is primitive type {2} which is not supported by XDRPCStructArgumentInfo.", (object)fieldInfo.Name, (object)structType.Name, (object)fieldType.Name));
                    }
                    data.Info = XDRPCMarshaler.GenerateArgumentInfo(fieldType, fieldInfo.GetValue(structToMarshal), ArgumentType.ByRef);
                    if (marshalAsAttribute != null && marshalAsAttribute.Value == UnmanagedType.LPStruct)
                    {
                        XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = new XDRPCStructArgumentInfo <T> .StructBufferData();

                        structBufferData.Info           = data.Info;
                        structBufferData.BufferOffset   = this.GetCurrentOffset(this._referenceBufferDataList, num3);
                        structBufferData.BufferSize     = num3;
                        structBufferData.ReferenceIndex = -1;
                        this.FillPointerBufferData(out data, bufferDataList, packAttribute);
                        this._referenceBufferDataList.Add(structBufferData);
                    }
                    else
                    {
                        int packAlignment = this.CalculatePackAlignment(packAttribute, num3);
                        data.BufferOffset   = this.GetCurrentOffset(bufferDataList, packAlignment);
                        data.BufferSize     = num3;
                        data.ReferenceIndex = -1;
                    }
                    num1 = Math.Max(num1, data.BufferSize);
                    num2 = Math.Max(num2, data.BufferSize);
                    if (this.FillUnionBufferData(ref data, fieldInfo, unionWritingField))
                    {
                        flag1 = true;
                    }
                    bufferDataList.Add(data);
                }
                else
                {
                    if (!fieldType.IsValueType)
                    {
                        throw new XDRPCInvalidTypeException(fieldType, string.Format("Type {0} found in struct type {1} is not supported by XDRPCStructArgumentInfo.", (object)fieldType.Name, (object)structType.Name));
                    }
                    List <XDRPCStructArgumentInfo <T> .StructBufferData> bufferDataList1 = new List <XDRPCStructArgumentInfo <T> .StructBufferData>();
                    string   empty             = string.Empty;
                    object[] customAttributes2 = fieldInfo.GetCustomAttributes(typeof(XDRPCUnionAttribute), false);
                    if (customAttributes2 != null && customAttributes2.Length != 0)
                    {
                        empty = ((XDRPCUnionAttribute)customAttributes2[0]).Value;
                    }
                    int num3 = this.PopulateStructBufferRecursive(fieldType, fieldInfo.GetValue(structToMarshal), bufferDataList1, depth + 1, empty);
                    if (marshalAsAttribute != null && marshalAsAttribute.Value == UnmanagedType.LPStruct)
                    {
                        this.FillPointerBufferData(out data, bufferDataList, packAttribute);
                        if (this.FillUnionBufferData(ref data, fieldInfo, unionWritingField))
                        {
                            flag1 = true;
                        }
                        bufferDataList.Add(data);
                        int currentOffset = this.GetCurrentOffset(this._referenceBufferDataList, 8);
                        for (int index = 0; index < bufferDataList1.Count; ++index)
                        {
                            XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = bufferDataList1[index];
                            structBufferData.BufferOffset += currentOffset;
                            if (structBufferData.NextOffset != 0)
                            {
                                structBufferData.NextOffset += currentOffset;
                            }
                            this._referenceBufferDataList.Add(structBufferData);
                        }
                        num1 = data.BufferSize;
                    }
                    else
                    {
                        data.BufferOffset = this.GetCurrentOffset(bufferDataList, this.CalculatePackAlignment(packAttribute, num3));
                        if (this.FillUnionBufferData(ref data, fieldInfo, unionWritingField))
                        {
                            flag1 = true;
                        }
                        for (int index = 0; index < bufferDataList1.Count; ++index)
                        {
                            XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = bufferDataList1[index];
                            structBufferData.BufferOffset += data.BufferOffset;
                            if (structBufferData.NextOffset != 0)
                            {
                                data.BufferSize              = structBufferData.NextOffset;
                                structBufferData.NextOffset += data.BufferOffset;
                            }
                            if (data.Ignore)
                            {
                                structBufferData.Ignore = true;
                            }
                            bufferDataList.Add(structBufferData);
                        }
                    }
                    num1 = Math.Max(num1, num3);
                    num2 = Math.Max(num2, data.BufferSize);
                }
            }
            if (!string.IsNullOrEmpty(unionWritingField))
            {
                if (!flag1)
                {
                    throw new XDRPCInvalidTypeException(structType, string.Format("Struct of type {0} has the XDRPCUnion attribute with the non-existant field {1} specified, please provide a correct field name.", (object)structType.Name, (object)unionWritingField));
                }
                this.SetNextOffset(bufferDataList, num2);
            }
            int packAlignment1 = this.CalculatePackAlignment(packAttribute, num1);

            this.CalculateNextOffset(bufferDataList, packAlignment1);
            return(packAlignment1);
        }