Skip to content

Commit d5c7a6c

Browse files
committed
Merge branch 'tovfsornottovfs' into 'master'
Address incorrect std::filesystem::path usage Closes #8738 See merge request OpenMW/openmw!5033
2 parents 51e5328 + 82914be commit d5c7a6c

File tree

16 files changed

+132
-154
lines changed

16 files changed

+132
-154
lines changed

apps/openmw/mwgui/windowmanagerimp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ namespace MWGui
209209
SDL_GL_GetDrawableSize(window, &dw, &dh);
210210

211211
mScalingFactor = Settings::gui().mScalingFactor * (dw / w);
212+
constexpr VFS::Path::NormalizedView resourcePath("mygui");
212213
mGuiPlatform = std::make_unique<MyGUIPlatform::Platform>(viewer, guiRoot, resourceSystem->getImageManager(),
213-
resourceSystem->getVFS(), mScalingFactor, "mygui", logpath / "MyGUI.log");
214+
resourceSystem->getVFS(), mScalingFactor, resourcePath, logpath / "MyGUI.log");
214215

215216
mGui = std::make_unique<MyGUI::Gui>();
216217
mGui->initialise({});

components/bsa/ba2dx10file.cpp

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
#include <algorithm>
44
#include <cassert>
55
#include <cstring>
6-
#include <filesystem>
76
#include <format>
87
#include <istream>
98

109
#include <zlib.h>
1110

1211
#include <components/esm/fourcc.hpp>
1312
#include <components/files/constrainedfilestream.hpp>
14-
#include <components/files/conversion.hpp>
1513
#include <components/files/utils.hpp>
16-
#include <components/misc/strings/lower.hpp>
14+
#include <components/vfs/pathutil.hpp>
1715

1816
#include "ba2file.hpp"
1917
#include "memorystream.hpp"
@@ -148,28 +146,18 @@ namespace Bsa
148146
}
149147
}
150148

151-
#ifdef _WIN32
152-
const auto& path = str;
153-
#else
154-
// Force-convert the path into something UNIX can handle first
155-
// to make sure std::filesystem::path doesn't think the entire path is the filename on Linux
156-
// and subsequently purge it to determine the file folder.
157-
std::string path(str);
158-
std::replace(path.begin(), path.end(), '\\', '/');
159-
#endif
160-
161-
const auto p = std::filesystem::path{ path }; // Purposefully damage Unicode strings.
162-
const auto fileName = Misc::StringUtils::lowerCase(p.stem().string());
163-
const auto ext = Misc::StringUtils::lowerCase(p.extension().string()); // Purposefully damage Unicode strings.
164-
const auto folder = Misc::StringUtils::lowerCase(p.parent_path().string());
149+
const VFS::Path::Normalized path(str);
150+
151+
const std::string_view fileName = path.stem();
152+
const std::string_view folder = path.parent().value();
165153

166154
uint32_t folderHash = generateHash(folder);
167155
auto it = mFolders.find(folderHash);
168156
if (it == mFolders.end())
169157
return std::nullopt; // folder not found
170158

171159
uint32_t fileHash = generateHash(fileName);
172-
uint32_t extHash = generateExtensionHash(ext);
160+
uint32_t extHash = generateExtensionHash(path.filename());
173161
auto iter = it->second.find({ fileHash, extHash });
174162
if (iter == it->second.end())
175163
return std::nullopt; // file not found
@@ -229,13 +217,6 @@ namespace Bsa
229217
fail("Add file is not implemented for compressed BSA: " + filename);
230218
}
231219

232-
Files::IStreamPtr BA2DX10File::getFile(const char* file)
233-
{
234-
if (auto fileRec = getFileRecord(file); fileRec)
235-
return getFile(*fileRec);
236-
fail("File not found: " + std::string(file));
237-
}
238-
239220
constexpr const uint32_t DDSD_CAPS = 0x00000001;
240221
constexpr const uint32_t DDSD_HEIGHT = 0x00000002;
241222
constexpr const uint32_t DDSD_WIDTH = 0x00000004;

