summaryrefslogtreecommitdiff
path: root/VP-isowork.lua
blob: ace75afecf708bb02e770e9cb76c2612752abe7b (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
load "VP-slz3-patch.lua"

function apply_hacks(hacks, file)
    local k, v, i, h

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

function prepare_out(file_out)
    local iso_file
    if got_psp then
        local x = Output "VP-FR.iso"
        x:destroy()
        iso_file = dvdabstract(file_out or "VP-FR.iso", true)
    else
        iso_file = Output(file_out or "VP-FR.bin")
    end
    local iso = isobuilder(iso_file)
    iso.iso_file = iso_file
    return iso
end

function startup(file_in, file_in2, iso_out, file_in3, file_in4)
    local cdutil, cdutil2, cdutil3, cdutil4
    if not loadmodule then error("This can only be used with lua-interface...") end
    loadmodule "lualibs"
    loadmodule "luahandle"
    loadmodule "luacd"
    pcall(loadmodule, "luaslz")
    loadmodule "luahttp"
    loadmodule "luaxml"
    local r, v
    r, v = pcall(cdabstract, file_in or "VP-SQUISH.bin")
    if not r then
        v = dvdabstract(file_in or "VP-PSP-EUR.iso")
    end
    cdutil = cdutils(v)
    cdutil.iso_in = v
    if file_in2 then
        v = cdabstract(file_in2)
        cdutil2 = cdutils(v)
        cdutil2.iso_in = v
    end
    if file_in3 then
        r, v = pcall(cdabstract, file_in3)
        if not r then
            v = dvdabstract(file_in3)
        end
        cdutil3 = cdutils(v)
        cdutil3.iso_in = v
    end
    if file_in4 then
        v = cdabstract(file_in4)
        cdutil4 = cdutils(v)
        cdutil4.iso_in = v
    end
    return cdutil, cdutil2, iso_out, cdutil3, cdutil4
end

anti_cd_swap_hack = {
    [0x278C] = { 0x00, 0x00, 0x00, 0x00 },
    [0x27A0] = { 0x00, 0x00, 0x00, 0x00 },
    [0x27B4] = { 0x21, 0x10, 0x00, 0x00 },
    [0x2820] = { 0x00, 0x00, 0x00, 0x00 },
}

function iso_identify(cdutil, additionnal)
    local slusdirentuscd1 = cdutil:findpath "/SLUS_011.56;1"
    local slusdirentuscd2 = cdutil:findpath "/SLUS_011.79;1"
    local slusdirentjpcd1 = cdutil:findpath "/SLPM_863.79;1"
    local slusdirentjpcd2 = cdutil:findpath "/SLPM_863.80;1"
    local slusdirentsquished = cdutil:findpath "/VALKYRIE.EXE;1"
    local pspdata = cdutil:findpath "/UMD_DATA.BIN"
    if pspdata then
        local pspdataf = cdutil:cdfile(pspdata)
        local umddata = pspdataf:read()
        if umddata == "ULUS-10107|400F6E7A41D6C586|0001|G" then
            if not additionnal then got_us = true else got_add_us = true end
        elseif umddata == "ULES-00724|2A9CD5DEEA986357|0001|G" then
            if not additionnal then got_eu = true else got_add_eu = true end
        elseif umddata == "ULJM-05101|7862BF3F2632E5B5|0001|G" then
            if not additionnal then got_jp = true else got_add_jp = true end
        else
            error "Wrong PSP iso."
        end
        
        if not additionnal then
            got_psp = true
            got_cd1 = true
            got_cd2 = true
        else
            got_add_psp = true
            got_add_cd1 = true
            got_add_cd2 = true
        end
    end
    
    if (slusdirentuscd1 or slusdirentjpcd1) and got_cd1 and not additionnal then error "Got twice CD1." end
    if (slusdirentuscd2 or slusdirentjpcd2) and got_cd2 and not additionnal then error "Got twice CD2." end
    if (slusdirentuscd1 or slusdirentjpcd1) and got_add_cd1 and additionnal then error "Got twice CD1." end
    if (slusdirentuscd2 or slusdirentjpcd2) and got_add_cd2 and additionnal then error "Got twice CD2." end
    
    if (slusdirentuscd1 or slusdirentuscd2) and not additionnal then got_us = true end
    if (slusdirentjpcd1 or slusdirentjpcd2) and not additionnal then got_jp = true end
    if (slusdirentuscd1 or slusdirentjpcd1) and not additionnal then got_cd1 = true end
    if (slusdirentuscd2 or slusdirentjpcd2) and not additionnal then got_cd2 = true end
    
    if (slusdirentuscd1 or slusdirentuscd2) and additionnal then got_add_us = true end
    if (slusdirentjpcd1 or slusdirentjpcd2) and additionnal then got_add_jp = true end
    if (slusdirentuscd1 or slusdirentjpcd1) and additionnal then got_add_cd1 = true end
    if (slusdirentuscd2 or slusdirentjpcd2) and additionnal then got_add_cd2 = true end
    
    if slusdirentsquished and not additionnal then
        got_cd1 = true
        got_cd2 = true
        -- let's fallback...
        got_us = true
    end

    if slusdirentsquished and additionnal then
        got_add_cd1 = true
        got_add_cd2 = true
        -- let's fallback...
        got_add_jp = true
    end

    if not slusdirent then
        slusdirent = slusdirentuscd1 or slusdirentuscd2 or slusdirentjpcd1 or slusdirentjpcd2 or slusdirentsquished
    end
    
    if got_us and got_jp then error "2 isos must from the same version of the game." end
    if got_add_us and got_add_jp then error "2 isos must from the same version of the game." end
    
    if got_us and got_add_jp and got_cd1 and got_cd2 and got_add_cd1 and got_add_cd2 then
        jp_in_us = true
    end
    
    -- should check for PSP / PSX mixup...
end

function build_basic(iso, cdutil, iso_full)
    local cp = function(path, name, dirtree) copyfile(path, name, dirtree, iso, cdutil) end
    iso:foreword(cdutil)
    local pvd = createpvd(cdutil)
    pvd.volid = jp_in_us and "VP-JP-US" or "VP-FR"
    local root = iso:setbasics(pvd)
    dirent = cdutil:findpath "/"
    root:setbasicsxa()
    root:fromdir(dirent)
    if got_psp then
        root:setdvdmode()
        psp_dirtrees.root = root
        psp_dirtrees.PSP_GAME = iso:createdir(root, "PSP_GAME", 1, cdutil:findpath "/PSP_GAME")
        psp_dirtrees.PSP_GAME:setbasicsxa()
        psp_dirtrees.SYSDIR = iso:createdir(psp_dirtrees.PSP_GAME, "SYSDIR", 1, cdutil:findpath "/PSP_GAME/SYSDIR")
        psp_dirtrees.SYSDIR:setbasicsxa()
        psp_dirtrees.USRDIR = iso:createdir(psp_dirtrees.PSP_GAME, "USRDIR", 1, cdutil:findpath "/PSP_GAME/USRDIR")
        psp_dirtrees.USRDIR:setbasicsxa()
--[[
        psp_dirtrees.kmodule = iso:createdir(psp_dirtrees.USRDIR, "kmodule", 1, cdutil:findpath "/PSP_GAME/USRDIR/kmodule")
        psp_dirtrees.kmodule:setbasicsxa()
--]]
        cp("", "UMD_DATA.BIN", root)
        cp("/PSP_GAME/SYSDIR", "EBOOT.BIN", psp_dirtrees.SYSDIR)
        cp("/PSP_GAME/SYSDIR", "BOOT.BIN", psp_dirtrees.SYSDIR)
        for _, f in ipairs { "PARAM.SFO", "ICON0.PNG", "PIC0.PNG", "PIC1.PNG" } do
            cp("/PSP_GAME", f, psp_dirtrees.PSP_GAME)
        end
--[[
        for _, prx in ipairs { "audiocodec.prx", "libatrac3plus.prx", "mpeg.prx", "mpegbase.prx", "psmf.prx", "sc_sascore.prx", "videocodec.prx" } do
            cp("/PSP_GAME/USRDIR/kmodule", prx, psp_dirtrees.kmodule)
        end
]]--
        for _, prx in ipairs { "Title_master.prx", "Staff_master.prx", "Battle_master.prx" } do
            cp("/PSP_GAME/USRDIR", prx, psp_dirtrees.USRDIR)
        end
        pspval1_sec = iso:getdispsect()
        local dummy = Buffer()
        dummy:writeU8(0)
        local dirent = cdutil:findpath "/PSP_GAME/USRDIR/PSPVAL1.PFS"
        psp_dirtrees.PSPVAL1 = iso:createfile(psp_dirtrees.USRDIR, "PSPVAL1.PFS", dummy, dirent)
        psp_dirtrees.PSPVAL1:setbasicsxa()
        dummy:destroy()
    else
        local systemcnfstring
        systemcnfstring = jp_in_us and "BOOT=cdrom:\\VP-JP-US.EXE;1\r\nTCB=4\r\nEVENT=16\r\nSTACK=801fff00\r\n" or "BOOT=cdrom:\\VP-FR.EXE;1\r\nTCB=4\r\nEVENT=16\r\nSTACK=801fff00\r\n"
        local systemcnf = Buffer()
        local slus = Buffer(true)
        slus:copyfrom(cdutil:cdfile(slusdirent))
        slus:seek(0)
        systemcnf:write(systemcnfstring)
        local dirent = cdutil:findpath("/SYSTEM.CNF;1")
        iso:createfile(root, "SYSTEM.CNF", systemcnf, dirent):setbasicsxa()
        if iso_full then
            apply_hacks(anti_cd_swap_hack, slus)
        end
        apply_hacks(slz3_patch, slus)
        slus:seek(0)
        iso:createfile(root, jp_in_us and "VP-JP-US.EXE" or "VP-FR.EXE", slus, slusdirent):setbasicsxa()
        slus:seek(0)
        local readme = Buffer()
        readme:write("Readme...")
        iso:createfile(root, "README.TXT", readme, dirent):setbasicsxa()
        local cursect = iso:getdispsect()
        local falsesect = {}
        for i = cursect, 149 do
            iso:createsector(falsesect, MODE2_FORM1)
        end
    end
end