Skip to content

Commit

Permalink
Merge pull request #591 from LmeSzinc/dev
Browse files Browse the repository at this point in the history
Bug fix
  • Loading branch information
LmeSzinc authored Jul 24, 2024
2 parents 11cefad + 19eeb54 commit 6ea848c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 36 deletions.
2 changes: 2 additions & 0 deletions module/device/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ def is_mumu_over_version_356(self) -> bool:
which has nemud.app_keep_alive and always be a vertical device
MuMu PRO on mac has the same feature
"""
if not self.is_mumu_family:
return False
if self.nemud_app_keep_alive != '':
return True
if IS_MACINTOSH:
Expand Down
42 changes: 22 additions & 20 deletions module/device/method/nemu_ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,26 +419,28 @@ def up(self):
if ret > 0:
raise NemuIpcError('nemu_input_event_touch_up failed')

@staticmethod
def serial_to_id(serial: str):
"""
Predict instance ID from serial
E.g.
"127.0.0.1:16384" -> 0
"127.0.0.1:16416" -> 1
Port from 16414 to 16418 -> 1
def serial_to_id(serial: str):
"""
Predict instance ID from serial
E.g.
"127.0.0.1:16384" -> 0
"127.0.0.1:16416" -> 1
Returns:
int: instance_id, or None if failed to predict
"""
try:
port = int(serial.split(':')[1])
except (IndexError, ValueError):
return None
index, offset = divmod(port - 16384, 32)
if 0 <= index < 32 and offset in [0, 1, 2]:
return index
else:
return None
Returns:
int: instance_id, or None if failed to predict
"""
try:
port = int(serial.split(':')[1])
except (IndexError, ValueError):
return None
index, offset = divmod(port - 16384 + 16, 32)
offset -= 16
if 0 <= index < 32 and offset in [-2, -1, 0, 1, 2]:
return index
else:
return None


class NemuIpc(Platform):
Expand All @@ -452,7 +454,7 @@ def nemu_ipc(self) -> NemuIpcImpl:
# Try existing settings first
if self.config.EmulatorInfo_path:
folder = os.path.abspath(os.path.join(self.config.EmulatorInfo_path, '../../'))
index = serial_to_id(self.serial)
index = NemuIpcImpl.serial_to_id(self.serial)
if index is not None:
try:
return NemuIpcImpl(
Expand Down
17 changes: 17 additions & 0 deletions module/device/platform/emulator_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ def MuMuPlayer12_id(self):

return None

@cached_property
def LDPlayer_id(self):
"""
Convert LDPlayer instance name to instance id.
Example names:
leidian0
leidian1
Returns:
int: Instance ID, or None if this is not a LDPlayer instance
"""
res = re.search(r'leidian(\d+)', self.name)
if res:
return int(res.group(1))

return None


class EmulatorBase:
# Values here must match those in argument.yaml EmulatorInfo.Emulator.option
Expand Down
24 changes: 24 additions & 0 deletions module/device/platform/emulator_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ def multi_to_single(exe):
else:
yield exe

@staticmethod
def single_to_console(exe: str):
"""
Convert a string that might be a single instance executable to its console.
Args:
exe (str): Path to emulator executable
Returns:
str: Path to emulator console
"""
if 'MuMuPlayer.exe' in exe:
return exe.replace('MuMuPlayer.exe', 'MuMuManager.exe')
elif 'LDPlayer.exe' in exe:
return exe.replace('LDPlayer.exe', 'ldconsole.exe')
elif 'dnplayer.exe' in exe:
return exe.replace('dnplayer.exe', 'ldconsole.exe')
elif 'Bluestacks.exe' in exe:
return exe.replace('Bluestacks.exe', 'bsconsole.exe')
elif 'MEmu.exe' in exe:
return exe.replace('MEmu.exe', 'memuc.exe')
else:
return exe

@staticmethod
def vbox_file_to_serial(file: str) -> str:
"""
Expand Down
59 changes: 43 additions & 16 deletions module/device/platform/platform_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _emulator_start(self, instance: EmulatorInstance):
"""
Start a emulator without error handling
"""
exe = instance.emulator.path
exe: str = instance.emulator.path
if instance == Emulator.MuMuPlayer:
# NemuPlayer.exe
self.execute(exe)
Expand All @@ -94,24 +94,29 @@ def _emulator_start(self, instance: EmulatorInstance):
if instance.MuMuPlayer12_id is None:
logger.warning(f'Cannot get MuMu instance index from name {instance.name}')
self.execute(f'"{exe}" -v {instance.MuMuPlayer12_id}')
elif instance == Emulator.LDPlayerFamily:
# ldconsole.exe launch --index 0
self.execute(f'"{Emulator.single_to_console(exe)}" launch --index {instance.LDPlayer_id}')
elif instance == Emulator.NoxPlayerFamily:
# Nox.exe -clone:Nox_1
self.execute(f'"{exe}" -clone:{instance.name}')
elif instance == Emulator.BlueStacks5:
# HD-Player.exe --instance Pie64
self.execute(f'"{exe}" --instance {instance.name}')
elif instance == Emulator.BlueStacks4:
# BlueStacks\Client\Bluestacks.exe -vmname Android_1
# Bluestacks.exe -vmname Android_1
self.execute(f'"{exe}" -vmname {instance.name}')
elif instance == Emulator.MEmuPlayer:
# MEmu.exe MEmu_0
self.execute(f'"{exe}" {instance.name}')
else:
raise EmulatorUnknown(f'Cannot start an unknown emulator instance: {instance}')

