Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
    try {
        std::cout << "BIG FAT WARNING: this server is currently under development. If you find any errors (you most likely will)";
        std::cout << " report them to mantis or the forums." << std::endl << std::endl;
        FileSystemHandler::Cleanup();
        FileSystemHandler::Initialize(false);
        CGameServer* server = 0;
        CGameSetup* gameSetup = 0;

        if (argc > 1)
        {
            std::string script = argv[1];
            std::cout << "Loading script: " << script << std::endl;
            gameSetup = new CGameSetup();	// to store the gamedata inside
            if (!gameSetup->Init(script))	// 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
            const std::string modArchive = archiveScanner->ModNameToModArchive(gameSetup->baseMod);
            GameData* data = new GameData();
            data->SetMap(gameSetup->mapName, archiveScanner->GetMapChecksum(gameSetup->mapName));
            data->SetMod(gameSetup->baseMod, archiveScanner->GetModChecksum(modArchive));
            data->SetScript(gameSetup->scriptName);

            server = new CGameServer(gameSetup->hostport, false, data, gameSetup);

            if (gameSetup->autohostport > 0)
                server->AddAutohostInterface(gameSetup->autohostport);
            else
            {
                std::cout << "You should specify an AutohostPort in the script" << std::endl;
            }

            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
            delete gameSetup;
        }
        else
        {
            std::cout << "usage: dedicated <full_path_to_script>" << std::endl;
        }

        FileSystemHandler::Cleanup();
    }
    catch (const std::exception& err)
    {
        std::cout << "Exception raised: " << err.what() << std::endl;
    }
    return 0;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
	try {
	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[0]);
		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->SetMap(gameSetup->mapName, gameSetup->mapHash);
			gameSetup->LoadStartPositions(false); // reduced mode
		}
		else
		{
			data->SetMap(gameSetup->mapName, 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->SetMod(gameSetup->baseMod, gameSetup->modHash);
		} else {
			const std::string modArchive = archiveScanner->ModNameToModArchive(gameSetup->baseMod);
			data->SetMod(gameSetup->baseMod, archiveScanner->GetModChecksum(modArchive));
		}

		data->SetScript(gameSetup->scriptName);
		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();
	}
	catch (const std::exception& err)
	{
		std::cout << "Exception raised: " << err.what() << std::endl;
	}
	return 0;
}
Ejemplo n.º 3
0
void CPreGame::StartServer(const std::string& setupscript)
{
	assert(!gameServer);
	GameData* startupData = new GameData();
	CGameSetup* setup = new CGameSetup();
	setup->Init(setupscript);
	std::string map = setup->mapName;
	std::string mod = setup->baseMod;
	std::string script = setup->scriptName;

	startupData->SetRandomSeed(static_cast<unsigned>(gu->usRandInt()));
	if (!map.empty())
	{
		// would be better to use MapInfo here, but this doesn't work
		LoadMap(map); // map into VFS
		std::string mapDefFile;
		const std::string extension = map.substr(map.length()-3);
		if (extension == "smf")
			mapDefFile = std::string("maps/")+map.substr(0,map.find_last_of('.'))+".smd";
		else if(extension == "sm3")
			mapDefFile = string("maps/")+map;
		else
			throw std::runtime_error("CPreGame::StartServer(): Unknown extension: " + extension);

		MapParser mp(map);
		LuaTable mapRoot = mp.GetRoot();
		const std::string mapWantedScript = mapRoot.GetString("script",     "");
		const std::string scriptFile      = mapRoot.GetString("scriptFile", "");

		if (!mapWantedScript.empty()) {
			script = mapWantedScript;
		}
	}
	startupData->SetScript(script);
	// here we now the name of the script to use

	CScriptHandler::SelectScript(script);
	std::string scriptWantedMod;
	scriptWantedMod = CScriptHandler::Instance().chosenScript->GetModName();
	if (!scriptWantedMod.empty()) {
		mod = scriptWantedMod;
	}
	LoadMod(mod);

	// make sure s is a modname (because the same mod can be in different archives on different computers)
	mod = archiveScanner->ModArchiveToModName(mod);
	setup->baseMod = mod;
	std::string modArchive = archiveScanner->ModNameToModArchive(mod);
	startupData->SetMod(mod, archiveScanner->GetModChecksum(modArchive));

	std::string mapFromScript = CScriptHandler::Instance().chosenScript->GetMapName();
	if (!mapFromScript.empty() && map != mapFromScript) {
		//TODO unload old map
		LoadMap(mapFromScript, true);
	}

	startupData->SetMap(map, archiveScanner->GetMapChecksum(map));
	setup->LoadStartPositions();

	good_fpu_control_registers("before CGameServer creation");
	startupData->SetSetup(setup->gameSetupText);
	gameServer = new CGameServer(settings.get(), false, startupData, setup);
	gameServer->AddLocalClient(settings->myPlayerName, SpringVersion::GetFull());
	good_fpu_control_registers("after CGameServer creation");
}