Implemented KRSource and KRSourceManager. Implemented MoveToBundle api.

This commit is contained in:
Kearwood Kip Gilbert
2019-08-18 17:57:41 -07:00
parent fdda52d406
commit cbbbe41cb0
17 changed files with 410 additions and 56 deletions

View File

@@ -67,6 +67,8 @@ add_sources(KRResource.cpp)
add_sources(KRReverbZone.cpp) add_sources(KRReverbZone.cpp)
add_sources(KRScene.cpp) add_sources(KRScene.cpp)
add_sources(KRSceneManager.cpp) add_sources(KRSceneManager.cpp)
add_sources(KRSource.cpp)
add_sources(KRSourceManager.cpp)
add_sources(KRShader.cpp) add_sources(KRShader.cpp)
add_sources(KRShaderManager.cpp) add_sources(KRShaderManager.cpp)
add_sources(KRSpotLight.cpp) add_sources(KRSpotLight.cpp)

View File

@@ -114,7 +114,7 @@ bool KRBundle::save(KRDataBlock &data) {
return true; return true;
} }
void KRBundle::append(KRResource &resource) KRDataBlock* KRBundle::append(KRResource &resource)
{ {
// Serialize resource to binary representation // Serialize resource to binary representation
KRDataBlock resource_data; 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 // 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 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->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(); m_pData->lock();
@@ -163,4 +163,7 @@ void KRBundle::append(KRResource &resource)
sprintf(file_header->checksum, "%07o", check_sum); sprintf(file_header->checksum, "%07o", check_sum);
m_pData->unlock(); m_pData->unlock();
KRDataBlock *pFileData = m_pData->getSubBlock((int)resource_data_start, (int)resource_data.getSize());
return pFileData;
} }

View File

@@ -44,7 +44,7 @@ public:
virtual bool save(const std::string& path); virtual bool save(const std::string& path);
virtual bool save(KRDataBlock &data); virtual bool save(KRDataBlock &data);
void append(KRResource &resource); KRDataBlock* append(KRResource &resource);
private: private:
KRDataBlock *m_pData; KRDataBlock *m_pData;

View File