components/bsa/ba2dx10file.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ namespace Bsa
5959
/// Read header information from the input source
6060
void readHeader(std::istream& stream) override;
6161

62-
Files::IStreamPtr getFile(const char* filePath);
6362
Files::IStreamPtr getFile(const FileStruct* fileStruct);
6463
void addFile(const std::string& filename, std::istream& file);
6564
};

components/bsa/ba2file.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "ba2file.hpp"
22

3+
#include <components/misc/pathhelpers.hpp>
4+
35
namespace Bsa
46
{
57
constexpr const uint32_t crc32table[256] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
@@ -32,7 +34,7 @@ namespace Bsa
3234
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e,
3335
0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d };
3436

35-
uint32_t generateHash(const std::string& name)
37+
uint32_t generateHash(std::string_view name)
3638
{
3739
uint32_t result = 0;
3840
for (auto c : name)
@@ -46,8 +48,15 @@ namespace Bsa
4648
return result;
4749
}
4850

49-
uint32_t generateExtensionHash(std::string_view extension)
51+
uint32_t generateExtensionHash(VFS::Path::NormalizedView file)
5052
{
53+
std::string_view extension;
54+
if (const std::size_t pos = Misc::findExtension(file.value()); pos != std::string_view::npos)
55+
{
56+
// ext including .
57+
extension = file.value();
58+
extension.remove_prefix(pos);
59+
}
5160
uint32_t result = 0;
5261
for (size_t i = 0; i < 4 && i < extension.size() - 1; i++)
5362
result |= static_cast<uint8_t>(extension[i + 1]) << (8 * i);

components/bsa/ba2file.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#include <cstdint>
55
#include <string>
66

7+
#include <components/vfs/pathutil.hpp>
8+
79
namespace Bsa
810
{
9-
uint32_t generateHash(const std::string& name);
10-
uint32_t generateExtensionHash(std::string_view extension);
11+
uint32_t generateHash(std::string_view name);
12+
uint32_t generateExtensionHash(VFS::Path::NormalizedView file);
1113

1214
enum class BA2Version : std::uint32_t
1315
{

components/bsa/ba2gnrlfile.cpp

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
#include <algorithm>
44
#include <cassert>
5-
#include <filesystem>
65
#include <format>
76
#include <fstream>
87

98
#include <zlib.h>
109

1110
#include <components/esm/fourcc.hpp>
1211
#include <components/files/constrainedfilestream.hpp>
13-
#include <components/files/conversion.hpp>
1412
#include <components/files/utils.hpp>
15-
#include <components/misc/strings/lower.hpp>
13+
#include <components/vfs/pathutil.hpp>
1614

1715
#include "ba2file.hpp"
1816
#include "memorystream.hpp"
@@ -139,28 +137,18 @@ namespace Bsa
139137
}
140138
}
141139

142-
#ifdef _WIN32
143-
const auto& path = str;
144-
#else
145-
// Force-convert the path into something UNIX can handle first
146-
// to make sure std::filesystem::path doesn't think the entire path is the filename on Linux
147-
// and subsequently purge it to determine the file folder.
148-
std::string path(str);
149-
std::replace(path.begin(), path.end(), '\\', '/');
150-
#endif
151-
152-
const auto p = std::filesystem::path{ path }; // Purposefully damage Unicode strings.
153-
const auto fileName = Misc::StringUtils::lowerCase(p.stem().string());
154-
const auto ext = Misc::StringUtils::lowerCase(p.extension().string()); // Purposefully damage Unicode strings.
155-
const auto folder = Misc::StringUtils::lowerCase(p.parent_path().string());
140+
const VFS::Path::Normalized path(str);
141+
142+
const std::string_view fileName = path.stem();
143+
const std::string_view folder = path.parent().value();
156144

157145
uint32_t folderHash = generateHash(folder);
158146
auto it = mFolders.find(folderHash);
159147
if (it == mFolders.end())
160148
return FileRecord(); // folder not found, return default which has offset of sInvalidOffset
161149

162150
uint32_t fileHash = generateHash(fileName);
163-
uint32_t extHash = generateExtensionHash(ext);
151+
uint32_t extHash = generateExtensionHash(path.filename());
164152
auto iter = it->second.find({ fileHash, extHash });
165153
if (iter == it->second.end())
166154
return FileRecord(); // file not found, return default which has offset of sInvalidOffset
@@ -183,16 +171,6 @@ namespace Bsa
183171
fail("Add file is not implemented for compressed BSA: " + filename);
184172
}
185173

