-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgm8x_fix.lua
266 lines (216 loc) · 6.76 KB
/
gm8x_fix.lua
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
--| Requires |------------------------------------------------------------------
require "patch_types"
--| Split functions |-----------------------------------------------------------
local write, wait, exit = io.write, io.read, os.exit
local function concat(t)
return table.concat(t, "\n")
end
local function print(str, ...)
_G.print(tostring(str):format(...))
end
local function prompt(msg, ...)
local char
repeat
io.write((msg .. " [y/n] "):format(...))
char = string.lower(io.read())
until char == 'y' or char == 'n'
return char == 'y'
end
local function check_patch(file, patches)
local char
local patched, unpatched = true, true
for _,patch in ipairs(patches) do
file:seek("set", patch.pos)
char = file:read(1)
if char ~= patch.newByte then patched = false end
if char ~= patch.origByte then unpatched = false end
end
return patched and PatchState.DONE or
unpatched and PatchState.ABLE or
PatchState.UNFOUND
end
local function patch_exe(file, patches)
for _,patch in ipairs(patches) do
file:seek("set", patch.pos)
file:write(patch.newByte)
end
end
local titleBox = concat {
"+-----------------------------------------+",
"| gm8x_fix_lua v0.2 -- Feb 6th 2023 |",
"| https://github.com/Apis035/gm8x_fix_lua |",
"+-----------------------------------------+",
"",
}
local help = concat {
"Usage: " .. arg[0] .. " file [options]",
" or drag the game file into the patcher.",
"",
" -h print this help",
" -s silent mode",
" -nb disable backup",
" -ni disable input lag patch",
" -nj disable joystick patch",
" -ns disable scheduler patch",
" -nr disable display reset patch",
" -nm disable memory patch",
" -nd disable DirectPlay Patch",
"",
}
--| Argument parsing |----------------------------------------------------------
local inputFile
local isSilent, makeBackup = false, true
local disabledPatch = {}
for i=1, #arg do
local arg = arg[i]
if arg == "-h" then
print(titleBox)
print(help)
exit(1)
elseif arg == "-s" then isSilent = true
elseif arg == "-nb" then makeBackup = false
elseif arg == "-nj" then disabledPatch[PatchType.JOY] = true
elseif arg == "-nm" then disabledPatch[PatchType.MEM] = true
elseif arg == "-nd" then disabledPatch[PatchType.DPLAY] = true
elseif arg == "-ns" then disabledPatch[PatchType.SCHED] = true
elseif arg == "-ni" then disabledPatch[PatchType.INPUTLAG] = true
elseif arg == "-nr" then disabledPatch[PatchType.RESET] = true
-- Select last inputted file if multiple file is supplied
else inputFile = arg end
end
-- Show title if not in silent mode
if not isSilent or inputFile == nil then
print(titleBox)
end
-- Check if user disabled all patches
local isAllPatchesDisabled = true
for i=1, #PatchType do
if disabledPatch[i] ~= true then
isAllPatchesDisabled = false
break
end
end
if isAllPatchesDisabled then
print "All patches is disabled, no operation will be performed."
exit(1)
end
-- No file specified
if inputFile == nil then
print "Input file is not specified."
print ""
print(help)
exit(1)
end
-- Disable print and prompt if silent mode is on
if isSilent then
print = function() end
prompt = function() return true end
end
--| Reading input file |--------------------------------------------------------
print("Inspecting file %s...\n", inputFile)
local file, err = io.open(inputFile, "r+b")
-- Can't open file
if file == nil then
print("Could not open file (%s).", err)
exit(1)
end
-- No MZ header
if file:read(2) ~= "MZ" then
print "This is not an executable file."
file:close()
exit(1)
end
--| Scanning patches |----------------------------------------------------------
local patchList = require "patches"
local hasAppliedPatch, canApplyPatch = false, false
for _,patch in pairs(patchList) do
patch.state = check_patch(file, patch.bytes)
-- Assume patch is not found if patch is disabled
if disabledPatch[patch.type] and patch.state == PatchState.ABLE then
patch.state = PatchState.UNFOUND
end
-- There are patch that has been applied
if patch.state == PatchState.DONE then
hasAppliedPatch = true
end
-- There are patch that can be applied
if patch.state == PatchState.ABLE then
canApplyPatch = true
end
end
-- No applicable patch
if not canApplyPatch and not hasAppliedPatch then
print "This game cannot be patched."
print "It may not be a GameMaker 7.0, 8.0, or 8.1 game."
file:close()
exit(1)
end
-- List applied patches
if hasAppliedPatch then
print "Patches already applied:"
for _,patch in pairs(patchList) do
if patch.state == PatchState.DONE then
print(" * %s", patch.name)
end
end
end
print ""
-- List applicable patches
if canApplyPatch then
print "Patches can be applied:"
for _,patch in pairs(patchList) do
if patch.state == PatchState.ABLE then
print(" * %s", patch.name)
end
end
else
-- All patch has been applied
print "No new patches can be applied."
file:close()
exit(1)
end
print ""
--| Applying patches |----------------------------------------------------------
-- Backup file
if makeBackup then
print "Backup will be made before applying patches.\n"
local backupName = inputFile .. '.bak'
local backupFile = io.open(backupName, "rb")
local doBackup = backupFile == nil
if backupFile then
backupFile:close()
print("Previous backup file exist on %s", backupName)
doBackup = prompt "Do you want to overwrite backup file?"
or not prompt "Continue applying patches without backup?"
and exit(1)
end
if doBackup then
os.execute("copy " .. inputFile .. " " .. backupName)
end
end
print ""
-- Begin applying patch
local joystickPatched = false
for _,patch in ipairs(patchList) do
-- Check if joystick already patched
if patch.type == PatchType.JOY and patch.state == PatchState.DONE then
joystickPatched = true
end
-- Warn if joystick is not patched
if patch.state == PatchState.ABLE then
if patch.type == PatchType.SCHED and not joystickPatched then
print "Joystick patch is not applied."
print "Scheduler patch may not work without joystick patch."
end
-- Apply patch
if prompt("Apply %s?", patch.name) then
patch_exe(file, patch.bytes)
if patch.type == PatchType.JOY then
joystickPatched = true
end
end
end
end
file:close()
print "All done!\n"
exit(0)