Exemplo n.º 1
0
        static void Main(string[] args)
        {
            /*RpDbDatabase database = new RpDbDatabase();
             * database.tables = new RpDb.Entities.RpDbTable[2];
             *
             * database.tables[0] = new RpDb.Entities.RpDbTable("TableTest0", typeof(TestingClass));
             * database.tables[0].AddNewEntry(new TestingClass());
             * database.tables[0].AddNewEntry(new TestingClass());
             * database.tables[0].AddNewEntry(new TestingClass());
             * database.tables[0].AddNewEntry(new TestingClass());
             *
             * database.tables[1] = new RpDb.Entities.RpDbTable("TableTest0", typeof(TestingClass));
             * database.tables[1].AddNewEntry(new TestingClass());
             * database.tables[1].AddNewEntry(new TestingClass());
             * database.tables[1].AddNewEntry(new TestingClass());
             * database.tables[1].AddNewEntry(new TestingClass());
             *
             * database.SaveNow(@"E:\testdb.bin");
             *
             * Console.WriteLine("done");
             * Console.ReadLine();*/

            RpDbDatabase readDb = RpDbDatabase.LoadDatabase(@"E:\testdb.bin", new Type[] { typeof(TestingClass) });

            var table = readDb.tables[0];

            foreach (var uuid in table.uuidLookup.Keys)
            {
                RpDbTableEntry entry = table.ReadResultByUuid(uuid);
                Console.WriteLine(((TestingClass)entry.data).testingInt);
            }

            Console.WriteLine("Done");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        public static RpDbTableEntry ReadEntry(Stream s, RpDbTable table)
        {
            //Create object.
            RpDbTableEntry entry = new RpDbTableEntry(table, null);

            entry.offset = (UInt32)s.Position;
            //First, read in the UUID.
            UInt32 uuid = ReaderTools.ReadUInt32(s);

            entry.uuid = uuid;
            //Read in the lengths.
            UInt16[] lengths = new UInt16[table.typeOrder.Length];
            for (int i = 0; i < table.typeOrder.Length; i++)
            {
                lengths[i] = ReaderTools.ReadUInt16(s);
            }
            //Now, read in the data
            object obj = Activator.CreateInstance(table.type);

            for (int i = 0; i < table.typeOrder.Length; i++)
            {
                Writer.PropIdPair pair = table.typeOrder[i];
                //Skip this if the length is zero. (it's null)
                UInt16 length = lengths[i];
                if (length != 0)
                {
                    //Read in the data.
                    byte[] buf = new byte[length];
                    s.Read(buf, 0, length);
                    //Todo: Actually get the data from it.
                    //For now, just read it in as a string
                    object d = Encoding.UTF8.GetString(buf);
                    //Now, set it inside the object.
                    pair.prop.SetValue(obj, d, null);
                }
            }
            //Set the entry's object to this.
            entry.data = obj;
            //Return the entry.
            return(entry);
        }
Exemplo n.º 3
0
        public static UInt32 WriteEntry(Stream dataStream, Stream tableStream, RpDbTableEntry entry, List <PropIdPair> order)
        {
            //Add the data and return the offset.
            UInt32 offset = (UInt32)dataStream.Position;

            //Write the offset and uuid to the table.
            WriterTools.WriteUInt32(tableStream, offset);
            WriterTools.WriteUInt32(tableStream, entry.uuid);
            //Now, we'll write the data.
            //Get the serialized data for this class, following the order offered.
            byte[][] serData = new byte[order.Count][]; //Data to be serialized.
            for (int i = 0; i < order.Count; i++)
            {
                //This is run for each property in this. Serialize it.
                var pair = order[i];
                var prop = pair.prop;
                //Get the value from this using the property found earlier.
                object value = prop.GetValue(entry.data, null);
                Type   type  = value.GetType();
                //Convert this to bytes.
                byte[] data = GetSerializedData(value, type);
                //Add it.
                serData[i] = data;
            }
            //Write UUID
            WriterTools.WriteUInt32(dataStream, entry.uuid);
            //Write the lengths
            foreach (byte[] d in serData)
            {
                WriterTools.WriteUInt16(dataStream, (ushort)d.Length);
            }
            //Now, write all the data.
            foreach (byte[] d in serData)
            {
                dataStream.Write(d, 0, d.Length);
            }

            //Return offset.
            return(offset);
        }