mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-27 22:49:25 +02:00
Replaced CFileInfo with more correct and efficient alternative
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
|
|
||||||
#include "../lib/filesystem/Filesystem.h"
|
#include "../lib/filesystem/Filesystem.h"
|
||||||
#include "../lib/filesystem/CFileInfo.h"
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
#include "SDL_image.h"
|
#include "SDL_image.h"
|
||||||
#include "CBitmapHandler.h"
|
#include "CBitmapHandler.h"
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
#include "CPreGame.h"
|
#include "CPreGame.h"
|
||||||
|
|
||||||
#include "../lib/filesystem/Filesystem.h"
|
#include "../lib/filesystem/Filesystem.h"
|
||||||
#include "../lib/filesystem/CFileInfo.h"
|
|
||||||
#include "../lib/filesystem/CCompressedStream.h"
|
#include "../lib/filesystem/CCompressedStream.h"
|
||||||
|
|
||||||
#include "../lib/CStopWatch.h"
|
#include "../lib/CStopWatch.h"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#include "../lib/NetPacks.h"
|
#include "../lib/NetPacks.h"
|
||||||
|
|
||||||
#include "../lib/filesystem/Filesystem.h"
|
#include "../lib/filesystem/Filesystem.h"
|
||||||
#include "../lib/filesystem/CFileInfo.h"
|
#include "../lib/filesystem/FileInfo.h"
|
||||||
#include "../CCallback.h"
|
#include "../CCallback.h"
|
||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
#include "CPlayerInterface.h"
|
#include "CPlayerInterface.h"
|
||||||
@@ -803,12 +803,12 @@ void YourTurn::applyCl( CClient *cl )
|
|||||||
|
|
||||||
void SaveGame::applyCl(CClient *cl)
|
void SaveGame::applyCl(CClient *cl)
|
||||||
{
|
{
|
||||||
CFileInfo info(fname);
|
const auto stem = FileInfo::GetPathStem(fname);
|
||||||
CResourceHandler::get("local")->createResource(info.getStem() + ".vcgm1");
|
CResourceHandler::get("local")->createResource(stem.to_string() + ".vcgm1");
|
||||||
|
|
||||||
try
|
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);
|
cl->saveCommonState(save);
|
||||||
save << *cl;
|
save << *cl;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ bool StartBatchCopyDataProgram(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
class VCMIDirsWIN32 : public IVCMIDirs
|
class VCMIDirsWIN32 final : public IVCMIDirs
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
boost::filesystem::path userDataPath() const override;
|
boost::filesystem::path userDataPath() const override;
|
||||||
@@ -284,7 +284,7 @@ bfs::path VCMIDirsWIN32::oldUserDataPath() const
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
const char* profileDirA;
|
const char* profileDirA;
|
||||||
if (profileDirA = std::getenv("userprofile")) // STL way succeed
|
if ((profileDirA = std::getenv("userprofile"))) // STL way succeed
|
||||||
return bfs::path(profileDirA) / "vcmi";
|
return bfs::path(profileDirA) / "vcmi";
|
||||||
#endif
|
#endif
|
||||||
else
|
else
|
||||||
@@ -365,7 +365,7 @@ std::string IVCMIDirsUNIX::genHelpString() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef VCMI_APPLE
|
#ifdef VCMI_APPLE
|
||||||
class VCMIDirsOSX : public IVCMIDirsUNIX
|
class VCMIDirsOSX final : public IVCMIDirsUNIX
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
boost::filesystem::path userDataPath() const override;
|
boost::filesystem::path userDataPath() const override;
|
||||||
|
|||||||
@@ -229,6 +229,8 @@
|
|||||||
<Unit filename="filesystem/CMemoryStream.h" />
|
<Unit filename="filesystem/CMemoryStream.h" />
|
||||||
<Unit filename="filesystem/CZipLoader.cpp" />
|
<Unit filename="filesystem/CZipLoader.cpp" />
|
||||||
<Unit filename="filesystem/CZipLoader.h" />
|
<Unit filename="filesystem/CZipLoader.h" />
|
||||||
|
<Unit filename="filesystem/FileInfo.cpp" />
|
||||||
|
<Unit filename="filesystem/FileInfo.h" />
|
||||||
<Unit filename="filesystem/FileStream.cpp" />
|
<Unit filename="filesystem/FileStream.cpp" />
|
||||||
<Unit filename="filesystem/FileStream.h" />
|
<Unit filename="filesystem/FileStream.h" />
|
||||||
<Unit filename="filesystem/Filesystem.cpp" />
|
<Unit filename="filesystem/Filesystem.cpp" />
|
||||||
|
|||||||
@@ -13,7 +13,6 @@
|
|||||||
#include "ISimpleResourceLoader.h"
|
#include "ISimpleResourceLoader.h"
|
||||||
#include "ResourceID.h"
|
#include "ResourceID.h"
|
||||||
|
|
||||||
class CFileInfo;
|
|
||||||
class CInputStream;
|
class CInputStream;
|
||||||
class JsonNode;
|
class JsonNode;
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@
|
|||||||
#include "CCompressedStream.h"
|
#include "CCompressedStream.h"
|
||||||
|
|
||||||
#include "CBinaryReader.h"
|
#include "CBinaryReader.h"
|
||||||
#include "CFileInfo.h"
|
|
||||||
|
|
||||||
ArchiveEntry::ArchiveEntry()
|
ArchiveEntry::ArchiveEntry()
|
||||||
: offset(0), fullSize(0), compressedSize(0)
|
: 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
|
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
|
else
|
||||||
{
|
{
|
||||||
return std::unique_ptr<CInputStream>(new CFileInputStream(archive, entry.offset, entry.fullSize));
|
return make_unique<CFileInputStream>(archive, entry.offset, entry.fullSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@
|
|||||||
#include "ISimpleResourceLoader.h"
|
#include "ISimpleResourceLoader.h"
|
||||||
#include "ResourceID.h"
|
#include "ResourceID.h"
|
||||||
|
|
||||||
class CFileInfo;
|
|
||||||
class CFileInputStream;
|
class CFileInputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
#include "CFileInputStream.h"
|
#include "CFileInputStream.h"
|
||||||
|
|
||||||
#include "CFileInfo.h"
|
|
||||||
|
|
||||||
CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 start, si64 size)
|
CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 start, si64 size)
|
||||||
: dataStart{start},
|
: dataStart{start},
|
||||||
dataSize{size},
|
dataSize{size},
|
||||||
@@ -20,9 +18,6 @@ CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 st
|
|||||||
fileStream.seekg(dataStart, std::ios::beg);
|
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 CFileInputStream::read(ui8 * data, si64 size)
|
||||||
{
|
{
|
||||||
si64 origin = tell();
|
si64 origin = tell();
|
||||||
|
|||||||
@@ -13,8 +13,6 @@
|
|||||||
#include "CInputStream.h"
|
#include "CInputStream.h"
|
||||||
#include "FileStream.h"
|
#include "FileStream.h"
|
||||||
|
|
||||||
class CFileInfo;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class which provides method definitions for reading a file from the filesystem.
|
* 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);
|
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.
|
* Reads n bytes from the stream into the data buffer.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
#include "CFilesystemLoader.h"
|
#include "CFilesystemLoader.h"
|
||||||
|
|
||||||
#include "CFileInfo.h"
|
|
||||||
#include "CFileInputStream.h"
|
#include "CFileInputStream.h"
|
||||||
#include "FileStream.h"
|
#include "FileStream.h"
|
||||||
|
|
||||||
@@ -19,8 +18,7 @@ std::unique_ptr<CInputStream> CFilesystemLoader::load(const ResourceID & resourc
|
|||||||
{
|
{
|
||||||
assert(fileList.count(resourceName));
|
assert(fileList.count(resourceName));
|
||||||
|
|
||||||
std::unique_ptr<CInputStream> stream(new CFileInputStream(baseDirectory / fileList.at(resourceName)));
|
return make_unique<CFileInputStream>(baseDirectory / fileList.at(resourceName));
|
||||||
return stream;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CFilesystemLoader::existsResource(const ResourceID & resourceName) const
|
bool CFilesystemLoader::existsResource(const ResourceID & resourceName) const
|
||||||
|
|||||||
@@ -13,7 +13,6 @@
|
|||||||
#include "ISimpleResourceLoader.h"
|
#include "ISimpleResourceLoader.h"
|
||||||
#include "ResourceID.h"
|
#include "ResourceID.h"
|
||||||
|
|
||||||
class CFileInfo;
|
|
||||||
class CInputStream;
|
class CInputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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
|
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
|
bool CZipLoader::existsResource(const ResourceID & resourceName) const
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
#include "Filesystem.h"
|
#include "Filesystem.h"
|
||||||
|
|
||||||
#include "CFileInfo.h"
|
|
||||||
|
|
||||||
#include "CArchiveLoader.h"
|
#include "CArchiveLoader.h"
|
||||||
#include "CFilesystemLoader.h"
|
#include "CFilesystemLoader.h"
|
||||||
#include "AdapterLoaders.h"
|
#include "AdapterLoaders.h"
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
#include "ResourceID.h"
|
#include "ResourceID.h"
|
||||||
|
#include "FileInfo.h"
|
||||||
#include "CFileInfo.h"
|
|
||||||
|
|
||||||
// trivial to_upper that completely ignores localization and only work with ASCII
|
// trivial to_upper that completely ignores localization and only work with ASCII
|
||||||
// Technically not a problem since
|
// Technically not a problem since
|
||||||
@@ -23,28 +22,45 @@ static inline void toUpper(std::string & string)
|
|||||||
for (char & symbol : string)
|
for (char & symbol : string)
|
||||||
toUpper(symbol);
|
toUpper(symbol);
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
static inline void toUpper(std::string & string)
|
||||||
|
{
|
||||||
|
boost::to_upper(string);
|
||||||
|
}
|
||||||
#endif
|
#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()
|
ResourceID::ResourceID()
|
||||||
:type(EResType::OTHER)
|
:type(EResType::OTHER)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
ResourceID::ResourceID(std::string name)
|
ResourceID::ResourceID(std::string name_)
|
||||||
:type(EResType::UNDEFINED)
|
: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)
|
ResourceID::ResourceID(std::string name_, EResType::Type type_)
|
||||||
:type(EResType::UNDEFINED)
|
:type{type_},
|
||||||
|
name{readName(std::move(name_))}
|
||||||
{
|
{
|
||||||
setType(type);
|
|
||||||
setName(std::move(name));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ResourceID::getName() const
|
std::string ResourceID::getName() const
|
||||||
@@ -56,7 +72,7 @@ EResType::Type ResourceID::getType() const
|
|||||||
{
|
{
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
#if 0
|
||||||
void ResourceID::setName(std::string name)
|
void ResourceID::setName(std::string name)
|
||||||
{
|
{
|
||||||
// setName shouldn't be used if type is UNDEFINED
|
// setName shouldn't be used if type is UNDEFINED
|
||||||
@@ -72,26 +88,17 @@ void ResourceID::setName(std::string name)
|
|||||||
this->name.erase(dotPos);
|
this->name.erase(dotPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_TRIVIAL_TOUPPER
|
|
||||||
toUpper(this->name);
|
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)
|
void ResourceID::setType(EResType::Type type)
|
||||||
{
|
{
|
||||||
this->type = type;
|
this->type = type;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
|
EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension)
|
||||||
{
|
{
|
||||||
#ifdef ENABLE_TRIVIAL_TOUPPER
|
|
||||||
toUpper(extension);
|
toUpper(extension);
|
||||||
#else
|
|
||||||
boost::to_upper(extension);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static const std::map<std::string, EResType::Type> stringToRes =
|
static const std::map<std::string, EResType::Type> stringToRes =
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -100,18 +100,18 @@ public:
|
|||||||
|
|
||||||
std::string getName() const;
|
std::string getName() const;
|
||||||
EResType::Type getType() const;
|
EResType::Type getType() const;
|
||||||
void setName(std::string name);
|
//void setName(std::string name);
|
||||||
void setType(EResType::Type type);
|
//void setType(EResType::Type type);
|
||||||
|
|
||||||
private:
|
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.
|
* 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.
|
* Required to prevent conflicts if files with different types (e.g. text and image) have the same name.
|
||||||
*/
|
*/
|
||||||
EResType::Type type;
|
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
|
namespace std
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "StdInc.h"
|
#include "StdInc.h"
|
||||||
|
|
||||||
#include "../lib/filesystem/Filesystem.h"
|
#include "../lib/filesystem/Filesystem.h"
|
||||||
#include "../lib/filesystem/CFileInfo.h"
|
#include "../lib/filesystem/FileInfo.h"
|
||||||
#include "../lib/int3.h"
|
#include "../lib/int3.h"
|
||||||
#include "../lib/mapping/CCampaignHandler.h"
|
#include "../lib/mapping/CCampaignHandler.h"
|
||||||
#include "../lib/StartInfo.h"
|
#include "../lib/StartInfo.h"
|
||||||
@@ -2362,13 +2362,13 @@ void CGameHandler::sendAndApply( NewStructures * info )
|
|||||||
void CGameHandler::save(const std::string & filename )
|
void CGameHandler::save(const std::string & filename )
|
||||||
{
|
{
|
||||||
logGlobal->infoStream() << "Saving to " << filename;
|
logGlobal->infoStream() << "Saving to " << filename;
|
||||||
CFileInfo info(filename);
|
const auto stem = FileInfo::GetPathStem(filename);
|
||||||
//CResourceHandler::get("local")->createResource(info.getStem() + ".vlgm1");
|
const auto savefname = stem.to_string() + ".vsgm1";
|
||||||
CResourceHandler::get("local")->createResource(info.getStem() + ".vsgm1");
|
CResourceHandler::get("local")->createResource(savefname);
|
||||||
|
|
||||||
{
|
{
|
||||||
logGlobal->infoStream() << "Ordering clients to serialize...";
|
logGlobal->infoStream() << "Ordering clients to serialize...";
|
||||||
SaveGame sg(info.getStem() + ".vcgm1");
|
SaveGame sg(savefname);
|
||||||
sendToAllClients(&sg);
|
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);
|
saveCommonState(save);
|
||||||
logGlobal->infoStream() << "Saving server state";
|
logGlobal->infoStream() << "Saving server state";
|
||||||
save << *this;
|
save << *this;
|
||||||
|
|||||||
Reference in New Issue
Block a user