Пример #1
0
        void SaveProject()
        {
            if (State.ProjectFileName != null)
            {
                NBTag presentationTag = new NBTCompound("Presentation");
                presentationTag.Append("OriginalVisible", picOriginal.Visible);
                presentationTag.Append("OriginalZoom", OriginalZoom);
                presentationTag.Append("BestMatchVisible", picBestMatch.Visible);
                presentationTag.Append("BestMatchZoom", picBestMatch.Zoom);
                presentationTag.Append("BestMatchWireframe", picBestMatch.Wireframe);
                presentationTag.Append("BestMatchShowLastChange", picBestMatch.ShowLastChange);
                presentationTag.Append("DiffVisible", picDiff.Visible);
                presentationTag.Append("DiffInvert", picDiff.Invert);
                presentationTag.Append("DiffExaggerate", picDiff.Exaggerate);
                presentationTag.Append("DiffShowColor", picDiff.ShowColor);
                presentationTag.Append("DiffZoom", picDiff.Zoom);
                presentationTag.Append("DiffShowLastChange", picDiff.ShowLastChange);
                presentationTag.Append("SyncZoom", cmOriginalZoomSync.Checked);

                NBTag tag = State.SerializeNBT();
                tag.Append(presentationTag);
                tag.WriteTag(State.ProjectFileName);
                Text = Path.GetFileName(State.ProjectFileName) + " | SuperImageEvolver | saved " + DateTime.Now;
            }
            else
            {
                SaveProjectAs();
            }
        }
Пример #2
0
        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));
            }
        }
Пример #3
0
        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);
        }
Пример #4
0
        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);
        }
Пример #5
0
        public NBTag Append(string name, params NBTag[] tags)
        {
            if (tags == null)
            {
                throw new ArgumentNullException("tags");
            }
            NBTCompound compound = new NBTCompound(name);

            foreach (NBTag tag in tags)
            {
                compound.Tags.Add(tag.Name, tag);
            }
            return(Append(compound));
        }
Пример #6
0
        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);
        }
Пример #7
0
        public NBTag SerializeNBT()
        {
            NBTCompound tag = new NBTCompound("ProjectOptions");

            tag.Append("Matte", Matte);
            tag.Append("BackColor", BackColor);
            tag.Append("MaxOverlap", MaxOverlap);
            tag.Append("MinAlpha", MinAlpha);
            tag.Append("RefreshRate", RefreshRate.Ticks);
            tag.Append("WireframeColor", WireframeColor);
            tag.Append("LastChangeColor1", LastChangeColor1);
            tag.Append("LastChangeColor2", LastChangeColor2);
            tag.Append("RiskMargin", RiskMargin);
            tag.Append("RiskRate", RiskRate);
            return(tag);
        }
        public static NBTag WriteModule(string tagName, IModule module)
        {
            NBTCompound    root    = new NBTCompound(tagName);
            IModuleFactory factory = GetFactoryByType(module.GetType());

            root.Append("ID", factory.ID);

            bool auto =
                !factory.ModuleType.GetCustomAttributes(typeof(DisableAutoSerializationAttribute), true).Any();

            if (auto)
            {
                root.Append(WriteModuleProperties(module));
            }
            NBTag customSettings = new NBTCompound("Settings");

            module.WriteSettings(customSettings);
            root.Append(customSettings);
            return(root);
        }
Пример #9
0
        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.");
            }
        }
Пример #10
0
        public static NBTag WriteModuleProperties(IModule module)
        {
            IModuleFactory factory = GetFactoryByType(module.GetType());
            NBTag          root    = new NBTCompound("Properties");

            foreach (PropertyInfo p in factory.ModuleType.GetProperties())
            {
                object val = p.GetValue(module, null);
                if (p.PropertyType == typeof(byte))
                {
                    root.Append(p.Name, (byte)val);
                }
                else if (p.PropertyType == typeof(short))
                {
                    root.Append(p.Name, (short)val);
                }
                else if (p.PropertyType == typeof(int))
                {
                    root.Append(p.Name, (int)val);
                }
                else if (p.PropertyType == typeof(long))
                {
                    root.Append(p.Name, (long)val);
                }
                else if (p.PropertyType == typeof(float))
                {
                    root.Append(p.Name, (float)val);
                }
                else if (p.PropertyType == typeof(double))
                {
                    root.Append(p.Name, (double)val);
                }
                else if (p.PropertyType == typeof(byte[]))
                {
                    root.Append(p.Name, (byte[])val);
                }
                else if (p.PropertyType == typeof(string))
                {
                    root.Append(p.Name, (string)val);
                }
                else if (p.PropertyType == typeof(bool))
                {
                    root.Append(p.Name, (bool)val);
                }
                else if (p.PropertyType == typeof(Color))
                {
                    root.Append(p.Name, (Color)val);
                }
                else if (p.PropertyType == typeof(Point))
                {
                    root.Append(p.Name, (Point)val);
                }
                else if (p.PropertyType == typeof(PointF))
                {
                    root.Append(p.Name, (PointF)val);
                }
                else
                {
                    throw new NotSupportedException("Unknown property type.");
                }
            }
            return(root);
        }