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

Set gameDir to $XDG_DATA_HOME and copy assets on initialization #658

Merged
merged 2 commits into from
Jan 20, 2024
Merged
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
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