예제 #1
0
        public object Unserial(Pointer _pointer, SerialAttribute _attribute, System.Xml.XmlNode _fieldNode, Dictionary <Pointer, string> _delayBindingTable)
        {
            // get list
            IList list = (IList)(_pointer.GetValue());

            if (list == null)
            {
                Type            fieldType       = Type.GetType(((XmlElement)_fieldNode).GetAttribute("typeinfo"));
                ConstructorInfo listConstructor = fieldType.GetConstructor(new Type[0]);
                Debug.Assert(listConstructor != null, "Cannot find valid constructor for list");
                list = (IList)(listConstructor.Invoke(new object[0]));
            }
            // get value type
            Type           valueType  = list.GetType().GetGenericArguments()[0];
            ISerializeType valueIType = Serialable.FindSuitableSerialType(valueType);

            int index = 0;

            foreach (XmlNode valueNode in _fieldNode.ChildNodes)
            {
                //XmlNode valueContentNode = valueNode.FirstChild;
                object valueObject = valueIType.Unserial(new Pointer(list, index), _attribute, valueNode, _delayBindingTable);
                if (valueObject != null)
                {
                    list.Add(valueObject);
                }
                ++index;
            }
            return(list);
        }
예제 #2
0
        public object Clone(Pointer _pointer, SerialAttribute _attribute, object _original, Dictionary <Pointer, string> _delayBindingTable)
        {
            // get list
            IList list = (IList)(_pointer.GetValue());

            if (list == null)
            {
                Type            fieldType       = _original.GetType();
                ConstructorInfo listConstructor = fieldType.GetConstructor(new Type[0]);
                Debug.Assert(listConstructor != null, "Connot find valid constructor for list");
                list = (IList)(listConstructor.Invoke(new object[0]));
            }
            // get value type
            Type           valueType    = list.GetType().GetGenericArguments()[0];
            ISerializeType valueIType   = Serialable.FindSuitableSerialType(valueType);
            IList          originalList = (IList)_original;

            IEnumerator it    = originalList.GetEnumerator();
            int         index = 0;

            while (it.MoveNext())
            {
                object valueObject = valueIType.Clone(new Pointer(list, index), _attribute, it.Current, _delayBindingTable);
                if (valueObject != null)
                {
                    list.Add(valueObject);
                }
                ++index;
            }
            return(list);
        }
예제 #3
0
 public XmlNode Serial(Object _object, SerialAttribute _attribute, XmlDocument _doc, string _nameField)
 {
     if (_object != null)
     {
         XmlElement root       = null;
         Serialable serialable = (Serialable)(_object);
         if (serialable == null)
         {
             return(null);
         }
         if (_attribute.GetIsReference() ==
             SerialAttribute.AttributePolicy.PolicyReference)
         {
             root = _doc.CreateElement(((Serialable)(_object)).GetThisType().Name);
             root.SetAttribute("value", serialable.GUID);
         }
         else if (_attribute.GetIsReference() ==
                  SerialAttribute.AttributePolicy.PolicyCopy)
         {
             root = (XmlElement)serialable.DoSerial(_doc);
         }
         root.SetAttribute("name", _nameField);
         return(root);
     }
     else
     {
         return(null);
     }
 }
예제 #4
0
        public Object Clone(Pointer _pointer, SerialAttribute _attribute, object _original, Dictionary <Pointer, string> _delayBindingTable)
        {
            // get dictionary
            IDictionary dictionary = (IDictionary)(_pointer.GetValue());

            if (dictionary == null)
            {
                Type            fieldType             = _original.GetType();
                ConstructorInfo dictionaryConstructor = fieldType.GetConstructor(new Type[0]);
                Debug.Assert(dictionaryConstructor != null, "Cannot find valid constructor for dictionary");
                dictionary = (IDictionary)(dictionaryConstructor.Invoke(new object[0]));
            }
            // get key value type
            Type[]         keyValueType       = dictionary.GetType().GetGenericArguments();
            ISerializeType keyIType           = Serialable.FindSuitableSerialType(keyValueType[0]);
            ISerializeType valueIType         = Serialable.FindSuitableSerialType(keyValueType[1]);
            IDictionary    orignialDictionary = (IDictionary)_original;

            IDictionaryEnumerator it = orignialDictionary.GetEnumerator();

            while (it.MoveNext())
            {
                object keyObject   = it.Key;
                object valueObject = valueIType.Clone(new Pointer(dictionary, keyObject), _attribute, it.Value, _delayBindingTable);
                if (valueObject != null)
                {
                    dictionary.Add(keyObject, valueObject);
                }
            }
            return(dictionary);
        }
