public void TestMinNumDigits() { UniqueNamer test = new UniqueNamer('_', 5); test.Name("a"); Assert.AreEqual(test.Name("a"), "a_00001"); Assert.Throws<NotSupportedException>(delegate { UniqueNamer bad = new UniqueNamer('_', 11); }); }
public void TestName() { UniqueNamer test = new UniqueNamer(); Assert.AreEqual(test.Name("a"), "a"); Assert.AreEqual(test.Name("a1"), "a1"); Assert.AreEqual(test.Name("a"), "a_1"); Assert.AreEqual(test.Name("a_1"), "a_2"); }
public void TestChange() { UniqueNamer test = new UniqueNamer(); test.Name("a"); test.Change("a", "b"); Assert.AreEqual(test.Name("a"), "a"); Assert.AreEqual(test.Name("b"), "b_1"); }
public void TestRetire() { UniqueNamer test = new UniqueNamer(); test.Name("a"); string a_1 = test.Name("a"); // should be a_1 test.Retire("a"); Assert.AreEqual(test.Name("a"), "a"); Assert.AreEqual(test.Name("a"), "a_2"); }
public void Resolve(UniqueNamer namer) { try { m_updating = true; Uri resUri = GetAttribute<Uri>(Schema.prefabInstanceType.prefabRefAttribute); if (resUri != null) m_prefab = Globals.ResourceService.Load(resUri) as IPrefab; if (m_prefab == null) return; // update name and uri if(resUri == null) SetAttribute(Schema.prefabInstanceType.prefabRefAttribute, m_prefab.Uri); IGameObjectGroup gobgroup = DomNode.As<IGameObjectGroup>(); if (string.IsNullOrWhiteSpace(gobgroup.Name)) gobgroup.Name = "PrefabInst_" + m_prefab.Name; DomNode[] gobs = DomNode.Copy(m_prefab.GameObjects.AsIEnumerable<DomNode>()); HashSet<string> gobIds = new HashSet<string>(); gobgroup.GameObjects.Clear(); foreach (var gobNode in gobs) { gobNode.InitializeExtensions(); IGameObject gob = gobNode.As<IGameObject>(); m_intsToOriginal.Add(gobNode, gob.Name); gobIds.Add(gob.Name); ObjectOverride objectOverride; m_overridesMap.TryGetValue(gob.Name, out objectOverride); updateNode(gobNode, objectOverride); string name = gob.Name; if (namer != null) gob.Name = namer.Name(gob.Name); gobgroup.GameObjects.Add(gob); } // cleanup m_overridesmap List<string> overrideIds = new List<string>(m_overridesMap.Keys); foreach (string id in overrideIds) { if (!gobIds.Contains(id)) { ObjectOverride objectOverride = m_overridesMap[id]; m_overridesMap.Remove(id); m_overrideList.Remove(objectOverride); } } } finally { m_updating = false; } }
void ValidateSubtree(DomNode folder) { var uniqueNamer = new UniqueNamer(); foreach (DomNode node in folder.Subtree) { foreach (DomNode child in node.Children) { if (child.Type.IdAttribute != null) { string id = child.GetId(); string uniqueId = uniqueNamer.Name(id); if (id != uniqueId) throw new InvalidTransactionException("id collision"); } } uniqueNamer.Clear(); } }
public void TestSuffixes() { UniqueNamer testSpace = new UniqueNamer(' '); Assert.AreEqual(testSpace.Name("a"), "a"); Assert.AreEqual(testSpace.Name("a"), "a 1"); UniqueNamer testDash = new UniqueNamer('-'); Assert.AreEqual(testDash.Name("a"), "a"); Assert.AreEqual(testDash.Name("a"), "a-1"); UniqueNamer testForwardSlash = new UniqueNamer('/'); Assert.AreEqual(testForwardSlash.Name("a"), "a"); Assert.AreEqual(testForwardSlash.Name("a"), "a/1"); UniqueNamer testBackSlash = new UniqueNamer('\\'); Assert.AreEqual(testBackSlash.Name("a"), "a"); Assert.AreEqual(testBackSlash.Name("a"), "a\\1"); UniqueNamer testParens = new UniqueNamer('('); Assert.AreEqual(testParens.Name("a"), "a"); Assert.AreEqual(testParens.Name("a"), "a(1)"); Assert.Throws<ArgumentException>(delegate { UniqueNamer bad = new UniqueNamer('@'); }); }
private DomNode CreatePrefab(IEnumerable<IGameObject> gobs) { UniqueNamer uniqueNamer = new UniqueNamer(); DomNode[] temp = new DomNode[1]; List<IGameObject> copyList = new List<IGameObject>(); AABB bound = new AABB(); foreach (IGameObject gameObject in SelectedGobs) { IBoundable boundable = gameObject.As<IBoundable>(); bound.Extend(boundable.BoundingBox); Matrix4F world = TransformUtils.ComputeWorldTransform(gameObject); temp[0] = gameObject.As<DomNode>(); DomNode[] copies = DomNode.Copy(temp); copies[0].InitializeExtensions(); IGameObject copy = copies[0].As<IGameObject>(); copy.Name = uniqueNamer.Name(copy.Name); TransformUtils.SetTransform(copy, world); copyList.Add(copy); } DomNode prefab = new DomNode(Schema.prefabType.Type, Schema.prefabRootElement); var list = prefab.GetChildList(Schema.prefabType.gameObjectChild); Vec3F center = bound.Center; foreach (IGameObject gob in copyList) { gob.Translation = gob.Translation - center; gob.UpdateTransform(); list.Add(gob.As<DomNode>()); } return prefab; }
/// <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; }