update flycast patch script to support multiple instances
This commit is contained in:
+35
-24
@@ -1,39 +1,50 @@
|
|||||||
#!/bin/env python3
|
#!/bin/env python3
|
||||||
|
|
||||||
|
import argparse
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def get_ip_address(ifname):
|
def get_ip_address(ifname):
|
||||||
data = subprocess.check_output(['ifconfig', ifname])
|
data = subprocess.check_output(["ifconfig", ifname])
|
||||||
for line in data.splitlines():
|
for line in data.splitlines():
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if line.startswith(b'inet '):
|
if line.startswith(b"inet "):
|
||||||
return line.split()[1].decode('ascii')
|
return line.split()[1].decode("ascii")
|
||||||
raise RuntimeError('cannot get address for interface ' + ifname)
|
raise RuntimeError("cannot get address for interface " + ifname)
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
if len(argv) < 2:
|
parser = argparse.ArgumentParser()
|
||||||
raise RuntimeError(f'Usage: {argv[0]} <original-destination> [new-destination]')
|
parser.add_argument("--pid", "-p", type=int, default=0)
|
||||||
if os.geteuid() != 0:
|
parser.add_argument("orig_destination", type=str)
|
||||||
raise RuntimeError('You must use sudo to run this script')
|
parser.add_argument("new_destination", type=str, default=None, nargs="?")
|
||||||
original_destination = argv[1]
|
args = parser.parse_args()
|
||||||
new_destination = argv[2] if len(argv) > 2 else get_ip_address('en0')
|
|
||||||
|
|
||||||
print(f'Finding occurrences of \"{original_destination}\"')
|
if os.geteuid() != 0:
|
||||||
addresses_str = subprocess.check_output(['memwatch', 'Flycast.app', 'find', f'\"{original_destination}\"'])
|
raise RuntimeError("You must use sudo to run this script")
|
||||||
for line in addresses_str.splitlines():
|
new_destination = args.new_destination or get_ip_address("en0")
|
||||||
# line is like '(0) 00007FFF038500A0 (rw-)' (we care only about the address)
|
proc_arg = "Flycast.app" if args.pid == 0 else str(args.pid)
|
||||||
tokens = line.split()
|
|
||||||
if len(tokens) != 3:
|
|
||||||
continue
|
|
||||||
print(f'Replacing \"{original_destination}\" with \"{new_destination}\" at {tokens[1]} in Flycast')
|
|
||||||
subprocess.check_call(['memwatch', 'Flycast.app', 'write', tokens[1], f'\"{new_destination}\" 00'])
|
|
||||||
|
|
||||||
return 0
|
print(f'Finding occurrences of "{args.orig_destination}"')
|
||||||
|
addresses_str = subprocess.check_output(
|
||||||
|
["memwatch", proc_arg, "find", f'"{args.orig_destination}"']
|
||||||
|
)
|
||||||
|
for line in addresses_str.splitlines():
|
||||||
|
# line is like '(0) 00007FFF038500A0 (rw-)' (we care only about the address)
|
||||||
|
tokens = line.split()
|
||||||
|
if len(tokens) != 3:
|
||||||
|
continue
|
||||||
|
print(
|
||||||
|
f'Replacing "{args.orig_destination}" with "{new_destination}" at {tokens[1]} in Flycast'
|
||||||
|
)
|
||||||
|
subprocess.check_call(
|
||||||
|
["memwatch", proc_arg, "write", tokens[1], f'"{new_destination}" 00']
|
||||||
|
)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
sys.exit(main(sys.argv))
|
sys.exit(main(sys.argv))
|
||||||
|
|||||||
Reference in New Issue
Block a user