[Add] Filesystem abstraction and path based stuff.
This commit is contained in:
parent
c51612d764
commit
b791a6e7dc
@ -1,13 +1,6 @@
|
|||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <string.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 "lephisto.h"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
@ -35,6 +28,7 @@
|
|||||||
#include "economy.h"
|
#include "economy.h"
|
||||||
#include "menu.h"
|
#include "menu.h"
|
||||||
#include "mission.h"
|
#include "mission.h"
|
||||||
|
#include "lfile.h"
|
||||||
#include "music.h"
|
#include "music.h"
|
||||||
|
|
||||||
#define XML_START_ID "Start"
|
#define XML_START_ID "Start"
|
||||||
@ -82,8 +76,6 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine,
|
|||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char* home, dir[PATH_MAX];
|
|
||||||
|
|
||||||
// Print the version.
|
// Print the version.
|
||||||
snprintf(version, VERSION_LEN, "%d.%d.%d", VMAJOR, VMINOR, VREV);
|
snprintf(version, VERSION_LEN, "%d.%d.%d", VMAJOR, VMINOR, VREV);
|
||||||
LOG(" "APPNAME" v%s", version);
|
LOG(" "APPNAME" v%s", version);
|
||||||
@ -95,19 +87,8 @@ int main(int argc, char** argv) {
|
|||||||
input_init();
|
input_init();
|
||||||
|
|
||||||
// Create the home directory if needed.
|
// Create the home directory if needed.
|
||||||
#ifdef LINUX
|
if(lfile_dirMakeExist("."))
|
||||||
struct stat buf;
|
WARN("Unable to create lephisto directory '%s'", lfile_basePath());
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
// Set the configuration.
|
// Set the configuration.
|
||||||
conf_setDefaults(); // Default config values.
|
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;
|
int menu_open = 0;
|
||||||
|
|
||||||
// Main menu.
|
// Main menu.
|
||||||
static void menu_main_close(void);
|
void menu_main_close(void);
|
||||||
static void menu_main_load(char* str);
|
static void menu_main_load(char* str);
|
||||||
static void menu_main_new(char* str);
|
static void menu_main_new(char* str);
|
||||||
// Small menu.
|
// Small menu.
|
||||||
@ -90,7 +90,7 @@ void menu_main(void) {
|
|||||||
menu_Open(MENU_MAIN);
|
menu_Open(MENU_MAIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void menu_main_close(void) {
|
void menu_main_close(void) {
|
||||||
window_destroy(window_get("Main Menu"));
|
window_destroy(window_get("Main Menu"));
|
||||||
|
|
||||||
gl_freeTexture(window_getImage(window_get("BG"), "imgBG"));
|
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) {
|
static void menu_main_load(char* str) {
|
||||||
(void)str;
|
(void)str;
|
||||||
|
|
||||||
menu_main_close();
|
load_game_menu();
|
||||||
load_game("test.xml");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void menu_main_new(char* str) {
|
static void menu_main_new(char* str) {
|
||||||
|
14
src/player.c
14
src/player.c
@ -20,6 +20,7 @@
|
|||||||
#include "ltime.h"
|
#include "ltime.h"
|
||||||
#include "hook.h"
|
#include "hook.h"
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
|
#include "lfile.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
|
||||||
#define XML_GUI_ID "GUIs" // XML section identifier.
|
#define XML_GUI_ID "GUIs" // XML section identifier.
|
||||||
@ -1313,16 +1314,21 @@ void player_screenshot(void) {
|
|||||||
int done;
|
int done;
|
||||||
char filename[PATH_MAX];
|
char filename[PATH_MAX];
|
||||||
|
|
||||||
|
if(lfile_dirMakeExist("../screenshots")) {
|
||||||
|
WARN("Aborting screenshots");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
done = 0;
|
done = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if(screenshot_cur >= 128) {
|
if(screenshot_cur >= 999) {
|
||||||
// Just incase I fucked up. :)
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
snprintf(filename, PATH_MAX, "../screenshots/screenshot%03d.png",
|
snprintf(filename, PATH_MAX, "%sscreenshots/screenshot%03d.png",
|
||||||
screenshot_cur);
|
lfile_basePath(), screenshot_cur);
|
||||||
fp = fopen(filename, "r"); // Myeah, I know it's a horrible way to check.
|
fp = fopen(filename, "r"); // Myeah, I know it's a horrible way to check.
|
||||||
if(fp == NULL) done = 1;
|
if(fp == NULL) done = 1;
|
||||||
else {
|
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 "lephisto.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "xml.h"
|
#include "xml.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include "toolkit.h"
|
||||||
|
#include "menu.h"
|
||||||
|
#include "lfile.h"
|
||||||
#include "save.h"
|
#include "save.h"
|
||||||
|
|
||||||
// Externs.
|
#define LOAD_WIDTH 400
|
||||||
extern int player_save(xmlTextWriterPtr writer);
|
#define LOAD_HEIGHT 300
|
||||||
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 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) {
|
static int save_data(xmlTextWriterPtr writer) {
|
||||||
// The data itself.
|
// The data itself.
|
||||||
if(player_save(writer) < 0) return -1;
|
if(player_save(writer) < 0) return -1;
|
||||||
@ -31,7 +36,7 @@ static int save_data(xmlTextWriterPtr writer) {
|
|||||||
|
|
||||||
// Save the current game.
|
// Save the current game.
|
||||||
int save_all(void) {
|
int save_all(void) {
|
||||||
char file[PATH_MAX], *home;
|
char file[PATH_MAX];
|
||||||
xmlDocPtr doc;
|
xmlDocPtr doc;
|
||||||
xmlTextWriterPtr writer;
|
xmlTextWriterPtr writer;
|
||||||
|
|
||||||
@ -54,21 +59,13 @@ int save_all(void) {
|
|||||||
xmlw_endElem(writer); // lephisto_save.
|
xmlw_endElem(writer); // lephisto_save.
|
||||||
xmlw_done(writer);
|
xmlw_done(writer);
|
||||||
|
|
||||||
#ifdef LINUX
|
if(lfile_dirMakeExist("saves") < 0) {
|
||||||
struct stat buf;
|
WARN("Aborting save...");
|
||||||
home = getenv("HOME");
|
xmlFreeTextWriter(writer);
|
||||||
snprintf(file, PATH_MAX, "%s/.lephisto/saves", home);
|
xmlFreeDoc(doc);
|
||||||
stat(file, &buf);
|
return -1;
|
||||||
if(!S_ISDIR(buf.st_mode))
|
}
|
||||||
if(mkdir(file, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
|
snprintf(file, PATH_MAX, "%ssaves/%s.ls", lfile_basePath(), player_name);
|
||||||
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
|
|
||||||
|
|
||||||
xmlFreeTextWriter(writer);
|
xmlFreeTextWriter(writer);
|
||||||
xmlSaveFileEnc(file, doc, "UTF-8");
|
xmlSaveFileEnc(file, doc, "UTF-8");
|
||||||
@ -77,8 +74,23 @@ int save_all(void) {
|
|||||||
return 0;
|
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.
|
// Load a new game.
|
||||||
int load_game(char* file) {
|
static int load_game(char* file) {
|
||||||
xmlNodePtr node;
|
xmlNodePtr node;
|
||||||
xmlDocPtr doc;
|
xmlDocPtr doc;
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
int save_all(void);
|
int save_all(void);
|
||||||
int load_game(char* file);
|
void load_game_menu(void);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user