forked from PekanMmd/Pokemon-XD-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XGRelocationTable.swift
144 lines (103 loc) · 3.32 KB
/
XGRelocationTable.swift
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// XGRelocationTable.swift
// GoD Tool
//
// Created by The Steez on 19/10/2017.
//
//
import Cocoa
let common = XGCommon()
class XGCommon : XGRelocationTable {
@objc init() {
super.init(file: XGFiles.common_rel)
self.dataStart = Int(self.data!.getWordAtOffset(kCommonRELDataStartOffsetLocation))
}
var dictionary : [Int : (Int, Int)] {
var dict = [Int : (Int, Int)]()
for i in 0 ..< kNumberRelPointers {
dict[i] = (self.getPointer(index: i), self.getValueAtPointer(index: i))
}
return dict
}
}
let pocket = XGPocket()
class XGPocket : XGRelocationTable {
@objc init() {
super.init(file: XGFiles.pocket_menu)
}
}
let kCommonRELDataStartOffsetLocation = 0x6c
let kRELDataStartOffsetLocation = 0x64
let kRELPointersStartOffsetLocation = 0x24
let kRELPointersHeaderPointerOffset = 0x28
let kRELPointersFirstPointerOffset = 0x8
let kRELSizeOfPointer = 0x10
let kRELPointerDataPointer1Offset = 0x4
let kRELPointerDataPointer2Offset = 0xc
class XGRelocationTable: NSObject {
var file : XGFiles!
@objc var data : XGMutableData!
@objc var dataStart = 0
@objc var pointersStart = 0
@objc var firstPointer = 0
var numberOfPointers = 0
init(file: XGFiles) {
super.init()
self.file = file
self.data = file.data
if data != nil {
self.dataStart = Int(data.getWordAtOffset(kRELDataStartOffsetLocation))
self.pointersStart = Int(data.getWordAtOffset(kRELPointersStartOffsetLocation))
self.firstPointer = pointersStart + kRELPointersFirstPointerOffset
let pointersHeaderOffset = data.get4BytesAtOffset(kRELPointersHeaderPointerOffset)
let pointersEnd = data.get4BytesAtOffset(pointersHeaderOffset + 0xc)
var currentOffset = firstPointer
var found = false
while currentOffset < pointersEnd && !found {
let val = data.get4BytesAtOffset(currentOffset)
if val >= 0xca01 && val <= 0xcaff {
found = true
} else {
numberOfPointers += 1
}
currentOffset += kRELSizeOfPointer
}
}
}
@objc private var pointers = [Int : Int]()
@objc func getPointerOffset(index: Int) -> Int {
return firstPointer + (index * kRELSizeOfPointer) + kRELPointerDataPointer1Offset
}
@objc func getPointer(index: Int) -> Int {
if pointers[index] == nil {
let offset = firstPointer + (index * kRELSizeOfPointer) + kRELPointerDataPointer1Offset
pointers[index] = Int(data.getWordAtOffset(offset)) + dataStart
}
return pointers[index] ?? 0
}
@objc func getValueAtPointer(index: Int) -> Int {
let startOffset = getPointer(index: index)
return Int(data.getWordAtOffset(startOffset))
}
@objc func setValueAtPointer(index: Int, newValue value: Int) {
let startOffset = getPointer(index: index)
data.replaceWordAtOffset(startOffset, withBytes: value.unsigned)
data.save()
}
@objc func replacePointer(index: Int, newAbsoluteOffset newOffset: Int) {
let offset1 = firstPointer + (index * kRELSizeOfPointer) + kRELPointerDataPointer1Offset
let offset2 = firstPointer + (index * kRELSizeOfPointer) + kRELPointerDataPointer2Offset
for offset in [offset1, offset2] {
data.replaceWordAtOffset(offset, withBytes: UInt32(newOffset - dataStart))
}
data.save()
pointers[index] = newOffset
}
func allPointers() -> [Int] {
var p = [Int]()
for i in 0 ..< self.numberOfPointers {
p.append(self.getPointer(index: i))
}
return p
}
}