Beispiel #1
1
        IResource IResourceResolver.Resolve(Uri uri)
        {
            IResource resource = null;
            try
            {

                string fileName = uri.LocalPath;
                string ext = Path.GetExtension(fileName).ToLower();
                if (ext == m_ext)
                {
                    using (Stream stream = File.OpenRead(fileName))
                    {
                        var reader = new CustomDomXmlReader(Globals.ResourceRoot, m_schemaLoader);
                        DomNode node = reader.Read(stream, uri);                        
                        resource = Prefab.Create(node, uri);
                    }
                }
            }
            catch (System.IO.IOException e)
            {
                Outputs.WriteLine(OutputMessageType.Warning, "Could not load resource: " + e.Message);
            }

            return resource;
        }
Beispiel #2
0
        IResource IResourceResolver.Resolve(Uri uri)
        {
            IResource resource = null;

            try
            {
                string fileName = uri.LocalPath;
                string ext      = Path.GetExtension(fileName).ToLower();
                if (ext == m_ext)
                {
                    using (Stream stream = File.OpenRead(fileName))
                    {
                        var     reader = new CustomDomXmlReader(Globals.ResourceRoot, m_schemaLoader);
                        DomNode node   = reader.Read(stream, uri);
                        resource = Prefab.Create(node, uri);
                    }
                }
            }
            catch (System.IO.IOException e)
            {
                Outputs.WriteLine(OutputMessageType.Warning, "Could not load resource: " + e.Message);
            }

            return(resource);
        }
Beispiel #3
0
        /// <summary>
        /// Open or create new document.
        /// It opens if the file exist otherwise it will creates new document
        /// </summary>
        public static GameDocument OpenOrCreate(Uri uri, SchemaLoader schemaLoader)
        {
            if (!uri.IsAbsoluteUri)
            {
                return(null);
            }

            var docRegistry = Globals.MEFContainer.GetExportedValue <GameDocumentRegistry>();

            GameDocument document = docRegistry.FindDocument(uri) as GameDocument;

            if (document != null)
            {
                return(document);
            }

            string filePath = uri.LocalPath;

            DomNode rootNode = null;

            if (File.Exists(filePath))
            {
                // read existing document using custom dom XML reader
                using (FileStream stream = File.OpenRead(filePath))
                {
                    var reader = new CustomDomXmlReader(Globals.ResourceRoot, schemaLoader);
                    rootNode = reader.Read(stream, uri);
                }
            }
            else
            {
                // create new document by creating a Dom node of the root type defined by the schema
                rootNode = new DomNode(Schema.gameType.Type, Schema.gameRootElement);
                rootNode.SetAttribute(Schema.gameType.nameAttribute, "Game".Localize());
            }


            GameObjectFolder rootFolder = Adapters.As <GameObjectFolder>(rootNode.GetChild(Schema.gameType.gameObjectFolderChild));

            if (rootFolder == null)
            {
                // create the game object folder
                rootFolder      = (GameObjectFolder)GameObjectFolder.Create();
                rootFolder.Name = "GameObjects".Localize("this is the name of a folder in the project lister");
                rootNode.SetChild(Schema.gameType.gameObjectFolderChild, rootFolder.DomNode);
            }


            // create bookmarks
            DomNode bookmarks = rootNode.GetChild(Schema.gameType.bookmarksChild);

            if (bookmarks == null)
            {
                bookmarks = new DomNode(Schema.bookmarksType.Type);
                rootNode.SetChild(Schema.gameType.bookmarksChild, bookmarks);
            }

            DomNode layersNode = rootNode.GetChild(Schema.gameType.layersChild);

            if (layersNode == null)
            {
                layersNode = new DomNode(Schema.layersType.Type);
                rootNode.SetChild(Schema.gameType.layersChild, layersNode);
            }

            // Create the grid
            DomNode gridNode = rootNode.GetChild(Schema.gameType.gridChild);

            if (gridNode == null)
            {
                gridNode = new DomNode(Schema.gridType.Type);
                rootNode.SetChild(Schema.gameType.gridChild, gridNode);
            }

            document     = rootNode.As <GameDocument>();
            document.Uri = uri;

            // Initialize Dom extensions now that the data is complete
            rootNode.InitializeExtensions();

            docRegistry.Add(document);


            UniqueNamer uniqueNamer = new UniqueNamer('_');

            foreach (DomNode node in rootNode.Subtree)
            {
                if (node.Type.IdAttribute != null)
                {
                    uniqueNamer.Name(node.GetId());
                }
            }


            // sync all the prefab instances
            DomNode folderNode = document.RootGameObjectFolder.As <DomNode>();

            foreach (DomNode node in folderNode.Subtree)
            {
                PrefabInstance prefab = node.As <PrefabInstance>();
                if (prefab == null)
                {
                    continue;
                }
                prefab.Resolve(uniqueNamer);
            }

            if (ResolveOnLoad)
            {
                // resovle all the game references.
                foreach (var subGame in document.GetChildList <GameReference>(Schema.gameType.gameReferenceChild))
                {
                    subGame.Resolve();
                }
            }

            document.Dirty = false;
            return(document);
        }
