summaryrefslogtreecommitdiff
path: root/PE/pe-hack.lua
blob: 97f1ec28dfc81f9a88f1fdbcd168d2341045ac01 (plain)
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
267
268
slus_hacks = {

-- ASM Ptr hack
  [0x1603c] = { 0xbc },

-- ASM Font hack
  [0x284e4] = { 0x21 },
  [0x28540] = { 0x21, 0x18, 0x00, 0x00 },
  [0x28fb8] = { 0x21, 0x18, 0x00, 0x00 },
  [0x29074] = { 0x00, 0x00, 0x00 },


-- Text "Susceptible to poison"
  [0x81c96] = { 0x25, 0x34, 0x3d, 0x38, 0x3c, 0x34, 0x44, 0x47,
                0xff },

-- Text "Susceptible to acid"
  [0x81cc0] = { 0x14, 0x3c, 0x3f, 0x3e, 0x38, 0x42, 0x3e, 0x3d,
                0x3d, 0x34, 0x0f, 0x3f, 0x30, 0x41, 0x0f, 0x30,
	        0x32, 0x38, 0x33, 0x34, 0xff },

-- Text "Easily tranquilized"
  [0x81ce8] = { 0x22, 0x34, 0x3d, 0x42, 0x38, 0x31, 0x3b, 0x34,
                0x0f, 0x30, 0x44, 0x47, 0x0f, 0x43, 0x41, 0x30,
	        0x3d, 0x40, 0x44, 0x38, 0x3b, 0x38, 0x42, 0x30,
	        0x3d, 0x43, 0x42, 0xff },

-- Text "Sensitive to heat"
  [0x81d0e] = { 0x22, 0x34, 0x3d, 0x42, 0x38, 0x31, 0x3b, 0x34,
                0x0f, 0x51, 0x0f, 0x3b, 0x30, 0x0f, 0x32, 0x37,
	        0x30, 0x3b, 0x34, 0x44, 0x41, 0xff },

-- Text "Sensitive to cold"
  [0x81d32] = { 0x22, 0x34, 0x3d, 0x42, 0x38, 0x31, 0x3b, 0x34,
                0x0f, 0x30, 0x44, 0x0f, 0x35, 0x41, 0x3e, 0x38,
	        0x33, 0xff },

-- Text "Objects easily stolen"
  [0x81d5a] = { 0x1f, 0x3e, 0x42, 0x42, 0x4e, 0x33, 0x34, 0x0f,
                0x33, 0x34, 0x42, 0x0f, 0x3e, 0x31, 0x39, 0x34,
                0x43, 0x42, 0x0f, 0x51, 0x0f, 0x45, 0x3e, 0x3b,
                0x34, 0x41, 0xff },

-- Text "Gets confused easily"
  [0x81d85] = { 0x12, 0x3e, 0x3d, 0x35, 0x44, 0x42, 0x38, 0x3e,
                0x3d, 0x0f, 0x3f, 0x3e, 0x42, 0x42, 0x38, 0x31,
                0x3b, 0x34, 0x0f, 0x34, 0x43, 0x0f, 0x34, 0x35,
                0x35, 0x38, 0x32, 0x30, 0x32, 0x34, 0xff },

-- Text "Failed to escape"
  [0x81dad] = { 0x15, 0x44, 0x38, 0x43, 0x34, 0x0f, 0x4d, 0x32,
                0x37, 0x44, 0x4d, 0x34, 0xff },

-- Text "Can't escape!"
  [0x81dca] = { 0x15, 0x44, 0x38, 0x43, 0x34, 0x0f, 0x38, 0x3c,
                0x3f, 0x3e, 0x42, 0x42, 0x38, 0x31, 0x3b, 0x34,
                0x2b, 0xff },


-- Font width table
  [0x81ea1] = {  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  6,  6,  8, 11, 14,
                 9,  8,  7,  8,  7,  7,  8,  9,  6,  7,  9,  8, 11,  9,  7,  8,
                 7,  9,  7,  7,  9,  9, 11,  7,  9,  7,  9,  4,  7,  6,  4,  4,
                 8,  8,  7,  7,  7,  7,  8,  9,  5,  4,  8,  5, 11,  9,  7,  8,
                 8,  7,  6,  5,  9,  9, 11,  7,  9,  6,  4,  4,  7,  7,  7,  7,
                 7,  7,  7,  7,  5,  5,  7,  7,  9,  9,  9,  7, 11, 11,  0,  0,
                 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
                 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
                 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
                 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 },

-- PE.BIN filename extension
  [0x01b4c] = { 0x54, 0x49, 0x4d },
}

function apply_hacks(hacks, file)
    local k, v, i, h
    for k, v in pairs(slus_hacks) do
        for i, h in ipairs(v) do
            file[k + i - 1] = h
        end
    end
end

