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-28 13:43:11 +01:00
|
|
|
OHRESOURCE OysterResource::LoadResource(const wchar_t* filename, ResourceType type, int customID, bool force)
|
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);
|
2013-11-28 13:43:11 +01:00
|
|
|
|
2013-11-26 10:55:51 +01:00
|
|
|
if(resourceData)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
2013-11-28 13:43:11 +01:00
|
|
|
if(force)
|
|
|
|
{
|
|
|
|
return OysterResource::ReloadResource(filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Add new reference
|
|
|
|
resourcePrivate.SaveResource(resourceData, false);
|
|
|
|
return resourceData->GetResourceHandle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resourceData = OResource::Load(filename, type);
|
|
|
|
if(resourceData)
|
|
|
|
{
|
|
|
|
resourceData->SetResourceID(customID);
|
|
|
|
resourcePrivate.SaveResource(resourceData);
|
|
|
|
}
|
2013-11-22 10:46:25 +01:00
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
|
|
|
|
return resourceData->GetResourceHandle();
|
2013-11-22 10:46:25 +01:00
|
|
|
}
|
2013-11-28 13:43:11 +01:00
|
|
|
OHRESOURCE OysterResource::LoadResource(const wchar_t filename[], CustomLoadFunction loadFnc, int customId, bool force)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
2013-11-28 13:43:11 +01:00
|
|
|
if(!filename)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!loadFnc)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-26 10:55:51 +01:00
|
|
|
|
2013-11-22 10:46:25 +01:00
|
|
|
OResource *resourceData = resourcePrivate.FindResource(filename);
|
|
|
|
if(resourceData)
|
|
|
|
{
|
2013-11-28 13:43:11 +01:00
|
|
|
if(force)
|
|
|
|
{
|
|
|
|
return OysterResource::ReloadResource(filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Add new reference
|
|
|
|
resourcePrivate.SaveResource(resourceData, false);
|
|
|
|
return resourceData->GetResourceHandle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
resourceData = OResource::Load(filename, loadFnc);
|
|
|
|
if(resourceData)
|
|
|
|
{
|
|
|
|
resourceData->SetResourceID(customId);
|
|
|
|
resourcePrivate.SaveResource(resourceData);
|
|
|
|
}
|
2013-11-22 10:46:25 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-11-27 21:12:37 +01:00
|
|
|
OHRESOURCE OysterResource::ReloadResource(const wchar_t filename[])
|
2013-11-26 10:55:51 +01:00
|
|
|
{
|
|
|
|
OResource *resourceData = resourcePrivate.FindResource(filename);
|
|
|
|
if(!resourceData) return 0; //The resource has not been loaded
|
|
|
|
|
|
|
|
return OResource::Reload(resourceData)->GetResourceHandle();
|
|
|
|
}
|
2013-11-27 21:12:37 +01:00
|
|
|
OHRESOURCE OysterResource::ReloadResource(OHRESOURCE resource)
|
2013-11-26 10:55:51 +01:00
|
|
|
{
|
|
|
|
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++)
|
|
|
|
{
|
2013-11-27 21:12:37 +01:00
|
|
|
//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)
|
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(resourceData);
|
|
|
|
if(t)
|
|
|
|
{
|
|
|
|
if(OResource::Release(t))
|
2013-11-26 10:55:51 +01:00
|
|
|
{
|
2013-11-27 21:12:37 +01:00
|
|
|
const wchar_t* temp = t->GetResourceFilename();
|
2013-11-26 10:55:51 +01:00
|
|
|
delete resourcePrivate.resources[temp];
|
|
|
|
resourcePrivate.resources.erase(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-27 21:12:37 +01:00
|
|
|
void OysterResource::ReleaseResource(const wchar_t filename[])
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
2013-11-27 21:12:37 +01:00
|
|
|
OResource* t = resourcePrivate.FindResource(filename);
|
2013-11-22 10:46:25 +01:00
|
|
|
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-27 21:12:37 +01:00
|
|
|
void OysterResource::SetResourceId(const wchar_t c[], unsigned int id)
|
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(c);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2013-11-27 21:12:37 +01:00
|
|
|
return ResourceType_INVALID;
|
|
|
|
}
|
|
|
|
ResourceType OysterResource::GetResourceType (const wchar_t c[])
|
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(c);
|
|
|
|
|
|
|
|
if(t) return t->GetResourceType();
|
|
|
|
|
|
|
|
return ResourceType_INVALID;
|
2013-11-22 10:46:25 +01:00
|
|
|
}
|
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-27 21:12:37 +01:00
|
|
|
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)
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(resourceData);
|
|
|
|
|
|
|
|
if(t) return t->GetResourceID();
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-27 21:12:37 +01:00
|
|
|
int OysterResource::GetResourceId(const wchar_t c[])
|
|
|
|
{
|
|
|
|
OResource* t = resourcePrivate.FindResource(c);
|
2013-11-22 10:46:25 +01:00
|
|
|
|
2013-11-27 21:12:37 +01:00
|
|
|
if(t) return t->GetResourceID();
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-22 10:46:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|