File: resource_manager_generic.cpp

package info (click to toggle)
clanlib 1.0~svn3827-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 24,696 kB
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,742; ansic: 463; perl: 424; php: 247; sh: 53
file content (473 lines) | stat: -rw-r--r-- 13,932 bytes parent folder | download | duplicates (7)
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
/*
**  ClanLib SDK
**  Copyright (c) 1997-2005 The ClanLib Team
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
**  Note: Some of the libraries ClanLib may link to may have additional
**  requirements or restrictions.
**
**  File Author(s):
**
**    Magnus Norddahl
**    (if your name is missing here, please add it)
*/

#include <iterator>
#include "Core/precomp.h"
#include "resource_manager_generic.h"
#include "resource_generic.h"
#include "API/Core/IOData/inputsource_provider_file.h"
#include "API/Core/System/error.h"
#include "API/Core/System/clanstring.h"
#include "API/Core/IOData/zip_archive.h"
#include "API/Core/XML/dom_node_list.h"

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Construction:

CL_ResourceManager_Generic::CL_ResourceManager_Generic() : provider(0), delete_provider(false)
{
}

CL_ResourceManager_Generic::~CL_ResourceManager_Generic()
{
	if (delete_provider) delete provider;
}

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Attributes:

bool CL_ResourceManager_Generic::exists(const std::string &res_id)
{
	if (resources.find(res_id) != resources.end()) return true;

	// Search in all additional resources:
	std::list<CL_ResourceManager>::iterator it;
	for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
	{
		if (it->exists(res_id)) return true;
	}

	return false;
}

CL_Resource &CL_ResourceManager_Generic::get_resource(const std::string &res_id)
{
	std::map<std::string, CL_Resource>::iterator it = resources.find(res_id);
	if (it == resources.end())
	{
		std::list<CL_ResourceManager>::iterator it2;
		for (it2 = additional_resources.begin(); it2 != additional_resources.end(); ++it2)
		{
			CL_ResourceManager &additional_manager = *it2;
			if (additional_manager.exists(res_id))
			{
				return additional_manager.get_resource(res_id, false);
			}
		}

		throw CL_Error(CL_String::format("Resource '%1' not found", res_id));
	}
	return it->second;
}

std::vector<std::string> CL_ResourceManager_Generic::get_all_resources(const std::string &original_section_name)
{
	std::vector<std::string> result;

	// Make sure we have a trailing / in section
	std::string section_name = add_trailing_slash(original_section_name);

	// Add local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->first.substr(0, section_name.length()) == section_name)
			{
				result.push_back(it->first);
			}
		}
	}

	// Add resources for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::vector<std::string> additionals = it->get_all_resources(section_name);
			std::copy(additionals.begin(), additionals.end(), std::back_inserter(result));
		}
	}

	return result;
}

std::vector<std::string> CL_ResourceManager_Generic::get_all_resources()
{
	std::vector<std::string> result;

	// Add local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			result.push_back(it->first);
		}
	}

	// Add resources for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::vector<std::string> additionals = it->get_all_resources();
			std::copy(additionals.begin(), additionals.end(), std::back_inserter(result));
		}
	}

	return result;
}

std::vector<std::string> CL_ResourceManager_Generic::get_all_sections()
{
	std::vector<std::string> result;

	// Add local sections:
	{
		CL_DomElement element = document.named_item("resources").to_element();
		add_sections(result, element);
	}

	// Add sections for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::vector<std::string> additionals = it->get_all_sections();
			std::copy(additionals.begin(), additionals.end(), std::back_inserter(result));
		}
	}

	return result;
}

std::vector<std::string> CL_ResourceManager_Generic::get_sections(const std::string &section_name)
{
	std::vector<std::string> result;

	// Add local sections:
	{
		CL_DomElement element = document.named_item("resources").to_element();
		add_section(result, element, section_name);
	}

	// Add sections for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::vector<std::string> additionals = it->get_sections(section_name);
			std::copy(additionals.begin(), additionals.end(), std::back_inserter(result));
		}
	}

	return result;
}

std::vector<std::string> CL_ResourceManager_Generic::get_resources_of_type(const std::string &type_id)
{
	std::vector<std::string> result;

	// Add local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->second.get_type() == type_id) result.push_back(it->first);
		}
	}

	// Add resources for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::vector<std::string> additionals = it->get_resources_of_type(type_id);
                        std::copy(additionals.begin(), additionals.end(), std::back_inserter(result));
		}
	}

	return result;
}

std::vector<std::string> CL_ResourceManager_Generic::get_resources_of_type(const std::string &type_id, const std::string &original_section_name)
{
	std::vector<std::string> result;

	// Make sure we have a trailing / in section
	std::string section_name = add_trailing_slash(original_section_name);

	// Add local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->first.substr(0, section_name.length()) == section_name)
			{
				if (it->second.get_type() == type_id) result.push_back(it->first);
			}
		}
	}

	// Add resources for all additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			std::vector<std::string> additionals = it->get_resources_of_type(type_id, section_name);
                        std::copy(additionals.begin(), additionals.end(), std::back_inserter(result));
		}
	}

	return result;
}

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Signals:

CL_Signal_v1<CL_Resource &> CL_ResourceManager_Generic::sig_resource_added;

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Operations:

