Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * removes all particle / emitter durations from Content.ggpk (Path Of Exile)
- * creates new file, renames old .ggpk to .bak*
- *
- * code isn't optimized at all, but works fine.
- * 2014-12-21.
- *
- */
- #include <sstream>
- #include <string>
- #include <fstream>
- #include <iostream>
- using namespace std;
- //========================================
- inline bool isFileExists (const string& name) {
- if (FILE *file = fopen(name.c_str(), "r")) {
- fclose(file);
- return true;
- } else {
- return false;
- }
- }
- //========================================
- const int CHARCODE_PREV_0 = 0x2F;
- const int CHARCODE_AFTER_9 = 0x3A;
- const int CHARCODE_POINT = 0x2E;
- const int CHARCODE_SPACE = 0x20;
- /**
- * changes strings like "var 1" -> "var 0", "var 23.456" -> "var 00.000" etc where '0' == param var.
- *
- * @param line - string for replacement.
- * @param varName - finding string. next symbols will be replaced to param var if they're digital.
- * @param var - new var symbol.
- * @param offset - skip symbols before var position.
- * @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".
- */
- void replaceStringVar(string& line, const string& varName, const string& var, int offset = 0, bool multivars = true) {
- //unicode.
- size_t linePos = 0;
- size_t lineLen = line.length();
- size_t varNameLen = varName.length();
- size_t newVarPos = 0;
- int oldVar = 0;
- bool next = true;
- size_t varNum = 0;
- while((linePos = line.find(varName, linePos)) != string::npos) {
- next = true;
- varNum = 0;
- while (next) {
- newVarPos = linePos + varNameLen + offset + varNum;
- if (newVarPos < lineLen) {
- oldVar = int(line[newVarPos]);
- if (oldVar > CHARCODE_PREV_0 && oldVar < CHARCODE_AFTER_9) {
- line.replace(newVarPos, 1, var);
- } else if (oldVar == CHARCODE_POINT) {
- //skip
- } else if (oldVar == CHARCODE_SPACE) {
- if (!multivars) {
- next = false;
- }
- } else {
- next = false;
- }
- } else {
- break;
- }
- varNum += 2; //unicode.
- }
- linePos += varNameLen;
- }
- }
- //========================================
- /** not really, it's just converting. don't use wstring. */
- string ansiToUnicode(string& line) {
- const char cNull = 0x00;
- string ret = "";
- int len = line.length();
- for (int i = 0; i < len; i++) {
- //actually it must be first \0, then char but it's easier to replace the next symbol, not the next+1
- ret += line[i];
- ret += cNull;
- }
- return ret;
- }
- //========================================
- int main() {
- string fIn = "Content.ggpk";
- string fOut = fIn + "_repl.tmp";
- const string replVar = "0";
- //todo: make array
- string sAnsiPD1 = "particle_duration ";
- string sRepl1 = ansiToUnicode(sAnsiPD1);
- string sAnsiPD2 = "particle_duration_variance ";
- string sRepl2 = ansiToUnicode(sAnsiPD2);
- string sAnsiPD3 = "particles_per_second ";
- string sRepl3 = ansiToUnicode(sAnsiPD3);
- string sAnsiPD4 = "emitter_duration ";
- string sRepl4 = ansiToUnicode(sAnsiPD4);
- //===
- const char charNewLine1 = 0x0A;
- string sNewLine;
- sNewLine = charNewLine1;
- ifstream inFile(fIn.c_str(), ifstream::binary);
- ofstream outFile(fOut.c_str(), ofstream::binary);
- //todo: check if everything is ok
- size_t iLine = 0;
- string line;
- bool isContinue = getline(inFile, line);
- if (isContinue) {
- cout<<"\nopened file: "<<fIn<<"\n";
- while (isContinue) {
- //iLine++;
- istringstream iss(line);
- replaceStringVar(line, sRepl1, replVar, 0, true);
- replaceStringVar(line, sRepl2, replVar, 0, true);
- replaceStringVar(line, sRepl3, replVar, 0, true);
- replaceStringVar(line, sRepl4, replVar, 0, true);
- outFile<<line;
- isContinue = getline(inFile, line);
- //todo: read bigger part of file for less i/o operations.
- if (isContinue) {
- outFile<<sNewLine;
- }
- }
- inFile.close();
- outFile.close();
- cout<<"replacement: done.\n";
- string defaultBak = fIn + ".bak";
- string newBak = "";
- bool renamed = false;
- for (char c = '0'; c < '6'; c++) {
- newBak = defaultBak + c;
- if (!isFileExists(newBak)) {
- int result = rename(fIn.c_str(), newBak.c_str());
- if (result == 0) {
- cout<<"old file saved as: "<<newBak<<"\n";
- result = rename(fOut.c_str(), fIn.c_str());
- if (result == 0) {
- cout<<"new file saved as: "<<fIn<<"\n";
- }
- renamed = true;
- break;
- }
- }
- }
- if (!renamed) {
- cout<<"can't rename file: "<<fIn<<"\n to: "<<newBak<<"\n\nFile with replacements is: "<<fOut;
- }
- } else {
- cout<<"\nCan't open file.";
- }
- cout<<"\n.";
- char c[1024];
- cin>>c;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement