public PssgAttributeInfo AddAttributeInfo(string name, PssgNodeInfo nodeInfo) { // For each attributeInfo in nodeInfo, the ids have to be consecutive if (GetAttributeInfo(name).Length > 0) { return null; } if (attributeInfo == null) { attributeInfo = new PssgAttributeInfo[1]; } else { Array.Resize(ref attributeInfo, attributeInfo.Length + 1); } int newID = 0; List<int> currentKeys = new List<int>(nodeInfo.attributeInfo.Keys); if (currentKeys.Count > 0) { foreach (int k in currentKeys) { newID = Math.Max(newID, k); } newID++; } else { newID = attributeInfo.Length; } PssgAttributeInfo attrInfo = new PssgAttributeInfo(newID, name); if (newID == attributeInfo.Length) { attributeInfo[attrInfo.id - 1] = attrInfo; this.nodeInfo[nodeInfo.id - 1].attributeInfo.Add(attrInfo.id, attrInfo); } else { for (int i = attributeInfo.Length - 2; i >= newID - 1; i--) { attributeInfo[i + 1] = attributeInfo[i]; attributeInfo[i + 1].id = i + 2; } attributeInfo[attrInfo.id - 1] = attrInfo; this.nodeInfo[nodeInfo.id - 1].attributeInfo.Add(attrInfo.id, attrInfo); // Fix the NodeInfos foreach (PssgNodeInfo nInfo in this.nodeInfo) { List<int> keys = new List<int>(nInfo.attributeInfo.Keys); //keys.Sort(); if (nInfo != nodeInfo) { for (int i = keys.Count - 1; i >= 0; i--) { if (keys[i] >= newID) { PssgAttributeInfo aInfo = attributeInfo[keys[i]]; nInfo.attributeInfo.Remove(keys[i]); nInfo.attributeInfo.Add(keys[i] + 1, aInfo); } } } } // Edit CNode to fix CAttr.id rootNode.AddAttributeInfo(newID); } return attrInfo; }
public PssgNodeInfo(PssgBinaryReader reader, PssgFile file) { attributeInfo = new SortedDictionary<int, PssgAttributeInfo>(); id = reader.ReadInt32(); name = reader.ReadPSSGString(); int attributeInfoCount = reader.ReadInt32(); PssgAttributeInfo ai; for (int i = 0; i < attributeInfoCount; i++) { ai = new PssgAttributeInfo(reader); try { attributeInfo.Add(ai.id, ai); } catch (ArgumentException ex) { if (MessageBox.Show("The attribute of id " + ai.id + ", named " + ai.name + ", already exists, and will not be saved. Continue?", "PSSG Editor", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) continue; else throw new Exception("The attribute of id " + ai.id + ", named " + ai.name + ", already exists."); } file.attributeInfo[ai.id - 1] = ai; } }