Skip to content

Commit 51c6ab5

Browse files
committed
(Emscripten) Working on emscripten support.
1 parent 990ca10 commit 51c6ab5

18 files changed

Lines changed: 172 additions & 181 deletions

File tree

inc/Utils/FileSystem/FileSystem.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,16 @@ namespace SR_UTILS_NS {
2525
static bool WriteToFile(const std::string& path, const std::string_view& text);
2626
static std::string NormalizePath(const std::string& path);
2727
static void NormalizePathInPlace(std::string& path);
28-
2928
static std::vector<std::string> ReadAllLines(const SR_UTILS_NS::Path& path);
3029

3130
static uint64_t ReadHashFromFile(const SR_UTILS_NS::Path& path);
3231
static bool WriteHashToFile(const SR_UTILS_NS::Path& path, uint64_t hash);
3332

34-
static std::string ReadBinaryAsString(const Path& path, bool checkError = true);
35-
static std::vector<char> ReadBinary(std::string_view path);
36-
static std::vector<uint8_t> ReadFileAsVector(const std::string& path);
37-
static std::shared_ptr<std::vector<uint8_t>> ReadFileAsBlob(const std::string& path);
33+
static std::shared_ptr<std::string> ReadFileAsBlob(const std::string& path);
34+
static bool ReadFile(const Path& path, std::string& buffer);
3835

39-
static std::string ReadAllText(const std::string& path);
4036
static std::vector<std::string_view> ReadAllTextAsStringViewVector(const Path& path, std::string& buffer);
4137

42-
static char* Load(std::string path);
43-
4438
static void ForEachFileInFolder(const Path& path, bool recursive, const SR_HTYPES_NS::Function<void(const Path&)>& func);
4539

4640
static uint64_t GetFileHash(const std::string& path);

inc/Utils/FileSystem/Path.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,10 @@ template<> struct fmt::formatter<SR_UTILS_NS::Path>
134134
}
135135
};
136136

137+
template<> struct std::hash<SR_UTILS_NS::Path> {
138+
size_t operator()(const SR_UTILS_NS::Path& path) const noexcept {
139+
return path.GetHash();
140+
}
141+
};
142+
137143
#endif //SR_ENGINE_PATH_H

inc/Utils/FileSystem/VirtualFS.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Created by Monika on 02.03.2026.
3+
//
4+
5+
#ifndef SR_ENGINE_COMMON_VIRTUAL_FS_H
6+
#define SR_ENGINE_COMMON_VIRTUAL_FS_H
7+
8+
#include <Utils/Common/Singleton.h>
9+
#include <Utils/FileSystem/Path.h>
10+
#include <Utils/Types/FlatHashMap.h>
11+
12+
namespace SR_UTILS_NS {
13+
class VirtualFS : public Singleton<VirtualFS> {
14+
SR_REGISTER_SINGLETON(VirtualFS);
15+
public:
16+
SR_NODISCARD bool IsFileExist(const Path& path) const;
17+
SR_NODISCARD bool TryReadFile(const Path& path, std::string& buffer) const;
18+
SR_NODISCARD bool WriteFile(const Path& path, std::string_view data);
19+
20+
private:
21+
SR_HTYPES_NS::FlatHashMap<Path, std::string> m_files;
22+
23+
};
24+
}
25+
26+
#endif //SR_ENGINE_COMMON_VIRTUAL_FS_H

