Prechádzať zdrojové kódy

Added FileSystem class

Marko Pintera 13 rokov pred
rodič
commit
781d13837b

+ 4 - 0
CamelotClient/CamelotClient.cpp

@@ -4,6 +4,7 @@
 #include "stdafx.h"
 
 #include "CmApplication.h"
+#include "CmDynLibManager.h"
 
 #include "TestingGround.h"
 
@@ -14,6 +15,9 @@ int _tmain(int argc, _TCHAR* argv[])
 	gApplication().startUp("CamelotGLRenderer.dll");
 	//gApplication().startUp("CamelotD3D9Renderer.dll");
 
+	DynLib* loadedLibrary = gDynLibManager().load("CamelotFreeImgImporter.dll"); // TODO - Load this automatically somehow
+
+
 	test();
 
 	gApplication().runMainLoop();

+ 1 - 0
CamelotClient/TestingGround.cpp

@@ -4,6 +4,7 @@
 #include "CmResource.h"
 #include "CmTextureData.h"
 #include "CmTextureManager.h"
+#include "CmImporter.h"
 
 using namespace CamelotEngine;
 

+ 3 - 0
CamelotRenderer/Source/CmImporter.cpp

@@ -2,6 +2,7 @@
 #include "CmPath.h"
 #include "CmSpecificImporter.h"
 #include "CmDebug.h"
+#include "CmDataStream.h"
 #include "CmException.h"
 
 namespace CamelotEngine
@@ -57,6 +58,8 @@ namespace CamelotEngine
 
 		//SpecificImporter* importer = mAssetImporters[ext];
 
+		//FileDataStream fileSteam()
+
 		//ResourcePtr importedResource = importer->import(inputFilePath);
 
 		// TODO - Use AssetDatabase for loading the resource

+ 2 - 0
CamelotUtility/CamelotUtility.vcxproj

@@ -85,6 +85,7 @@
     <ClInclude Include="Include\CmDynLibManager.h" />
     <ClInclude Include="Include\CmException.h" />
     <ClInclude Include="Include\CmFileSerializer.h" />
+    <ClInclude Include="Include\CmFileSystem.h" />
     <ClInclude Include="Include\CmIReflectable.h" />
     <ClInclude Include="Include\CmLog.h" />
     <ClInclude Include="Include\CmManagedDataBlock.h" />
@@ -124,6 +125,7 @@
     <ClInclude Include="Include\CmTextureData.h" />
     <ClCompile Include="Source\CmBinarySerializer.cpp" />
     <ClCompile Include="Source\CmFileSerializer.cpp" />
+    <ClCompile Include="Source\CmFileSystem.cpp" />
     <ClCompile Include="Source\CmIReflectable.cpp" />
     <ClCompile Include="Source\CmISerializable.cpp" />
     <ClCompile Include="Source\CmRTTIField.cpp" />

+ 6 - 0
CamelotUtility/CamelotUtility.vcxproj.filters

@@ -180,6 +180,9 @@
     <ClInclude Include="Include\CmTextureDataST.h">
       <Filter>Header Files\SerializationTypes</Filter>
     </ClInclude>
+    <ClInclude Include="Include\CmFileSystem.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Include\CmAxisAlignedBox.cpp">
@@ -260,5 +263,8 @@
     <ClCompile Include="Source\CmISerializable.cpp">
       <Filter>Source Files\Serialization</Filter>
     </ClCompile>
+    <ClCompile Include="Source\CmFileSystem.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 1 - 1
CamelotUtility/Include/CmDataStream.h

@@ -371,7 +371,7 @@ namespace CamelotEngine
     /** Common subclass of DataStream for handling data from 
 		std::basic_istream.
 	*/
-	class CM_UTILITY_EXPORT FileDataStream : DataStream
+	class CM_UTILITY_EXPORT FileDataStream : public DataStream
 	{
 	protected:
 		/// Reference to source stream (read)

+ 15 - 0
CamelotUtility/Include/CmFileSystem.h

@@ -0,0 +1,15 @@
+#pragma once
+
+#include "CmPrerequisitesUtil.h"
+
+namespace CamelotEngine
+{
+	class FileSystem
+	{
+		static DataStreamPtr open(const String& fullPath, bool readOnly = true);
+
+		static DataStreamPtr create(const String& fullPath);
+
+		static void remove(const String& fullPath);
+	};
+}

+ 1 - 0
CamelotUtility/Include/CmFwdDeclUtil.h

@@ -25,6 +25,7 @@ namespace CamelotEngine {
 	class MemoryDataStream;
 	class FileDataStream;
 	class TextureData;
+	class FileSystem;
 	// Reflection
 	class IReflectable;
 	class RTTITypeBase;

+ 18 - 0
CamelotUtility/Include/CmPath.h

@@ -20,5 +20,23 @@ namespace CamelotEngine
 		{
 			return boost::filesystem::extension(path);
 		}
+
+
+		static bool isAbsolute(const String& path)
+		{
+#if CM_PLATFORM == CM_PLATFORM_WIN32
+			if (isalpha(UINT8(path[0])) && path[1] == ':')
+				return true;
+#endif
+			return path[0] == '/' || path[0] == '\\';
+		}
+
+		static String concatenatePath(const String& base, const String& name)
+		{
+			if (base.empty() || isAbsolute(name.c_str()))
+				return name;
+			else
+				return base + '/' + name;
+		}
 	};
 }