예제 #5
0
        public System.Xml.XmlNode Serial(object _object, SerialAttribute _attribute, System.Xml.XmlDocument _doc, string _nameField)
        {
            if (_object != null)
            {
                IList          list       = (IList)(_object);
                Type           valueType  = _object.GetType().GetGenericArguments()[0];
                ISerializeType valueIType = Serialable.FindSuitableSerialType(valueType);

                XmlElement root = _doc.CreateElement(typeof(IList).ToString());
                root.SetAttribute("name", _nameField);
                root.SetAttribute("typeinfo", _object.GetType().ToString());

                IEnumerator it = list.GetEnumerator();
                while (it.MoveNext())
                {
//                     XmlNode valueNodeHead = _doc.CreateElement("Value");
//                     root.AppendChild(valueNodeHead);
                    XmlNode valueNode = valueIType.Serial(it.Current, _attribute, _doc, "");
                    root.AppendChild(valueNode);
                }
                return(root);
            }
            else
            {
                return(null);
            }
        }
예제 #6
0
        // load single prefab file
        private void LoadPrefab(string _filepath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(_filepath);
            XmlNode nodePrefab = doc.SelectSingleNode("Prefab");

            // load gameobjects and build relationships
            /*Dictionary<string, GameObject> tempList = new Dictionary<string, GameObject>();*/
            // LoadFromNode
            List <GameObject> gameObjects = new List <GameObject>();

            Serialable.BeginSupportingDelayBinding();
            foreach (XmlNode nodeGameObject in nodePrefab.ChildNodes)
            {
                GameObject newGameObject = GameObject.DoUnserial(nodeGameObject) as GameObject;
                gameObjects.Add(newGameObject);
                /*GameObject newGameObject = GameObject.LoadFromNode(nodeGameObject, null);*/

                /*tempList.Add(newGameObject.GUID, newGameObject);*/
            }
            Serialable.EndSupportingDelayBinding();
            // add root to prefabeList
            foreach (GameObject gameObject in gameObjects)
            {
                if (gameObject.Parent == null)
                {
                    AddItem(gameObject.Name, gameObject);
                }
            }
        }
예제 #7
0
        public static GameObjectList LoadFromNode(XmlNode node, Scene scene)
        {
            GameObjectList gameObjectList = new GameObjectList();

            Serialable.BeginSupportingDelayBinding();
            foreach (XmlNode gameObject in node.ChildNodes)
            {
                GameObject newGameObject = (GameObject)(Serialable.DoUnserial(gameObject));
                gameObjectList.AddGameObject(newGameObject);
            }
            Serialable.EndSupportingDelayBinding();
            return(gameObjectList);
        }
예제 #8
0
        protected override void PostClone(Serialable _object)
        {
            // clone children
            GameObject prototype = _object as GameObject;

            if (prototype.Children != null)
            {
                foreach (GameObject child in prototype.Children)
                {
                    GameObject cloneChild = child.DoClone() as GameObject;
                    cloneChild.m_parent = this;
                }
            }
            SetComponentsReference();
        }
예제 #9
0
 public Object Clone(Pointer _pointer, SerialAttribute _attribute, object _original, Dictionary <Pointer, string> _delayBindingTable)
 {
     if (_attribute.GetIsCloneReference() ==
         SerialAttribute.AttributePolicy.PolicyReference)
     {
         _delayBindingTable.Add(_pointer, ((Serialable)_original).GUID);
         return(null);
     }
     else if (_attribute.GetIsCloneReference() ==
              SerialAttribute.AttributePolicy.PolicyCopy)
     {
         Serialable obj = ((Serialable)_original).DoClone();
         return(obj);
     }
     return(null);
 }
예제 #10
0
 public Object Unserial(Pointer _pointer, SerialAttribute _attribute, XmlNode _fieldNode, Dictionary <Pointer, string> _delayBindingTable)
 {
     if (_attribute.GetIsReference() ==
         SerialAttribute.AttributePolicy.PolicyReference)
     {
         _delayBindingTable.Add(_pointer, ((XmlElement)_fieldNode).GetAttribute("value"));
         return(null);
     }
     else if (_attribute.GetIsReference() ==
              SerialAttribute.AttributePolicy.PolicyCopy)
     {
         Serialable obj = Serialable.DoUnserial(_fieldNode);
         return(obj);
     }
     return(null);
 }
예제 #11
0
        /**
         * @brief initialize the engine before starting game
         *
         * initialize() will be invoked automatically by XNA
         */
        protected override void Initialize()
        {
            base.Initialize();                                                  // it will evoke LoadContent()
            Serialable.InitializeSerializeTypeTable();
            InitSingleton();

            if (_gameEngineMode == GameEngineMode.MapEditor)
            {
                Editor.GameEngineStarted();
            }
            else if (_gameEngineMode == GameEngineMode.InGame &&
                     !m_passiveMode)
            {
                MountInGameProject();
            }
        }
예제 #12
0
        /**
         * @brief load bttree from file
         **/
        public static BTTree Load(string _filepath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(_filepath);

            BTTree newBTTree = new BTTree();

            XmlNode nodeBTTree   = doc.SelectSingleNode("BTTree");
            XmlNode nodeRoot     = nodeBTTree.SelectSingleNode("Root");
            XmlNode nodeRealRoot = nodeRoot.FirstChild;

            Serialable.BeginSupportingDelayBinding();
            newBTTree.m_root = Serialable.DoUnserial(nodeRealRoot) as BTNode;
            Serialable.EndSupportingDelayBinding(true);
            return(newBTTree);
        }
예제 #13
0
        private void Clone(Serialable _object)
        {
            Type objectType = GetThisType();

            // set property
            FieldInfo[] fields = objectType.GetFields(BindingFlags.Instance |
                                                      BindingFlags.NonPublic |
                                                      BindingFlags.Public);
            // scan field
            foreach (FieldInfo field in fields)
            {
                // get attributes
                SerialAttribute[] attributes =
                    (SerialAttribute[])(field.GetCustomAttributes(typeof(SerialAttribute), true));
                if (attributes.Length == 0)
                {
                    continue;
                }
                // get other
                string fieldName = field.Name;
                Type   fieldType = field.FieldType;

                // find a suitable unserializor to deal with it
                ISerializeType iserializer = FindSuitableSerialType(fieldType);
                if (iserializer != null)
                {
                    // the field may be null
                    Object fieldObject = field.GetValue(_object);
                    if (fieldObject != null)
                    {
                        Object resultObject = iserializer.Clone(
                            new Pointer(this, field),
                            attributes[0],
                            fieldObject,
                            m_delayBindingTable);
                        if (resultObject != null)
                        {
                            field.SetValue(this, resultObject);
                        }
                    }
                }
            }
        }
예제 #14
0
        public Serialable DoClone()
        {
            Type            type            = GetThisType();
            ConstructorInfo constructorInfo = type.GetConstructor(new Type[0]);

            Debug.Assert(constructorInfo != null, "Cannot find valid constructor for type: " + type.ToString());
            Serialable resObject = (Serialable)(constructorInfo.Invoke(new Object[0]));

            Debug.Assert(resObject != null, "Cannot instantiate object: " + type.ToString());

            resObject.m_delayBindingTable = new Dictionary <Pointer, string>();
            resObject.PreClone(this);
            resObject.Clone(this);
            // don't clone guid
            resObject.m_guid = Guid.NewGuid().ToString();
            resObject.PostClone(this);
            // add to guid table for delay binding request
            guidTable.Add(resObject.GUID, resObject);

            return(resObject);
        }
예제 #15
0
 /**
  * @brief core part of delay binding
  * */
 private void DelayBinding()
 {
     if (m_delayBindingTable == null)
     {
         return;
     }
     foreach (KeyValuePair <Pointer, string> keyValue in m_delayBindingTable)
     {
         Pointer field      = keyValue.Key;
         string  targetGuid = keyValue.Value;
         if (guidTable.ContainsKey(targetGuid))
         {
             Serialable target = guidTable[targetGuid];
             field.SetValue(target);
             //field.SetValue(this, target);
         }
     }
     PostDelayBinding();
     // clear
     m_delayBindingTable = null;
 }