@@ -83,6 +83,7 @@ KRContext::KRContext(const KrInitializeInfo* initializeInfo)
m_pAnimationCurveManager = new KRAnimationCurveManager(*this); m_pAnimationCurveManager = new KRAnimationCurveManager(*this);
m_pSoundManager = new KRAudioManager(*this); m_pSoundManager = new KRAudioManager(*this);
m_pUnknownManager = new KRUnknownManager(*this); m_pUnknownManager = new KRUnknownManager(*this);
m_pSourceManager = new KRSourceManager(*this);
m_streamingEnabled = true; m_streamingEnabled = true;
@@ -147,6 +148,11 @@ KRContext::~KRContext() {
m_pSoundManager = NULL; m_pSoundManager = NULL;
} }
if(m_pSourceManager) {
delete m_pSourceManager;
m_pSourceManager = NULL;
}
if(m_pUnknownManager) { if(m_pUnknownManager) {
delete m_pUnknownManager; delete m_pUnknownManager;
m_pUnknownManager = NULL; m_pUnknownManager = NULL;
@@ -218,13 +224,14 @@ KRAnimationCurveManager *KRContext::getAnimationCurveManager() {
KRAudioManager *KRContext::getAudioManager() { KRAudioManager *KRContext::getAudioManager() {
return m_pSoundManager; return m_pSoundManager;
} }
KRSourceManager *KRContext::getSourceManager() {
return m_pSourceManager;
}
KRUnknownManager *KRContext::getUnknownManager() { KRUnknownManager *KRContext::getUnknownManager() {
return m_pUnknownManager; return m_pUnknownManager;
} }
std::vector<KRResource *> KRContext::getResources() std::vector<KRResource *> KRContext::getResources()
{ {
std::vector<KRResource *> resources; std::vector<KRResource *> resources;
for(unordered_map<std::string, KRScene *>::iterator itr = m_pSceneManager->getScenes().begin(); itr != m_pSceneManager->getScenes().end(); itr++) { for(unordered_map<std::string, KRScene *>::iterator itr = m_pSceneManager->getScenes().begin(); itr != m_pSceneManager->getScenes().end(); itr++) {
@@ -249,6 +256,13 @@ std::vector<KRResource *> KRContext::getResources()
resources.push_back((*itr).second); resources.push_back((*itr).second);
} }
unordered_map<std::string, unordered_map<std::string, KRSource *> > sources = m_pSourceManager->getSources();
for(unordered_map<std::string, unordered_map<std::string, KRSource *> >::iterator itr = sources.begin(); itr != sources.end(); itr++) {
for(unordered_map<std::string, KRSource *>::iterator itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr2++) {
resources.push_back((*itr2).second);
}
}
unordered_map<std::string, unordered_map<std::string, KRUnknown *> > unknowns = m_pUnknownManager->getUnknowns(); unordered_map<std::string, unordered_map<std::string, KRUnknown *> > unknowns = m_pUnknownManager->getUnknowns();
for(unordered_map<std::string, unordered_map<std::string, KRUnknown *> >::iterator itr = unknowns.begin(); itr != unknowns.end(); itr++) { for(unordered_map<std::string, unordered_map<std::string, KRUnknown *> >::iterator itr = unknowns.begin(); itr != unknowns.end(); itr++) {
for(unordered_map<std::string, KRUnknown *>::iterator itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr2++) { for(unordered_map<std::string, KRUnknown *>::iterator itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr2++) {
@@ -256,69 +270,76 @@ std::vector<KRResource *> 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; 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 name = KRResource::GetFileBase(file_name);
std::string extension = KRResource::GetFileExtension(file_name); std::string extension = KRResource::GetFileExtension(file_name);
KRResource *resource = nullptr;
// fprintf(stderr, "KRContext::loadResource - Loading: %s\n", file_name.c_str()); // fprintf(stderr, "KRContext::loadResource - Loading: %s\n", file_name.c_str());
if(extension.compare("krbundle") == 0) { 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) { } 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) { } 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) { } 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) { } 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) { } 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) { } 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) { } 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) { } 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) { } 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) { } 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) { } 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) { } 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) { } 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) { } else if(extension.compare("obj") == 0) {
KRResource::LoadObj(*this, file_name); resource = KRResource::LoadObj(*this, file_name);
#if !TARGET_OS_IPHONE #if !TARGET_OS_IPHONE
/* /*
// FINDME, TODO, HACK! - Uncomment // FINDME, TODO, HACK! - Uncomment
} else if(extension.compare("fbx") == 0) { } else if(extension.compare("fbx") == 0) {
KRResource::LoadFbx(*this, file_name); resource = KRResource::LoadFbx(*this, file_name);
*/ */
} else if(extension.compare("blend") == 0) { } else if(extension.compare("blend") == 0) {
KRResource::LoadBlenderScene(*this, file_name); resource = KRResource::LoadBlenderScene(*this, file_name);
#endif #endif
} else { } else {
m_pUnknownManager->load(name, extension, data); resource = m_pUnknownManager->load(name, extension, data);
} }
return resource;
} }
void KRContext::loadResource(std::string path) { KrResult KRContext::loadResource(const KrLoadResourceInfo* loadResourceInfo) {
KRDataBlock *data = new KRDataBlock(); if (loadResourceInfo->resourceHandle < 0 || loadResourceInfo->resourceHandle >= m_resourceMapSize) {
if(data->load(path)) { return KR_ERROR_OUT_OF_BOUNDS;
loadResource(path, data);
} else {
KRContext::Log(KRContext::LOG_LEVEL_ERROR, "KRContext::loadResource - Failed to open file: %s", path.c_str());
delete data;
} }
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) KrResult KRContext::unloadResource(const KrUnloadResourceInfo* unloadResourceInfo)
@@ -345,6 +366,29 @@ KrResult KRContext::createBundle(const KrCreateBundleInfo* createBundleInfo)
return KR_SUCCESS; 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<KRBundle*>(bundleResource);
if (bundle == nullptr) {
return KR_ERROR_INCORRECT_TYPE;
}
return resource->moveToBundle(bundle);
}
KrResult KRContext::saveResource(const KrSaveResourceInfo* saveResourceInfo) KrResult KRContext::saveResource(const KrSaveResourceInfo* saveResourceInfo)
{ {
if (saveResourceInfo->resourceHandle < 0 || saveResourceInfo->resourceHandle >= m_resourceMapSize) { if (saveResourceInfo->resourceHandle < 0 || saveResourceInfo->resourceHandle >= m_resourceMapSize) {

View File

@@ -19,6 +19,7 @@
#include "KRAnimationManager.h" #include "KRAnimationManager.h"
#include "KRAnimationCurveManager.h" #include "KRAnimationCurveManager.h"
#include "KRUnknownManager.h" #include "KRUnknownManager.h"
#include "KRSourceManager.h"
#include "KRStreamer.h" #include "KRStreamer.h"
class KRAudioManager; class KRAudioManager;
@@ -39,11 +40,13 @@ public:
~KRContext(); ~KRContext();
KrResult createBundle(const KrCreateBundleInfo* createBundleInfo); KrResult createBundle(const KrCreateBundleInfo* createBundleInfo);
KrResult moveToBundle(const KrMoveToBundleInfo* moveToBundleInfo);
KrResult loadResource(const KrLoadResourceInfo* loadResourceInfo);
KrResult unloadResource(const KrUnloadResourceInfo* unloadResourceInfo); KrResult unloadResource(const KrUnloadResourceInfo* unloadResourceInfo);
KrResult saveResource(const KrSaveResourceInfo* saveResourceInfo); KrResult saveResource(const KrSaveResourceInfo* saveResourceInfo);
void loadResource(const std::string &file_name, KRDataBlock *data); KRResource* loadResource(const std::string &file_name, KRDataBlock *data);
void loadResource(std::string path);
KRBundleManager *getBundleManager(); KRBundleManager *getBundleManager();
KRSceneManager *getSceneManager(); KRSceneManager *getSceneManager();
@@ -55,6 +58,7 @@ public:
KRAnimationCurveManager *getAnimationCurveManager(); KRAnimationCurveManager *getAnimationCurveManager();
KRAudioManager *getAudioManager(); KRAudioManager *getAudioManager();
KRUnknownManager *getUnknownManager(); KRUnknownManager *getUnknownManager();
KRSourceManager *getSourceManager();
KRCamera *createCamera(int width, int height); KRCamera *createCamera(int width, int height);
@@ -116,6 +120,7 @@ private:
KRAnimationCurveManager *m_pAnimationCurveManager; KRAnimationCurveManager *m_pAnimationCurveManager;
KRAudioManager *m_pSoundManager; KRAudioManager *m_pSoundManager;
KRUnknownManager *m_pUnknownManager; KRUnknownManager *m_pUnknownManager;
KRSourceManager *m_pSourceManager;
KRResource** m_resourceMap; KRResource** m_resourceMap;
size_t m_resourceMapSize; size_t m_resourceMapSize;

View File

@@ -97,7 +97,7 @@ void KRMaterialManager::add(KRMaterial *new_material) {
m_materials[lowerName] = 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; KRMaterial *pMaterial = NULL;
char szSymbol[16][256]; char szSymbol[16][256];
data->lock(); data->lock();
@@ -284,5 +284,5 @@ bool KRMaterialManager::load(const char *szName, KRDataBlock *data) {
} }
data->unlock(); data->unlock();
delete data; delete data;
return true; return pMaterial;
} }

View File

@@ -49,7 +49,7 @@ public:
KRMaterialManager(KRContext &context, KRTextureManager *pTextureManager, KRShaderManager *pShaderManager); KRMaterialManager(KRContext &context, KRTextureManager *pTextureManager, KRShaderManager *pShaderManager);
virtual ~KRMaterialManager(); virtual ~KRMaterialManager();
bool load(const char *szName, KRDataBlock *data); KRMaterial* load(const char *szName, KRDataBlock *data);
void add(KRMaterial *new_material); void add(KRMaterial *new_material);
KRMaterial *getMaterial(const std::string &name); KRMaterial *getMaterial(const std::string &name);

View File

@@ -13,10 +13,8 @@
#include "KRResource+blend.h" #include "KRResource+blend.h"
std::vector<KRResource *> KRResource::LoadBlenderScene(KRContext &context, const std::string& path) { KRScene* KRResource::LoadBlenderScene(KRContext &context, const std::string& path) {
std::vector<KRResource *> resources;
KRScene *pScene = new KRScene(context, KRResource::GetFileBase(path)); KRScene *pScene = new KRScene(context, KRResource::GetFileBase(path));
resources.push_back(pScene);
KRDataBlock data; KRDataBlock data;
@@ -24,7 +22,7 @@ std::vector<KRResource *> KRResource::LoadBlenderScene(KRContext &context, const
//KRBlendFile blend_file = KRBlendFile(pFile); //KRBlendFile blend_file = KRBlendFile(pFile);
} }
return resources; return pScene;
} }

View File

@@ -11,12 +11,9 @@
#include "KRResource.h" #include "KRResource.h"
#include "KRMesh.h" #include "KRMesh.h"
std::vector<KRResource *> KRResource::LoadObj(KRContext &context, const std::string& path) KRMesh* KRResource::LoadObj(KRContext &context, const std::string& path)
{ {
std::vector<KRResource *> resources;
KRMesh *new_mesh = new KRMesh(context, KRResource::GetFileBase(path)); KRMesh *new_mesh = new KRMesh(context, KRResource::GetFileBase(path));
resources.push_back(new_mesh);
KRMesh::mesh_info mi; KRMesh::mesh_info mi;
@@ -337,5 +334,5 @@ std::vector<KRResource *> KRResource::LoadObj(KRContext &context, const std::str
free(pFaces); free(pFaces);
} }
return resources; return new_mesh;
} }

