#include "worldmap.h" dWMResourceMng_c::dWMResourceMng_c() { hasSetPath = false; isSetLoaded = false; isLoadingComplete = false; MapReport("dWMResourceMng_c constructed @ %p\n", this); } 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; 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 bool isLoadingComplete = true; for (int i = 0; i < setData->count; i++) { void *thisResource = resLoaders[i].load(setData->getName(i)); MapReport("Load(%s) result: %p\n", setData->getName(i), thisResource); isLoadingComplete &= (thisResource != 0); } // if they are ALL loaded, then this will be true if (isLoadingComplete) { prepareResources(); } 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; } 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); } } } }