SHARE
TWEET

Untitled

HertzDevil Jul 1st, 2013 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. unsigned long int get32le(fstream* input){
  9.     unsigned char b1=input->get();
  10.     unsigned char b2=input->get();
  11.     unsigned char b3=input->get();
  12.     unsigned char b4=input->get();
  13.     long int i1=b1;
  14.     long int i2=b2;
  15.     long int i3=b3;
  16.     long int i4=b4;
  17.     return i1 | (i2<<8) | (i3<<16) | (i4<<24);}
  18.  
  19. int main(int argc, char** argv){
  20.     if (argc <= 1){
  21.         cout << "Instrument Reorderer for FamiTracker\n"
  22.              << "Usage: reinst [filename] ([listname])\n"
  23.              << "Description: Reads a text file containing space-delimited hexadecimal numbers,\n"
  24.              << "             so that the ordering of the FamiTracker module's instruments,\n"
  25.              << "             including those not created, matches the text file.\n"
  26.              << "Options:\n"
  27.              << "[filename]: Name of the FTM\n"
  28.              << "[listname]: Name of the reorder list. Defaults to \"reinst.txt\" if not present"
  29.              << endl;}
  30.     else{
  31.         int version;
  32.         fstream instlist((argc >= 3) ? (argv[2]) : ("reinst.txt"), ios::in);
  33.         if (!instlist){
  34.             cerr << "Cannot open instrument list!" << endl;
  35.             exit(1);}
  36.         fstream ftm(argv[1], ios::in | ios::out | ios::binary);
  37.         if (!ftm){
  38.             cerr << "Cannot open FamiTracker module!" << endl;
  39.             exit(1);}
  40.         fstream* ftmP = &ftm;
  41.         int* permute = new int[65];
  42.         for (int i=0;i<=0x40;i++){
  43.             permute[i]=i;}
  44.         for (int i=0;i<0x40;i++){
  45.             if (instlist){
  46.                 string str;
  47.                 instlist >> str;
  48.                 if (str!="") permute[stoi(str,NULL,16)]=i;}}
  49.         ftm.seekg(0x2A);
  50.         ftm.seekg(get32le(ftmP)+0x14,ios_base::cur);
  51.         ftm.seekg(get32le(ftmP)+0x14,ios_base::cur);
  52.         int pos=get32le(ftmP);
  53.         pos+=0x10+ftm.tellg();
  54.         int songs=ftm.get()+1;
  55.         int zero=0;
  56.         while (zero<songs) if (ftm.get()=='\0') zero++;
  57.         int headerpos=ftm.tellg();
  58.         ftm.seekg(pos); // seeks through "INSTRUMENT      " for normally created FTMs
  59.         version=get32le(ftmP);
  60.         ftm.seekg(0x4,ios_base::cur);
  61.         int instcount=get32le(ftmP);
  62.         for (int i=0;i<instcount;i++){
  63.             int newinst=permute[ftm.peek()];
  64.             ftm.seekp(ftm.tellg(),ios_base::beg);
  65.             ftm.write(reinterpret_cast<char*>(&newinst),1);
  66.             ftm.seekg(0x3,ios_base::cur);
  67.             switch(ftm.get()){
  68.                 case 1: // 2A03
  69.                     ftm.seekg(0x9E+0x30*(version>=2)+0x60*(version>=6),ios_base::cur); break;
  70.                 case 2: // VRC6
  71.                     ftm.seekg(0xE,ios_base::cur); break;
  72.                 case 3: // VRC7
  73.                     ftm.seekg(0xC,ios_base::cur); break;
  74.                 case 4: // FDS
  75.                     ftm.seekg(0x6C,ios_base::cur);
  76.                     for (int j=0;j<3;j++) ftm.seekg(ftm.get()+0xC,ios_base::cur); break;
  77.                 case 5: // N163
  78.                     ftm.seekg(0xE,ios_base::cur);
  79.                     int wavecount=get32le(ftmP);
  80.                     ftm.seekg(0x4,ios_base::cur);
  81.                     ftm.seekg(wavecount*get32le(ftmP),ios_base::cur); break;}
  82.             ftm.seekg(get32le(ftmP),ios_base::cur);}
  83.         ftm.seekg(0x14,ios_base::cur);
  84.         ftm.seekg(get32le(ftmP)+0x14,ios_base::cur);
  85.         ftm.seekg(get32le(ftmP)+0x10,ios_base::cur); // seeks through "PATTERNS        " for normally created FTMs
  86.         version=get32le(ftmP);
  87.         int patsize=get32le(ftmP)+ftm.tellg();
  88.         while (ftm.tellg()<patsize){
  89.             int track=get32le(ftmP);
  90.             int channel=get32le(ftmP);
  91.             ftm.seekg(0x4,ios_base::cur);
  92.             int rowcount=get32le(ftmP);
  93.             for(int i=0;i<rowcount;i++){
  94.                 ftm.seekg(0x6,ios_base::cur);
  95.                 int newinst2=permute[ftm.peek()];
  96.                 ftm.seekp(ftm.tellg(),ios_base::beg);
  97.                 ftm.write(reinterpret_cast<char*>(&newinst2),1);
  98.                 int pos2=ftm.tellg();
  99.                 ftm.seekg(headerpos+(songs+1)*channel+track+1);
  100.                 ftm.seekg(pos2+3+2*ftm.get());}}}
  101.     return 0;}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top