diff --git a/source/client/client.cpp b/source/client/client.cpp index a7f5447..8a59232 100644 --- a/source/client/client.cpp +++ b/source/client/client.cpp @@ -1 +1,7 @@ -#include "client.h" \ No newline at end of file +#include "client.h" + + +int runClient( const std::string& host_address ) +{ + return 0; +} \ No newline at end of file diff --git a/source/client/client.h b/source/client/client.h index cb8909f..bd30a06 100644 --- a/source/client/client.h +++ b/source/client/client.h @@ -1,4 +1,8 @@ #ifndef _TTRTS_CLIENT_H_ #define _TTRTS_CLIENT_H_ +#include + +int runClient( const std::string& host_address ); + #endif \ No newline at end of file diff --git a/source/client/filesystem.cpp b/source/client/filesystem.cpp index 59ac6b8..a2abd1f 100644 --- a/source/client/filesystem.cpp +++ b/source/client/filesystem.cpp @@ -13,18 +13,19 @@ #include #include +// ===================================================================================================================== // time for waiting between file stats static const std::chrono::milliseconds sk_waitTime = std::chrono::milliseconds(100); // Check if a file exists -inline bool FileExists( const std::string& name ) +bool FileExists( const std::string& name ) { struct stat buffer; return (stat (name.c_str(), &buffer) == 0); } // Wait for a file to exist -inline void WaitForFile( const std::string& name, const std::chrono::milliseconds& time ) +void WaitForFile( const std::string& name, const std::chrono::milliseconds& time ) { while( !FileExists(name) ) std::this_thread::sleep_for(time); } @@ -49,8 +50,59 @@ bool OutputGameStateFile(CTTRTSGame &game, const std::string &gameDir) return true; } -int runFromFilesystem( const std::string& directory, const std::string gamefile ) +std::string getMapsDir() { + std::string maps = STRINGIFY(TTRTS_MAPS); + if( getenv("TTRTS_MAPS") ) + { + maps = getenv("TTRTS_MAPS"); + + // Additional trailing slash + if( maps.back() != '/' ) + maps += "/"; + } + + return maps; +} + +std::string getGamesDir() +{ + std::string dir = STRINGIFY(TTRTS_GAMES); + if( getenv("TTRTS_GAMES") ) + { + dir = getenv("TTRTS_GAMES"); + + // Additional trailing slash + if( dir.back() != '/' ) + dir += "/"; + } + return dir; +} + +// ===================================================================================================================== +int runFromFilesystem( const std::string& gamestring ) +{ + std::string gamefile = gamestring; + + // Default for maps + std::string ttrts_maps_dir = getMapsDir(); + + // Default for games + std::string ttrts_games_dir = getGamesDir(); + + // If file path is not local path and file doesn't exist + if( gamefile.find("/") == std::string::npos + && access( gamefile.c_str(), F_OK ) == -1 ) + { + gamefile = ttrts_maps_dir + gamefile; + } + + // If still not good + if( access( gamefile.c_str(), F_OK ) == -1 ) + { + std::cerr<<"Error: "<< gamefile <<" file not found"< +#include -int runFromFilesystem( const std::string& directory, const std::string gamefile ); +#include "game.h" + +#define STRINGIFY(x) _STRINGIFY(x) +#define _STRINGIFY(x) #x + +bool FileExists( const std::string& name ); + +void WaitForFile( const std::string& name, const std::chrono::milliseconds& time ); + +int runFromFilesystem( const std::string& gamefile ); + +bool OutputGameStateFile(CTTRTSGame &game, const std::string &gameDir); + +std::string getMapsDir(); +std::string getGamesDir(); #endif \ No newline at end of file diff --git a/source/client/main.cpp b/source/client/main.cpp index 8c31966..0d78823 100644 --- a/source/client/main.cpp +++ b/source/client/main.cpp @@ -1,11 +1,9 @@ #include "game.h" #include "filesystem.h" +#include "server.h" +#include "client.h" #include -#include - -#define STRINGIFY(x) _STRINGIFY(x) -#define _STRINGIFY(x) #x static const char* sk_usage = #include "usage.h" @@ -22,44 +20,18 @@ int main(int argc, char* argv[]) } // Attempt to open the game file - std::string gameFile = argv[1]; + std::string arg1 = argv[1]; - // Default for maps - std::string ttrts_maps_dir = STRINGIFY(TTRTS_MAPS); - if( getenv("TTRTS_MAPS") ) + if( arg1 == "client" ) { - ttrts_maps_dir = getenv("TTRTS_MAPS"); - - // Additional trailing slash - if( ttrts_maps_dir.back() != '/' ) - ttrts_maps_dir += "/"; + if( argc == 3 ) + return runClient(argv[2]); } - - // Default for games - std::string ttrts_games_dir = STRINGIFY(TTRTS_GAMES); - if( getenv("TTRTS_GAMES") ) + else if ( arg1 == "server" ) { - ttrts_games_dir = getenv("TTRTS_GAMES"); - - // Additional trailing slash - if( ttrts_games_dir.back() != '/' ) - ttrts_games_dir += "/"; + return runServer(); } - - // If file path is not local path and file doesn't exist - if( gameFile.find("/") == std::string::npos - && access( gameFile.c_str(), F_OK ) == -1 ) - { - gameFile = ttrts_maps_dir + gameFile; - } - - // If still not good - if( access( gameFile.c_str(), F_OK ) == -1 ) - { - std::cerr<<"Error: "<