2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-16 01:37:03 +00:00

CVarManager: Bring back de/serialization

This commit is contained in:
2022-02-27 17:11:10 -08:00
parent d6f8ca44de
commit c79ddb8c42
13 changed files with 243 additions and 153 deletions

View File

@@ -17,6 +17,7 @@ void CMemoryStreamOut::Write(const u8* ptr, u32 len) {
if (len != 0) {
memcpy(x7c_ptr + x84_position, ptr, len);
x84_position += len;
}
}
}

View File

@@ -21,5 +21,6 @@ public:
~CMemoryStreamOut() override;
void Write(const u8* ptr, u32 len) override;
u32 GetWritePosition() const { return x84_position; }
};
} // namespace metaforce

View File

@@ -0,0 +1,24 @@
#include "Runtime/Streams/CTextInStream.hpp"
#include <algorithm>
namespace metaforce {
CTextInStream::CTextInStream(CInputStream& in, int len) : m_in(&in), m_len(len) {}
std::string CTextInStream::GetNextLine() {
std::string ret;
while (true) {
auto chr = m_in->ReadChar();
ret += chr;
if (ret.back() == '\r' || ret.back() == '\n') {
if (ret.back() == '\r') {
m_in->ReadChar();
}
break;
}
}
ret.erase(std::remove(ret.begin(), ret.end(), '\r'), ret.end());
ret.erase(std::remove(ret.begin(), ret.end(), '\n'), ret.end());
return ret;
}
} // namespace metaforce

View File

@@ -0,0 +1,14 @@
#pragma once
#include "Runtime/Streams/CInputStream.hpp"
namespace metaforce {
class CTextInStream {
CInputStream* m_in;
s32 m_len;
public:
CTextInStream(CInputStream& in, int len);
bool IsEOF() { return m_in->GetReadPosition() >= m_len; }
std::string GetNextLine();
};
}

View File

@@ -0,0 +1,29 @@
#include "Runtime/Streams/CTextOutStream.hpp"
namespace metaforce {
CTextOutStream::CTextOutStream(COutputStream& out) : m_out(&out) {}
void CTextOutStream::WriteString(const std::string& str) { CTextOutStream::WriteString(str.c_str(), str.length()); }
void CTextOutStream::WriteString(const char* str, u32 len) {
bool wroteCarriageReturn = false;
bool wroteLineFeed = false;
for (u32 i = 0; i < len; ++i) {
if (str[i] == '\r') {
wroteCarriageReturn = true;
} else if (str[i] == '\n' && !wroteCarriageReturn) {
m_out->WriteChar('\r');
wroteLineFeed = true;
}
m_out->WriteChar(str[i]);
}
/* If we didn't write either a line feed or carriage return we need to do that now */
if (!wroteCarriageReturn && !wroteLineFeed) {
m_out->WriteChar('\r');
m_out->WriteChar('\n');
}
/* Since this is a text buffer, we always want to flush after writing a string */
m_out->Flush();
}
} // namespace metaforce

View File

@@ -0,0 +1,14 @@
#pragma once
#include "Runtime/Streams/COutputStream.hpp"
namespace metaforce {
class CTextOutStream {
COutputStream* m_out;
public:
CTextOutStream(COutputStream& out);
void WriteString(const std::string& str);
void WriteString(const char* str, u32 len);
};
}