From cbbbe41cb0b988391edc5bca98acb61e817bbdfe Mon Sep 17 00:00:00 2001 From: Kearwood Kip Gilbert Date: Sun, 18 Aug 2019 17:57:41 -0700 Subject: [PATCH] Implemented KRSource and KRSourceManager. Implemented MoveToBundle api. --- kraken/CMakeLists.txt | 2 + kraken/KRBundle.cpp | 7 ++- kraken/KRBundle.h | 2 +- kraken/KRContext.cpp | 104 +++++++++++++++++++++++++---------- kraken/KRContext.h | 9 ++- kraken/KRMaterialManager.cpp | 4 +- kraken/KRMaterialManager.h | 2 +- kraken/KRResource+blend.cpp | 6 +- kraken/KRResource+obj.cpp | 9 +-- kraken/KRResource.cpp | 14 ++++- kraken/KRResource.h | 13 +++-- kraken/KRSource.cpp | 65 ++++++++++++++++++++++ kraken/KRSource.h | 59 ++++++++++++++++++++ kraken/KRSourceManager.cpp | 102 ++++++++++++++++++++++++++++++++++ kraken/KRSourceManager.h | 60 ++++++++++++++++++++ kraken/kraken.cpp | 7 ++- kraken/public/kraken.h | 1 + 17 files changed, 410 insertions(+), 56 deletions(-) create mode 100644 kraken/KRSource.cpp create mode 100644 kraken/KRSource.h create mode 100644 kraken/KRSourceManager.cpp create mode 100644 kraken/KRSourceManager.h diff --git a/kraken/CMakeLists.txt b/kraken/CMakeLists.txt index 78ba67f..1d008d7 100644 --- a/kraken/CMakeLists.txt +++ b/kraken/CMakeLists.txt @@ -67,6 +67,8 @@ add_sources(KRResource.cpp) add_sources(KRReverbZone.cpp) add_sources(KRScene.cpp) add_sources(KRSceneManager.cpp) +add_sources(KRSource.cpp) +add_sources(KRSourceManager.cpp) add_sources(KRShader.cpp) add_sources(KRShaderManager.cpp) add_sources(KRSpotLight.cpp) diff --git a/kraken/KRBundle.cpp b/kraken/KRBundle.cpp index 996a9d2..da996f6 100755 --- a/kraken/KRBundle.cpp +++ b/kraken/KRBundle.cpp @@ -114,7 +114,7 @@ bool KRBundle::save(KRDataBlock &data) { return true; } -void KRBundle::append(KRResource &resource) +KRDataBlock* KRBundle::append(KRResource &resource) { // Serialize resource to binary representation KRDataBlock resource_data; @@ -124,7 +124,7 @@ void KRBundle::append(KRResource &resource) // Padding is added at the end of file to align next header to a 512 byte boundary. Padding at the end of the archive includes an additional 1024 bytes -- two zero-ed out file headers that mark the end of the archive size_t padding_size = RoundUpSize(resource_data.getSize()) - resource_data.getSize() + KRENGINE_KRBUNDLE_HEADER_SIZE * 2; - + size_t resource_data_start = m_pData->getSize() + KRENGINE_KRBUNDLE_HEADER_SIZE; m_pData->expand(KRENGINE_KRBUNDLE_HEADER_SIZE + resource_data.getSize() + padding_size - KRENGINE_KRBUNDLE_HEADER_SIZE * 2); // We will overwrite the existing zero-ed out file headers that marked the end of the archive, so we don't have to include their size here m_pData->lock(); @@ -163,4 +163,7 @@ void KRBundle::append(KRResource &resource) sprintf(file_header->checksum, "%07o", check_sum); m_pData->unlock(); + + KRDataBlock *pFileData = m_pData->getSubBlock((int)resource_data_start, (int)resource_data.getSize()); + return pFileData; } diff --git a/kraken/KRBundle.h b/kraken/KRBundle.h index fbe4b3b..b854d22 100755 --- a/kraken/KRBundle.h +++ b/kraken/KRBundle.h @@ -44,7 +44,7 @@ public: virtual bool save(const std::string& path); virtual bool save(KRDataBlock &data); - void append(KRResource &resource); + KRDataBlock* append(KRResource &resource); private: KRDataBlock *m_pData; diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index 255584c..1fd38a0 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -83,6 +83,7 @@ KRContext::KRContext(const KrInitializeInfo* initializeInfo) m_pAnimationCurveManager = new KRAnimationCurveManager(*this); m_pSoundManager = new KRAudioManager(*this); m_pUnknownManager = new KRUnknownManager(*this); + m_pSourceManager = new KRSourceManager(*this); m_streamingEnabled = true; @@ -147,6 +148,11 @@ KRContext::~KRContext() { m_pSoundManager = NULL; } + if(m_pSourceManager) { + delete m_pSourceManager; + m_pSourceManager = NULL; + } + if(m_pUnknownManager) { delete m_pUnknownManager; m_pUnknownManager = NULL; @@ -218,13 +224,14 @@ KRAnimationCurveManager *KRContext::getAnimationCurveManager() { KRAudioManager *KRContext::getAudioManager() { return m_pSoundManager; } +KRSourceManager *KRContext::getSourceManager() { + return m_pSourceManager; +} KRUnknownManager *KRContext::getUnknownManager() { return m_pUnknownManager; } - std::vector KRContext::getResources() { - std::vector resources; for(unordered_map::iterator itr = m_pSceneManager->getScenes().begin(); itr != m_pSceneManager->getScenes().end(); itr++) { @@ -248,6 +255,13 @@ std::vector KRContext::getResources() for(unordered_map::iterator itr = m_pSoundManager->getSounds().begin(); itr != m_pSoundManager->getSounds().end(); itr++) { resources.push_back((*itr).second); } + + unordered_map > sources = m_pSourceManager->getSources(); + for(unordered_map >::iterator itr = sources.begin(); itr != sources.end(); itr++) { + for(unordered_map::iterator itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr2++) { + resources.push_back((*itr2).second); + } + } unordered_map > unknowns = m_pUnknownManager->getUnknowns(); for(unordered_map >::iterator itr = unknowns.begin(); itr != unknowns.end(); itr++) { @@ -256,69 +270,76 @@ std::vector KRContext::getResources() } } - // FINDME, TODO - Not yet exporting shaders, as they are currently only being used as standard Kraken assets. In the future people may want their custom shaders to be exported. - return resources; } -void KRContext::loadResource(const std::string &file_name, KRDataBlock *data) { +KRResource* KRContext::loadResource(const std::string &file_name, KRDataBlock *data) { std::string name = KRResource::GetFileBase(file_name); std::string extension = KRResource::GetFileExtension(file_name); + + KRResource *resource = nullptr; // fprintf(stderr, "KRContext::loadResource - Loading: %s\n", file_name.c_str()); if(extension.compare("krbundle") == 0) { - m_pBundleManager->loadBundle(name.c_str(), data); + resource = m_pBundleManager->loadBundle(name.c_str(), data); } else if(extension.compare("krmesh") == 0) { - m_pMeshManager->loadModel(name.c_str(), data); + resource = m_pMeshManager->loadModel(name.c_str(), data); } else if(extension.compare("krscene") == 0) { - m_pSceneManager->loadScene(name.c_str(), data); + resource = m_pSceneManager->loadScene(name.c_str(), data); } else if(extension.compare("kranimation") == 0) { - m_pAnimationManager->loadAnimation(name.c_str(), data); + resource = m_pAnimationManager->loadAnimation(name.c_str(), data); } else if(extension.compare("kranimationcurve") == 0) { - m_pAnimationCurveManager->loadAnimationCurve(name.c_str(), data); + resource = m_pAnimationCurveManager->loadAnimationCurve(name.c_str(), data); } else if(extension.compare("pvr") == 0) { - m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); + resource = m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); } else if(extension.compare("ktx") == 0) { - m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); + resource = m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); } else if(extension.compare("tga") == 0) { - m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); + resource = m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data); } else if(extension.compare("vsh") == 0) { - m_pShaderManager->loadVertexShader(name.c_str(), data); + resource = m_pSourceManager->load(name, extension, data); } else if(extension.compare("fsh") == 0) { - m_pShaderManager->loadFragmentShader(name.c_str(), data); + resource = m_pSourceManager->load(name, extension, data); } else if(extension.compare("mtl") == 0) { - m_pMaterialManager->load(name.c_str(), data); + resource = m_pMaterialManager->load(name.c_str(), data); } else if(extension.compare("mp3") == 0) { - m_pSoundManager->load(name.c_str(), extension, data); + resource = m_pSoundManager->load(name.c_str(), extension, data); } else if(extension.compare("wav") == 0) { - m_pSoundManager->load(name.c_str(), extension, data); + resource = m_pSoundManager->load(name.c_str(), extension, data); } else if(extension.compare("aac") == 0) { - m_pSoundManager->load(name.c_str(), extension, data); + resource = m_pSoundManager->load(name.c_str(), extension, data); } else if(extension.compare("obj") == 0) { - KRResource::LoadObj(*this, file_name); + resource = KRResource::LoadObj(*this, file_name); #if !TARGET_OS_IPHONE /* // FINDME, TODO, HACK! - Uncomment } else if(extension.compare("fbx") == 0) { - KRResource::LoadFbx(*this, file_name); + resource = KRResource::LoadFbx(*this, file_name); */ } else if(extension.compare("blend") == 0) { - KRResource::LoadBlenderScene(*this, file_name); + resource = KRResource::LoadBlenderScene(*this, file_name); #endif } else { - m_pUnknownManager->load(name, extension, data); + resource = m_pUnknownManager->load(name, extension, data); } + return resource; } -void KRContext::loadResource(std::string path) { - KRDataBlock *data = new KRDataBlock(); - if(data->load(path)) { - loadResource(path, data); - } else { - KRContext::Log(KRContext::LOG_LEVEL_ERROR, "KRContext::loadResource - Failed to open file: %s", path.c_str()); - delete data; +KrResult KRContext::loadResource(const KrLoadResourceInfo* loadResourceInfo) { + if (loadResourceInfo->resourceHandle < 0 || loadResourceInfo->resourceHandle >= m_resourceMapSize) { + return KR_ERROR_OUT_OF_BOUNDS; } + KRDataBlock *data = new KRDataBlock(); + if(!data->load(loadResourceInfo->pResourcePath)) { + KRContext::Log(KRContext::LOG_LEVEL_ERROR, "KRContext::loadResource - Failed to open file: %s", loadResourceInfo->pResourcePath); + delete data; + return KR_ERROR_UNEXPECTED; + } + + KRResource *resource = loadResource(loadResourceInfo->pResourcePath, data); + m_resourceMap[loadResourceInfo->resourceHandle] = resource; + return KR_SUCCESS; } KrResult KRContext::unloadResource(const KrUnloadResourceInfo* unloadResourceInfo) @@ -345,6 +366,29 @@ KrResult KRContext::createBundle(const KrCreateBundleInfo* createBundleInfo) return KR_SUCCESS; } +KrResult KRContext::moveToBundle(const KrMoveToBundleInfo* moveToBundleInfo) +{ + if (moveToBundleInfo->bundleHandle < 0 || moveToBundleInfo->bundleHandle >= m_resourceMapSize) { + return KR_ERROR_OUT_OF_BOUNDS; + } + if (moveToBundleInfo->resourceHandle < 0 || moveToBundleInfo->resourceHandle >= m_resourceMapSize) { + return KR_ERROR_OUT_OF_BOUNDS; + } + KRResource* resource = m_resourceMap[moveToBundleInfo->resourceHandle]; + if (resource == nullptr) { + return KR_ERROR_NOT_MAPPED; + } + KRResource* bundleResource = m_resourceMap[moveToBundleInfo->bundleHandle]; + if (bundleResource == nullptr) { + return KR_ERROR_NOT_MAPPED; + } + KRBundle* bundle = dynamic_cast(bundleResource); + if (bundle == nullptr) { + return KR_ERROR_INCORRECT_TYPE; + } + return resource->moveToBundle(bundle); +} + KrResult KRContext::saveResource(const KrSaveResourceInfo* saveResourceInfo) { if (saveResourceInfo->resourceHandle < 0 || saveResourceInfo->resourceHandle >= m_resourceMapSize) { diff --git a/kraken/KRContext.h b/kraken/KRContext.h index 3e7641f..9b0cfab 100755 --- a/kraken/KRContext.h +++ b/kraken/KRContext.h @@ -19,6 +19,7 @@ #include "KRAnimationManager.h" #include "KRAnimationCurveManager.h" #include "KRUnknownManager.h" +#include "KRSourceManager.h" #include "KRStreamer.h" class KRAudioManager; @@ -39,11 +40,13 @@ public: ~KRContext(); KrResult createBundle(const KrCreateBundleInfo* createBundleInfo); + KrResult moveToBundle(const KrMoveToBundleInfo* moveToBundleInfo); + KrResult loadResource(const KrLoadResourceInfo* loadResourceInfo); KrResult unloadResource(const KrUnloadResourceInfo* unloadResourceInfo); KrResult saveResource(const KrSaveResourceInfo* saveResourceInfo); - void loadResource(const std::string &file_name, KRDataBlock *data); - void loadResource(std::string path); + KRResource* loadResource(const std::string &file_name, KRDataBlock *data); + KRBundleManager *getBundleManager(); KRSceneManager *getSceneManager(); @@ -55,6 +58,7 @@ public: KRAnimationCurveManager *getAnimationCurveManager(); KRAudioManager *getAudioManager(); KRUnknownManager *getUnknownManager(); + KRSourceManager *getSourceManager(); KRCamera *createCamera(int width, int height); @@ -116,6 +120,7 @@ private: KRAnimationCurveManager *m_pAnimationCurveManager; KRAudioManager *m_pSoundManager; KRUnknownManager *m_pUnknownManager; + KRSourceManager *m_pSourceManager; KRResource** m_resourceMap; size_t m_resourceMapSize; diff --git a/kraken/KRMaterialManager.cpp b/kraken/KRMaterialManager.cpp index becf2f2..2dce5f2 100755 --- a/kraken/KRMaterialManager.cpp +++ b/kraken/KRMaterialManager.cpp @@ -97,7 +97,7 @@ void KRMaterialManager::add(KRMaterial *new_material) { m_materials[lowerName] = new_material; } -bool KRMaterialManager::load(const char *szName, KRDataBlock *data) { +KRMaterial* KRMaterialManager::load(const char *szName, KRDataBlock *data) { KRMaterial *pMaterial = NULL; char szSymbol[16][256]; data->lock(); @@ -284,5 +284,5 @@ bool KRMaterialManager::load(const char *szName, KRDataBlock *data) { } data->unlock(); delete data; - return true; + return pMaterial; } diff --git a/kraken/KRMaterialManager.h b/kraken/KRMaterialManager.h index d2e85b1..8a4c335 100755 --- a/kraken/KRMaterialManager.h +++ b/kraken/KRMaterialManager.h @@ -49,7 +49,7 @@ public: KRMaterialManager(KRContext &context, KRTextureManager *pTextureManager, KRShaderManager *pShaderManager); virtual ~KRMaterialManager(); - bool load(const char *szName, KRDataBlock *data); + KRMaterial* load(const char *szName, KRDataBlock *data); void add(KRMaterial *new_material); KRMaterial *getMaterial(const std::string &name); diff --git a/kraken/KRResource+blend.cpp b/kraken/KRResource+blend.cpp index 5d4f7c2..3c12fc1 100755 --- a/kraken/KRResource+blend.cpp +++ b/kraken/KRResource+blend.cpp @@ -13,10 +13,8 @@ #include "KRResource+blend.h" -std::vector KRResource::LoadBlenderScene(KRContext &context, const std::string& path) { - std::vector resources; +KRScene* KRResource::LoadBlenderScene(KRContext &context, const std::string& path) { KRScene *pScene = new KRScene(context, KRResource::GetFileBase(path)); - resources.push_back(pScene); KRDataBlock data; @@ -24,7 +22,7 @@ std::vector KRResource::LoadBlenderScene(KRContext &context, const //KRBlendFile blend_file = KRBlendFile(pFile); } - return resources; + return pScene; } diff --git a/kraken/KRResource+obj.cpp b/kraken/KRResource+obj.cpp index 057bf53..834685e 100755 --- a/kraken/KRResource+obj.cpp +++ b/kraken/KRResource+obj.cpp @@ -11,12 +11,9 @@ #include "KRResource.h" #include "KRMesh.h" -std::vector KRResource::LoadObj(KRContext &context, const std::string& path) -{ - std::vector resources; - +KRMesh* KRResource::LoadObj(KRContext &context, const std::string& path) +{ KRMesh *new_mesh = new KRMesh(context, KRResource::GetFileBase(path)); - resources.push_back(new_mesh); KRMesh::mesh_info mi; @@ -337,5 +334,5 @@ std::vector KRResource::LoadObj(KRContext &context, const std::str free(pFaces); } - return resources; + return new_mesh; } diff --git a/kraken/KRResource.cpp b/kraken/KRResource.cpp index fbd0ada..d4143da 100755 --- a/kraken/KRResource.cpp +++ b/kraken/KRResource.cpp @@ -8,6 +8,7 @@ #include "KREngine-common.h" #include "KRResource.h" +#include "KRBundle.h" KRResource::KRResource(KRContext &context, std::string name) : KRContextObject(context) { m_name = name; @@ -67,4 +68,15 @@ bool KRResource::save(const std::string& path) } else { return false; } -} \ No newline at end of file +} + +KrResult KRResource::moveToBundle(KRBundle* bundle) +{ + KRDataBlock* data = bundle->append(*this); + if (data == nullptr) { + return KR_ERROR_UNEXPECTED; + } + // TODO(kearwood) - We must re-attach the KRResource to the returned KRDataBlock + delete data; + return KR_SUCCESS; +} diff --git a/kraken/KRResource.h b/kraken/KRResource.h index 9af319f..1cb7db9 100755 --- a/kraken/KRResource.h +++ b/kraken/KRResource.h @@ -12,7 +12,9 @@ #ifndef KRRESOURCE_H #define KRRESOURCE_H - +class KRBundle; +class KRScene; +class KRMesh; class KRResource : public KRContextObject { public: @@ -20,6 +22,8 @@ public: virtual std::string getExtension() = 0; virtual bool save(const std::string& path); virtual bool save(KRDataBlock &data) = 0; + + KrResult moveToBundle(KRBundle* bundle); static std::string GetFileExtension(const std::string& name); static std::string GetFileBase(const std::string& name); @@ -27,11 +31,10 @@ public: virtual ~KRResource(); - - static std::vector LoadObj(KRContext &context, const std::string& path); + static KRMesh* LoadObj(KRContext &context, const std::string& path); #if !TARGET_OS_IPHONE -// static void LoadFbx(KRContext &context, const std::string& path); TODO, FINDME, HACK! - Uncomment - static std::vector LoadBlenderScene(KRContext &context, const std::string& path); +// static KRScene* LoadFbx(KRContext &context, const std::string& path); TODO, FINDME, HACK! - Uncomment + static KRScene* LoadBlenderScene(KRContext &context, const std::string& path); #endif protected: diff --git a/kraken/KRSource.cpp b/kraken/KRSource.cpp new file mode 100644 index 0000000..aba023b --- /dev/null +++ b/kraken/KRSource.cpp @@ -0,0 +1,65 @@ +// +// KRSource.cpp +// KREngine +// +// Copyright 2012 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KRSource.h" + +KRSource::KRSource(KRContext &context, std::string name, std::string extension) : KRResource(context, name) +{ + m_pData = new KRDataBlock(); + m_extension = extension; +} + +KRSource::KRSource(KRContext &context, std::string name, std::string extension, KRDataBlock *data) : KRResource(context, name) +{ + m_pData = data; + m_extension = extension; +} + +KRSource::~KRSource() +{ + delete m_pData; +} + +std::string KRSource::getExtension() +{ + return m_extension; +} + +bool KRSource::save(KRDataBlock &data) +{ + data.append(*m_pData); + return true; +} + +KRDataBlock *KRSource::getData() +{ + return m_pData; +} diff --git a/kraken/KRSource.h b/kraken/KRSource.h new file mode 100644 index 0000000..cdeb9d1 --- /dev/null +++ b/kraken/KRSource.h @@ -0,0 +1,59 @@ +// +// KRSource.h +// KREngine +// +// Copyright 2012 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#ifndef KRSOURCE_H +#define KRSOURCE_H + +#include "KREngine-common.h" +#include "KRContextObject.h" +#include "KRDataBlock.h" +#include "KRResource.h" + +class KRSource : public KRResource { + +public: + KRSource(KRContext &context, std::string name, std::string extension); + KRSource(KRContext &context, std::string name, std::string extension, KRDataBlock *data); + virtual ~KRSource(); + + virtual std::string getExtension(); + + virtual bool save(KRDataBlock &data); + + KRDataBlock *getData(); + +private: + + std::string m_extension; + KRDataBlock *m_pData; +}; + +#endif /* defined(KRSOURCE_H) */ diff --git a/kraken/KRSourceManager.cpp b/kraken/KRSourceManager.cpp new file mode 100644 index 0000000..b7b3662 --- /dev/null +++ b/kraken/KRSourceManager.cpp @@ -0,0 +1,102 @@ +// +// SourceManager.cpp +// KREngine +// +// Copyright 2012 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "KRSourceManager.h" +#include "KREngine-common.h" + +KRSourceManager::KRSourceManager(KRContext &context) : KRContextObject(context) +{ + +} + +KRSourceManager::~KRSourceManager() +{ + for(unordered_map >::iterator extension_itr = m_sources.begin(); extension_itr != m_sources.end(); extension_itr++) { + for(unordered_map::iterator name_itr=(*extension_itr).second.begin(); name_itr != (*extension_itr).second.end(); name_itr++) { + delete (*name_itr).second; + } + } +} + +unordered_map > &KRSourceManager::getSources() +{ + return m_sources; +} + +void KRSourceManager::add(KRSource *source) +{ + std::string lower_name = source->getName(); + std::string lower_extension = source->getExtension(); + + std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower); + std::transform(lower_extension.begin(), lower_extension.end(), lower_extension.begin(), ::tolower); + + unordered_map >::iterator extension_itr = m_sources.find(lower_extension); + if(extension_itr == m_sources.end()) { + m_sources[lower_extension] = unordered_map(); + extension_itr = m_sources.find(lower_extension); + } + + unordered_map::iterator name_itr = (*extension_itr).second.find(lower_name); + if(name_itr != (*extension_itr).second.end()) { + delete (*name_itr).second; + (*name_itr).second = source; + } else { + (*extension_itr).second[lower_name] = source; + } +} + +KRSource *KRSourceManager::load(const std::string &name, const std::string &extension, KRDataBlock *data) +{ + KRSource *source = new KRSource(getContext(), name, extension, data); + if(source) add(source); + return source; +} + +KRSource *KRSourceManager::get(const std::string &name, const std::string &extension) +{ + std::string lower_name = name; + std::string lower_extension = extension; + + std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), ::tolower); + std::transform(lower_extension.begin(), lower_extension.end(), lower_extension.begin(), ::tolower); + + return m_sources[lower_extension][lower_name]; +} + + +const unordered_map &KRSourceManager::get(const std::string &extension) +{ + std::string lower_extension = extension; + std::transform(lower_extension.begin(), lower_extension.end(), lower_extension.begin(), ::tolower); + return m_sources[lower_extension]; +} + diff --git a/kraken/KRSourceManager.h b/kraken/KRSourceManager.h new file mode 100644 index 0000000..b3eeabe --- /dev/null +++ b/kraken/KRSourceManager.h @@ -0,0 +1,60 @@ +// +// FileManager.h +// KREngine +// +// Copyright 2012 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#ifndef KRSOURCE_MANAGER_H +#define KRSOURCE_MANAGER_H + +#include "KREngine-common.h" + +#include "KRSource.h" +#include "KRContextObject.h" +#include "KRDataBlock.h" + +class KRSourceManager : public KRContextObject { +public: + KRSourceManager(KRContext &context); + virtual ~KRSourceManager(); + + void add(KRSource *source); + + KRSource *load(const std::string &name, const std::string &extension, KRDataBlock *data); + KRSource *get(const std::string &name, const std::string &extension); + + + const unordered_map &get(const std::string &extension); + + unordered_map > &getSources(); + +private: + unordered_map > m_sources; +}; + +#endif /* defined(KRUNKNOWN_MANAGER_H) */ diff --git a/kraken/kraken.cpp b/kraken/kraken.cpp index 4a90253..eb1a166 100644 --- a/kraken/kraken.cpp +++ b/kraken/kraken.cpp @@ -31,7 +31,7 @@ KrResult KrLoadResource(const KrLoadResourceInfo* pLoadResourceInfo) if (!sContext) { return KR_ERROR_NOT_INITIALIZED; } - sContext->loadResource(pLoadResourceInfo->pResourcePath); + sContext->loadResource(pLoadResourceInfo); return KR_SUCCESS; } @@ -71,5 +71,8 @@ KrResult KrCreateBundle(const KrCreateBundleInfo* pCreateBundleInfo) KrResult KrMoveToBundle(const KrMoveToBundleInfo* pMoveToBundleInfo) { - return KR_ERROR_NOT_IMPLEMENTED; + if (!sContext) { + return KR_ERROR_NOT_INITIALIZED; + } + return sContext->moveToBundle(pMoveToBundleInfo); } diff --git a/kraken/public/kraken.h b/kraken/public/kraken.h index 3afdfd1..d81ecbb 100644 --- a/kraken/public/kraken.h +++ b/kraken/public/kraken.h @@ -41,6 +41,7 @@ typedef enum { KR_ERROR_NOT_IMPLEMENTED = 2, KR_ERROR_OUT_OF_BOUNDS = 3, KR_ERROR_NOT_MAPPED = 4, + KR_ERROR_INCORRECT_TYPE = 5, KR_ERROR_UNEXPECTED = 0x10000000, KR_RESULT_MAX_ENUM = 0x7FFFFFFF } KrResult;