View File

@@ -8,6 +8,7 @@
#include "KREngine-common.h" #include "KREngine-common.h"
#include "KRResource.h" #include "KRResource.h"
#include "KRBundle.h"
KRResource::KRResource(KRContext &context, std::string name) : KRContextObject(context) { KRResource::KRResource(KRContext &context, std::string name) : KRContextObject(context) {
m_name = name; m_name = name;
@@ -68,3 +69,14 @@ bool KRResource::save(const std::string& path)
return false; return false;
} }
} }
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;
}

View File

@@ -12,7 +12,9 @@
#ifndef KRRESOURCE_H #ifndef KRRESOURCE_H
#define KRRESOURCE_H #define KRRESOURCE_H
class KRBundle;
class KRScene;
class KRMesh;
class KRResource : public KRContextObject class KRResource : public KRContextObject
{ {
public: public:
@@ -21,17 +23,18 @@ public:
virtual bool save(const std::string& path); virtual bool save(const std::string& path);
virtual bool save(KRDataBlock &data) = 0; virtual bool save(KRDataBlock &data) = 0;
KrResult moveToBundle(KRBundle* bundle);
static std::string GetFileExtension(const std::string& name); static std::string GetFileExtension(const std::string& name);
static std::string GetFileBase(const std::string& name); static std::string GetFileBase(const std::string& name);
static std::string GetFilePath(const std::string& name); static std::string GetFilePath(const std::string& name);
virtual ~KRResource(); virtual ~KRResource();
static KRMesh* LoadObj(KRContext &context, const std::string& path);
static std::vector<KRResource *> LoadObj(KRContext &context, const std::string& path);
#if !TARGET_OS_IPHONE #if !TARGET_OS_IPHONE
// static void LoadFbx(KRContext &context, const std::string& path); TODO, FINDME, HACK! - Uncomment // static KRScene* LoadFbx(KRContext &context, const std::string& path); TODO, FINDME, HACK! - Uncomment
static std::vector<KRResource *> LoadBlenderScene(KRContext &context, const std::string& path); static KRScene* LoadBlenderScene(KRContext &context, const std::string& path);
#endif #endif
protected: protected:

65
kraken/KRSource.cpp Normal file
View File

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

59
kraken/KRSource.h Normal file
View File

@@ -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) */

102
kraken/KRSourceManager.cpp Normal file
View File

@@ -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<std::string, unordered_map<std::string, KRSource *> >::iterator extension_itr = m_sources.begin(); extension_itr != m_sources.end(); extension_itr++) {
for(unordered_map<std::string, KRSource *>::iterator name_itr=(*extension_itr).second.begin(); name_itr != (*extension_itr).second.end(); name_itr++) {
delete (*name_itr).second;
}
}
}
unordered_map<std::string, unordered_map<std::string, KRSource *> > &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<std::string, unordered_map<std::string, KRSource *> >::iterator extension_itr = m_sources.find(lower_extension);
if(extension_itr == m_sources.end()) {
m_sources[lower_extension] = unordered_map<std::string, KRSource *>();
extension_itr = m_sources.find(lower_extension);
}
unordered_map<std::string, KRSource *>::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<std::string, KRSource *> &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];
}

