2020-03-02 04:24:47 -05:00
using System ;
using System.IO ;
using System.IO.Compression ;
2020-05-08 01:44:21 -04:00
using System.Linq ;
2023-12-21 14:15:38 -05:00
using System.Text.Json ;
2020-03-02 04:24:47 -05:00
using System.Threading ;
2023-12-21 14:15:38 -05:00
using ACE.Common ;
2020-03-02 04:24:47 -05:00
namespace ACE.Server
{
partial class Program
{
private static void DoOutOfBoxSetup ( string configFile )
{
2023-12-21 14:15:38 -05:00
MasterConfiguration config ;
2020-03-02 04:24:47 -05:00
var exeLocation = Path . GetDirectoryName ( System . Reflection . Assembly . GetExecutingAssembly ( ) . Location ) ;
var configJsExample = Path . Combine ( exeLocation , "Config.js.example" ) ;
var exampleFile = new FileInfo ( configJsExample ) ;
if ( ! exampleFile . Exists )
{
2023-12-21 14:15:38 -05:00
config = new MasterConfiguration ( ) ;
2020-03-02 04:24:47 -05:00
}
else
{
if ( ! IsRunningInContainer )
{
2023-12-21 14:15:38 -05:00
Console . WriteLine ( "config.js Configuration file is missing, cloning from example file." ) ;
2020-03-02 04:24:47 -05:00
File . Copy ( configJsExample , configFile , true ) ;
}
else
{
2023-12-21 14:15:38 -05:00
Console . WriteLine ( "config.js Configuration file is missing, ACEmulator is running in a container, cloning from docker file." ) ;
2020-03-02 04:24:47 -05:00
var configJsDocker = Path . Combine ( exeLocation , "Config.js.docker" ) ;
File . Copy ( configJsDocker , configFile , true ) ;
}
2023-12-21 14:15:38 -05:00
var fileText = File . ReadAllText ( configFile ) ;
2024-01-02 09:53:20 -06:00
config = JsonSerializer . Deserialize < MasterConfiguration > ( fileText , ConfigManager . SerializerOptions ) ;
2023-12-21 14:15:38 -05:00
}
2020-03-02 04:24:47 -05:00
Console . WriteLine ( "Performing setup for ACEmulator..." ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( "Welcome to ACEmulator! To configure your world for first use, please follow the instructions below. Press enter at each prompt to accept default values." ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
2024-02-01 02:20:25 -06:00
var variable = string . Empty ;
var nonInteractiveSetup = Convert . ToBoolean ( Environment . GetEnvironmentVariable ( "ACE_NONINTERACTIVE_SETUP" ) ) ;
2020-03-02 04:24:47 -05:00
Console . Write ( $"Enter the name for your World (default: \" { config . Server . WorldName } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_WORLD_NAME" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . Server . WorldName = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( "The next two entries should use defaults, unless you have specific network environments..." ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the Host address for your World (default: \" { config . Server . Network . Host } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = "0.0.0.0" ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . Server . Network . Host = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the Port for your World (default: \" { config . Server . Network . Port } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = "9000" ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . Server . Network . Port = Convert . ToUInt32 ( variable . Trim ( ) ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the directory location for your DAT files (default: \" { config . Server . DatFilesDirectory } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_DAT_FILES_DIRECTORY" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
{
var path = Path . GetFullPath ( variable . Trim ( ) ) ;
if ( ! Path . EndsInDirectorySeparator ( path ) )
path + = Path . DirectorySeparatorChar ;
//path = path.Replace($"{Path.DirectorySeparatorChar}", $"{Path.DirectorySeparatorChar}{Path.DirectorySeparatorChar}");
config . Server . DatFilesDirectory = path ;
}
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
2022-01-19 12:02:23 -05:00
Console . WriteLine ( "Next we will configure your SQL server connections. You will need to provide a database name, username and password for each." ) ;
2020-03-02 04:24:47 -05:00
Console . WriteLine ( "Default names for the databases are recommended, and it is also recommended you not use root for login to database. The password must not be blank." ) ;
Console . WriteLine ( "It is also recommended the SQL server be hosted on the same machine as this server, so defaults for Host and Port would be ideal as well." ) ;
Console . WriteLine ( "As before, pressing enter will use default value." ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the database name for your authentication database (default: \" { config . MySql . Authentication . Database } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_SQL_AUTH_DATABASE_NAME" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Authentication . Database = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the database name for your shard database (default: \" { config . MySql . Shard . Database } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_SQL_SHARD_DATABASE_NAME" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Shard . Database = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the database name for your world database (default: \" { config . MySql . World . Database } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_SQL_WORLD_DATABASE_NAME" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . World . Database = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . Write ( "Typically, all three databases will be on the same SQL server, is this how you want to proceed? (Y/n) " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = "n" ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! variable . Equals ( "n" , StringComparison . OrdinalIgnoreCase ) & & ! variable . Equals ( "no" , StringComparison . OrdinalIgnoreCase ) )
{
Console . Write ( $"Enter the Host address for your SQL server (default: \" { config . MySql . World . Host } \ "): " ) ;
variable = Console . ReadLine ( ) ;
if ( ! string . IsNullOrWhiteSpace ( variable ) )
{
config . MySql . Authentication . Host = variable . Trim ( ) ;
config . MySql . Shard . Host = variable . Trim ( ) ;
config . MySql . World . Host = variable . Trim ( ) ;
}
Console . WriteLine ( ) ;
Console . Write ( $"Enter the Port for your SQL server (default: \" { config . MySql . World . Port } \ "): " ) ;
variable = Console . ReadLine ( ) ;
if ( ! string . IsNullOrWhiteSpace ( variable ) )
{
config . MySql . Authentication . Port = Convert . ToUInt32 ( variable . Trim ( ) ) ;
config . MySql . Shard . Port = Convert . ToUInt32 ( variable . Trim ( ) ) ;
config . MySql . World . Port = Convert . ToUInt32 ( variable . Trim ( ) ) ;
}
Console . WriteLine ( ) ;
}
else
{
Console . Write ( $"Enter the Host address for your authentication database (default: \" { config . MySql . Authentication . Host } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_SQL_AUTH_DATABASE_HOST" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Authentication . Host = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the Port for your authentication database (default: \" { config . MySql . Authentication . Port } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_SQL_AUTH_DATABASE_PORT" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Authentication . Port = Convert . ToUInt32 ( variable . Trim ( ) ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the Host address for your shard database (default: \" { config . MySql . Shard . Host } \ "): " ) ;
2025-01-14 17:52:17 -08:00
if ( ! nonInteractiveSetup )
2024-02-01 02:20:25 -06:00
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_SQL_SHARD_DATABASE_HOST" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Shard . Host = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the Port for your shard database (default: \" { config . MySql . Shard . Port } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_SQL_SHARD_DATABASE_PORT" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Shard . Port = Convert . ToUInt32 ( variable . Trim ( ) ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the Host address for your world database (default: \" { config . MySql . World . Host } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_SQL_WORLD_DATABASE_HOST" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . World . Host = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the Port for your world database (default: \" { config . MySql . World . Port } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "ACE_SQL_WORLD_DATABASE_PORT" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . World . Port = Convert . ToUInt32 ( variable . Trim ( ) ) ;
Console . WriteLine ( ) ;
}
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
2025-01-14 20:53:05 -05:00
Console . Write ( "Typically, all three databases will be using the same SQL server credentials, is this how you want to proceed? (Y/n) " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = "y" ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! variable . Equals ( "n" , StringComparison . OrdinalIgnoreCase ) & & ! variable . Equals ( "no" , StringComparison . OrdinalIgnoreCase ) )
{
Console . Write ( $"Enter the username for your SQL server (default: \" { config . MySql . World . Username } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "MYSQL_USER" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
{
config . MySql . Authentication . Username = variable . Trim ( ) ;
config . MySql . Shard . Username = variable . Trim ( ) ;
config . MySql . World . Username = variable . Trim ( ) ;
}
Console . WriteLine ( ) ;
Console . Write ( $"Enter the password for your SQL server (default: \" { config . MySql . World . Password } \ "): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Environment . GetEnvironmentVariable ( "MYSQL_PASSWORD" ) ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! string . IsNullOrWhiteSpace ( variable ) )
{
config . MySql . Authentication . Password = variable . Trim ( ) ;
config . MySql . Shard . Password = variable . Trim ( ) ;
config . MySql . World . Password = variable . Trim ( ) ;
}
}
else
{
Console . Write ( $"Enter the username for your authentication database (default: \" { config . MySql . Authentication . Username } \ "): " ) ;
variable = Console . ReadLine ( ) ;
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Authentication . Username = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the password for your authentication database (default: \" { config . MySql . Authentication . Password } \ "): " ) ;
variable = Console . ReadLine ( ) ;
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Authentication . Password = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the username for your shard database (default: \" { config . MySql . Shard . Username } \ "): " ) ;
variable = Console . ReadLine ( ) ;
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Shard . Username = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the password for your shard database (default: \" { config . MySql . Shard . Password } \ "): " ) ;
variable = Console . ReadLine ( ) ;
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . Shard . Password = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the username for your world database (default: \" { config . MySql . World . Username } \ "): " ) ;
variable = Console . ReadLine ( ) ;
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . World . Username = variable . Trim ( ) ;
Console . WriteLine ( ) ;
Console . Write ( $"Enter the password for your world database (default: \" { config . MySql . World . Password } \ "): " ) ;
variable = Console . ReadLine ( ) ;
if ( ! string . IsNullOrWhiteSpace ( variable ) )
config . MySql . World . Password = variable . Trim ( ) ;
}
2023-12-21 14:15:38 -05:00
Console . WriteLine ( "commiting configuration to disk..." ) ;
2024-01-02 09:53:20 -06:00
var jsonString = JsonSerializer . Serialize ( config , ConfigManager . SerializerOptions ) ;
2023-12-21 14:15:38 -05:00
File . WriteAllText ( configFile , jsonString ) ;
2020-03-02 04:24:47 -05:00
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
2025-01-14 20:53:05 -05:00
Console . Write ( "Do you want ACEmulator to attempt to initialize your SQL databases? This will erase any existing ACEmulator specific databases that may already exist on the server (Y/n): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
2023-07-20 02:05:18 -04:00
var sqlConnectInfo = $"server={config.MySql.World.Host};port={config.MySql.World.Port};user={config.MySql.World.Username};password={config.MySql.World.Password};{config.MySql.World.ConnectionOptions}" ;
2023-07-01 01:58:55 -06:00
var sqlConnect = new MySqlConnector . MySqlConnection ( sqlConnectInfo ) ;
2024-02-01 02:20:25 -06:00
if ( nonInteractiveSetup )
{
variable = Convert . ToBoolean ( Environment . GetEnvironmentVariable ( "ACE_SQL_INITIALIZE_DATABASES" ) ) ? "y" : "n" ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! variable . Equals ( "n" , StringComparison . OrdinalIgnoreCase ) & & ! variable . Equals ( "no" , StringComparison . OrdinalIgnoreCase ) )
{
Console . WriteLine ( ) ;
Console . Write ( $"Waiting for connection to SQL server at {config.MySql.World.Host}:{config.MySql.World.Port} .... " ) ;
for ( ; ; )
{
try
{
2023-07-20 02:05:18 -04:00
using ( var sqlTestConnection = new MySqlConnector . MySqlConnection ( $"server={config.MySql.World.Host};port={config.MySql.World.Port};user={config.MySql.World.Username};password={config.MySql.World.Password};{config.MySql.World.ConnectionOptions}" ) )
2020-03-02 04:24:47 -05:00
{
Console . Write ( "." ) ;
sqlTestConnection . Open ( ) ;
}
break ;
}
2023-07-01 01:58:55 -06:00
catch ( MySqlConnector . MySqlException )
2020-03-02 04:24:47 -05:00
{
2020-03-24 00:58:12 -04:00
Console . Write ( "." ) ;
2020-03-02 04:24:47 -05:00
Thread . Sleep ( 5000 ) ;
}
}
Console . WriteLine ( " connected!" ) ;
if ( IsRunningInContainer )
{
2024-02-01 02:20:25 -06:00
// if using our supplied docker compose file which includes mysql server, mysql is initialized with a test database called ace%, we will delete this database if it exists
2020-03-02 04:24:47 -05:00
Console . Write ( "Clearing out temporary ace% database .... " ) ;
2024-02-01 02:20:25 -06:00
var sqlDBFile = "DROP DATABASE IF EXISTS `ace%`;" ;
2023-07-01 01:58:55 -06:00
var script = new MySqlConnector . MySqlCommand ( sqlDBFile , sqlConnect ) ;
2020-03-02 04:24:47 -05:00
Console . Write ( $"Importing into SQL server at {config.MySql.World.Host}:{config.MySql.World.Port} .... " ) ;
try
{
2023-07-01 01:58:55 -06:00
ExecuteScript ( script ) ;
2020-03-02 04:24:47 -05:00
}
2023-07-01 01:58:55 -06:00
catch ( MySqlConnector . MySqlException )
2020-03-02 04:24:47 -05:00
{
}
Console . WriteLine ( " done!" ) ;
}
Console . WriteLine ( "Searching for base SQL scripts .... " ) ;
2020-05-08 01:44:21 -04:00
foreach ( var file in new DirectoryInfo ( $"DatabaseSetupScripts{Path.DirectorySeparatorChar}Base" ) . GetFiles ( "*.sql" ) . OrderBy ( f = > f . Name ) )
2020-03-02 04:24:47 -05:00
{
Console . Write ( $"Found {file.Name} .... " ) ;
var sqlDBFile = File . ReadAllText ( file . FullName ) ;
switch ( file . Name )
{
2022-01-19 12:02:23 -05:00
case "AuthenticationBase.sql" :
2023-07-20 02:05:18 -04:00
sqlConnectInfo = $"server={config.MySql.Authentication.Host};port={config.MySql.Authentication.Port};user={config.MySql.Authentication.Username};password={config.MySql.Authentication.Password};{config.MySql.Shard.ConnectionOptions}" ;
2022-01-19 12:02:23 -05:00
sqlDBFile = sqlDBFile . Replace ( "ace_auth" , config . MySql . Authentication . Database ) ;
2020-03-02 04:24:47 -05:00
break ;
2022-01-19 12:02:23 -05:00
case "ShardBase.sql" :
2023-07-20 02:05:18 -04:00
sqlConnectInfo = $"server={config.MySql.Shard.Host};port={config.MySql.Shard.Port};user={config.MySql.Shard.Username};password={config.MySql.Shard.Password};{config.MySql.Shard.ConnectionOptions}" ;
2022-01-19 12:02:23 -05:00
sqlDBFile = sqlDBFile . Replace ( "ace_shard" , config . MySql . Shard . Database ) ;
break ;
case "WorldBase.sql" :
default :
2023-07-20 02:05:18 -04:00
//sqlConnectInfo = $"server={config.MySql.World.Host};port={config.MySql.World.Port};user={config.MySql.World.Username};password={config.MySql.World.Password};{config.MySql.Shard.ConnectionOptions}";
2022-01-19 12:02:23 -05:00
sqlDBFile = sqlDBFile . Replace ( "ace_world" , config . MySql . World . Database ) ;
2020-03-02 04:24:47 -05:00
break ;
}
2023-07-01 01:58:55 -06:00
var script = new MySqlConnector . MySqlCommand ( sqlDBFile , sqlConnect ) ;
2020-03-02 04:24:47 -05:00
Console . Write ( $"Importing into SQL server at {config.MySql.World.Host}:{config.MySql.World.Port} .... " ) ;
try
{
2023-07-01 01:58:55 -06:00
ExecuteScript ( script ) ;
2020-03-02 04:24:47 -05:00
}
2023-07-01 01:58:55 -06:00
catch ( MySqlConnector . MySqlException )
2020-03-02 04:24:47 -05:00
{
}
Console . WriteLine ( " complete!" ) ;
}
Console . WriteLine ( "Base SQL scripts import complete!" ) ;
Console . WriteLine ( "Searching for Update SQL scripts .... " ) ;
2022-01-19 12:02:23 -05:00
PatchDatabase ( "Authentication" , config . MySql . Authentication . Host , config . MySql . Authentication . Port , config . MySql . Authentication . Username , config . MySql . Authentication . Password , config . MySql . Authentication . Database , config . MySql . Shard . Database , config . MySql . World . Database ) ;
2020-03-02 04:24:47 -05:00
2022-01-19 12:02:23 -05:00
PatchDatabase ( "Shard" , config . MySql . Shard . Host , config . MySql . Shard . Port , config . MySql . Shard . Username , config . MySql . Shard . Password , config . MySql . Authentication . Database , config . MySql . Shard . Database , config . MySql . World . Database ) ;
2020-03-02 04:24:47 -05:00
2022-01-19 12:02:23 -05:00
PatchDatabase ( "World" , config . MySql . World . Host , config . MySql . World . Port , config . MySql . World . Username , config . MySql . World . Password , config . MySql . Authentication . Database , config . MySql . Shard . Database , config . MySql . World . Database ) ;
2020-03-02 04:24:47 -05:00
}
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( ) ;
Console . Write ( "Do you want to download the latest world database and import it? (Y/n): " ) ;
2024-02-01 02:20:25 -06:00
if ( ! nonInteractiveSetup )
variable = Console . ReadLine ( ) ;
else
{
variable = Convert . ToBoolean ( Environment . GetEnvironmentVariable ( "ACE_SQL_DOWNLOAD_LATEST_WORLD_RELEASE" ) ) ? "y" : "n" ;
Console . WriteLine ( $"{variable}" ) ;
}
2020-03-02 04:24:47 -05:00
if ( ! variable . Equals ( "n" , StringComparison . OrdinalIgnoreCase ) & & ! variable . Equals ( "no" , StringComparison . OrdinalIgnoreCase ) )
{
Console . WriteLine ( ) ;
if ( IsRunningInContainer )
{
Console . WriteLine ( " " ) ;
Console . WriteLine ( "This process will take a while, depending on many factors, and may look stuck while reading and importing the world database, please be patient! " ) ;
Console . WriteLine ( " " ) ;
}
Console . Write ( "Looking up latest release from ACEmulator/ACE-World-16PY-Patches .... " ) ;
2022-05-07 11:30:42 -07:00
var url = "https://api.github.com/repos/ACEmulator/ACE-World-16PY-Patches/releases/latest" ;
2022-07-13 12:30:32 -04:00
using var client = new WebClient ( ) ;
var html = client . GetStringFromURL ( url ) . Result ;
2020-03-02 04:24:47 -05:00
2023-12-21 14:15:38 -05:00
var json = JsonSerializer . Deserialize < JsonElement > ( html ) ;
string tag = json . GetProperty ( "tag_name" ) . GetString ( ) ;
string dbURL = json . GetProperty ( "assets" ) [ 0 ] . GetProperty ( "browser_download_url" ) . GetString ( ) ;
string dbFileName = json . GetProperty ( "assets" ) [ 0 ] . GetProperty ( "name" ) . GetString ( ) ;
2020-03-02 04:24:47 -05:00
Console . WriteLine ( $"Found {tag} !" ) ;
Console . Write ( $"Downloading {dbFileName} .... " ) ;
2022-07-13 12:30:32 -04:00
var dlTask = client . DownloadFile ( dbURL , dbFileName ) ;
dlTask . Wait ( ) ;
2020-03-02 04:24:47 -05:00
Console . WriteLine ( "download complete!" ) ;
Console . Write ( $"Extracting {dbFileName} .... " ) ;
ZipFile . ExtractToDirectory ( dbFileName , "." , true ) ;
Console . WriteLine ( "extraction complete!" ) ;
Console . Write ( $"Deleting {dbFileName} .... " ) ;
File . Delete ( dbFileName ) ;
Console . WriteLine ( "Deleted!" ) ;
2020-03-24 00:58:12 -04:00
var sqlFile = dbFileName . Substring ( 0 , dbFileName . Length - 4 ) ;
2020-03-02 04:24:47 -05:00
Console . Write ( $"Importing {sqlFile} into SQL server at {config.MySql.World.Host}:{config.MySql.World.Port} (This will take a while, please be patient) .... " ) ;
2020-03-24 00:58:12 -04:00
using ( var sr = File . OpenText ( sqlFile ) )
2020-03-02 04:24:47 -05:00
{
2020-03-24 00:58:12 -04:00
var line = string . Empty ;
var completeSQLline = string . Empty ;
2022-01-19 12:02:23 -05:00
var dbname = config . MySql . World . Database ;
2020-03-24 00:58:12 -04:00
while ( ( line = sr . ReadLine ( ) ) ! = null )
{
2022-01-19 12:02:23 -05:00
line = line . Replace ( "ace_world" , dbname ) ;
2020-03-24 00:58:12 -04:00
//do minimal amount of work here
if ( line . EndsWith ( ";" ) )
{
completeSQLline + = line + Environment . NewLine ;
2023-07-01 01:58:55 -06:00
var script = new MySqlConnector . MySqlCommand ( completeSQLline , sqlConnect ) ;
2020-03-24 00:58:12 -04:00
try
{
2023-07-01 01:58:55 -06:00
ExecuteScript ( script ) ;
2020-03-24 00:58:12 -04:00
}
2023-07-01 01:58:55 -06:00
catch ( MySqlConnector . MySqlException )
2020-03-24 00:58:12 -04:00
{
}
completeSQLline = string . Empty ;
}
else
completeSQLline + = line + Environment . NewLine ;
}
2020-03-02 04:24:47 -05:00
}
Console . WriteLine ( " complete!" ) ;
Console . Write ( $"Deleting {sqlFile} .... " ) ;
File . Delete ( sqlFile ) ;
Console . WriteLine ( "Deleted!" ) ;
}
2023-07-01 01:58:55 -06:00
CleanupConnection ( sqlConnect ) ;
2020-03-02 04:24:47 -05:00
Console . WriteLine ( "exiting setup for ACEmulator." ) ;
}
2023-07-01 01:58:55 -06:00
private static void ExecuteScript ( MySqlConnector . MySqlCommand scriptCommand )
2020-03-02 04:24:47 -05:00
{
2023-07-01 01:58:55 -06:00
if ( scriptCommand . Connection . State ! = System . Data . ConnectionState . Open )
{
scriptCommand . Connection . Open ( ) ;
}
scriptCommand . ExecuteNonQuery ( ) ;
2020-03-02 04:24:47 -05:00
Console . Write ( "." ) ;
}
2023-07-01 01:58:55 -06:00
private static void CleanupConnection ( MySqlConnector . MySqlConnection connection )
{
if ( connection . State ! = System . Data . ConnectionState . Closed )
{
try
{
connection . Close ( ) ;
}
catch
{
}
}
}
2020-03-02 04:24:47 -05:00
}
}