Skip to content

Commit

Permalink
Set gameDir to $XDG_DATA_HOME and copy assets on initialization #658
Browse files Browse the repository at this point in the history
from SkyLeite/develop
  • Loading branch information
Drewol committed Jan 20, 2024
2 parents 76c359f + 861fc14 commit cc7402a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Main/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,45 @@ 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) {
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);
}
}
}

// Set the locale so that functions such as `fopen` use UTF-8.
{
String prevLocale = setlocale(LC_CTYPE, nullptr);
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Just run 'usc-game' or 'usc-game_Debug' from within the 'bin' folder.
- `-autoskip` - Skips beginning of song to the first chart note
- `-debug` - Used to show relevant debug info in game such as hit timings, and scoring debug info
- `-test` - Runs test scene, for development purposes only
- `-gamedir` - Sets the directory the game loads assets from. If unset, attempts reading from `$XDG_DATA_HOME/unnamed-sdvx-clone`. Finally, uses the executable directory if all else fails.

## How to build:

Expand Down

0 comments on commit cc7402a

Please sign in to comment.