1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
$Id: resource.cpp,v 1.16 2002/01/15 16:00:39 mbn Exp $
------------------------------------------------------------------------
ClanLib, the platform independent game SDK.
This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
version 2. See COPYING for details.
For a total list of contributers see CREDITS.
------------------------------------------------------------------------
*/
#include "Core/precomp.h"
#include "API/Core/Resources/resource.h"
#include "API/Core/Resources/resourceoptions.h"
#include "API/Core/Resources/resourcedata.h"
#include "API/Core/Resources/resource_manager.h"
#include "API/Core/IOData/inputsource_provider.h"
#include "API/Core/IOData/inputsource_provider_file.h"
#include "API/Core/IOData/inputsource.h"
#include "API/Core/System/cl_assert.h"
#include "Core/Resources/resource_manager_file.h"
#include <ctype.h>
#include <algorithm>
/////////////////////////////////////////////////////////////////////////////
// CL_Resource_Generic:
class CL_Resource_Generic
{
public:
CL_Signal_v0 sig_load_file;
CL_Signal_v1<CL_InputSourceProvider *> sig_load_datafile;
CL_Signal_v1<CL_OutputSourceProvider *> sig_save_datafile;
CL_Signal_v0 sig_unload;
std::map<std::string, CL_ResourceData*> datas;
std::string type, name, location;
CL_ResourceOptions options;
int ref_count, load_ref;
CL_ResourceManager_File *manager;
CL_Resource_Generic()
: ref_count(0), load_ref(0), manager(NULL)
{
}
~CL_Resource_Generic()
{
std::map<std::string, CL_ResourceData*>::iterator it(datas.begin());
std::map<std::string, CL_ResourceData*>::iterator it_end(datas.end());
for (; it != it_end; ++it)
delete it->second;
}
void add_ref()
{
ref_count++;
}
void release_ref()
{
ref_count--;
if (ref_count == 0) delete this;
}
};
/////////////////////////////////////////////////////////////////////////////
// CL_Resource construction:
CL_Resource::CL_Resource(
const std::string &type,
const std::string &name,
const std::string &location,
const CL_ResourceOptions &options,
const CL_ResourceManager &manager)
: impl(NULL)
{
std::string lower_name = name;
std::transform(lower_name.begin(), lower_name.end(), lower_name.begin(), tolower);
impl = new CL_Resource_Generic;
impl->add_ref();
impl->type = type;
impl->name = lower_name;
impl->location = location;
impl->options = options;
impl->manager = manager.impl;
}
CL_Resource::CL_Resource(const CL_Resource ©)
: impl(NULL)
{
impl = copy.impl;
if (impl) impl->add_ref();
}
CL_Resource::CL_Resource()
: impl(NULL)
{
impl = new CL_Resource_Generic;
impl->add_ref();
}
CL_Resource::CL_Resource(class CL_Resource_Generic *impl)
: impl(impl)
{
if (impl) impl->add_ref();
}
CL_Resource::~CL_Resource()
{
if (impl) impl->release_ref();
}
/////////////////////////////////////////////////////////////////////////////
// CL_Resource attributes:
const std::string &CL_Resource::get_type() const
{
return impl->type;
}
const std::string &CL_Resource::get_name() const
{
return impl->name;
}
const std::string &CL_Resource::get_location() const
{
return impl->location;
}
std::string CL_Resource::get_full_location() const
{
CL_InputSourceProvider_File *file_provider = (CL_InputSourceProvider_File *) impl->manager->get_resource_provider();
return file_provider->get_path(get_location().c_str());
}
CL_ResourceOptions &CL_Resource::get_options()
{
return impl->options;
}
CL_ResourceManager CL_Resource::get_manager()
{
return CL_ResourceManager(impl->manager);
}
CL_ResourceData *CL_Resource::get_data(const std::string &name)
{
return impl->datas[name];
}
int CL_Resource::get_reference_count() const
{
return impl->load_ref;
}
/////////////////////////////////////////////////////////////////////////////
// CL_Resource signals:
CL_Signal_v0 &CL_Resource::sig_load_file()
{
return impl->sig_load_file;
}
CL_Signal_v1<CL_InputSourceProvider *> &CL_Resource::sig_load_datafile()
{
return impl->sig_load_datafile;
}
CL_Signal_v1<CL_OutputSourceProvider *> &CL_Resource::sig_save_datafile()
{
return impl->sig_save_datafile;
}
CL_Signal_v0 &CL_Resource::sig_unload()
{
return impl->sig_unload;
}
/////////////////////////////////////////////////////////////////////////////
// CL_Resource operations:
void CL_Resource::operator = (const CL_Resource ©)
{
if (impl) impl->release_ref();
impl = copy.impl;
if (impl) impl->add_ref();
}
void CL_Resource::attach_data(const std::string &name, CL_ResourceData *data)
{
impl->datas[name] = data;
}
void CL_Resource::detach_data(CL_ResourceData *data)
{
std::map<std::string, CL_ResourceData *>::iterator it;
for (it = impl->datas.begin(); it != impl->datas.end(); it++)
{
if (it->second == data)
{
impl->datas.erase(it);
break;
}
}
}
void CL_Resource::unload()
{
if (!impl) return;
if (impl->manager == 0) return;
impl->load_ref--;
cl_assert(impl->load_ref >= 0);
if (impl->load_ref == 0)
{
impl->sig_unload();
}
}
void CL_Resource::load()
{
if (!impl) return;
if (impl->manager == 0) return;
if (impl->manager->from_source) load_file();
else
{
// CL_InputSource *inputsource = impl->manager->get_resource_provider()->open_source(get_name().c_str());
load_datafile(impl->manager->get_resource_provider());
// delete inputsource;
}
}
void CL_Resource::load_file()
{
if (!impl) return;
impl->load_ref++;
if (impl->load_ref == 1) impl->sig_load_file();
}
void CL_Resource::load_datafile(CL_InputSourceProvider *input)
{
if (!impl) return;
impl->load_ref++;
if (impl->load_ref == 1)
{
impl->sig_load_datafile(input);
}
}
void CL_Resource::save_datafile(CL_OutputSourceProvider *output)
{
if (!impl) return;
impl->sig_save_datafile(output);
}
/////////////////////////////////////////////////////////////////////////////
// CL_Resource implementation:
|