Exemple #1
0
        public IDocument Open(Uri uri)
        {
            IGameDocument activeDoc = m_documentRegistry.GetActiveDocument <IGameDocument>();

            if (activeDoc != null)
            {
                if (!m_documentService.Close(activeDoc))
                {
                    return(null);
                }
            }

            List <IResource> resources = new List <IResource>(m_resourceService.Resources);

            foreach (var res in resources)
            {
                m_resourceService.Unload(res.Uri);
            }

            GameDocument document = GameDocument.OpenOrCreate(uri, m_schemaLoader);

            m_contextRegistry.ActiveContext = document.As <GameContext>();
            return(document);
        }
Exemple #2
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(uri, 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(LevelEditorXLE.Patches.GetGameType(), 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
            foreach (var node in document.As <DomNode>().Subtree.AsIEnumerable <IPrefabInstance>())
            {
                node.Resolve(uniqueNamer);
            }

            LevelEditorXLE.Patches.CreateDefaultNodes(rootNode);

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

                LevelEditorXLE.Patches.ResolveOnLoad(document);
            }

            document.Dirty = false;
            return(document);
        }