Ver Fonte

Made file providers capable of accepting a source string, via the Core's addSource and removeSource methods, moved old addArchive method from resource manager and reimplemented it as a source for physfs file provider of type 'archive', made the basic file provider accept folders as a source of type 'folder'

Ivan Safrin há 10 anos atrás
pai
commit
9402fd310c

+ 5 - 4
build/osx/TemplateApp/TemplateApp.xcodeproj/project.pbxproj

@@ -25,7 +25,7 @@
 		8A86536B1B729559009F94DD /* PolycodeTemplateApp.mm in Sources */ = {isa = PBXBuildFile; fileRef = 8A86536A1B729559009F94DD /* PolycodeTemplateApp.mm */; };
 		8A86536B1B729559009F94DD /* PolycodeTemplateApp.mm in Sources */ = {isa = PBXBuildFile; fileRef = 8A86536A1B729559009F94DD /* PolycodeTemplateApp.mm */; };
 		8A86536D1B729D77009F94DD /* PolycodeView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 8A86536C1B729D77009F94DD /* PolycodeView.mm */; };
 		8A86536D1B729D77009F94DD /* PolycodeView.mm in Sources */ = {isa = PBXBuildFile; fileRef = 8A86536C1B729D77009F94DD /* PolycodeView.mm */; };
 		8A8653701B72C431009F94DD /* default.pak in Resources */ = {isa = PBXBuildFile; fileRef = 8A86536F1B72C431009F94DD /* default.pak */; };
 		8A8653701B72C431009F94DD /* default.pak in Resources */ = {isa = PBXBuildFile; fileRef = 8A86536F1B72C431009F94DD /* default.pak */; };
-		8A8653721B72C527009F94DD /* main_icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 8A8653711B72C527009F94DD /* main_icon.png */; };
+		8A8653961B752DBE009F94DD /* main_icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 8A8653951B752DBE009F94DD /* main_icon.png */; };
 /* End PBXBuildFile section */
 /* End PBXBuildFile section */
 
 
 /* Begin PBXFileReference section */
 /* Begin PBXFileReference section */
@@ -52,7 +52,7 @@
 		8A86536C1B729D77009F94DD /* PolycodeView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = PolycodeView.mm; path = ../../../../src/view/osx/PolycodeView.mm; sourceTree = "<group>"; };
 		8A86536C1B729D77009F94DD /* PolycodeView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = PolycodeView.mm; path = ../../../../src/view/osx/PolycodeView.mm; sourceTree = "<group>"; };
 		8A86536E1B72C301009F94DD /* PolycodeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PolycodeView.h; path = ../../../../include/polycode/view/osx/PolycodeView.h; sourceTree = "<group>"; };
 		8A86536E1B72C301009F94DD /* PolycodeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PolycodeView.h; path = ../../../../include/polycode/view/osx/PolycodeView.h; sourceTree = "<group>"; };
 		8A86536F1B72C431009F94DD /* default.pak */ = {isa = PBXFileReference; lastKnownFileType = file; name = default.pak; path = ../../../../assets/default/default.pak; sourceTree = "<group>"; };
 		8A86536F1B72C431009F94DD /* default.pak */ = {isa = PBXFileReference; lastKnownFileType = file; name = default.pak; path = ../../../../assets/default/default.pak; sourceTree = "<group>"; };
-		8A8653711B72C527009F94DD /* main_icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = main_icon.png; path = ../../../../assets/icons/main_icon.png; sourceTree = "<group>"; };
+		8A8653951B752DBE009F94DD /* main_icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = main_icon.png; path = ../../../../assets/icons/main_icon.png; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 /* End PBXFileReference section */
 
 
 /* Begin PBXFrameworksBuildPhase section */
 /* Begin PBXFrameworksBuildPhase section */
@@ -105,8 +105,8 @@
 				8A86536E1B72C301009F94DD /* PolycodeView.h */,
 				8A86536E1B72C301009F94DD /* PolycodeView.h */,
 				8A86536C1B729D77009F94DD /* PolycodeView.mm */,
 				8A86536C1B729D77009F94DD /* PolycodeView.mm */,
 				8A86536F1B72C431009F94DD /* default.pak */,
 				8A86536F1B72C431009F94DD /* default.pak */,
-				8A8653711B72C527009F94DD /* main_icon.png */,
 				8A86533A1B72931C009F94DD /* Images.xcassets */,
 				8A86533A1B72931C009F94DD /* Images.xcassets */,
+				8A8653951B752DBE009F94DD /* main_icon.png */,
 				8A86533C1B72931C009F94DD /* MainMenu.xib */,
 				8A86533C1B72931C009F94DD /* MainMenu.xib */,
 				8A8653331B72931C009F94DD /* Supporting Files */,
 				8A8653331B72931C009F94DD /* Supporting Files */,
 			);
 			);