60
kraken/KRSourceManager.h Normal file
View File

@@ -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<std::string, KRSource *> &get(const std::string &extension);
unordered_map<std::string, unordered_map<std::string, KRSource *> > &getSources();
private:
unordered_map<std::string, unordered_map<std::string, KRSource *> > m_sources;
};
#endif /* defined(KRUNKNOWN_MANAGER_H) */

View File

@@ -31,7 +31,7 @@ KrResult KrLoadResource(const KrLoadResourceInfo* pLoadResourceInfo)
if (!sContext) { if (!sContext) {
return KR_ERROR_NOT_INITIALIZED; return KR_ERROR_NOT_INITIALIZED;
} }
sContext->loadResource(pLoadResourceInfo->pResourcePath); sContext->loadResource(pLoadResourceInfo);
return KR_SUCCESS; return KR_SUCCESS;
} }
@@ -71,5 +71,8 @@ KrResult KrCreateBundle(const KrCreateBundleInfo* pCreateBundleInfo)
KrResult KrMoveToBundle(const KrMoveToBundleInfo* pMoveToBundleInfo) KrResult KrMoveToBundle(const KrMoveToBundleInfo* pMoveToBundleInfo)
{ {
return KR_ERROR_NOT_IMPLEMENTED; if (!sContext) {
return KR_ERROR_NOT_INITIALIZED;
}
return sContext->moveToBundle(pMoveToBundleInfo);
} }

View File

@@ -41,6 +41,7 @@ typedef enum {
KR_ERROR_NOT_IMPLEMENTED = 2, KR_ERROR_NOT_IMPLEMENTED = 2,
KR_ERROR_OUT_OF_BOUNDS = 3, KR_ERROR_OUT_OF_BOUNDS = 3,
KR_ERROR_NOT_MAPPED = 4, KR_ERROR_NOT_MAPPED = 4,
KR_ERROR_INCORRECT_TYPE = 5,
KR_ERROR_UNEXPECTED = 0x10000000, KR_ERROR_UNEXPECTED = 0x10000000,
KR_RESULT_MAX_ENUM = 0x7FFFFFFF KR_RESULT_MAX_ENUM = 0x7FFFFFFF
} KrResult; } KrResult;