Browse Source

Added resource mapping so that resources in standalone build can be loaded by using project library relative paths

BearishSun 10 years ago
parent
commit
a77ece41a1

+ 3 - 0
BansheeEngine/BansheeEngine.vcxproj

@@ -255,6 +255,7 @@
     <ClCompile Include="Source\BsCursor.cpp" />
     <ClCompile Include="Source\BsDrawHelper.cpp" />
     <ClCompile Include="Source\BsDropDownAreaPlacement.cpp" />
+    <ClCompile Include="Source\BsGameResourceManager.cpp" />
     <ClCompile Include="Source\BsGUIDropDownContent.cpp" />
     <ClCompile Include="Source\BsGUIElementStyle.cpp" />
     <ClCompile Include="Source\BsGUIPanel.cpp" />
@@ -296,6 +297,7 @@
     <ClInclude Include="Include\BsDrawHelper.h" />
     <ClInclude Include="Include\BsDropDownAreaPlacement.h" />
     <ClInclude Include="Include\BsEngineShaderIncludeHandler.h" />
+    <ClInclude Include="Include\BsGameResourceManager.h" />
     <ClInclude Include="Include\BsGameSettings.h" />
     <ClInclude Include="Include\BsGameSettingsRTTI.h" />
     <ClInclude Include="Include\BsGUIDropDownContent.h" />
@@ -323,6 +325,7 @@
     <ClInclude Include="Include\BsRendererMaterial.h" />
     <ClInclude Include="Include\BsRendererMaterialManager.h" />
     <ClInclude Include="Include\BsRendererUtility.h" />
+    <ClInclude Include="Include\BsResourceMappingRTTI.h" />
     <ClInclude Include="Include\BsScriptCode.h" />
     <ClInclude Include="Include\BsScriptCodeImporter.h" />
     <ClInclude Include="Include\BsScriptCodeImportOptions.h" />

+ 9 - 0
BansheeEngine/BansheeEngine.vcxproj.filters

@@ -404,6 +404,12 @@
     <ClInclude Include="Include\BsEngineShaderIncludeHandler.h">
       <Filter>Header Files\Resources</Filter>
     </ClInclude>
+    <ClInclude Include="Include\BsGameResourceManager.h">
+      <Filter>Header Files\Resources</Filter>
+    </ClInclude>
+    <ClInclude Include="Include\BsResourceMappingRTTI.h">
+      <Filter>Header Files\RTTI</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsGUIElement.cpp">
@@ -676,5 +682,8 @@
     <ClCompile Include="Source\BsEngineShaderIncludeHandler.cpp">
       <Filter>Source Files\Resources</Filter>
     </ClCompile>
+    <ClCompile Include="Source\BsGameResourceManager.cpp">
+      <Filter>Source Files\Resources</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 1 - 0
BansheeEngine/Include/BsPaths.h

@@ -14,6 +14,7 @@ namespace BansheeEngine
 	static const char* GAME_RESOURCES_FOLDER_NAME = "Resources\\";
 	static const char* GAME_SETTINGS_NAME = "GameSettings.asset";
 	static const char* GAME_RESOURCE_MANIFEST_NAME = "ResourceManifest.asset";
+	static const char* GAME_RESOURCE_MAPPING_NAME = "ResourceMapping.asset";
 
 	/** Contains common engine paths. */
 	class BS_EXPORT Paths

+ 2 - 1
BansheeEngine/Include/BsPrerequisites.h

@@ -207,6 +207,7 @@ namespace BansheeEngine
 		TID_GUISkinEntry = 30010,
 		TID_Light = 30011,
 		TID_CLight = 30012,
-		TID_GameSettings = 30013
+		TID_GameSettings = 30013,
+		TID_ResourceMapping = 30014
 	};
 }

+ 38 - 0
BansheeEngine/Include/BsResourceMappingRTTI.h

@@ -0,0 +1,38 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsPrerequisites.h"
+#include "BsRTTIType.h"
+#include "BsGameResourceManager.h"
+
+namespace BansheeEngine
+{
+	class BS_EXPORT ResourceMappingRTTI : public RTTIType<ResourceMapping, IReflectable, ResourceMappingRTTI>
+	{
+	private:
+		BS_PLAIN_MEMBER(mMapping)
+
+	public:
+		ResourceMappingRTTI()
+		{
+			BS_ADD_PLAIN_FIELD(mMapping, 0);
+		}
+
+		const String& getRTTIName() override
+		{
+			static String name = "ResourceMapping";
+			return name;
+		}
+
+		UINT32 getRTTIId() override
+		{
+			return TID_ResourceMapping;
+		}
+
+		std::shared_ptr<IReflectable> newRTTIObject() override
+		{
+			return ResourceMapping::create();
+		}
+	};
+}

+ 36 - 0
SBansheeEngine/Source/BsGameResourceManager.cpp → BansheeEngine/Source/BsGameResourceManager.cpp

@@ -1,15 +1,46 @@
 //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #include "BsGameResourceManager.h"
