Skip to content

Commit

Permalink
Added check for invalid flags being passed.
Browse files Browse the repository at this point in the history
Added resource path for OSX Application bundles.
  • Loading branch information
ko2fan committed May 12, 2015
1 parent 6d2911c commit dba4ddd
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 30 deletions.
94 changes: 64 additions & 30 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
#define USER_DIR "."
#endif

#ifdef OSX_APP
#include "ResourcePath.hpp"
#endif

using namespace boost::iostreams;
using namespace boost::program_options;
using namespace boost::archive;
Expand Down Expand Up @@ -127,15 +131,64 @@ int main(int argc, char* argv[]) {
("seed", value<int>(), "Use given seed")
("replay", value<string>(), "Replay game from file");
variables_map vars;
store(parse_command_line(argc, argv, flags), vars);
if (vars.count("help")) {
std::cout << flags << endl;
return 0;
string dataPath;
string userPath;
string uploadUrl;
string overrideSettings;
int seed;
bool genExit;
bool replay;
try
{
store(parse_command_line(argc, argv, flags), vars);

if (vars.count("help")) {
std::cout << flags << endl;
return 0;
}
if (vars.count("run_tests")) {
testAll();
return 0;
}

if (vars.count("data_dir"))
dataPath = vars["data_dir"].as<string>();
else
#ifdef OSX_APP
dataPath = resourcePath();
#else
dataPath = DATA_DIR;
#endif

if (vars.count("user_dir"))
userPath = vars["user_dir"].as<string>();
else
if (const char* localPath = std::getenv("XDG_DATA_HOME"))
userPath = localPath + string("/KeeperRL");
else
userPath = USER_DIR;

if (vars.count("upload_url"))
uploadUrl = vars["upload_url"].as<string>();
else
uploadUrl = "http://keeperrl.com/retired";

if (vars.count("override_settings"))
overrideSettings = vars["override_settings"].as<string>();

seed = vars.count("seed") ? vars["seed"].as<int>() : int(time(0));

genExit = vars.count("gen_world_exit");
replay = vars.count("replay");
}
if (vars.count("run_tests")) {
testAll();
catch(error& e)
{
std::cout << "Error parsing flags: ";
for (int count = 1; count < argc; ++count)
std::cout << argv[count] << endl;
return 0;
}

unique_ptr<View> view;
unique_ptr<CompressedInput> input;
unique_ptr<CompressedOutput> output;
Expand All @@ -146,34 +199,16 @@ int main(int argc, char* argv[]) {
Spell::init();
Epithet::init();
Vision::init();
string dataPath;
if (vars.count("data_dir"))
dataPath = vars["data_dir"].as<string>();
else
dataPath = DATA_DIR;

string freeDataPath = dataPath + "/data_free";
string paidDataPath = dataPath + "/data";
string contribDataPath = dataPath + "/data_contrib";
tilesPresent = !!opendir(paidDataPath.c_str());
string userPath;
if (vars.count("user_dir"))
userPath = vars["user_dir"].as<string>();
else
if (const char* localPath = std::getenv("XDG_DATA_HOME"))
userPath = localPath + string("/KeeperRL");
else
userPath = USER_DIR;

Debug() << "Data path: " << dataPath;
Debug() << "User path: " << userPath;
string uploadUrl;
if (vars.count("upload_url"))
uploadUrl = vars["upload_url"].as<string>();
else
uploadUrl = "http://keeperrl.com/retired";

makeDir(userPath);
string overrideSettings;
if (vars.count("override_settings"))
overrideSettings = vars["override_settings"].as<string>();
Options options(userPath + "/options.txt", overrideSettings);
Renderer renderer("KeeperRL", Vec2(36, 36), contribDataPath);
Clock clock;
Expand All @@ -183,10 +218,9 @@ int main(int argc, char* argv[]) {
guiFactory.loadNonFreeImages(paidDataPath + "/images");
if (tilesPresent)
initializeRendererTiles(renderer, paidDataPath + "/images");
int seed = vars.count("seed") ? vars["seed"].as<int>() : int(time(0));

// int forceMode = vars.count("force_keeper") ? 0 : -1;
bool genExit = vars.count("gen_world_exit");
if (vars.count("replay")) {
if (replay) {
string fname = vars["replay"].as<string>();
Debug() << "Reading from " << fname;
seed = fromString<int>(fname.substr(lognamePref.size()));
Expand Down
43 changes: 43 additions & 0 deletions resource_path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent.gom@gmail.com),
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

#ifndef RESOURCE_PATH_HPP
#define RESOURCE_PATH_HPP

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <string>

////////////////////////////////////////////////////////////
/// \brief Return the path to the resource folder.
///
/// \return The path to the resource folder associate
/// with the main bundle or an empty string is there is no bundle.
///
////////////////////////////////////////////////////////////
std::string resourcePath(void);

#endif
52 changes: 52 additions & 0 deletions resource_path.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Marco Antognini (antognini.marco@gmail.com),
// Laurent Gomila (laurent.gom@gmail.com),
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "ResourcePath.hpp"
#import <Foundation/Foundation.h>

////////////////////////////////////////////////////////////
std::string resourcePath(void)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

std::string rpath;
NSBundle* bundle = [NSBundle mainBundle];

if (bundle == nil) {
#ifdef DEBUG
NSLog(@"bundle is nil... thus no resources path can be found.");
#endif
} else {
NSString* path = [bundle resourcePath];
rpath = [path UTF8String] + std::string("/");
}

[pool drain];

return rpath;
}

0 comments on commit dba4ddd

Please sign in to comment.