Пример #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();
        }
Пример #2
0
        public static RpDbDatabase ReadDatabase(Stream s, Type[] types)
        {
            //Read "RpDb" at the beginning
            char[] intro = ReaderTools.ReadChars(s, 4);
            //Read the version.
            UInt16 version = ReaderTools.ReadUInt16(s);

            //Skip 32 bytes of reserved space.
            s.Position += 32;
            //Read table count.
            UInt16 tableCount = ReaderTools.ReadUInt16(s);
            //Create the database object.
            RpDbDatabase db = new RpDbDatabase(s);

            //Create the tables array in the database.
            db.tables = new RpDbTable[tableCount];
            //Read the table offsets.
            UInt32[] tableOffsets = new UInt32[tableCount];
            string[] tableNames   = new string[tableCount];
            //Read each of the offsets and table names.
            for (int i = 0; i < tableCount; i++)
            {
                tableOffsets[i] = ReaderTools.ReadUInt32(s);
                //Read the name for verification.
                tableNames[i] = ReaderTools.ReadString(s);
            }
            //Read each of the tables, starting at the offsets.
            UInt32 startingOffset = (UInt32)s.Position;

            for (int tableId = 0; tableId < tableCount; tableId++)
            {
                //Jump to the position offered.
                s.Position         = startingOffset + tableOffsets[tableId];
                db.tables[tableId] = TableReader.ReadTable(s, types, db);
            }
            //Return the database.
            return(db);
        }
Пример #3
0
        public static RpDbTable ReadTable(Stream s, Type[] types, RpDbDatabase database)
        {
            //Read the table name.
            string name = ReaderTools.ReadString(s);
            //Read the type
            string typeName = ReaderTools.ReadString(s);
            //Determine the type.
            Type t = null;

            //Loop through types and find match.
            foreach (Type tt in types)
            {
                if (tt.ToString() == typeName)
                {
                    t = tt;
                }
            }
            //Check if it was found.
            if (t == null)
            {
                throw new Exception("Failed to find type '" + typeName + "'.");
            }
            //Create the object.
            RpDbTable table = new RpDbTable(name, t, database);
            //Read the number of entries.
            UInt32 entryCount = ReaderTools.ReadUInt32(s);
            //Read number of types.
            UInt16 typeCount = ReaderTools.ReadUInt16(s);

            //Read the type table in. This'll give us the order.
            Writer.PropIdPair[] order = new Writer.PropIdPair[typeCount];
            for (int i = 0; i < typeCount; i++)
            {
                //Read in.
                UInt16      typeId = ReaderTools.ReadUInt16(s);
                ClassAttrib attr   = null;
                System.Reflection.PropertyInfo chosenProp = null;
                //Find this in the object.
                foreach (var prop in table.type.GetProperties())
                {
                    //Check the UUID.
                    ClassAttrib propAttr = Writer.TableWriter.GetUuidFromProperty(prop);
                    //If there is no UUID, skip this.
                    if (propAttr == null)
                    {
                        continue;
                    }
                    //Add this to the order if it matches
                    if (propAttr.uuid == typeId)
                    {
                        attr       = propAttr;
                        chosenProp = prop;
                    }
                }
                if (attr != null && chosenProp != null)
                {
                    order[i] = new Writer.PropIdPair(chosenProp, typeId);
                }
                else
                {
                    Console.WriteLine("No attr found for ID " + typeId.ToString() + ".");
                }
            }
            table.typeOrder = order;
            //Now that we know the order of the types, we can read it in.
            //Skip 32 bytes of reserved data.
            s.Position += 32;
            //Read each of the offsets in. Add them to the dict as we go.
            for (int i = 0; i < entryCount; i++)
            {
                //Read offset
                UInt32 offset = ReaderTools.ReadUInt32(s);
                //Read UUID
                UInt32 uuid = ReaderTools.ReadUInt32(s);
                //Insert.
                table.uuidLookup.Add(uuid, offset);
            }
            //Now, the data begins. Add the flag.
            table.tableDataOffset = s.Position;
            //We can now continue to the next table.
            return(table);
        }
Пример #4
0
 public RpDbTable(string _name, Type _type, RpDbDatabase _db)
 {
     name     = _name;
     type     = _type;
     database = _db;
 }