예제 #1
0
파일: xoreos.cpp 프로젝트: clone2727/xoreos
int main(int argc, char **argv) {
	initPlatform();
	initConfig();

	std::vector<Common::UString> args;
	Common::Platform::getParameters(argc, argv, args);

	Common::UString target;
	int code;
	if (!parseCommandline(args, target, code))
		return code;

	// Check the requested target
	if (target.empty() || !ConfigMan.hasGame(target)) {
		Common::UString path = ConfigMan.getString("path");
		if (path.empty()) {
			if (ConfigMan.getBool("listdebug", false)) {
				listDebug();
				return 0;
			}

			error("Neither an existing target, nor a path specified");
		}

		bool useExisting = false;
		if (target.empty()) {
			target = ConfigMan.findGame(path);
			if (!target.empty()) {
				warning("No target specified, but found a target with a matching path");
				useExisting = true;
			}
		}

		target = ConfigMan.createGame(path, target);
		if (target.empty())
			error("Failed creating a new config target for the game");

		if (!useExisting)
			warning("Creating a new target for this game");
	}

	if (!ConfigMan.setGame(target) || !ConfigMan.isInGame())
		error("No target \"%s\" in the config file", target.c_str());

	/* Open the log file.
	 *
	 * NOTE: A log is opened by default, unless the logfile config value
	 *       is set to an empty string or nologfile is set to true.
	 */
	Common::UString logFile = DebugMan.getDefaultLogFile();
	if (ConfigMan.hasKey("logfile"))
		logFile = ConfigMan.getString("logfile");
	if (ConfigMan.getBool("nologfile", false))
		logFile.clear();

	if (!logFile.empty())
		if (!DebugMan.openLogFile(logFile))
			warning("Failed to open log file \"%s\" for writing", logFile.c_str());

	DebugMan.logCommandLine(args);

	status("Target \"%s\"", target.c_str());

	Common::UString dirArg = ConfigMan.getString("path");
	if (dirArg.empty())
		error("Target \"%s\" is missing a path", target.c_str());

	Common::UString baseDir;
	try {
		baseDir = Common::FilePath::canonicalize(dirArg);
	} catch (...) {
		error("Invalid path \"%s\"", dirArg.c_str());
	}

	if (!Common::FilePath::isDirectory(baseDir) && !Common::FilePath::isRegularFile(baseDir))
		error("No such file or directory \"%s\"", baseDir.c_str());

	Engines::GameThread *gameThread = new Engines::GameThread;
	try {
		// Enable requested debug channels
		initDebug();

		// Initialize all necessary subsystems
		init();

		// Probe and create the game engine
		gameThread->init(baseDir);

		if (ConfigMan.getBool("listdebug", false)) {
			// List debug channels
			listDebug();
		} else {
			// Run the game
			gameThread->run();
			EventMan.runMainLoop();
		}

	} catch (...) {
		EventMan.raiseFatalError();

		Common::exceptionDispatcherError();
	}

	if (EventMan.fatalErrorRaised())
		std::exit(1);

	status("Shutting down");

	try {
		delete gameThread;
	} catch (...) {
	}

	try {
		// Configs changed, we should save them
		if (ConfigMan.changed()) {
			// But don't clobber a broken save
			if (!configFileIsBroken)
				ConfigMan.save();
		}
	} catch (...) {
		Common::exceptionDispatcherError();
	}

	deinit();
	return 0;
}
예제 #2
0
int main(int argc, char **argv) {
	atexit(deinit);

	initConfig();

	Common::UString target;
	int code;
	if (!parseCommandline(argc, argv, target, code))
		return code;

	// Open the requested log file
	Common::UString logFile = ConfigMan.getString("logfile");
	if (!logFile.empty())
		DebugMan.openLogFile(logFile);

	// Check the requested target
	if (target.empty() || !ConfigMan.hasGame(target)) {
		Common::UString path = ConfigMan.getString("path");
		if (path.empty()) {
			if (ConfigMan.getBool("listdebug", false)) {
				listDebug();
				return 0;
			}

			error("Neither an existing target, nor a path specified");
		}

		bool useExisting = false;
		if (target.empty()) {
			target = ConfigMan.findGame(path);
			if (!target.empty()) {
				warning("No target specified, but found a target with a matching path");
				useExisting = true;
			}
		}

		target = ConfigMan.createGame(path, target);
		if (target.empty())
			error("Failed creating a new config target for the game");

		if (!useExisting)
			warning("Creating a new target for this game");
	}

	status("Target \"%s\"", target.c_str());
	if (!ConfigMan.setGame(target) || !ConfigMan.isInGame())
		error("No target \"%s\" in the config file", target.c_str());

	Common::UString dirArg = ConfigMan.getString("path");
	if (dirArg.empty())
		error("Target \"%s\" is missing a path", target.c_str());

	Common::UString baseDir = Common::FilePath::makeAbsolute(dirArg);
	if (!Common::FilePath::isDirectory(baseDir) && !Common::FilePath::isRegularFile(baseDir))
		error("No such file or directory \"%s\"", baseDir.c_str());

	Engines::GameThread *gameThread = new Engines::GameThread;
	try {
		// Initialize all necessary subsystems
		init();

		// Probe and create the game engine
		gameThread->init(baseDir);

		// Enable requested debug channels
		initDebug();

		if (ConfigMan.getBool("listdebug", false)) {
			// List debug channels
			listDebug();
		} else {
			// Run the game
			gameThread->run();
			EventMan.runMainLoop();
		}

	} catch (Common::Exception &e) {
		Common::printException(e);
		std::exit(1);
	}

	status("Shutting down");

	delete gameThread;

	// Configs changed, we should save them
	if (ConfigMan.changed()) {
		// But don't clobber a broken save
		if (!configFileIsBroken)
			ConfigMan.save();
	}

	return 0;
}