+ 110 - 0
CamelotUtility/Source/CmFileSystem.cpp

@@ -0,0 +1,110 @@
+#include "CmFileSystem.h"
+#include "CmDataStream.h"
+#include "CmPath.h"
+#include "CmException.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#if CM_PLATFORM == CM_PLATFORM_LINUX || CM_PLATFORM == CM_PLATFORM_APPLE || \
+	CM_PLATFORM == CM_PLATFORM_SYMBIAN || CM_PLATFORM == CM_PLATFORM_IPHONE
+#   include <sys/param.h>
+#   define MAX_PATH MAXPATHLEN
+#endif
+
+#if CM_PLATFORM == CM_PLATFORM_WIN32
+#  define WIN32_LEAN_AND_MEAN
+#  if !defined(NOMINMAX) && defined(_MSC_VER)
+#	define NOMINMAX // required to stop windows.h messing up std::min
+#  endif
+#  include <windows.h>
+#  include <direct.h>
+#  include <io.h>
+#endif
+
+namespace CamelotEngine
+{
+	DataStreamPtr FileSystem::open(const String& fullPath, bool readOnly)
+	{
+		// Use filesystem to determine size 
+		// (quicker than streaming to the end and back)
+		struct stat tagStat;
+		int ret = stat(fullPath.c_str(), &tagStat);
+		assert(ret == 0 && "Problem getting file size" );
+		(void)ret;  // Silence warning
+
+		// Always open in binary mode
+		// Also, always include reading
+		std::ios::openmode mode = std::ios::in | std::ios::binary;
+		std::istream* baseStream = 0;
+		std::ifstream* roStream = 0;
+		std::fstream* rwStream = 0;
+
+		if (!readOnly)
+		{
+			mode |= std::ios::out;
+			rwStream = new std::fstream();
+			rwStream->open(fullPath.c_str(), mode);
+			baseStream = rwStream;
+		}
+		else
+		{
+			roStream = new std::ifstream();
+			roStream->open(fullPath.c_str(), mode);
+			baseStream = roStream;
+		}
+
+		// Should check ensure open succeeded, in case fail for some reason.
+		if (baseStream->fail())
+		{
+			if(roStream != nullptr)
+				delete roStream;
+
+			if(rwStream != nullptr)
+				delete rwStream;
+
+			CM_EXCEPT(FileNotFoundException, "Cannot open file: " + fullPath);
+		}
+
+		/// Construct return stream, tell it to delete on destroy
+		FileDataStream* stream = 0;
+		if (rwStream)
+		{
+			// use the writeable stream 
+			stream = new FileDataStream(fullPath, rwStream, (size_t)tagStat.st_size, true);
+		}
+		else
+		{
+			// read-only stream
+			stream = new FileDataStream(fullPath, roStream, (size_t)tagStat.st_size, true);
+		}
+		return DataStreamPtr(stream);
+	}
+
+	DataStreamPtr FileSystem::create(const String& fullPath)
+	{
+		// Always open in binary mode
+		// Also, always include reading
+		std::ios::openmode mode = std::ios::out | std::ios::binary;
+		std::fstream* rwStream = new std::fstream();
+		rwStream->open(fullPath.c_str(), mode);
+
+		// Should check ensure open succeeded, in case fail for some reason.
+		if (rwStream->fail())
+		{
+			if(rwStream != nullptr)
+				delete rwStream;
+
+			CM_EXCEPT(FileNotFoundException, "Cannot open file: " + fullPath);
+		}
+
+		/// Construct return stream, tell it to delete on destroy
+		FileDataStream* stream = new FileDataStream(fullPath, rwStream, 0, true);
+		return DataStreamPtr(stream);
+	}
+
+	void FileSystem::remove(const String& fullPath)
+	{
+		::remove(fullPath.c_str());
+	}
+}