Standardize data types using a data type enum. Get function implemented.

master
sigonasr2 1 year ago
parent 8336b0e305
commit 05f8c90ec0
  1. BIN
      SHNFileDecryptor/AbState.shn.bak
  2. 50
      SHNFileDecryptor/SHNFileDecryptor.h
  3. 2
      SHNFileDecryptor/SHNFile_example.cpp

Binary file not shown.

@ -37,9 +37,19 @@ class SHNFile{
std::string ReadString(); std::string ReadString();
uint32_t GetRecordLength(); uint32_t GetRecordLength();
public: public:
enum class DataType:int{
BYTE=1,
SBYTE=7,
UINT16=2,
INT16=6,
UINT32=3,
INT32=8,
FLOAT=4,
STRING=5
};
struct Data{ struct Data{
std::shared_ptr<void>data; std::shared_ptr<void>data;
int type=0; DataType type=DataType::BYTE;
Data(byte b); Data(byte b);
Data(sbyte b); Data(sbyte b);
Data(uint16_t n); Data(uint16_t n);
@ -82,6 +92,7 @@ public:
void Write(int row,int col,uint32_t val); void Write(int row,int col,uint32_t val);
void Write(int row,int col,float val); void Write(int row,int col,float val);
void Write(int row,int col,std::string val); void Write(int row,int col,std::string val);
const SHNFile::Data Get(int row,int col)const;
SHNFile(); SHNFile();
}; };
#ifdef OLC_PGEX_SHNFile #ifdef OLC_PGEX_SHNFile
@ -223,7 +234,7 @@ float SHNFile::ReadSingle(){
return f; return f;
} }
void SHNFile::WriteSingle(std::ofstream&f,float n){ void SHNFile::WriteSingle(std::ofstream&f,float n){
byte bytes[4]; byte bytes[4]={};
memcpy(&n,&bytes,4); memcpy(&n,&bytes,4);
for(int i=0;i<4;i++){ for(int i=0;i<4;i++){
f<<unsigned char(bytes[i]); f<<unsigned char(bytes[i]);
@ -254,42 +265,42 @@ uint32_t SHNFile::GetRecordLength(){
SHNFile::Data::Data(byte b){ SHNFile::Data::Data(byte b){
std::shared_ptr<byte>ptr=std::make_shared<byte>(byte(b)); std::shared_ptr<byte>ptr=std::make_shared<byte>(byte(b));
data=ptr; data=ptr;
type=1; type=DataType::BYTE;
} }
SHNFile::Data::Data(sbyte b){ SHNFile::Data::Data(sbyte b){
std::shared_ptr<sbyte>ptr=std::make_shared<sbyte>(sbyte(b)); std::shared_ptr<sbyte>ptr=std::make_shared<sbyte>(sbyte(b));
data=ptr; data=ptr;
type=7; type=DataType::SBYTE;
} }
SHNFile::Data::Data(uint16_t n){ SHNFile::Data::Data(uint16_t n){
std::shared_ptr<uint16_t>ptr=std::make_shared<uint16_t>(uint16_t(n)); std::shared_ptr<uint16_t>ptr=std::make_shared<uint16_t>(uint16_t(n));
data=ptr; data=ptr;
type=2; type=DataType::UINT16;
} }
SHNFile::Data::Data(int16_t n){ SHNFile::Data::Data(int16_t n){
std::shared_ptr<int16_t>ptr=std::make_shared<int16_t>(int16_t(n)); std::shared_ptr<int16_t>ptr=std::make_shared<int16_t>(int16_t(n));
data=ptr; data=ptr;
type=6; type=DataType::INT16;
} }
SHNFile::Data::Data(uint32_t n){ SHNFile::Data::Data(uint32_t n){
std::shared_ptr<uint32_t>ptr=std::make_shared<uint32_t>(uint32_t(n)); std::shared_ptr<uint32_t>ptr=std::make_shared<uint32_t>(uint32_t(n));
data=ptr; data=ptr;
type=3; type=DataType::UINT32;
} }
SHNFile::Data::Data(int32_t n){ SHNFile::Data::Data(int32_t n){
std::shared_ptr<int32_t>ptr=std::make_shared<int32_t>(int32_t(n)); std::shared_ptr<int32_t>ptr=std::make_shared<int32_t>(int32_t(n));
data=ptr; data=ptr;
type=8; type=DataType::INT32;
} }
SHNFile::Data::Data(float n){ SHNFile::Data::Data(float n){
std::shared_ptr<float>ptr=std::make_shared<float>(float(n)); std::shared_ptr<float>ptr=std::make_shared<float>(float(n));
data=ptr; data=ptr;
type=4; type=DataType::FLOAT;
} }
SHNFile::Data::Data(std::string str){ SHNFile::Data::Data(std::string str){
std::shared_ptr<std::string>ptr=std::make_shared<std::string>(str); std::shared_ptr<std::string>ptr=std::make_shared<std::string>(str);
data=ptr; data=ptr;
type=5; type=DataType::STRING;
} }
template <typename T> template <typename T>
T SHNFile::Data::Get(){ T SHNFile::Data::Get(){
@ -301,28 +312,28 @@ void SHNFile::Data::Set(T b){
} }
std::string SHNFile::Data::GetDisplayText(){ std::string SHNFile::Data::GetDisplayText(){
switch(type){ switch(type){
case 1:{ case DataType::BYTE:{
return std::to_string(int(Get<byte>())); return std::to_string(int(Get<byte>()));
}break; }break;
case 7:{ case DataType::SBYTE:{
return std::to_string(int(Get<sbyte>())); return std::to_string(int(Get<sbyte>()));
}break; }break;
case 2:{ case DataType::UINT16:{
return std::to_string(Get<uint16_t>()); return std::to_string(Get<uint16_t>());
}break; }break;
case 6:{ case DataType::INT16:{
return std::to_string(Get<int16_t>()); return std::to_string(Get<int16_t>());
}break; }break;
case 3:{ case DataType::UINT32:{
return std::to_string(Get<uint32_t>()); return std::to_string(Get<uint32_t>());
}break; }break;
case 8:{ case DataType::INT32:{
return std::to_string(Get<int32_t>()); return std::to_string(Get<int32_t>());
}break; }break;
case 4:{ case DataType::FLOAT:{
return std::to_string(Get<float>()); return std::to_string(Get<float>());
}break; }break;
case 5:{ case DataType::STRING:{
return Get<std::string>(); return Get<std::string>();
}break; }break;
} }
@ -497,6 +508,9 @@ void SHNFile::Save(){
WriteBytes(encryptedFile,data); WriteBytes(encryptedFile,data);
std::cout<<"File "<<filename<<" Saved!"<<std::endl; std::cout<<"File "<<filename<<" Saved!"<<std::endl;
} }
const SHNFile::Data SHNFile::Get(int row,int col)const{
return contents[row][col];
};
void SHNFile::Write(int row,int col,byte val){ void SHNFile::Write(int row,int col,byte val){
contents[row][col].Set<byte>(val); contents[row][col].Set<byte>(val);
} }

@ -4,6 +4,8 @@
int main(){ int main(){
SHNFile shn; SHNFile shn;
shn.Load("AbState.shn"); shn.Load("AbState.shn");
SHNFile::Data dat=shn.Get(3,6);
std::cout<<dat<<std::endl;
shn.Write(1,1,"Noodles"); shn.Write(1,1,"Noodles");
shn.Save(); shn.Save();
} }
Loading…
Cancel
Save