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




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

	MapReport("dWMResourceMng_c constructed @ %p\n", this);
}

dWMResourceMng_c::~dWMResourceMng_c() {
	MapReport("dWMResourceMng_c destructed\n");

	if (isSetLoaded) {
		MapReport("Unloading %d resources\n", setData->count);

		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;

			MapReport("Generated set path: %s\n", setPath);
		}

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

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

		MapReport("Set loaded! Data: %p - Count: %d\n", result, setData->count);

		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
	isLoadingComplete = true;

	for (int i = 0; i < setData->count; i++) {
		void *thisResource = resLoaders[i].load(setData->getName(i));

		u32 key = setData->entries[i].key;
		char key1 = key >> 24;
		char key2 = key >> 16;
		char key3 = key >> 8;
		char key4 = key & 0xFF;

		MapReport("Load(%c%c%c%c:%s) result: %p\n", key>>24, key>>16, key>>8, key&0xFF, setData->getName(i), thisResource);

		isLoadingComplete &= (thisResource != 0);
	}

	// if they are ALL loaded, then this will be true
	if (isLoadingComplete) {
		prepareResources();

		MapReport("Loading completed!\n");
	}

	return isLoadingComplete;
}


void *dWMResourceMng_c::operator[](u32 key) {
	MapReport("Obtaining resource %c%c%c%c\n", key>>24, key>>16, key>>8, key&0xFF);

	if (!isLoadingComplete) {
		MapReport("Loading is not complete. Returning 0\n");
		return 0;
	}

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

	MapReport("Not found!\n");

	return 0;
}


void dWMResourceMng_c::prepareResources() {
	// first off, initialise every brres file used
	for (int i = 0; i < resCount(); i++) {
		if ((keyForIndex(i) >> 16) == '3D') {
			nw4r::g3d::ResFile resFile(dataForIndex(i));
			resFile.CheckRevision();
			resFile.Init();
		}
	}

	// now that they're all initialised, loop through the brres files and bind
	// them to every other file ... because I don't think it's a good idea to
	// bind them before they've been initialised
	for (int i = 0; i < resCount(); i++) {
		if ((keyForIndex(i) >> 16) == '3D') {
			nw4r::g3d::ResFile firstFile(dataForIndex(i));

			for (int j = 0; j < resCount(); j++) {
				nw4r::g3d::ResFile secondFile(dataForIndex(j));

				firstFile.Bind(secondFile);
			}
		}
	}
}