Example #1
0
        public void TestEscaping()
        {
            var reader = new SectionedStreamReader(new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes("Ello"))));

            Assert.False(reader.isStartSection(SectionedStreamWriter.Escape("[ello]")));
            Assert.False(reader.isStartSection(SectionedStreamWriter.Escape("ello]")));
            Assert.False(reader.isStartSection(SectionedStreamWriter.Escape("[ello")));
            Assert.True(reader.isStartSection("[ello]"));
        }
        /// <summary>
        /// Reads and applies a single attribute
        /// </summary>
        private void readAttribute(IModelObject obj, SectionedStreamReader strm)
        {
            var name       = strm.ReadLine();
            var att        = ReflectionHelper.GetAttributeByName(obj.GetType(), name);
            var serialized = readAttributeValue(strm);

            if (serialized == StringSerializer.Unknown)
            {
                return;                                         //TODO: print zis shit
            }
            var deserialized = deserializeAttributeValue(serialized, att);

            att.SetData(obj, deserialized);
        }
 /// <summary>
 /// Deserializes all attributes for given object from the stream
 /// </summary>
 public void DeserializeAttributes(IModelObject obj, SectionedStreamReader strm)
 {
     while (strm.CurrentSection == "EntityAttributes")
     {
         try
         {
             readAttribute(obj, strm);
         }
         catch (Exception ex)
         {
             DI.Get <IErrorLogger>().Log(ex, "Can't deserialize attribute!");
         }
     }
 }
        /// <summary>
        /// Adds the objects found in the stream to given model
        /// </summary>
        /// <param name="reader"></param>
        public List <IModelObject> Deserialize(StreamReader reader)
        {
            var ret  = new List <IModelObject>();
            var strm = new SectionedStreamReader(reader);

            while (strm.CurrentSection == ObjectsSection)
            {
                var id       = int.Parse(strm.ReadLine());
                var typeName = strm.ReadLine();
                var type     = typeSerializer.Deserialize(typeName);
                if (type == null)
                {
                    Console.WriteLine("Unexisting type: " + typeName);
                    continue;
                }
                try
                {
                    var obj = (IModelObject)Activator.CreateInstance(type);
                    myObjectDictionary.setObjectID(obj, id);

                    ret.Add(obj);
                }
                catch (Exception ex)
                {
                    DI.Get <IErrorLogger>().Log(ex, "Cant create model object");
                }
            }

            while (strm.CurrentSection == AttributesSection)
            {
                var id  = int.Parse(strm.ReadLine());
                var obj = myObjectDictionary.getObjectByID(id);
                if (obj == null)
                {
                    Console.WriteLine("Unknown object with ID: " + id);
                    // Can be caused by corrupt save, or by an unexisting type for which the object was not loaded.
                    continue;
                }

                DeserializeAttributes(obj, strm);
            }
            return(ret);
        }
        /// <summary>
        /// Reads an attributes value
        /// </summary>
        private string readAttributeValue(SectionedStreamReader strm)
        {
            builder.Clear();

            while (strm.CurrentSection == "AttributeData")
            {
                builder.AppendLine(strm.ReadLine());
            }

            string serialized;

            if (builder.Length == 0)
            {
                serialized = strm.ReadLine();
            }
            else
            {
                serialized = builder.ToString();
            }
            return(serialized);
        }