2013-11-22 10:46:25 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// Created by [Dennis Andersen] [2013]
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
#include "OysterResource.h"
|
|
|
|
#include "OResource.h"
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
using namespace Oyster::Resource;
|
|
|
|
|
|
|
|
class ResourcePrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
std::map<std::wstring, OResource*> resources;
|
|
|
|
|
|
|
|
OResource* FindResource(const OHRESOURCE& h) const;
|
|
|
|
OResource* FindResource(const wchar_t c[]) const;
|
|
|
|
void SaveResource(OResource* r, bool addNew = true);
|
|
|
|
|
|
|
|
} resourcePrivate;
|
|
|
|
|
|
|
|
|
2013-11-26 10:55:51 +01:00
|
|
|
OHRESOURCE OysterResource::LoadResource(const wchar_t* filename, ResourceType type)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
2013-11-26 10:55:51 +01:00
|
|
|
if(!filename) return 0;
|
2013-11-22 10:46:25 +01:00
|
|
|
|
2013-11-26 10:55:51 +01:00
|
|
|
OResource *resourceData = resourcePrivate.FindResource(filename);
|
|
|
|
if(resourceData)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
2013-11-26 10:55:51 +01:00
|
|
|
//Add new reference
|
|
|
|
resourcePrivate.SaveResource(resourceData, false);
|
|
|
|
return resourceData->GetResourceHandle();
|
2013-11-22 10:46:25 +01:00
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
|
|
|
|
resourceData = OResource::Load(filename, type);
|
|
|
|
|
|
|
|
if(!resourceData) return 0;
|
|
|
|
|
|
|
|
resourcePrivate.SaveResource(resourceData);
|
|
|
|
|
|
|
|
return resourceData->GetResourceHandle();
|
2013-11-22 10:46:25 +01:00
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
OHRESOURCE OysterResource::LoadResource(const wchar_t filename[], CustomLoadFunction loadFnc, unsigned int CustomId)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
2013-11-26 10:55:51 +01:00
|
|
|
if(!filename) return 0;
|
|
|
|
if(!loadFnc) return 0;
|
|
|
|
|
2013-11-22 10:46:25 +01:00
|
|
|
OResource *resourceData = resourcePrivate.FindResource(filename);
|
|
|
|
if(resourceData)
|
|
|
|
{
|
2013-11-26 10:55:51 +01:00
|
|
|
//Add new reference
|
2013-11-22 10:46:25 +01:00
|
|
|
resourcePrivate.SaveResource(resourceData, false);
|
|
|
|
return resourceData->GetResourceHandle();
|
|
|
|
}
|
|
|
|
|
2013-11-26 10:55:51 +01:00
|
|
|
resourceData = OResource::Load(filename, loadFnc);
|
2013-11-22 10:46:25 +01:00
|
|
|
|
2013-11-26 10:55:51 +01:00
|
|
|
if(!resourceData) return 0;
|
|
|
|
|
|
|
|
if(resourceData) resourceData->SetResourceID(CustomId);
|
2013-11-22 10:46:25 +01:00
|
|
|
resourcePrivate.SaveResource(resourceData);
|
|
|
|
|
2013-11-26 10:55:51 +01:00
|
|
|
return (OHRESOURCE)resourceData->GetResourceHandle();
|
2013-11-22 10:46:25 +01:00
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
|
|
|
|
OHRESOURCE 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)
|
|
|
|
{
|
|
|
|
OResource *resourceData = resourcePrivate.FindResource(resource);
|
|
|
|
if(!resourceData) return 0; //The resource has not been loaded
|
|
|
|
|
|
|
|
return OResource::Reload(resourceData)->GetResourceHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OysterResource::Clean()
|
|
|
|
{
|
|
|
|
auto i = resourcePrivate.resources.begin();
|
|
|
|
auto last = resourcePrivate.resources.end();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void OysterResource::ReleaseResource(const OHRESOURCE& resourceData)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(resourceData);
|
|
|
|
if(t)
|
|
|
|
{
|
2013-11-26 10:55:51 +01:00
|
|
|
if(OResource::Release(t))
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
|
|
|
const wchar_t* temp = t->GetResourceFilename();
|
|
|
|
delete resourcePrivate.resources[temp];
|
|
|
|
resourcePrivate.resources.erase(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
|
|
|
|
void OysterResource::SetResourceId (const OHRESOURCE& resourceData, unsigned int id)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(resourceData);
|
|
|
|
|
|
|
|
if(t) t->SetResourceID(id);
|
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
ResourceType OysterResource::GetResourceType (const OHRESOURCE& resourceData)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(resourceData);
|
|
|
|
|
|
|
|
if(t) return t->GetResourceType();
|
|
|
|
|
|
|
|
return ResourceType_UNKNOWN;
|
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
const wchar_t* OysterResource::GetResourceFilename (const OHRESOURCE& resourceData)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(resourceData);
|
|
|
|
|
|
|
|
if(t) return t->GetResourceFilename();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
unsigned int OysterResource::GetResourceId (const OHRESOURCE& resourceData)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(resourceData);
|
|
|
|
|
|
|
|
if(t) return t->GetResourceID();
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OResource* ResourcePrivate::FindResource(const OHRESOURCE& h) const
|
|
|
|
{
|
|
|
|
for (auto i = this->resources.begin(); i != this->resources.end() ; i++)
|
|
|
|
{
|
|
|
|
if(i->second->GetResourceHandle() == h)
|
|
|
|
{
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
OResource* ResourcePrivate::FindResource(const wchar_t c[]) const
|
|
|
|
{
|
|
|
|
std::wstring temp = c;
|
|
|
|
auto t = this->resources.find(c);
|
|
|
|
if(t == this->resources.end()) return 0;
|
|
|
|
|
|
|
|
return t->second;
|
|
|
|
}
|
|
|
|
void ResourcePrivate::SaveResource( OResource* r, bool addNew )
|
|
|
|
{
|
|
|
|
if(!r) return;
|
|
|
|
|
2013-11-26 10:55:51 +01:00
|
|
|
if(addNew)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
|
|
|
this->resources[r->GetResourceFilename()] = r;
|
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
|
|
|
|
r->resourceRef.Incref();
|
2013-11-22 10:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|