summaryrefslogtreecommitdiff
path: root/src/wmresourcemng.cpp
blob: 4a717531ba74081c116d03c61ff19b0c3db7a533 (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
#include "worldmap.h"




dWMResourceMng_c::dWMResourceMng_c() {
	hasSetPath = false;
	isSetLoaded = false;
	isLoadingComplete = false;
}

dWMResourceMng_c::~dWMResourceMng_c() {
	if (isSetLoaded) {
		for (int i = 0; i < setData->count; i++) {
			resLoaders[i].unload();
		}

		delete[] resLoaders;
	}

	setLoader.unload();
}



bool dWMResourceMng_c::isLoaded() {
	return isLoadingComplete;
}


bool dWMResourceMng_c::loadSet(const char *setName) {
	if (isLoadingComplete)
		return true;

	// only start the loading process if we haven't done that already
	if (!isSetLoaded) {
		if (!hasSetPath) {
			snprintf(setPath, sizeof(setPath), "/Maps/%s.fileset", setName);
			hasSetPath = true;
		}

		void *result = setLoader.load(setPath);
		if (result == 0)
			return false;

		// cool, we've got it, now load everything here
		setData = (WMResSetHeader*)result;

		resLoaders = new dDvdLoader_c[setData->count];

		isSetLoaded = true;
	}

	// now try to load every resource
	// if even ONE of them fails, then don't return true
	bool isLoadingComplete = true;

	for (int i = 0; i < setData->count; i++) {
		isLoadingComplete &= (resLoaders[i].load(setData->getName(i)) != 0);
	}

	// if they are ALL loaded, then we'll return true!
	return isLoadingComplete;
}


void *dWMResourceMng_c::operator[](u32 key) {
	if (!isLoadingComplete)
		return 0;

	for (int i = 0; i < setData->count; i++) {
		if (key == setData->entries[i].key)
			return resLoaders[i].buffer;
	}

	return 0;
}