SaveMessage.cpp
SaveMessage.cpp
CPP1/* Copyright Statement:2 *3 * (C) 2021-2025 Cubios Inc. All rights reserved.4 */56#include "native.h"7#include "SaveMessage.h"8#include <sstream>910namespace Cubios11{12SaveMessage::SaveMessage(uint8_t* data,int length)13{14 this->m_numBytes = GAME_SAVE_SIZE;15 this->m_usedBits = 0;16 this->m_curPos = 0;1718 memset(this->m_bytes,0x00,GAME_SAVE_SIZE);1920 if(data!=NULL && length>0 && length<=GAME_SAVE_SIZE)21 {22 this->m_numBytes = length;23 memcpy(this->m_bytes,data,length);24 }25}2627SaveMessage::~SaveMessage()28{29}3031bool SaveMessage::checkOverflow(int bits)32{33 int newLength = this->m_usedBits + bits;34 if (newLength > this->m_numBytes * 8)35 {36 LOG_i("SaveMessage: maximum message size exceeded");3738 return true;39 }4041 return false;42}4344void SaveMessage::Print()45{46 std::stringstream ss;4748 for (int curByte = 0; curByte < this->m_numBytes; curByte++)49 {50 char str = m_bytes[curByte];51 for (int i = 128; i > 0; i = i/2)52 {53 ss << (bool) ((int) str & i);54 }55 ss << " ";5657 if(curByte==(this->m_numBytes/2)-1)58 {59 LOG_i("%s",ss.str().c_str());6061 ss.str("");62 ss.clear();63 }64 }6566 LOG_i("%s",ss.str().c_str());67}6869void SaveMessage::WriteByte(uint8_t value)70{71 this->WriteInt(value,8);72}7374void SaveMessage::WriteInt(int value, int bits)75{76 if(this->checkOverflow(bits)) return;7778 if(value<0)79 {80 LOG_i("SaveMessage::WriteInt: negative value %d may be sent incorrectly, use WriteSignedInt instead ",value);81 }8283 for (int i = 0; i < bits; i++)84 {85 int byte = this->curByte();86 int bit = this->curBit();8788 // Create bitmask for the correspondent bit of the integer to store here89 int mask = 1 << (bits - i - 1);9091 // Whether to set the current bit or not92 bool set = (bool) (value & mask);93 this->m_bytes[byte] |= (uint8_t) set << (7 - bit);94 this->m_curPos++;95 }9697 this->m_usedBits += bits;98}99100void SaveMessage::WriteSignedInt(int value, int bits)101{102 if(this->checkOverflow(bits+1)) return;103104 uint8_t byte = m_bytes[this->curByte()];105 int sign = (value>>31) & 0x1;106 byte |= (int)sign << (7 - this->curBit());107 this->m_bytes[this->curByte()] = byte;108109 this->m_curPos += 1;110 this->m_usedBits += 1;111112 for (int i = 0; i < bits; i++)113 {114 int byte = this->curByte();115 int bit = this->curBit();116117 // Create bitmask for the correspondent bit of the integer to store here118 int mask = 1 << (bits - i - 1);119120 // Whether to set the current bit or not121 bool set = (bool) (std::abs(value) & mask);122 this->m_bytes[byte] |= (uint8_t) set << (7 - bit);123 this->m_curPos++;124 }125126 this->m_usedBits += bits;127}128129void SaveMessage::WriteBool(bool value)130{131 if(this->checkOverflow(1)) return;132133 uint8_t byte = m_bytes[this->curByte()];134 byte |= (int) value << (7 - this->curBit());135 this->m_bytes[this->curByte()] = byte;136137 this->m_curPos += 1;138 this->m_usedBits += 1;139}140141void SaveMessage::WriteFloat(float value)142{143 uint32_t fi;144 memcpy(&fi,(uint32_t*)&value,sizeof(float));145 this->WriteInt(fi,32);146}147148uint8_t SaveMessage::ReadByte()149{150 return this->ReadInt(8);151}152153int SaveMessage::ReadInt(int bits)154{155 int value = 0;156157 for (int i = 0; i < bits; i++)158 {159 int byte = this->curByte();160 int bit = this->curBit();161162 // Create bitmask for the correspondent bit of the data that we want to read163 int mask = 1 << (7 - bit);164165 // Whether to set the current bit or not166 bool set = (bool) (this->m_bytes[byte] & mask);167 value |= (int) set << (bits - i - 1);168 this->m_curPos++;169 }170171 return value;172}173174int SaveMessage::ReadSignedInt(int bits)175{176 int value = 0;177178 bool negative = this->ReadBool();179180 for (int i = 0; i < bits; i++)181 {182 int byte = this->curByte();183 int bit = this->curBit();184185 // Create bitmask for the correspondent bit of the data that we want to read186 int mask = 1 << (7 - bit);187188 // Whether to set the current bit or not189 bool set = (bool) (this->m_bytes[byte] & mask);190 value |= (int) set << (bits - i - 1);191 this->m_curPos++;192 }193194 if(negative) value*=-1;195196 return value;197}198199bool SaveMessage::ReadBool()200{201 unsigned int byte = this->m_bytes[this->curByte()];202 unsigned int mask = 1 << (7 - this->curBit());203204 this->m_curPos += 1;205 return (byte & mask) > 0;206}207208float SaveMessage::ReadFloat()209{210 int fi = this->ReadInt(32);211 float ff;212 memcpy((uint32_t*)&ff,&fi,sizeof(float));213214 return ff;215}216217uint8_t* SaveMessage::GetData(int& length)218{219 length = this->m_numBytes;220 if(length>GAME_SAVE_SIZE) length = GAME_SAVE_SIZE;221222 return this->m_bytes;223}224225void SaveMessage::SetData(uint8_t* data, int length)226{227 if(data!=NULL && length>0 && length<=GAME_SAVE_SIZE)228 {229 this->Reset(true);230 this->m_numBytes = length;231 memcpy(this->m_bytes,data,length);232 }233}234235}236
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.