RLP's Computing Blog

Syncing Game Data Between Android Phones

Article Info

Recent Changes

About


The script below actually syncs between a plugged in Android phone and BlueStacks, an Android emulator used for gaming, but it could just as easily do two different phones.

The script is very specific to a specific game, I’m just posting it because other people might find it useful, especially in the era where you can’t touch any of these files on the phone itself so you can just use DropSync or whatever.

The script is intended to be run on Windows using Git Bash (https://about.gitlab.com/blog/git-command-line-on-windows-with-git-bash/).

#!/bin/bash

# Error trapping from https://gist.github.com/oldratlee/902ad9a398affca37bfcfab64612e7d1
__error_trapper() {
  local parent_lineno="$1"
  local code="$2"
  local commands="$3"
  echo "error exit status $code, at file $0 on or near line $parent_lineno: $commands"
}
trap '__error_trapper "${LINENO}/${BASH_LINENO}" "$?" "$BASH_COMMAND"' ERR

set -euE -o pipefail
shopt -s failglob

export MSYS_NO_PATHCONV=1
phone_id=13[REPLACE]27R
desktop_id=localhost:5555

cd ~/[REPLACE]/TaleOfImmortalAndroid/

date=$(date +%Y-%m-%d)
phone_dir="Phone/$date/"
mkdir -p "$phone_dir"
desktop_dir="Desktop/$date/"
mkdir -p "$desktop_dir"

# Get both versions
adb -s "$phone_id" pull -a -Z /storage/self/primary/Android/data/com.guigugame.guigubahuangoverseas/files/Android "$phone_dir"
adb connect localhost:5555
adb -s "$desktop_id" pull -a -Z /storage/self/primary/Android/data/com.guigugame.guigubahuangoverseas/files/Android "$desktop_dir"

# Check for recency
set +o pipefail
find "$phone_dir" "$desktop_dir" -type f -printf '%T@ %t %p\n' | sort -k1,1nr | head
recentest="$(find "$phone_dir" "$desktop_dir" -type f -printf '%T@ %p\n' | sort -k1,1nr | head -n 1 | sed -r -e 's/\S* //' -e 's;/.*;;')"
set -o pipefail

if [[ "$recentest" == "Desktop" ]]
then
  source_dir="$desktop_dir/Android/"
  target_id="$phone_id"
  target_name="phone"
elif [[ "$recentest" == "Phone" ]]
then
  source_dir="$phone_dir/Android/"
  target_id="$desktop_id"
  target_name="desktop"
else
  echo "Bad device."
  exit
fi

echo "Most recent seems to be $recentest, enter to continue copy $source_dir to $target_id ($target_name)"
read line

# Push the data to a temp place because adb can't push to anywhere useful
adb -s "$target_id" push "$source_dir" /data/local/tmp/

# Delete the settings file so each side can have their own
adb -s "$target_id" shell rm /data/local/tmp/Android/CacheData/DataGloble.cache

# Show the situation
adb -s "$target_id" shell ls -l '/data/local/tmp/Android/CacheData/*'

# Copy the files to the real place
adb -s "$target_id" shell cp -pr /data/local/tmp/Android/ /storage/self/primary/Android/data/com.guigugame.guigubahuangoverseas/files/