Changed from vectors to lists for performance gains.

master
sigonasr2 1 year ago
parent 8e86f3149b
commit 3a0d73eb79
  1. BIN
      SHNFileDecryptor/AbState.shn.bak
  2. 183
      SHNFileDecryptor/SHNFileDecryptor.cpp

Binary file not shown.

@ -3,13 +3,13 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <chrono>
#include <list>
typedef std::byte byte; typedef std::byte byte;
typedef char sbyte; typedef char sbyte;
class SHNFile{ class SHNFile{
std::vector<byte>cryptHeader;
std::vector<byte>data;
std::vector<byte>ReadBytes(std::ifstream&file,int bytes){ std::vector<byte>ReadBytes(std::ifstream&file,int bytes){
std::vector<byte>byteArr; std::vector<byte>byteArr;
for(int i=0;i<bytes;i++){ for(int i=0;i<bytes;i++){
@ -39,9 +39,8 @@ class SHNFile{
num = num3; num = num3;
} }
} }
int marker=0; std::list<byte>ReadBytes(int bytes){
std::vector<byte>ReadBytes(int bytes){ std::list<byte>byteArr={};
std::vector<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]);
@ -53,31 +52,47 @@ class SHNFile{
return byteArr; return byteArr;
} }
byte ReadByte(){ byte ReadByte(){
std::vector<byte>b=ReadBytes(1); std::list<byte>b=ReadBytes(1);
if(b.size()>0){ if(b.size()>0){
return b[0]; return b.front();
} else { } else {
return byte(0); return byte(0);
} }
} }
uint16_t ReadUInt16(){ uint16_t ReadUInt16(){
std::vector<byte>intBytes=ReadBytes(2); std::list<byte>intBytes=ReadBytes(2);
uint16_t numb = uint16_t(intBytes[1])<<8|uint16_t(intBytes[0]); uint16_t numb = uint16_t(intBytes.back())<<8;
intBytes.pop_back();
numb |= uint16_t(intBytes.back());
return numb; return numb;
} }
int16_t ReadInt16(){ int16_t ReadInt16(){
std::vector<byte>intBytes=ReadBytes(2); std::list<byte>intBytes=ReadBytes(2);
int16_t numb = int16_t(intBytes[1])<<8|int16_t(intBytes[0]); int16_t numb = int16_t(intBytes.back())<<8;
intBytes.pop_back();
numb |= int16_t(intBytes.back());
return numb; return numb;
} }
uint32_t ReadUInt32(){ uint32_t ReadUInt32(){
std::vector<byte>intBytes=ReadBytes(4); std::list<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.back())<<24;
intBytes.pop_back();
numb |= uint32_t(intBytes.back())<<16;
intBytes.pop_back();
numb |= uint32_t(intBytes.back())<<8;
intBytes.pop_back();
numb |= uint32_t(intBytes.back());
return numb; return numb;
} }
int ReadInt32(){ int ReadInt32(){
std::vector<byte>intBytes=ReadBytes(4); std::list<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.back())<<24;
intBytes.pop_back();
numb |= int(intBytes.back())<<16;
intBytes.pop_back();
numb |= int(intBytes.back())<<8;
intBytes.pop_back();
numb |= int(intBytes.back());
return numb; return numb;
} }
struct Column{ struct Column{
@ -86,39 +101,44 @@ class SHNFile{
int length=0; int length=0;
}; };
std::string ReadString(int bytes){ std::string ReadString(int bytes){
std::vector<byte>strBytes=ReadBytes(bytes); std::list<byte>strBytes=ReadBytes(bytes);
std::string str=""; std::string str="";
for(int i=0;i<strBytes.size();i++){ for(auto it=strBytes.begin();it!=strBytes.end();++it){
if(strBytes[i]!=byte(0)){ if(*it!=byte(0)){
str+=unsigned char(strBytes[i]); str+=unsigned char(*it);
} }
} }
return str; return str;
} }
float ReadSingle(){ float ReadSingle(){
std::vector<byte>strBytes=ReadBytes(4); std::list<byte>strBytes=ReadBytes(4);
byte bytes[]={strBytes[0],strBytes[1],strBytes[2],strBytes[3]}; byte bytes[4];
bytes[0]=strBytes.front();
strBytes.pop_front();
bytes[1]=strBytes.front();
strBytes.pop_front();
bytes[2]=strBytes.front();
strBytes.pop_front();
bytes[3]=strBytes.front();
float f; float f;
memcpy(&f,&bytes,sizeof(float)); memcpy(&f,&bytes,sizeof(float));
return f; return f;
} }
sbyte ReadSByte(){ sbyte ReadSByte(){
return sbyte(ReadBytes(1)[0]); return sbyte(ReadBytes(1).front());
} }
std::string ReadString(){ std::string ReadString(){
std::string str=""; std::string str="";
while(true){ while(true){
std::vector<byte>byteArr=ReadBytes(1); std::list<byte>byteArr=ReadBytes(1);
if(byteArr.size()>0&&byteArr[0]!=byte(0)){ if(byteArr.size()>0&&byteArr.front()!=byte(0)){
str+=unsigned char(byteArr[0]); str+=unsigned char(byteArr.front());
}else{ }else{
break; break;
} }
} }
return str; return str;
} }
uint32_t header,recordCount,defaultRecordLength,columnCount;
std::vector<Column>columns;
public: public:
struct Data{ struct Data{
std::shared_ptr<void>data; std::shared_ptr<void>data;
@ -217,14 +237,28 @@ class SHNFile{
} }
}; };
private: private:
std::vector<std::vector<Data>>contents;
friend std::ostream&operator<<(std::ostream&out,SHNFile::Data&d){ friend std::ostream&operator<<(std::ostream&out,SHNFile::Data&d){
out<<d.GetDisplayText(); out<<d.GetDisplayText();
return out; return out;
} }
int marker=0;
std::vector<byte>cryptHeader;
std::vector<byte>data;
uint32_t header=0,recordCount=0,defaultRecordLength=0,columnCount=0;
std::vector<Column>columns;
std::vector<std::vector<Data>>contents;
std::string filename;
public: public:
SHNFile(std::string file){ void Load(std::string file){
std::ifstream f(file,std::ios::binary); cryptHeader.clear();
data.clear();
header=recordCount=defaultRecordLength=columnCount=0;
columns.clear();
contents.clear();
marker=0;
std::chrono::time_point<std::chrono::high_resolution_clock>timer=std::chrono::high_resolution_clock::now();
filename=file;
std::ifstream f(filename,std::ios::binary);
cryptHeader=ReadBytes(f,0x20); cryptHeader=ReadBytes(f,0x20);
data=ReadBytes(f,ReadInt32(f)-0x24); data=ReadBytes(f,ReadInt32(f)-0x24);
Decrypt(); Decrypt();
@ -248,53 +282,62 @@ public:
std::vector<Data>row; std::vector<Data>row;
for(int j=0;j<columns.size();j++){ for(int j=0;j<columns.size();j++){
switch(columns[j].type){ switch(columns[j].type){
case 1: case 1:
case 12: case 12:
case 0x10:{ case 0x10:{
row.push_back(ReadByte()); row.push_back(ReadByte());
}break; }break;
case 2:{ case 2:{
row.push_back(ReadUInt16()); row.push_back(ReadUInt16());
}break; }break;
case 3: case 3:
case 11: case 11:
case 0x12: case 0x12:
case 0x1b:{ case 0x1b:{
row.push_back(ReadUInt32()); row.push_back(ReadUInt32());
}break; }break;
case 5:{ case 5:{
row.push_back(ReadSingle()); row.push_back(ReadSingle());
}break; }break;
case 9: case 9:
case 0x18:{ case 0x18:{
row.push_back(ReadString(columns[j].length)); row.push_back(ReadString(columns[j].length));
}break; }break;
case 13: case 13:
case 0x15:{ case 0x15:{
row.push_back(ReadInt16()); row.push_back(ReadInt16());
}break; }break;
case 20:{ case 20:{
row.push_back(ReadSByte()); row.push_back(ReadSByte());
}break; }break;
case 0x16:{ case 0x16:{
row.push_back(ReadInt32()); row.push_back(ReadInt32());
}break; }break;
case 0x1a:{ case 0x1a:{
row.push_back(ReadString()); row.push_back(ReadString());
}break; }break;
} }
} }
contents.push_back(row); contents.push_back(row);
} }
for(int i=0;i<contents.size();i++){ std::chrono::duration<float>dur=std::chrono::high_resolution_clock::now()-timer;
for(int j=0;j<contents[i].size();j++){ std::cout<<"Loaded "<<contents.size()<<" rows, "<<contents.size()*columnCount<<" columns, "<<dur.count()<<"s"<<std::endl;
std::cout<<contents[i][j]<<'\t'; }
} void Save(){
std::cout<<std::endl; std::ofstream fBackup(filename+".bak",std::ios::binary);
std::cout<<"Saving a backup to "<<filename+".bak"<<std::endl;
for(int i=0;i<data.size();i++){
fBackup<<unsigned char(data[i]);
} }
std::cout<<"Saving new file..."<<std::endl;
//std::ofstream f("save_"+filename,std::ios::binary);
} }
SHNFile(){}
}; };
int main(){ int main(){
SHNFile shn("AbState.shn"); SHNFile shn;
shn.Load("AbState.shn");
shn.Load("AbState.shn");
shn.Save();
} }
Loading…
Cancel
Save