Advertisement
dvk

PoE particle remover

dvk
Dec 24th, 2014
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. /**
  2. * removes all particle / emitter durations from Content.ggpk (Path Of Exile)
  3. * creates new file, renames old .ggpk to .bak*
  4. *
  5. * code isn't optimized at all, but works fine.
  6. * 2014-12-21.
  7. *
  8. */
  9.  
  10. #include <sstream>
  11. #include <string>
  12. #include <fstream>
  13. #include <iostream>
  14.  
  15. using namespace std;
  16.  
  17. //========================================
  18.  
  19. inline bool isFileExists (const string& name) {
  20.     if (FILE *file = fopen(name.c_str(), "r")) {
  21.         fclose(file);
  22.         return true;
  23.     } else {
  24.         return false;
  25.     }
  26. }
  27.  
  28. //========================================
  29.  
  30. const int CHARCODE_PREV_0 = 0x2F;
  31. const int CHARCODE_AFTER_9 = 0x3A;
  32. const int CHARCODE_POINT = 0x2E;
  33. const int CHARCODE_SPACE = 0x20;
  34.  
  35. /**
  36. * changes strings like "var 1" -> "var 0", "var 23.456" -> "var 00.000" etc where '0' == param var.
  37. *
  38. * @param line - string for replacement.
  39. * @param varName - finding string. next symbols will be replaced to param var if they're digital.
  40. * @param var - new var symbol.
  41. * @param offset - skip symbols before var position.
  42. * @param multivars - if there're can be few vars. i.e. with multivars = true and var = "0": "someparam 1 2.2 3" -> "someparam 0 0.0 0".
  43. */
  44. void replaceStringVar(string& line, const string& varName, const string& var, int offset = 0, bool multivars = true) {
  45.         //unicode.
  46.         size_t linePos = 0;
  47.         size_t lineLen = line.length();
  48.         size_t varNameLen = varName.length();
  49.         size_t newVarPos = 0;
  50.         int oldVar = 0;
  51.         bool next = true;
  52.         size_t varNum = 0;
  53.         while((linePos = line.find(varName, linePos)) != string::npos) {
  54.             next = true;
  55.             varNum = 0;
  56.             while (next) {
  57.                 newVarPos = linePos + varNameLen + offset + varNum;
  58.                 if (newVarPos < lineLen) {
  59.                     oldVar = int(line[newVarPos]);
  60.                     if (oldVar > CHARCODE_PREV_0 && oldVar < CHARCODE_AFTER_9) {
  61.                         line.replace(newVarPos, 1, var);
  62.                     } else if (oldVar == CHARCODE_POINT) {
  63.                         //skip
  64.                     } else if (oldVar == CHARCODE_SPACE) {
  65.                         if (!multivars) {
  66.                             next = false;
  67.                         }
  68.                     } else {
  69.                         next = false;
  70.                     }
  71.                 } else {
  72.                     break;
  73.                 }
  74.                 varNum += 2; //unicode.
  75.             }
  76.             linePos += varNameLen;
  77.         }        
  78. }
  79.  
  80. //========================================
  81.  
  82. /** not really, it's just converting. don't use wstring. */
  83. string ansiToUnicode(string& line) {
  84.     const char cNull = 0x00;
  85.     string ret = "";
  86.     int len = line.length();
  87.     for (int i = 0; i < len; i++) {
  88.         //actually it must be first \0, then char but it's easier to replace the next symbol, not the next+1
  89.         ret += line[i];
  90.         ret += cNull;
  91.     }
  92.     return ret;
  93. }
  94.  
  95. //========================================
  96.  
  97. int main() {
  98.     string fIn = "Content.ggpk";
  99.  
  100.     string fOut = fIn + "_repl.tmp";
  101.     const string replVar = "0";
  102.  
  103.     //todo: make array
  104.     string sAnsiPD1 = "particle_duration ";    
  105.     string sRepl1 = ansiToUnicode(sAnsiPD1);    
  106.  
  107.     string sAnsiPD2 = "particle_duration_variance ";    
  108.     string sRepl2 = ansiToUnicode(sAnsiPD2);    
  109.  
  110.     string sAnsiPD3 = "particles_per_second ";    
  111.     string sRepl3 = ansiToUnicode(sAnsiPD3);
  112.  
  113.     string sAnsiPD4 = "emitter_duration ";    
  114.     string sRepl4 = ansiToUnicode(sAnsiPD4);
  115.  
  116.  
  117.     //===
  118.    
  119.     const char charNewLine1 = 0x0A;
  120.     string sNewLine;
  121.     sNewLine = charNewLine1;
  122.  
  123.     ifstream inFile(fIn.c_str(), ifstream::binary);
  124.     ofstream outFile(fOut.c_str(), ofstream::binary);
  125.     //todo: check if everything is ok
  126.    
  127.     size_t iLine = 0;
  128.     string line;
  129.  
  130.     bool isContinue = getline(inFile, line);
  131.  
  132.     if (isContinue) {
  133.         cout<<"\nopened file: "<<fIn<<"\n";
  134.  
  135.         while (isContinue) {
  136.             //iLine++;
  137.  
  138.             istringstream iss(line);
  139.            
  140.             replaceStringVar(line, sRepl1, replVar, 0, true);
  141.             replaceStringVar(line, sRepl2, replVar, 0, true);
  142.             replaceStringVar(line, sRepl3, replVar, 0, true);
  143.             replaceStringVar(line, sRepl4, replVar, 0, true);
  144.  
  145.             outFile<<line;
  146.  
  147.             isContinue = getline(inFile, line);
  148.             //todo: read bigger part of file for less i/o operations.
  149.             if (isContinue) {
  150.                 outFile<<sNewLine;        
  151.             }
  152.         }
  153.  
  154.         inFile.close();
  155.         outFile.close();
  156.  
  157.         cout<<"replacement: done.\n";
  158.  
  159.         string defaultBak = fIn + ".bak";
  160.         string newBak = "";
  161.         bool renamed = false;
  162.         for (char c = '0'; c < '6'; c++) {
  163.             newBak = defaultBak + c;
  164.             if (!isFileExists(newBak)) {
  165.                 int result = rename(fIn.c_str(), newBak.c_str());
  166.                 if (result == 0) {
  167.                     cout<<"old file saved as: "<<newBak<<"\n";
  168.                     result = rename(fOut.c_str(), fIn.c_str());
  169.                     if (result == 0) {
  170.                         cout<<"new file saved as: "<<fIn<<"\n";
  171.                     }
  172.                     renamed = true;
  173.                     break;
  174.                 }
  175.             }
  176.         }
  177.         if (!renamed) {
  178.             cout<<"can't rename file: "<<fIn<<"\n to: "<<newBak<<"\n\nFile with replacements is: "<<fOut;
  179.         }
  180.  
  181.        
  182.     } else {
  183.         cout<<"\nCan't open file.";
  184.     }
  185.  
  186.     cout<<"\n.";
  187.     char c[1024];
  188.     cin>>c;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement