[Add] Filesystem abstraction and path based stuff.
This commit is contained in:
		
							parent
							
								
									c51612d764
								
							
						
					
					
						commit
						b791a6e7dc
					
				| @ -1,13 +1,6 @@ | ||||
| #include <SDL.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| #ifdef LINUX | ||||
| #include <sys/types.h> | ||||
| #include <sys/stat.h> | ||||
| #include <unistd.h> | ||||
| #include <errno.h> | ||||
| #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.
 | ||||
|  | ||||
							
								
								
									
										48
									
								
								src/lfile.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								src/lfile.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,48 @@ | ||||
| #include <string.h> | ||||
| #ifdef LINUX | ||||
| #include <stdlib.h> | ||||
| #include <sys/types.h> | ||||
| #include <sys/stat.h> | ||||
| #include <unistd.h> | ||||
| #include <errno.h> | ||||
| #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; | ||||
| } | ||||
| 
 | ||||
							
								
								
									
										5
									
								
								src/lfile.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/lfile.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | ||||
| #pragma once | ||||
| 
 | ||||
| char* lfile_basePath(void); | ||||
| int   lfile_dirMakeExist(char* path); | ||||
| 
 | ||||
| @ -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) { | ||||
|  | ||||
							
								
								
									
										14
									
								
								src/player.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								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 { | ||||
|  | ||||
							
								
								
									
										76
									
								
								src/save.c
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								src/save.c
									
									
									
									
									
								
							| @ -1,25 +1,30 @@ | ||||
| #include <stdlib.h> | ||||
| #ifdef LINUX | ||||
| #include <sys/types.h> | ||||
| #include <sys/stat.h> | ||||
| #include <unistd.h> | ||||
| #include <errno.h> | ||||
| #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; | ||||
| 
 | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| #pragma once | ||||
| 
 | ||||
| int save_all(void); | ||||
| int load_game(char* file); | ||||
| void load_game_menu(void); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Allanis
						Allanis