function mainpatch()
    local dirent, pvd, root, is_cd1, is_cd2, slusname, fmvdir, systemcnfstring, inslus, slus, peimg, pesect, tim, startsect, fmvdirtree

    dirent = cdutil:findpath "/SLUS_006.62;1" or cdutil:findpath "/SLUS_006.68;1"  or error "Not a Parasite Eve CD"

    iso:foreword(cdutil)
    pvd = createpvd(cdutil)

    print "Identifying CD..."
    if (dirent.id == "SLUS_006.62;1") then
        print "CD is Parasite Eve CD1"
	pvd.volid = "PARASITE_EVE_FRANCAIS_CD1"
        slusname = "PE-FR.CD1"
        fmvdir = "FMV1"
        is_cd1 = true
        is_cd2 = false
    else
        print "CD is Parasite Eve CD2"
	pvd.volid = "PARASITE_EVE_FRANCAIS_CD2"
        slusname = "PE-FR.CD2"
        fmvdir = "FMV2"
        is_cd1 = false
        is_cd2 = true
    end
    root = iso:setbasics(pvd)

    systemcnfstring = "BOOT=cdrom:\\" .. slusname .. [[;1
TCB=4
EVENT=16
STACK=801fff00
]]

    systemcnf = Buffer()
    systemcnf:write(systemcnfstring)
    iso:createfile(root, "SYSTEM.CNF", systemcnf, dirent)

    print "Reading CD's slus file"
    inslus = cdfile(cdutil, dirent)
    slus = Buffer(true)
    slus:copyfrom(inslus)
    apply_hacks(slus_hacks, slus)
    
    dirent = cdutil:findpath "/"
    root:setbasicsxa()
    root:fromdir(dirent)
    
    dirent = cdutil:findpath "/PE.IMG;1"
    pesect = dirent.Sector
    peimg = cdfile(cdutil, dirent)
    tim = Buffer()
    peimg:copyto(tim, 0x23800)
    startsect = iso:createfile(root, "PE.TIM", tim, dirent).sector
    print "Compiling IMG file"
    do_img_file(slus, startsect, pesect)

    print "Writing final main file"
    slus:seek(0)
    iso:createfile(root, slusname, slus):setbasicsxa()
    
    print "Copying FMV directory"
    dirent = cdutil:findpath("/" .. fmvdir)
    fmvdirtree = iso:createdir(root, fmvdir, 1, dirent)
--    fmvdirtree.hardhide = true
    iso:copydir(fmvdirtree, cdutil, dirent)
    
    print "Finalizing CD"
    iso:createfile(root, "README.TXT", Input("readme.txt")):setbasicsxa()

    iso:close()
end

function do_img_file(slus, startsect, pesect)
    local i, j, sect1, tab1, sect2, tab2, sect3, tab3, sizes1, sizes2, sizes3, sect, file, file1, file2, file3, songsect, b1, b2, b3, b4, lastsect, s1, s2, s3
    
    sect1 = {}
    tab1 = {}
    sect2 = {}
    tab2 = {}
    sect3 = {}
    tab3 = {}
    sizes1 = {}
    sizes2 = {}
    sizes3 = {}
    
    
    print "Putting various files"
    slus:seek(0x838da)
    for i = 1, 80, 1 do
        sect1[i] = slus:readU16() + pesect
    end
    for i = 1, 79, 1 do
--        print(i)
        file = cdfile(cdutil, sect1[i], (sect1[i + 1] - sect1[i]) * 2048)
	s1 = iso:putfile(file)
        tab1[i] = s1 - startsect
	print("Putting file " .. i .. " from sector " .. sect1[i] .. " to sector " .. s1)
    end
    tab1[80] = iso:getdispsect() - startsect
    songsect = tab1[80]
    
    print "Putting musics"
    slus:seek(0x83980)
    for i = 1, 251, 1 do
        sect2[i] = slus:readU16() + sect1[80]
    end
    for i = 1, 250, 1 do
--	print(i)
	file = nil
	if ((sect2[i + 1] - sect2[i]) ~= 0) then
    	    file = cdfile(cdutil, sect2[i], (sect2[i + 1] - sect2[i]) * 2048)
	end
	if (file) then
	    s1 = iso:putfile(file)
	end
	tab2[i] = s1 - startsect - songsect
	print("Putting music " .. i .. " from sector " .. sect2[i] .. " to sector " .. s1)
    end
    tab2[251] = iso:getdispsect() - startsect - songsect
    
    
    print "Putting rooms"
    slus:seek(0x83b78)
    for i = 1, 438, 1 do
        sect3[i] = slus:readU32() + pesect
	b1 = slus:readU8()
	b2 = slus:readU8()
	b3 = slus:readU8()
	b4 = slus:readU8()
	sizes1[i] = b1
	sizes2[i] = orB(b2, (shl(andB(b3, 0x0f), 8)))
	sizes3[i] = orB(shr(b3, 4), shl(b4, 4))
    end
    for i = 1, 438, 1 do
--        print(i)
	file1 = nil
	file2 = nil
	file3 = nil
	if (sizes1[i] ~= 0) then
	    print("File " .. i .. " from sector " .. sect3[i])
            file1 = cdfile(cdutil, sect3[i], sizes1[i] * 2048)
	end
	if (sizes2[i] ~= 0) then
	    print("File " .. i .. " from sector " .. sect3[i] + sizes1[i])
    	    file2 = cdfile(cdutil, sect3[i] + sizes1[i], sizes2[i] * 2048)
	end
	if (sizes3[i] ~= 0) then
	    print("File " .. i .. " from sector " .. sect3[i] + sizes1[i] + sizes2[i])
	    file3 = cdfile(cdutil, sect3[i] + sizes1[i] + sizes2[i], sizes3[i] * 2048)
	end
	
	s1 = iso:getdispsect() - startsect
	if (file1) then
	    print("Put file " .. i .. " at " .. s1)
	    iso:putfile(file1)
	end
	s2 = iso:getdispsect() - startsect
	if (file2) then
	    iso:putfile(file2)
	end
	s3 = iso:getdispsect() - startsect
	if (file3) then
	    iso:putfile(file3)
	end
	tab3[i] = s1
	sizes1[i] = s2 - s1
	sizes2[i] = s3 - s2
	sizes3[i] = iso:getdispsect() - s3
    end
    
    slus:wseek(0x838da)
    for i = 1, 80, 1 do
        slus:writeU16(tab1[i])
    end
    slus:writeU16(0)
    slus:writeU16(songsect)
    slus:writeU16(0)
    for i = 1, 251, 1 do
        slus:writeU16(tab2[i])
    end

    slus:wseek(0x83b78)
end

mainpatch()