Пример #1
0
        private object  ConvertValue(string path, Type type, object value)
        {
            ClientClass gc = value as ClientClass;

            if (gc != null)
            {
                IncompleteGameObjectException incompleteEx = null;
                object result = Activator.CreateInstance(type);

                for (int j = 0; j < gc.fields.Length; j++)
                {
                    FieldInfo subField = type.GetField(gc.fields[j].name);
                    InternalNGDebug.Assert(subField != null, "Field \"" + gc.fields[j].name + "\" was not found in type \"" + type + "\".");

                    if (subField.IsLiteral == true)
                    {
                        continue;
                    }

                    try
                    {
                        result = this.SetValue(path + "." + gc.fields[j].name, result, gc.fields[j].name, gc.fields[j].value);
                    }
                    catch (IncompleteGameObjectException ex)
                    {
                        if (incompleteEx == null)
                        {
                            incompleteEx = ex;
                        }
                        else
                        {
                            incompleteEx.Aggregate(ex);
                        }
                    }
                }

                if (incompleteEx != null)
                {
                    throw incompleteEx;
                }

                return(result);
            }

            ArrayData a = value as ArrayData;

            if (a != null)
            {
                if (a.array != null)
                {
                    if (type.IsArray == true)
                    {
                        IncompleteGameObjectException incompleteEx = null;
                        Type  subType = Utility.GetArraySubType(type);
                        Array result  = Array.CreateInstance(type, a.array.Length);

                        for (int j = 0; j < a.array.Length; j++)
                        {
                            try
                            {
                                result.SetValue(this.ConvertValue(path + "#" + j, subType, a.array.GetValue(j)), j);
                            }
                            catch (IncompleteGameObjectException ex)
                            {
                                if (incompleteEx == null)
                                {
                                    incompleteEx = ex;
                                }
                                else
                                {
                                    incompleteEx.Aggregate(ex);
                                }
                            }
                        }

                        if (incompleteEx != null)
                        {
                            throw incompleteEx;
                        }

                        return(result);
                    }
                    else if (typeof(IList).IsAssignableFrom(type) == true)
                    {
                        IncompleteGameObjectException incompleteEx = null;
                        IList result  = Activator.CreateInstance(type) as IList;
                        Type  subType = Utility.GetArraySubType(type);

                        for (int j = 0; j < a.array.Length; j++)
                        {
                            try
                            {
                                result.Add(this.ConvertValue(path + "#" + j, subType, a.array.GetValue(j)));
                            }
                            catch (IncompleteGameObjectException ex)
                            {
                                if (incompleteEx == null)
                                {
                                    incompleteEx = ex;
                                }
                                else
                                {
                                    incompleteEx.Aggregate(ex);
                                }
                            }
                        }

                        if (incompleteEx != null)
                        {
                            throw incompleteEx;
                        }

                        return(result);
                    }
                    else
                    {
                        throw new InvalidCastException("Type \"" + type + "\" is not supported as an array.");
                    }
                }

                return(null);
            }

            UnityObject uo = value as UnityObject;

            if (uo != null)
            {
                return(this.TryFetchFromProject(path, uo));
            }

            return(null);
        }
