-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
127 lines (107 loc) · 4.25 KB
/
sync.py
File metadata and controls
127 lines (107 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os.path
from pathlib import Path
import platform
from dataclasses import dataclass
import subprocess
# path const
WINDOWS_WORLDS = os.path.expanduser("~\\AppData\\Local\\Packages\\Microsoft.MinecraftUWP_8wekyb3d8bbwe\\"
"LocalState\\games\\com.mojang\\minecraftWorlds")
ANDROID_WORLDS = "/sdcard/Android/data/com.mojang.minecraftpe/files/games/com.mojang/minecraftWorlds"
@dataclass
class World:
dir_name: str
world_name: str
def __eq__(self, other):
return self.dir_name == other.dir_name and self.world_name == other.world_name
def input_int(prompt: str, min_: int, max_: int, default: None | int=None) -> int:
while True:
try:
input_str = input(prompt)
if input_str == "" and default is not None:
return default
value = int(input_str)
if min_ <= value <= max_:
return value
else:
print(f"Please input an integer between {min_} and {max_}.")
except ValueError:
print(f"Please input an integer between {min_} and {max_}.")
def list_windows():
worlds_dir = Path(WINDOWS_WORLDS)
worlds: list[World] = []
for world_dir in worlds_dir.iterdir():
with open(world_dir / "levelname.txt", encoding="utf-8") as f:
world_name = f.read().strip()
worlds.append(World(world_dir.name, world_name))
return worlds
def list_android():
worlds: list[World] = []
subfolders = subprocess.run(
["adb", "shell", "ls", "-d", f"{ANDROID_WORLDS}/*/"],
capture_output=True,
text=True,
).stdout.strip().splitlines()
for subfolder in subfolders:
dir_name = subfolder.rstrip("/").split("/")[-1]
world_name = subprocess.run(
["adb", "shell", "cat", f"{subfolder}levelname.txt"],
capture_output=True,
text=True,
encoding="utf-8",
).stdout.strip()
worlds.append(World(dir_name, world_name))
return worlds
def print_worlds(worlds: list[World]):
print(f"{'No.':<5}{'World Name':<20}{'Folder Name':<10}")
for index, world in enumerate(worlds):
print(f"{index+1:<6}{world.world_name:<20}{world.dir_name:<10}")
def handle_windows2android():
windows_worlds = list_windows()
print_worlds(windows_worlds)
index = input_int("Please select a world in Windows, 0 to exit: ", 0, len(windows_worlds))
if index == 0:
return
world = windows_worlds[index-1]
android_worlds = list_android()
if world in android_worlds:
replace = input_int("A same world found in Android, replace it? (0/[1]): ", 0, 1, 1)
if replace == 0:
return
android_world_dir = ANDROID_WORLDS + "/" + world.dir_name
print("Deleting world in Android...")
subprocess.run(["adb", "shell", "rm", "-rf", android_world_dir])
print("Copying world to Android...")
windows_world_dir = WINDOWS_WORLDS + "\\" + world.dir_name
subprocess.run(["adb", "push", windows_world_dir, ANDROID_WORLDS])
def handle_android2windows():
android_worlds = list_android()
print_worlds(android_worlds)
index = input_int("Please select a world in Android, 0 to exit: ", 0, len(android_worlds))
if index == 0:
return
world = android_worlds[index-1]
windows_worlds = list_windows()
if world in windows_worlds:
replace = input_int("A same world found in Windows, replace it? (0/[1]): ", 0, 1, 1)
if replace == 0:
return
windows_world_dir = WINDOWS_WORLDS + "\\" + world.dir_name
print("Deleting world in Windows...")
subprocess.run(f'rmdir /s /q "{windows_world_dir}"', shell=True)
print("Copying world to Windows...")
android_world_dir = ANDROID_WORLDS + "/" + world.dir_name
subprocess.run(["adb", "pull", android_world_dir, WINDOWS_WORLDS])
def main():
choice = input_int("Select sync mode: 1) Windows -> Android 2) Android -> Windows : ", 1, 2)
match choice:
case 1:
handle_windows2android()
case 2:
handle_android2windows()
case _:
print("Invalid choice.")
if __name__ == "__main__":
if not platform.system() == "Windows":
print("The program only works on Windows.")
exit(1)
main()