aboutsummaryrefslogtreecommitdiff
path: root/engines/illusions/resourcesystem.cpp
blob: 9e81f1f9df8b69d0ef6470666d0692d48ec62bde (plain)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "illusions/resourcesystem.h"
#include "illusions/illusions.h"

#include "common/algorithm.h"
#include "common/debug.h"

namespace Illusions {

// ResourceInstance

void ResourceInstance::load(Resource *resource) {
}

void ResourceInstance::unload() {
}

void ResourceInstance::pause() {
}

void ResourceInstance::unpause() {
}

ResourceInstance::~ResourceInstance() {
}

// Resource

void Resource::loadData() {
	debug("Resource::loadData()");
	
	Common::File fd;
	if (!fd.open(_filename))
		error("Resource::loadData() Could not open %s for reading", _filename.c_str());
	_dataSize = fd.size();
	_data = (byte*)malloc(_dataSize);
	fd.read(_data, _dataSize);
	debug("Resource::loadData() OK");
}

void Resource::unloadData() {
	debug("Resource::unloadData()");
	
	free(_data);
	_data = 0;
	_dataSize = 0;
}

// ResourceSystem

ResourceSystem::ResourceSystem(IllusionsEngine *vm)
	: _vm(vm) {
}

ResourceSystem::~ResourceSystem() {
	// Delete all registered resource loaders
	for (ResourceLoadersMapIterator it = _resourceLoaders.begin(); it != _resourceLoaders.end(); ++it)
		delete (*it)._value;
}

void ResourceSystem::addResourceLoader(uint32 resTypeId, BaseResourceLoader *resourceLoader) {
	_resourceLoaders[resTypeId] = resourceLoader;
}

void ResourceSystem::loadResource(uint32 resId, uint32 tag, uint32 threadId) {
	debug("ResourceSystem::loadResource(%08X, %08X, %08X)", resId, tag, threadId);
	BaseResourceLoader *resourceLoader = getResourceLoader(resId);

	Resource *resource = new Resource();
	resource->_loaded = false;
	resource->_resId = resId;
	resource->_tag = tag;
	resource->_threadId = threadId;
	resource->_gameId = _vm->getGameId();

	resourceLoader->buildFilename(resource);

	if (resourceLoader->isFlag(kRlfLoadFile)) {
		debug("ResourceSystem::loadResource() kRlfLoadFile");
		resource->loadData();
	}
	
	resourceLoader->load(resource);
	
	if (resourceLoader->isFlag(kRlfFreeDataAfterLoad)) {
		debug("ResourceSystem::loadResource() kRlfFreeDataAfterLoad");
		resource->unloadData();
	}
	
	resource->_loaded = true;

	_resources.push_back(resource);
	// TODO? Not sure if this is needed krnfileAdd(filenameb, taga);

}

void ResourceSystem::unloadResourceById(uint32 resId) {
	Resource *resource = getResource(resId);
	if (resource) 
		unloadResource(resource);
}

void ResourceSystem::unloadResourcesByTag(uint32 tag) {
	ResourcesArrayIterator it = Common::find_if(_resources.begin(), _resources.end(), ResourceEqualByTag(tag));
	while (it != _resources.end()) {
		unloadResource(*it);
		it = Common::find_if(it, _resources.end(), ResourceEqualByTag(tag));
	}
}

void ResourceSystem::unloadSceneResources(uint32 sceneId1, uint32 sceneId2) {
	ResourcesArrayIterator it = Common::find_if(_resources.begin(), _resources.end(), ResourceNotEqualByScenes(sceneId1, sceneId2));
	while (it != _resources.end()) {
		unloadResource(*it);
		it = Common::find_if(it, _resources.end(), ResourceNotEqualByScenes(sceneId1, sceneId2));
	}
}

BaseResourceLoader *ResourceSystem::getResourceLoader(uint32 resId) {
	ResourceLoadersMapIterator it = _resourceLoaders.find(ResourceTypeId(resId));
	if (it != _resourceLoaders.end())
		return (*it)._value;
	error("ResourceSystem::getResourceLoader() Could not find resource loader for resource id %08X", resId);
}

Resource *ResourceSystem::getResource(uint32 resId) {
	ResourcesArrayIterator it = Common::find_if(_resources.begin(), _resources.end(), ResourceEqualById(resId));
	return it != _resources.end() ? *it : 0;
}

void ResourceSystem::unloadResource(Resource *resource) {
	debug("Unloading %08X... (tag: %08X)", resource->_resId, resource->_tag);
	ResourcesArrayIterator it = Common::find_if(_resources.begin(), _resources.end(), ResourceEqualByValue(resource));
	if (it != _resources.end())
		_resources.remove_at(it - _resources.begin());
	delete resource;
}

} // End of namespace Illusions