public override string ToString() { switch (Type) { case NBTType.Bytes: return(String.Format("{0}[{1}] {2}", Type, GetBytes().Length, Name)); case NBTType.List: NBTList list = (NBTList)this; return(String.Format("{0}<{1}>[{2}] {3}", Type, list.ListType, list.Tags.Length, Name)); case NBTType.Compound: NBTCompound comp = (NBTCompound)this; return(String.Format("{0}[{1}] {2}", Type, comp.Tags.Count, Name)); case NBTType.String: if (Payload != null) { return(String.Format("{0} {1} = \"{2}\"", Type, Name, Payload)); } else { return(String.Format("{0} {1} = null", Type, Name)); } default: return(String.Format("{0} {1} = {2}", Type, Name, Payload)); } }
public NBTCompound SerializeNBT() { HasChangedSinceSave = false; NBTCompound tag = new NBTCompound("SuperImageEvolver"); tag.Append("FormatVersion", FormatVersion); tag.Append("Shapes", Shapes); tag.Append("Vertices", Vertices); tag.Append("ImprovementCounter", ImprovementCounter); tag.Append("MutationCounter", MutationCounter); tag.Append("RiskyMoveCounter", RiskyMoveCounter); tag.Append("ElapsedTime", DateTime.UtcNow.Subtract(TaskStart).Ticks); tag.Append(ProjectOptions.SerializeNBT()); tag.Append(BestMatch.SerializeNBT("BestMatch")); NBTag initializerTag = ModuleManager.WriteModule("Initializer", Initializer); tag.Append(initializerTag); NBTag mutatorTag = ModuleManager.WriteModule("Mutator", Mutator); tag.Append(mutatorTag); NBTag evaluatorTag = ModuleManager.WriteModule("Evaluator", Evaluator); tag.Append(evaluatorTag); byte[] imageData; using (MemoryStream ms = new MemoryStream()) { lock ( OriginalImage ) { OriginalImage.Save(ms, ImageFormat.Png); } ms.Flush(); imageData = new byte[ms.Length]; Buffer.BlockCopy(ms.GetBuffer(), 0, imageData, 0, imageData.Length); } tag.Append("ImageData", imageData); List <NBTCompound> statTags = new List <NBTCompound>(); foreach (MutationType mtype in Enum.GetValues(typeof(MutationType))) { NBTCompound stat = new NBTCompound("MutationTypeStat"); stat.Append("Type", mtype.ToString()); stat.Append("Count", MutationCounts[mtype]); stat.Append("Sum", MutationImprovements[mtype]); statTags.Add(stat); } var stats = new NBTList("MutationStats", NBTType.Compound, statTags.ToArray()); tag.Append(stats); return(tag); }
public NBTag SerializeNBT(string tagName) { NBTCompound compound = new NBTCompound(tagName); compound.Append("Divergence", Divergence); NBTList tag = new NBTList("Shapes", NBTType.Compound, Shapes.Length); for (int i = 0; i < Shapes.Length; i++) { tag[i] = Shapes[i].SerializeNBT(); } compound.Append(tag); return(compound); }
public NBTag SerializeNBT() { NBTCompound tag = new NBTCompound("Shape"); tag.Append("Color", Color); NBTList points = new NBTList("Points", NBTType.PointF, Points.Length); for (int p = 0; p < Points.Length; p++) { points[p] = new NBTag(NBTType.PointF, null, Points[p], points); } tag.Append(points); return(tag); }
public string ToString(bool recursive) { string output = GetIndentedName() + Environment.NewLine; NBTList thisList = this as NBTList; if (thisList == null || thisList.ListType == NBTType.Compound || thisList.ListType == NBTType.Bytes || thisList.ListType == NBTType.List) { foreach (NBTag tag in this) { output += tag.ToString(recursive); } } return(output); }
public void WriteTag(BinaryWriter writer, bool writeType) { if (writeType) { writer.Write((byte)Type); } if (Type == NBTType.End) { return; } if (writeType) { WriteString(Name, writer); } switch (Type) { case NBTType.Byte: writer.Write((byte)Payload); return; case NBTType.Short: writer.Write((short)Payload); return; case NBTType.Int: writer.Write((int)Payload); return; case NBTType.Long: writer.Write((long)Payload); return; case NBTType.Float: writer.Write((float)Payload); return; case NBTType.Double: writer.Write((double)Payload); return; case NBTType.Bytes: writer.Write(((byte[])Payload).Length); writer.Write((byte[])Payload); return; case NBTType.String: WriteString((string)Payload, writer); return; case NBTType.Bool: writer.Write((bool)Payload); return; case NBTType.Color: writer.Write(((Color)Payload).ToArgb()); return; case NBTType.Point: writer.Write(((Point)Payload).X); writer.Write(((Point)Payload).Y); break; case NBTType.PointF: writer.Write(((PointF)Payload).X); writer.Write(((PointF)Payload).Y); break; case NBTType.List: NBTList list = (NBTList)this; writer.Write((byte)list.ListType); writer.Write(list.Tags.Length); for (int i = 0; i < list.Tags.Length; i++) { list.Tags[i].WriteTag(writer, false); } return; case NBTType.Compound: foreach (NBTag tag in this) { tag.WriteTag(writer, true); } writer.Write((byte)NBTType.End); return; default: return; } }
public static NBTag ReadTag(BinaryReader reader, NBTType type, string name, NBTag parent) { if (name == null && type != NBTType.End) { name = ReadString(reader); } switch (type) { case NBTType.End: return(new NBTag(NBTType.End, parent)); case NBTType.Byte: return(new NBTag(NBTType.Byte, name, reader.ReadByte(), parent)); case NBTType.Short: return(new NBTag(NBTType.Short, name, reader.ReadInt16(), parent)); case NBTType.Int: return(new NBTag(NBTType.Int, name, reader.ReadInt32(), parent)); case NBTType.Long: return(new NBTag(NBTType.Long, name, reader.ReadInt64(), parent)); case NBTType.Float: return(new NBTag(NBTType.Float, name, reader.ReadSingle(), parent)); case NBTType.Double: return(new NBTag(NBTType.Double, name, reader.ReadDouble(), parent)); case NBTType.Bytes: int bytesLength = reader.ReadInt32(); return(new NBTag(NBTType.Bytes, name, reader.ReadBytes(bytesLength), parent)); case NBTType.String: return(new NBTag(NBTType.String, name, ReadString(reader), parent)); case NBTType.Bool: return(new NBTag(NBTType.Bool, name, reader.ReadBoolean(), parent)); case NBTType.Color: return(new NBTag(NBTType.Color, name, Color.FromArgb(reader.ReadInt32()), parent)); case NBTType.Point: int iX = reader.ReadInt32(); int iY = reader.ReadInt32(); return(new NBTag(NBTType.Point, name, new Point(iX, iY), parent)); case NBTType.PointF: float fX = reader.ReadSingle(); float fY = reader.ReadSingle(); return(new NBTag(NBTType.PointF, name, new PointF(fX, fY), parent)); case NBTType.List: NBTList list = new NBTList { Type = NBTType.List, Name = name, Parent = parent, ListType = (NBTType)reader.ReadByte() }; int listLength = reader.ReadInt32(); list.Tags = new NBTag[listLength]; for (int i = 0; i < listLength; i++) { list.Tags[i] = ReadTag(reader, list.ListType, "", list); } return(list); case NBTType.Compound: NBTCompound compound = new NBTCompound(name) { Parent = parent }; while (true) { NBTag childTag = ReadTag(reader, (NBTType)reader.ReadByte(), null, compound); if (childTag.Type == NBTType.End) { break; } if (childTag.Name == null) { continue; } if (compound.Tags.ContainsKey(childTag.Name)) { throw new IOException( "NBT parsing error: null names and duplicate names are not allowed within a compound tags."); } else { compound.Tags.Add(childTag.Name, childTag); } } return(compound); default: throw new IOException("NBT parsing error: unknown tag type."); } }