This repository was archived by the owner on Mar 11, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +31
-52
lines changed Expand file tree Collapse file tree 4 files changed +31
-52
lines changed Original file line number Diff line number Diff line change @@ -135,40 +135,6 @@ namespace tkge::Assets
135
135
{
136
136
public:
137
137
virtual ~IAsset () = default ;
138
-
139
- protected:
140
- [[nodiscard]] virtual ReadonlyByteStream& byteStream () noexcept = 0;
141
- [[nodiscard]] virtual const ReadonlyByteStream& byteStream () const noexcept = 0;
142
- };
143
-
144
- template <typename T>
145
- class IMoveableAsset : public virtual IAsset
146
- {
147
- public:
148
- explicit IMoveableAsset (std::string filename) : _byteStream(std::move(filename)) {}
149
- // static_assert(!std::is_base_of_v<ICopyableAsset<T>, T>, "Cannot derive from both move and copy assets!");
150
- ~IMoveableAsset () override = default ;
151
- [[nodiscard]] ReadonlyByteStream& byteStream () noexcept override { return this ->_byteStream ; }
152
- [[nodiscard]] const ReadonlyByteStream& byteStream () const noexcept override { return this ->_byteStream ; }
153
-
154
- private:
155
- ReadonlyByteStream _byteStream;
156
-
157
- friend T;
158
- };
159
- template <typename T>
160
- class ICopyableAsset : public virtual IAsset
161
- {
162
- public:
163
- explicit ICopyableAsset (std::string filename) : _byteStream(std::make_shared<ReadonlyByteStream>(std::move(filename))) {}
164
- // static_assert(!std::is_base_of_v<IMoveableAsset<T>, T>, "Cannot derive from both move and copy assets!");
165
- ~ICopyableAsset () override = default ;
166
- [[nodiscard]] ReadonlyByteStream& byteStream () noexcept override { return *this ->_byteStream ; }
167
- [[nodiscard]] const ReadonlyByteStream& byteStream () const noexcept override { return *this ->_byteStream ; }
168
-
169
- private:
170
- std::shared_ptr<ReadonlyByteStream> _byteStream;
171
-
172
- friend T;
138
+ virtual bool Load (ReadonlyByteStream) = 0;
173
139
};
174
140
} // namespace tkge::Assets
Original file line number Diff line number Diff line change 4
4
5
5
namespace tkge ::Assets
6
6
{
7
- class TextAsset final : public ICopyableAsset<TextAsset>
7
+ class TextAsset final : public IAsset
8
8
{
9
9
public:
10
- explicit TextAsset (std::string filename) : ICopyableAsset(std::move(filename)) {}
11
- [[nodiscard]] std::string ReadAllText () const ;
12
- [[nodiscard]] std::string ReadAt (std::size_t position, std::size_t size) const ;
10
+ explicit TextAsset () = default;
11
+ bool Load (ReadonlyByteStream byteStream) override ;
12
+
13
+ [[nodiscard]] const std::string& Text () const noexcept { return this ->_text ; }
14
+
15
+ private:
16
+ std::string _text;
13
17
};
14
18
} // namespace tkge::Assets
Original file line number Diff line number Diff line change @@ -10,20 +10,33 @@ namespace tkge
10
10
class AssetLoader
11
11
{
12
12
public:
13
+ explicit AssetLoader () : _paths(this ->GetSearchPaths ()) {}
14
+ AssetLoader (const AssetLoader&) = delete;
15
+ AssetLoader (AssetLoader&&) = default;
16
+ AssetLoader& operator =(const AssetLoader&) = delete;
17
+ AssetLoader& operator =(AssetLoader&&) = default ;
18
+ ~AssetLoader () = default ;
19
+
13
20
template <typename T>
14
21
requires std::is_base_of_v<Assets::IAsset, T>
15
22
std::unique_ptr<T> LoadAsset (const std::string& fileName) const
16
23
{
17
- const std::vector<std::filesystem::path> paths = this ->GetSearchPaths ();
18
-
19
- for (const auto & path : paths)
24
+ for (const auto & path : this ->_paths )
20
25
{
21
- if (std::filesystem::exists (path / fileName)) { return std::make_unique<T>((path / fileName).string ()); }
26
+ if (std::filesystem::exists (path / fileName))
27
+ {
28
+ auto asset = std::make_unique<T>();
29
+ asset->Load (Assets::ReadonlyByteStream{(path / fileName).string ()});
30
+ return asset;
31
+ }
22
32
}
23
33
24
34
throw std::runtime_error (" Asset not found: " + fileName);
25
35
}
26
36
27
37
[[nodiscard]] std::vector<std::filesystem::path> GetSearchPaths () const ;
38
+
39
+ private:
40
+ std::vector<std::filesystem::path> _paths;
28
41
};
29
42
} // namespace tkge
Original file line number Diff line number Diff line change 1
1
#include < tkge/Assets/TextAsset.hpp>
2
- std::string tkge::Assets::TextAsset::ReadAllText () const
3
- {
4
- const auto vData = this ->byteStream ().AsSpan <char >(0 , this ->byteStream ().GetStreamSize ());
5
- return std::string{vData.begin (), vData.end ()};
6
- }
7
2
8
- std::string tkge::Assets::TextAsset::ReadAt (std:: size_t position, std:: size_t size) const
3
+ bool tkge::Assets::TextAsset::Load (ReadonlyByteStream byteStream)
9
4
{
10
- const auto vData = this ->byteStream ().AsSpan <char >(position, size);
11
- return std::string{vData.begin (), vData.end ()};
12
- }
5
+ const auto vData = byteStream.AsSpan <char >(0 , byteStream.GetStreamSize ());
6
+ this ->_text = std::string{vData.begin (), vData.end ()};
7
+ return true ;
8
+ }
You can’t perform that action at this time.
0 commit comments