void CL_ResourceManager_Generic::load(
	const std::string &config_file,
	CL_InputSourceProvider *input_provider,
	bool delete_inputsource_provider)
{
	if (delete_provider)
		delete provider;

	provider = input_provider;
	delete_provider = delete_inputsource_provider;

	if (provider == 0)
	{
		provider = new CL_InputSourceProvider_File(CL_String::get_path(config_file));
		delete_provider = true;
	}
	else
	{
		CL_InputSourceProvider *new_provider = provider->create_relative(CL_String::get_path(config_file));
		if (delete_provider) delete provider;
		provider = new_provider;
		delete_provider = true;
	}

	try
	{
		document.load(provider->open_source(CL_String::get_filename(config_file)), true);
	}
	catch( CL_Error err )
	{
		// a hack to report the filename of the malformed xml file
		err.message.insert(0, config_file + ": " );
		throw err;
	}

	CL_DomElement root = document.named_item("resources").to_element();
	if (root.is_null()) throw CL_Error(config_file + ": Not a valid XML resource file.");
	parse_section(root);

	std::map<std::string, CL_Resource>::iterator it;
	for (it = resources.begin(); it != resources.end(); ++it)
	{
		sig_resource_added(it->second);
	}
}

void CL_ResourceManager_Generic::add_resources(const CL_ResourceManager &resources)
{
	additional_resources.push_back(resources);
}

void CL_ResourceManager_Generic::remove_resources(const CL_ResourceManager &resources)
{
	additional_resources.remove(resources);
}

void CL_ResourceManager_Generic::load_all()
{
	load_section(std::string());
}

void CL_ResourceManager_Generic::unload_all()
{
	unload_section(std::string());
}

void CL_ResourceManager_Generic::load_section(const std::string &original_section_name)
{
	// Make sure we have a trailing / in section
	std::string section_name = add_trailing_slash(original_section_name);

	// Load local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->first.substr(0, section_name.length()) == section_name)
			{
				it->second.load();
			}
		}
	}

	// Load additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			CL_ResourceManager &additional_manager = *it;
			additional_manager.load_section(section_name);
		}
	}
}

void CL_ResourceManager_Generic::unload_section(const std::string &original_section_name)
{
	// Make sure we have a trailing / in section
	std::string section_name = add_trailing_slash(original_section_name);

	// Load local resources:
	{
		std::map<std::string, CL_Resource>::iterator it;
		for (it = resources.begin(); it != resources.end(); ++it)
		{
			if (it->first.substr(0, section_name.length()) == section_name)
			{
				it->second.unload();
			}
		}
	}

	// Load additional resources:
	{
		std::list<CL_ResourceManager>::iterator it;
		for (it = additional_resources.begin(); it != additional_resources.end(); ++it)
		{
			CL_ResourceManager &additional_manager = *it;
			additional_manager.unload_section(section_name);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CL_ResourceManager_Generic Implementation:

void CL_ResourceManager_Generic::parse_section(CL_DomElement &sectionElement, const std::string &prefix)
{
	std::string path = prefix + sectionElement.get_attribute("name") + "/";
	if (sectionElement.get_tag_name() == "resources") path = prefix;

	for (CL_DomNode node = sectionElement.get_first_child(); !node.is_null(); node = node.get_next_sibling())
	{
		if (!node.is_element()) continue;

		CL_DomElement element = node.to_element();
		if (element.get_tag_name() == "section")
		{
			parse_section(element, path);
		}
		else if(element.get_tag_name() == "include")
		{
			// get the file name and provider name
			std::string file_name = element.get_attribute("file");
			std::string zip_name = element.get_attribute("provider");
			bool local = element.get_attribute("local","false") == "true";

			CL_ResourceManager *additional_resource;
			if(local)
			{
				file_name = provider->get_pathname(file_name);
				additional_resource = new CL_ResourceManager(file_name, provider);
			}
			else if(zip_name != "")
			{
				CL_Zip_Archive *arch = new CL_Zip_Archive(zip_name);
				file_name = arch->get_pathname(file_name);
				additional_resource = new CL_ResourceManager(file_name, arch);
			}
			else
			{
				file_name = provider->get_pathname(file_name);
				additional_resource = new CL_ResourceManager(file_name);
			}

			add_resources(*additional_resource);
		}
		else
		{
			CL_Resource resource(CL_SharedPtr<CL_Resource_Generic>(new CL_Resource_Generic(element, self)));
			resources[path + element.get_attribute("name")] = resource;
		}
	}
}

void CL_ResourceManager_Generic::add_sections(std::vector<std::string> &returnList, CL_DomElement &sectionElement, const std::string &prefix)
{
	std::string path = prefix + sectionElement.get_attribute("name") + "/";
	if (sectionElement.get_tag_name() == "resources") path = prefix;

	for (CL_DomNode node = sectionElement.get_first_child(); !node.is_null(); node = node.get_next_sibling())
	{
		if (!node.is_element()) continue;

		CL_DomElement element = node.to_element();
		if (element.get_tag_name() == "section")
		{
			returnList.push_back(path + element.get_attribute("name"));
			add_sections(returnList, element, path);
		}
	}
}

void CL_ResourceManager_Generic::add_section(std::vector<std::string> &returnList, CL_DomElement &sectionElement, const std::string &section_name)
{
	for (CL_DomNode node = sectionElement.get_first_child(); !node.is_null(); node = node.get_next_sibling())
	{
		if (!node.is_element()) continue;

		CL_DomElement element = node.to_element();
		if (element.get_tag_name() == "section")
		{
			if (sectionElement.get_attribute("name") == section_name)
				returnList.push_back(element.get_attribute("name"));
			add_section(returnList, element, section_name);
		}
	}
}

std::string CL_ResourceManager_Generic::add_trailing_slash(const std::string &string)
{
	if(string.length() && string.at(string.length() - 1) != '/')
		return string + '/';
	else
		return string;
}