1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-27 14:57:44 +00:00
sapphire/src/dbm/main.cpp

206 lines
5.5 KiB
C++
Raw Normal View History

2018-11-25 16:45:48 +01:00
#include <iostream>
#include <cctype>
#include <set>
#include <common/Logging/Logger.h>
#include <experimental/filesystem>
#include <MySqlConnector.h>
namespace filesys = std::experimental::filesystem;
#include <fstream>
#include <streambuf>
#include <regex>
#include <map>
#include "DbManager.h"
2018-12-23 03:53:08 +01:00
using namespace Sapphire;
2018-11-25 16:45:48 +01:00
std::vector< std::string > getAllFilesInDir( const std::string& dirPath,
const std::vector< std::string > dirSkipList = {} )
{
// Create a vector of string
std::vector< std::string > listOfFiles;
try
{
// Check if given path exists and points to a directory
if( filesys::exists( dirPath ) && filesys::is_directory( dirPath ) )
{
// Create a Recursive Directory Iterator object and points to the starting of directory
filesys::recursive_directory_iterator iter( dirPath );
// Create a Recursive Directory Iterator object pointing to end.
filesys::recursive_directory_iterator end;
// Iterate till end
while( iter != end )
{
// Check if current entry is a directory and if exists in skip list
if( filesys::is_directory( iter->path() ) &&
( std::find( dirSkipList.begin(), dirSkipList.end(), iter->path().filename() ) != dirSkipList.end() ) )
{
// Skip the iteration of current directory pointed by iterator
// c++17 Filesystem API to skip current directory iteration
iter.disable_recursion_pending();
}
else
{
// Add the name in vector
listOfFiles.push_back( iter->path().string() );
}
std::error_code ec;
2018-11-25 16:45:48 +01:00
// Increment the iterator to point to next entry in recursive iteration
iter.increment( ec );
if( ec )
{
std::cerr << "Error While Accessing : " << iter->path().string() << " :: " << ec.message() << '\n';
}
}
}
}
catch( std::system_error& e )
{
std::cerr << "Exception :: " << e.what();
}
return listOfFiles;
}
std::string delChar( std::string &str, char del )
{
str.erase( std::remove( str.begin(), str.end(), del ), str.end() );
return str;
}
void printUsage()
{
2018-12-23 03:53:08 +01:00
Logger::info( " Usage: sapphire_dbm " );
Logger::info( "\t --mode" );
Logger::info( "\t\t initialize -> Creates DB if not present and inserts default tables/data" );
Logger::info( "\t\t check -> Checks if Sapphire DB-Version matches your DB-Version" );
Logger::info( "\t\t update -> Updates your DB-Version to Sapphire DB-Version" );
Logger::info( "\t\t clearchars -> Removes all character data from DB. Accounts will stay untouched" );
Logger::info( "\t\t liquidate -> Removes all tables and deletes the DB" );
Logger::info( "\t --user <mysqlUserName>" );
Logger::info( "\t --pass <mysqlPassword> ( default empty )" );
Logger::info( "\t --host <mysqlHost> ( default 127.0.0.1 )" );
Logger::info( "\t --port <mysqlPort> ( default 3306 )" );
Logger::info( "\t --database <mysqlDatabase>" );
Logger::info( "\t --sfile <path/to/schemafile> ( default sql/schema/schema.sql )" );
Logger::info( "\t --force ( skips user input / auto Yes )" );
2018-11-25 16:45:48 +01:00
}
int main( int32_t argc, char* argv[] )
{
std::string arg( "" );
std::string val( "" );
std::string mode;
std::string user;
std::string host;
std::string database;
2018-12-19 18:12:29 +09:00
std::string pass;
2018-12-23 03:53:08 +01:00
Logger::init( "log/SapphireDbm" );
2018-11-25 16:45:48 +01:00
2018-12-12 12:07:26 +01:00
std::string sFile;
std::string iFile;
2018-11-25 16:45:48 +01:00
2018-12-12 12:23:56 +01:00
bool force = false;
2018-11-25 16:45:48 +01:00
std::vector< std::string > args( argv + 1, argv + argc );
for( uint32_t i = 0; i + 1 < args.size(); i += 2 )
{
arg = std::string( args[ i ] );
val = std::string( args[ i + 1 ] );
// trim '-' from start of arg
arg = arg.erase( 0, arg.find_first_not_of( '-' ) );
if( arg == "mode" )
mode = val;
2018-12-12 12:07:26 +01:00
else if( arg == "initialize" || arg == "liquidate" )
mode = arg;
2018-11-25 16:45:48 +01:00
else if( arg == "user" )
user = val;
else if( arg == "host" )
host = val;
else if( arg == "database" )
database = val;
2018-12-19 18:12:29 +09:00
else if( arg == "pass" )
pass = val;
2018-12-12 12:07:26 +01:00
else if( arg == "sfile" )
sFile = val;
else if( arg == "ifile" )
iFile = val;
2018-12-12 12:23:56 +01:00
else if( arg == "force" )
2018-12-19 18:12:29 +09:00
force = true;
}
2018-11-25 16:45:48 +01:00
if( host.empty() )
host = "127.0.0.1";
if( mode.empty() || user.empty() || database.empty() )
{
printUsage();
return 0;
}
2018-12-19 18:12:29 +09:00
auto dbm = DbManager( host, database, user, pass, 3306 );
2018-12-12 12:07:26 +01:00
if( !sFile.empty() && !iFile.empty() )
{
dbm.setInsertFile( iFile );
dbm.setSchemaFile( sFile );
}
2018-12-19 18:12:29 +09:00
if( force )
2018-12-12 12:23:56 +01:00
dbm.setForceMode( true );
2018-11-25 16:45:48 +01:00
//initialize|check|update|clearchars|liquidate
if( mode.find( "initialize" ) != std::string::npos )
{
dbm.setMode( Mode::INIT );
}
else if( mode.find( "check" ) != std::string::npos )
{
dbm.setMode( Mode::CHECK );
}
else if( mode.find( "update" ) != std::string::npos )
{
dbm.setMode( Mode::UPDATE );
}
else if( mode.find( "clearchars" ) != std::string::npos )
{
dbm.setMode( Mode::CLEAN_CHARS );
}
else if( mode.find( "liquidate" ) != std::string::npos )
{
dbm.setMode( Mode::LIQUIDATE );
}
else
2018-12-19 18:12:29 +09:00
{
2018-12-23 03:53:08 +01:00
Logger::fatal( "Not a valid mode: " + mode + " !" );
2018-12-12 12:07:26 +01:00
return 1;
2018-12-19 18:12:29 +09:00
}
2018-12-23 03:53:08 +01:00
Logger::info( "Launching in " + mode + " mode..." );
2018-11-25 16:45:48 +01:00
if( !dbm.connect() )
{
2018-12-23 03:53:08 +01:00
Logger::fatal( "Could not connect to server!" );
Logger::fatal( dbm.getLastError() );
2018-12-12 12:07:26 +01:00
return 1;
2018-11-25 16:45:48 +01:00
}
if( !dbm.performAction() )
2018-11-25 16:45:48 +01:00
{
2018-12-23 03:53:08 +01:00
Logger::fatal( "Could not perform action!" );
Logger::fatal( dbm.getLastError() );
2018-12-12 12:07:26 +01:00
return 1;
2018-11-25 16:45:48 +01:00
}
return 0;
}