From e1d908ae3e8cafcc732fd251ddc623f9823afbb2 Mon Sep 17 00:00:00 2001 From: kts Date: Fri, 27 Feb 2015 19:49:24 -0800 Subject: [PATCH] Added basic AssetManager class skeleton. This will be the governing class for the loading of files and ensuring data remains the same between clients via checksum comparisons. --- .../Roll them Bones.xcodeproj/project.pbxproj | 12 ++++++ src/Asset.cpp | 14 +++++++ src/Asset.hpp | 23 +++++++++++ src/AssetManager.cpp | 29 ++++++++++++++ src/AssetManager.hpp | 40 +++++++++++++++++++ src/Core.cpp | 9 +++++ src/Core.hpp | 3 ++ src/Log.hpp | 4 +- src/checksum.cpp | 17 ++++---- 9 files changed, 140 insertions(+), 11 deletions(-) create mode 100644 src/Asset.cpp create mode 100644 src/Asset.hpp create mode 100644 src/AssetManager.cpp create mode 100644 src/AssetManager.hpp diff --git a/build/osx/Roll them Bones.xcodeproj/project.pbxproj b/build/osx/Roll them Bones.xcodeproj/project.pbxproj index 156baec..b52f94c 100644 --- a/build/osx/Roll them Bones.xcodeproj/project.pbxproj +++ b/build/osx/Roll them Bones.xcodeproj/project.pbxproj @@ -14,6 +14,8 @@ 20517DCE1A9D44DB00DE49E9 /* Core.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 20517DCC1A9D44DB00DE49E9 /* Core.cpp */; }; 205182A81A9DD62C00DE49E9 /* cube.obj in Resources */ = {isa = PBXBuildFile; fileRef = 205182A71A9DD62C00DE49E9 /* cube.obj */; }; 205182C01A9DD63E00DE49E9 /* cube.obj in Copy Models */ = {isa = PBXBuildFile; fileRef = 205182A71A9DD62C00DE49E9 /* cube.obj */; }; + 205183C71A9F324700DE49E9 /* AssetManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 205183C51A9F324600DE49E9 /* AssetManager.cpp */; }; + 205183CC1A9F330000DE49E9 /* Asset.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 205183CA1A9F330000DE49E9 /* Asset.cpp */; }; 2056AE3C1A8A421500833760 /* Mat4.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2056AE361A8A421500833760 /* Mat4.cpp */; }; 2056AE3D1A8A421500833760 /* RenderObject.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2056AE381A8A421500833760 /* RenderObject.cpp */; }; 2056AE3E1A8A421500833760 /* Vec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2056AE3A1A8A421500833760 /* Vec.cpp */; }; @@ -79,6 +81,10 @@ 20517DCC1A9D44DB00DE49E9 /* Core.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Core.cpp; path = ../../src/Core.cpp; sourceTree = SOURCE_ROOT; }; 20517DCD1A9D44DB00DE49E9 /* Core.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Core.hpp; path = ../../src/Core.hpp; sourceTree = SOURCE_ROOT; }; 205182A71A9DD62C00DE49E9 /* cube.obj */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = cube.obj; path = models/cube.obj; sourceTree = ""; }; + 205183C51A9F324600DE49E9 /* AssetManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = AssetManager.cpp; path = ../../src/AssetManager.cpp; sourceTree = SOURCE_ROOT; }; + 205183C61A9F324600DE49E9 /* AssetManager.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = AssetManager.hpp; path = ../../src/AssetManager.hpp; sourceTree = SOURCE_ROOT; }; + 205183CA1A9F330000DE49E9 /* Asset.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Asset.cpp; path = ../../src/Asset.cpp; sourceTree = SOURCE_ROOT; }; + 205183CB1A9F330000DE49E9 /* Asset.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Asset.hpp; path = ../../src/Asset.hpp; sourceTree = SOURCE_ROOT; }; 2056AE361A8A421500833760 /* Mat4.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Mat4.cpp; path = ../../src/Mat4.cpp; sourceTree = SOURCE_ROOT; }; 2056AE371A8A421500833760 /* Mat4.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = Mat4.hpp; path = ../../src/Mat4.hpp; sourceTree = SOURCE_ROOT; }; 2056AE381A8A421500833760 /* RenderObject.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RenderObject.cpp; path = ../../src/RenderObject.cpp; sourceTree = SOURCE_ROOT; }; @@ -129,6 +135,10 @@ 080E96DDFE201D6D7F000001 /* Classes */ = { isa = PBXGroup; children = ( + 205183CA1A9F330000DE49E9 /* Asset.cpp */, + 205183CB1A9F330000DE49E9 /* Asset.hpp */, + 205183C51A9F324600DE49E9 /* AssetManager.cpp */, + 205183C61A9F324600DE49E9 /* AssetManager.hpp */, 20517DCC1A9D44DB00DE49E9 /* Core.cpp */, 20517DCD1A9D44DB00DE49E9 /* Core.hpp */, 2057239F1A9B3F04001400FA /* Quat.cpp */, @@ -358,6 +368,8 @@ 20517D531A9BFB1B00DE49E9 /* fio.cpp in Sources */, 20517D601A9C083B00DE49E9 /* checksum.cpp in Sources */, 20517DCE1A9D44DB00DE49E9 /* Core.cpp in Sources */, + 205183C71A9F324700DE49E9 /* AssetManager.cpp in Sources */, + 205183CC1A9F330000DE49E9 /* Asset.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/Asset.cpp b/src/Asset.cpp new file mode 100644 index 0000000..e15fc00 --- /dev/null +++ b/src/Asset.cpp @@ -0,0 +1,14 @@ +/* =============================================================== +Asset +================================================================ */ +#include "Asset.hpp" +Asset::Asset() { + flags = 0; + data = NULL; + data_length = 0; +} +Asset::~Asset() { + if (flags & IS_LOADED) { + free(data); + } +} diff --git a/src/Asset.hpp b/src/Asset.hpp new file mode 100644 index 0000000..0f46104 --- /dev/null +++ b/src/Asset.hpp @@ -0,0 +1,23 @@ +/* ================================================================ +Asset +---------------- +Our basic Asset class. This class exists as a container for data, but does not directly load any data itself. It is responsible for holding data assigned by the Asset Manager and for clearing data upon destruction. +================================================================ */ +#include + +class Asset { + friend class AssetManager; + public: + Asset(); + ~Asset(); + enum FLAGS { + IS_LOADED = (1 << 1) // indicates data is populated + }; + protected: + unsigned int flags; // Our asset state flags + std::string uuid; // Our UUID + std::string filename; // Our filename, relative to the assets directory + std::string data_checksum; // Our full data checksum + char *data; // Our data, NULL if IS_LOADED is not set + size_t data_length; // Our data's length, in bytes +}; diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp new file mode 100644 index 0000000..7bb2119 --- /dev/null +++ b/src/AssetManager.cpp @@ -0,0 +1,29 @@ +/* =============================================================== +AssetManager +================================================================ */ +#include "AssetManager.hpp" +#include "Log.hpp" +#include "fio.hpp" +#include "checksum.hpp" + +AssetManager::AssetManager() { + +} +AssetManager::~AssetManager() { + +} +/* ======== Asset Loading ======== */ +Asset* AssetManager::loadFile(const char *filename) { + LOG(LOG_INFO) << FUNC_NAME << " " << filename << ": " << std::hex << crc32(1337, filename, strlen(filename)); + // read in the file and get its checksum + char *buffer = NULL; + size_t len = asset_fileToMem(filename, &buffer); + if (len == 0) { + LOG(LOG_ERROR) << FUNC_NAME << " read length was 0, could not load file " << filename; + return NULL; + } + LOG(LOG_INFO) << FUNC_NAME << " " << filename << "'s CRC32 is " << std::hex << crc32(1337, buffer, len); + LOG(LOG_INFO) << filename << ": " << std::hex << crc32(1337, filename, strlen(filename)) << " " << std::hex << crc32(1337, buffer, len) << " " << std::hex << crc32(1337, buffer, len/2) << " " << std::hex << crc32(1337, buffer+(len/2), len/2); + free(buffer); + return NULL; +} diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp new file mode 100644 index 0000000..a779def --- /dev/null +++ b/src/AssetManager.hpp @@ -0,0 +1,40 @@ +/* ================================================================ +Asset Manager +---------------- +This class is responsible for the loading of all files considered as assets. + +An Asset is an object that holds some amount of unique data that is normally read from a file in one of the asset directories. Each Asset has a file checksum, a special UUID, the pathname of the file it references, and the data for said file. + +The AssetManager holds a special Files Cache. This cache is a basic list of unloaded Asset files, containing all but their data. It is constructed from all files in the assets directories and is updated when a new file has been added to the cache. + +Files may be added to the Cache through the following manners: + 1. Adding to an assets folder and updating the cache + 2. Loading a specific file with addFile + * This will add the file to the preferred user assets folder + 3. Calling addData(const char *filename, const char *bytes, size_t len) + * This will create the given filename in the preferred user assets folder and write bytes up to len to the file. + +Beyond the File Cache, there is the Live Cache. This cache is the current list of loaded Assets. These Live Caches are generally loaded from and saved to a Campaign. + +================================================================ */ +#include "Asset.hpp" +#include "fio.hpp" +#include + +class AssetManager { + public: + AssetManager(); + ~AssetManager(); + // loads given file as an asset + Asset* loadFile(const char *filename); + // attempts to find asset with the given UUID in the asset cache + // Asset* loadAsset(const char *uuid); + // saves the loaded assets in the asset cache + // saves all Assets in assets to the given cache file + // int saveCache(const char *cachefile); + // + // int loadCache(const char *cachefile) + private: + std::vector cache; // Cache of all files known + // Table live_cache; // cache of loaded assets +}; diff --git a/src/Core.cpp b/src/Core.cpp index b78d8bf..4594ea3 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -16,6 +16,8 @@ Core::Core() { v_window = NULL; v_context = 0; v_fbo = 0; + scene = NULL; + asset_manager = NULL; } Core::~Core() { @@ -75,6 +77,8 @@ int Core::initSystem() { // FIXME: temporary location for adding cameras/etc. for testing // Create our basic RenderScene scene = new RenderScene(); + // TEMP: this should be assigned elsewhere. + asset_manager = new AssetManager(); // TEMP: our framebuffer rendering program Program *program = new Program(); // FIXME: check for GLSL version and automagically load the appropriate shader file @@ -149,6 +153,8 @@ int Core::initSystem() { RenderSet *sert = new RenderSet(); sert->setProgram(program_model); + asset_manager->loadFile("data/models/cube.obj"); + RenderObject *objerct = new RenderObject(); Mesh *mersh = new Mesh("data/models/cube.obj"); mersh->buildMesh(); @@ -298,3 +304,6 @@ void Core::doRender() { SDL_Window* Core::getWindow() { return v_window; } +AssetManager* Core::getAssetManager() { + return asset_manager; +} diff --git a/src/Core.hpp b/src/Core.hpp index fe2e099..4419787 100644 --- a/src/Core.hpp +++ b/src/Core.hpp @@ -7,6 +7,7 @@ Core is somewhat like a service locator, but with larger engine capabilities - i #define CORE_HPP #include "common.hpp" #include "RenderScene.hpp" +#include "AssetManager.hpp" class Core { public: Core(); @@ -19,6 +20,7 @@ class Core { // SDL_Window* getWindow(); RenderScene *getScene(); + AssetManager *getAssetManager(); // Audio* getAudio(); // Net* getNet(); unsigned int flags; // Core state flags @@ -30,6 +32,7 @@ class Core { int v_height; // height of our display int v_flags; // video flags RenderScene *scene; // our current render scene + AssetManager *asset_manager; // Audio *audio_service; // Net *net_service; }; diff --git a/src/Log.hpp b/src/Log.hpp index dccb295..76cd72a 100644 --- a/src/Log.hpp +++ b/src/Log.hpp @@ -37,8 +37,8 @@ class Log { }; #ifndef MAX_LOG_LEVEL -#define MAX_LOG_LEVEL LOG_ERROR +#define MAX_LOG_LEVEL LOG_DEBUG #endif #define LOG(level) if (level > MAX_LOG_LEVEL) ; else Log().Get(level) -#endif \ No newline at end of file +#endif diff --git a/src/checksum.cpp b/src/checksum.cpp index e2bcc45..8795321 100644 --- a/src/checksum.cpp +++ b/src/checksum.cpp @@ -9,27 +9,26 @@ checksum.cpp/checksum.hpp provide functionality for checking and generating chec uint32_t crc32(uint32_t crc, const char *buf, size_t len) { static uint32_t table[256]; static int table_gen = 0; - uint32_t rem; + uint32_t word; uint8_t octet; int i, j; const char *p, *q; - + // Generate our table if not yet generated if (table_gen == 0) { for (i = 0; i < 256; i++) { - rem = i; + word = i; for (j = 0; j < 8; j++) { - if (rem & 1) { - rem >>= 1; - rem ^= 0xedb88320; + if (word & 1) { + word = (word >> 1) ^ 0xedb88320; } else { - rem >>= 1; + word >>= 1; } } - table[i] = rem; + table[i] = word; } table_gen = 1; } - + // Generate our CRC32! crc = ~crc; q = buf + len; for (p = buf; p < q; p++) {