瀏覽代碼

Replaced CFileInfo with more correct and efficient alternative

Zyx-2000 9 年之前
父節點
當前提交
70e3c81b9f

+ 0 - 1
client/CBitmapHandler.cpp

@@ -1,7 +1,6 @@
 #include "StdInc.h"
 
 #include "../lib/filesystem/Filesystem.h"
-#include "../lib/filesystem/CFileInfo.h"
 #include "SDL.h"
 #include "SDL_image.h"
 #include "CBitmapHandler.h"

+ 0 - 1
client/CPreGame.cpp

@@ -2,7 +2,6 @@
 #include "CPreGame.h"
 
 #include "../lib/filesystem/Filesystem.h"
-#include "../lib/filesystem/CFileInfo.h"
 #include "../lib/filesystem/CCompressedStream.h"
 
 #include "../lib/CStopWatch.h"

+ 6 - 6
client/NetPacksClient.cpp

@@ -2,7 +2,7 @@
 #include "../lib/NetPacks.h"
 
 #include "../lib/filesystem/Filesystem.h"
-#include "../lib/filesystem/CFileInfo.h"
+#include "../lib/filesystem/FileInfo.h"
 #include "../CCallback.h"
 #include "Client.h"
 #include "CPlayerInterface.h"
@@ -636,8 +636,8 @@ void BattleSetActiveStack::applyCl( CClient *cl )
 	PlayerColor playerToCall; //player that will move activated stack
 	if( activated->hasBonusOfType(Bonus::HYPNOTIZED) )
 	{
-		playerToCall = ( GS(cl)->curB->sides[0].color == activated->owner 
-			? GS(cl)->curB->sides[1].color 
+		playerToCall = ( GS(cl)->curB->sides[0].color == activated->owner
+			? GS(cl)->curB->sides[1].color
 			: GS(cl)->curB->sides[0].color );
 	}
 	else
@@ -803,12 +803,12 @@ void YourTurn::applyCl( CClient *cl )
 
 void SaveGame::applyCl(CClient *cl)
 {
-	CFileInfo info(fname);
-	CResourceHandler::get("local")->createResource(info.getStem() + ".vcgm1");
+	const auto stem = FileInfo::GetPathStem(fname);
+	CResourceHandler::get("local")->createResource(stem.to_string() + ".vcgm1");
 
 	try
 	{
-		CSaveFile save(*CResourceHandler::get()->getResourceName(ResourceID(info.getStem(), EResType::CLIENT_SAVEGAME)));
+		CSaveFile save(*CResourceHandler::get()->getResourceName(ResourceID(stem.to_string(), EResType::CLIENT_SAVEGAME)));
 		cl->saveCommonState(save);
 		save << *cl;
 	}

+ 11 - 11
lib/VCMIDirs.cpp

@@ -31,7 +31,7 @@ void IVCMIDirs::init()
 
 	#ifndef CSIDL_MYDOCUMENTS
 	#define CSIDL_MYDOCUMENTS CSIDL_PERSONAL
-	#endif    
+	#endif
 #endif // __MINGW32__
 
 #include <windows.h>
@@ -87,7 +87,7 @@ bool StartBatchCopyDataProgram(
 		"%5%"													"\n"
 		"del \"%%~f0\"&exit"									"\n" // Script deletes itself
 		;
-	
+
 	const auto startGameString =
 		bfs::equivalent(currentPath, from) ?
 		(boost::format("start \"\" %1%") % (to / exeName)) :						// Start game in new path.
@@ -107,7 +107,7 @@ bool StartBatchCopyDataProgram(
 	return true;
 }
 
-class VCMIDirsWIN32 : public IVCMIDirs
+class VCMIDirsWIN32 final : public IVCMIDirs
 {
 	public:
 		boost::filesystem::path userDataPath() const override;
@@ -166,7 +166,7 @@ void VCMIDirsWIN32::init()
 		{
 			const std::wstring& pathStr = path.native();
 			std::unique_ptr<wchar_t[]> result(new wchar_t[pathStr.length() + 2]);
-			
+
 			size_t i = 0;
 			for (const wchar_t ch : pathStr)
 				result[i++] = ch;
@@ -195,7 +195,7 @@ void VCMIDirsWIN32::init()
 			return false;
 		else if (!bfs::is_empty(from)) // TODO: Log warn. Some files not moved. User should try to move files.
 			return false;
-		
+
 		if (bfs::current_path() == from)
 			bfs::current_path(to);
 
@@ -203,7 +203,7 @@ void VCMIDirsWIN32::init()
 		bfs::remove(from);
 		return true;
 	};
-	
+
 	// Retrieves the fully qualified path for the file that contains the specified module.
 	// The module must have been loaded by the current process.
 	// If this parameter is nullptr, retrieves the path of the executable file of the current process.
@@ -247,7 +247,7 @@ void VCMIDirsWIN32::init()
 		// Start copying script and exit program.
 		if (StartBatchCopyDataProgram(from, to, executableName))
 			exit(ERROR_SUCCESS);
-		
+
 		// Everything failed :C
 		return false;
 	};
@@ -262,14 +262,14 @@ bfs::path VCMIDirsWIN32::userDataPath() const
 
 	if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_MYDOCUMENTS, FALSE) != FALSE)
 		return bfs::path(profileDir) / "My Games\\vcmi";
-	
+
 	return ".";
 }
 
 bfs::path VCMIDirsWIN32::oldUserDataPath() const
 {
 	wchar_t profileDir[MAX_PATH];
-	
+
 	if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_PROFILE, FALSE) == FALSE) // WinAPI way failed
 	{
 #if defined(_MSC_VER) && _MSC_VER >= 1700
@@ -284,7 +284,7 @@ bfs::path VCMIDirsWIN32::oldUserDataPath() const
 		}
 #else
 		const char* profileDirA;
-		if (profileDirA = std::getenv("userprofile")) // STL way succeed
+		if ((profileDirA = std::getenv("userprofile"))) // STL way succeed
 			return bfs::path(profileDirA) / "vcmi";
 #endif
 		else
@@ -365,7 +365,7 @@ std::string IVCMIDirsUNIX::genHelpString() const
 }
 
 #ifdef VCMI_APPLE
-class VCMIDirsOSX : public IVCMIDirsUNIX
+class VCMIDirsOSX final : public IVCMIDirsUNIX
 {
 	public:
 		boost::filesystem::path userDataPath() const override;

+ 2 - 0
lib/VCMI_lib.cbp

@@ -229,6 +229,8 @@
 		<Unit filename="filesystem/CMemoryStream.h" />
 		<Unit filename="filesystem/CZipLoader.cpp" />
 		<Unit filename="filesystem/CZipLoader.h" />
+		<Unit filename="filesystem/FileInfo.cpp" />
+		<Unit filename="filesystem/FileInfo.h" />
 		<Unit filename="filesystem/FileStream.cpp" />
 		<Unit filename="filesystem/FileStream.h" />
 		<Unit filename="filesystem/Filesystem.cpp" />

+ 0 - 1
lib/filesystem/AdapterLoaders.h

@@ -13,7 +13,6 @@
 #include "ISimpleResourceLoader.h"
 #include "ResourceID.h"
 
-class CFileInfo;
 class CInputStream;
 class JsonNode;
 

+ 3 - 4
lib/filesystem/CArchiveLoader.cpp

@@ -5,7 +5,6 @@
 #include "CCompressedStream.h"
 
 #include "CBinaryReader.h"
-#include "CFileInfo.h"
 
 ArchiveEntry::ArchiveEntry()
 	: offset(0), fullSize(0), compressedSize(0)
@@ -140,13 +139,13 @@ std::unique_ptr<CInputStream> CArchiveLoader::load(const ResourceID & resourceNa
 
 	if (entry.compressedSize != 0) //compressed data
 	{
-		std::unique_ptr<CInputStream> fileStream(new CFileInputStream(archive, entry.offset, entry.compressedSize));
+		auto fileStream = make_unique<CFileInputStream>(archive, entry.offset, entry.compressedSize);
 
-		return std::unique_ptr<CInputStream>(new CCompressedStream(std::move(fileStream), false, entry.fullSize));
+		return make_unique<CCompressedStream>(std::move(fileStream), false, entry.fullSize);
 	}
 	else
 	{
-		return std::unique_ptr<CInputStream>(new CFileInputStream(archive, entry.offset, entry.fullSize));
+		return make_unique<CFileInputStream>(archive, entry.offset, entry.fullSize);
 	}
 }
 

+ 0 - 1
lib/filesystem/CArchiveLoader.h

@@ -13,7 +13,6 @@
 #include "ISimpleResourceLoader.h"
 #include "ResourceID.h"
 
-class CFileInfo;
 class CFileInputStream;
 
 /**

+ 0 - 5
lib/filesystem/CFileInputStream.cpp

@@ -1,8 +1,6 @@
 #include "StdInc.h"
 #include "CFileInputStream.h"
 
-#include "CFileInfo.h"
-
 CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 start, si64 size)
   : dataStart{start},
 	dataSize{size},
@@ -20,9 +18,6 @@ CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 st
 	fileStream.seekg(dataStart, std::ios::beg);
 }
 
-CFileInputStream::CFileInputStream(const CFileInfo & file, si64 start, si64 size)
-	: CFileInputStream{file.getName(), start, size} {}
-
 si64 CFileInputStream::read(ui8 * data, si64 size)
 {
 	si64 origin = tell();

+ 0 - 9
lib/filesystem/CFileInputStream.h

@@ -13,8 +13,6 @@
 #include "CInputStream.h"
 #include "FileStream.h"
 
-class CFileInfo;
-
 /**
  * A class which provides method definitions for reading a file from the filesystem.
  */
@@ -32,13 +30,6 @@ public:
 	 */
 	CFileInputStream(const boost::filesystem::path & file, si64 start = 0, si64 size = 0);
 
-	/**
-	 * C-tor. Opens the specified file.
-	 *
-	 * @see CFileInputStream::CFileInputStream(const boost::filesystem::path &, si64, si64)
-	 */
-	CFileInputStream(const CFileInfo & file, si64 start=0, si64 size=0);
-
 	/**
 	 * Reads n bytes from the stream into the data buffer.
 	 *

+ 1 - 3
lib/filesystem/CFilesystemLoader.cpp

@@ -1,7 +1,6 @@
 #include "StdInc.h"
 #include "CFilesystemLoader.h"
 
-#include "CFileInfo.h"
 #include "CFileInputStream.h"
 #include "FileStream.h"
 
@@ -19,8 +18,7 @@ std::unique_ptr<CInputStream> CFilesystemLoader::load(const ResourceID & resourc
 {
 	assert(fileList.count(resourceName));
 
-	std::unique_ptr<CInputStream> stream(new CFileInputStream(baseDirectory / fileList.at(resourceName)));
-	return stream;
+	return make_unique<CFileInputStream>(baseDirectory / fileList.at(resourceName));
 }
 
 bool CFilesystemLoader::existsResource(const ResourceID & resourceName) const

+ 0 - 1
lib/filesystem/CFilesystemLoader.h

@@ -13,7 +13,6 @@
 #include "ISimpleResourceLoader.h"
 #include "ResourceID.h"
 
-class CFileInfo;
 class CInputStream;
 
 /**

+ 1 - 1
lib/filesystem/CZipLoader.cpp

@@ -86,7 +86,7 @@ std::unordered_map<ResourceID, unz64_file_pos> CZipLoader::listFiles(const std::
 
 std::unique_ptr<CInputStream> CZipLoader::load(const ResourceID & resourceName) const
 {
-	return std::unique_ptr<CInputStream>(new CZipStream(archiveName, files.at(resourceName)));
+	return make_unique<CZipStream>(archiveName, files.at(resourceName));
 }
 
 bool CZipLoader::existsResource(const ResourceID & resourceName) const

+ 0 - 2
lib/filesystem/Filesystem.cpp

@@ -1,8 +1,6 @@
 #include "StdInc.h"
 #include "Filesystem.h"
 
-#include "CFileInfo.h"
-
 #include "CArchiveLoader.h"
 #include "CFilesystemLoader.h"
 #include "AdapterLoaders.h"

+ 30 - 23
lib/filesystem/ResourceID.cpp

@@ -1,7 +1,6 @@
 #include "StdInc.h"
 #include "ResourceID.h"
-
-#include "CFileInfo.h"
+#include "FileInfo.h"
 
 // trivial to_upper that completely ignores localization and only work with ASCII
 // Technically not a problem since
@@ -23,28 +22,45 @@ static inline void toUpper(std::string & string)
 	for (char & symbol : string)
 		toUpper(symbol);
 }
-
+#else
+static inline void toUpper(std::string & string)
+{
+	boost::to_upper(string);
+}
 #endif
 
+static inline EResType::Type readType(const std::string& name)
+{
+	return EResTypeHelper::getTypeFromExtension(FileInfo::GetExtension(name).to_string());
+}
+
+static inline std::string readName(std::string name)
+{
+	const auto dotPos = name.find_last_of('.');
+
+	if (dotPos != std::string::npos)
+		name.resize(dotPos);
+
+	toUpper(name);
+
+	return name;
+}
 
 ResourceID::ResourceID()
 	:type(EResType::OTHER)
 {
 }
 
-ResourceID::ResourceID(std::string name)
-	:type(EResType::UNDEFINED)
+ResourceID::ResourceID(std::string name_)
+	:type{readType(name_)},
+	name{readName(std::move(name_))}
 {
-	CFileInfo info(std::move(name));
-	setType(info.getType());
-	setName(info.getStem());
 }
 
-ResourceID::ResourceID(std::string name, EResType::Type type)
-	:type(EResType::UNDEFINED)
+ResourceID::ResourceID(std::string name_, EResType::Type type_)
+	:type{type_},
+	name{readName(std::move(name_))}
 {
-	setType(type);
-	setName(std::move(name));
 }
 
 std::string ResourceID::getName() const
@@ -56,7 +72,7 @@ EResType::Type ResourceID::getType() const
 {
 	return type;
 }
-
+#if 0
 void ResourceID::setName(std::string name)
 {
 	// setName shouldn't be used if type is UNDEFINED
@@ -72,26 +88,17 @@ void ResourceID::setName(std::string name)
 		this->name.erase(dotPos);
 	}
 
-#ifdef ENABLE_TRIVIAL_TOUPPER
 	toUpper(this->name);
-#else
-	// strangely enough but this line takes 40-50% of filesystem loading time
-	boost::to_upper(this->name);
-#endif
 }
 
 void ResourceID::setType(EResType::Type type)
 {
 	this->type = type;
 }
-
+#endif
 EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
 {
-#ifdef ENABLE_TRIVIAL_TOUPPER
 	toUpper(extension);
-#else
-	boost::to_upper(extension);
-#endif
 
 	static const std::map<std::string, EResType::Type> stringToRes =
 	{

+ 5 - 5
lib/filesystem/ResourceID.h

@@ -100,18 +100,18 @@ public:
 
 	std::string getName() const;
 	EResType::Type getType() const;
-	void setName(std::string name);
-	void setType(EResType::Type type);
+	//void setName(std::string name);
+	//void setType(EResType::Type type);
 
 private:
-	/** Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case. **/
-	std::string name;
-
 	/**
 	 * Specifies the resource type. EResType::OTHER if not initialized.
 	 * Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
 	 */
 	EResType::Type type;
+
+	/** Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case. **/
+	std::string name;
 };
 
 namespace std