+#include "BsResourceMappingRTTI.h"
 #include "BsResources.h"
+#include "BsFileSystem.h"
 
 namespace BansheeEngine
 {
+	void ResourceMapping::add(const Path& from, const Path& to)
+	{
+		mMapping[from] = to;
+	}
+
+	SPtr<ResourceMapping> ResourceMapping::create()
+	{
+		return bs_shared_ptr_new<ResourceMapping>();
+	}
+
+	RTTITypeBase* ResourceMapping::getRTTIStatic()
+	{
+		return ResourceMappingRTTI::instance();
+	}
+
+	RTTITypeBase* ResourceMapping::getRTTI() const
+	{
+		return getRTTIStatic();
+	}
+
 	HResource StandaloneResourceLoader::load(const Path& path, bool keepLoaded) const
 	{
+		auto iterFind = mMapping.find(path);
+		if(iterFind != mMapping.end())
+			return gResources().load(iterFind->second, true, keepLoaded);
+		
 		return gResources().load(path, true, keepLoaded);
 	}
 
+	void StandaloneResourceLoader::setMapping(const SPtr<ResourceMapping>& mapping)
+	{
+		mMapping = mapping->getMap();
+	}
+
 	GameResourceManager::GameResourceManager()
 		:mLoader(bs_shared_ptr_new<StandaloneResourceLoader>())
 	{
@@ -21,6 +52,11 @@ namespace BansheeEngine
 		return mLoader->load(path, keepLoaded);
 	}
 
+	void GameResourceManager::setMapping(const SPtr<ResourceMapping>& mapping)
+	{
+		mLoader->setMapping(mapping);
+	}
+
 	void GameResourceManager::setLoader(const SPtr<IGameResourceLoader>& loader)
 	{
 		mLoader = loader;

+ 10 - 1
Game/Source/Main.cpp

@@ -12,6 +12,7 @@
 #include "BsSceneObject.h"
 #include "BsSceneManager.h"
 #include "BsRenderAPI.h"
+#include "BsGameResourceManager.h"
 
 void runApplication();
 
@@ -73,6 +74,15 @@ void runApplication()
 
 	Application::startUp(renderWindowDesc, RenderAPIPlugin::DX11);
 
+	// Note: What if script tries to load resources during startup? The manifest nor the mapping wont be set up yet.
+	Path resourcesPath = Paths::getGameResourcesPath();
+	Path resourceMappingPath = resourcesPath + GAME_RESOURCE_MAPPING_NAME;
+
+	FileDecoder mappingFd(resourceMappingPath);
+	SPtr<ResourceMapping> resMapping = std::static_pointer_cast<ResourceMapping>(mappingFd.decode());
+
+	GameResourceManager::instance().setMapping(resMapping);
+
 	if (gameSettings->fullscreen)
 	{
 		if (gameSettings->useDesktopResolution)
@@ -109,7 +119,6 @@ void runApplication()
 	FileEncoder fe(gameSettingsPath);
 	fe.encode(gameSettings.get());
 
-	Path resourcesPath = Paths::getGameResourcesPath();
 	Path resourceManifestPath = resourcesPath + GAME_RESOURCE_MANIFEST_NAME;
 
 	ResourceManifestPtr manifest;

+ 23 - 1
SBansheeEditor/Source/BsScriptBuildManager.cpp

@@ -20,6 +20,7 @@
 #include "BsBuiltinResources.h"
 #include "BsSceneObject.h"
 #include "BsDebug.h"
+#include "BsGameResourceManager.h"
 
 namespace BansheeEngine
 {
@@ -223,6 +224,7 @@ namespace BansheeEngine
 	void ScriptBuildManager::internal_PackageResources(MonoString* buildFolder, ScriptPlatformInfo* info)
 	{
 		UnorderedSet<Path> usedResources;
+		SPtr<ResourceMapping> resourceMap = ResourceMapping::create();
 
 		// Get all resources manually included in build
 		Vector<ProjectLibrary::FileEntry*> buildResources = gProjectLibrary().getResourcesForBuild();
@@ -289,11 +291,14 @@ namespace BansheeEngine
 		} 
 
 		// Copy resources
-		Path outputPath = MonoUtil::monoToWString(buildFolder);
+		Path buildPath = MonoUtil::monoToWString(buildFolder);
+
+		Path outputPath = buildPath;
 		outputPath.append(GAME_RESOURCES_FOLDER_NAME);
 
 		FileSystem::createDir(outputPath);
 
+		Path libraryDir = gProjectLibrary().getResourcesFolder();
 		for (auto& entry : usedResources)
 		{
 			String uuid;
@@ -310,6 +315,16 @@ namespace BansheeEngine
 			Path destPath = outputPath;
 			destPath.setFilename(entry.getFilename());
 
+			// Create library -> packaged resource mapping
+			Path relSourcePath = sourcePath;
+			if (sourcePath.isAbsolute())
+				relSourcePath.makeRelative(libraryDir);
+
+			Path relDestPath = GAME_RESOURCES_FOLDER_NAME;
+			relDestPath.setFilename(entry.getFilename());
+
+			resourceMap->add(relSourcePath, relDestPath);
+
 			// If resource is prefab make sure to update it in case any of the prefabs it is referencing changed
 			if (resMeta->getTypeID() == TID_Prefab)
 			{
@@ -383,6 +398,13 @@ namespace BansheeEngine
 
 		ResourceManifestPtr manifest = gProjectLibrary()._getManifest();
 		ResourceManifest::save(manifest, manifestPath, internalResourcesFolder);
+
+		// Save resource map
+		Path mappingPath = outputPath;
+		mappingPath.append(GAME_RESOURCE_MAPPING_NAME);
+
+		FileEncoder fe(mappingPath);
+		fe.encode(resourceMap.get());
 	}
 
 	void ScriptBuildManager::internal_CreateStartupSettings(MonoString* buildFolder, ScriptPlatformInfo* info)

+ 0 - 75
SBansheeEngine/Include/BsGameResourceManager.h

@@ -1,75 +0,0 @@
-//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
-//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
-#pragma once
-
-#include "BsScriptEnginePrerequisites.h"
-#include "BsModule.h"
-
-namespace BansheeEngine
-{
-	/**
-	 * @brief	Interface that can be implemented by the resource loaders required
-	 *			by GameResources.
-	 */
-	class BS_SCR_BE_EXPORT IGameResourceLoader
-	{
-	public:
-		virtual ~IGameResourceLoader() { }
-
-		/**
-		 * @brief	Loads the resource at the specified path.
-		 */
-		virtual HResource load(const Path& path, bool keepLoaded) const = 0;
-	};
-
-	/**
-	 * @brief	Handles loading of game resources when the standalone game is running.
-	 */
-	class BS_SCR_BE_EXPORT StandaloneResourceLoader : public IGameResourceLoader
-	{
-	public:
-		/**
-		 * @copydoc	IGameResourceLoader::load
-		 */
-		HResource load(const Path& path, bool keepLoaded) const override;
-	};
-
-	/**
-	 * @brief	Keeps track of resources that can be dynamically loaded
-	 *			during runtime. These resources will be packed with the game
-	 *			build so that they're available on demand.
-	 *
-	 *			Internal resource handle can be overridden so that editor or other
-	 *			systems can handle resource loading more directly.
-	 */
-	class BS_SCR_BE_EXPORT GameResourceManager : public Module<GameResourceManager>
-	{
-	public:
-		GameResourceManager();
-
-		/**
-		 * @brief	Loads the resource at the specified path.
-		 * 			
-		 * @see	Resources::load
-		 */
-		HResource load(const Path& path, bool keepLoaded) const;	
-
-		/**
-		 * @copydoc	load
-		 */
-		template <class T>
-		ResourceHandle<T> load(const Path& filePath, bool keepLoaded)
-		{
-			return static_resource_cast<T>(load(filePath, keepLoaded));
-		}
-
-		/**
-		 * @brief	Sets the resource loader implementation that determines how are the
-		 *			paths provided to ::load loaded.
-		 */
-		void setLoader(const SPtr<IGameResourceLoader>& loader);
-
-	private:
-		SPtr<IGameResourceLoader> mLoader;
-	};
-}

+ 0 - 2
SBansheeEngine/SBansheeEngine.vcxproj

@@ -251,7 +251,6 @@
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClInclude Include="Include\BsEngineScriptLibrary.h" />
-    <ClInclude Include="Include\BsGameResourceManager.h" />
     <ClInclude Include="Include\BsManagedComponent.h" />
     <ClInclude Include="Include\BsManagedComponentRTTI.h" />
     <ClInclude Include="Include\BsManagedDiff.h" />
@@ -365,7 +364,6 @@
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsEngineScriptLibrary.cpp" />
-    <ClCompile Include="Source\BsGameResourceManager.cpp" />
     <ClCompile Include="Source\BsManagedComponent.cpp" />
     <ClCompile Include="Source\BsManagedDiff.cpp" />
     <ClCompile Include="Source\BsManagedResource.cpp" />

+ 0 - 6
SBansheeEngine/SBansheeEngine.vcxproj.filters

@@ -320,9 +320,6 @@
     <ClInclude Include="Include\BsScriptGUISkin.h">
       <Filter>Header Files\GUI</Filter>
     </ClInclude>
-    <ClInclude Include="Include\BsGameResourceManager.h">
-      <Filter>Header Files</Filter>
-    </ClInclude>
     <ClInclude Include="Include\BsScriptResources.h">
       <Filter>Header Files</Filter>
     </ClInclude>
@@ -628,9 +625,6 @@
     <ClCompile Include="Source\BsScriptGUISkin.cpp">
       <Filter>Source Files\GUI</Filter>
     </ClCompile>
-    <ClCompile Include="Source\BsGameResourceManager.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="Source\BsScriptResources.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>