예제 #16
0
        public XmlNode Serial(Object _object, SerialAttribute _attribute, XmlDocument _doc, string _nameField)
        {
            if (_object != null)
            {
                // get value <key, value>
                IDictionary    dictionary   = (IDictionary)(_object);
                Type[]         keyValueType = _object.GetType().GetGenericArguments();
                ISerializeType keyIType     = Serialable.FindSuitableSerialType(keyValueType[0]);
                ISerializeType valueIType   = Serialable.FindSuitableSerialType(keyValueType[1]);

                XmlElement root = _doc.CreateElement(typeof(IDictionary).ToString()); // FROM .Name
                root.SetAttribute("name", _nameField);
                root.SetAttribute("typeinfo", _object.GetType().ToString());

                IDictionaryEnumerator it = dictionary.GetEnumerator();
                while (it.MoveNext())
                {
                    XmlNode keyValueNode = _doc.CreateElement("KeyValue");
                    root.AppendChild(keyValueNode);
                    // key
                    XmlNode keyNodeHead = _doc.CreateElement("Key");
                    keyValueNode.AppendChild(keyNodeHead);
                    XmlNode keyNode = keyIType.Serial(it.Key, _attribute, _doc, "");
                    keyNodeHead.AppendChild(keyNode);
                    // value
                    XmlNode valueNodeHead = _doc.CreateElement("Value");
                    keyValueNode.AppendChild(valueNodeHead);
                    XmlNode valueNode = valueIType.Serial(it.Value, _attribute, _doc, "");
                    valueNodeHead.AppendChild(valueNode);
                }
                return(root);
            }
            else
            {
                return(null);
            }
        }
예제 #17
0
        public object Unserial(Pointer _pointer, SerialAttribute _attribute, XmlNode _fieldNode, Dictionary <Pointer, string> _delayBindingTable)
        {
            // get dictionary
            IDictionary dictionary = (IDictionary)(_pointer.GetValue());

            if (dictionary == null)
            {
                Type            fieldType             = Type.GetType(((XmlElement)_fieldNode).GetAttribute("typeinfo"));
                ConstructorInfo dictionaryConstructor = fieldType.GetConstructor(new Type[0]);
                Debug.Assert(dictionaryConstructor != null, "Cannot find valid constructor for dictionary");
                dictionary = (IDictionary)(dictionaryConstructor.Invoke(new object[0]));
            }
            // get key value type
            Type[]         keyValueType = dictionary.GetType().GetGenericArguments();
            ISerializeType keyIType     = Serialable.FindSuitableSerialType(keyValueType[0]);
            ISerializeType valueIType   = Serialable.FindSuitableSerialType(keyValueType[1]);

            foreach (XmlNode keyValueNode in _fieldNode.ChildNodes)
            {
                // foreach keyValue
                // key
                XmlNode keyNode        = keyValueNode.SelectSingleNode("Key");
                XmlNode keyContentNode = keyNode.FirstChild;
                // do not support delay binding for key
                object keyObject = keyIType.Unserial(null, _attribute, keyContentNode, _delayBindingTable);
                // value
                XmlNode valueNode        = keyValueNode.SelectSingleNode("Value");
                XmlNode valueContentNode = valueNode.FirstChild;
                object  valueObject      = valueIType.Unserial(new Pointer(dictionary, keyObject), _attribute, valueContentNode, _delayBindingTable);
                // insert into dictionary
                if (valueObject != null)
                {
                    dictionary.Add(keyObject, valueObject);
                }
            }
            return(dictionary);
        }
예제 #18
0
        /**
         * @brief Load models under the directory
         *
         * @param _modelDirectory the directory containing .model files
         * @param _project CatProject
         *
         * @result the CatModelList
         * */
        public static CatModelList LoadModels(string _modelDirectory, CatProject _project)
        {
            // create
            CatModelList modelList = new CatModelList();

            // search for .material files under _materialDirectory
            if (!Directory.Exists(_modelDirectory))
            {
                return(modelList);
            }
            string[] files = Directory.GetFiles(_modelDirectory, "*.model");
            foreach (string file in files)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(file);
                XmlNode nodeModel = doc.SelectSingleNode(
                    typeof(CatModel).ToString());
                Serialable.BeginSupportingDelayBinding();
                CatModel catsModel = CatModel.DoUnserial(nodeModel) as CatModel;
                Serialable.EndSupportingDelayBinding();
                modelList.AddModel(catsModel);
            }
            return(modelList);
        }