186-
Files::IStreamPtr BA2GNRLFile::getFile(const char* file)
187-
{
188-
FileRecord fileRec = getFileRecord(file);
189-
if (!fileRec.isValid())
190-
{
191-
fail("File not found: " + std::string(file));
192-
}
193-
return getFile(fileRec);
194-
}
195-
196174
Files::IStreamPtr BA2GNRLFile::getFile(const FileRecord& fileRecord)
197175
{
198176
const uint32_t inputSize = fileRecord.packedSize ? fileRecord.packedSize : fileRecord.size;

components/bsa/ba2gnrlfile.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ namespace Bsa
4747
/// Read header information from the input source
4848
void readHeader(std::istream& input) override;
4949

50-
Files::IStreamPtr getFile(const char* filePath);
5150
Files::IStreamPtr getFile(const FileStruct* fileStruct);
5251
void addFile(const std::string& filename, std::istream& file);
5352
};

components/bsa/compressedbsafile.cpp

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <algorithm>
2828
#include <cassert>
2929
#include <cerrno>
30-
#include <filesystem>
3130
#include <format>
3231
#include <istream>
3332
#include <system_error>
@@ -38,7 +37,8 @@
3837
#include <components/files/constrainedfilestream.hpp>
3938
#include <components/files/conversion.hpp>
4039
#include <components/files/utils.hpp>
41-
#include <components/misc/strings/lower.hpp>
40+
#include <components/misc/pathhelpers.hpp>
41+
#include <components/vfs/pathutil.hpp>
4242

4343
#include "memorystream.hpp"
4444

@@ -218,26 +218,26 @@ namespace Bsa
218218
}
219219
}
220220

221-
#ifdef _WIN32
222-
const auto& path = str;
223-
#else
224-
// Force-convert the path into something UNIX can handle first
225-
// to make sure std::filesystem::path doesn't think the entire path is the filename on Linux
226-
// and subsequently purge it to determine the file folder.
227-
std::string path(str);
228-
std::replace(path.begin(), path.end(), '\\', '/');
229-
#endif
221+
const VFS::Path::Normalized path(str);
230222

231-
const auto p = std::filesystem::path{ path }; // Purposefully damage Unicode strings.
232-
const auto stem = p.stem();
233-
const auto ext = p.extension().string(); // Purposefully damage Unicode strings.
223+
const std::string_view stem = path.stem();
224+
const std::string_view folder = path.parent().value();
234225

235-
std::uint64_t folderHash = generateHash(p.parent_path(), {});
226+
std::uint64_t folderHash = generateHash(folder, {});
236227

237228
auto it = mFolders.find(folderHash);
238229
if (it == mFolders.end())
239230
return FileRecord();
240231

232+
const std::string_view file = path.filename().value();
233+
std::string_view ext;
234+
if (const std::size_t pos = Misc::findExtension(file); pos != std::string_view::npos)
235+
{
236+
// ext including .
237+
ext = file;
238+
ext.remove_prefix(pos);
239+
}
240+
241241
std::uint64_t fileHash = generateHash(stem, ext);
242242
auto iter = it->second.mFiles.find(fileHash);
243243
if (iter == it->second.mFiles.end())
@@ -262,16 +262,6 @@ namespace Bsa
262262
fail("Add file is not implemented for compressed BSA: " + filename);
263263
}
264264

