Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy Junk Jack saves and player data between PC and Phone editions.
# Author: Tyler Nijmeh <tylernij@gmail.com>.
import os
import tarfile
import shutil
import subprocess
#TODO: Make paths constant
appdata = os.getenv('APPDATA')
WIN_JJ = appdata + '/Junk Jack'
WIN_JJ_PLAYERS = WIN_JJ + '/players'
WIN_JJ_WORLDS = WIN_JJ + '/worlds'
ANDROID_JJ_ROOT = '/data/data/com.pixbits.junkjack'
ANDROID_JJ = ANDROID_JJ_ROOT + '/files'
ANDROID_JJ_PLAYERS = ANDROID_JJ + '/players'
ANDROID_JJ_WORLDS = ANDROID_JJ + '/worlds'
# Execute phone command via adb
def adbShell(command):
subprocess.call('adb shell "' + command + '"')
def adbPush(a, b):
subprocess.call('adb push ' + a + ' ' + b)
def adbPull(a):
subprocess.call('adb pull ' + a)
# Remove phone and PC backups
def rmJJTar():
# Remove from local
try:
os.remove('junkjack.tar')
except:
pass
# Remove from phone
try:
adbShell('rm -rf /sdcard/junkjack.tar')
except:
pass
# Transfer local player and world folders to the PC game data
def updateJJPCFiles():
try:
shutil.rmtree(WIN_JJ_PLAYERS)
shutil.rmtree(WIN_JJ_WORLDS)
except:
pass
shutil.copytree('players', WIN_JJ_PLAYERS)
shutil.copytree('worlds', WIN_JJ_WORLDS)
# Transfer local player and world folders to the phone game data
def updateJJPhoneFiles():
adbPush('junkjack.tar', '/sdcard/')
adbShell('su -c rm -rf ' + ANDROID_JJ_PLAYERS + ' ' + ANDROID_JJ_WORLDS)
adbShell('su -c tar xf /sdcard/junkjack.tar -C ' + ANDROID_JJ)
# Update user permissions for these files
uid = subprocess.check_output("adb shell su -c \"ls -la /data/data/com.pixbits.junkjack | grep files | awk '{ print $3 }\"'")
uid = uid.decode('utf-8').strip()
adbShell('su -c chown -R ' + uid + ':' + uid + ' ' + ANDROID_JJ)
# Extract game data from tar
def extractTar():
tar = tarfile.open('junkjack.tar')
tar.extractall()
tar.close()
# Tar up PC game data
def createPCTar():
currentDir = os.getcwd()
os.chdir(WIN_JJ)
tar = tarfile.open(currentDir + '/junkjack.tar', 'w')
for name in ['players', 'worlds']:
tar.add(name)
tar.close()
os.chdir(currentDir)
# Tar up phone game data
def createPhoneTar():
adbShell('su -c tar cvf /sdcard/junkjack.tar -C ' + ANDROID_JJ + ' .')
adbPull('/sdcard/junkjack.tar')
# Transfer from Phone --> PC
def phoneToPC():
rmJJTar()
createPhoneTar()
extractTar()
updateJJPCFiles()
rmJJTar()
# Transfer from PC --> Phone
def PCToPhone():
rmJJTar()
createPCTar()
updateJJPhoneFiles()
rmJJTar()
# Main method for selecting a mode
def main():
# Welcome message
print('Junk Jack World Transfer')
print('~ Tyler Nijmeh <tylernij@gmail.com>.')
print('NOTE: ANDROID MUST BE ROOTED')
print('NOTE: BACKUP YOUR GAME JUST IN CASE')
print()
print('Which device should receive the new data?')
print('Options: pc, phone')
print()
mode = input('Mode: ')
if (mode == 'pc'):
phoneToPC()
elif (mode == 'phone'):
PCToPhone()
else:
print('Invalid mode. Choose a valid option.')
main()
# Success
print('Done! Restart the game.')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.