Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed gameDir to SDL_GetPrefPath #662

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Main/include/GameConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
DefineEnum(GameConfigKeys,
// Version of the config
ConfigVersion,

//Global Storage Configuration
DataFolder,

// Screen settings
ScreenWidth,
Expand Down Expand Up @@ -293,6 +296,7 @@ DefineEnum(QualityLevel,
class GameConfig : public Config<Enum_GameConfigKeys>
{
public:
String GetDefaultDataFolder();
GameConfig();
void SetKeyBinding(GameConfigKeys key, Key value);

Expand Down
68 changes: 22 additions & 46 deletions Main/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,9 @@ bool Application::ReloadConfig(const String& profile)

bool Application::m_LoadConfig(String profileName /* must be by value */)
{

bool successful = false;

String configPath = "Main.cfg";
String basePath = g_gameConfig.GetString(GameConfigKeys::DataFolder);
String configPath = basePath + std::string("Main.cfg");
File mainConfigFile;
if (mainConfigFile.OpenRead(Path::Absolute(configPath)))
{
Expand Down Expand Up @@ -576,9 +575,10 @@ void Application::m_SaveConfig()
{
if (!g_gameConfig.IsDirty())
return;

String basePath = g_gameConfig.GetString(GameConfigKeys::DataFolder);
String configPath = basePath + std::string("Main.cfg");

String profile = g_gameConfig.GetString(GameConfigKeys::CurrentProfileName);
String configPath = "Main.cfg";
if (profile == "Main")
{
//Save everything into main.cfg
Expand Down Expand Up @@ -975,7 +975,7 @@ bool Application::m_Init()
&custom_info);
#endif
#endif

g_gameConfig.Set(GameConfigKeys::DataFolder,g_gameConfig.GetDefaultDataFolder());
// Must have command line
assert(m_commandLine.size() >= 1);

Expand All @@ -991,42 +991,25 @@ bool Application::m_Init()
}
}
}

if (Path::gameDir.empty()) {
char* xdgDataDir = std::getenv("XDG_DATA_HOME");

if (xdgDataDir) {
String gameDir = Utility::Sprintf("%s%c%s", xdgDataDir, Path::sep, "unnamed-sdvx-clone");
auto executableDir = Path::RemoveLast(Path::GetExecutablePath());

Path::gameDir = gameDir;

if (!Path::IsDirectory(gameDir)) {
Logf("%s does not yet exist. Creating...", Logger::Severity::Info, *gameDir);

auto response = Path::CreateDir(gameDir);
if (response == 1) {
Logf("Created: %s", Logger::Severity::Info, *gameDir, response);
} else {
Logf("Failed creating directory %s. The game will probably crash soon.", Logger::Severity::Info, *gameDir, response);
}

std::list<String> requiredDirectories = { "skins", "fonts", "audio", "LightPlugins" };

for (String directory : requiredDirectories) {
auto sourceDir = Utility::Sprintf("%s%c%s", executableDir, Path::sep, directory);
auto destDir = Path::Absolute(directory);

response = Path::CopyDir(sourceDir, destDir);
if (response == 1) {
if(Path::gameDir.empty())
{
Path::gameDir = g_gameConfig.GetString(GameConfigKeys::DataFolder);
}
//Create the data folders if they doesn't exist
std::list<String> requiredDirectories = {"crash_dumps","replays","screenshots", "skins", "fonts", "audio", "LightPlugins" };
String executableDir = Path::RemoveLast(Path::GetExecutablePath());
for(auto directory : requiredDirectories)
{
auto sourceDir = Utility::Sprintf("%s%c%s", executableDir, Path::sep, directory);
String destDir = Path::Absolute(directory);
if (!Path::IsDirectory(destDir))
{
Logf("%s does not yet exist. Creating...", Logger::Severity::Info, *destDir);
auto response = Path::CopyDirUnsafe(sourceDir, destDir);
if (response == 1) {
Logf("Copied: %s to %s", Logger::Severity::Info, *sourceDir, *destDir, response);
} else {
Logf("Failed copying %s to %s. The game will probably crash soon.", Logger::Severity::Error, *sourceDir, *destDir, response);
}
}

} else {
Logf("Setting gamedir to $XDG_DATA_HOME (%s). If data is missing, you can either copy the game data in %s to that directory, unset the $XDG_DATA_HOME variable or run the game with -gamedir=%s", Logger::Severity::Warning, *gameDir, *executableDir, *executableDir);
}
}
}
Expand Down Expand Up @@ -1242,13 +1225,6 @@ bool Application::m_Init()
renderSema = SDL_CreateSemaphore(0);
m_renderThread = Thread(threadedRenderer);



///TODO: check if directory exists already?
Path::CreateDir(Path::Absolute("screenshots"));
Path::CreateDir(Path::Absolute("songs"));
Path::CreateDir(Path::Absolute("replays"));
Path::CreateDir(Path::Absolute("crash_dumps"));
Logger::Get().SetLogLevel(g_gameConfig.GetEnum<Logger::Enum_Severity>(GameConfigKeys::LogLevel));
return true;
}
Expand Down
17 changes: 15 additions & 2 deletions Main/src/GameConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "stdafx.h"
#include "GameConfig.hpp"

#include "Shared/Path.hpp"
#include "Shared/Log.hpp"
#include "HitStat.hpp"

Expand Down Expand Up @@ -48,9 +48,22 @@ void GameConfig::SetKeyBinding(GameConfigKeys key, Graphics::Key value)
{
SetEnum<Enum_Key>(key, value);
}

String GameConfig::GetDefaultDataFolder(){
auto basePath = SDL_GetPrefPath("USC-Game","Data");
if(!Path::IsDirectory(basePath)){
Path::CreateDirRecursive(basePath);
}
return basePath;
}
void GameConfig::InitDefaults()
{
String basePath = GetDefaultDataFolder();
Set(GameConfigKeys::DataFolder,basePath);
std::string songsPath = basePath + std::string("Songs");
if(!Path::IsDirectory(songsPath)){
Path::CreateDirRecursive(songsPath);
}

// Do not set ConfigVersion to GameConfig::VERSION here. It will cause problems for config files with no ConfigVersion field.
// For a new config, ConfigVersion will be set to the appropriate value in Application::m_LoadConfig.
Set(GameConfigKeys::ConfigVersion, 0);
Expand Down
1 change: 1 addition & 0 deletions Shared/include/Shared/Path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Path
// Copies a folder and all files in it
// the target directory must not exist
static bool CopyDir(String srcFolder, String dstFolder);
static bool CopyDirUnsafe(String srcFolder, String dstFolder);
// Go to specified path using the system default file browser
static bool ShowInFileBrowser(const String& path);
// Open external program with specified parameters (used to open charts in editor)
Expand Down
11 changes: 7 additions & 4 deletions Shared/src/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ String Path::Absolute(const String& path)
return path;

String baseDir = !gameDir.empty() ? gameDir : RemoveLast(GetExecutablePath());
return baseDir + sep + path;
if(baseDir[baseDir.length()-1] != sep)
baseDir.push_back(sep);
return baseDir + path;
}
String Path::RemoveLast(const String& path, String* lastOut /*= nullptr*/)
{
Expand Down Expand Up @@ -182,12 +184,13 @@ bool Path::ClearDir(const String& path)
}
return true;
}
bool Path::CopyDir(String srcFolder, String dstFolder)
bool Path::CopyDir(String srcFolder, String dstFolder){
CopyDirUnsafe(Absolute(srcFolder),Absolute(dstFolder));
}
bool Path::CopyDirUnsafe(String srcFolder, String dstFolder)
{
srcFolder = Absolute(srcFolder);

// Remove trailing seperators
dstFolder = Absolute(dstFolder);
dstFolder.TrimBack(Path::sep);

if(!CreateDir(dstFolder))
Expand Down