AssetCache works fine on Windows now. Need to reorganize AssetCache's _WIN32 preprocessor conditionals - it's pretty ugly.
parent
a531be218c
commit
b1efc3ef74
|
@ -3,6 +3,7 @@
|
|||
#include "checksum.hpp"
|
||||
#include "fio.hpp"
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#if _WIN32
|
||||
#include <Windows.h>
|
||||
|
@ -34,8 +35,12 @@ AssetCache::~AssetCache() {
|
|||
/* ======== Cache loading ======== */
|
||||
int AssetCache::fromFile(const char *cache_file) {
|
||||
int ret;
|
||||
#ifdef _WIN32
|
||||
FILE *file;
|
||||
fopen_s(&file, cache_file, "rb");
|
||||
#else
|
||||
FILE *file = fopen(cache_file, "rb");
|
||||
//FILE *file = asset_fopen(cache_file, "rb");
|
||||
#endif
|
||||
if (file == NULL) {
|
||||
LOG(LOG_WARNING) << "Could not open cache file: " << cache_file;
|
||||
return 1;
|
||||
|
@ -44,13 +49,17 @@ int AssetCache::fromFile(const char *cache_file) {
|
|||
uint32_t fsum = 0;
|
||||
off_t fsize = 0;
|
||||
#ifdef _WIN32
|
||||
while ((ret = fscanf_s(file, "%s %u %jd\n", fpath, 1024, &fsum, (intmax_t*)&fsize)) != EOF)
|
||||
while ((ret = fscanf_s(file, "%s %u %d\n", fpath, 1024, &fsum, (intmax_t*)&fsize)) != EOF)
|
||||
#else
|
||||
while ((ret = fscanf(file, "%s %u %jd\n", fpath, &fsum, (intmax_t*)&fsize)) != EOF)
|
||||
#endif
|
||||
{
|
||||
char fullpath[1024];
|
||||
#ifdef _WIN32
|
||||
sprintf_s(fullpath, 1024, "%s/%s", directory.c_str(), fpath);
|
||||
#else
|
||||
snprintf(fullpath, 1024, "%s/%s", directory.c_str(), fpath);
|
||||
#endif
|
||||
// only add the asset if it exists.
|
||||
#ifdef _WIN32
|
||||
DWORD file_attrib = GetFileAttributes(fullpath);
|
||||
|
@ -70,6 +79,7 @@ int AssetCache::fromFile(const char *cache_file) {
|
|||
LOG(LOG_DEBUG) << "opened " << fpath << " " << fsum << " " << fsize;
|
||||
}
|
||||
}
|
||||
fclose(file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -104,14 +114,18 @@ int AssetCache::fromDir(const char *dir) {
|
|||
return 0;
|
||||
}
|
||||
int AssetCache::traverseDir_r(const char *dir) {
|
||||
char full_dir[1024]; // "full" dir path, e.g, "<appdata>/data/models"
|
||||
snprintf(full_dir, 1024, "%s/%s", directory.c_str(), dir);
|
||||
#if _WIN32
|
||||
WIN32_FIND_DATA ffd;
|
||||
HANDLE h_find = INVALID_HANDLE_VALUE;
|
||||
char wdir[1024];
|
||||
|
||||
StringCbCopyN(wdir, 1024, dir, dir_len);
|
||||
size_t dir_len = strlen(dir)+1;
|
||||
if (directory.size() > 0) {
|
||||
StringCbCopyN(wdir, 1024, directory.c_str(), directory.size());
|
||||
StringCbCatN(wdir, 1024, "/", 1);
|
||||
} else {
|
||||
StringCbCopyN(wdir, 1024, "", 0);
|
||||
}
|
||||
StringCbCatN(wdir, 1024, dir, dir_len);
|
||||
StringCbCatN(wdir, 1024, "/*", 2);
|
||||
|
||||
if ((h_find = FindFirstFile(wdir, &ffd)) == INVALID_HANDLE_VALUE) {
|
||||
|
@ -120,33 +134,37 @@ int AssetCache::traverseDir_r(const char *dir) {
|
|||
}
|
||||
do {
|
||||
if (strcmp(ffd.cFileName, "..") == 0 || strcmp(ffd.cFileName, ".") == 0 || strcmp(ffd.cFileName, ".CACHE") == 0) continue;
|
||||
char fpath[1024];
|
||||
StringCbCopyN(fpath, 1024, dir, dir_len);
|
||||
StringCbCatN(fpath, 1024, "/", 2);
|
||||
StringCbCatN(fpath, 1024, ffd.cFileName, strlen(ffd.cFileName)+1);
|
||||
char lpath[1024];
|
||||
if (strlen(dir) > 0) { // if directory is not fully relative
|
||||
sprintf_s(lpath, 1024, "%s/%s", dir, ffd.cFileName);
|
||||
} else {
|
||||
sprintf_s(lpath, 1024, "%s", ffd.cFileName);
|
||||
}
|
||||
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
traverseDir_r(fpath);
|
||||
traverseDir_r(lpath);
|
||||
} else {
|
||||
Asset *asset = NULL;
|
||||
if (assets->exists(fpath)) {
|
||||
asset = assets->get(fpath);
|
||||
if (assets->exists(lpath)) {
|
||||
asset = assets->get(lpath);
|
||||
if (ffd.nFileSizeLow != asset->data_length) {
|
||||
LOG(LOG_DEBUG) << " file " << fpath << " changed from " << asset->data_length << " to " << ffd.nFileSizeLow;
|
||||
LOG(LOG_DEBUG) << " file " << lpath << " changed from " << asset->data_length << " to " << ffd.nFileSizeLow;
|
||||
asset->data_length = ffd.nFileSizeLow;
|
||||
// TODO: recalculate checksum?
|
||||
}
|
||||
} else {
|
||||
asset = new Asset();
|
||||
asset->filename.assign(fpath);
|
||||
asset->filename.assign(lpath);
|
||||
asset->flags &= ~Asset::IS_NULL;
|
||||
// FIXME: this is not the right way to do this. We also should use nFileSizeHigh. Also, this does not match the file's actual bytes
|
||||
asset->data_length = ffd.nFileSizeLow;
|
||||
assets->set(fpath, asset);
|
||||
assets->set(lpath, asset);
|
||||
}
|
||||
}
|
||||
} while (FindNextFile(h_find, &ffd));
|
||||
FindClose(h_find);
|
||||
#else
|
||||
char full_dir[1024]; // "full" dir path, e.g, "<appdata>/data/models"
|
||||
snprintf(full_dir, 1024, "%s/%s", directory.c_str(), dir);
|
||||
struct dirent **files;
|
||||
struct stat file_stat;
|
||||
int n, i;
|
||||
|
@ -211,7 +229,12 @@ This function saves the AssetCache to the given asset cache file
|
|||
*/
|
||||
int AssetCache::toFile(const char *cache_file) {
|
||||
//FILE *file = asset_fopen(cache_file, "wb");
|
||||
#ifdef _WIN32
|
||||
FILE *file;
|
||||
fopen_s(&file, cache_file, "wb");
|
||||
#else
|
||||
FILE *file = fopen(cache_file, "wb");
|
||||
#endif
|
||||
if (file == NULL) {
|
||||
LOG(LOG_WARNING) << FUNC_NAME << ": Could not open cache file: " << cache_file << " " << errno;
|
||||
return 1;
|
||||
|
@ -219,7 +242,11 @@ int AssetCache::toFile(const char *cache_file) {
|
|||
HashEntry<Asset*> *entry;
|
||||
while ((entry = assets->iterate()) != NULL) {
|
||||
Asset *asset = (Asset*)(entry->data);
|
||||
#ifdef _WIN32
|
||||
fprintf(file, "%s %u %d\n", asset->filename.c_str(), asset->data_checksum, (intmax_t)asset->data_length);
|
||||
#else
|
||||
fprintf(file, "%s %u %jd\n", asset->filename.c_str(), asset->data_checksum, (intmax_t)asset->data_length);
|
||||
#endif
|
||||
LOG(LOG_DEBUG) << ": saving " << asset->filename.c_str() << " bytes " << asset->data_length;
|
||||
}
|
||||
fclose(file);
|
||||
|
@ -255,7 +282,11 @@ Asset* AssetCache::loadAsset(const char *filename) {
|
|||
if (asset == NULL) return NULL;
|
||||
if (!(asset->flags & Asset::IS_LOADED)) {
|
||||
char fpath[1024];
|
||||
#ifdef _WIN32
|
||||
sprintf_s(fpath, 1024, "%s/%s", directory.c_str(), asset->filename.c_str());
|
||||
#else
|
||||
snprintf(fpath, 1024, "%s/%s", directory.c_str(), asset->filename.c_str());
|
||||
#endif
|
||||
asset->data_length = fileToMem(fpath, &(asset->data));
|
||||
asset->flags |= Asset::IS_LOADED;
|
||||
if (asset->data_checksum == 0) {
|
||||
|
|
Loading…
Reference in New Issue