Beispiel #1
0
ResourceHandle ResourceManager::getResource(const String& path)
{
	Path name = PathGetFile(path);
	auto key = MurmurHash64(name.c_str(), name.size(), 0);

	return resources.get(key, HandleInvalid);
}
Beispiel #2
0
ResourceHandle ResourceManager::loadResource(const String& path)
{
	Path name = PathGetFile(path);

	ResourceLoadOptions options;
	options.name = name;
	options.asynchronousLoad = asynchronousLoading;
	
	return loadResource(options);
}
Beispiel #3
0
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);
}
Beispiel #4
0
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 );
	}
}
Beispiel #5
0
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;
}
Beispiel #6
0
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;
}
Beispiel #7
0
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();
}
Beispiel #8
0
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;
}