inc/Utils/Types/FlatHashMap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace SR_HTYPES_NS {
3131
SR_NODISCARD uint64_t size() const { return m_map.size(); }
3232
SR_NODISCARD uint64_t count(const Key& key) const { return m_map.count(key); }
3333
SR_NODISCARD bool empty() const { return m_map.empty(); }
34+
SR_NODISCARD bool contains(const Key& key) const { return m_map.count(key) > 0; }
3435
bool erase(const Key& key) { return m_map.erase(key) > 0; }
3536
Iterator erase(ConstIterator pos) { return m_map.erase(pos); }
3637
Iterator erase(ConstIterator first, ConstIterator last) { return m_map.erase(first, last); }

src/Utils/Common/Features.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace SR_UTILS_NS {
1515
static const StringAtom SR_FEATURE_VARIANT_RELEASE_ID = "Release";
1616
static const StringAtom SR_FEATURE_VARIANT_VALIDATION_ID = "Validation";
1717
static const StringAtom SR_FEATURE_VARIANT_EMSCRIPTEN_ID = "Emscripten";
18+
static const StringAtom SR_FEATURE_VARIANT_WINDOWS_ID = "Windows";
1819

1920
bool FeatureGroup::Register(StringAtom name, bool value) {
2021
if (m_values.count(name)) {
@@ -81,13 +82,16 @@ namespace SR_UTILS_NS {
8182
std::vector<StringAtom> groups = { SR_FEATURE_COMMON_GROUP_ID };
8283
std::vector<StringAtom> variants = {
8384
#ifdef SR_DEBUG
84-
SR_FEATURE_VARIANT_DEBUG_ID
85+
SR_FEATURE_VARIANT_DEBUG_ID,
8586
#endif
8687
#ifdef SR_RELEASE
87-
SR_FEATURE_VARIANT_RELEASE_ID
88+
SR_FEATURE_VARIANT_RELEASE_ID,
8889
#endif
8990
#if defined(SR_EMSCRIPTEN)
90-
SR_FEATURE_VARIANT_EMSCRIPTEN_ID
91+
SR_FEATURE_VARIANT_EMSCRIPTEN_ID,
92+
#endif
93+
#if defined(SR_WIN32)
94+
SR_FEATURE_VARIANT_WINDOWS_ID,
9195
#endif
9296
};
9397

src/Utils/Debug.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,16 @@ namespace SR_UTILS_NS {
101101

102102
InitColorTheme();
103103

104-
#ifndef SR_ANDROID
104+
#if !defined(SR_ANDROID) && !defined(SR_EMSCRIPTEN)
105105
auto&& successfulPath = SR_PLATFORM_NS::GetApplicationPath().GetFolder().Concat("/successful");
106106
if (successfulPath.Exists(Path::Type::File))
107107
Platform::Delete(successfulPath);
108108
#endif
109109

110+
m_isInit = true;
111+
m_showUseMemory = ShowUsedMemory;
112+
113+
#ifndef SR_EMSCRIPTEN
110114
m_logPath = Path(log_path);
111115

112116
if (!m_logPath.GetFolder().CreateIfNotExists()) {
@@ -122,10 +126,8 @@ namespace SR_UTILS_NS {
122126
SR_PLATFORM_NS::WriteConsoleError("Debug::Init() : failed to open log file!\n\tLog path: " + m_logPath.ToString());
123127
}
124128

125-
m_isInit = true;
126-
m_showUseMemory = ShowUsedMemory;
127-
128129
Print("Debugger has been initialized. \n\tLog path: " + m_logPath.ToString(), DebugLogType::Debug);
130+
#endif
129131

130132
#ifdef SR_COMMON_GIT_METADATA
131133
std::string gitMetadata;

src/Utils/FileSystem/FileSystem.cpp

Lines changed: 18 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
//
44

55
#include <Utils/FileSystem/FileSystem.h>
6-
#include <Utils/Debug.h>
6+
#include <Utils/FileSystem/VirtualFS.h>
77
#include <Utils/Platform/Platform.h>
8-
#include <Utils/FileSystem/Path.h>
98
#include <Utils/Common/Hashes.h>
109
#include <Utils/Common/StringUtils.h>
1110
#include <Utils/Profile/TracyContext.h>
@@ -21,33 +20,11 @@ namespace SR_UTILS_NS {
2120
return data;
2221
}
2322

24-
SR_COMMON_DLL_API char* FileSystem::Load(std::string path) {
25-
/// open file
26-
std::ifstream infile(path);
27-
if (!infile.is_open()) {
28-
SR_ERROR("FileSystem::Load() : failed open file!\n\tPath: "+path);
29-
return nullptr;
30-
}
31-
32-
33-
/// get length of file
34-
infile.seekg(0, std::ios::end);
35-
size_t length = infile.tellg();
36-
infile.seekg(0, std::ios::beg);
37-
38-
char* buffer = new char[length];
39-
40-
/// read file
41-
infile.read(buffer, length);
42-
43-
return buffer;
44-
}
45-
4623
SR_COMMON_DLL_API std::vector<std::string_view> FileSystem::ReadAllTextAsStringViewVector(const Path& path, std::string& buffer) {
4724
SR_TRACY_ZONE;
4825
std::vector<std::string_view> result;
4926

50-
if (!SR_PLATFORM_NS::ReadFile(path, buffer)) {
27+
if (!SR_UTILS_NS::FileSystem::ReadFile(path, buffer)) {
5128
return result;
5229
}
5330

@@ -78,88 +55,6 @@ namespace SR_UTILS_NS {
7855
return result;
7956
}
8057

81-
SR_COMMON_DLL_API std::string FileSystem::ReadAllText(const std::string& path) {
82-
SR_TRACY_ZONE;
83-
84-
std::string data = std::string();
85-
std::ifstream stream(path, std::ios::in);
86-
if (!stream) {
87-
SR_ERROR("FileSystem::ReadAllText() : failed to open \"" + path + "\" file!");
88-
return data;
89-
}
90-
91-
stream.seekg(0, std::ios::end);
92-
std::streampos bytes = stream.tellg();
93-
stream.seekg(0, std::ios::beg);
94-
data.reserve(bytes);
95-
96-
if (stream.is_open()) {
97-
std::string line;
98-
bool first = false;
99-
while (getline(stream, line)) {
100-
if (!first) {
101-
first = true;
102-
data += line;
103-
}
104-
else {
105-
data += "\n";
106-
data += line;
107-
}
108-
}
109-
stream.close();
110-
}
111-
return data;
112-
}
113-
114-
SR_COMMON_DLL_API std::vector<uint8_t> FileSystem::ReadFileAsVector(const std::string &path) {
115-
std::ifstream file(path, std::ifstream::binary | std::ios::in);
116-
117-
if (file.fail()) {
118-
return std::vector<uint8_t>();
119-
}
120-
121-
file.seekg(0, std::ios::end);
122-
std::streampos end = file.tellg();
123-
file.seekg(0, std::ios::beg);
124-
std::streampos start = file.tellg();
125-
size_t size = static_cast<size_t>(end - start);
126-
127-
std::vector<uint8_t> result(size);
128-
129-
file.read(reinterpret_cast<char*>(result.data()), size);
130-
131-
return result;
132-
}
133-
134-
SR_COMMON_DLL_API std::vector<char> FileSystem::ReadBinary(const std::string_view path) {
135-
/*std::ifstream ifd(path, std::ios::binary | std::ios::ate);
136-
int size = ifd.tellg();
137-
ifd.seekg(0, std::ios::beg);
138-
std::vector<char> buffer;
139-
buffer.resize(size); // << resize not reserve
140-
ifd.read(buffer.data(), size);*/
141-
142-
//std::ifstream input(path, std::ios::binary);
143-
//std::vector<uint32_t> buffer(std::istreambuf_iterator<char>(input), {});
144-
145-
std::ifstream file(path.data(), std::ios::ate | std::ios::binary);
146-
147-
if (!file.is_open()) {
148-
SR_ERROR("FileSystem::ReadBinary() : failed to open \"{}\"file!", path);
149-
return std::vector<char>();
150-
}
151-
152-
size_t fileSize = (size_t) file.tellg();
153-
std::vector<char> buffer(fileSize);
154-
155-
file.seekg(0);
156-
file.read(buffer.data(), fileSize);
157-
158-
file.close();
159-
160-
return buffer;
161-
}
162-
16358
SR_COMMON_DLL_API bool FileSystem::CreatePath(std::string path, uint32_t offset) {
16459
if (path.empty()) {
16560
return false;
@@ -187,15 +82,11 @@ namespace SR_UTILS_NS {
18782
#endif
18883
}
18984

190-
//std::string FileSystem::GetFullPath(const std::string& path) {
191-
//#ifdef SR_WIN32
192-
// char fullFilename[MAX_PATH];
193-
// GetFullPathName(path.c_str(), MAX_PATH, fullFilename, nullptr);
194-
// return std::string(fullFilename);
195-
//#else
196-
// return std::string();
197-
//#endif
198-
//}
85+
SR_COMMON_DLL_API bool FileSystem::ReadFile(const Path& path, std::string& buffer) {
86+
SR_TRACY_ZONE;
87+
88+
return SR_PLATFORM_NS::ReadFile(path, buffer);
89+
}
19990

20091
SR_COMMON_DLL_API std::string FileSystem::NormalizePath(const std::string &path) {
20192
SR_TRACY_ZONE;
@@ -228,29 +119,6 @@ namespace SR_UTILS_NS {
228119
return true;
229120
}
230121

231-
SR_COMMON_DLL_API std::string FileSystem::ReadBinaryAsString(const Path& path, bool checkError) {
232-
SR_TRACY_ZONE;
233-
234-
std::ifstream file(path.ToStringRef(), std::ios::ate | std::ios::binary);
235-
236-
if (!file.is_open()) {
237-
SR_UNUSED_VARIABLE(checkError);
238-
SRAssert2(!checkError, "FileSystem::ReadBinaryAsString() : failed to open \"" + path.ToStringRef() + "\" file!");
239-
return std::string();
240-
}
241-
242-
size_t fileSize = (size_t) file.tellg();
243-
std::string buffer;
244-
buffer.resize(fileSize);
245-
246-
file.seekg(0);
247-
file.read(buffer.data(), fileSize);
248-
249-
file.close();
250-
251-
return buffer;
252-
}
253-
254122
SR_COMMON_DLL_API uint64_t FileSystem::GetFileHash(const std::string& path) {
255123
SR_TRACY_ZONE;
256124
SR_TRACY_ZONE_TEXT(path);
@@ -259,7 +127,7 @@ namespace SR_UTILS_NS {
259127

260128
{
261129
SR_TRACY_ZONE_N("Read file");
262-
if (!SR_PLATFORM_NS::ReadFile(path, buffer)) {
130+
if (!SR_UTILS_NS::FileSystem::ReadFile(path, buffer)) {
263131
SR_WARN("FileSystem::GetFileHash() : failed to read file!\n\tPath: " + path);
264132
return SR_UINT64_MAX;
265133
}
@@ -297,8 +165,16 @@ namespace SR_UTILS_NS {
297165
return hash;
298166
}
299167

300-
SR_COMMON_DLL_API std::shared_ptr<std::vector<uint8_t>> FileSystem::ReadFileAsBlob(const std::string &path) {
301-
return std::make_shared<std::vector<uint8_t>>(std::move(ReadFileAsVector(path)));
168+
SR_COMMON_DLL_API std::shared_ptr<std::string> FileSystem::ReadFileAsBlob(const std::string& path) {
169+
SR_TRACY_ZONE;
170+
171+
std::shared_ptr<std::string> pBuffer = std::make_shared<std::string>();
172+
if (!SR_UTILS_NS::FileSystem::ReadFile(path, *pBuffer)) {
173+
SR_ERROR("FileSystem::ReadFileAsBlob() : failed to read file!\n\tPath: " + path);
174+
return nullptr;
175+
}
176+
177+
return pBuffer;
302178
}
303179

304180
SR_COMMON_DLL_API uint64_t FileSystem::ReadHashFromFile(const Path& path) {

src/Utils/FileSystem/VirtualFS.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Created by Monika on 02.03.2026.
3+
//
4+
5+
#include <Utils/FileSystem/VirtualFS.h>
6+
#include <Utils/Types/LockGuard.h>
7+
8+
namespace SR_UTILS_NS {
9+
bool VirtualFS::IsFileExist(const Path& path) const {
10+
SR_LOCK_GUARD;
11+
return m_files.contains(path);
12+
}
13+
14+
bool VirtualFS::TryReadFile(const Path& path, std::string& buffer) const {
15+
SR_LOCK_GUARD;
16+
auto it = m_files.find(path);
17+
if (it != m_files.end()) {
18+
buffer = it->second;
19+
return true;
20+
}
21+
return false;
22+
}
23+
24+
bool VirtualFS::WriteFile(const Path& path, std::string_view data) {
25+
SR_LOCK_GUARD;
26+
m_files[path] = data;
27+
return true;
28+
}
29+
}

0 commit comments

Comments
 (0)