Implement FlxStreamBuffer and beginnings of ConsoleCommands in unreal

This commit is contained in:
2023-10-24 01:44:09 -04:00
parent 9dc9d31d54
commit 5381b5708a
6 changed files with 106 additions and 219 deletions

View File

@@ -3,125 +3,42 @@
#include "CoreMinimal.h"
#include "lpx-basebuffer.hpp"
/////////////////////////////////////////////////////
//
// FlxStringDecoder
//
// This class is used to decipher 8-bit strings that
// contain packed ints, strings, and other data.
// The typical example of usage is to decipher the
// serialized animation queues fed to us by Luprex.
//
// The FlxStringDecoder doesn't make a copy of the string
// you pass in, instead, it stores a pointer to it.
// So be sure not to free the string until you're
// done analyzing it.
//
// You'll note that some of the function names are
// lowercase, with underscores. That's because they're
// inherited from Luprex classes, and luprex classes
// use that naming convention. There's not any easy
// workaround for that.
//
/////////////////////////////////////////////////////
class FlxStringDecoder : public BaseReader<FlxStringDecoder> {
class FlxStreamBufferCore {
private:
const char* Text;
size_t Size;
bool err_eof_on_read_;
bool err_string_too_long_;
bool err_integer_truncated_;
protected:
public:
// You can check and clear these error flags at will.
//
bool ErrBeyondEOF;
bool ErrStringTooLong;
public:
// This function is required by BaseReader.
// It's not meant for end users.
//
void read_bytes_into(char* buffer, size_t size) {
if (size > Size) {
memset(buffer, 0, size);
ErrBeyondEOF = true;
set_at_eof();
}
else {
memcpy(buffer, Text, size);
Text += size;
Size -= size;
}
}
// This function is required by BaseReader.
// It's not meant for end users.
//
void raise_string_too_long() {
ErrStringTooLong = true;
}
public:
// The type returned by read_string.
//
using read_string_type = std::string;
// Initialize the string decoder with a text to analyze.
//
FlxStringDecoder(std::string_view s);
// Get the remaining text as a string_view.
//
std::string_view GetRest();
// Reinitialize with a new string_view.
//
// If clear is true, clears the error flags.
//
void Reset(std::string_view s, bool clear);
// Get the size of the remaining text.
//
size_t get_size() { return Size; }
void *basebuffer_malloc(size_t size) { return malloc(size); }
void basebuffer_free(void *p) { free(p); }
void clear_error_flags() { err_eof_on_read_ = err_string_too_long_ = err_integer_truncated_ = false; }
void raise_eof_on_read() { err_eof_on_read_ = true; }
void raise_string_too_long() { err_string_too_long_ = true; }
void raise_integer_truncated() { err_integer_truncated_ = true; }
// Return true if the remaining text is empty.
//
bool at_eof() { return Size == 0; }
// Move the cursor to EOF.
//
void set_at_eof();
// Read a string as a string_view
//
// This reads a string from the source, returning
// it as a string_view that points into the buffer.
// Naturally, if you release the buffer, the
// string_view is invalidated.
//
// This is considerably faster than read_string.
//
std::string_view read_string_view();
// Inherited Methods:
//
// The following methods are inherited from BaseReader:
//
// uint8_t read_uint8();
// uint16_t read_uint16();
// uint32_t read_uint32();
// uint64_t read_uint64();
// int8_t read_int8();
// int16_t read_int16();
// int32_t read_int32();
// int64_t read_int64();
// bool read_bool();
// char read_char();
// float read_float();
// double read_double();
// size_t read_length();
// std::string read_string_limit(uint64_t size);
// std::string read_string();
//
bool get_err_eof_on_read() const { return err_eof_on_read_; }
bool get_err_string_too_long() const { return err_string_too_long_; }
bool get_err_integer_truncated() const { return err_integer_truncated_; }
bool any_error() const { return err_eof_on_read_ || err_string_too_long_ || err_integer_truncated_; }
};
class FlxStreamBuffer : public BaseBuffer<FlxStreamBufferCore, std::string> {
public:
using BaseBuffer::BaseBuffer;
void write_dxyz(const FVector &xyz) {
write_double(xyz.X);
write_double(xyz.Y);
write_double(xyz.Z);
}
FVector read_dxyz() {
double x = read_double();
double y = read_double();
double z = read_double();
return FVector(x, y, z);
}
};