@@ -189,7 +189,7 @@
 			buildActionMask = 2147483647;
 			buildActionMask = 2147483647;
 			files = (
 			files = (
 				8A86533B1B72931C009F94DD /* Images.xcassets in Resources */,
 				8A86533B1B72931C009F94DD /* Images.xcassets in Resources */,
-				8A8653721B72C527009F94DD /* main_icon.png in Resources */,
+				8A8653961B752DBE009F94DD /* main_icon.png in Resources */,
 				8A86533E1B72931C009F94DD /* MainMenu.xib in Resources */,
 				8A86533E1B72931C009F94DD /* MainMenu.xib in Resources */,
 				8A8653701B72C431009F94DD /* default.pak in Resources */,
 				8A8653701B72C431009F94DD /* default.pak in Resources */,
 			);
 			);
@@ -367,6 +367,7 @@
 				8A86534F1B72931C009F94DD /* Release */,
 				8A86534F1B72931C009F94DD /* Release */,
 			);
 			);
 			defaultConfigurationIsVisible = 0;
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 		};
 /* End XCConfigurationList section */
 /* End XCConfigurationList section */
 	};
 	};

+ 2 - 1
build/osx/TemplateApp/TemplateApp/PolycodeTemplateApp.mm

@@ -7,7 +7,8 @@
 
 
 PolycodeTemplateApp::PolycodeTemplateApp(PolycodeView *view) {
 PolycodeTemplateApp::PolycodeTemplateApp(PolycodeView *view) {
     core = new CocoaCore(view, 1280,720,false,false, 0,0,60);
     core = new CocoaCore(view, 1280,720,false,false, 0,0,60);
-	CoreServices::getInstance()->getResourceManager()->addArchive("default.pak");
+    
+    core->addFileSource("archive", "default.pak");
 	CoreServices::getInstance()->getResourceManager()->addDirResource("default");
 	CoreServices::getInstance()->getResourceManager()->addDirResource("default");
 
 
 	// Write your code here!
 	// Write your code here!

+ 6 - 1
include/polycode/core/PolyBasicFileProvider.h

@@ -38,9 +38,14 @@ namespace Polycode {
     };
     };
     
     
     class _PolyExport BasicFileProvider : public CoreFileProvider {
     class _PolyExport BasicFileProvider : public CoreFileProvider {
-        public:    
+        public:
+            BasicFileProvider();
             Polycode::CoreFile *openFile(const String &fileName, const String &opts);
             Polycode::CoreFile *openFile(const String &fileName, const String &opts);
             void closeFile(Polycode::CoreFile *file);
             void closeFile(Polycode::CoreFile *file);
+            void addSource(const String &source);
+            void removeSource(const String &source);
+      
+            std::vector<String> sourceFolders;
         
         
     };
     };
     
     

+ 3 - 0
include/polycode/core/PolyCore.h

@@ -300,6 +300,9 @@ namespace Polycode {
         CoreFile *openFile(const Polycode::String& fileName, const Polycode::String& opts);
         CoreFile *openFile(const Polycode::String& fileName, const Polycode::String& opts);
         void closeFile(CoreFile *file);
         void closeFile(CoreFile *file);
         
         
+        void addFileSource(const String &type, const String &source);
+        void removeFileSource(const String &type, const String &source);
+        
         std::vector<OSFileEntry> parseFolder(const Polycode::String& pathString, bool showHidden);
         std::vector<OSFileEntry> parseFolder(const Polycode::String& pathString, bool showHidden);
 
 
         virtual bool systemParseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) = 0;
         virtual bool systemParseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) = 0;

+ 4 - 0
include/polycode/core/PolyCoreFileProvider.h

@@ -68,6 +68,10 @@ namespace Polycode {
         virtual void closeFile(CoreFile *file) = 0;
         virtual void closeFile(CoreFile *file) = 0;
         virtual bool parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector);
         virtual bool parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector);
         
         
+        virtual void addSource(const String &source);
+        virtual void removeSource(const String &source);
+        
+        String type;
         bool canListFiles;
         bool canListFiles;
     };
     };
 
 

+ 4 - 0
include/polycode/core/PolyPhysFSFileProvider.h

@@ -41,11 +41,15 @@ namespace Polycode {
     class _PolyExport PhysFSFileProvider : public CoreFileProvider {
     class _PolyExport PhysFSFileProvider : public CoreFileProvider {
         public:
         public:
             PhysFSFileProvider();
             PhysFSFileProvider();
+            ~PhysFSFileProvider();
         
         
             Polycode::CoreFile *openFile(const String &fileName, const String &opts);
             Polycode::CoreFile *openFile(const String &fileName, const String &opts);
             void closeFile(Polycode::CoreFile *file);
             void closeFile(Polycode::CoreFile *file);
         
         
             bool parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector);
             bool parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector);
+        
+            void addSource(const String &source);
+            void removeSource(const String &source);
     };
     };
     
     
 }
 }

