Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Script (outdated)

By: m2e on Jan 6th, 2013  |  syntax: Python  |  size: 9.75 KB  |  hits: 83  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #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)
  2. #readme.txt can be found at http://pastebin.com/S8S6GTmm
  3. #THIS SCRIPT ALONE WON'T DO YOU MUCH GOOD. READ THE README FILE
  4.  
  5. import urllib2
  6. import zipfile
  7. import os
  8. import subprocess
  9. import shutil
  10. import time
  11. import sys
  12. import traceback
  13. from ctypes import windll
  14.  
  15. rapt = 'rapt-6.14.1.0'
  16. raptz = 'rapt-6.14.1.0.zip'
  17.  
  18. class PROGRESS:
  19.     RAPTEXISTS = 1
  20.     ASDKEXISTS = 2
  21.     UNRPAEXISTS = 3
  22.     UNPACKED = 4
  23.     VIDEOSDELETED = 5
  24.     SCRIPTSCOPIED = 6
  25.     DECEXISTS = 7
  26.     DECOMPILED = 8
  27.     PATCHED = 9
  28.     RECOMPILED = 11
  29.     COPIEDTORAPT = 12
  30.     CONFIGURED = 13
  31.     BUILT = 14
  32.  
  33. FindWindow = windll.user32.FindWindowA
  34.  
  35. def delete(a):
  36.     try:
  37.         os.remove(a)
  38.     except:
  39.         pass
  40.  
  41. def getscript(url, filename):
  42.     u = urllib2.urlopen(url)
  43.     with open(filename, "wb") as f:
  44.         f.write(u.read())
  45.  
  46.  
  47. def download(url, file_name=""):
  48.     if file_name == "":
  49.         file_name = url.split('/')[-1]
  50.     u = urllib2.urlopen(url)
  51.     f = open(file_name, 'wb')
  52.     meta = u.info()
  53.     file_size = int(meta.getheaders("Content-Length")[0])
  54.  
  55.     file_size_dl = 0
  56.     block_sz = 8192
  57.     while True:
  58.         buf = u.read(block_sz)
  59.         if not buf:
  60.             break
  61.  
  62.         file_size_dl += len(buf)
  63.         f.write(buf)
  64.         status = r"    %3.2f%%" % (file_size_dl * 100. / file_size)
  65.         status = status + '\r'
  66.         print status,
  67.        
  68.     f.close()
  69.     print ""
  70.  
  71. def downloadrapt():
  72.     if os.path.exists(raptz):
  73.         print 'RAPT is already downloaded'
  74.     else:
  75.         print 'Downloading RAPT...'
  76.         download('http://www.renpy.org/dl/android/rapt-6.14.1.0.zip')
  77.            
  78. def extractrapt():
  79.     print 'Extracting...',
  80.     z = zipfile.ZipFile(raptz)
  81.     z.extractall()
  82.     z.close()
  83.     print 'Done'
  84.    
  85.     print 'Cleaning...',
  86.     delete(raptz)
  87.     print "Done"
  88.    
  89. def raptinstallsdk():
  90.     print "Installing Android SDK through RAPT..."
  91.     print "--------------------------------------------------------------------------------"
  92.     subprocess.call(["android.py", "installsdk"], shell=True, cwd=rapt)
  93.     print "--------------------------------------------------------------------------------"
  94.  
  95. def getunrpa():
  96.     print "Downloading UnRPA script...",
  97.     getscript("http://pastebin.com/raw.php?i=2j6pM5XM", "unrpa.py" )
  98.     print "Done"
  99.  
  100. def unpack():
  101.     print "Unpacking...",
  102.     subprocess.call(["unrpa.py", "-s", "-p", rapt + "\\ks\\game\\", "-m", "game\\data.rpa"], shell=True)
  103.     print "Done"
  104.    
  105.  
  106. def deletevideos():
  107.     print "Deleting videos...",
  108.     filenames = os.listdir( os.path.join(rapt, "ks", "game", "video") )
  109.     for filename in filenames:
  110.         if os.path.isfile( os.path.join(rapt, "ks", "game", "video", filename) ) and filename.endswith(".mkv"):
  111.             delete(os.path.join(rapt, "ks", "game", "video", filename))
  112.     print "Done"
  113.    
  114. def copyscripts():
  115.     print "Copying game scripts...",
  116.     filenames = os.listdir( "game" )
  117.     for filename in filenames:
  118.         if os.path.isfile( os.path.join("game", filename) ) and filename.endswith(".rpyc") and not filename.startswith("ui_settings"):
  119.             shutil.copy( os.path.join("game", filename), os.path.join(rapt, "ks", "game", filename) )
  120.     print "Done"
  121.  
  122. def getdec():
  123.     print "Downloading decompile script...",
  124.     getscript("http://pastebin.com/raw.php?i=1n8Apg1S", "decompile.rpy" )
  125.     print "Done"
  126.  
  127. def decompile():
  128.     shutil.copy( "decompile.rpy", os.path.join("game", "decompile.rpy"))
  129.     print "Running KS to trigger the decompile process...",
  130.     subprocess.Popen('"Katawa Shoujo.exe"', shell=True)
  131.  
  132.     while True:
  133.         time.sleep(1)
  134.         ret = FindWindow("pygame", "Katawa Shoujo")
  135.         if ret != 0:
  136.             subprocess.call(["taskkill", "/IM", "Katawa Shoujo.exe", "/F"], shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE)
  137.             print "Done"
  138.             break
  139.            
  140.     print "Cleaning up...",
  141.     delete("ui_settings.rpy")
  142.     filenames = os.listdir('.')
  143.     for filename in filenames:
  144.         if "ui_settings" in filename:
  145.             os.rename(filename, "ui_settings.rpy")
  146.         if not "ui_settings" in filename and filename.endswith(".rpy.txt"):
  147.             delete(filename)
  148.     print "Done"
  149.            
  150.     shutil.copy("ui_settings.rpy", os.path.join("game", "ui_settings.rpy"))
  151.    
  152.     delete(os.path.join("game", "decompile.rpy"))
  153.     delete(os.path.join("game", "decompile.rpyc"))
  154.  
  155. def patch():
  156.     print "Patching...",
  157.     aloadable = """\
  158.  
  159.        def aloadable(name):
  160.            f = None
  161.            try:
  162.                f = renpy.loader.load(name)
  163.            except:
  164.                return False
  165.            f.close()
  166.            return True
  167.    """
  168.     f = open("ui_settings.rpy", "r")
  169.     s = f.read()
  170.     f.close()
  171.    
  172.     patched = False
  173.     try:
  174.         s.index('aloadable')
  175.         print "The script is already patched"
  176.         patched = true
  177.     except:
  178.         pass
  179.    
  180.     if not patched:
  181.         i = s.index('    python:') + len('    python:')
  182.         s = s[:i] + aloadable + s[i:]
  183.         s = s.replace("renpy.loadable", "aloadable")
  184.        
  185.         f = open("ui_settings.rpy", "w")
  186.         f.write(s)
  187.         f.close()
  188.         print "Done"
  189.  
  190. def recompile():
  191.     shutil.copy( "ui_settings.rpy", os.path.join("game", "ui_settings.rpy"))
  192.     print "Running KS to recompile the patched script...",
  193.     subprocess.Popen('"Katawa Shoujo.exe"', shell=True)
  194.     while True:
  195.         time.sleep(1)
  196.         ret = FindWindow("pygame", "Katawa Shoujo")
  197.         if ret != 0:
  198.             subprocess.call(["taskkill", "/IM", "Katawa Shoujo.exe", "/F"], shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE)
  199.             print "Done"
  200.             break
  201.  
  202. def copytorapt():
  203.     print "Copying patched scripts...",
  204.     shutil.copy(os.path.join("game", "ui_settings.rpyc"), os.path.join(rapt, "ks", "game", "ui_settings.rpyc"))
  205.     shutil.copy(os.path.join("game", "bytecode.rpyb"), os.path.join(rapt, "ks", "game", "bytecode.rpyb"))
  206.     print "Done"
  207.    
  208. def configure():
  209.     print "Configuring the package...",
  210.     f = open( os.path.join(rapt, "ks", ".android.json"), "w")
  211.     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"}')
  212.     f.close()
  213.     shutil.copy( os.path.join(rapt, "ks", "game", "ui", "bt-logoonly.png"), os.path.join(rapt, "ks", "android-icon.png") )
  214.     shutil.copy( os.path.join("game", "presplash.png"), os.path.join(rapt, "ks", "android-presplash.jpg") )
  215.     print "Done"
  216.    
  217. def build():
  218.     print "Building, transferring control to RAPT"
  219.     print "--------------------------------------------------------------------------------"
  220.     subprocess.call(["android.py", "build", "ks", "release"], shell=True, cwd=rapt)
  221.     print "--------------------------------------------------------------------------------"
  222.     print "Copying Katawa Shoujo package...",
  223.     shutil.copy( os.path.join(rapt, "bin", "KatawaShoujo-1-release.apk"), "KatawaShoujo.apk")
  224.     print "Done"
  225.    
  226. def buildclean():
  227.     print "Cleaning...",
  228.     filenames = os.listdir( os.path.join(rapt, "bin"))
  229.     for filename in filenames:
  230.         if os.path.isfile( os.path.join(rapt, "bin", filename ) ) and "KatawaShoujo" in filename:
  231.             delete( os.path.join(rapt, "bin", filename ) )
  232.     print "Done"
  233.    
  234. def setprogress(a):
  235.     f = open("progress.txt", "w")
  236.     f.write(str(a))
  237.     f.close()
  238.    
  239. def getprogress():
  240.     if not os.path.exists("progress.txt"):
  241.         return 0
  242.     f = open("progress.txt", "r")
  243.     r = f.readline()
  244.     f.close()
  245.     return int(r)
  246.    
  247. print ""
  248. print 'Running KS2Android, please open readme.txt and follow the instructions'
  249. print ""
  250.  
  251. try:
  252.     p = getprogress()
  253.     if p < PROGRESS.RAPTEXISTS:
  254.         downloadrapt()
  255.         extractrapt()
  256.         setprogress(PROGRESS.RAPTEXISTS)
  257.  
  258.     if p < PROGRESS.ASDKEXISTS:
  259.         raptinstallsdk()
  260.         setprogress(PROGRESS.ASDKEXISTS)
  261.  
  262.     if p < PROGRESS.UNRPAEXISTS:
  263.         getunrpa()
  264.         setprogress(PROGRESS.UNRPAEXISTS)
  265.        
  266.     if p < PROGRESS.UNPACKED:
  267.         unpack()
  268.         setprogress(PROGRESS.UNPACKED)
  269.  
  270.     if p < PROGRESS.VIDEOSDELETED:
  271.         deletevideos()
  272.         setprogress(PROGRESS.VIDEOSDELETED)
  273.    
  274.     if p < PROGRESS.SCRIPTSCOPIED:
  275.         copyscripts()
  276.         setprogress(PROGRESS.SCRIPTSCOPIED)
  277.  
  278.     if p < PROGRESS.DECEXISTS:
  279.         getdec()    
  280.         setprogress(PROGRESS.DECEXISTS)
  281.        
  282.     if p < PROGRESS.DECOMPILED:
  283.         decompile()
  284.         setprogress(PROGRESS.DECOMPILED)
  285.  
  286.     if p < PROGRESS.PATCHED:
  287.         patch()
  288.         setprogress(PROGRESS.PATCHED)
  289.        
  290.     if p < PROGRESS.RECOMPILED:
  291.         recompile()
  292.         setprogress(PROGRESS.RECOMPILED)
  293.        
  294.     if p < PROGRESS.COPIEDTORAPT:
  295.         copytorapt()
  296.         setprogress(PROGRESS.COPIEDTORAPT)
  297.  
  298.     if p < PROGRESS.CONFIGURED:
  299.         configure()
  300.         setprogress(PROGRESS.CONFIGURED)
  301.    
  302.     if p < PROGRESS.BUILT:
  303.         build()
  304.         buildclean()
  305.         setprogress(PROGRESS.BUILT)
  306.  
  307.     print ""
  308.     print "ALL WORK IS DONE!"
  309.     print ""
  310.     print "You should find your package under the name KatawaShoujo.apk"
  311.     print "Copy that file to your Android phone and install it."
  312.     print ""
  313.  
  314. except:
  315.     print traceback.format_exc()
  316.     print "An exception has occured, please copy the above information and contact the author"
  317.  
  318. raw_input("Press Enter to exit...")