예제 #19
0
        /**
         * @brief unserilize a serialable class
         *
         * @param _node the XmlNode
         *
         * @result the Serialable object
         * */
        public static Serialable DoUnserial(XmlNode _node)
        {
            // instantiate
            string typeName   = _node.Name;
            Type   objectType = Type.GetType(typeName);

            if (objectType == null)
            {
                // check in type manager
                objectType = Mgr <TypeManager> .Singleton.GetCatComponentType(typeName);
            }
            if (objectType == null)
            {
                objectType = Mgr <TypeManager> .Singleton.GetBTTreeNodeType(typeName);
            }
            Debug.Assert(objectType != null, "Cannot find type: " + typeName);
            Debug.Assert(objectType.IsSubclassOf(typeof(Serialable)), "Instantiating non-serialiable type: " + typeName);

            if (null != objectType)
            {
                // instance a object
                ConstructorInfo constructorInfo = objectType.GetConstructor(new Type[0]);
                Debug.Assert(constructorInfo != null, "Cannot find valid constructor for type: " + typeName);
                Serialable resObject = (Serialable)(constructorInfo.Invoke(new Object[0]));
                Debug.Assert(resObject != null, "Cannot instantiate object: " + typeName);

                resObject.m_delayBindingTable = new Dictionary <Pointer, string>();
                resObject.PreUnserial(ref _node);
                resObject.Unserial(_node);
                resObject.PostUnserial(_node);
                // add to guid table for delay binding request
                guidTable.Add(resObject.GUID, resObject);
                return(resObject);
            }
            return(null);
        }
예제 #20
0
        /**
         * @brief create a scene from an XML file
         *
         * @param node the XML node
         * @param game the game engine
         *
         * @result scene
         * */
        public static Scene LoadScene(string _filename) // node is the current node
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(_filename);
            XmlNode node = doc.SelectSingleNode("Scene");

            Scene newScene = new Scene();

            // working list
            newScene._renderList = new RenderList();
            //newScene._colliderList = new ColliderList();
            newScene.m_physicsSystem = new PhysicsSystem();
            newScene.m_shadowSystem  = new ShadowSystem(Mgr <CatProject> .Singleton);
            newScene._renderList.SetShadowRender(newScene.m_shadowSystem);
            newScene._uiRenderer = new UIRenderer();
            newScene.m_physicsSystem.Initialize();

            if (Mgr <GameEngine> .Singleton._gameEngineMode == GameEngine.GameEngineMode.MapEditor)
            {
                newScene._debugDrawableList = new DebugDrawableList();
                newScene._debugDrawableList.AddItem(newScene);

                newScene._selectableList = new RepeatableList <ISelectable>();
            }
            // bind to current scene
            //Mgr<Scene>.Singleton = newScene;

            // init scene basic list
            // load and construct scene here
            XmlNode    basic      = node.SelectSingleNode("SceneBasic");
            XmlElement sceneBasic = (XmlElement)basic;

            newScene.setYAngle(float.Parse(sceneBasic.GetAttribute("viewAngle")));
            String playerGameObjectName = sceneBasic.GetAttribute("playerGameObjectName");

            XmlElement XBound = (XmlElement)sceneBasic.SelectSingleNode("XBound");

            newScene._XBound = new Vector2(float.Parse(XBound.GetAttribute("min")),
                                           float.Parse(XBound.GetAttribute("max")));

            XmlElement YBound = (XmlElement)sceneBasic.SelectSingleNode("YBound");

            newScene._YBound = new Vector2(float.Parse(YBound.GetAttribute("min")),
                                           float.Parse(YBound.GetAttribute("max")));

            XmlElement ZBound = (XmlElement)sceneBasic.SelectSingleNode("ZBound");

            newScene._ZBound = new Vector2(float.Parse(ZBound.GetAttribute("min")),
                                           float.Parse(ZBound.GetAttribute("max")));

            // camera
            XmlNode nodeCamera = node.SelectSingleNode(typeof(Camera).ToString());

            Serialable.BeginSupportingDelayBinding();
            Camera camera = Serialable.DoUnserial((XmlElement)nodeCamera) as Camera;

            Serialable.EndSupportingDelayBinding();
            //Mgr<Camera>.Singleton = camera;
            newScene.m_camera = camera;
            newScene.m_camera.UpdateView();
            newScene.m_camera.UpdateProjection();

            // postprocess manager
            XmlNode nodePostProcessManager =
                node.SelectSingleNode(typeof(PostProcessManager).ToString());

            Serialable.BeginSupportingDelayBinding();
            newScene.m_postProcessManager =
                Serialable.DoUnserial((XmlElement)nodePostProcessManager) as PostProcessManager;
            Serialable.EndSupportingDelayBinding();

            // gameObjects
            XmlNode gameObjects = node.SelectSingleNode("GameObjects");

            newScene._gameObjectList = GameObjectList.LoadFromNode(gameObjects, newScene);

            PostLoadScene(newScene);

            return(newScene);
        }
예제 #21
0
 virtual protected void PostClone(Serialable _object)
 {
 }