From b791a6e7dcc51eddd3a0fecde4e62d0e1211c110 Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 14 May 2013 22:16:10 +0100 Subject: [PATCH] [Add] Filesystem abstraction and path based stuff. --- src/lephisto.c | 25 ++--------------- src/lfile.c | 48 +++++++++++++++++++++++++++++++ src/lfile.h | 5 ++++ src/menu.c | 7 ++--- src/player.c | 14 +++++++--- src/save.c | 76 +++++++++++++++++++++++++++++--------------------- src/save.h | 2 +- 7 files changed, 114 insertions(+), 63 deletions(-) create mode 100644 src/lfile.c create mode 100644 src/lfile.h diff --git a/src/lephisto.c b/src/lephisto.c index ff3947d..1223865 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -1,13 +1,6 @@ #include #include -#ifdef LINUX -#include -#include -#include -#include -#endif - #include "lephisto.h" #include "conf.h" #include "log.h" @@ -35,6 +28,7 @@ #include "economy.h" #include "menu.h" #include "mission.h" +#include "lfile.h" #include "music.h" #define XML_START_ID "Start" @@ -82,8 +76,6 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int main(int argc, char** argv) { #endif - char* home, dir[PATH_MAX]; - // Print the version. snprintf(version, VERSION_LEN, "%d.%d.%d", VMAJOR, VMINOR, VREV); LOG(" "APPNAME" v%s", version); @@ -95,19 +87,8 @@ int main(int argc, char** argv) { input_init(); // Create the home directory if needed. -#ifdef LINUX - struct stat buf; - - home = getenv("HOME"); - snprintf(dir, PATH_MAX, "%s/.lephisto", home); - stat(dir, &buf); - if(!S_ISDIR(buf.st_mode)) { - if(mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) < 0) - WARN("Unable to create lephisto directory '%s'", dir); - else - DEBUG("Created lephisto directory '%s'", dir); - } -#endif + if(lfile_dirMakeExist(".")) + WARN("Unable to create lephisto directory '%s'", lfile_basePath()); // Set the configuration. conf_setDefaults(); // Default config values. diff --git a/src/lfile.c b/src/lfile.c new file mode 100644 index 0000000..61e1733 --- /dev/null +++ b/src/lfile.c @@ -0,0 +1,48 @@ +#include +#ifdef LINUX +#include +#include +#include +#include +#include +#endif + +#include "lephisto.h" +#include "log.h" +#include "lfile.h" + +// Return lephisto's base path. +static char lephisto_base[128] = "\0"; +char* lfile_basePath(void) { + char* home; + + if(lephisto_base[0] == '\0') { +#ifdef LINUX + home = getenv("HOME"); +#endif + snprintf(lephisto_base, PATH_MAX, "%s/.lephisto/", home); + } + + return lephisto_base; +} + +// Check if a directory exists, and create it if it doesn't. +// Based on lephisto_base. +int lfile_dirMakeExist(char* path) { + char file[PATH_MAX]; + +#ifdef LINUX + struct stat buf; + + snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), path); + stat(file, &buf); + if(!S_ISDIR(buf.st_mode)) + if(mkdir(file, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { + WARN("Dir '%s' does not exist and unable to create", path); + return -1; + } +#endif + + return 0; +} + diff --git a/src/lfile.h b/src/lfile.h new file mode 100644 index 0000000..ab49ba9 --- /dev/null +++ b/src/lfile.h @@ -0,0 +1,5 @@ +#pragma once + +char* lfile_basePath(void); +int lfile_dirMakeExist(char* path); + diff --git a/src/menu.c b/src/menu.c index 58fe034..e8b3552 100644 --- a/src/menu.c +++ b/src/menu.c @@ -40,7 +40,7 @@ int menu_open = 0; // Main menu. -static void menu_main_close(void); +void menu_main_close(void); static void menu_main_load(char* str); static void menu_main_new(char* str); // Small menu. @@ -90,7 +90,7 @@ void menu_main(void) { menu_Open(MENU_MAIN); } -static void menu_main_close(void) { +void menu_main_close(void) { window_destroy(window_get("Main Menu")); gl_freeTexture(window_getImage(window_get("BG"), "imgBG")); @@ -102,8 +102,7 @@ static void menu_main_close(void) { static void menu_main_load(char* str) { (void)str; - menu_main_close(); - load_game("test.xml"); + load_game_menu(); } static void menu_main_new(char* str) { diff --git a/src/player.c b/src/player.c index 296283a..42bad03 100644 --- a/src/player.c +++ b/src/player.c @@ -20,6 +20,7 @@ #include "ltime.h" #include "hook.h" #include "map.h" +#include "lfile.h" #include "player.h" #define XML_GUI_ID "GUIs" // XML section identifier. @@ -1313,16 +1314,21 @@ void player_screenshot(void) { int done; char filename[PATH_MAX]; + if(lfile_dirMakeExist("../screenshots")) { + WARN("Aborting screenshots"); + return; + } + done = 0; do { - if(screenshot_cur >= 128) { + if(screenshot_cur >= 999) { // Just incase I fucked up. :) - WARN("You have reached the maximum amount of screenshots [128]"); + WARN("You have reached the maximum amount of screenshots [999]"); return; } - snprintf(filename, PATH_MAX, "../screenshots/screenshot%03d.png", - screenshot_cur); + snprintf(filename, PATH_MAX, "%sscreenshots/screenshot%03d.png", + lfile_basePath(), screenshot_cur); fp = fopen(filename, "r"); // Myeah, I know it's a horrible way to check. if(fp == NULL) done = 1; else { diff --git a/src/save.c b/src/save.c index bb6219a..dd53052 100644 --- a/src/save.c +++ b/src/save.c @@ -1,25 +1,30 @@ -#include -#ifdef LINUX -#include -#include -#include -#include -#endif - #include "lephisto.h" #include "log.h" #include "xml.h" #include "player.h" +#include "toolkit.h" +#include "menu.h" +#include "lfile.h" #include "save.h" -// Externs. -extern int player_save(xmlTextWriterPtr writer); -extern int missions_save(xmlTextWriterPtr writer); -extern int var_save(xmlTextWriterPtr writer); // misn var. -extern int player_load(xmlNodePtr parent); -// Static. -static int save_data(xmlTextWriterPtr writer); +#define LOAD_WIDTH 400 +#define LOAD_HEIGHT 300 +#define BUTTON_WIDTH 50 +#define BUTTON_HEIGHT 30 + +// Externs. +extern int player_save(xmlTextWriterPtr writer); +extern int missions_save(xmlTextWriterPtr writer); +extern int var_save(xmlTextWriterPtr writer); // misn var. +extern int player_load(xmlNodePtr parent); +extern void menu_main_close(void); +// Static. +static int save_data(xmlTextWriterPtr writer); +static void load_menu_close(char* str); +static int load_game(char* file); + +// Save all the game data. static int save_data(xmlTextWriterPtr writer) { // The data itself. if(player_save(writer) < 0) return -1; @@ -31,7 +36,7 @@ static int save_data(xmlTextWriterPtr writer) { // Save the current game. int save_all(void) { - char file[PATH_MAX], *home; + char file[PATH_MAX]; xmlDocPtr doc; xmlTextWriterPtr writer; @@ -54,21 +59,13 @@ int save_all(void) { xmlw_endElem(writer); // lephisto_save. xmlw_done(writer); -#ifdef LINUX - struct stat buf; - home = getenv("HOME"); - snprintf(file, PATH_MAX, "%s/.lephisto/saves", home); - stat(file, &buf); - if(!S_ISDIR(buf.st_mode)) - if(mkdir(file, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { - WARN("Unable to create '%s' for saving: %s", file, strerror(errno)); - WARN("Aborting save..."); - xmlFreeTextWriter(writer); - xmlFreeDoc(doc); - return -1; - } - snprintf(file, PATH_MAX, "%s/.lephisto/saves/%s.ls", home, player_name); -#endif + if(lfile_dirMakeExist("saves") < 0) { + WARN("Aborting save..."); + xmlFreeTextWriter(writer); + xmlFreeDoc(doc); + return -1; + } + snprintf(file, PATH_MAX, "%ssaves/%s.ls", lfile_basePath(), player_name); xmlFreeTextWriter(writer); xmlSaveFileEnc(file, doc, "UTF-8"); @@ -77,8 +74,23 @@ int save_all(void) { return 0; } +// Open the load game menu. +void load_game_menu(void) { + unsigned int wid; + + wid = window_create("Load Game", -1, -1, LOAD_WIDTH, LOAD_HEIGHT); + window_addButton(wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, + "btnBack", "Back", load_menu_close); +} + +static void load_menu_close(char* str) { + (void)str; + + window_destroy(window_get("Load Game")); +} + // Load a new game. -int load_game(char* file) { +static int load_game(char* file) { xmlNodePtr node; xmlDocPtr doc; diff --git a/src/save.h b/src/save.h index c35a0af..b130505 100644 --- a/src/save.h +++ b/src/save.h @@ -1,5 +1,5 @@ #pragma once int save_all(void); -int load_game(char* file); +void load_game_menu(void);