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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
/*
$Id: resource_manager_file.cpp,v 1.22 2002/01/09 09:54:31 grumbel 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 <API/Core/Resources/resource.h>
#include "Core/precomp.h"
#include "resource_manager_file.h"
#include "resource_tokenizer.h"
#include <API/Core/System/clanstring.h>
#include <API/Core/System/error.h>
#include <API/Core/System/cl_assert.h>
#include <API/Core/IOData/inputsource_provider_file.h>
#include <API/Core/IOData/inputsource.h>
#include <API/Core/IOData/inputsource.h>
#include <API/Core/Resources/resourcetype.h>
#include <algorithm>
#include <ctype.h>
CL_ResourceManager_File::CL_ResourceManager_File()
: resource_provider(0), ref_count(1), delete_resource_provider(false)
{
}
CL_ResourceManager_File::CL_ResourceManager_File(
const std::string &config_file,
CL_InputSourceProvider *provider,
bool read_directly_from_source,
bool delete_inputsource_provider)
: ref_count(1)
{
filename = config_file;
resource_provider = provider;
delete_resource_provider = delete_inputsource_provider;
from_source = read_directly_from_source;
if (provider == NULL)
{
if (read_directly_from_source)
{
resource_provider = CL_InputSourceProvider::create_file_provider(get_path(filename).c_str());
filename = get_filename(filename); // cut off path of filename
}
else
{
resource_provider = CL_InputSourceProvider::create_datafile_provider(filename.c_str());
}
delete_resource_provider = true;
}
parse();
}
CL_ResourceManager_File::CL_ResourceManager_File(
const std::string &file_name,
const bool is_datafile)
: ref_count(1)
{
resource_provider = NULL;
filename = file_name;
delete_resource_provider = true;
if (is_datafile)
{
resource_provider = CL_InputSourceProvider::create_datafile_provider(filename.c_str());
from_source = false;
}
else
{
resource_provider = CL_InputSourceProvider::create_file_provider(get_path(filename).c_str());
filename = get_filename(filename); // cut off path of filename
from_source = true;
}
parse();
}
CL_ResourceManager_File::~CL_ResourceManager_File()
{
std::list<CL_ResourceManager_File *>::iterator it;
for (it = additional_resources.begin(); it != additional_resources.end(); it++)
{
CL_ResourceManager_File *cur = *it;
cur->release_ref();
}
if (delete_resource_provider)
{
delete resource_provider;
resource_provider = NULL;
}
/*
bool first_msg = true;
for (
std::list<CL_Resource>::iterator it = resources.begin();
it != resources.end();
it++)
{
CL_Resource &res = *it;
if (res.get_reference_count() != 0)
{
if (first_msg)
{
std::cout << std::endl;
std::cout << "Not all resources in " << filename.c_str() << " was unloaded!" << std::endl;
first_msg = false;
}
std::cout << res.get_name().c_str() << " had load count " << res.get_reference_count() << std::endl;
}
}
if (!first_msg)
{
std::cout << std::endl;
std::cout << "Please check your program for memory leaks!" << std::endl;
}
*/
}
void CL_ResourceManager_File::add_ref()
{
ref_count++;
}
void CL_ResourceManager_File::release_ref()
{
ref_count--;
if (ref_count == 0) delete this;
}
void CL_ResourceManager_File::add_resources(CL_ResourceManager_File *resources)
{
resources->add_ref();
additional_resources.push_back(resources);
}
void CL_ResourceManager_File::remove_resources(CL_ResourceManager_File *resources)
{
std::list<CL_ResourceManager_File *>::iterator it = std::find(additional_resources.begin(), additional_resources.end(), resources);
if (it != additional_resources.end())
{
additional_resources.erase(it);
resources->release_ref();
}
}
void CL_ResourceManager_File::load_all()
{
std::list<CL_Resource>::iterator it_end(resources.end());
for (
std::list<CL_Resource>::iterator it = resources.begin();
it != it_end;
++it)
{
it->load();
}
std::list<CL_ResourceManager_File *>::iterator it_additional;
for (it_additional = additional_resources.begin(); it_additional != additional_resources.end(); it_additional++)
{
CL_ResourceManager_File *cur = *it_additional;
cur->load_all();
}
}
void CL_ResourceManager_File::unload_all()
{
std::list<CL_Resource>::iterator it_end(resources.end());
for (
std::list<CL_Resource>::iterator it = resources.begin();
it != it_end;
++it)
{
it->unload();
}
std::list<CL_ResourceManager_File *>::iterator it_additional;
for (it_additional = additional_resources.begin(); it_additional != additional_resources.end(); it_additional++)
{
CL_ResourceManager_File *cur = *it_additional;
cur->unload_all();
}
}
void CL_ResourceManager_File::load_section(const std::string §ion_name)
{
CL_String prefix = section_name;
prefix << "/";
int prefix_len = prefix.get_length();
std::list<CL_Resource>::iterator it_end(resources.end());
for (
std::list<CL_Resource>::iterator it = resources.begin();
it != it_end;
++it)
{
CL_String name = it->get_name();
if (name.mid(0, prefix_len) == prefix)
{
it->load();
}
}
std::list<CL_ResourceManager_File *>::iterator it_additional;
for (it_additional = additional_resources.begin(); it_additional != additional_resources.end(); it_additional++)
{
CL_ResourceManager_File *cur = *it_additional;
cur->load_section(section_name);
}
}
void CL_ResourceManager_File::unload_section(const std::string §ion_name)
{
CL_String prefix = section_name;
prefix << "/";
int prefix_len = prefix.get_length();
std::list<CL_Resource>::iterator it_end(resources.end());
for (
std::list<CL_Resource>::iterator it = resources.begin();
it != it_end;
++it)
{
CL_String name = it->get_name();
if (name.mid(0, prefix_len) == prefix)
{
it->unload();
}
}
std::list<CL_ResourceManager_File *>::iterator it_additional;
for (it_additional = additional_resources.begin(); it_additional != additional_resources.end(); it_additional++)
{
CL_ResourceManager_File *cur = *it_additional;
cur->unload_section(section_name);
}
}
char tol(char ch)
{
if('A' <= ch && ch <= 'Z')
return ch += 0x20;
else
return ch;
}
CL_Resource &CL_ResourceManager_File::find_resource(const std::string &res_id, bool &found)
{
static CL_Resource null_resource;
std::string lower_res_id = res_id;
std::transform(lower_res_id.begin(), lower_res_id.end(), lower_res_id.begin(), tol);
//std::transform(lower_res_id.begin(), lower_res_id.end(), lower_res_id.begin(), tolower);
std::list<CL_Resource>::iterator it_end(resources.end());
for (
std::list<CL_Resource>::iterator it = resources.begin();
it != it_end;
++it)
{
found = true;
if (it->get_name() == lower_res_id) return *it;
}
found = false;
return null_resource;
}
CL_Resource &CL_ResourceManager_File::get_resource(const std::string &res_id)
{
bool found = false;
CL_Resource &local_resource = find_resource(res_id, found);
if (found) return local_resource;
std::list<CL_ResourceManager_File *>::iterator it_additional;
for (it_additional = additional_resources.begin(); it_additional != additional_resources.end(); it_additional++)
{
CL_ResourceManager_File *cur = *it_additional;
CL_Resource &additional_resource = cur->find_resource(res_id, found);
if (found) return additional_resource;
}
CL_String err;
err << "Resource " << res_id.c_str() << " not found.";
throw CL_Error(err.get_string());
static CL_Resource null_resource;
return null_resource;
}
std::list<std::string> *CL_ResourceManager_File::get_all_resources()
{
std::list<std::string> *retval = new std::list<std::string>;
std::list<CL_Resource>::iterator it_end(resources.end());
for (
std::list<CL_Resource>::iterator it = resources.begin();
it != it_end;
++it)
{
retval->push_back(it->get_name());
}
return retval;
}
std::list<std::string> *CL_ResourceManager_File::get_resources_of_type(std::string type_id)
{
std::list<std::string> *retval = new std::list<std::string>;
std::list<CL_Resource>::iterator it_end(resources.end());
for (
std::list<CL_Resource>::iterator it = resources.begin();
it != it_end;
++it)
{
if (it->get_type() == type_id)
retval->push_back(it->get_name());
}
return retval;
}
CL_InputSourceProvider *CL_ResourceManager_File::get_resource_provider()
{
return resource_provider;
}
void CL_ResourceManager_File::parse()
{
CL_InputSource *input = NULL;
if (from_source) input = resource_provider->open_source(filename.c_str());
else input = resource_provider->open_source(default_scriptfile_id);
try
{
CL_ResourceTokenizer lexer(filename, input);
while (true)
{
std::string token = lexer.get_next_token();
if (token == "") break;
if (token == "location") // obsolete. Will read it but issues a warning.
{
token = lexer.get_next_token();
if (token != "=")
throw CL_Error(lexer.write_error("Missing '=' following global 'location'-declaration"));
token = lexer.get_next_token();
if (token != "datafile")
throw CL_Error(lexer.write_error("Syntax error following global 'location'-declaration"));
token = lexer.get_next_token();
if (token != "(")
throw CL_Error(lexer.write_error("Missing '(' following 'datafile' keyword"));
token = lexer.get_next_token();
token = lexer.get_next_token();
if (token != ")")
throw CL_Error(lexer.write_error("Missing ')' following datafile filename"));
token = lexer.get_next_token();
if (token != ";")
throw CL_Error(lexer.write_error("Missing ';' following datafile filename"));
cl_info(0, "The location keyword is obsolete in resource files. Please don't use it anymore.");
}
else // assume it is a section body (resource or section)
{
parse_section_body(token, lexer, "");
}
}
}
catch (...)
{
delete input;
throw;
}
delete input;
}
void CL_ResourceManager_File::parse_include(CL_ResourceTokenizer &lexer)
{
/* std::string input_file = lexer.get_next_token();
std::string token = lexer.get_next_token();
if (token != ";")
throw CL_Error(lexer.write_error("Missing ';' following include"));
CL_ResourceManager *manager = CL_ResourceManager::create(input_file.c_str(), NULL, true);
*/
/* std::list<std::string> *resource_list = manager->get_all_resources();
for (
std::list<std::string>::iterator it = resource_list->begin();
it != resource_list->end();
it++)
{
CL_Resource *res = manager->get_resource(*it);
if (res != NULL)
{
resources.push_back(res);
}
}
delete resource_list;
delete manager;
*/
}
void CL_ResourceManager_File::parse_section_body(
std::string token,
CL_ResourceTokenizer &lexer,
std::string prefix)
{
if (token == "section")
{
CL_String section_prefix = prefix;
section_prefix << lexer.get_next_token();
section_prefix << "/";
token = lexer.get_next_token();
if (token != "{")
throw CL_Error(lexer.write_error("Missing '{'"));
while (true)
{
token = lexer.get_next_token();
if (token == "}") break;
parse_section_body(token, lexer, section_prefix.get_string());
}
}
else // must be a resource then
{
CL_String name = prefix;
name << token;
token = lexer.get_next_token();
if (token != "=")
{
CL_String err;
err << "Missing '=' following declaration of resource '" << name << "'";
throw CL_Error(lexer.write_error(err.get_string()));
}
// read the location (if available):
std::string location;
token = lexer.get_next_token();
if(token == "(")
location = "";
else {
location = token;
token = lexer.get_next_token();
}
// read resource options:
if (token != "(")
{
CL_String err;
err << "Missing '(' following declaration of resource '" << name << "'";
throw CL_Error(lexer.write_error(err.get_string()));
}
CL_ResourceOptions options;
while (true)
{
token = lexer.get_next_token();
if (token == ")") break;
std::string option_name = token;
token = lexer.get_next_token();
if (token != "=")
{
CL_String err;
err << "Syntax error in options following declaration of resource '" << name << "'";
throw CL_Error(lexer.write_error(err.get_string()));
}
token = lexer.get_next_token();
if (token == "(") // it is a value list
{
CL_ResourceOption option(option_name);
while (true)
{
token = lexer.get_next_token();
if (token == ")") break;
option.add_value(token);
token = lexer.get_next_token();
if (token == ")") break;
if (token != ",")
{
CL_String err;
err << "Missing ',' or ')' following declaration of multiple option values in option '" <<
option_name << "' in resource '" << name << "' (found '" << token << "')";
throw CL_Error(lexer.write_error(err.get_string()));
}
}
options.add(option);
}
else
{
std::string option_value = token;
options.add(CL_ResourceOption(option_name, option_value));
}
token = lexer.get_next_token();
if (token == ")") break;
if (token != ",")
{
CL_String err;
err << "Syntax error in options following declaration of resource '" << name << "'";
throw CL_Error(lexer.write_error(err.get_string()));
}
}
token = lexer.get_next_token();
if (token != ";")
{
CL_String err;
err << "Missing ';' following declaration of resource '" << name << "'";
throw CL_Error(lexer.write_error(err.get_string()));
}
if (options.exists("type") == false)
{
CL_String err;
err << "No type declared for resource '" << name << "'";
throw CL_Error(lexer.write_error(err.get_string()));
}
CL_Resource res = create_resource(name.get_string(), location, options);
resources.push_back(res);
}
}
CL_Resource CL_ResourceManager_File::create_resource(
std::string name,
std::string location,
const CL_ResourceOptions &options)
{
std::string type = options.get_option("type").get_value();
CL_ResourceOptions op;
CL_ResourceManager manage(this);
CL_Resource resource(type, name, location, options, manage);
bool found_type = false;
for (
std::list<CL_ResourceType*>::iterator it = CL_ResourceType::resource_types.begin();
it != CL_ResourceType::resource_types.end();
it++)
{
if ((*it)->get_type() == type)
{
(*it)->connect_data(resource);
found_type = true;
}
}
if (found_type == false)
{
CL_String err;
err << "Unknown type '" << options.get_option("type").get_value().c_str() << "' declared for resource '" << name << "'";
// throw CL_Error(lexer.write_error(err));
throw CL_Error(err.get_string());
}
return resource;
}
std::string CL_ResourceManager_File::get_filename(const std::string &pathname)
{
std::string::size_type pos1 = pathname.find_last_of('/');
std::string::size_type pos2 = pathname.find_last_of('\\');
if (pos1 == std::string::npos && pos2 == std::string::npos)
{
return pathname;
}
else if (pos1 != std::string::npos && pos2 == std::string::npos)
{
return pathname.substr(pos1 + 1);
}
else if (pos1 == std::string::npos && pos2 != std::string::npos)
{
return pathname.substr(pos2 + 1);
}
else // (pos1 != std::string::npos && pos2 != std::string::npos)
{
return pathname.substr(((pos1>pos2) ? pos1 : pos2) + 1);
}
}
std::string CL_ResourceManager_File::get_path(const std::string &pathname)
{
std::string::size_type length = pathname.length();
std::string::size_type pos1 = pathname.find_last_of('/');
std::string::size_type pos2 = pathname.find_last_of('\\');
if (pos1 == std::string::npos) pos1 = length;
if (pos2 == std::string::npos) pos2 = length;
if (pos1 < 0) pos1 = length;
if (pos2 < 0) pos2 = length;
if (pos1 == length && pos2 == length) return std::string();
int pos = (pos1 < pos2) ? pos1 : pos2;
return pathname.substr(0, pos);
}
|