示例#1
0
        public static CompoundObject ReadFromFile(string fileName)
        // This method reads an xml file containing a CompoundObject
        // and returns with the generated CompoundObject.
        // All child objects with other XML-files are also read
        // (however it will not be possible to edit them without
        // explicitly open thems as separate files.
        // TODO: Error handling is non-existent.
        {
            string path = Path.GetDirectoryName(fileName);

            XmlSerializer  ser    = new XmlSerializer(typeof(CompoundObject));
            FileStream     fs     = new FileStream(fileName, FileMode.Open);
            XmlReader      reader = XmlReader.Create(fs);
            CompoundObject co     = (CompoundObject)ser.Deserialize(reader);

            // Iterate ChildObjects to load all child objects
            foreach (ChildObject cor in co.ChildObjects)
            {
                // Iterate all state properties
                foreach (TStateProperties <ChildObjectStateProperties> sp in cor.StateProperties)
                {
                    // Only read files if the child object is referenced from a
                    // separate file, otherwise the serialization will already have
                    // populated the child object.
                    if ((sp.Properties.File != "") && (sp.Properties.CompObj == null))
                    {
                        string newFile = System.IO.Path.Combine(path, sp.Properties.File);

                        CompoundObject childCo = CompoundObject.ReadFromFile(newFile);

                        sp.Properties.CompObj = childCo;
                    }
                }
            }

            // Iterate SpawnObjects of BreakableObject Behaviour properties to load all spawn objects
            if (co.Behaviour.Type == "breakableObject")
            {
                foreach (SpawnObject so in co.Behaviour.BreakableObjProps.SpawnObjects)
                {
                    ChildObject cor = so.MyChildObject;

                    // Iterate all state properties
                    foreach (TStateProperties <ChildObjectStateProperties> sp in cor.StateProperties)
                    {
                        // Only read files if the child object is referenced from a
                        // separate file, otherwise the serialization will already have
                        // populated the child object.
                        if ((sp.Properties.File != "") && (sp.Properties.CompObj == null))
                        {
                            string newFile = System.IO.Path.Combine(path, sp.Properties.File);

                            CompoundObject childCo = CompoundObject.ReadFromFile(newFile);

                            sp.Properties.CompObj = childCo;
                        }
                    }
                }
            }

            // Iterate ClippedWindows to load all each child objects
            foreach (ClippedWindowRef cwr in co.ClippedWindows)
            {
                // Iterate all state properties
                foreach (TStateProperties <ClippedWindowProperties> sp in cwr.StateProperties)
                {
                    string newFile = System.IO.Path.Combine(path, sp.Properties.SpaceSceneFile);

                    CompoundObject childCo = CompoundObject.ReadFromFile(newFile);

                    sp.Properties.CompObj = childCo;
                }
            }

            fs.Close();

            return(co);
        }
示例#2
0
        // Following is special cases for landing scenes. Maybe these will be specialised
        // xml files with specialised format.

        //[XmlElement("planetActor")]
        //public ObservableCollection<PlanetActorRef> PlanetActors
        //{
        //   get { return _planetActors; }
        //   set { _planetActors = value; }
        //}

        //// At serialisation of a parent CompoundObject this collection
        //// is read. Once read, the spaceSceneFile is used to create
        //// a child CompoundObject.
        //[XmlElement("clippedWindow")]
        //public ObservableCollection<ClippedWindowRef> ClippedWindows
        //{
        //   get { return _clippedWindows; }
        //   set { _clippedWindows = value; }
        //}

        #endregion

        #region Public Methods

        public void AnalyseForChildFiles(string path)
        {
            // Iterate ChildObjects to load all child objects
            foreach (ChildObject cor in ChildObjects)
            {
                // Only read files if the child object is referenced from a
                // separate file, otherwise the serialization will already have
                // populated the child object.
                if ((cor.File != "") && (cor.File != "undef_file.xml") && (cor.CompObj == null))
                {
                    string newFile = System.IO.Path.Combine(path, cor.File);

                    CompoundObject childCo = CompoundObject.ReadFromFile(newFile);

                    cor.CompObj = childCo;
                }
                else
                {
                    //...however, the child objects may have behaviours or systems that
                    // has spawn objects or child objects that need to be loaded
                    cor.CompObj.AnalyseForChildFiles(path);
                }
            }

            // Iterate SpawnObjects of BreakableObject Behaviour properties to load all spawn objects
            if (Behaviour.Type == "breakableObject")
            {
                foreach (SpawnObject so in Behaviour.BreakableObjProps.SpawnObjects)
                {
                    ChildObject cor = so.MyChildObject;

                    // Only read files if the child object is referenced from a
                    // separate file, otherwise the serialization will already have
                    // populated the child object.
                    if ((cor.File != "") && (cor.File != "undef_file.xml") && (cor.CompObj == null))
                    {
                        string newFile = System.IO.Path.Combine(path, cor.File);

                        CompoundObject childCo = CompoundObject.ReadFromFile(newFile);

                        cor.CompObj = childCo;
                    }
                }
            }


            // Iterate SpawnObjects of ObejctFactories in Systems.Propertiesto load all spawn objects
            foreach (CoSystem sys in Systems)
            {
                if (sys.Type == "objectFactory")
                {
                    foreach (SpawnObject so in sys.ObjFactStateProperties.SpawnObjects)
                    {
                        ChildObject cor = so.MyChildObject;

                        // Only read files if the child object is referenced from a
                        // separate file, otherwise the serialization will already have
                        // populated the child object.
                        if ((cor.File != "") && (cor.File != "undef_file.xml") && (cor.CompObj == null))
                        {
                            string newFile = System.IO.Path.Combine(path, cor.File);

                            CompoundObject childCo = CompoundObject.ReadFromFile(newFile);

                            cor.CompObj = childCo;
                        }
                    }
                }
            }

            //// Iterate ClippedWindows to load all each child objects
            //foreach (ClippedWindowRef cwr in ClippedWindows)
            //{
            //   // Iterate all state properties
            //   foreach (TStateProperties<ClippedWindowProperties> sp in cwr.StateProperties)
            //   {
            //      string newFile = System.IO.Path.Combine(path, sp.Properties.SpaceSceneFile);

            //      CompoundObject childCo = CompoundObject.ReadFromFile(newFile);

            //      sp.Properties.CompObj = childCo;
            //   }
            //}
        }