[Change] Huge changes to Lephisto's data management.

This commit is contained in:
Allanis 2014-04-26 18:53:22 +01:00
parent 82a82ed70e
commit 05398e71d2
24 changed files with 342 additions and 236 deletions

View File

@ -58,7 +58,7 @@
#include "pilot.h"
#include "player.h"
#include "physics.h"
#include "pack.h"
#include "ldata.h"
#include "rng.h"
#include "space.h"
#include "faction.h"
@ -387,7 +387,7 @@ int ai_init(void) {
uint32_t nfiles, i;
/* Get the file list. */
files = pack_listfiles(data, &nfiles);
files = ldata_list(AI_PREFIX, &nfiles);
/* Load the profiles. */
for(i = 0; i < nfiles; i++)
@ -399,11 +399,6 @@ int ai_init(void) {
if(ai_loadProfile(files[i])) /* Load the profiles. */
WARN("Error loading AI profile '%s'", files[i]);
/* Free the char allocated by pack. */
for(i = 0; i < nfiles; i++)
free(files[i]);
free(files);
DEBUG("Loaded %d AI profile%c", nprofiles, (nprofiles==1)?' ':'s');
return 0;
@ -456,7 +451,7 @@ static int ai_loadProfile(char* filename) {
lua_loadVector(L);
/* Now load the file, since all the functions have been previously loaded. */
buf = pack_readfile(DATA, filename, &bufsize);
buf = ldata_read(filename, &bufsize);
if(luaL_dobuffer(L, buf, bufsize, filename) != 0) {
ERR("Error loading AI file: %s\n"
"%s\n"

View File

@ -13,7 +13,7 @@
#include "input.h"
#include "music.h"
#include "nebulae.h"
#include "pack.h"
#include "ldata.h"
#include "lfile.h"
#include "conf.h"
@ -85,32 +85,6 @@ static void print_usage(char** argv) {
* @brief Set the default configuration.
*/
void conf_setDefaults(void) {
int i, nfiles;
char** files;
size_t len;
/* Find data. */
if(lfile_fileExists("%s-%d.%d.%d", DATA_NAME, VMAJOR, VMINOR, VREV)) {
data = malloc(PATH_MAX);
snprintf(data, PATH_MAX, "%s-%d.%d.%d", DATA_NAME, VMAJOR, VMINOR, VREV);
} else if(lfile_fileExists(DATA_DEF))
data = DATA_DEF;
else {
files = lfile_readDir(&nfiles, ".");
len = strlen(DATA_NAME);
for(i = 0; i < nfiles; i++) {
if(strncmp(files[i], DATA_NAME, len) == 0) {
/* Must be packfile. */
if(pack_check(files[i]))
continue;
data = strdup(files[i]);
}
}
for(i = 0; i < nfiles; i++)
free(files[i]);
free(files);
}
/* GL. */
gl_screen.w = 800;
gl_screen.h = 600;
@ -142,7 +116,10 @@ int conf_loadConfig(const char* file) {
if(luaL_dofile(L, file) == 0) {
/* Conf file exists indeed. */
/* Global. */
conf_loadString("data", data);
lua_getglobal(L, "data");
if(lua_isstring(L, -1))
ldata_setPath((char*)lua_tostring(L, -1));
lua_remove(L, -1);
/* OpenGL properties.. */
@ -212,14 +189,12 @@ int conf_loadConfig(const char* file) {
/* Joystick. */
lua_getglobal(L, "joystick");
if(lua_isnumber(L, -1)) {
if(lua_isnumber(L, -1))
indjoystick = (int)lua_tonumber(L, -1);
lua_remove(L, -1);
}
else if(lua_isstring(L, -1)) {
else if(lua_isstring(L, -1))
namjoystick = strdup((char*)lua_tostring(L, -1));
lua_remove(L, -1);
}
/* Keybindings. */
for(i = 0; strcmp(keybindNames[i], "end"); i++) {
@ -362,7 +337,7 @@ void conf_parseCLI(int argc, char** argv) {
gl_screen.flags |= OPENGL_VSYNC;
break;
case 'd':
data = strdup(optarg);
ldata_setPath(optarg);
break;
case 'j':
indjoystick = atoi(optarg);

View File

@ -16,7 +16,7 @@
#include "lephisto.h"
#include "lxml.h"
#include "pack.h"
#include "ldata.h"
#include "log.h"
#include "spfx.h"
#include "pilot.h"
@ -154,7 +154,7 @@ void commodity_Jettison(int pilot, Commodity* com, int quantity) {
/* Init/exit */
int commodity_load(void) {
uint32_t bufsize;
char* buf = pack_readfile(DATA, COMMODITY_DATA, &bufsize);
char* buf = ldata_read(COMMODITY_DATA, &bufsize);
xmlNodePtr node;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);

View File

@ -10,7 +10,7 @@
#include "opengl.h"
#include "lephisto.h"
#include "log.h"
#include "pack.h"
#include "ldata.h"
#include "lxml.h"
#include "rng.h"
#include "faction.h"
@ -548,7 +548,7 @@ void factions_reset(void) {
int factions_load(void) {
int mem;
uint32_t bufsize;
char* buf = pack_readfile(DATA, FACTION_DATA, &bufsize);
char* buf = ldata_read(FACTION_DATA, &bufsize);
xmlNodePtr factions, node;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);

View File

@ -22,7 +22,7 @@
#include "lephisto.h"
#include "log.h"
#include "pack.h"
#include "ldata.h"
#define FONT_DEF "../dat/font.ttf" /**< Default font path. */
@ -523,7 +523,7 @@ void gl_fontInit(glFont* font, const char* fname, const unsigned int h) {
if(font == NULL) font = &gl_defFont;
FT_Byte* buf = pack_readfile(DATA, (fname!=NULL) ? fname : FONT_DEF, &bufsize);
FT_Byte* buf = ldata_read((fname != NULL) ? fname : FONT_DEF, &bufsize);
/* Allocatagery. */
font->textures = malloc(sizeof(GLuint)*128);

View File

@ -10,7 +10,7 @@
#include "SDL.h"
#include "lephisto.h"
#include "log.h"
#include "pack.h"
#include "ldata.h"
#include "font.h"
#include "music.h"
#include "intro.h"
@ -33,7 +33,7 @@ static int intro_load(void) {
int i, p, n;
int mem;
intro_buf = pack_readfile(DATA, "../dat/intro", &intro_size);
intro_buf = ldata_read("../dat/intro", &intro_size);
intro_length = intro_size; /* Length aproximation! */
/* Create intro font. */

198
src/ldata.c Normal file
View File

@ -0,0 +1,198 @@
/**
* @file ldata.c
*
* @brief Wrapper to handle reading/writing the ldata file.
*
* Optimizes to minimize the opens and frees, plus tries to read from the
* filesystem instead of always looking for a packfile.
*/
#include "lephisto.h"
#include "log.h"
#include "md5.h"
#include "lxml.h"
#include "pack.h"
#include "lfile.h"
#include "ldata.h"
#define LDATA_FILENAME "ldata"
#define LDATA_DEF "ldata"
#define XML_START_ID "Start" /**< XML document tag of module start file. */
#define START_DATA "../dat/start.xml" /**< Path to module start file. */
/* Packfile. */
static char* ldata_filename = NULL; /**< Packfile name. */
static Packfile_t* ldata_pack = NULL; /**< Actual packfile. */
static char* ldata_packName = NULL; /**< Name of the ldata module. */
/* File list. */
static char** ldata_fileList = NULL; /**< List of the files in the packfile. */
static uint32_t ldata_fileNList = 0; /**< Number of files in ldata_fileList. */
/**
* @brief Check to see if path is a ldata file.
* @param path Path to check to see if it's an ldata file.
* @return 1 if it is an ldata file, otherwise 0.
*/
int ldata_check(char* path) {
return pack_check(path);
}
/**
* @brief Set the current ldata path to use.
* @param path Path to set.
*/
int ldata_setPath(char* path) {
if(ldata_filename != NULL)
free(ldata_filename);
ldata_filename = (path == NULL) ? NULL : strdup(path);
return 0;
}
/**
* @brief Open a packfile if needed.
*/
static int ldata_openPackfile(void) {
int i;
char** files;
int nfiles;
size_t len;
if(lfile_fileExists("%s-%d.%d.%d", LDATA_FILENAME, VMAJOR, VMINOR, VREV)) {
ldata_filename = malloc(PATH_MAX);
snprintf(ldata_filename, PATH_MAX, "%s-%d.%d.%d", LDATA_FILENAME, VMAJOR,
VMINOR, VREV);
}
else if(lfile_fileExists(LDATA_DEF))
ldata_filename = strdup(LDATA_DEF);
else {
files = lfile_readDir(&nfiles, ".");
len = strlen(LDATA_FILENAME);
for(i = 0; i < nfiles; i++) {
if(strncmp(files[i], LDATA_FILENAME, len)==0) {
/* Must be packed. */
if(pack_check(files[i]))
continue;
ldata_filename = strdup(files[i]);
break;
}
}
for(i = 0; i < nfiles; i++)
free(files[i]);
free(files);
}
return 0;
}
/**
* @brief Open the ldata file.
* @return 0 on success.
*/
int ldata_open(void) {
return 0;
}
/**
* @brief Close and clean up the ldata file.
*/
void ldata_close(void) {
unsigned int i;
/* Destroy the name. */
if(ldata_packName != NULL) {
free(ldata_packName);
ldata_packName = NULL;
}
/* Destroy the list. */
if(ldata_fileList != NULL) {
for(i = 0; i < ldata_fileNList; i++)
free(ldata_fileList[i]);
free(ldata_fileList);
ldata_fileList = NULL;
ldata_fileNList = 0;
}
/* Close the packfile. */
/*
pack_close(ldata_pack);
ldata_pack = NULL;
*/
}
/**
* @brief Get the ldata's name.
* @return The ldata's name.
*/
char* ldata_name(void) {
char* buf;
uint32_t size;
xmlNodePtr node;
xmlDocPtr doc;
/* Alreay loaded. */
if(ldata_packName != NULL)
return ldata_packName;
/* We'll just read it and parse it. */
buf = ldata_read(START_DATA, &size);
doc = xmlParseMemory(buf, size);
/* Make sure it's what we are looking for. */
node = doc->xmlChildrenNode;
if(!xml_isNode(node, XML_START_ID)) {
ERR("Malformed '"START_DATA"' file: missing root element '"XML_START_ID"'");
return NULL;
}
/* Check if node is valid. */
node = node->xmlChildrenNode; /* First system node. */
if(node == NULL) {
ERR("Malformed '"START_DATA"' file: does not contain elements");
return NULL;
}
do {
xmlr_strd(node, "name", ldata_packName);
} while(xml_nextNode(node));
xmlFreeDoc(doc);
free(buf);
/* Check if data name is found. */
if(ldata_packName == NULL)
WARN("No ldata packname found.");
return ldata_packName;
}
/**
* @brief Read a file from the ldata.
*/
void* ldata_read(const char* filename, uint32_t* filesize) {
if(ldata_filename == NULL)
ldata_openPackfile();
return pack_readfile(ldata_filename, filename, filesize);
}
/**
* @brief Get the list of files in the ldata.
*/
char** ldata_list(const char* path, uint32_t* nfiles) {
(void)path;
if(ldata_fileList != NULL) {
*nfiles = ldata_fileNList;
return ldata_fileList;
}
if(ldata_filename == NULL)
ldata_openPackfile();
ldata_fileList = pack_listfiles(ldata_filename, &ldata_fileNList);
*nfiles = ldata_fileNList;
return ldata_fileList;
}

16
src/ldata.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include <stdint.h>
/* ldata open/close. */
int ldata_open(void);
void ldata_close(void);
/* General. */
int ldata_check(char* path);
int ldata_setPath(char* path);
char* ldata_name(void);
/* Individual file functions. */
void* ldata_read(const char* filename, uint32_t* filesize);
char** ldata_list(const char* path, uint32_t* nfiles);

View File

@ -36,7 +36,7 @@
#include "rng.h"
#include "ai.h"
#include "outfit.h"
#include "pack.h"
#include "ldata.h"
#include "weapon.h"
#include "faction.h"
#include "lxml.h"
@ -55,9 +55,6 @@
#include "nebulae.h"
#define XML_START_ID "Start" /**< XML document tag of module start file. */
#define START_DATA "../dat/start.xml" /**< Path to module start file. */
#define CONF_FILE "conf" /**< Configuration file by default. */
#define VERSION_FILE "VERSION" /**< Version file by default. */
#define VERSION_LEN 10 /**< Max length of the version file. */
@ -72,8 +69,6 @@ static char version[VERSION_LEN]; /**< Contains version. */
static glTexture* loading; /**< Loading screen. */
/* Some defaults. */
char* data = NULL; /**< Path to datafile. */
char* dataname = NULL; /**< Name of data file. */
int nosound = 0; /**< Disable sound when loaded. */
int show_fps = 1; /**< Shows fps - default true. */
int max_fps = 0; /**< Default FPS limit, 0 is no limit. */
@ -90,7 +85,6 @@ static void load_all(void);
static void unload_all(void);
static void display_fps(const double dt);
static void window_caption(void);
static void data_name(void);
static void debug_sigInit(void);
/* Update. */
static void fps_control(void);
@ -151,9 +145,12 @@ int main(int argc, char** argv) {
conf_loadConfig(buf); /* Have Lua parse config. */
conf_parseCLI(argc, argv); /* Parse CLI arguments. */
/* Open the data. */
if(ldata_open() != 0)
ERR("Failed to open ldata.");
/* Load the data basics. */
data_name();
LOG(" %s", dataname);
LOG(" %s", ldata_name());
DEBUG();
/* Display the SDL Version. */
@ -263,6 +260,9 @@ int main(int argc, char** argv) {
gl_freeFont(NULL);
gl_freeFont(&gl_smallFont);
/* Close data. */
ldata_close();
/* Exit subsystems. */
toolkit_exit(); /* Kill the toolkit. */
ai_exit(); /* Stop the Lua AI magicness. */
@ -292,7 +292,7 @@ void loadscreen_load(void) {
int nload;
/* Count the loading screens. */
files = pack_listfiles(data, &nfiles);
files = ldata_list("../gfx/loading/", &nfiles);
len = strlen("../gfx/loading/");
nload = 0;
loadscreens = malloc(sizeof(char*) * nfiles);
@ -300,8 +300,7 @@ void loadscreen_load(void) {
if(strncmp(files[i], "../gfx/loading/", len)==0) {
loadscreens[nload] = files[i];
nload++;
} else
free(files[i]);
}
}
/* Must have loading screens. */
@ -315,8 +314,6 @@ void loadscreen_load(void) {
loading = gl_newImage(file_path);
/* Clean up. */
for(i = 0; i < nload; i++)
free(loadscreens[i]);
free(loadscreens);
}
@ -595,73 +592,16 @@ static void display_fps(const double dt) {
gl_print(NULL, x, y, NULL, "%3.1fx", dt_mod);
}
/**
* @fn static void data_name(void)
*
* @brief Set the data module's name.
*/
static void data_name(void) {
uint32_t bufsize;
char* buf;
/* Check if data file is valid. */
if((DATA == NULL) || (pack_check(DATA))) {
WARN("Data file '%s' not found", DATA);
WARN("You should specify which data file to use with '-d'");
WARN("See -h or --help for more information");
SDL_Quit();
exit(EXIT_FAILURE);
}
/* Check the version. */
buf = pack_readfile(DATA, VERSION_FILE, &bufsize);
if(strncmp(buf, version, bufsize) != 0) {
WARN("Lephisto version and data module version differ!");
WARN("Lephisto is v%s, data is for v%s", version, buf);
}
free(buf);
/* Load the datafiles name. */
buf = pack_readfile(DATA, START_DATA, &bufsize);
xmlNodePtr node;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
node = doc->xmlChildrenNode;
if(!xml_isNode(node, XML_START_ID)) {
ERR("Maformed '"START_DATA"' file: missing root element '"XML_START_ID"'");
return;
}
node = node->xmlChildrenNode; /* First node. */
if(node == NULL) {
ERR("Malformed '"START_DATA"' file: does not contain elements");
return;
}
do {
xmlr_strd(node, "name", dataname);
} while(xml_nextNode(node));
xmlFreeDoc(doc);
free(buf);
}
/**
* @fn static void window_caption(void)
*
* @brief Set the window caption.
*/
static void window_caption(void) {
size_t len;
char* tmp;
char buf[PATH_MAX];
len = strlen(dataname) + strlen(APPNAME);
tmp = malloc(sizeof(char)*len);
snprintf(tmp, len, APPNAME" - %s", dataname);
SDL_WM_SetCaption(tmp, NULL);
free(tmp);
snprintf(buf, PATH_MAX, APPNAME" - %s", ldata_name());
SDL_WM_SetCaption(buf, NULL);
}
static char human_version[50]; /**< Stores human readable version string. */
@ -675,7 +615,7 @@ static char human_version[50]; /**< Stores human readable version string. */
*/
char* lephisto_version(void) {
if(human_version[0] == '\0')
snprintf(human_version, 50, " "APPNAME" v%s - %s", version, dataname);
snprintf(human_version, 50, " "APPNAME" v%s - %s", version, ldata_name());
return human_version;
}

View File

@ -19,15 +19,6 @@
#define pow2(x) ((x)*(x)) /**< ^2 */
#define DATA_NAME "ldata" /**< Default data name. */
#ifndef DATA_DEF
#define DATA_DEF DATA_NAME /**< Default data packfile. */
#endif
extern char* data; /**< Modifiable datafile. */
#define DATA data /**< Standard data file to use. */
extern char *dataname; /**< Datafile name. */
/* Max filename path. */
#ifndef PATH_MAX
# define PATH_MAX 256 /**< If not already defined. */

View File

@ -20,7 +20,7 @@
#include "space.h"
#include "land.h"
#include "map.h"
#include "pack.h"
#include "ldata.h"
#include "lluadef.h"
#include "llua.h"
@ -142,9 +142,9 @@ static int llua_packfileLoader(lua_State* L) {
filename = (char*) lua_tostring(L, 1);
/* Try to locate the data. */
buf = pack_readfile(DATA, filename, &bufsize);
buf = ldata_read(filename, &bufsize);
if(buf == NULL) {
lua_pushfstring(L, "%s not found in packfile %s", filename, DATA);
lua_pushfstring(L, "%s not found in ldata.", filename);
return 1;
}

View File

@ -10,7 +10,7 @@
#include "lephisto.h"
#include "log.h"
#include "hook.h"
#include "pack.h"
#include "ldata.h"
#include "lxml.h"
#include "faction.h"
#include "player.h"
@ -117,7 +117,7 @@ static int mission_init(Mission* mission, MissionData* misn, int load) {
misn_loadLibs(mission->L); /* Load our custom libraries. */
/* Load the file. */
buf = pack_readfile(DATA, misn->lua, &bufsize);
buf = ldata_read(misn->lua, &bufsize);
if(luaL_dobuffer(mission->L, buf, bufsize, misn->lua) != 0) {
ERR("Error loading mission file: %s\n"
"%s\n"
@ -551,7 +551,7 @@ static MissionData* mission_parse(const xmlNodePtr parent) {
/* Load/Free. */
int missions_load(void) {
uint32_t bufsize;
char* buf = pack_readfile(DATA, MISSION_DATA, &bufsize);
char* buf = ldata_read(MISSION_DATA, &bufsize);
MissionData* tmp;

View File

@ -13,7 +13,7 @@
#include "lephisto.h"
#include "log.h"
#include "pack.h"
#include "ldata.h"
#include "music.h"
#define MUSIC_PREFIX "../snd/music/" /**< Prefix of where to find music. */
@ -173,7 +173,7 @@ static int music_find(void) {
if(music_disabled) return 0;
/* Get the file list. */
files = pack_listfiles(data, &nfiles);
files = ldata_list(MUSIC_PREFIX, &nfiles);
/* Load the profiles. */
mem = 0;
@ -199,11 +199,6 @@ static int music_find(void) {
}
music_selection = realloc(music_selection, sizeof(char*)*nmusic_selection);
/* Free the char* allocated by pack. */
for(i = 0; i < nfiles; i++)
free(files[i]);
free(files);
DEBUG("Loaded %d song%c", nmusic_selection, (nmusic_selection==1)?' ':'s');
return 0;
@ -238,7 +233,7 @@ void music_load(const char* name) {
/* Load the data. */
snprintf(filename, PATH_MAX, MUSIC_PREFIX"%s"MUSIC_SUFFIX, name);
music_data = pack_readfile(DATA, filename, &size);
music_data = ldata_read(filename, &size);
music_rw = SDL_RWFromMem(music_data, size);
music_music = Mix_LoadMUS_RW(music_rw);
if(music_music == NULL)
@ -336,7 +331,7 @@ static int music_luaInit(void) {
lua_loadMusic(music_lua, 0); /* Write it. */
/* Load the actual lua music code. */
buf = pack_readfile(DATA, MUSIC_LUA_PATH, &bufsize);
buf = ldata_read(MUSIC_LUA_PATH, &bufsize);
if(luaL_dobuffer(music_lua, buf, bufsize, MUSIC_LUA_PATH) != 0) {
ERR("Error loading music file: %s\n"
"%s\n"

View File

@ -12,7 +12,7 @@
#include "lephisto.h"
#include "log.h"
#include "pack.h"
#include "ldata.h"
#include "opengl.h"
/* Requirements. */
@ -394,9 +394,9 @@ static glTexture* gl_loadNewImage(const char* path) {
char* buf;
/* Load from packfile. */
buf = pack_readfile(DATA, (char*)path, &filesize);
buf = ldata_read(path, &filesize);
if(buf == NULL) {
ERR("Loading surface from packfile.");
ERR("Loading surface from ldata.");
return NULL;
}
SDL_RWops* rw = SDL_RWFromMem(buf, filesize);

View File

@ -5,7 +5,7 @@
#include "lephisto.h"
#include "log.h"
#include "pack.h"
#include "ldata.h"
#include "lxml.h"
#include "spfx.h"
#include "outfit.h"
@ -1038,7 +1038,7 @@ static int outfit_parse(Outfit* tmp, const xmlNodePtr parent) {
int outfit_load(void) {
int i, mem;
uint32_t bufsize;
char* buf = pack_readfile(DATA, OUTFIT_DATA, &bufsize);
char* buf = ldata_read(OUTFIT_DATA, &bufsize);
xmlNodePtr node;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);

View File

@ -32,13 +32,31 @@
* -- Pack the files.
*/
/**
* @struct Packfile
*
* @brief Abstracts around packfiles.
*/
struct Packfile_s {
#ifdef _POSIX_SOURCE
int fd; /**< File descriptor. */
#else
FILE* fp; /**< For non-posix. */
#endif
uint32_t pos; /**< Cursor position. */
uint32_t start; /**< File start. */
uint32_t end; /**< File end. */
};
#undef DEBUG /* This will be spammy. */
#define DEBUG(str, args...) do{;} while(0)
#define BLOCKSIZE 128*1024 /**< The read/write block size */
/* Max filename length. */
#define MAX_FILENAME 100 /**< Maximum file name length. */
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
#define PERMS S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH /**< Default permissions. */
@ -165,9 +183,9 @@ int pack_files(const char* outfile, const char** infiles, const uint32_t nfiles)
ERR("File %s does not exist", infiles[i]);
return -1;
}
if(strlen(infiles[i]) > MAX_FILENAME) {
if(strlen(infiles[i]) > PATH_MAX) {
ERR("filename '%s' is too long, should be only %d characters", infiles[i],
MAX_FILENAME);
PATH_MAX);
return -1;
}
namesize += strlen(infiles[i]);
@ -251,30 +269,32 @@ int pack_files(const char* outfile, const char** infiles, const uint32_t nfiles)
}
#undef WRITE
#ifdef _POSIX_SOURCE
#define READ(b,n) if(read(file->fd, (b), (n))!=(n)) { \
ERR("Fewer bytes read than expected."); \
free(buf); return NULL; }
#else
#define READ(b,n) if(fread((b), 1, (n), file->fp)!=(n)) { \
ERR("Fewer bytes read then expected"); \
free(buf); return NULL; }
#endif
/**
* @fn int pack_open(Packfile* file, const char* packfile, const char* filename)
*
* @brief Open a file in the packfile for reading.
* @param file Packfile to store data into.
* @param packfile Path to real packfile.
* @param filename Name of the file within th. packfile.
* @return 0 on success.
*/
#ifdef _POSIX_SOURCE
#define READ(b,n) if(read(file->fd, (b), (n))!=(n)) { \
ERR("Too few bytes read. Expected more."); \
free(buf); return -1; }
#else
#define READ(b,n) if(fread((b), 1, (n), file->fp)!=(n)) { \
ERR("Fewer bytes read then expected"); \
free(buf); return -1; }
#endif
int pack_open(Packfile* file, const char* packfile, const char* filename) {
Packfile_t* pack_open(const char* packfile, const char* filename) {
int j;
uint32_t nfiles, i;
char* buf = malloc(MAX_FILENAME);
char* buf = malloc(PATH_MAX);
Packfile_t* file;
file->start = file->end = 0;
/* Allocate memory. */
file = malloc(sizeof(Packfile_t));
memset(file, 0, sizeof(Packfile_t));
buf = malloc(PATH_MAX);
#ifdef _POSIX_SOURCE
file->fd = open(packfile, O_RDONLY);
@ -284,13 +304,13 @@ int pack_open(Packfile* file, const char* packfile, const char* filename) {
if(file->fp == NULL) {
#endif
ERR("Error opening %s: %s", filename, strerror(errno));
return -1;
return NULL;
}
READ(buf, sizeof(magic)); /* Make sure it's a packfile. */
if(memcmp(buf, &magic, sizeof(magic))) {
ERR("File %s is not a valid packfile", filename);
return -1;
return NULL;
}
READ(&nfiles, 4);
@ -324,7 +344,7 @@ int pack_open(Packfile* file, const char* packfile, const char* filename) {
if(errno) {
#endif
ERR("Failure to seek to file start: %s", strerror(errno));
return -1;
return NULL;
}
READ(&file->end, 4);
DEBUG("\t%d bytes", file->end);
@ -332,15 +352,13 @@ int pack_open(Packfile* file, const char* packfile, const char* filename) {
file->end += file->start;
} else {
ERR("File '%s' not found in packfile '%s'", filename, packfile);
return -1;
return NULL;
}
return 0;
return file;
}
#undef READ
/**
* @fn ssize_t pack_read(Packfile* file, void* buf, size_t count)
*
* @brief Reads data from a packfile.
*
* Behaves like POSIX read.
@ -349,7 +367,7 @@ int pack_open(Packfile* file, const char* packfile, const char* filename) {
* @param count Bytes to read.
* @return Bytes to read or -1 on error.
*/
ssize_t pack_read(Packfile* file, void* buf, size_t count) {
ssize_t pack_read(Packfile_t* file, void* buf, size_t count) {
if(file->pos + count > file->end)
count = file->end - file->pos; /* Can't go past the end! */
if(count == 0) return 0;
@ -370,8 +388,6 @@ ssize_t pack_read(Packfile* file, void* buf, size_t count) {
}
/**
* @fn off_t pack_seek(Packfile* file, off_t offset, int whence)
*
* @brief Seeks within a file inside a packfile.
*
* Behaves like lseek/fseek.
@ -382,7 +398,7 @@ ssize_t pack_read(Packfile* file, void* buf, size_t count) {
* @param whence Either SEEK_SET, SEEK_CUR or SEEK_END.
* @return The position moved to.
*/
off_t pack_seek(Packfile* file, off_t offset, int whence) {
off_t pack_seek(Packfile_t* file, off_t offset, int whence) {
DEBUG("Attempting to seek offset: %d, whence: %d", offset, whence);
off_t ret;
switch(whence) {
@ -427,13 +443,11 @@ off_t pack_seek(Packfile* file, off_t offset, int whence) {
}
/**
* @fn long pack_tell(Packfile* file)
*
* @brief Get the current position in the file.
* @param file Packfile to get the position from.
* @return The current position in the file.
*/
long pack_tell(Packfile* file) {
long pack_tell(Packfile_t* file) {
return file->pos - file->start;
}
@ -447,16 +461,19 @@ long pack_tell(Packfile* file) {
* @return A pointer to the sata in the file or NULL if an error accured.
*/
void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesize) {
Packfile* file = (Packfile*)malloc(sizeof(Packfile));
Packfile_t* file;
void* buf;
char* str;
int size, bytes;
/* Initialize size to 0. */
if(filesize)
*filesize = 0;
if(pack_open(file, packfile, filename)) {
ERR("Opening packfile");
/* Open the packfile. */
file = pack_open(packfile, filename);
if(file == NULL) {
ERR("Opening packfile '%s'.", packfile);
return NULL;
}
DEBUG("Opened file '%s' from '%s'", filename, packfile);
@ -505,7 +522,6 @@ void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesi
return NULL;
}
DEBUG("Closed '%s' in '%s'", filename, packfile);
free(file);
if(filesize)
*filesize = size;
@ -565,7 +581,7 @@ char** pack_listfiles(const char* packfile, uint32_t* nfiles) {
for(i = 0; i < *nfiles; i++) {
/* Start searching files. */
j = 0;
filenames[i] = malloc(MAX_FILENAME * sizeof(char));
filenames[i] = malloc(PATH_MAX * sizeof(char));
READ(&filenames[i][j], 1); /* Get the name. */
while(filenames[i][j++] != '\0')
READ(&filenames[i][j], 1);
@ -584,13 +600,11 @@ char** pack_listfiles(const char* packfile, uint32_t* nfiles) {
#undef READ
/**
* @fn int pack_close(Packfile* file)
*
* @brief Closes a packfile.
* @param file Packfile to close.
* @return 0 on success.
*/
int pack_close(Packfile* file) {
int pack_close(Packfile_t* file) {
int i;
#ifdef _POSIX_SOURCE
i = close(file->fd);

View File

@ -6,33 +6,19 @@
#include <stdint.h>
#include <sys/types.h>
/**
* @struct Packfile
*
* @brief Abstracts around packfile
*/
typedef struct Packfile_ {
#ifdef _POSIX_SOURCE
int fd; /**< File descriptor. */
#else
FILE* fp; /**< For non-posix. */
#endif
uint32_t pos; /**< position. */
uint32_t start; /**< File start */
uint32_t end; /**< File end. */
} Packfile;
struct Packfile_s;
typedef struct Packfile_s Packfile_t;
/* Packfile manipulation. Automatically allocated and freed (with open and close). */
/* Basic. */
int pack_check(const char* filename);
int pack_files(const char* outfile, const char** infiles, const uint32_t nfiles);
int pack_open(Packfile* file, const char* packfile, const char* filename);
ssize_t pack_read(Packfile* file, void* buf, const size_t count);
off_t pack_seek(Packfile* file, off_t offset, int whence);
long pack_tell(Packfile* file);
int pack_close(Packfile* file);
Packfile_t* pack_open(const char* packfile, const char* filename);
ssize_t pack_read(Packfile_t* file, void* buf, const size_t count);
off_t pack_seek(Packfile_t* file, off_t offset, int whence);
long pack_tell(Packfile_t* file);
int pack_close(Packfile_t* file);
/* Fancy stuff. */
void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesize);

View File

@ -11,7 +11,7 @@
#include "lephisto.h"
#include "log.h"
#include "weapon.h"
#include "pack.h"
#include "ldata.h"
#include "lxml.h"
#include "spfx.h"
#include "rng.h"
@ -1785,7 +1785,7 @@ static int fleet_parse(Fleet* tmp, const xmlNodePtr parent) {
int fleet_load(void) {
int mem;
uint32_t bufsize;
char* buf = pack_readfile(DATA, FLEET_DATA, &bufsize);
char* buf = ldata_read(FLEET_DATA, &bufsize);
xmlNodePtr node;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);

View File

@ -11,7 +11,7 @@
#include "log.h"
#include "opengl.h"
#include "font.h"
#include "pack.h"
#include "ldata.h"
#include "lxml.h"
#include "space.h"
#include "rng.h"
@ -337,7 +337,7 @@ static int player_newMake(void) {
sysname = NULL;
player_mission = NULL;
buf = pack_readfile(DATA, START_DATA, &bufsize);
buf = ldata_read(START_DATA, &bufsize);
xmlNodePtr node, cur, tmp;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);
@ -1393,7 +1393,7 @@ int gui_init(void) {
*/
int gui_load(const char* name) {
uint32_t bufsize;
char* buf = pack_readfile(DATA, GUI_DATA, &bufsize);
char* buf = ldata_read(GUI_DATA, &bufsize);
char* tmp;
int found = 0;

View File

@ -8,6 +8,7 @@
#include "menu.h"
#include "lfile.h"
#include "hook.h"
#include "ldata.h"
#include "save.h"
#define LOAD_WIDTH 400
@ -70,7 +71,7 @@ int save_all(void) {
/* Save the version or something.. */
xmlw_startElem(writer, "version");
xmlw_elem(writer, "lephisto", "%d.%d.%d", VMAJOR, VMINOR, VREV);
xmlw_elem(writer, "data", dataname);
xmlw_elem(writer, "data", ldata_name);
xmlw_endElem(writer); /* Version. */
if(save_data(writer) < 0) {

View File

@ -3,7 +3,7 @@
#include "lephisto.h"
#include "log.h"
#include "pack.h"
#include "ldata.h"
#include "lxml.h"
#include "toolkit.h"
#include "ship.h"
@ -372,7 +372,7 @@ static int ship_parse(Ship* tmp, xmlNodePtr parent) {
int ships_load(void) {
int mem;
uint32_t bufsize;
char* buf = pack_readfile(DATA, SHIP_DATA, &bufsize);
char* buf = ldata_read(SHIP_DATA, &bufsize);
xmlNodePtr node;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);

View File

@ -12,7 +12,7 @@
#include "lephisto.h"
#include "log.h"
#include "pack.h"
#include "ldata.h"
#include "music.h"
#include "physics.h"
#include "sound.h"
@ -434,7 +434,7 @@ static int sound_makeList(void) {
if(sound_disabled) return 0;
/* Get the file list. */
files = pack_listfiles(data, &nfiles);
files = ldata_list(SOUND_PREFIX, &nfiles);
/* Load the profiles. */
mem = 0;
@ -463,11 +463,6 @@ static int sound_makeList(void) {
/* Shrink to minimum ram usage. */
sound_list = realloc(sound_list, sound_nlist*sizeof(alSound));
/* Free the char* allocated by pack. */
for(i = 0; i < nfiles; i++)
free(files[i]);
free(files);
DEBUG("Loaded %d sound%s", sound_nlist, (sound_nlist==1)?"":"s");
return 0;
@ -503,7 +498,7 @@ static Mix_Chunk* sound_load(char* filename) {
if(sound_disabled) return NULL;
/* Get the file data buffer from the packfile. */
wavdata = pack_readfile(DATA, filename, &size);
wavdata = ldata_read(filename, &size);
rw = SDL_RWFromMem(wavdata, size);
/* Bind to OpenAL buffer. */

View File

@ -7,7 +7,7 @@
#include "log.h"
#include "physics.h"
#include "rng.h"
#include "pack.h"
#include "ldata.h"
#include "player.h"
#include "pause.h"
#include "weapon.h"
@ -616,7 +616,7 @@ static int planets_load(void) {
xmlNodePtr node;
xmlDocPtr doc;
buf = pack_readfile(DATA, PLANET_DATA, &bufsize);
buf = ldata_read(PLANET_DATA, &bufsize);
doc = xmlParseMemory(buf, bufsize);
node = doc->xmlChildrenNode;
@ -1135,7 +1135,7 @@ int space_load(void) {
*/
static int systems_load(void) {
uint32_t bufsize;
char* buf = pack_readfile(DATA, SYSTEM_DATA, &bufsize);
char* buf = ldata_read(SYSTEM_DATA, &bufsize);
xmlNodePtr node;
xmlDocPtr doc = xmlParseMemory(buf, bufsize);

View File

@ -15,7 +15,7 @@
#include "log.h"
#include "lxml.h"
#include "space.h"
#include "pack.h"
#include "ldata.h"
#include "unidiff.h"
#define CHUNK_SIZE 32 /**< Size of chunk to allocate. */
@ -153,7 +153,7 @@ int diff_apply(char* name) {
if(diff_isApplied(name))
return 0;
buf = pack_readfile(DATA, DIFF_DATA, &bufsize);
buf = ldata_read(DIFF_DATA, &bufsize);
doc = xmlParseMemory(buf, bufsize);
node = doc->xmlChildrenNode;