-
Notifications
You must be signed in to change notification settings - Fork 3
/
patch.groovy
96 lines (91 loc) · 2.82 KB
/
patch.groovy
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
/*
* Script to patch binary files
*/
class Patch {
public int offset
public def patches = []
Patch(String offset, String code) {
println offset + " : " + code
this.offset = Integer.parseInt(offset, 16)
def bytes = getParts(code, 2)
for(String b : bytes) this.patches << Integer.parseInt(b, 16).intValue()
}
private static String[] getParts(String string, int partitionSize) {
def parts = []
int len = string.length()
for (int i=0; i<len; i+=partitionSize)
parts << string.substring(i, Math.min(len, i + partitionSize))
return parts;
}
}
String action = args[0]
if(action == "apply") {
if(args.length != 3) {
println "patch.jar.exe apply <new> <old>"
System.exit(0)
}
String patch = new File(args[1]).text
RandomAccessFile raf = new RandomAccessFile(args[2], "rw")
def patches = []
String currentOffset = ""
String offsetBuffer = ""
String codeBuffer = ""
boolean offsetFlag = false
boolean commentFlag = false
int i = 0
for(char c : patch.toCharArray()) {
i++
if(commentFlag){
if(c == ';' || c == '\n'){
commentFlag = false
}
continue;
}
else if(c == ';') {
commentFlag = true
}
else if(offsetFlag) {
if(c == '\n') {
currentOffset = offsetBuffer
offsetBuffer = ""
offsetFlag = false
} else {
offsetBuffer += c
}
}
else if(c == '$') {
offsetBuffer = ""
offsetFlag = true
if(currentOffset != "") {
patches << new Patch(currentOffset.replaceAll(/\s/,""), codeBuffer.replaceAll(/\s/,""))
currentOffset = ""
offsetBuffer = ""
codeBuffer = ""
}
}
else if (i == patch.length()) {
codeBuffer += c
patches << new Patch(currentOffset.replaceAll(/\s/,""), codeBuffer.replaceAll(/\s/,""))
break;
}
else codeBuffer += c
}
for(Patch p : patches) {
println "Applying Patch " + p.offset + ": " + p.patches
raf.seek(p.offset)
raf.write(p.patches as byte[], 0, p.patches.size())
}
}
else if(action == "create") {
if(args.length != 3) {
println "patch.jar.exe create <new> <old>"
System.exit(0)
}
RandomAccessFile n = new RandomAccessFile(args[1], "rw")
RandomAccessFile o = new RandomAccessFile(args[2], "rw")
int maxl = Math.max(n.length(), o.length())
int minl = Math.max(n.length(), o.length())
}
else {
println "patch.jar.exe: Action not found"
}