From 4f25fa9ae42a0d43c20e954e8e50c66a6b057c92 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 27 Oct 2023 17:20:23 -0400 Subject: We can output a code for witnesspuzzles --- ext/wittle_generator/Serializer.cpp | 135 ++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 ext/wittle_generator/Serializer.cpp (limited to 'ext/wittle_generator/Serializer.cpp') diff --git a/ext/wittle_generator/Serializer.cpp b/ext/wittle_generator/Serializer.cpp new file mode 100644 index 0000000..420926f --- /dev/null +++ b/ext/wittle_generator/Serializer.cpp @@ -0,0 +1,135 @@ +#include "Serializer.h" + +#include +#include + +#include "Base64.h" + +void Serializer::writeByte(char val) { buffer_.push_back(val); } + +void Serializer::writeInt(int val) { + int b1 = (val & 0x000000FF) >> 0; + int b2 = (val & 0x0000FF00) >> 8; + int b3 = (val & 0x00FF0000) >> 16; + int b4 = (val & 0xFF000000) >> 24; + writeByte(b1); + writeByte(b2); + writeByte(b3); + writeByte(b4); +} + +void Serializer::writeLong(long val) { + long i1 = val & 0xFFFFFFFF; + long i2 = (val - i1) / 0x100000000; + writeInt(i1); + writeInt(i2); +} + +void Serializer::writeColor(int val) { + switch (val) { + case 0x1: { + // Black + writeByte(0xFF); + writeByte(0xFF); + writeByte(0xFF); + writeByte(0xFF); + break; + } + case 0x2: { + // White + writeByte(0x00); + writeByte(0x00); + writeByte(0x00); + writeByte(0xFF); + break; + } + case 0x3: { + // Red + writeByte(0xFF); + writeByte(0x00); + writeByte(0x00); + writeByte(0xFF); + break; + } + case 0x4: { + // Purple + writeByte(0x80); + writeByte(0x00); + writeByte(0x80); + writeByte(0xFF); + break; + } + case 0x5: { + // Green + writeByte(0x00); + writeByte(0xFF); + writeByte(0x00); + writeByte(0xFF); + break; + } + case 0x6: { + // Cyan + writeByte(0x00); + writeByte(0xFF); + writeByte(0xFF); + writeByte(0xFF); + break; + } + case 0x7: { + // Magenta + writeByte(0xFF); + writeByte(0x00); + writeByte(0xFF); + writeByte(0xFF); + break; + } + case 0x8: { + // Yellow + writeByte(0xFF); + writeByte(0xFF); + writeByte(0x00); + writeByte(0xFF); + break; + } + case 0x9: { + // Blue + writeByte(0x00); + writeByte(0x00); + writeByte(0xFF); + writeByte(0xFF); + break; + } + case 0xA: { + // Orange + writeByte(0xFF); + writeByte(0x8C); + writeByte(0x00); + writeByte(0xFF); + break; + } + case 0xF: { + // X???? + break; + } + } +} + +void Serializer::writeString(const std::string& val) { + writeInt(val.length()); + for (char ch : val) { + writeByte(ch); + } +} + +std::string Serializer::str() const { + int i = 0; + for (char ch : buffer_) { + std::cout << std::hex << static_cast(ch); + if (i++ % 4 == 3) { + std::cout << " "; + } + } + std::cout << std::endl; + + return "_" + macaron::Base64::Encode(buffer_); +} -- cgit 1.4.1