#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') { if (isResFile_byIndex(i)) { 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') { if (isResFile_byIndex(i)) { nw4r::g3d::ResFile firstFile(dataForIndex(i)); for (int j = 0; j < resCount(); j++) { if (isResFile_byIndex(j)) { nw4r::g3d::ResFile secondFile(dataForIndex(j)); firstFile.Bind(secondFile); } } } } }