#SAVE THIS AS KS2Android.py (make sure you have show file extensions option (google it) turned on so you don't accidentally name it KS2Android.py.txt)
#readme.txt can be found at http://pastebin.com/S8S6GTmm
#THIS SCRIPT ALONE WON'T DO YOU MUCH GOOD. READ THE README FILE
import urllib2
import zipfile
import os
import subprocess
import shutil
import time
import sys
import traceback
from ctypes import windll
rapt = 'rapt-6.14.1.0'
raptz = 'rapt-6.14.1.0.zip'
class PROGRESS:
RAPTEXISTS = 1
ASDKEXISTS = 2
UNRPAEXISTS = 3
UNPACKED = 4
VIDEOSDELETED = 5
SCRIPTSCOPIED = 6
DECEXISTS = 7
DECOMPILED = 8
PATCHED = 9
RECOMPILED = 11
COPIEDTORAPT = 12
CONFIGURED = 13
BUILT = 14
FindWindow = windll.user32.FindWindowA
def delete(a):
try:
os.remove(a)
except:
pass
def getscript(url, filename):
u = urllib2.urlopen(url)
with open(filename, "wb") as f:
f.write(u.read())
def download(url, file_name=""):
if file_name == "":
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
file_size_dl = 0
block_sz = 8192
while True:
buf = u.read(block_sz)
if not buf:
break
file_size_dl += len(buf)
f.write(buf)
status = r" %3.2f%%" % (file_size_dl * 100. / file_size)
status = status + '\r'
print status,
f.close()
print ""
def downloadrapt():
if os.path.exists(raptz):
print 'RAPT is already downloaded'
else:
print 'Downloading RAPT...'
download('http://www.renpy.org/dl/android/rapt-6.14.1.0.zip')
def extractrapt():
print 'Extracting...',
z = zipfile.ZipFile(raptz)
z.extractall()
z.close()
print 'Done'
print 'Cleaning...',
delete(raptz)
print "Done"
def raptinstallsdk():
print "Installing Android SDK through RAPT..."
print "--------------------------------------------------------------------------------"
subprocess.call(["android.py", "installsdk"], shell=True, cwd=rapt)
print "--------------------------------------------------------------------------------"
def getunrpa():
print "Downloading UnRPA script...",
getscript("http://pastebin.com/raw.php?i=2j6pM5XM", "unrpa.py" )
print "Done"
def unpack():
print "Unpacking...",
subprocess.call(["unrpa.py", "-s", "-p", rapt + "\\ks\\game\\", "-m", "game\\data.rpa"], shell=True)
print "Done"
def deletevideos():
print "Deleting videos...",
filenames = os.listdir( os.path.join(rapt, "ks", "game", "video") )
for filename in filenames:
if os.path.isfile( os.path.join(rapt, "ks", "game", "video", filename) ) and filename.endswith(".mkv"):
delete(os.path.join(rapt, "ks", "game", "video", filename))
print "Done"
def copyscripts():
print "Copying game scripts...",
filenames = os.listdir( "game" )
for filename in filenames:
if os.path.isfile( os.path.join("game", filename) ) and filename.endswith(".rpyc") and not filename.startswith("ui_settings"):
shutil.copy( os.path.join("game", filename), os.path.join(rapt, "ks", "game", filename) )
print "Done"
def getdec():
print "Downloading decompile script...",
getscript("http://pastebin.com/raw.php?i=1n8Apg1S", "decompile.rpy" )
print "Done"
def decompile():
shutil.copy( "decompile.rpy", os.path.join("game", "decompile.rpy"))
print "Running KS to trigger the decompile process...",
subprocess.Popen('"Katawa Shoujo.exe"', shell=True)
while True:
time.sleep(1)
ret = FindWindow("pygame", "Katawa Shoujo")
if ret != 0:
subprocess.call(["taskkill", "/IM", "Katawa Shoujo.exe", "/F"], shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE)
print "Done"
break
print "Cleaning up...",
delete("ui_settings.rpy")
filenames = os.listdir('.')
for filename in filenames:
if "ui_settings" in filename:
os.rename(filename, "ui_settings.rpy")
if not "ui_settings" in filename and filename.endswith(".rpy.txt"):
delete(filename)
print "Done"
shutil.copy("ui_settings.rpy", os.path.join("game", "ui_settings.rpy"))
delete(os.path.join("game", "decompile.rpy"))
delete(os.path.join("game", "decompile.rpyc"))
def patch():
print "Patching...",
aloadable = """\
def aloadable(name):
f = None
try:
f = renpy.loader.load(name)
except:
return False
f.close()
return True
"""
f = open("ui_settings.rpy", "r")
s = f.read()
f.close()
patched = False
try:
s.index('aloadable')
print "The script is already patched"
patched = true
except:
pass
if not patched:
i = s.index(' python:') + len(' python:')
s = s[:i] + aloadable + s[i:]
s = s.replace("renpy.loadable", "aloadable")
f = open("ui_settings.rpy", "w")
f.write(s)
f.close()
print "Done"
def recompile():
shutil.copy( "ui_settings.rpy", os.path.join("game", "ui_settings.rpy"))
print "Running KS to recompile the patched script...",
subprocess.Popen('"Katawa Shoujo.exe"', shell=True)
while True:
time.sleep(1)
ret = FindWindow("pygame", "Katawa Shoujo")
if ret != 0:
subprocess.call(["taskkill", "/IM", "Katawa Shoujo.exe", "/F"], shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE)
print "Done"
break
def copytorapt():
print "Copying patched scripts...",
shutil.copy(os.path.join("game", "ui_settings.rpyc"), os.path.join(rapt, "ks", "game", "ui_settings.rpyc"))
shutil.copy(os.path.join("game", "bytecode.rpyb"), os.path.join(rapt, "ks", "game", "bytecode.rpyb"))
print "Done"
def configure():
print "Configuring the package...",
f = open( os.path.join(rapt, "ks", ".android.json"), "w")
f.write('{"orientation": "landscape", "include_pil": false, "icon_name": "Katawa Shoujo", "include_sqlite": false, "permissions": ["INTERNET", "VIBRATE"], "layout": null, "name": "Katawa Shoujo", "package": "vn.katawashoujo", "source": false, "version": "1", "numeric_version": "1"}')
f.close()
shutil.copy( os.path.join(rapt, "ks", "game", "ui", "bt-logoonly.png"), os.path.join(rapt, "ks", "android-icon.png") )
shutil.copy( os.path.join("game", "presplash.png"), os.path.join(rapt, "ks", "android-presplash.jpg") )
print "Done"
def build():
print "Building, transferring control to RAPT"
print "--------------------------------------------------------------------------------"
subprocess.call(["android.py", "build", "ks", "release"], shell=True, cwd=rapt)
print "--------------------------------------------------------------------------------"
print "Copying Katawa Shoujo package...",
shutil.copy( os.path.join(rapt, "bin", "KatawaShoujo-1-release.apk"), "KatawaShoujo.apk")
print "Done"
def buildclean():
print "Cleaning...",
filenames = os.listdir( os.path.join(rapt, "bin"))
for filename in filenames:
if os.path.isfile( os.path.join(rapt, "bin", filename ) ) and "KatawaShoujo" in filename:
delete( os.path.join(rapt, "bin", filename ) )
print "Done"
def setprogress(a):
f = open("progress.txt", "w")
f.write(str(a))
f.close()
def getprogress():
if not os.path.exists("progress.txt"):
return 0
f = open("progress.txt", "r")
r = f.readline()
f.close()
return int(r)
print ""
print 'Running KS2Android, please open readme.txt and follow the instructions'
print ""
try:
p = getprogress()
if p < PROGRESS.RAPTEXISTS:
downloadrapt()
extractrapt()
setprogress(PROGRESS.RAPTEXISTS)
if p < PROGRESS.ASDKEXISTS:
raptinstallsdk()
setprogress(PROGRESS.ASDKEXISTS)
if p < PROGRESS.UNRPAEXISTS:
getunrpa()
setprogress(PROGRESS.UNRPAEXISTS)
if p < PROGRESS.UNPACKED:
unpack()
setprogress(PROGRESS.UNPACKED)
if p < PROGRESS.VIDEOSDELETED:
deletevideos()
setprogress(PROGRESS.VIDEOSDELETED)
if p < PROGRESS.SCRIPTSCOPIED:
copyscripts()
setprogress(PROGRESS.SCRIPTSCOPIED)
if p < PROGRESS.DECEXISTS:
getdec()
setprogress(PROGRESS.DECEXISTS)
if p < PROGRESS.DECOMPILED:
decompile()
setprogress(PROGRESS.DECOMPILED)
if p < PROGRESS.PATCHED:
patch()
setprogress(PROGRESS.PATCHED)
if p < PROGRESS.RECOMPILED:
recompile()
setprogress(PROGRESS.RECOMPILED)
if p < PROGRESS.COPIEDTORAPT:
copytorapt()
setprogress(PROGRESS.COPIEDTORAPT)
if p < PROGRESS.CONFIGURED:
configure()
setprogress(PROGRESS.CONFIGURED)
if p < PROGRESS.BUILT:
build()
buildclean()
setprogress(PROGRESS.BUILT)
print ""
print "ALL WORK IS DONE!"
print ""
print "You should find your package under the name KatawaShoujo.apk"
print "Copy that file to your Android phone and install it."
print ""
except:
print traceback.format_exc()
print "An exception has occured, please copy the above information and contact the author"
raw_input("Press Enter to exit...")