+ 6 - 6
server/CGameHandler.cpp

@@ -1,7 +1,7 @@
 #include "StdInc.h"
 
 #include "../lib/filesystem/Filesystem.h"
-#include "../lib/filesystem/CFileInfo.h"
+#include "../lib/filesystem/FileInfo.h"
 #include "../lib/int3.h"
 #include "../lib/mapping/CCampaignHandler.h"
 #include "../lib/StartInfo.h"
@@ -2362,13 +2362,13 @@ void CGameHandler::sendAndApply( NewStructures * info )
 void CGameHandler::save(const std::string & filename )
 {
     logGlobal->infoStream() << "Saving to " << filename;
-	CFileInfo info(filename);
-	//CResourceHandler::get("local")->createResource(info.getStem() + ".vlgm1");
-	CResourceHandler::get("local")->createResource(info.getStem() + ".vsgm1");
+	const auto stem	= FileInfo::GetPathStem(filename);
+	const auto savefname = stem.to_string() + ".vsgm1";
+	CResourceHandler::get("local")->createResource(savefname);
 
 	{
         logGlobal->infoStream() << "Ordering clients to serialize...";
-		SaveGame sg(info.getStem() + ".vcgm1");
+		SaveGame sg(savefname);
 		sendToAllClients(&sg);
 	}
 
@@ -2383,7 +2383,7 @@ void CGameHandler::save(const std::string & filename )
 // 		}
 
 		{
-			CSaveFile save(*CResourceHandler::get("local")->getResourceName(ResourceID(info.getStem(), EResType::SERVER_SAVEGAME)));
+			CSaveFile save(*CResourceHandler::get("local")->getResourceName(ResourceID(stem.to_string(), EResType::SERVER_SAVEGAME)));
 			saveCommonState(save);
             logGlobal->infoStream() << "Saving server state";
 			save << *this;