Beispiel #4
0
        /// <summary>
        /// Open or create new document.
        /// It opens if the file exist otherwise it will creates new document        
        /// </summary>        
        public static GameDocument OpenOrCreate(Uri uri, SchemaLoader schemaLoader)
        {
            if (!uri.IsAbsoluteUri)
                return null;

            var docRegistry = Globals.MEFContainer.GetExportedValue<GameDocumentRegistry>();

            GameDocument document = docRegistry.FindDocument(uri) as GameDocument;
            if (document != null)
            {
                return document;
            }

            string filePath = uri.LocalPath;

            DomNode rootNode = null;
            if (File.Exists(filePath))
            {               
                // read existing document using custom dom XML reader
                using (FileStream stream = File.OpenRead(filePath))
                {
                    var reader = new CustomDomXmlReader(Globals.ResourceRoot, schemaLoader);
                    rootNode = reader.Read(stream, uri);
                }
            }
            else
            {
                // create new document by creating a Dom node of the root type defined by the schema                 
                rootNode = new DomNode(Schema.gameType.Type, Schema.gameRootElement);
                rootNode.SetAttribute(Schema.gameType.nameAttribute, "Game".Localize());
            }


            GameObjectFolder rootFolder = Adapters.As<GameObjectFolder>(rootNode.GetChild(Schema.gameType.gameObjectFolderChild));
            if (rootFolder == null)
            {
                // create the game object folder
                rootFolder = (GameObjectFolder)GameObjectFolder.Create();
                rootFolder.Name = "GameObjects".Localize("this is the name of a folder in the project lister");
                rootNode.SetChild(Schema.gameType.gameObjectFolderChild, rootFolder.DomNode);
            }
            

            // create bookmarks 
            DomNode bookmarks = rootNode.GetChild(Schema.gameType.bookmarksChild);
            if (bookmarks == null)
            {
                bookmarks = new DomNode(Schema.bookmarksType.Type);
                rootNode.SetChild(Schema.gameType.bookmarksChild, bookmarks);
            }

            DomNode layersNode = rootNode.GetChild(Schema.gameType.layersChild);
            if (layersNode == null)
            {
                layersNode = new DomNode(Schema.layersType.Type);
                rootNode.SetChild(Schema.gameType.layersChild, layersNode);
            }

            // Create the grid
            DomNode gridNode = rootNode.GetChild(Schema.gameType.gridChild);
            if (gridNode == null)
            {
                gridNode = new DomNode(Schema.gridType.Type);
                rootNode.SetChild(Schema.gameType.gridChild, gridNode);
            }

            document = rootNode.As<GameDocument>();
            document.Uri = uri;

            // Initialize Dom extensions now that the data is complete
            rootNode.InitializeExtensions();
           
            docRegistry.Add(document);


            UniqueNamer uniqueNamer = new UniqueNamer('_');
            foreach (DomNode node in rootNode.Subtree)
            {
                if (node.Type.IdAttribute != null)
                {
                    uniqueNamer.Name(node.GetId());
                }
            }


            // sync all the prefab instances
            DomNode folderNode = document.RootGameObjectFolder.As<DomNode>();
            foreach (DomNode node in folderNode.Subtree)
            {
                PrefabInstance prefab = node.As<PrefabInstance>();
                if (prefab == null) continue;
                prefab.Resolve(uniqueNamer);
            }

            if (ResolveOnLoad)
            {
                // resovle all the game references.
                foreach (var subGame in document.GetChildList<GameReference>(Schema.gameType.gameReferenceChild))
                {
                    subGame.Resolve();
                }
            }

            document.Dirty = false;
            return document;
        }