forked from PekanMmd/Pokemon-XD-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXGStringManager.swift
192 lines (152 loc) · 3.56 KB
/
XGStringManager.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// XGStringManager.swift
// GoD Tool
//
// Created by StarsMmd on 20/08/2017.
//
//
import Foundation
var allStringTables = [XGStringTable]()
var stringsLoaded = false
func loadAllStrings() {
if !stringsLoaded {
previousFound = 0
previousFrom = 0
previousFree = []
allStringTables = [XGFiles.common_rel.stringTable]
if game == .Colosseum || region == .US {
allStringTables += [XGStringTable.dol()]
}
if game == .XD && region == .US {
allStringTables += [XGStringTable.dol2()]
}
if game == .XD {
allStringTables += [XGFiles.tableres2.stringTable]
}
XGFolders.StringTables.map{ (file: XGFiles) -> Void in
if file.fileType == .msg {
let table = file.stringTable
if table.numberOfEntries > 0 {
allStringTables += [table]
}
}
}
stringsLoaded = true
}
}
func getStringWithID(id: Int) -> XGString? {
if id == 0 {
return nil
}
loadAllStrings()
for table in allStringTables {
if table.containsStringWithId(id) {
if let s = table.stringWithID(id) {
return s
}
}
}
return nil
}
func getStringSafelyWithID(id: Int) -> XGString {
if id == 0 {
return XGString(string: "-", file: nil, sid: nil)
}
loadAllStrings()
for table in allStringTables {
if table.containsStringWithId(id) {
if let s = table.stringWithID(id) {
return s
}
}
}
return XGString(string: "-", file: nil, sid: nil)
}
func getStringsContaining(substring: String) -> [XGString] {
loadAllStrings()
var found = [XGString]()
for table in allStringTables {
for str in table.allStrings() where str.containsSubstring(substring) {
found.append(str)
}
}
return found
}
private func doesMSGIDExists(_ id: Int) -> Bool {
loadAllStrings()
for table in allStringTables {
if table.containsStringWithId(id) {
return true
}
}
return false
}
private func numberOfFreeMSGIDsFrom(_ id: Int) -> Int {
loadAllStrings()
var free = 10
for table in allStringTables {
free = min(free, table.numberOfFreeMSGIDsFrom(id))
}
return free
}
var isSearchingForFreeStringID = false
func freeMSGID() -> Int? {
// start from 1000 just to be safe
// might have special rules for lower ids
// like they could be hard coded or hidden in
// weird files
// plenty of ids available so no need to risk it
// even if unlikely
// can use freemsgid(from:) if below 1000 is desired
// Update: upon testing it seems starting at 1000 is smart.
// id 1 showed no text but id 1000 worked as expected
// don't know what the minimum is but 1000 is guaranteed
if isSearchingForFreeStringID {
return nil
}
return freeMSGID(from: 1000)
}
private var previousFrom = 0
private var previousFound = 0
private var previousFree = [Int]()
func freeMSGID(from: Int) -> Int? {
guard !isSearchingForFreeStringID else {
return nil
}
isSearchingForFreeStringID = true
ISO.extractMenuFSYS()
ISO.extractSpecificStringTables()
ISO.extractCommon()
var min = from < 1 ? 1 : from
if min <= previousFound && min >= previousFrom {
min = previousFound + 1
} else {
previousFrom = from
}
for i in min ..< min + 10 {
if previousFree.contains(i) {
previousFree.remove(at: previousFree.index(of: i)!)
previousFound = i
isSearchingForFreeStringID = false
return i
}
}
for i in min ... kMaxStringID {
if verbose {
printg("checking id: \(i)")
}
let num = numberOfFreeMSGIDsFrom(i)
if num > 0 {
previousFound = i
if num > 2 {
for j in 1 ..< num {
previousFree.addUnique(num + j)
}
}
isSearchingForFreeStringID = false
return i
}
}
isSearchingForFreeStringID = false
return nil
}