-
Notifications
You must be signed in to change notification settings - Fork 0
/
patcher.cpp
111 lines (86 loc) · 2.51 KB
/
patcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "patcher.h"
int Patcher::patch(QString filePath, QString patchPath, int cursopPos, QByteArray codec)
{
FileHandler file;
FileHandler patch;
if( patch.openFile(patchPath, true) != 0)
{
errorString = "Unable to open patching file.";
return 1;
}
if(file.openFile(filePath) != 0)
{
errorString = "Unable to open patched file.";
return 1;
}
QTextDocument doc(file.getFileContent());
QTextCursor cursor(&doc);
if (cursopPos != -1)
cursor.setPosition(cursopPos);
else
{
cursor.movePosition(QTextCursor::End);
cursor.select(QTextCursor::LineUnderCursor);
while(cursor.selectedText().trimmed().isEmpty()) {
if (cursor.atStart()) {
errorString = "Inner process problems with cursor.";
return 1;
}
cursor.removeSelectedText();
cursor.movePosition(QTextCursor::Up);
cursor.select(QTextCursor::LineUnderCursor);
}
cursor.movePosition(QTextCursor::StartOfLine);
}
cursor.insertText(patch.getFileContent() + "\n");
QByteArray newContent = doc.toPlainText().toUtf8();
if(file.saveFile(newContent, codec) != 0) {
errorString = "Error while saving patched file.";
return 1;
}
file.closeFile();
patch.closeFile();
patchedFilePath = filePath;
return 0;
}
int Patcher::patchCode(QString filePath, QString patchPath, int breakpoint, QByteArray codec) {
QString dirpath = filePath.section('/', 0, -2);
QDir temp(dirpath);
QString dirName = filePath.section('/', -1, -1).section('.', -2, 0);
QString fileCopy = temp.absolutePath() + '/' + dirName + '/' + filePath.section('/', -1, -1);
if(!(temp.exists(dirName)))
temp.mkdir(dirName);
if(!(temp.cd(dirName))) {
errorString = "Unable to access " + dirName;
return 1;
}
if(temp.exists(fileCopy))
temp.remove(fileCopy);
if (!(QFile::copy(filePath, fileCopy))) {
errorString = "Unable to create file copy " + fileCopy;
return 1;
}
workingDir = temp.absolutePath();
return patch(fileCopy, patchPath, breakpoint, codec);
}
QString &Patcher::getPatchedFilePath()
{
return patchedFilePath;
}
QString &Patcher::getWorkingDir()
{
return workingDir;
}
QString Patcher::getErrorString()
{
QString ret = errorString;
errorString = QString();
return ret;
}
Patcher::Patcher() :
errorString(QString())
{
}
Patcher::~Patcher()
{
}