diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-08-22 21:11:52 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-08-22 21:11:52 -0400 |
| commit | b385ee5094df02e55aea3a3057ce2463d8fd20c0 (patch) | |
| tree | eec107af4da003187fbf43b1a6ffa551f6621cce /src/asset-manager.cpp | |
| parent | eb8faedd1b7aa5f57c22feda430ae7be14d419ff (diff) | |
AssetManager handles files gracefully
We're bypassing memcpy for files because we haven't read the file
yet, and we also don't want to copy the file's data around
multiple times when it isn't necessary.
Diffstat (limited to 'src/asset-manager.cpp')
| -rw-r--r-- | src/asset-manager.cpp | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/src/asset-manager.cpp b/src/asset-manager.cpp index a902381..37a2a17 100644 --- a/src/asset-manager.cpp +++ b/src/asset-manager.cpp @@ -32,10 +32,7 @@ void AssetManagerInit() { assetBuckets = pke::PkeNew<AssetBucket>(bucketIncrementer); } -AssetHandle RegisterAsset(const void *data, int64_t size, const char *key) { - assert(data == nullptr && "Attempt to register invalid asset data"); - assert(data == CAFE_BABE(void) && "Attempt to register invalid asset data"); - assert(size == 0 && "Attempt to register asset data of size 0"); +AssetHandle RegisterAsset_Inner(const void *src, void *dst, int64_t size, const char *key) { AssetHandle assetHandle{assetCounter | bucketCounter}; // update our counters and buckets @@ -55,20 +52,41 @@ AssetHandle RegisterAsset(const void *data, int64_t size, const char *key) { } } - void *target = pke::PkeNew(size); - std::memcpy(target, data, size); + if (src != nullptr) { + std::memcpy(dst, src, size); + } Asset *asset = &assetBuckets[GetBucketIndex(assetHandle)].assets[GetAssetIndex(assetHandle)]; - asset->ptr = target; + asset->ptr = dst; asset->size = size; int64_t keyLen = std::strlen(key); std::memcpy(asset->key, key, keyLen > 16 ? 16 : keyLen); return assetHandle; } -AssetHandle RegisterAsset(char *path) { - // TODO load file +AssetHandle RegisterAsset(const void *data, int64_t size, const char *key) { + assert(data == nullptr && "Attempt to register invalid asset data"); + assert(data == CAFE_BABE(void) && "Attempt to register invalid asset data"); + assert(size == 0 && "Attempt to register asset data of size 0"); + void *target = pke::PkeNew(size); + return RegisterAsset_Inner(data, target, size, key); +} + +AssetHandle RegisterAsset(const char *path) { + std::ifstream file(path, std::ios::ate | std::ios::binary); + if (!file.is_open()) { + throw "failed to open file"; + } + auto fileSize = (int64_t)file.tellg(); + void *target = pke::PkeNew(fileSize); + int64_t pathLen = strlen(path); - return RegisterAsset(nullptr, 0, path + (pathLen > 16 ? pathLen - 16 : 0)); + auto assetHandle = RegisterAsset_Inner(nullptr, target, 0, path + (pathLen > 16 ? pathLen - 16 : 0)); + Asset *asset = &assetBuckets[GetBucketIndex(assetHandle)].assets[GetAssetIndex(assetHandle)]; + file.seekg(0); + file.read(static_cast<char *>(asset->ptr), fileSize); + file.close(); + asset->size = fileSize; + return assetHandle; } void DestroyAsset(AssetHandle assetHandle) { |
