Skip to content

Commit

Permalink
Merge pull request #10 from savaughn/9-release-builds-cant-find-save-…
Browse files Browse the repository at this point in the history
…folder

Fixed save file path read for builds
  • Loading branch information
savaughn authored Sep 29, 2023
2 parents ef53790 + 72999ca commit 8a60aca
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 28 deletions.
69 changes: 47 additions & 22 deletions src/filehelper.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include "filehelper.h"
#include <libgen.h> // For dirname function
#include <mach-o/dyld.h> // For _NSGetExecutablePath function

char *resolvedPath = NULL;
char *absolutePath = NULL;

void getAbsolutePath(const char* basePath, const char* fileName) {
// Combine the base path and file name
char fullPath[strlen(basePath) + 1 + strlen(fileName) + 1];
sprintf(fullPath, "%s/%s", basePath, fileName);

// Get the absolute path
resolvedPath = realpath(fullPath, NULL);
if (resolvedPath) {
// Allocate memory for the absolute path
absolutePath = (char*)malloc(strlen(resolvedPath) + 1);
if (absolutePath) {
strcpy(absolutePath, resolvedPath);
}
}
}
// This will only work on macOS
void get_save_files(struct SaveFileData *save_data)
{
DIR *dir;
struct dirent *entry;
const char *saveDir = save_data->saveDir;
char **saves_file_path = save_data->saves_file_path;
int *numSaves = &(save_data->numSaves);
char *saveDir = save_data->saveDir;
int numSaves = 0;

char *executablePath = NULL;
uint32_t size = 0;
int result = 0;

// Get the path of the executable
result = _NSGetExecutablePath(NULL, &size);
if (result == -1)
{
executablePath = malloc(size);
result = _NSGetExecutablePath(executablePath, &size);
}

// Get the directory of the executable
char *executableDir = dirname(executablePath);

// Combine the executable directory and save directory
char *saveDirPath = malloc(strlen(executableDir) + 1 + strlen(saveDir) + 1);
sprintf(saveDirPath, "%s/%s", executableDir, saveDir);

// Get the absolute path
absolutePath = realpath(saveDirPath, NULL);
if (absolutePath) {
saveDir = absolutePath;
}

dir = opendir(saveDir);
if (dir == NULL)
{
perror("Error opening directory");
perror("Error opening directory: ");
printf("Path: %s\n", saveDir);
return;
}

Expand All @@ -38,15 +56,22 @@ void get_save_files(struct SaveFileData *save_data)
// Check if the entry is a regular file with a .sav extension
if (entry->d_type == DT_REG && strstr(entry->d_name, ".sav"))
{
getAbsolutePath(saveDir, entry->d_name);
// Combine the base path and file name
char fullPath[strlen(saveDir) + 1 + strlen(entry->d_name) + 1];
sprintf(fullPath, "%s/%s", saveDir, entry->d_name);

// Get the absolute path
absolutePath = realpath(fullPath, NULL);
if (absolutePath) {
saves_file_path[*numSaves] = absolutePath;
(*numSaves)++;
save_data->saves_file_path[numSaves] = absolutePath;
numSaves++;
}
}
}

closedir(dir);
save_data->numSaves = numSaves;
free(saveDirPath);
}

void free_filehelper_pointers(void) {
Expand Down
17 changes: 17 additions & 0 deletions src/fileselectscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,21 @@ void DrawFileSelectScreen(struct pksav_gen2_save *save_player1, struct pksav_gen
save_player2 = NULL;
}
}

// change directory button between back and next buttons in line horizontally centered vertically
int button_width = MeasureText("Change Save Directory", 20) + 20;
DrawText("Change Save Directory", SCREEN_WIDTH / 2 - button_width / 2, BACK_BUTTON_Y, 20, BLACK);
if (CheckCollisionPointRec(GetMousePosition(), (Rectangle){SCREEN_WIDTH / 2 - button_width / 2 - 10, BACK_BUTTON_Y - 30, button_width, BUTTON_HEIGHT}))
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
*current_screen = SCREEN_FILE_EDIT;
selected_saves_index[0] = -1;
selected_saves_index[1] = -1;
trainer1->trainer_id = 0;
trainer2->trainer_id = 0;
save_player1 = NULL;
save_player2 = NULL;
}
}
}
17 changes: 11 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main(int argc, char *argv[])
char inputText[MAX_INPUT_CHARS + 1] = "\0"; // Input text buffer
int textSize = 0; // Current text size

Rectangle inputBox = {SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 - 20, 200, 40};
Rectangle inputBox = { 50, SCREEN_HEIGHT / 2 - 20, SCREEN_WIDTH - 100, 40};
bool editingText = false; // Flag to indicate if the text is being edited

while (!should_close_window && !WindowShouldClose())
Expand Down Expand Up @@ -121,6 +121,12 @@ int main(int argc, char *argv[])
}
break;
case SCREEN_FILE_EDIT:
// Placeholder Text
if (!editingText && textSize == 0) {
strcpy(inputText, save_file_data.saveDir);
textSize = strlen(inputText);
}

if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
// Check if the mouse is clicked within the input box
Expand Down Expand Up @@ -157,8 +163,8 @@ int main(int argc, char *argv[])
editingText = false;
}
}
DrawText("Specify folder name containing saves", 190, 100, 20, BLACK);
DrawText("Saves folder must be in same folder as application: ", 190, 200, 20, BLACK);
DrawText("Specify folder name containing saves", 50, SCREEN_HEIGHT / 2 - 75, 20, BLACK);
DrawText("relative to executable (e.g. \"../my_saves\" or \"saves\")", 50, SCREEN_HEIGHT / 2 - 50, 20, BLACK);

// Draw the input box
DrawRectangleRec(inputBox, WHITE);
Expand All @@ -175,14 +181,13 @@ int main(int argc, char *argv[])
}

// Draw the save button
DrawText("Save", NEXT_BUTTON_X, NEXT_BUTTON_Y, 20, BLACK);
DrawText("Save!", NEXT_BUTTON_X, NEXT_BUTTON_Y, 20, textSize ? BLACK : LIGHTGRAY);

if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
if (CheckCollisionPointRec(GetMousePosition(), (Rectangle){NEXT_BUTTON_X - 15, NEXT_BUTTON_Y - 30, BUTTON_WIDTH, BUTTON_HEIGHT}))
if (CheckCollisionPointRec(GetMousePosition(), (Rectangle){NEXT_BUTTON_X - 15, NEXT_BUTTON_Y - 30, BUTTON_WIDTH, BUTTON_HEIGHT}) && textSize > 0)
{
save_file_data.saveDir = inputText;
printf("Save directory changed to %s\n", save_file_data.saveDir);
save_file_data.numSaves = 0;
*save_file_data.saves_file_path = NULL;
get_save_files(&save_file_data);
Expand Down

0 comments on commit 8a60aca

Please sign in to comment.