Ejemplo n.º 1
0
        private static Instance Reflect_BIN(ClassDescriptor classDesc, Type objType)
        {
            if (!typeof(Instance).IsAssignableFrom(objType))
            {
                throw new Exception("'T' must be an Instance.");
            }

            Instance obj = (Instance)Activator.CreateInstance(objType);

            foreach (PropertyDescriptor prop in classDesc.Properties)
            {
                string    propertyName = prop.Name.Replace(" ", "_");
                FieldInfo field        = objType.GetField(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);

                if (field != null)
                {
                    BinaryPropertyFormat format = prop.Format;
                    object value = null;

                    switch (format)
                    {
                    // In most use cases, we can just set the value directly.
                    case BinaryPropertyFormat.Float:
                    case BinaryPropertyFormat.Double:
                    case BinaryPropertyFormat.Bool:
                    case BinaryPropertyFormat.Int:
                    case BinaryPropertyFormat.Int64:
                        value = prop.Value;
                        break;

                    // In some cases however, we need to handle it directly.
                    case BinaryPropertyFormat.Vector3:
                        float[] xyz = prop.Value as float[];
                        value = new Vector3(xyz);
                        break;

                    case BinaryPropertyFormat.CFrame:
                        float[] components = prop.Value as float[];
                        value = CFrame.FromComponents(components);
                        break;

                    case BinaryPropertyFormat.Enum:
                        Type fieldType = field.FieldType;
                        if (fieldType.IsEnum)
                        {
                            uint enumIndex = (uint)prop.Value;
                            int  index     = (int)enumIndex;
                            value = Enum.ToObject(fieldType, index);
                        }
                        break;

                    case BinaryPropertyFormat.String:
                        value = prop.Value.ToString();
                        break;

                    default:
                        break;
                    }

                    if (value != null)
                    {
                        field.SetValue(obj, value);
                    }
                }
            }

            foreach (ClassDescriptor child in classDesc.Children)
            {
                Type classType = GetClassType(child.ClassName);
                if (classType != null)
                {
                    Instance childObj = Reflect_BIN(child, classType);
                    childObj.Parent = obj;
                }
            }

            return(obj);
        }