[Add] Using the pack cache now, initial loading is a lot faster.
This commit is contained in:
parent
ba26575890
commit
44db60f32b
6
src/ai.c
6
src/ai.c
@ -101,7 +101,7 @@ static int ai_accel(lua_State* L); /* Accelerate. */
|
|||||||
|
|
||||||
/* Internal C routines. */
|
/* Internal C routines. */
|
||||||
static void ai_run(lua_State* L, const char* funcname);
|
static void ai_run(lua_State* L, const char* funcname);
|
||||||
static int ai_loadProfile(char* filename);
|
static int ai_loadProfile(const char* filename);
|
||||||
static void ai_freetask(Task* t);
|
static void ai_freetask(Task* t);
|
||||||
static void ai_setMemory(void);
|
static void ai_setMemory(void);
|
||||||
static void ai_create(Pilot* pilot, char* param);
|
static void ai_create(Pilot* pilot, char* param);
|
||||||
@ -383,7 +383,7 @@ void ai_destroy(Pilot* p) {
|
|||||||
* @return 0 on no errors.
|
* @return 0 on no errors.
|
||||||
*/
|
*/
|
||||||
int ai_init(void) {
|
int ai_init(void) {
|
||||||
char** files;
|
const char** files;
|
||||||
uint32_t nfiles, i;
|
uint32_t nfiles, i;
|
||||||
|
|
||||||
/* Get the file list. */
|
/* Get the file list. */
|
||||||
@ -412,7 +412,7 @@ int ai_init(void) {
|
|||||||
* @param[in] filename File to create the profile from.
|
* @param[in] filename File to create the profile from.
|
||||||
* @return 0 on no error.
|
* @return 0 on no error.
|
||||||
*/
|
*/
|
||||||
static int ai_loadProfile(char* filename) {
|
static int ai_loadProfile(const char* filename) {
|
||||||
char* buf = NULL;
|
char* buf = NULL;
|
||||||
uint32_t bufsize = 0;
|
uint32_t bufsize = 0;
|
||||||
lua_State* L;
|
lua_State* L;
|
||||||
|
95
src/ldata.c
95
src/ldata.c
@ -22,13 +22,13 @@
|
|||||||
#define START_DATA "../dat/start.xml" /**< Path to module start file. */
|
#define START_DATA "../dat/start.xml" /**< Path to module start file. */
|
||||||
|
|
||||||
/* Packfile. */
|
/* Packfile. */
|
||||||
static char* ldata_filename = NULL; /**< Packfile name. */
|
static char* ldata_filename = NULL; /**< Packfile name. */
|
||||||
static Packfile_t* ldata_pack = NULL; /**< Actual packfile. */
|
static Packcache_t* ldata_cache = NULL; /**< Actual packfile. */
|
||||||
static char* ldata_packName = NULL; /**< Name of the ldata module. */
|
static char* ldata_packName = NULL; /**< Name of the ldata module. */
|
||||||
|
|
||||||
/* File list. */
|
/* File list. */
|
||||||
static char** ldata_fileList = NULL; /**< List of the files in the packfile. */
|
static const char** ldata_fileList = NULL; /**< List of the files in the packfile. */
|
||||||
static uint32_t ldata_fileNList = 0; /**< Number of files in ldata_fileList. */
|
static uint32_t ldata_fileNList = 0; /**< Number of files in ldata_fileList. */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check to see if path is a ldata file.
|
* @brief Check to see if path is a ldata file.
|
||||||
@ -59,30 +59,43 @@ static int ldata_openPackfile(void) {
|
|||||||
int nfiles;
|
int nfiles;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
if(lfile_fileExists("%s-%d.%d.%d", LDATA_FILENAME, VMAJOR, VMINOR, VREV)) {
|
/* Try to find the ldata file. */
|
||||||
ldata_filename = malloc(PATH_MAX);
|
if(ldata_filename == NULL) {
|
||||||
snprintf(ldata_filename, PATH_MAX, "%s-%d.%d.%d", LDATA_FILENAME, VMAJOR,
|
/* Check ldata with version appended. */
|
||||||
VMINOR, VREV);
|
if(lfile_fileExists("%s-%d.%d.%d", LDATA_FILENAME, VMAJOR, VMINOR, VREV)) {
|
||||||
}
|
ldata_filename = malloc(PATH_MAX);
|
||||||
else if(lfile_fileExists(LDATA_DEF))
|
snprintf(ldata_filename, PATH_MAX, "%s-%d.%d.%d",
|
||||||
ldata_filename = strdup(LDATA_DEF);
|
LDATA_FILENAME, VMAJOR, VMINOR, VREV);
|
||||||
else {
|
}
|
||||||
files = lfile_readDir(&nfiles, ".");
|
/* Check default ldata. */
|
||||||
len = strlen(LDATA_FILENAME);
|
else if(lfile_fileExists(LDATA_DEF))
|
||||||
for(i = 0; i < nfiles; i++) {
|
ldata_filename = strdup(LDATA_DEF);
|
||||||
if(strncmp(files[i], LDATA_FILENAME, len)==0) {
|
/* Try to open any ldata in path. */
|
||||||
/* Must be packed. */
|
else {
|
||||||
if(pack_check(files[i]))
|
files = lfile_readDir(&nfiles, ".");
|
||||||
continue;
|
len = strlen(LDATA_FILENAME);
|
||||||
|
for(i = 0; i < nfiles; i++) {
|
||||||
ldata_filename = strdup(files[i]);
|
if(strncmp(files[i], LDATA_FILENAME, len)==0) {
|
||||||
break;
|
/* Must be a packfile. */
|
||||||
}
|
if(pack_check(files[i]))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
ldata_filename = strdup(files[i]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Clean up. */
|
||||||
|
for(i = 0; i < nfiles; i++)
|
||||||
|
free(files[i]);
|
||||||
|
free(files);
|
||||||
}
|
}
|
||||||
for(i = 0; i < nfiles; i++)
|
|
||||||
free(files[i]);
|
|
||||||
free(files);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Open the cache. */
|
||||||
|
ldata_cache = pack_openCache(ldata_filename);
|
||||||
|
if(ldata_cache == NULL)
|
||||||
|
WARN("Unalbe to create Packcache from '%s'.", ldata_filename);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,8 +111,6 @@ int ldata_open(void) {
|
|||||||
* @brief Close and clean up the ldata file.
|
* @brief Close and clean up the ldata file.
|
||||||
*/
|
*/
|
||||||
void ldata_close(void) {
|
void ldata_close(void) {
|
||||||
unsigned int i;
|
|
||||||
|
|
||||||
/* Destroy the name. */
|
/* Destroy the name. */
|
||||||
if(ldata_packName != NULL) {
|
if(ldata_packName != NULL) {
|
||||||
free(ldata_packName);
|
free(ldata_packName);
|
||||||
@ -108,26 +119,23 @@ void ldata_close(void) {
|
|||||||
|
|
||||||
/* Destroy the list. */
|
/* Destroy the list. */
|
||||||
if(ldata_fileList != NULL) {
|
if(ldata_fileList != NULL) {
|
||||||
for(i = 0; i < ldata_fileNList; i++)
|
/* No need to free memory since cache does that. */
|
||||||
free(ldata_fileList[i]);
|
|
||||||
|
|
||||||
free(ldata_fileList);
|
|
||||||
ldata_fileList = NULL;
|
ldata_fileList = NULL;
|
||||||
ldata_fileNList = 0;
|
ldata_fileNList = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Close the packfile. */
|
/* Close the packfile. */
|
||||||
/*
|
if(ldata_cache) {
|
||||||
pack_close(ldata_pack);
|
pack_closeCache(ldata_cache);
|
||||||
ldata_pack = NULL;
|
ldata_cache = NULL;
|
||||||
*/
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the ldata's name.
|
* @brief Get the ldata's name.
|
||||||
* @return The ldata's name.
|
* @return The ldata's name.
|
||||||
*/
|
*/
|
||||||
char* ldata_name(void) {
|
const char* ldata_name(void) {
|
||||||
char* buf;
|
char* buf;
|
||||||
uint32_t size;
|
uint32_t size;
|
||||||
xmlNodePtr node;
|
xmlNodePtr node;
|
||||||
@ -172,15 +180,16 @@ char* ldata_name(void) {
|
|||||||
* @brief Read a file from the ldata.
|
* @brief Read a file from the ldata.
|
||||||
*/
|
*/
|
||||||
void* ldata_read(const char* filename, uint32_t* filesize) {
|
void* ldata_read(const char* filename, uint32_t* filesize) {
|
||||||
if(ldata_filename == NULL)
|
if(ldata_cache == NULL)
|
||||||
ldata_openPackfile();
|
ldata_openPackfile();
|
||||||
return pack_readfile(ldata_filename, filename, filesize);
|
|
||||||
|
return pack_readfileCached(ldata_cache, filename, filesize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get the list of files in the ldata.
|
* @brief Get the list of files in the ldata.
|
||||||
*/
|
*/
|
||||||
char** ldata_list(const char* path, uint32_t* nfiles) {
|
const char** ldata_list(const char* path, uint32_t* nfiles) {
|
||||||
(void)path;
|
(void)path;
|
||||||
|
|
||||||
if(ldata_fileList != NULL) {
|
if(ldata_fileList != NULL) {
|
||||||
@ -188,10 +197,10 @@ char** ldata_list(const char* path, uint32_t* nfiles) {
|
|||||||
return ldata_fileList;
|
return ldata_fileList;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ldata_filename == NULL)
|
if(ldata_cache == NULL)
|
||||||
ldata_openPackfile();
|
ldata_openPackfile();
|
||||||
|
|
||||||
ldata_fileList = pack_listfiles(ldata_filename, &ldata_fileNList);
|
ldata_fileList = pack_listfilesCached(ldata_cache, &ldata_fileNList);
|
||||||
*nfiles = ldata_fileNList;
|
*nfiles = ldata_fileNList;
|
||||||
return ldata_fileList;
|
return ldata_fileList;
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,9 @@ void ldata_close(void);
|
|||||||
/* General. */
|
/* General. */
|
||||||
int ldata_check(char* path);
|
int ldata_check(char* path);
|
||||||
int ldata_setPath(char* path);
|
int ldata_setPath(char* path);
|
||||||
char* ldata_name(void);
|
const char* ldata_name(void);
|
||||||
|
|
||||||
/* Individual file functions. */
|
/* Individual file functions. */
|
||||||
void* ldata_read(const char* filename, uint32_t* filesize);
|
void* ldata_read(const char* filename, uint32_t* filesize);
|
||||||
char** ldata_list(const char* path, uint32_t* nfiles);
|
const char** ldata_list(const char* path, uint32_t* nfiles);
|
||||||
|
|
||||||
|
@ -285,8 +285,8 @@ int main(int argc, char** argv) {
|
|||||||
void loadscreen_load(void) {
|
void loadscreen_load(void) {
|
||||||
int i;
|
int i;
|
||||||
char file_path[PATH_MAX];
|
char file_path[PATH_MAX];
|
||||||
char** loadscreens;
|
const char** loadscreens;
|
||||||
char** files;
|
const char** files;
|
||||||
uint32_t nfiles;
|
uint32_t nfiles;
|
||||||
size_t len;
|
size_t len;
|
||||||
int nload;
|
int nload;
|
||||||
|
@ -164,7 +164,7 @@ static void music_free(void) {
|
|||||||
* @return 0 on success.
|
* @return 0 on success.
|
||||||
*/
|
*/
|
||||||
static int music_find(void) {
|
static int music_find(void) {
|
||||||
char** files;
|
const char** files;
|
||||||
uint32_t nfiles, i;
|
uint32_t nfiles, i;
|
||||||
char tmp[64];
|
char tmp[64];
|
||||||
int len;
|
int len;
|
||||||
|
80
src/pack.c
80
src/pack.c
@ -144,7 +144,6 @@ Packcache_t* pack_openCache(const char* packfile) {
|
|||||||
cache->index[i] = strdup(buf);
|
cache->index[i] = strdup(buf);
|
||||||
READ(cache, &cache->start[i], 4);
|
READ(cache, &cache->start[i], 4);
|
||||||
DEBUG("'%s' found at %d", filename, cache->start[i]);
|
DEBUG("'%s' found at %d", filename, cache->start[i]);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
/* Return the built cache. */
|
/* Return the built cache. */
|
||||||
return cache;
|
return cache;
|
||||||
@ -220,7 +219,7 @@ Packfile_t* pack_openFromCache(Packcache_t* cache, const char* filename) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -599,32 +598,14 @@ long pack_tell(Packfile_t* file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesize)
|
* @brief Read a file from a packfile.
|
||||||
*
|
|
||||||
* @brief Read an entire file into memory.
|
|
||||||
* @param packfile Name of the packfile to read from.
|
|
||||||
* @param filename Name of the packed file to read.
|
|
||||||
* @param filesize Is set to the size of the 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) {
|
static void* pack_readfilePack(Packfile_t* file,
|
||||||
Packfile_t* file;
|
const char* filename, uint32_t* filesize) {
|
||||||
void* buf;
|
void* buf;
|
||||||
char* str;
|
char* str;
|
||||||
int size, bytes;
|
int size, bytes;
|
||||||
|
|
||||||
/* Initialize size to 0. */
|
|
||||||
if(filesize)
|
|
||||||
*filesize = 0;
|
|
||||||
|
|
||||||
/* 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);
|
|
||||||
|
|
||||||
/* Read the entire file. */
|
/* Read the entire file. */
|
||||||
size = file->end - file->start;
|
size = file->end - file->start;
|
||||||
buf = malloc(size+1);
|
buf = malloc(size+1);
|
||||||
@ -634,8 +615,8 @@ void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesi
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if((bytes = pack_read(file, buf, size)) != size) {
|
if((bytes = pack_read(file, buf, size)) != size) {
|
||||||
ERR("Reading '%s' from packfile '%s'. Expected %d bytes got %d bytes",
|
ERR("Reading '%s' from packfile. Expected %d bytes got %d bytes",
|
||||||
filename, packfile, size, bytes);
|
filename, size, bytes);
|
||||||
free(buf);
|
free(buf);
|
||||||
free(file);
|
free(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -675,6 +656,32 @@ void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesi
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Read an entire file into memory.
|
||||||
|
* @param packfile Name of the packfile to read from.
|
||||||
|
* @param filename Name of the packed file to read.
|
||||||
|
* @param filesize Is set to the size of the 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_t* file;
|
||||||
|
|
||||||
|
/* Initialize size to 0. */
|
||||||
|
if(filesize)
|
||||||
|
*filesize = 0;
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
|
||||||
|
return pack_readfilePack(file, filename, filesize);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn char** pack_listfiles(const char* packfile, uint32_t* nfiles)
|
* @fn char** pack_listfiles(const char* packfile, uint32_t* nfiles)
|
||||||
*
|
*
|
||||||
@ -732,6 +739,29 @@ char** pack_listfiles(const char* packfile, uint32_t* nfiles) {
|
|||||||
return filenames;
|
return filenames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Read an entire file from the cache.
|
||||||
|
*/
|
||||||
|
void* pack_readfileCached(Packcache_t* cache, const char* filename, uint32_t* filesize) {
|
||||||
|
Packfile_t* file;
|
||||||
|
|
||||||
|
file = pack_openFromCache(cache, filename);
|
||||||
|
if(file == NULL)
|
||||||
|
ERR("Unable to create packfile from packcache.");
|
||||||
|
return pack_readfilePack(file, filename, filesize);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the list of files in a packcache.
|
||||||
|
* @param cache Cache to get list of files from.
|
||||||
|
* @param nfiles Number of files in the list.
|
||||||
|
* @return A read only list of the files from the pack cache.
|
||||||
|
*/
|
||||||
|
const char** pack_listfilesCached(Packcache_t* cache, uint32_t* nfiles) {
|
||||||
|
*nfiles = cache->nindex;
|
||||||
|
return (const char**)cache->index;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Closes a packfile.
|
* @brief Closes a packfile.
|
||||||
* @param file Packfile to close.
|
* @param file Packfile to close.
|
||||||
|
@ -33,4 +33,6 @@ int pack_close(Packfile_t* file);
|
|||||||
/* Fancy stuff. */
|
/* Fancy stuff. */
|
||||||
void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesize);
|
void* pack_readfile(const char* packfile, const char* filename, uint32_t* filesize);
|
||||||
char** pack_listfiles(const char* packfile, uint32_t* nfiles);
|
char** pack_listfiles(const char* packfile, uint32_t* nfiles);
|
||||||
|
void* pack_readfileCached(Packcache_t* cache, const char* filename, uint32_t* filesize);
|
||||||
|
const char** pack_listfilesCached(Packcache_t* cache, uint32_t* nfiles);
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ static alVoice* voice_pool = NULL; /**< Pool of free voices. */
|
|||||||
/* General prototypes. */
|
/* General prototypes. */
|
||||||
static void print_MixerVersion(void);
|
static void print_MixerVersion(void);
|
||||||
static int sound_makeList(void);
|
static int sound_makeList(void);
|
||||||
static Mix_Chunk* sound_load(char* filename);
|
static Mix_Chunk* sound_load(const char* filename);
|
||||||
static void sound_free(alSound* snd);
|
static void sound_free(alSound* snd);
|
||||||
/* Voices. */
|
/* Voices. */
|
||||||
static void voice_markStopped(int channel);
|
static void voice_markStopped(int channel);
|
||||||
@ -425,7 +425,7 @@ int sound_updateListener(double dir, double x, double y) {
|
|||||||
* @brief Make the list of available sounds.
|
* @brief Make the list of available sounds.
|
||||||
*/
|
*/
|
||||||
static int sound_makeList(void) {
|
static int sound_makeList(void) {
|
||||||
char** files;
|
const char** files;
|
||||||
uint32_t nfiles, i;
|
uint32_t nfiles, i;
|
||||||
char tmp[64];
|
char tmp[64];
|
||||||
int len;
|
int len;
|
||||||
@ -489,7 +489,7 @@ int sound_volume(const double vol) {
|
|||||||
*
|
*
|
||||||
* @sa sound_makeList
|
* @sa sound_makeList
|
||||||
*/
|
*/
|
||||||
static Mix_Chunk* sound_load(char* filename) {
|
static Mix_Chunk* sound_load(const char* filename) {
|
||||||
void* wavdata;
|
void* wavdata;
|
||||||
unsigned int size;
|
unsigned int size;
|
||||||
SDL_RWops* rw;
|
SDL_RWops* rw;
|
||||||
|
Loading…
Reference in New Issue
Block a user