[Add] Using the pack cache now, initial loading is a lot faster.

This commit is contained in:
Allanis 2014-04-27 14:31:21 +01:00
parent ba26575890
commit 44db60f32b
8 changed files with 120 additions and 79 deletions

View File

@ -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;

View File

@ -23,11 +23,11 @@
/* 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. */
/** /**
@ -59,19 +59,24 @@ static int ldata_openPackfile(void) {
int nfiles; int nfiles;
size_t len; size_t len;
/* Try to find the ldata file. */
if(ldata_filename == NULL) {
/* Check ldata with version appended. */
if(lfile_fileExists("%s-%d.%d.%d", LDATA_FILENAME, VMAJOR, VMINOR, VREV)) { if(lfile_fileExists("%s-%d.%d.%d", LDATA_FILENAME, VMAJOR, VMINOR, VREV)) {
ldata_filename = malloc(PATH_MAX); ldata_filename = malloc(PATH_MAX);
snprintf(ldata_filename, PATH_MAX, "%s-%d.%d.%d", LDATA_FILENAME, VMAJOR, snprintf(ldata_filename, PATH_MAX, "%s-%d.%d.%d",
VMINOR, VREV); LDATA_FILENAME, VMAJOR, VMINOR, VREV);
} }
/* Check default ldata. */
else if(lfile_fileExists(LDATA_DEF)) else if(lfile_fileExists(LDATA_DEF))
ldata_filename = strdup(LDATA_DEF); ldata_filename = strdup(LDATA_DEF);
/* Try to open any ldata in path. */
else { else {
files = lfile_readDir(&nfiles, "."); files = lfile_readDir(&nfiles, ".");
len = strlen(LDATA_FILENAME); len = strlen(LDATA_FILENAME);
for(i = 0; i < nfiles; i++) { for(i = 0; i < nfiles; i++) {
if(strncmp(files[i], LDATA_FILENAME, len)==0) { if(strncmp(files[i], LDATA_FILENAME, len)==0) {
/* Must be packed. */ /* Must be a packfile. */
if(pack_check(files[i])) if(pack_check(files[i]))
continue; continue;
@ -79,10 +84,18 @@ static int ldata_openPackfile(void) {
break; break;
} }
} }
/* Clean up. */
for(i = 0; i < nfiles; i++) for(i = 0; i < nfiles; i++)
free(files[i]); free(files[i]);
free(files); 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;
} }

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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.

View File

@ -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);

View File

@ -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;