Beispiel #1
0
        public static bool Initialize()
        {
            if (!RealmDBMgr.Initialized)
            {
                RealmDBMgr.Initialized        = true;
                DatabaseUtil.DBErrorHook      = (Predicate <Exception>)(exception => CharacterRecord.GetCount() < 100);
                DatabaseUtil.DBType           = RealmServerConfiguration.DatabaseType;
                DatabaseUtil.ConnectionString = RealmServerConfiguration.DBConnectionString;
                DatabaseUtil.DefaultCharset   = RealmDBMgr.DefaultCharset;
                Assembly assembly = typeof(RealmDBMgr).Assembly;
                try
                {
                    if (!DatabaseUtil.InitAR(assembly))
                    {
                        return(false);
                    }
                }
                catch (Exception ex1)
                {
                    RealmDBMgr.OnDBError(ex1);
                    try
                    {
                        if (!DatabaseUtil.InitAR(assembly))
                        {
                            return(false);
                        }
                    }
                    catch (Exception ex2)
                    {
                        LogUtil.ErrorException(ex2, true, "Failed to initialize the Database.", new object[0]);
                    }
                }
            }

            int num = 0;

            try
            {
                num = CharacterRecord.GetCount();
            }
            catch
            {
            }

            if (num == 0)
            {
                DatabaseUtil.CreateSchema();
            }
            NHIdGenerator.InitializeCreators(new Action <Exception>(RealmDBMgr.OnDBError));
            ServerApp <WCell.RealmServer.RealmServer> .InitMgr.SignalGlobalMgrReady(typeof(RealmDBMgr));

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Tries to get the first available player entity ID.
        /// </summary>

        /// <returns>a non-zero ID if one was available; 0 otheriwse</returns>
        /// <remarks>If an available ID is found, it will be removed from the database.</remarks>
        public static uint GetPlayerEntityId()
        {
            // Lock so someone else doesn't grab the same row
            s_idLock.Enter();

            try
            {
                EntityIdStorage eid = GetFirstRecycledId(ObjectTypeId.Player);

                if (eid == null)
                {
                    if (CharacterRecord.Exists())
                    {
                        if (s_highestPlayerId == 0)
                        {
                            CharacterRecord highestChr =
                                CharacterRecord.FindFirst(new Order("_entityLowId", false));

                            s_highestPlayerId = highestChr.EntityLowId;
                        }
                    }

                    return(++s_highestPlayerId);
                }
                else
                {
                    RemoveRecycledId(eid);

                    return((uint)eid.EntityId);
                }
            }
            catch (Exception ex)
            {
                s_log.ErrorException("couldn't get an entity id", ex);
            }
            finally
            {
                s_idLock.Exit();
            }

            return(0);
        }
Beispiel #3
0
        public static bool Initialize()
        {
            if (!Initialized)
            {
                Initialized = true;
                DatabaseUtil.DBErrorHook = exception => CharacterRecord.GetCount() < 100;

                DatabaseUtil.DBType           = RealmServerConfiguration.DatabaseType;
                DatabaseUtil.ConnectionString = RealmServerConfiguration.DBConnectionString;
                DatabaseUtil.DefaultCharset   = DefaultCharset;

                var asm = typeof(RealmDBMgr).Assembly;

                try
                {
                    if (!DatabaseUtil.InitAR(asm))
                    {
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    // repeat init
                    OnDBError(e);
                    try
                    {
                        if (!DatabaseUtil.InitAR(asm))
                        {
                            return(false);
                        }
                    }
                    catch (Exception e2)
                    {
                        LogUtil.ErrorException(e2, true, "Failed to initialize the Database.");
                    }
                }
            }

            // Create tables if not already existing:
            // NHibernate wraps up all added Persistors once the first connection to the DB is established
            // which again happens during the first query to the DB - The line to check for any existing Characters.

            // After the first query, you cannot register any further types.
            var count = 0;

            try
            {
                count = CharacterRecord.GetCount();
            }
            catch
            {
            }

            if (count == 0)
            {
                // in case that the CharacterRecord table does not exist -> Recreate schema
                DatabaseUtil.CreateSchema();
            }

            NHIdGenerator.InitializeCreators(OnDBError);

            RealmServer.InitMgr.SignalGlobalMgrReady(typeof(RealmDBMgr));
            return(true);
        }