Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Odex64 committed May 30, 2024
1 parent df9abd4 commit 6826680
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Editor/Binary/Binary.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export module Binary;
import std;

export template <typename T>
concept BinaryType = std::integral<T> || std::floating_point<T>;
concept BinaryType = std::integral<T> || std::floating_point<T> || std::same_as<T, std::string>;

template<typename T>
concept DerivedStream = std::derived_from<T, std::ios>;
Expand Down
15 changes: 6 additions & 9 deletions Editor/Binary/BinaryWriter.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ public:
BinaryWriter& operator=(BinaryWriter&&) = delete;

template<BinaryType T>
void Write(T data)
void Write(const T& data)
{
Stream->write(reinterpret_cast<char*>(&data), sizeof(T));
Stream->write(reinterpret_cast<const char*>(&data), sizeof(T));
}

void Write(const std::string& data, bool writeSize = true)
template<>
void Write(const std::string& data)
{
std::size_t size{ data.size() };
if (writeSize) {
Stream->write(reinterpret_cast<char*>(&size), sizeof(std::uint32_t));
}

Stream->write(data.c_str(), size);
}

Expand All @@ -42,8 +39,8 @@ public:

[[nodiscard]] std::string ToString() const noexcept override
{
if (std::ostringstream* d = dynamic_cast<std::ostringstream*>(Stream.get()); d != nullptr) {
return d->str();
if (std::ostringstream* stream = dynamic_cast<std::ostringstream*>(Stream.get()); stream != nullptr) {
return stream->str();
}

return std::string{};
Expand Down
7 changes: 3 additions & 4 deletions Editor/Main.ixx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import std;
import Xnb;
import BinaryReader;
import Texture;

int main()
{
//std::expected<Xnb, std::string> file{ Xnb::Read("Images/Misc/profile_bg2-copy.xnb") };
//std::expected<Xnb, std::string> file{ Xnb::Read("profile_bg.xnb") };
//if (!file.has_value()) return 1;
//file.value().Type->Export("Images/Misc/profile_bg2.png");
//std::expected<Xnb, std::string> file{ Xnb::Write<Texture>("Images/Misc/profile_bg2.png") };
//file.value().Type->Export("profile_bg.png");
std::expected<Xnb, std::string> file{ Xnb::Write<Texture>("profile_bg.png") };

//for (const auto& entry : std::filesystem::recursive_directory_iterator("Images/Misc/SFDLogo")) {
// if (entry.is_regular_file() && entry.path().extension() == ".png") {
Expand Down
1 change: 0 additions & 1 deletion Editor/Parser/Texture.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module;

export module Texture;

import std;
import Converter;
import BinaryReader;
import BinaryWriter;
Expand Down
14 changes: 7 additions & 7 deletions Editor/Parser/Xnb.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public:
if (!std::filesystem::exists(file)) return std::unexpected("File does not exist");

BinaryWriter stream{ file.relative_path().replace_extension(".xnb") };
stream.Write("XNB", false);
stream.Write<std::string>("XNB");
stream.Write<std::uint8_t>(static_cast<std::uint8_t>('w'));
stream.Write<std::uint8_t>(5);
stream.Write<std::uint8_t>(128);
Expand All @@ -161,12 +161,12 @@ public:

BinaryWriter memoryStream{};
memoryStream.Write7BitEncodedInteger(1);
memoryStream.Write7BitEncodedInteger(type->ReaderName().size());
memoryStream.Write(type->ReaderName(), false);
memoryStream.Write7BitEncodedInteger(static_cast<std::int32_t>(type->ReaderName().size()));
memoryStream.Write<std::string>(type->ReaderName());
memoryStream.Write<std::int32_t>(0);
memoryStream.Write7BitEncodedInteger(0);
memoryStream.Write7BitEncodedInteger(1);
memoryStream.Write(type->Binary(), false);
memoryStream.Write<std::string>(type->Binary());

const std::string decompressedData{ memoryStream.ToString() };
const std::size_t decompressedSize{ decompressedData.size() };
Expand Down Expand Up @@ -194,9 +194,9 @@ public:

compressedData.resize(compressedSize);

stream.Write<std::uint32_t>(compressedSize + _headerSize);
stream.Write<std::uint32_t>(decompressedSize);
stream.Write(compressedData, false);
stream.Write<std::uint32_t>(static_cast<std::uint32_t>(compressedSize + _headerSize));
stream.Write<std::uint32_t>(static_cast<std::uint32_t>(decompressedSize));
stream.Write<std::string>(compressedData);

return std::expected<Xnb, std::string>(
std::in_place,
Expand Down

0 comments on commit 6826680

Please sign in to comment.