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
1212namespace 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
0 commit comments