summaryrefslogtreecommitdiff
path: root/VP-isowork.lua
blob: a5976ad536e5d2756c1f6045566fdf34fe2eab5f (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
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)
    if got_psp then
        local x = Output "VP-FR.iso"
        x:destroy()
        globals[3] = dvdabstract(file_out or "VP-FR.iso", true)
    else
        globals[3] = Output(file_out or "VP-FR.bin")
    end
    local iso = isobuilder(globals[3])
    return iso
end

function startup(file_in, file_in2, iso_out)
    if not loadmodule then error("This can only be used with lua-interface...") end
    loadmodule "lualibs"
    loadmodule "luahandle"
    loadmodule "luacd"
    loadmodule "luaslz"
    loadmodule "luahttp"
    loadmodule "luaxml"
    globals[1] = cdabstract(file_in or "VP-SQUISH.bin")
--    globals[1] = dvdabstract(file_in or "VP-PSP-EUR.iso")
    local cdutil = cdutils(globals[1])
    if file_in2 then
        globals[2] = cdabstract(file_in2)
        local cdutil2 = cdutils(globals[2])
    end
    return cdutil, cdutil2, iso_out
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)
    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
            got_us = true
        elseif umddata == "ULES-00724|2A9CD5DEEA986357|0001|G" then
            got_eu = true
        elseif umddata == "ULJM-05101|7862BF3F2632E5B5|0001|G" then
            got_jp = true
        else
            error "Wrong PSP iso."
        end
        
        got_psp = true
    end
    
    if slusdirentuscd1 and got_cd1 then error "Got twice CD1." end
    if slusdirentuscd2 and got_cd2 then error "Got twice CD2." end
    
    if slusdirentuscd1 or slusdirentuscd2 then got_us = true end
    if slusdirentjpcd1 or slusdirentjpcd2 then got_jp = true end
    if slusdirentuscd1 or slusdirentjpcd1 then got_cd1 = true end
    if slusdirentuscd2 or slusdirentjpcd2 then got_cd2 = true end
    
    if slusdirentsquished then
        got_cd1 = true
        got_cd2 = true
        -- let's fallback...
        got_us = true
    end

    if not slusent then
        slusent = 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
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 = "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 = "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, "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