Пример #2
0
        private object  SetValue(string path, object instance, string name, object value)
        {
            IFieldModifier field = InnerUtility.GetFieldInfo(instance.GetType(), name);

            InternalNGDebug.Assert(field != null, "Field \"" + name + "\" was not found in type \"" + instance.GetType() + "\".");
            object fieldValue = field.GetValue(instance);

            UnityObject uo = value as UnityObject;

            if (uo != null)
            {
                field.SetValue(instance, this.TryFetchFromProject(path, uo));
                return(instance);
            }

            ClientClass gc = value as ClientClass;

            if (gc != null)
            {
                if (fieldValue == null)
                {
                    fieldValue = Activator.CreateInstance(field.Type);
                }

                IncompleteGameObjectException incompleteEx = null;

                for (int j = 0; j < gc.fields.Length; j++)
                {
                    FieldInfo subField = field.Type.GetField(gc.fields[j].name);
                    InternalNGDebug.Assert(subField != null, "Field \"" + gc.fields[j].name + "\" was not found in type \"" + field.Type + "\".");

                    if (subField.IsLiteral == true)
                    {
                        continue;
                    }

                    try
                    {
                        fieldValue = this.SetValue(path + '.' + gc.fields[j].name, fieldValue, gc.fields[j].name, gc.fields[j].value);
                    }
                    catch (IncompleteGameObjectException ex)
                    {
                        if (incompleteEx == null)
                        {
                            incompleteEx = ex;
                        }
                        else
                        {
                            incompleteEx.Aggregate(ex);
                        }
                    }
                }

                if (incompleteEx != null)
                {
                    throw incompleteEx;
                }

                field.SetValue(instance, fieldValue);

                return(instance);
            }

            ArrayData a = value as ArrayData;

            if (a != null)
            {
                if (a.array != null)
                {
                    Type subType = Utility.GetArraySubType(field.Type);
                    if (fieldValue == null)
                    {
                        fieldValue = Array.CreateInstance(subType, a.array.Length);
                    }

                    if (field.Type.IsArray == true)
                    {
                        IncompleteGameObjectException incompleteEx = null;
                        Array fieldArray = fieldValue as Array;

                        if (fieldArray.Length != a.array.Length)
                        {
                            fieldValue = Array.CreateInstance(subType, a.array.Length);
                            fieldArray = fieldValue as Array;
                        }

                        for (int j = 0; j < a.array.Length; j++)
                        {
                            try
                            {
                                fieldArray.SetValue(this.ConvertValue(path + "#" + j, subType, a.array.GetValue(j)), j);
                            }
                            catch (IncompleteGameObjectException ex)
                            {
                                if (incompleteEx == null)
                                {
                                    incompleteEx = ex;
                                }
                                else
                                {
                                    incompleteEx.Aggregate(ex);
                                }
                            }
                        }

                        if (incompleteEx != null)
                        {
                            throw incompleteEx;
                        }
                    }
                    else if (typeof(IList).IsAssignableFrom(field.Type) == true)
                    {
                        IncompleteGameObjectException incompleteEx = null;
                        IList fieldArray = fieldValue as IList;

                        for (int j = 0; j < a.array.Length; j++)
                        {
                            try
                            {
                                fieldArray[j] = this.ConvertValue(path + "#" + j, subType, a.array.GetValue(j));
                            }
                            catch (IncompleteGameObjectException ex)
                            {
                                if (incompleteEx == null)
                                {
                                    incompleteEx = ex;
                                }
                                else
                                {
                                    incompleteEx.Aggregate(ex);
                                }
                            }
                        }

                        if (incompleteEx != null)
                        {
                            throw incompleteEx;
                        }
                    }
                    else
                    {
                        throw new InvalidCastException("Type \"" + field.Type + "\" is not supported as an array.");
                    }

                    field.SetValue(instance, fieldValue);
                }

                return(instance);
            }

            EnumInstance e = value as EnumInstance;

            if (e != null)
            {
                field.SetValue(instance, e.value);
            }
            else
            {
                field.SetValue(instance, value);
            }

            return(instance);
        }
Пример #3
0
        public bool             CopyComponentToGameObjectAndClipboard(GameObject go)
        {
            GameObject newGO = null;

            if (go == null)
            {
                go = newGO = new GameObject();
            }

            try
            {
                IncompleteGameObjectException incompleteEx = null;
                Component c;

                go.GetComponents(this.type, ClientComponent.reuseComponents);

                int componentIndex = 0;

                if (newGO != null)
                {
                    c = go.AddComponent(this.type);
                }
                else
                {
                    for (int i = 0; i < this.parent.components.Count; i++)
                    {
                        if (this.parent.components[i].type == this.type)
                        {
                            if (this.parent.components[i] == this)
                            {
                                break;
                            }

                            ++componentIndex;
                        }
                    }

                    if (ClientComponent.reuseComponents.Count <= componentIndex)
                    {
                        c = go.AddComponent(this.type);
                    }
                    else
                    {
                        c = ClientComponent.reuseComponents[componentIndex];
                    }
                }

                for (int i = 0; i < this.fields.Length; i++)
                {
                    try
                    {
                        this.SetValue(c.GetType().FullName + "#" + componentIndex + "." + this.fields[i].name, c, this.fields[i].name, this.fields[i].value);
                    }
                    catch (IncompleteGameObjectException ex)
                    {
                        if (incompleteEx == null)
                        {
                            incompleteEx = ex;
                        }
                        else
                        {
                            incompleteEx.Aggregate(ex);
                        }
                    }
                }

                if (incompleteEx != null)
                {
                    throw incompleteEx;
                }

                if (ComponentUtility.CopyComponent(c) == false)
                {
                    Debug.LogError("Copy component failed.");
                }
                else
                {
                    return(true);
                }
            }
            catch (IncompleteGameObjectException)
            {
                throw;
            }
            catch (Exception ex)
            {
                InternalNGDebug.LogException("Copy component failed.", ex);
            }
            finally
            {
                if (newGO != null)
                {
                    GameObject.DestroyImmediate(newGO);
                }
            }

            return(false);
        }