int main(int argc, char *argv[]) { #ifdef _WIN32 try { #endif std::cout << "If you find any errors, report them to mantis or the forums." << std::endl << std::endl; ConfigHandler::Instantiate(""); FileSystemHandler::Cleanup(); FileSystemHandler::Initialize(false); CGameServer* server = 0; CGameSetup* gameSetup = 0; if (argc > 1) { const std::string script(argv[1]); std::cout << "Loading script from file: " << script << std::endl; ClientSetup settings; CFileHandler fh(argv[1]); if (!fh.FileExists()) throw content_error("Setupscript doesn't exists in given location: "+script); std::string buf; if (!fh.LoadStringData(buf)) throw content_error("Setupscript cannot be read: "+script); settings.Init(buf); gameSetup = new CGameSetup(); // to store the gamedata inside if (!gameSetup->Init(buf)) // read the script provided by cmdline { std::cout << "Failed to load script" << std::endl; return 1; } std::cout << "Starting server..." << std::endl; // Create the server, it will run in a separate thread GameData* data = new GameData(); UnsyncedRNG rng; rng.Seed(gameSetup->gameSetupText.length()); rng.Seed(script.length()); data->SetRandomSeed(rng.RandInt()); // Use script provided hashes if they exist if (gameSetup->mapHash != 0) { data->SetMapChecksum(gameSetup->mapHash); gameSetup->LoadStartPositions(false); // reduced mode } else { data->SetMapChecksum(archiveScanner->GetMapChecksum(gameSetup->mapName)); CFileHandler* f = new CFileHandler("maps/" + gameSetup->mapName); if (!f->FileExists()) { std::vector<std::string> ars = archiveScanner->GetArchivesForMap(gameSetup->mapName); if (ars.empty()) { throw content_error("Couldn't find any archives for map '" + gameSetup->mapName + "'."); } for (std::vector<std::string>::iterator i = ars.begin(); i != ars.end(); ++i) { if (!vfsHandler->AddArchive(*i, false)) { throw content_error("Couldn't load archive '" + *i + "' for map '" + gameSetup->mapName + "'."); } } } delete f; gameSetup->LoadStartPositions(); // full mode } if (gameSetup->modHash != 0) { data->SetModChecksum(gameSetup->modHash); } else { const std::string modArchive = archiveScanner->ModNameToModArchive(gameSetup->modName); data->SetModChecksum(archiveScanner->GetModChecksum(modArchive)); } data->SetSetup(gameSetup->gameSetupText); server = new CGameServer(&settings, false, data, gameSetup); while (!server->HasFinished()) // check if still running #ifdef _WIN32 Sleep(1000); #else sleep(1); // if so, wait 1 second #endif delete server; // delete the server after usage } else { std::cout << "usage: spring-dedicated <full_path_to_script>" << std::endl; } FileSystemHandler::Cleanup(); #ifdef _WIN32 } catch (const std::exception& err) { std::cout << "Exception raised: " << err.what() << std::endl; return 1; } #endif return 0; }
int main(int argc, char *argv[]) { #ifdef _WIN32 try { #endif SDL_Init(SDL_INIT_TIMER); std::cout << "If you find any errors, report them to mantis or the forums." << std::endl << std::endl; ConfigHandler::Instantiate(); // use the default config file FileSystemHandler::Initialize(false); CGameServer* server = 0; CGameSetup* gameSetup = 0; if (argc == 2) { const std::string script(argv[1]); std::string buf; std::cout << "Loading script from file: " << script << std::endl; ClientSetup settings; CFileHandler fh(argv[1]); if (!fh.FileExists()) throw content_error("script does not exist in given location: " + script); if (!fh.LoadStringData(buf)) throw content_error("script cannot be read: " + script); settings.Init(buf); gameSetup = new CGameSetup(); // to store the gamedata inside if (!gameSetup->Init(buf)) { // read the script provided by cmdline std::cout << "Failed to load script" << std::endl; return 1; } std::cout << "Starting server..." << std::endl; // Create the server, it will run in a separate thread GameData data; UnsyncedRNG rng; rng.Seed(gameSetup->gameSetupText.length()); rng.Seed(script.length()); data.SetRandomSeed(rng.RandInt()); // Use script provided hashes if they exist if (gameSetup->mapHash != 0) { data.SetMapChecksum(gameSetup->mapHash); gameSetup->LoadStartPositions(false); // reduced mode } else { data.SetMapChecksum(archiveScanner->GetArchiveCompleteChecksum(gameSetup->mapName)); CFileHandler f("maps/" + gameSetup->mapName); if (!f.FileExists()) { vfsHandler->AddArchiveWithDeps(gameSetup->mapName, false); } gameSetup->LoadStartPositions(); // full mode } if (gameSetup->modHash != 0) { data.SetModChecksum(gameSetup->modHash); } else { const std::string& modArchive = archiveScanner->ArchiveFromName(gameSetup->modName); const unsigned int modCheckSum = archiveScanner->GetArchiveCompleteChecksum(modArchive); data.SetModChecksum(modCheckSum); } data.SetSetup(gameSetup->gameSetupText); server = new CGameServer(settings.hostIP, settings.hostPort, &data, gameSetup); while (!server->HasFinished()) // check if still running #ifdef _WIN32 Sleep(1000); #else sleep(1); // if so, wait 1 second #endif delete server; // delete the server after usage } else { std::cout << "usage: " << argv[0] << " <full_path_to_script>" << std::endl; } FileSystemHandler::Cleanup(); ConfigHandler::Deallocate(); #ifdef _WIN32 } catch (const std::exception& err) { std::cout << "Exception raised: " << err.what() << std::endl; return 1; } #endif return 0; }
/** * Initializes instance of GameSetup */ void SpringApp::Startup() { std::string inputFile = cmdline->GetInputFile(); if (inputFile.empty()) { bool server = !cmdline->result("client") || cmdline->result("server"); #ifdef SYNCDEBUG CSyncDebugger::GetInstance()->Initialize(server, 64); #endif activeController = new SelectMenu(server); } else if (inputFile.rfind("sdf") == inputFile.size() - 3) { std::string demofile = inputFile; ClientSetup* startsetup = new ClientSetup(); startsetup->isHost = true; // local demo play startsetup->myPlayerName = configHandler->GetString("name", "unnamed")+ " (spec)"; #ifdef SYNCDEBUG CSyncDebugger::GetInstance()->Initialize(true, 64); //FIXME: add actual number of player #endif pregame = new CPreGame(startsetup); pregame->LoadDemo(demofile); } else if (inputFile.rfind("ssf") == inputFile.size() - 3) { std::string savefile = inputFile; ClientSetup* startsetup = new ClientSetup(); startsetup->isHost = true; startsetup->myPlayerName = configHandler->GetString("name", "unnamed"); #ifdef SYNCDEBUG CSyncDebugger::GetInstance()->Initialize(true, 64); //FIXME: add actual number of player #endif pregame = new CPreGame(startsetup); pregame->LoadSavefile(savefile); } else { std::string startscript = inputFile; CFileHandler fh(startscript); if (!fh.FileExists()) throw content_error("Setupscript doesn't exists in given location: "+startscript); std::string buf; if (!fh.LoadStringData(buf)) throw content_error("Setupscript cannot be read: "+startscript); ClientSetup* startsetup = new ClientSetup(); startsetup->Init(buf); // commandline parameters overwrite setup if (cmdline->result("client")) startsetup->isHost = false; else if (cmdline->result("server")) startsetup->isHost = true; #ifdef SYNCDEBUG CSyncDebugger::GetInstance()->Initialize(startsetup->isHost, 64); //FIXME: add actual number of player #endif pregame = new CPreGame(startsetup); if (startsetup->isHost) pregame->LoadSetupscript(buf); } }