Skip to content

Commit

Permalink
create function that returns game data folder path and use it everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmMoltony committed Dec 31, 2024
1 parent bb22099 commit 13042d6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
9 changes: 3 additions & 6 deletions src/BoardSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ public static class BoardSaver
{
public static string GetLevelSavePath()
{
// TODO add a function to get %localappdata%/nonoSharp
// this is because right now in auto save we save it in <level save path>/..
// which assumes that the data folder is one directory up the custom levels folder
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "nonoSharp", "CustomLevels");
return Path.Combine(Settings.GetDataFolderPath(), "CustomLevels");
}

public static void SaveBoard(Board board, string fileName, int maxHints)
public static void SaveBoard(Board board, string fileName, int maxHints, bool absolutePath = false)
{
string? saveData = board.Serialize();
if (saveData == null)
throw new InvalidOperationException("Board save data is null");

string filePath = Path.Combine(GetLevelSavePath(), $"{fileName}.nono");
string filePath = absolutePath ? $"{fileName}.nono" : Path.Combine(GetLevelSavePath(), $"{fileName}.nono");
string? levelDirPath = Path.GetDirectoryName(filePath);
if (levelDirPath == null)
throw new InvalidOperationException("Cannot determine level directory path");
Expand Down
2 changes: 1 addition & 1 deletion src/Editor/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void AutoSave()

private void checkAutoSave()
{
if (File.Exists(Path.Combine(BoardSaver.GetLevelSavePath(), "..", "EditorAutosave.nono")))
if (File.Exists(Path.Combine(Settings.GetDataFolderPath(), "EditorAutosave.nono")))
{
_state = EditorState.AutoSaveNotice;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Editor/EditorMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ public void EnableAutoSaveTimer(bool enabled)
public void AutoSave()
{
Log.Logger.Information("Auto saving level");
BoardSaver.SaveBoard(Board, Path.Combine("..", "EditorAutosave"), Board.maxHints);
BoardSaver.SaveBoard(Board, Path.Combine(Settings.GetDataFolderPath(), "EditorAutosave"), Board.maxHints, true);
}

public void LoadAutoSave()
{
Log.Logger.Information("Loading auto save");
Board = new();
Board.Make(Path.Combine(BoardSaver.GetLevelSavePath(), "..", "EditorAutosave"));
Board.Make(Path.Combine(Settings.GetDataFolderPath(), "EditorAutosave"));
}

private void makeButtons()
Expand Down
2 changes: 1 addition & 1 deletion src/Editor/SaveLevelState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void Update(MouseState mouse, MouseState mouseOld, KeyboardState kb, Keyb
if (_saveButton.IsClicked)
{
BoardSaver.SaveBoard(board, _levelNameBox.text, maxHints);
File.Delete(Path.Combine(BoardSaver.GetLevelSavePath(), "..", "EditorAutosave.nono"));
File.Delete(Path.Combine(Settings.GetDataFolderPath(), "EditorAutosave.nono"));
_saved = true;
}

Expand Down
7 changes: 6 additions & 1 deletion src/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ public static class Settings
public const float AccentColorDefaultDarkerAmount = 0.3f;
public const float AccentColorDefaultLighterAmount = 0.5f;

public static string GetDataFolderPath()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "nonoSharp");
}

public static void Initialize()
{
Log.Logger.Information("Initializing settings");

string settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "nonoSharp");
string settingsFolder = GetDataFolderPath();
Log.Logger.Information($"Settings folder: {settingsFolder}");
Directory.CreateDirectory(settingsFolder);

Expand Down

0 comments on commit 13042d6

Please sign in to comment.