def _emulator_stop(self, instance: EmulatorInstance):
"""
Stop a emulator without error handling
"""
logger.hr('Emulator stop', level=2)
exe = instance.emulator.path
exe: str = instance.emulator.path
if instance == Emulator.MuMuPlayer:
# MuMu6 does not have multi instance, kill one means kill all
# Has 4 processes
Expand Down Expand Up @@ -141,26 +146,35 @@ def _emulator_stop(self, instance: EmulatorInstance):
rf')'
)
elif instance == Emulator.MuMuPlayer12:
# MuMu 12 has 2 processes:
# E:\ProgramFiles\Netease\MuMuPlayer-12.0\shell\MuMuPlayer.exe -v 0
# "C:\Program Files\MuMuVMMVbox\Hypervisor\MuMuVMMHeadless.exe" --comment MuMuPlayer-12.0-0 --startvm xxx
# MuMuManager.exe api -v 1 shutdown_player
if instance.MuMuPlayer12_id is None:
logger.warning(f'Cannot get MuMu instance index from name {instance.name}')
self.execute(f'"{Emulator.single_to_console(exe)}" api -v {instance.MuMuPlayer12_id} shutdown_player')
elif instance == Emulator.LDPlayerFamily:
# ldconsole.exe quit --index 0
self.execute(f'"{Emulator.single_to_console(exe)}" quit --index {instance.LDPlayer_id}')
elif instance == Emulator.NoxPlayerFamily:
# Nox.exe -clone:Nox_1 -quit
self.execute(f'"{exe}" -clone:{instance.name} -quit')
elif instance == Emulator.BlueStacks5:
# BlueStack has 2 processes
# C:\Program Files\BlueStacks_nxt_cn\HD-Player.exe --instance Pie64
# C:\Program Files\BlueStacks_nxt_cn\BstkSVC.exe -Embedding
self.kill_process_by_regex(
rf'('
rf'MuMuVMMHeadless.exe.*--comment {instance.name}'
rf'|MuMuPlayer.exe.*-v {instance.MuMuPlayer12_id}'
rf'HD-Player.exe.*"--instance" "{instance.name}"'
rf')'
)
# There is also a shared service, no need to kill it
# "C:\Program Files\MuMuVMMVbox\Hypervisor\MuMuVMMSVC.exe" --Embedding
elif instance == Emulator.NoxPlayerFamily:
# Nox.exe -clone:Nox_1 -quit
self.execute(f'"{exe}" -clone:{instance.name} -quit')
elif instance == Emulator.BlueStacks4:
# E:\Program Files (x86)\BluestacksCN\bsconsole.exe quit --name Android
self.execute(f'"{Emulator.single_to_console(exe)}" quit --name {instance.name}')
elif instance == Emulator.MEmuPlayer:
# F:\Program Files\Microvirt\MEmu\memuc.exe stop -n MEmu_0
self.execute(f'"{Emulator.single_to_console(exe)}" stop -n {instance.name}')
else:
raise EmulatorUnknown(f'Cannot stop an unknown emulator instance: {instance}')

def _emulator_function_wrapper(self, func):
def _emulator_function_wrapper(self, func: callable):
"""
Args:
func (callable): _emulator_start or _emulator_stop
Expand Down Expand Up @@ -312,7 +326,20 @@ def emulator_start(self):

def emulator_stop(self):
logger.hr('Emulator stop', level=1)
return self._emulator_function_wrapper(self._emulator_stop)
for _ in range(3):
# Stop
if self._emulator_function_wrapper(self._emulator_stop):
# Success
return True
else:
# Failed to stop, start and stop again
if self._emulator_function_wrapper(self._emulator_start):
continue
else:
return False

logger.error('Failed to stop emulator 3 times, stopped')
return False


if __name__ == '__main__':
Expand Down
5 changes: 5 additions & 0 deletions tasks/combat/fuel.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ def use_fuel(self, current, skip_first_screenshot=True):
Returns:
bool: If used
Pages:
in: COMBAT_AGAIN
out: COMBAT_AGAIN
"""
limit = self.config.stored.TrailblazePower.FIXED_TOTAL
use = (limit - current) // self.fuel_trailblaze_power
Expand All @@ -173,6 +177,7 @@ def use_fuel(self, current, skip_first_screenshot=True):
has_fuel = True
if not has_fuel and timeout.reached():
logger.info("No fuel found")
self._fuel_cancel()
return False
if self.appear_then_click(FUEL):
has_fuel = True
Expand Down

0 comments on commit 6ea848c

Please sign in to comment.