From 972cfa0d39cb83f06259942b2b10ee319568911f Mon Sep 17 00:00:00 2001 From: dean11 Date: Wed, 27 Nov 2013 21:12:37 +0100 Subject: [PATCH] Fixed bugs with custom loading --- Code/Misc/Resource/Loaders/ByteLoader.cpp | 2 +- Code/Misc/Resource/Loaders/CustomLoader.cpp | 33 ++++++++--- Code/Misc/Resource/OResource.cpp | 10 ++-- Code/Misc/Resource/OResource.h | 32 +++++++--- Code/Misc/Resource/OResourceHandler.cpp | 65 +++++++++++++++++---- Code/Misc/Resource/OysterResource.h | 51 +++++++++++++--- 6 files changed, 150 insertions(+), 43 deletions(-) diff --git a/Code/Misc/Resource/Loaders/ByteLoader.cpp b/Code/Misc/Resource/Loaders/ByteLoader.cpp index 622d3a71..b20364ff 100644 --- a/Code/Misc/Resource/Loaders/ByteLoader.cpp +++ b/Code/Misc/Resource/Loaders/ByteLoader.cpp @@ -152,7 +152,7 @@ OResource* OResource::ByteLoader(const wchar_t filename[], ResourceType type, OR if(!old) { - resource = new OResource((OHRESOURCE)data, type, (sizeof(char) * sOut.size()), sizeof(char), filename); + resource = new OResource((OHRESOURCE&)data, type, (sizeof(char) * sOut.size()), sizeof(char), filename); } else { diff --git a/Code/Misc/Resource/Loaders/CustomLoader.cpp b/Code/Misc/Resource/Loaders/CustomLoader.cpp index 312c4c53..1e030a6b 100644 --- a/Code/Misc/Resource/Loaders/CustomLoader.cpp +++ b/Code/Misc/Resource/Loaders/CustomLoader.cpp @@ -12,34 +12,49 @@ using namespace Oyster::Resource; OResource* OResource::CustomLoader(const wchar_t filename[], CustomLoadFunction fnc) { - const CustomData &data = fnc(filename); + CustomData data; + memset(&data, 0, sizeof(CustomData)); - if(!data.loadedData) return 0; - if(!data.resourceUnloadFnc) return 0; + fnc(filename, data); + + if(!data.loadedData) + { + return 0; + } + if(!data.resourceUnloadFnc) + { + return 0; + } + /** For some wierd reason that i don't understand when trying to send data.loadedData directly as a + * parameter to OResource constructor, the value is changed when it arrives in the constructor. + * Doing it like this, storing in a temporary variable, the value stays correct. (What the fuck! I must be overloking something...)*/ + //OHRESOURCE temp = data.loadedData; + OResource *resource = new OResource(data.loadedData, ResourceType_UNKNOWN, 0, 0, filename); - OResource *resource = new OResource((OHRESOURCE)data.loadedData, ResourceType_UNKNOWN, 0, 0, filename); - resource->customData = new CustomResourceData(); resource->customData->unloadingFunction = data.resourceUnloadFnc; - resource->resourceData = (OHRESOURCE)data.loadedData; resource->customData->loadingFunction = fnc; return resource; } void OResource::CustomUnloader() { - this->customData->unloadingFunction((void*)this->resourceData); + this->customData->unloadingFunction(this->resourceData); } OResource* OResource::CustomReloader() { CustomUnloader(); - const CustomData &data = this->customData->loadingFunction(this->resourceFilename.c_str()); + CustomData data; + memset(&data, 0, sizeof(CustomData)); + + this->customData->loadingFunction(this->resourceFilename.c_str(), data); this->resourceData = (OHRESOURCE)data.loadedData; if(data.resourceUnloadFnc) + { this->customData->unloadingFunction = data.resourceUnloadFnc; - + } return this; } diff --git a/Code/Misc/Resource/OResource.cpp b/Code/Misc/Resource/OResource.cpp index 4311669f..e2aab644 100644 --- a/Code/Misc/Resource/OResource.cpp +++ b/Code/Misc/Resource/OResource.cpp @@ -7,17 +7,19 @@ using namespace Oyster::Resource; OResource::OResource(OHRESOURCE handle, ResourceType type, size_t resourceSize, size_t elementSize, ::std::wstring filename) - : resourceData (handle) - , resourceFilename (filename) + : resourceFilename (filename) , resourceSize (resourceSize) , resourceElementSize (elementSize) , resourceType (type) , customData (0) { - + resourceData = handle; } OResource::~OResource() -{} +{ + delete this->customData; + this->customData = 0; +} OResource* OResource::Load (const wchar_t filename[], ResourceType type) diff --git a/Code/Misc/Resource/OResource.h b/Code/Misc/Resource/OResource.h index c0e32ba5..a0573c92 100644 --- a/Code/Misc/Resource/OResource.h +++ b/Code/Misc/Resource/OResource.h @@ -27,19 +27,33 @@ namespace Oyster virtual~ OResource(); inline ResourceType GetResourceType() const - { return this->resourceType; } + { + return this->resourceType; + } inline const wchar_t* GetResourceFilename() const - { return this->resourceFilename.c_str(); } + { + return this->resourceFilename.c_str(); + } inline OHRESOURCE GetResourceHandle() const - { return this->resourceData; } + { + return this->resourceData; + } inline unsigned long long GetResourceSize() const - { return this->resourceSize; } + { + return this->resourceSize; + } inline unsigned long long GetResourceElementSize() const - { return this->resourceElementSize; } + { + return this->resourceElementSize; + } inline unsigned int GetResourceID() const - { return this->resourceID; } - inline void SetResourceID(unsigned int id) - { this->resourceID = id; } + { + return this->resourceID; + } + inline void SetResourceID(int id) + { + this->resourceID = id; + } public: static OResource* Load (const wchar_t filename[], ResourceType type); @@ -63,7 +77,7 @@ namespace Oyster size_t resourceSize; size_t resourceElementSize; ::std::wstring resourceFilename; - unsigned int resourceID; + int resourceID; CustomResourceData *customData; }; diff --git a/Code/Misc/Resource/OResourceHandler.cpp b/Code/Misc/Resource/OResourceHandler.cpp index 9d911875..21653d4e 100644 --- a/Code/Misc/Resource/OResourceHandler.cpp +++ b/Code/Misc/Resource/OResourceHandler.cpp @@ -43,7 +43,7 @@ OHRESOURCE OysterResource::LoadResource(const wchar_t* filename, ResourceType ty return resourceData->GetResourceHandle(); } -OHRESOURCE OysterResource::LoadResource(const wchar_t filename[], CustomLoadFunction loadFnc, unsigned int CustomId) +OHRESOURCE OysterResource::LoadResource(const wchar_t filename[], CustomLoadFunction loadFnc, int CustomId) { if(!filename) return 0; if(!loadFnc) return 0; @@ -67,14 +67,14 @@ OHRESOURCE OysterResource::LoadResource(const wchar_t filename[], CustomLoadFunc return (OHRESOURCE)resourceData->GetResourceHandle(); } -OHRESOURCE ReloadResource(const wchar_t filename[]) +OHRESOURCE OysterResource::ReloadResource(const wchar_t filename[]) { OResource *resourceData = resourcePrivate.FindResource(filename); if(!resourceData) return 0; //The resource has not been loaded return OResource::Reload(resourceData)->GetResourceHandle(); } -OHRESOURCE ReloadResource(OHRESOURCE resource) +OHRESOURCE OysterResource::ReloadResource(OHRESOURCE resource) { OResource *resourceData = resourcePrivate.FindResource(resource); if(!resourceData) return 0; //The resource has not been loaded @@ -89,12 +89,13 @@ void OysterResource::Clean() for (i; i != last; i++) { - if(OResource::Release(i->second)) - { - const wchar_t* temp = i->second->GetResourceFilename(); - delete resourcePrivate.resources[temp]; - resourcePrivate.resources.erase(temp); - } + //Remove all the references + while (!OResource::Release(i->second)); + + const wchar_t* temp = i->second->GetResourceFilename(); + delete resourcePrivate.resources[temp]; + resourcePrivate.resources.erase(temp); + } } void OysterResource::ReleaseResource(const OHRESOURCE& resourceData) @@ -110,6 +111,19 @@ void OysterResource::ReleaseResource(const OHRESOURCE& resourceData) } } } +void OysterResource::ReleaseResource(const wchar_t filename[]) +{ + OResource* t = resourcePrivate.FindResource(filename); + if(t) + { + if(OResource::Release(t)) + { + const wchar_t* temp = t->GetResourceFilename(); + delete resourcePrivate.resources[temp]; + resourcePrivate.resources.erase(temp); + } + } +} void OysterResource::SetResourceId (const OHRESOURCE& resourceData, unsigned int id) { @@ -117,13 +131,27 @@ void OysterResource::SetResourceId (const OHRESOURCE& resourceData, unsigned int if(t) t->SetResourceID(id); } +void OysterResource::SetResourceId(const wchar_t c[], unsigned int id) +{ + OResource* t = resourcePrivate.FindResource(c); + + if(t) t->SetResourceID(id); +} ResourceType OysterResource::GetResourceType (const OHRESOURCE& resourceData) { OResource* t = resourcePrivate.FindResource(resourceData); if(t) return t->GetResourceType(); - return ResourceType_UNKNOWN; + return ResourceType_INVALID; +} +ResourceType OysterResource::GetResourceType (const wchar_t c[]) +{ + OResource* t = resourcePrivate.FindResource(c); + + if(t) return t->GetResourceType(); + + return ResourceType_INVALID; } const wchar_t* OysterResource::GetResourceFilename (const OHRESOURCE& resourceData) { @@ -133,7 +161,15 @@ const wchar_t* OysterResource::GetResourceFilename (const OHRESOURCE& resourceDa return 0; } -unsigned int OysterResource::GetResourceId (const OHRESOURCE& resourceData) +OHRESOURCE OysterResource::GetResourceHandle(const wchar_t filename[]) +{ + OResource* t = resourcePrivate.FindResource(filename); + + if(t) return t->GetResourceHandle(); + + return 0; +} +int OysterResource::GetResourceId (const OHRESOURCE& resourceData) { OResource* t = resourcePrivate.FindResource(resourceData); @@ -141,7 +177,14 @@ unsigned int OysterResource::GetResourceId (const OHRESOURCE& resourceData) return -1; } +int OysterResource::GetResourceId(const wchar_t c[]) +{ + OResource* t = resourcePrivate.FindResource(c); + if(t) return t->GetResourceID(); + + return -1; +} OResource* ResourcePrivate::FindResource(const OHRESOURCE& h) const diff --git a/Code/Misc/Resource/OysterResource.h b/Code/Misc/Resource/OysterResource.h index 16d5122d..8fc0e560 100644 --- a/Code/Misc/Resource/OysterResource.h +++ b/Code/Misc/Resource/OysterResource.h @@ -12,11 +12,11 @@ namespace Oyster { struct CustomData; /** A Resource handle representing various resources */ - typedef unsigned long OHRESOURCE; + typedef void* OHRESOURCE; /** Typedef on a fuction required for custom unloading */ typedef void(*CustomUnloadFunction)(void* loadedData); /** Typedef on a fuction required for custom loading */ - typedef const CustomData&(*CustomLoadFunction)(const wchar_t filename[]); + typedef void(*CustomLoadFunction)(const wchar_t filename[], CustomData& outData); /** An enum class representing all avalible resources that is supported. */ enum ResourceType @@ -31,15 +31,14 @@ namespace Oyster ResourceType_COUNT, /**< Not used. */ ResourceType_UNKNOWN = -1, /**< Handle can be interpeted as void* */ + ResourceType_INVALID = -2, /**< Invalid or non existing resource */ }; - /** A struct to return when doing a custom resource Load - * By loading this way you are handing over the ownership to the resource loaded. - */ + /** A struct to fill when doing a custom resource Load. */ struct CustomData { - void* loadedData; ///