2020-04-02 22:40:27 -05:00
using System ;
2024-12-18 12:17:00 -05:00
using System.Collections.Concurrent ;
using Microsoft.EntityFrameworkCore ;
2017-05-02 09:25:35 -05:00
2018-03-18 20:01:22 -04:00
using log4net ;
2017-05-02 09:25:35 -05:00
namespace ACE.Database
{
2018-02-10 06:05:13 -06:00
public static class DatabaseManager
2017-05-02 09:25:35 -05:00
{
2018-03-18 20:01:22 -04:00
private static readonly ILog log = LogManager . GetLogger ( System . Reflection . MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
2018-02-10 06:05:13 -06:00
public static AuthenticationDatabase Authentication { get ; } = new AuthenticationDatabase ( ) ;
2017-06-30 08:08:43 -04:00
2020-03-02 08:03:50 -06:00
public static WorldDatabaseWithEntityCache World { get ; } = new WorldDatabaseWithEntityCache ( ) ;
2017-05-02 09:25:35 -05:00
2018-02-10 06:05:13 -06:00
private static SerializedShardDatabase serializedShardDb ;
2017-05-02 09:25:35 -05:00
2018-02-10 06:05:13 -06:00
public static SerializedShardDatabase Shard { get ; private set ; }
2017-05-02 09:25:35 -05:00
2018-08-10 13:52:19 -05:00
public static ShardConfigDatabase ShardConfig { get ; } = new ShardConfigDatabase ( ) ;
2021-01-16 16:24:42 -05:00
public static bool InitializationFailure = false ;
2017-10-21 13:32:06 -04:00
public static void Initialize ( bool autoRetry = true )
2017-05-02 09:25:35 -05:00
{
2018-02-14 12:06:26 -06:00
Authentication . Exists ( true ) ;
2018-08-10 13:52:19 -05:00
2020-02-29 16:58:29 -05:00
if ( Authentication . GetListofAccountsByAccessLevel ( ACE . Entity . Enum . AccessLevel . Admin ) . Count = = 0 )
{
2021-01-16 16:24:42 -05:00
log . Warn ( "Authentication Database does not contain any admin accounts. The next account to be created will automatically be promoted to an Admin account." ) ;
2020-02-29 16:58:29 -05:00
AutoPromoteNextAccountToAdmin = true ;
}
else
AutoPromoteNextAccountToAdmin = false ;
2018-02-14 12:06:26 -06:00
World . Exists ( true ) ;
2018-02-10 06:05:13 -06:00
2021-01-16 16:24:42 -05:00
if ( ! World . IsWorldDatabaseGuidRangeValid ( ) )
{
log . Fatal ( "World Database contains instance GUIDs outside of static range which will prevent GuidManager from properly assigning GUIDs and can result in GUID exhaustion prematurely." ) ;
InitializationFailure = true ;
return ;
}
2018-08-10 13:52:19 -05:00
var playerWeenieLoadTest = World . GetCachedWeenie ( "human" ) ;
if ( playerWeenieLoadTest = = null )
2021-01-16 16:24:42 -05:00
{
log . Fatal ( "World Database does not contain the weenie for human (1). Characters cannot be created or logged into until the missing weenie is restored." ) ;
InitializationFailure = true ;
return ;
}
2018-08-10 13:52:19 -05:00
2020-04-02 22:40:27 -05:00
// By default, we hold on to player biotas a little bit longer to help with offline updates like pass-up xp, allegiance updates, etc...
2020-11-20 12:30:18 -05:00
var shardDb = new ShardDatabaseWithCaching ( TimeSpan . FromMinutes ( Common . ConfigManager . Config . Server . ShardPlayerBiotaCacheTime ) , TimeSpan . FromMinutes ( Common . ConfigManager . Config . Server . ShardNonPlayerBiotaCacheTime ) ) ;
2018-02-10 06:05:13 -06:00
serializedShardDb = new SerializedShardDatabase ( shardDb ) ;
Shard = serializedShardDb ;
2018-02-14 12:06:26 -06:00
shardDb . Exists ( true ) ;
2017-05-02 09:25:35 -05:00
}
2017-06-30 08:08:43 -04:00
2020-02-29 16:58:29 -05:00
public static bool AutoPromoteNextAccountToAdmin { get ; set ; }
2017-06-30 08:08:43 -04:00
public static void Start ( )
{
serializedShardDb . Start ( ) ;
}
public static void Stop ( )
{
2020-04-12 17:01:22 -04:00
if ( serializedShardDb ! = null )
serializedShardDb . Stop ( ) ;
2017-06-30 08:08:43 -04:00
}
2024-12-18 12:17:00 -05:00
private static readonly ConcurrentDictionary < string , ServerVersion > cachedServerVersions = new ( ) ;
public static ServerVersion CachedServerVersionAutoDetect ( string database , string connectionString )
{
if ( ! cachedServerVersions . TryGetValue ( database , out ServerVersion serverVersion ) )
{
serverVersion = ServerVersion . AutoDetect ( connectionString ) ;
cachedServerVersions [ database ] = serverVersion ;
}
return serverVersion ;
}
2017-05-02 09:25:35 -05:00
}
2018-02-10 06:05:13 -06:00
}