+ 0 - 11
include/polycode/core/PolyResourceManager.h

@@ -89,17 +89,6 @@ namespace Polycode {
 			* @param recursive If true, will recurse into subdirectories.
 			* @param recursive If true, will recurse into subdirectories.
 			*/
 			*/
 			void addDirResource(const String& dirPath, bool recursive=true);
 			void addDirResource(const String& dirPath, bool recursive=true);
-			
-			/**
-			* Adds a zip or folder as a readable source. This doesn't actually load resources from it, just mounts it as a readable source, so you can call addDirResource on the folders inside of it like you would on regular folders. Most other disk IO in the engine (loading images, etc.) will actually check mounted archive files as well.
-			*/
-			void addArchive(const String& path);
-
-			/**
-			* Removes a zip or folder as a readable source.
-			*/
-			void removeArchive(const String& path);
-
 		
 		
 			void parseTexturesIntoPool(ResourcePool *pool, const String& dirPath, bool recursive, const String& basePath);
 			void parseTexturesIntoPool(ResourcePool *pool, const String& dirPath, bool recursive, const String& basePath);
 			void parseMaterialsIntoPool(ResourcePool *pool, const String& dirPath, bool recursive);
 			void parseMaterialsIntoPool(ResourcePool *pool, const String& dirPath, bool recursive);

BIN
lib/osx/libPolycore.a


+ 30 - 1
src/core/PolyBasicFileProvider.cpp

@@ -3,8 +3,37 @@
 
 
 using namespace Polycode;
 using namespace Polycode;
 
 
+BasicFileProvider::BasicFileProvider() {
+    type = "folder";
+}
+
+void BasicFileProvider::addSource(const String &source) {
+    sourceFolders.push_back(source);
+}
+
+void BasicFileProvider::removeSource(const String &source) {
+    for(int i=0; i < sourceFolders.size(); i++) {
+        if(sourceFolders[i] == source) {
+            sourceFolders.erase(sourceFolders.begin()+i);
+            return;
+        }
+    }
+}
+
 Polycode::CoreFile *BasicFileProvider::openFile(const String &fileName, const String &opts) {
 Polycode::CoreFile *BasicFileProvider::openFile(const String &fileName, const String &opts) {
-    FILE *file = fopen(fileName.c_str(), opts.c_str());
+    
+    FILE *file = NULL;
+    for(int i=0; i < sourceFolders.size(); i++) {
+        file = fopen((sourceFolders[i]+"/"+fileName).c_str(), opts.c_str());
+        if(file) {
+            break;
+        }
+    }
+    
+    if(!file) {
+        file = fopen(fileName.c_str(), opts.c_str());
+    }
+    
     if(file) {
     if(file) {
         BasicFile *retFile = NULL;
         BasicFile *retFile = NULL;
         retFile = new BasicFile();
         retFile = new BasicFile();

+ 18 - 0
src/core/PolyCore.cpp

@@ -346,6 +346,24 @@ namespace Polycode {
         assert(false); // CLOSING A FILE FOR A NON-EXISTING PROVIDER
         assert(false); // CLOSING A FILE FOR A NON-EXISTING PROVIDER
     }
     }
     
     
+    void Core::addFileSource(const String &type, const String &source) {
+        for(int i=0; i < fileProviders.size(); i++) {
+            if(fileProviders[i]->type == type) {
+                fileProviders[i]->addSource(source);
+                return;
+            }
+        }
+    }
+    
+    void Core::removeFileSource(const String &type, const String &source) {
+        for(int i=0; i < fileProviders.size(); i++) {
+            if(fileProviders[i]->type == type) {
+                fileProviders[i]->removeSource(source);
+                return;
+            }
+        }
+    }
+    
     std::vector<OSFileEntry> Core::parseFolder(const Polycode::String& pathString, bool showHidden) {
     std::vector<OSFileEntry> Core::parseFolder(const Polycode::String& pathString, bool showHidden) {
         std::vector<OSFileEntry> retVec;
         std::vector<OSFileEntry> retVec;
         
         

+ 8 - 0
src/core/PolyCoreFileProvider.cpp

@@ -55,4 +55,12 @@ void OSFileEntry::init(const Polycode::String& path, const Polycode::String& nam
 
 
 bool CoreFileProvider::parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) {
 bool CoreFileProvider::parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) {
     return false;
     return false;
+}
+
+void CoreFileProvider::addSource(const String &source) {
+    
+}
+
+void CoreFileProvider::removeSource(const String &source) {
+    
 }
 }

+ 20 - 0
src/core/PolyPhysFSFileProvider.cpp

@@ -1,10 +1,17 @@
 
 
 #include "polycode/core/PolyPhysFSFileProvider.h"
 #include "polycode/core/PolyPhysFSFileProvider.h"
+#include "polycode/core/PolyLogger.h"
 
 
 using namespace Polycode;
 using namespace Polycode;
 
 
 PhysFSFileProvider::PhysFSFileProvider() {
 PhysFSFileProvider::PhysFSFileProvider() {
     canListFiles = true;
     canListFiles = true;
+    type = "archive";
+	PHYSFS_init(NULL);
+}
+
+PhysFSFileProvider::~PhysFSFileProvider() {
+    PHYSFS_deinit();    
 }
 }
 
 
 bool PhysFSFileProvider::parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) {
 bool PhysFSFileProvider::parseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) {
@@ -60,6 +67,18 @@ Polycode::CoreFile *PhysFSFileProvider::openFile(const String &fileName, const S
     return NULL;
     return NULL;
 }
 }
 
 
+void PhysFSFileProvider::addSource(const String &source) {
+    if(PHYSFS_addToSearchPath(source.c_str(), 1) == 0) {
+        Logger::log("Error adding archive to resource manager... %s\n", PHYSFS_getLastError());
+    } else {
+        Logger::log("Added physfs archive: %s\n", source.c_str());
+    }
+}
+
+void PhysFSFileProvider::removeSource(const String &source) {
+    PHYSFS_removeFromSearchPath(source.c_str());
+}
+
 void PhysFSFileProvider::closeFile(Polycode::CoreFile *file) {
 void PhysFSFileProvider::closeFile(Polycode::CoreFile *file) {
     PhysFSFile *physFSFile = (PhysFSFile*) file;
     PhysFSFile *physFSFile = (PhysFSFile*) file;
     PHYSFS_close(physFSFile->physFSFile);
     PHYSFS_close(physFSFile->physFSFile);
@@ -96,3 +115,4 @@ int PhysFSFile::seek(long int offset, int origin) {
 long PhysFSFile::tell() {
 long PhysFSFile::tell() {
     return PHYSFS_tell(physFSFile);   
     return PHYSFS_tell(physFSFile);   
 }
 }
+

+ 0 - 16
src/core/PolyResourceManager.cpp

@@ -29,8 +29,6 @@
 #include "polycode/core/PolyMaterial.h"
 #include "polycode/core/PolyMaterial.h"
 #include "polycode/core/PolyShader.h"
 #include "polycode/core/PolyShader.h"
 #include "polycode/core/PolyTexture.h"
 #include "polycode/core/PolyTexture.h"
-
-#include "physfs.h"
 #include "tinyxml.h"
 #include "tinyxml.h"
 
 
 using std::vector;
 using std::vector;
@@ -187,13 +185,11 @@ void ResourcePool::checkForChangedFiles() {
 }
 }
 
 
 ResourceManager::ResourceManager() : EventDispatcher() {
 ResourceManager::ResourceManager() : EventDispatcher() {
-	PHYSFS_init(NULL);
     globalPool = new ResourcePool("Global", NULL);
     globalPool = new ResourcePool("Global", NULL);
 }
 }
 
 
 ResourceManager::~ResourceManager() {
 ResourceManager::~ResourceManager() {
     printf("Shutting down resource manager...\n");
     printf("Shutting down resource manager...\n");
-    PHYSFS_deinit();
 
 
     for(int i=0; i < pools.size(); i++)	{
     for(int i=0; i < pools.size(); i++)	{
         delete pools[i];
         delete pools[i];
@@ -358,18 +354,6 @@ void ResourceManager::parseTexturesIntoPool(ResourcePool *pool, const String& di
 	}
 	}
 }
 }
 
 
-void ResourceManager::addArchive(const String& path) {
-	if(PHYSFS_addToSearchPath(path.c_str(), 1) == 0) {	
-		Logger::log("Error adding archive to resource manager... %s\n", PHYSFS_getLastError());
-	} else {
-		Logger::log("Added archive: %s\n", path.c_str());
-	}
-}
-
-void ResourceManager::removeArchive(const String& path) {
-	PHYSFS_removeFromSearchPath(path.c_str());
-}
-
 void ResourceManager::Update(int elapsed) {
 void ResourceManager::Update(int elapsed) {
     globalPool->Update(elapsed);
     globalPool->Update(elapsed);
     for(int i=0; i < pools.size(); i++) {
     for(int i=0; i < pools.size(); i++) {