예제 #1
0
        private void ReadComponents(ES3Reader reader, GameObject go)
        {
            if (reader.StartReadCollection())
            {
                return;
            }

            var components = new List <Component>(go.GetComponents <Component>());

            // Read each Component in Components array
            while (true)
            {
                if (!reader.StartReadCollectionItem())
                {
                    break;
                }

                if (reader.StartReadObject())
                {
                    return;
                }

                Type type = null;

                string propertyName;
                while (true)
                {
                    propertyName = ReadPropertyName(reader);

                    if (propertyName == ES3Type.typeFieldName)
                    {
                        type = reader.ReadType();
                    }
                    else if (propertyName == ES3ReferenceMgrBase.referencePropertyName)
                    {
                        if (type == null)
                        {
                            throw new InvalidOperationException("Cannot load Component because no type data has been stored with it, so it's not possible to determine it's type");
                        }

                        var componentRef = reader.Read_ref();

                        // Rather than loading by reference, load using the Components list.
                        var c = components.Find(x => x.GetType() == type);
                        // If the Component exists in the Component list, load into it and remove it from the list.
                        if (c != null)
                        {
                            if (ES3ReferenceMgrBase.Current != null)
                            {
                                ES3ReferenceMgrBase.Current.Add(c, componentRef);
                            }

                            ES3TypeMgr.GetOrCreateES3Type(type).ReadInto <Component>(reader, c);
                            components.Remove(c);
                        }
                        // Else, create a new Component.
                        else
                        {
                            var component = ES3TypeMgr.GetOrCreateES3Type(type).Read <Component>(reader);
                            if (component != null)
                            {
                                ES3ReferenceMgrBase.Current.Add((Component)component, componentRef);
                            }
                        }
                        break;
                    }
                    else if (propertyName == null)
                    {
                        break;
                    }
                    else
                    {
                        reader.overridePropertiesName = propertyName;
                        ReadObject <Component>(reader);
                        break;
                    }
                }

                reader.EndReadObject();

                if (reader.EndReadCollectionItem())
                {
                    break;
                }
            }

            reader.EndReadCollection();
        }