Skip to content

Commit 3dcda21

Browse files
committed
use custom io streams for 64bit memory support
1 parent 3142815 commit 3dcda21

18 files changed

Lines changed: 333 additions & 231 deletions

File tree

libsave/include/SatisfactorySave/IO/Archive/IStreamArchive.h

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
#pragma once
22

3-
#include <fstream>
43
#include <memory>
54
#include <stack>
65
#include <string>
76
#include <vector>
87

98
#include "../../Utils/StackUtils.h"
9+
#include "../IOStream.h"
1010
#include "Archive.h"
1111

1212
namespace SatisfactorySave {
1313

1414
class SATISFACTORYSAVE_API IStreamArchive : public Archive {
1515
public:
16-
explicit IStreamArchive(std::unique_ptr<std::istream> istream) : istream_(std::move(istream)) {}
16+
explicit IStreamArchive(std::vector<char>&& buf)
17+
: data_buf_(std::make_unique<std::vector<char>>(std::move(buf))) {
18+
istream_ = std::make_unique<MemoryIStream>(std::as_bytes(std::span{data_buf_->data(), data_buf_->size()}));
19+
}
20+
21+
explicit IStreamArchive(std::span<const char> buf) {
22+
istream_ = std::make_unique<MemoryIStream>(std::as_bytes(buf));
23+
}
24+
25+
explicit IStreamArchive(const std::filesystem::path& path) {
26+
istream_ = std::make_unique<FileIStream>(path);
27+
}
1728

1829
template<typename T>
1930
inline T read() {
@@ -53,15 +64,15 @@ namespace SatisfactorySave {
5364
}
5465

5566
std::size_t tell() override {
56-
return static_cast<std::size_t>(istream_->tellg());
67+
return istream_->tell();
5768
}
5869

5970
void seek(std::size_t pos) override {
60-
istream_->seekg(static_cast<std::istream::pos_type>(pos));
71+
istream_->seek(pos);
6172
}
6273

63-
std::istream& rawStream() {
64-
return *istream_;
74+
[[nodiscard]] std::size_t size() const {
75+
return istream_->size();
6576
}
6677

6778
inline auto pushReadLimit(std::size_t size) {
@@ -81,8 +92,6 @@ namespace SatisfactorySave {
8192
}
8293

8394
protected:
84-
IStreamArchive() = default;
85-
8695
void serialize(void* data, std::size_t size) override;
8796

8897
void serializeString(std::string& s) override;
@@ -91,32 +100,9 @@ namespace SatisfactorySave {
91100

92101
void validateReadLimit(std::size_t size) override;
93102

94-
std::unique_ptr<std::istream> istream_;
103+
std::unique_ptr<std::vector<char>> data_buf_;
104+
std::unique_ptr<IStream> istream_;
95105
std::stack<std::size_t> read_limits_;
96106
std::stack<std::string> parent_class_info_;
97107
};
98-
99-
class SATISFACTORYSAVE_API IFStreamArchive : public IStreamArchive {
100-
public:
101-
explicit IFStreamArchive(const std::filesystem::path& filepath) {
102-
auto file = std::make_unique<std::ifstream>(filepath, std::ios::binary);
103-
if (!file->is_open()) {
104-
throw std::runtime_error("Cannot read file: " + filepath.string());
105-
}
106-
107-
// File size
108-
file->seekg(0, std::ios::end);
109-
filesize_ = file->tellg();
110-
file->seekg(0, std::ios::beg);
111-
112-
istream_ = std::move(file);
113-
}
114-
115-
[[nodiscard]] std::size_t size() const {
116-
return filesize_;
117-
}
118-
119-
protected:
120-
std::size_t filesize_ = 0;
121-
};
122108
} // namespace SatisfactorySave
Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
#pragma once
22

3-
#include <fstream>
43
#include <memory>
54

6-
#include "../MemoryStreams.h"
5+
#include "../IOStream.h"
76
#include "Archive.h"
87

98
namespace SatisfactorySave {
109

1110
class SATISFACTORYSAVE_API OStreamArchive : public Archive {
1211
public:
13-
explicit OStreamArchive(std::unique_ptr<std::ostream> ostream) : ostream_(std::move(ostream)) {}
12+
explicit OStreamArchive() : is_memory_stream_(true) {
13+
ostream_ = std::make_unique<MemoryOStream>();
14+
}
15+
16+
explicit OStreamArchive(const std::filesystem::path& filepath) : is_memory_stream_(false) {
17+
ostream_ = std::make_unique<FileOStream>(filepath);
18+
}
1419

1520
template<typename T>
1621
void write(T value) {
@@ -30,49 +35,28 @@ namespace SatisfactorySave {
3035
}
3136

3237
std::size_t tell() override {
33-
return static_cast<std::size_t>(ostream_->tellp());
38+
return ostream_->tell();
3439
}
3540

3641
void seek(std::size_t pos) override {
37-
ostream_->seekp(static_cast<std::istream::pos_type>(pos));
42+
ostream_->seek(pos);
3843
}
3944

40-
std::ostream& rawStream() {
41-
return *ostream_;
45+
[[nodiscard]] std::span<const std::byte> buffer_view() const {
46+
if (is_memory_stream_) {
47+
return dynamic_cast<MemoryOStream&>(*ostream_).buffer_view();
48+
}
49+
throw std::runtime_error("Not a memory stream! Cannot get buffer view!");
4250
}
4351

4452
protected:
45-
OStreamArchive() = default;
46-
4753
void serialize(void* data, std::size_t size) override;
4854

4955
void serializeString(std::string& s) override;
5056

5157
void serializeName(FName& n) override;
5258

53-
std::unique_ptr<std::ostream> ostream_;
54-
};
55-
56-
class SATISFACTORYSAVE_API OFStreamArchive : public OStreamArchive {
57-
public:
58-
explicit OFStreamArchive(const std::filesystem::path& filepath) {
59-
auto file = std::make_unique<std::ofstream>(filepath, std::ios::binary);
60-
if (!file->is_open()) {
61-
throw std::runtime_error("Cannot write file!");
62-
}
63-
64-
ostream_ = std::move(file);
65-
}
66-
};
67-
68-
class SATISFACTORYSAVE_API OMemStreamArchive : public OStreamArchive {
69-
public:
70-
explicit OMemStreamArchive() {
71-
ostream_ = std::make_unique<MemOStream>();
72-
}
73-
74-
[[nodiscard]] const std::vector<char>& data() const {
75-
return dynamic_cast<MemOStream&>(*ostream_).data();
76-
}
59+
std::unique_ptr<OStream> ostream_;
60+
bool is_memory_stream_;
7761
};
7862
} // namespace SatisfactorySave

0 commit comments

Comments
 (0)