#!/usr/bin/python
import os, sys, subprocess, shutil
# working directory for messing with big files
curr_dir=os.path.realpath(os.path.dirname(__file__))
# path of unself, make_self_npdrm, make_package_npdrm, and psn_package_npdrm.exe
tools_dir=os.path.join(curr_dir, 'bin')
def do(cmd):
""" shorthand for running comands """
#print cmd
p=subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
return p.communicate()
try:
sfo=os.path.realpath(sys.argv[1])
gamedir = os.path.realpath(os.path.dirname(sys.argv[1]))
except IndexError:
print "You need to specify the location of the PARAM.SFO file."
sys.exit(1)
# get gameid
gameid='BRKN00000'
with open(sfo, 'read') as f:
gameid=f.read()[-24:-15]
print "Files need to be copied from PS3_GAME to PS3 into /dev_hdd0/G%s (don't copy USRDIR/EBOOT.bin.)" % gameid[2:]
# make a copy of important files for package
os.mkdir(os.path.join(curr_dir, gameid+'-pkg'))
os.mkdir(os.path.join(curr_dir, gameid+'-pkg', 'USRDIR'))
shutil.copyfile(os.path.join(gamedir, 'ICON0.PNG'), os.path.join(curr_dir, gameid+'-pkg','ICON0.PNG'))
# shutil.copyfile(os.path.join(gamedir, 'USRDIR','EBOOT.BIN'), os.path.join(curr_dir, gameid + '-EBOOT.BIN.orig'))
try:
shutil.copyfile(os.path.join(gamedir, 'PIC1.PNG'), os.path.join(curr_dir, gameid+'-pkg','PIC1.PNG'))
except:
pass
# unself EBOOT
do("%s %s %s" % (os.path.join(tools_dir,'unself'), os.path.join(gamedir, 'USRDIR', 'EBOOT.BIN'), 'eboot_out.elf'))
# find & replace
b=''
with open('eboot_out.elf', 'read') as f:
b=f.read()
with open('eboot_out.elf', 'write') as f:
b = b.replace('dev_bdvd', 'dev_hdd0').replace('PS3_GAME','G' + gameid[2:])
f.write(b)
with open(sfo, 'read') as f:
b=f.read()
with open(os.path.join(curr_dir, gameid+'-pkg','PARAM.SFO'), 'write') as f:
b = b.replace('DG%c%cLibrary programs' %(0,0), 'HG%c%cLibrary programs' %(0,0))
f.write(b)
# re-self EBOOT
do('%s eboot_out.elf %s UP0001-%s_00-0000111122223333' % (os.path.join(tools_dir,'make_self_npdrm'), os.path.join(curr_dir, gameid+'-pkg', 'USRDIR', 'EBOOT.BIN'), gameid))
# make conf file for pkg
with open(os.path.join(curr_dir,gameid +'.conf'), 'write') as f:
f.write("""Content-ID = UP0001-%s_00-0000111122223333
k_licensee = 0x00000000000000000000000000000000
DRM_Type = Free
Content_Type = Game_Exec
PackageVersion = 01.00""" %(gameid))
#TODO: this doesn't work! I use wine and psn_package_npdrm to do it.)
#make pkg
#do('%s %s %s' % (os.path.join(tools_dir,'make_package_npdrm'), os.path.join(curr_dir, gameid + '.conf'), os.path.join(curr_dir, gameid+'-pkg')))
do('wine %s %s %s' % (os.path.join(tools_dir,'psn_package_npdrm.exe'), os.path.join(curr_dir, gameid + '.conf'), os.path.join(curr_dir, gameid+'-pkg')))
# cleanup
os.unlink('eboot_out.elf')
os.unlink(os.path.join(curr_dir, gameid + '.conf'))
shutil.rmtree(os.path.join(curr_dir, gameid+'-pkg'))