Beispiel #1
0
        public static IDatabaseObject Load(Type _type, uint _uid)
        {
            IDatabaseObject database = null;

            CheckDirectory(AppDomain.CurrentDomain.BaseDirectory + "/Database/" + _type.Name);

            try
            {
                if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/Database/" + _type.Name + "/" + _uid + ".sdb"))
                {
                    if (ListLastUID.ContainsKey(_type) == false || _uid > ListLastUID[_type])
                    {
                        ListLastUID[_type] = _uid;
                    }

                    database    = (IDatabaseObject)_type.Assembly.CreateInstance(_type.FullName);
                    database.ID = _uid;

                    byte[] buffer_database = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "/Database/" + _type.Name + "/" + _uid + ".sdb");

                    using (BufferReader reader = new BufferReader(buffer_database))
                    {
                        PropertyInfo[] propertyes = database.GetType().GetProperties();
                        for (int i = 0; i < propertyes.Length; ++i)
                        {
                            switch (propertyes[i].PropertyType.Name)
                            {
                            case "String":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.String() });
                                break;

                            case "Char":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Char() });
                                break;

                            case "Double":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Double() });
                                break;

                            case "Boolean":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Boolean() });
                                break;

                            case "Byte":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Byte() });
                                break;

                            case "Single":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Float() });
                                break;

                            case "Byte[]":
                                int buffer_len = reader.Int32();
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Bytes(buffer_len) });
                                break;

                            case "Int16":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Int16() });
                                break;

                            case "Int32":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Int32() });
                                break;

                            case "Int64":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.Int64() });
                                break;

                            case "UInt16":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.UInt16() });
                                break;

                            case "UInt32":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.UInt32() });
                                break;

                            case "UInt64":
                                propertyes[i].GetSetMethod(true).Invoke(database, new object[] { reader.UInt64() });
                                break;
                            }
                        }
                    }

                    return(database);
                }
            }
            catch (Exception ex)
            {
                ConsoleSystem.LogError($"[Database.Manager]: Exception from load {_type.Name} id {_uid} database: " + ex.Message);
            }
            return(null);
        }