Fix compatibility with gdiplus, which also defines byte. Use std::byte instead.

master
sigonasr2 1 year ago
parent 05f8c90ec0
commit e516dbf65a
  1. 156
      SHNFileDecryptor/SHNFileDecryptor.h
  2. 1
      SHNFileDecryptor/SHNFileDecryptor.vcxproj
  3. 3
      SHNFileDecryptor/SHNFileDecryptor.vcxproj.filters
  4. 4
      SHNFileDecryptor/SHNFile_example.cpp
  5. 6695
      SHNFileDecryptor/olcPixelGameEngine.h

@ -6,19 +6,19 @@
#include <string> #include <string>
#include <chrono> #include <chrono>
typedef std::byte byte; //typedef std::std::byte std::byte;
typedef char sbyte; typedef char sbyte;
class SHNFile{ class SHNFile{
std::vector<byte>ReadBytes(std::ifstream&file); std::vector<std::byte>ReadBytes(std::ifstream&file);
std::vector<byte>ReadBytes(std::ifstream&file,int bytes); std::vector<std::byte>ReadBytes(std::ifstream&file,int bytes);
void WriteBytes(std::ofstream&file,std::vector<byte>&data); void WriteBytes(std::ofstream&file,std::vector<std::byte>&data);
int ReadInt32(std::ifstream&file); int ReadInt32(std::ifstream&file);
void Encrypt(); void Encrypt();
void Decrypt(); void Decrypt();
std::vector<byte>ReadBytes(int bytes); std::vector<std::byte>ReadBytes(int bytes);
byte ReadByte(); std::byte ReadByte();
void WriteByte(std::ofstream&f,byte b); void WriteByte(std::ofstream&f,std::byte b);
void WriteSByte(std::ofstream&f,sbyte b); void WriteSByte(std::ofstream&f,sbyte b);
uint16_t ReadUInt16(); uint16_t ReadUInt16();
void WriteUInt16(std::ofstream&f,uint16_t val); void WriteUInt16(std::ofstream&f,uint16_t val);
@ -50,7 +50,7 @@ public:
struct Data{ struct Data{
std::shared_ptr<void>data; std::shared_ptr<void>data;
DataType type=DataType::BYTE; DataType type=DataType::BYTE;
Data(byte b); Data(std::byte b);
Data(sbyte b); Data(sbyte b);
Data(uint16_t n); Data(uint16_t n);
Data(int16_t n); Data(int16_t n);
@ -75,8 +75,8 @@ private:
int length=0; int length=0;
}; };
int marker=0; int marker=0;
std::vector<byte>cryptHeader; std::vector<std::byte>cryptHeader;
std::vector<byte>data,rawData; std::vector<std::byte>data,rawData;
uint32_t header=0,recordCount=0,defaultRecordLength=0,columnCount=0; uint32_t header=0,recordCount=0,defaultRecordLength=0,columnCount=0;
std::vector<Column>columns; std::vector<Column>columns;
std::vector<std::vector<Data>>contents; std::vector<std::vector<Data>>contents;
@ -84,7 +84,7 @@ private:
public: public:
void Load(std::string file); void Load(std::string file);
void Save(); void Save();
void Write(int row,int col,byte val); void Write(int row,int col,std::byte val);
void Write(int row,int col,sbyte val); void Write(int row,int col,sbyte val);
void Write(int row,int col,int16_t val); void Write(int row,int col,int16_t val);
void Write(int row,int col,uint16_t val); void Write(int row,int col,uint16_t val);
@ -96,31 +96,31 @@ public:
SHNFile(); SHNFile();
}; };
#ifdef OLC_PGEX_SHNFile #ifdef OLC_PGEX_SHNFile
std::vector<byte>SHNFile::ReadBytes(std::ifstream&file){ std::vector<std::byte>SHNFile::ReadBytes(std::ifstream&file){
std::vector<byte>byteArr; std::vector<std::byte>byteArr;
while(!file.eof()){ while(!file.eof()){
byteArr.push_back(byte(file.get())); byteArr.push_back(std::byte(file.get()));
} }
return byteArr; return byteArr;
} }
std::vector<byte>SHNFile::ReadBytes(std::ifstream&file,int bytes){ std::vector<std::byte>SHNFile::ReadBytes(std::ifstream&file,int bytes){
std::vector<byte>byteArr; std::vector<std::byte>byteArr;
for(int i=0;i<bytes;i++){ for(int i=0;i<bytes;i++){
if(!file.eof()){ if(!file.eof()){
byteArr.push_back(byte(file.get())); byteArr.push_back(std::byte(file.get()));
} else { } else {
break; break;
} }
} }
return byteArr; return byteArr;
} }
void SHNFile::WriteBytes(std::ofstream&file,std::vector<byte>&data){ void SHNFile::WriteBytes(std::ofstream&file,std::vector<std::byte>&data){
for(int i=0;i<data.size();i++){ for(int i=0;i<data.size();i++){
file<<unsigned char(data[i]); file<<unsigned char(data[i]);
} }
} }
int SHNFile::ReadInt32(std::ifstream&file){ int SHNFile::ReadInt32(std::ifstream&file){
std::vector<byte>intBytes=ReadBytes(file,4); std::vector<std::byte>intBytes=ReadBytes(file,4);
int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]); int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]);
return numb; return numb;
} }
@ -128,20 +128,20 @@ void SHNFile::Encrypt(){
Decrypt(); Decrypt();
} }
void SHNFile::Decrypt(){ void SHNFile::Decrypt(){
byte num = byte(data.size()); std::byte num = std::byte(data.size());
for(int i=data.size()-1;i>=0;i--){ for(int i=data.size()-1;i>=0;i--){
data[i] = byte(data[i]^num); data[i] = std::byte(data[i]^num);
byte num3 = byte(i); std::byte num3 = std::byte(i);
num3 = byte(num3&byte(15)); num3 = std::byte(num3&std::byte(15));
num3 = byte(int(num3)+0x55); num3 = std::byte(int(num3)+0x55);
num3 = byte(num3 ^ (byte((int(byte(i))*11)))); num3 = std::byte(num3 ^ (std::byte((int(std::byte(i))*11))));
num3 = byte(num3^num); num3 = std::byte(num3^num);
num3 = byte(int(num3)^170); num3 = std::byte(int(num3)^170);
num = num3; num = num3;
} }
} }
std::vector<byte>SHNFile::ReadBytes(int bytes){ std::vector<std::byte>SHNFile::ReadBytes(int bytes){
std::vector<byte>byteArr={}; std::vector<std::byte>byteArr={};
for(int i=0;i<bytes;i++){ for(int i=0;i<bytes;i++){
if(marker<data.size()){ if(marker<data.size()){
byteArr.push_back(data[marker]); byteArr.push_back(data[marker]);
@ -152,22 +152,22 @@ std::vector<byte>SHNFile::ReadBytes(int bytes){
} }
return byteArr; return byteArr;
} }
byte SHNFile::ReadByte(){ std::byte SHNFile::ReadByte(){
std::vector<byte>b=ReadBytes(1); std::vector<std::byte>b=ReadBytes(1);
if(b.size()>0){ if(b.size()>0){
return b[0]; return b[0];
} else { } else {
return byte(0); return std::byte(0);
} }
} }
void SHNFile::WriteByte(std::ofstream&f,byte b){ void SHNFile::WriteByte(std::ofstream&f,std::byte b){
f<<unsigned char(b); f<<unsigned char(b);
} }
void SHNFile::WriteSByte(std::ofstream&f,sbyte b){ void SHNFile::WriteSByte(std::ofstream&f,sbyte b){
f<<char(b); f<<char(b);
} }
uint16_t SHNFile::ReadUInt16(){ uint16_t SHNFile::ReadUInt16(){
std::vector<byte>intBytes=ReadBytes(2); std::vector<std::byte>intBytes=ReadBytes(2);
uint16_t numb = uint16_t(intBytes[1])<<8|uint16_t(intBytes[0]); uint16_t numb = uint16_t(intBytes[1])<<8|uint16_t(intBytes[0]);
return numb; return numb;
} }
@ -175,7 +175,7 @@ void SHNFile::WriteUInt16(std::ofstream&f,uint16_t val){
f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF); f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF);
} }
int16_t SHNFile::ReadInt16(){ int16_t SHNFile::ReadInt16(){
std::vector<byte>intBytes=ReadBytes(2); std::vector<std::byte>intBytes=ReadBytes(2);
int16_t numb = int16_t(intBytes[1])<<8|int16_t(intBytes[0]); int16_t numb = int16_t(intBytes[1])<<8|int16_t(intBytes[0]);
return numb; return numb;
} }
@ -183,7 +183,7 @@ void SHNFile::WriteInt16(std::ofstream&f,int16_t val){
f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF); f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF);
} }
uint32_t SHNFile::ReadUInt32(){ uint32_t SHNFile::ReadUInt32(){
std::vector<byte>intBytes=ReadBytes(4); std::vector<std::byte>intBytes=ReadBytes(4);
uint32_t numb = uint32_t(intBytes[3])<<24|uint32_t(intBytes[2])<<16|uint32_t(intBytes[1])<<8|uint32_t(intBytes[0]); uint32_t numb = uint32_t(intBytes[3])<<24|uint32_t(intBytes[2])<<16|uint32_t(intBytes[1])<<8|uint32_t(intBytes[0]);
return numb; return numb;
} }
@ -191,12 +191,12 @@ void SHNFile::WriteUInt32(std::ofstream&f,uint32_t val){
f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF)<<unsigned char((val>>16)&0xFF)<<unsigned char((val>>24)&0xFF); f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF)<<unsigned char((val>>16)&0xFF)<<unsigned char((val>>24)&0xFF);
} }
int SHNFile::Read(){ int SHNFile::Read(){
std::vector<byte>intBytes=ReadBytes(4); std::vector<std::byte>intBytes=ReadBytes(4);
int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]); int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]);
return numb; return numb;
} }
int32_t SHNFile::ReadInt32(){ int32_t SHNFile::ReadInt32(){
std::vector<byte>intBytes=ReadBytes(4); std::vector<std::byte>intBytes=ReadBytes(4);
int32_t numb = int32_t(intBytes[3])<<24|int32_t(intBytes[2])<<16|int32_t(intBytes[1])<<8|uint32_t(intBytes[0]); int32_t numb = int32_t(intBytes[3])<<24|int32_t(intBytes[2])<<16|int32_t(intBytes[1])<<8|uint32_t(intBytes[0]);
return numb; return numb;
} }
@ -204,10 +204,10 @@ void SHNFile::WriteInt32(std::ofstream&f,int32_t val){
f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF)<<unsigned char((val>>16)&0xFF)<<unsigned char((val>>24)&0xFF); f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF)<<unsigned char((val>>16)&0xFF)<<unsigned char((val>>24)&0xFF);
} }
std::string SHNFile::ReadString(int bytes){ std::string SHNFile::ReadString(int bytes){
std::vector<byte>strBytes=ReadBytes(bytes); std::vector<std::byte>strBytes=ReadBytes(bytes);
std::string str=""; std::string str="";
for(int i=0;i<strBytes.size();i++){ for(int i=0;i<strBytes.size();i++){
if(strBytes[i]!=byte(0)){ if(strBytes[i]!=std::byte(0)){
str+=unsigned char(strBytes[i]); str+=unsigned char(strBytes[i]);
} }
} }
@ -227,14 +227,14 @@ void SHNFile::WriteString(std::ofstream&f,std::string str,int bytes){
} }
} }
float SHNFile::ReadSingle(){ float SHNFile::ReadSingle(){
std::vector<byte>strBytes=ReadBytes(4); std::vector<std::byte>strBytes=ReadBytes(4);
byte bytes[]={strBytes[0],strBytes[1],strBytes[2],strBytes[3]}; std::byte bytes[]={strBytes[0],strBytes[1],strBytes[2],strBytes[3]};
float f; float f;
memcpy(&f,&bytes,4); memcpy(&f,&bytes,4);
return f; return f;
} }
void SHNFile::WriteSingle(std::ofstream&f,float n){ void SHNFile::WriteSingle(std::ofstream&f,float n){
byte bytes[4]={}; std::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]);
@ -246,8 +246,8 @@ sbyte SHNFile::ReadSByte(){
std::string SHNFile::ReadString(){ std::string SHNFile::ReadString(){
std::string str=""; std::string str="";
while(true){ while(true){
std::vector<byte>byteArr=ReadBytes(1); std::vector<std::byte>byteArr=ReadBytes(1);
if(byteArr.size()>0&&byteArr[0]!=byte(0)){ if(byteArr.size()>0&&byteArr[0]!=std::byte(0)){
str+=unsigned char(byteArr[0]); str+=unsigned char(byteArr[0]);
}else{ }else{
break; break;
@ -262,8 +262,8 @@ uint32_t SHNFile::GetRecordLength(){
} }
return start; return start;
} }
SHNFile::Data::Data(byte b){ SHNFile::Data::Data(std::byte b){
std::shared_ptr<byte>ptr=std::make_shared<byte>(byte(b)); std::shared_ptr<std::byte>ptr=std::make_shared<std::byte>(std::byte(b));
data=ptr; data=ptr;
type=DataType::BYTE; type=DataType::BYTE;
} }
@ -312,30 +312,30 @@ void SHNFile::Data::Set(T b){
} }
std::string SHNFile::Data::GetDisplayText(){ std::string SHNFile::Data::GetDisplayText(){
switch(type){ switch(type){
case DataType::BYTE:{ case DataType::BYTE:{
return std::to_string(int(Get<byte>())); return std::to_string(int(Get<std::byte>()));
}break; }break;
case DataType::SBYTE:{ case DataType::SBYTE:{
return std::to_string(int(Get<sbyte>())); return std::to_string(int(Get<sbyte>()));
}break; }break;
case DataType::UINT16:{ case DataType::UINT16:{
return std::to_string(Get<uint16_t>()); return std::to_string(Get<uint16_t>());
}break; }break;
case DataType::INT16:{ case DataType::INT16:{
return std::to_string(Get<int16_t>()); return std::to_string(Get<int16_t>());
}break; }break;
case DataType::UINT32:{ case DataType::UINT32:{
return std::to_string(Get<uint32_t>()); return std::to_string(Get<uint32_t>());
}break; }break;
case DataType::INT32:{ case DataType::INT32:{
return std::to_string(Get<int32_t>()); return std::to_string(Get<int32_t>());
}break; }break;
case DataType::FLOAT:{ case DataType::FLOAT:{
return std::to_string(Get<float>()); return std::to_string(Get<float>());
}break; }break;
case DataType::STRING:{ case DataType::STRING:{
return Get<std::string>(); return Get<std::string>();
}break; }break;
} }
} }
void SHNFile::Load(std::string file){ void SHNFile::Load(std::string file){
@ -356,10 +356,10 @@ void SHNFile::Load(std::string file){
cryptHeader=ReadBytes(f,0x20); cryptHeader=ReadBytes(f,0x20);
std::copy(cryptHeader.begin(),cryptHeader.end(),std::back_inserter(rawData)); std::copy(cryptHeader.begin(),cryptHeader.end(),std::back_inserter(rawData));
int readAmt=ReadInt32(f); int readAmt=ReadInt32(f);
rawData.push_back(byte(readAmt&0xFF)); rawData.push_back(std::byte(readAmt&0xFF));
rawData.push_back(byte((readAmt>>8)&0xFF)); rawData.push_back(std::byte((readAmt>>8)&0xFF));
rawData.push_back(byte((readAmt>>16)&0xFF)); rawData.push_back(std::byte((readAmt>>16)&0xFF));
rawData.push_back(byte((readAmt>>24)&0xFF)); rawData.push_back(std::byte((readAmt>>24)&0xFF));
data=ReadBytes(f,readAmt-0x24); data=ReadBytes(f,readAmt-0x24);
std::copy(data.begin(),data.end(),std::back_inserter(rawData)); std::copy(data.begin(),data.end(),std::back_inserter(rawData));
@ -457,7 +457,7 @@ void SHNFile::Save(){
case 1: case 1:
case 12: case 12:
case 0x10:{ case 0x10:{
WriteByte(f,col.Get<byte>()); WriteByte(f,col.Get<std::byte>());
}break; }break;
case 2:{ case 2:{
WriteUInt16(f,col.Get<uint16_t>()); WriteUInt16(f,col.Get<uint16_t>());
@ -511,8 +511,8 @@ void SHNFile::Save(){
const SHNFile::Data SHNFile::Get(int row,int col)const{ const SHNFile::Data SHNFile::Get(int row,int col)const{
return contents[row][col]; return contents[row][col];
}; };
void SHNFile::Write(int row,int col,byte val){ void SHNFile::Write(int row,int col,std::byte val){
contents[row][col].Set<byte>(val); contents[row][col].Set<std::byte>(val);
} }
void SHNFile::Write(int row,int col,sbyte val){ void SHNFile::Write(int row,int col,sbyte val){
contents[row][col].Set<sbyte>(val); contents[row][col].Set<sbyte>(val);

@ -134,6 +134,7 @@
<ClCompile Include="SHNFile_example.cpp" /> <ClCompile Include="SHNFile_example.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="olcPixelGameEngine.h" />
<ClInclude Include="SHNFileDecryptor.h" /> <ClInclude Include="SHNFileDecryptor.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

@ -23,5 +23,8 @@
<ClInclude Include="SHNFileDecryptor.h"> <ClInclude Include="SHNFileDecryptor.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="olcPixelGameEngine.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -1,6 +1,10 @@
#define OLC_PGEX_SHNFile #define OLC_PGEX_SHNFile
#include "SHNFileDecryptor.h" #include "SHNFileDecryptor.h"
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
int main(){ int main(){
SHNFile shn; SHNFile shn;
shn.Load("AbState.shn"); shn.Load("AbState.shn");

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save