public static DataTreeNode Create(DataNode data) { if (data is ObjectNode) return new ObjectTreeNode(data as ObjectNode); else if (data is ObjectRefNode) return new ObjectRefTreeNode(data as ObjectRefNode); else if (data is PrimitiveNode) return new PrimitiveTreeNode(data as PrimitiveNode); else if (data is EnumNode) return new EnumTreeNode(data as EnumNode); else if (data is StringNode) return new StringTreeNode(data as StringNode); else return new DataTreeNode(data); }
public static Image GetIcon(DataNode data) { return CorePluginRegistry.GetTypeImage(data.GetType()) ?? CorePluginRegistry.GetTypeImage(typeof(DataNode)); }
protected string[] GetAvailTypes(DataNode baseNode = null) { List<string> availTypes = new List<string>(); foreach (DataTreeNode dataNode in this.rootData) availTypes.AddRange(dataNode.Data.GetTypeStrings(true)); return availTypes.Distinct().ToArray(); }
protected DataTreeNode(DataNode data) { this.data = data; this.Text = this.NodeTypeName; this.Image = GetIcon(this.data); }
protected DataTreeNode AddData(DataNode data) { // Register type data layout nodes if (data is TypeDataLayoutNode) this.typeDataLayout[(data.Parent as ObjectNode).TypeString] = data as TypeDataLayoutNode; DataTreeNode dataNode = DataTreeNode.Create(data); foreach (DataNode child in data.SubNodes) { DataTreeNode childDataNode = this.AddData(child); childDataNode.Parent = dataNode; } this.UpdateTypeDataLayout(dataNode, false); return dataNode; }
protected bool IsObjectIdExisting(uint objId, DataNode baseNode = null) { if (objId == 0) return false; foreach (DataTreeNode dataNode in this.rootData) if (dataNode.Data.IsObjectIdDefined(objId)) return true; return false; }
public DelegateNode(string typeString, uint objId, DataNode method, DataNode target, DataNode invokeList) : base(DataType.Delegate, typeString, objId) { this.method = method; this.target = target; this.invokeList = invokeList; if (this.method != null) this.method.Parent = this; if (this.target != null) this.target.Parent = this; if (this.invokeList != null) this.invokeList.Parent = this; }
public static Image GetIcon(DataNode data) { return data.GetType().GetEditorImage(); }
/// <summary> /// Reads an <see cref="Duality.Serialization.MetaFormat.ArrayNode"/>, including possible child nodes. /// </summary> /// <param name="node"></param> protected ArrayNode ReadArray() { string arrTypeString = this.reader.ReadString(); uint objId = this.reader.ReadUInt32(); int arrRank = this.reader.ReadInt32(); int arrLength = this.reader.ReadInt32(); Type arrType = ReflectionHelper.ResolveType(arrTypeString, false); ArrayNode result = new ArrayNode(arrTypeString, objId, arrRank, arrLength); // Prepare object reference this.idManager.Inject(result, objId); // Store primitive data block bool nonPrimitive = false; if (arrType != null) { Array arrObj = Array.CreateInstance(arrType.GetElementType(), arrLength); if (arrObj is bool[]) { this.ReadArrayData(arrObj as bool[]); } else if (arrObj is byte[]) { this.ReadArrayData(arrObj as byte[]); } else if (arrObj is sbyte[]) { this.ReadArrayData(arrObj as sbyte[]); } else if (arrObj is short[]) { this.ReadArrayData(arrObj as short[]); } else if (arrObj is ushort[]) { this.ReadArrayData(arrObj as ushort[]); } else if (arrObj is int[]) { this.ReadArrayData(arrObj as int[]); } else if (arrObj is uint[]) { this.ReadArrayData(arrObj as uint[]); } else if (arrObj is long[]) { this.ReadArrayData(arrObj as long[]); } else if (arrObj is ulong[]) { this.ReadArrayData(arrObj as ulong[]); } else if (arrObj is float[]) { this.ReadArrayData(arrObj as float[]); } else if (arrObj is double[]) { this.ReadArrayData(arrObj as double[]); } else if (arrObj is decimal[]) { this.ReadArrayData(arrObj as decimal[]); } else if (arrObj is char[]) { this.ReadArrayData(arrObj as char[]); } else if (arrObj is string[]) { this.ReadArrayData(arrObj as string[]); } else { nonPrimitive = true; } if (!nonPrimitive) { result.PrimitiveData = arrObj; } } else { nonPrimitive = true; } // Store other data as sub-nodes if (nonPrimitive) { for (int i = 0; i < arrLength; i++) { DataNode child = this.ReadObjectData() as DataNode; child.Parent = result; } } return(result); }
/// <summary> /// Reads a <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes. /// </summary> /// <param name="node"></param> protected StructNode ReadStruct(bool classType) { // Read struct type string objTypeString = this.reader.ReadString(); uint objId = this.reader.ReadUInt32(); bool custom = this.reader.ReadBoolean(); bool surrogate = this.reader.ReadBoolean(); StructNode result = new StructNode(classType, objTypeString, objId, custom, surrogate); // Read surrogate constructor data if (surrogate) { custom = true; // Set fake object reference for surrogate constructor: No self-references allowed here. this.idManager.Inject(null, objId); CustomSerialIO customIO = new CustomSerialIO(); customIO.Deserialize(this); if (customIO.Data.Any()) { DummyNode surrogateConstructor = new DummyNode(); surrogateConstructor.Parent = result; foreach (var pair in customIO.Data) { StringNode key = new StringNode(pair.Key); DataNode value = pair.Value as DataNode; key.Parent = surrogateConstructor; value.Parent = surrogateConstructor; } } } // Prepare object reference this.idManager.Inject(result, objId); if (custom) { CustomSerialIO customIO = new CustomSerialIO(); customIO.Deserialize(this); foreach (var pair in customIO.Data) { StringNode key = new StringNode(pair.Key); DataNode value = pair.Value as DataNode; key.Parent = result; value.Parent = result; } } else { // Determine data layout bool wasThereBefore = this.GetCachedTypeDataLayout(objTypeString) != null; TypeDataLayout layout = this.ReadTypeDataLayout(objTypeString); if (!wasThereBefore) { TypeDataLayoutNode layoutNode = new TypeDataLayoutNode(new TypeDataLayout(layout)); layoutNode.Parent = result; } // Read fields if (this.dataVersion <= 2) { for (int i = 0; i < layout.Fields.Length; i++) { DataNode fieldValue = this.ReadObjectData() as DataNode; fieldValue.Parent = result; fieldValue.Name = layout.Fields[i].name; } } else if (this.dataVersion >= 3) { bool[] fieldOmitted = new bool[layout.Fields.Length]; this.ReadArrayData(fieldOmitted); for (int i = 0; i < layout.Fields.Length; i++) { if (fieldOmitted[i]) { continue; } DataNode fieldValue = this.ReadObjectData() as DataNode; fieldValue.Parent = result; fieldValue.Name = layout.Fields[i].name; } } } return(result); }
/// <summary> /// Writes the specified <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes. /// </summary> /// <param name="node"></param> protected void WriteStruct(StructNode node) { // Write the structs data type this.writer.Write(node.TypeString); this.writer.Write(node.ObjId); this.writer.Write(node.CustomSerialization); this.writer.Write(node.SurrogateSerialization); if (node.SurrogateSerialization) { CustomSerialIO customIO = new CustomSerialIO(); DummyNode surrogateConstructor = node.SubNodes.FirstOrDefault() as DummyNode; if (surrogateConstructor != null) { var enumerator = surrogateConstructor.SubNodes.GetEnumerator(); while (enumerator.MoveNext()) { StringNode key = enumerator.Current as StringNode; if (enumerator.MoveNext() && key != null) { DataNode value = enumerator.Current; customIO.WriteValue(key.StringValue, value); } } } customIO.Serialize(this); } if (node.CustomSerialization || node.SurrogateSerialization) { CustomSerialIO customIO = new CustomSerialIO(); var enumerator = node.SubNodes.GetEnumerator(); while (enumerator.MoveNext()) { StringNode key = enumerator.Current as StringNode; if (key != null && enumerator.MoveNext()) { DataNode value = enumerator.Current; customIO.WriteValue(key.StringValue, value); } } customIO.Serialize(this); } else { bool skipLayout = false; TypeDataLayout layout = null; if (node.SubNodes.FirstOrDefault() is TypeDataLayoutNode) { TypeDataLayoutNode typeDataLayout = node.SubNodes.FirstOrDefault() as TypeDataLayoutNode; this.WriteTypeDataLayout(typeDataLayout.Layout, node.TypeString); layout = typeDataLayout.Layout; skipLayout = true; } else { this.WriteTypeDataLayout(node.TypeString); layout = this.GetCachedTypeDataLayout(node.TypeString); } // Write the structs omitted mask bool[] fieldOmitted = new bool[layout.Fields.Length]; for (int i = 0; i < layout.Fields.Length; i++) { fieldOmitted[i] = !node.SubNodes.Any(n => !(n is DummyNode) && n.Name == layout.Fields[i].name); } this.WriteArrayData(fieldOmitted); // Write the structs fields foreach (DataNode subNode in node.SubNodes) { if (skipLayout) { skipLayout = false; continue; } if (subNode is DummyNode) { continue; } this.WriteObjectData(subNode); } } }
public DelegateNode(string typeString, uint objId, DataNode method, DataNode target, DataNode invokeList) : base(DataType.Delegate, typeString, objId) { this.method = method; this.target = target; this.invokeList = invokeList; if (this.method != null) { this.method.Parent = this; } if (this.target != null) { this.target.Parent = this; } if (this.invokeList != null) { this.invokeList.Parent = this; } }
/// <summary> /// Reads a <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes. /// </summary> /// <param name="node"></param> protected StructNode ReadStruct(bool classType) { // Read struct type string objTypeString = this.reader.GetAttribute("type"); string objIdString = this.reader.GetAttribute("id"); string customString = this.reader.GetAttribute("custom"); string surrogateString = this.reader.GetAttribute("surrogate"); uint objId = objIdString == null ? 0 : XmlConvert.ToUInt32(objIdString); bool custom = customString != null && XmlConvert.ToBoolean(customString); bool surrogate = surrogateString != null && XmlConvert.ToBoolean(surrogateString); StructNode result = new StructNode(classType, objTypeString, objId, custom, surrogate); // Read surrogate constructor data if (surrogate) { custom = true; // Set fake object reference for surrogate constructor: No self-references allowed here. this.idManager.Inject(null, objId); CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement); customIO.Deserialize(this); if (customIO.Data.Any()) { DummyNode surrogateConstructor = new DummyNode(); surrogateConstructor.Parent = result; foreach (var pair in customIO.Data) { StringNode key = new StringNode(pair.Key); DataNode value = pair.Value as DataNode; key.Parent = surrogateConstructor; value.Parent = surrogateConstructor; } } } // Prepare object reference this.idManager.Inject(result, objId); // Read custom object data if (custom) { CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement); customIO.Deserialize(this); foreach (var pair in customIO.Data) { StringNode key = new StringNode(pair.Key); DataNode value = pair.Value as DataNode; key.Parent = result; value.Parent = result; } } // Red non-custom object data else if (!this.reader.IsEmptyElement) { // Read fields bool scopeChanged; string fieldName; DataNode fieldValue; while (true) { fieldValue = this.ReadObjectData(out fieldName, out scopeChanged) as DataNode; if (scopeChanged) { break; } else { fieldValue.Name = fieldName; fieldValue.Parent = result; } } } return(result); }
/// <summary> /// Reads an <see cref="Duality.Serialization.MetaFormat.ArrayNode"/>, including possible child nodes. /// </summary> /// <param name="node"></param> protected ArrayNode ReadArray() { string arrTypeString = this.reader.GetAttribute("type"); string objIdString = this.reader.GetAttribute("id"); string arrRankString = this.reader.GetAttribute("rank"); string arrLengthString = this.reader.GetAttribute("length"); uint objId = objIdString == null ? 0 : XmlConvert.ToUInt32(objIdString); int arrRank = arrRankString == null ? 1 : XmlConvert.ToInt32(arrRankString); int arrLength = arrLengthString == null ? 0 : XmlConvert.ToInt32(arrLengthString); Type arrType = ReflectionHelper.ResolveType(arrTypeString, false); ArrayNode result = new ArrayNode(arrTypeString, objId, arrRank, arrLength); // Prepare object reference this.idManager.Inject(result, objId); // Store primitive data block bool nonPrimitive; if (arrType != null) { Array arrObj = Array.CreateInstance(arrType.GetElementType(), arrLength); if (arrObj is bool[] || arrObj is byte[] || arrObj is sbyte[] || arrObj is short[] || arrObj is ushort[] || arrObj is int[] || arrObj is uint[] || arrObj is long[] || arrObj is ulong[] || arrObj is float[] || arrObj is double[] || arrObj is decimal[] || arrObj is char[] || arrObj is string[]) { nonPrimitive = false; } else { nonPrimitive = true; } if (!nonPrimitive) { if (arrObj is byte[]) { byte[] byteArr = arrObj as byte[]; string binHexString = this.reader.ReadString(); byte[] byteArr2 = this.StringToByteArray(binHexString); for (int l = 0; l < arrLength; l++) { byteArr[l] = byteArr2[l]; } } else { for (int l = 0; l < arrLength; l++) { DataNode elemNode = this.ReadObjectData() as DataNode; if (arrObj != null) { if (elemNode is PrimitiveNode) { arrObj.SetValue((elemNode as PrimitiveNode).PrimitiveValue, l); } else if (elemNode is StringNode) { arrObj.SetValue((elemNode as StringNode).StringValue, l); } } } } result.PrimitiveData = arrObj; } } else { nonPrimitive = true; } // Store other data as sub-nodes if (nonPrimitive) { for (int i = 0; i < arrLength; i++) { DataNode child = this.ReadObjectData() as DataNode; child.Parent = result; } } return(result); }
/// <summary> /// Writes the specified <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes. /// </summary> /// <param name="node"></param> protected void WriteStruct(StructNode node) { // Write the structs data type this.writer.WriteAttributeString("type", node.TypeString); if (node.ObjId != 0) { this.writer.WriteAttributeString("id", XmlConvert.ToString(node.ObjId)); } if (node.CustomSerialization) { this.writer.WriteAttributeString("custom", XmlConvert.ToString(true)); } if (node.SurrogateSerialization) { this.writer.WriteAttributeString("surrogate", XmlConvert.ToString(true)); } if (node.SurrogateSerialization) { CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement); DummyNode surrogateConstructor = node.SubNodes.FirstOrDefault() as DummyNode; if (surrogateConstructor != null) { var enumerator = surrogateConstructor.SubNodes.GetEnumerator(); while (enumerator.MoveNext()) { StringNode key = enumerator.Current as StringNode; if (enumerator.MoveNext() && key != null) { DataNode value = enumerator.Current; customIO.WriteValue(key.StringValue, value); } } } customIO.Serialize(this); } if (node.CustomSerialization || node.SurrogateSerialization) { CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement); var enumerator = node.SubNodes.GetEnumerator(); while (enumerator.MoveNext()) { StringNode key = enumerator.Current as StringNode; if (key != null && enumerator.MoveNext()) { DataNode value = enumerator.Current; customIO.WriteValue(key.StringValue, value); } } customIO.Serialize(this); } else { // Write the structs fields foreach (DataNode subNode in node.SubNodes) { if (subNode is DummyNode) { continue; } if (subNode is TypeDataLayoutNode) { continue; } this.WriteObjectData(subNode, subNode.Name); } } }