From 3f2e3cbfb2b4e1996a88a81ae8630868b6d77c0c Mon Sep 17 00:00:00 2001 From: Louie Shprung Date: Wed, 4 Jan 2023 12:24:25 -0800 Subject: Fix save paths for Windows --- src/draw.c | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/draw.c b/src/draw.c index e49d636..607c786 100644 --- a/src/draw.c +++ b/src/draw.c @@ -213,9 +213,11 @@ int loadScore(){ FILE *savedata; char *path = malloc(sizeof(char) * 256); int hs = 0; - char *home = getenv("HOME"); - sprintf(path, "%s/.local/share/simple-snake/save.bin", home); + if(getenv("HOME") != NULL) + sprintf(path, "%s/.local/share/simple-snake/save.bin", getenv("HOME")); + else if(getenv("APPDATA") != NULL) + sprintf(path, "%s/simple-snake/save.bin", getenv("APPDATA")); savedata = fopen(path, "rb"); if(savedata != NULL){ @@ -230,26 +232,41 @@ int loadScore(){ void saveScore(int highscore){ FILE *savedata; char *path = malloc(sizeof(char) * 256); - char *home = getenv("HOME"); + bool has_appdata = false; //set to true if appdata is defined (implying WIN32) - sprintf(path, "%s/.local/share/simple-snake/save.bin", home); + if(getenv("HOME") != NULL) + sprintf(path, "%s/.local/share/simple-snake/save.bin", getenv("HOME")); + else if(getenv("APPDATA") != NULL){ + sprintf(path, "%s/simple-snake/save.bin", getenv("APPDATA")); + has_appdata = true; + } + else return; savedata = fopen(path, "wb"); //if the file does not exist, create it if(savedata == NULL){ - if(home == NULL) return; + if(!has_appdata){ + sprintf(path, "%s/.local/", getenv("HOME")); + mkdir(path, 0700); + + sprintf(path, "%s/.local/share/", getenv("HOME")); + mkdir(path, 0755); - sprintf(path, "%s/.local/", home); - mkdir(path, 0700); + sprintf(path, "%s/.local/share/simple-snake/", getenv("HOME")); + mkdir(path, 0755); - sprintf(path, "%s/.local/share/", home); - mkdir(path, 0755); + sprintf(path, "%s/.local/share/simple-snake/save.bin", getenv("HOME")); + savedata = fopen(path, "wb"); + } - sprintf(path, "%s/.local/share/simple-snake/", home); - mkdir(path, 0755); + else { + sprintf(path, "%s/simple-snake/", getenv("APPDATA")); + mkdir(path, 0755); + + sprintf(path, "%s/simple-snake/save.bin", getenv("APPDATA")); + savedata = fopen(path, "wb"); + } - sprintf(path, "%s/.local/share/simple-snake/save.bin", home); - savedata = fopen(path, "wb"); if(savedata == NULL) return; } -- cgit