Skip to content

Commit

Permalink
Added code to guess game directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Marukyu committed May 6, 2016
1 parent 9e604c0 commit 8e4dade
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
61 changes: 61 additions & 0 deletions src/Client/System/GameDirectoryGuesser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <Client/System/GameDirectoryGuesser.hpp>
#include <Shared/Utils/OSDetect.hpp>

#ifdef WOS_WINDOWS
# include <windows.h>
# include <shlobj.h>
#elif defined WOS_LINUX
# include <pwd.h>
# include <unistd.h>
# include <cstdlib>
#endif

std::vector<std::string> GameDirectoryGuesser::getDirectoryList()
{
std::vector<std::string> directories;

#ifdef WOS_WINDOWS

// TODO: Handle non-ASCII paths better.
TCHAR programFilesDirectory[MAX_PATH];
TCHAR programFilesX86Directory[MAX_PATH];
SHGetFolderPath(nullptr, CSIDL_PROGRAM_FILES, nullptr, 0, programFilesDirectory);
SHGetFolderPath(nullptr, CSIDL_PROGRAM_FILESX86, nullptr, 0, programFilesX86Directory);

std::wstring programFilesW(programFilesDirectory);
std::wstring programFilesX86W(programFilesX86Directory);

std::string programFilesA(programFilesW.begin(), programFilesW.end());
std::string programFilesX86A(programFilesX86W.begin(), programFilesX86W.end());

directories.push_back(programFilesA + "\\Steam\\SteamApps\\common\\Crypt of the NecroDancer");
directories.push_back(programFilesX86A + "\\Steam\\SteamApps\\common\\Crypt of the NecroDancer");

#elif defined WOS_LINUX

// Get user's home directory, if set through $HOME.
const char * homeDirectoryPtr = std::getenv("HOME");

if (homeDirectoryPtr == nullptr)
{
// Get user's home directory, if not set through $HOME.
homeDirectoryPtr = getpwuid(getuid())->pw_dir;
}

if (homeDirectoryPtr != nullptr)
{
std::string homeDirectory(homeDirectoryPtr);

// Try all capitalization variants. The exact capitalization depends on when Steam was installed/cleaned.
directories.push_back(homeDirectory + "/.local/share/steam/steamapps/common/Crypt of the NecroDancer");
directories.push_back(homeDirectory + "/.local/share/Steam/steamapps/common/Crypt of the NecroDancer");
directories.push_back(homeDirectory + "/.local/share/steam/SteamApps/common/Crypt of the NecroDancer");
directories.push_back(homeDirectory + "/.local/share/Steam/SteamApps/common/Crypt of the NecroDancer");
}

#endif

// TODO: Detect game path for Mac OS X.

return directories;
}
14 changes: 14 additions & 0 deletions src/Client/System/GameDirectoryGuesser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef SRC_CLIENT_SYSTEM_GAMEDIRECTORYGUESSER_HPP_
#define SRC_CLIENT_SYSTEM_GAMEDIRECTORYGUESSER_HPP_

#include <string>
#include <vector>

class GameDirectoryGuesser
{
public:

static std::vector<std::string> getDirectoryList();
};

#endif
24 changes: 18 additions & 6 deletions src/Client/System/NEApplication.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Client/Graphics/BitmapText.hpp>
#include <Client/System/GameDirectoryGuesser.hpp>
#include <Client/System/NEApplication.hpp>
#include <Client/System/NEInterface.hpp>
#include <SFML/Graphics/Image.hpp>
Expand Down Expand Up @@ -32,15 +33,25 @@ bool NEApplication::chooseGameDirectory()
return true;
}

// Check common game directories.
for (auto dir : GameDirectoryGuesser::getDirectoryList())
{
gameDirectory = dir;

if (checkGameDirectory())
{
return true;
}
}

bool showMessage = false;

FileChooser fc;
fc.setTitle("Select NecroDancer directory");

MessageBox errorMessage("This is not a valid NecroDancer directory.\n\n"
"Please select your NecroDancer installation directory (typically found in "
"your SteamApps folder)\nor click \"Cancel\" to exit.", "NecroEdit", MessageBox::Error,
MessageBox::OkCancel);
"your SteamApps folder)\nor click \"Cancel\" to exit.", "NecroEdit", MessageBox::Error, MessageBox::OkCancel);

do
{
Expand All @@ -66,7 +77,8 @@ bool NEApplication::chooseGameDirectory()

showMessage = true;

} while (!checkGameDirectory());
}
while (!checkGameDirectory());

return true;
}
Expand Down Expand Up @@ -174,7 +186,7 @@ int NEApplication::init(const std::vector<std::string>& args)
fontError.show();
return LoadErrorFont;
}

setTextureSmoothingEnabled(false);

open();
Expand All @@ -187,9 +199,9 @@ void NEApplication::printUsage(const std::string & exeName)
std::cout << "Usage: " << exeName << " [Options] [Dungeon File]" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "-g / --game-directory [dir]: Specify [dir] as containing the game's executable and resource files"
<< std::endl;
<< std::endl;
std::cout << "-r / --resource-directory [dir]: Specify [dir] as containing the editor's resource files"
<< std::endl;
<< std::endl;
std::cout << "-h / --help: Show this help" << std::endl;
std::cout << "--: Treat following arguments as dungeon file to load, rather than as options" << std::endl;
}
Expand Down

0 comments on commit 8e4dade

Please sign in to comment.