265-
Files::IStreamPtr CompressedBSAFile::getFile(const char* file)
266-
{
267-
FileRecord fileRec = getFileRecord(file);
268-
if (fileRec.mOffset == std::numeric_limits<uint32_t>::max())
269-
{
270-
fail("File not found: " + std::string(file));
271-
}
272-
return getFile(fileRec);
273-
}
274-
275265
Files::IStreamPtr CompressedBSAFile::getFile(const FileRecord& fileRecord)
276266
{
277267
size_t size = fileRecord.mSize & (~FileSizeFlag_Compression);
@@ -337,29 +327,31 @@ namespace Bsa
337327
return std::make_unique<Files::StreamWithBuffer<MemoryInputStream>>(std::move(memoryStreamPtr));
338328
}
339329

340-
std::uint64_t CompressedBSAFile::generateHash(const std::filesystem::path& stem, std::string extension)
330+
std::uint64_t CompressedBSAFile::generateHash(std::string_view str, std::string_view extension)
341331
{
342-
auto str = stem.u8string();
343-
size_t len = str.length();
344-
if (len == 0)
332+
if (str.empty())
345333
return 0;
346-
std::replace(str.begin(), str.end(), '/', '\\');
347-
Misc::StringUtils::lowerCaseInPlace(str);
348-
uint64_t result = str[len - 1];
334+
const auto at = [&](std::size_t i) -> char {
335+
const char c = str[i];
336+
if (c == '/')
337+
return '\\';
338+
return c;
339+
};
340+
const size_t len = str.length();
341+
uint64_t result = at(len - 1);
349342
if (len >= 3)
350-
result |= str[len - 2] << 8;
343+
result |= at(len - 2) << 8;
351344
result |= len << 16;
352-
result |= static_cast<uint32_t>(str[0] << 24);
345+
result |= static_cast<uint32_t>(at(0) << 24);
353346
if (len >= 4)
354347
{
355348
uint32_t hash = 0;
356349
for (size_t i = 1; i <= len - 3; ++i)
357-
hash = hash * 0x1003f + str[i];
350+
hash = hash * 0x1003f + at(i);
358351
result += static_cast<uint64_t>(hash) << 32;
359352
}
360353
if (extension.empty())
361354
return result;
362-
Misc::StringUtils::lowerCaseInPlace(extension);
363355
if (extension == ".kf")
364356
result |= 0x80;
365357
else if (extension == ".nif")

components/bsa/compressedbsafile.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#ifndef OPENMW_COMPONENTS_BSA_COMPRESSEDBSAFILE_HPP
2727
#define OPENMW_COMPONENTS_BSA_COMPRESSEDBSAFILE_HPP
2828

29-
#include <filesystem>
3029
#include <limits>
3130
#include <map>
3231

@@ -112,7 +111,7 @@ namespace Bsa
112111
FileRecord getFileRecord(std::string_view str) const;
113112

114113
/// \brief Normalizes given filename or folder and generates format-compatible hash.
115-
static std::uint64_t generateHash(const std::filesystem::path& stem, std::string extension);
114+
static std::uint64_t generateHash(std::string_view stem, std::string_view extension);
116115
Files::IStreamPtr getFile(const FileRecord& fileRecord);
117116

118117
public:
@@ -127,7 +126,6 @@ namespace Bsa
127126
/// Read header information from the input source
128127
void readHeader(std::istream& input) override;
129128

130-
Files::IStreamPtr getFile(const char* filePath);
131129
Files::IStreamPtr getFile(const FileStruct* fileStruct);
132130
void addFile(const std::string& filename, std::istream& file);
133131
};

0 commit comments

Comments
 (0)