ResourceHandle ResourceManager::getResource(const String& path) { Path name = PathGetFile(path); auto key = MurmurHash64(name.c_str(), name.size(), 0); return resources.get(key, HandleInvalid); }
ResourceHandle ResourceManager::loadResource(const String& path) { Path name = PathGetFile(path); ResourceLoadOptions options; options.name = name; options.asynchronousLoad = asynchronousLoading; return loadResource(options); }
void EditorFrame::onDocumentRenamed(Document* document) { if( !document ) return; String name = PathGetFile( document->getPath() ); if( name.empty() ) name = "untitled"; if( document->getUnsavedChanges() ) name += "*"; wxWindow* window = (wxWindow*) document->getWindow(); size_t index = notebookCtrl->GetPageIndex(window); notebookCtrl->SetPageText(index, name); }
void ResourceManager::sendPendingEvents() { ResourceEvent event; while( resourceEvents.try_pop_front(event) ) { Resource* resource = event.resource; auto base = PathGetFile(resource->path); auto key = MurmurHash64(base.c_str(), base.size(), 0); ResourceHandle handle = resources.get(key, HandleInvalid); assert( handle != HandleInvalid ); event.handle = handle; onResourceLoaded( event ); } }
void ResourceManager::handleWatchResource(Archive*, const FileWatchEvent& evt) { // Check if the filename maps to a known resource. auto file = PathGetFile(evt.filename); auto key = MurmurHash64(file.c_str(), file.size(), 0); if( !resources.has(key) ) return; // Resource is not known. // Reload the resource if it was modified. if( evt.action != FileWatchEventKind::Modified ) { #pragma TODO("Add rename support in live updating") LogDebug( "Resource was renamed - handle this" ); return; } // Register the decoded resource in the map. LogInfo("Reloading resource '%s'", file.c_str()); ResourceLoadOptions options; options.sendLoadEvent = false; options.name = evt.filename; Resource* resource = prepareResource(options); decodeResource(options); auto handle = resources.get(key, HandleInvalid); Resource* oldResource = handle.Resolve(); HandleId handleId = handle.getId(); ResourceEvent event; event.resource = resource; event.oldResource = oldResource; event.handle = handle; // Switch the resource but maintain the same handle. resource->addReference(); handleManager->handles.set(handleId, resource); onResourceReloaded(event); event.handle.id = HandleInvalid; }
ResourceHandle ResourceManager::loadResource(ResourceLoadOptions& options) { if( !archive ) return ResourceHandle(HandleInvalid); Path fileExt = PathGetFileExtension(options.name); // If the file has no extension, search for one with the same // name but with known resource loadable file extensions. if(fileExt.empty() && !findResource(options)) { LogError("Could not find matching file for '%s'", options.name.c_str()); return ResourceHandle(HandleInvalid); } // Check if the resource is already loaded. ResourceHandle handle = getResource(options.name); if( handle ) return handle; if( !validateResource(options.name) ) return ResourceHandle(HandleInvalid); Resource* resource = prepareResource(options); if( !resource ) return ResourceHandle(HandleInvalid); handle = ResourceHandleCreate(resource); if(handle == HandleInvalid) return ResourceHandle(HandleInvalid); // Register the decoded resource in the map. Path base = PathGetFile(options.name); auto key = MurmurHash64(base.c_str(), base.size(), 0); resources.set(key, handle); decodeResource(options); return handle; }
void EditorFrame::onDocumentAdded(Document* document) { if( !document ) return; eventManager->onDocumentCreate(*document); wxWindow* window = (wxWindow*) document->getWindow(); if( !window ) { LogDebug("Invalid window in document"); return; } String name = PathGetFile( document->getPath() ); if( name.empty() ) name = "untitled"; getNotebook()->AddPage(window, name, true, document->getBitmap()); getAUI()->Update(); }
Resource* ResourceManager::prepareResource( ResourceLoadOptions& options ) { const Path& path = options.name; Stream* stream = archive->openFile(path, GetResourcesAllocator()); if( !stream ) { LogWarn("Resource was not found: '%s'", path.c_str()); return nullptr; } const Path& file = PathGetFile(path); // Get the available resource loader and prepare the resource. ResourceLoader* loader = findLoader( PathGetFileExtension(file) ); if( !loader ) { LogWarn("No resource loader found for resource '%s'", file.c_str()); return nullptr; } options.stream = stream; Resource* resource = loader->prepare(options); if( !resource ) { LogError("Error preparing resource: '%s'", path.c_str()); return nullptr; } resource->setStatus( ResourceStatus::Loading ); resource->setPath( file ); options.resource = resource; return resource; }