Horizon Zero Dawn Harness pre-alpha

This commit is contained in:
J-Doiron
2024-12-23 11:56:19 -08:00
parent 959beb1ca5
commit 7ae066b33c
4 changed files with 330 additions and 0 deletions

21
horizonzdr/README.md Normal file
View File

@@ -0,0 +1,21 @@
# Horizon Zero Dawn Remastered
Navigates menus to the in-game benchmark then runs it.
## Prerequisites
- Python 3.10+
- Horizon Zero Dawn Remastered installed
- Keras OCR service
## Options
- `kerasHost`: string representing the IP address of the Keras service. e.x. `0.0.0.0`
- `kerasPort`: string representing the port of the Keras service. e.x. `8080`
## Output
report.json
- `resolution`: string representing the resolution the test was run at, formatted as "[width]x[height]", e.x. `1920x1080`
- `start_time`: number representing a timestamp of the test's start time in milliseconds
- `end_time`: number representing a timestamp of the test's end time in milliseconds

207
horizonzdr/hzdr.py Normal file
View File

@@ -0,0 +1,207 @@
"""Returnal test script"""
import os
import logging
import sys
import time
import pydirectinput as user
from hzdr_utils import get_resolution, get_args
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from harness_utils.keras_service import KerasService
from harness_utils.output import (
format_resolution,
seconds_to_milliseconds,
setup_log_directory,
write_report_json,
DEFAULT_LOGGING_FORMAT,
DEFAULT_DATE_FORMAT,
)
from harness_utils.misc import remove_files, press_n_times
from harness_utils.process import terminate_processes
from harness_utils.steam import (
exec_steam_run_command,
get_steamapps_common_path,
get_build_id
)
from harness_utils.artifacts import ArtifactManager, ArtifactType
STEAM_GAME_ID = 2561580
SCRIPT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
LOG_DIRECTORY = os.path.join(SCRIPT_DIRECTORY, "run")
PROCESS_NAME = "HorizonZeroDawnRemastered"
LOCAL_USER_SETTINGS = os.path.join(
os.getenv('LOCALAPPDATA'), "Returnal", "Steam",
"Saved", "Config", "WindowsNoEditor", "GameUserSettings.ini"
)
VIDEO_PATH = os.path.join(get_steamapps_common_path(), "Horizon Zero Dawn Remastered", "Movies", "Mono")
user.FAILSAFE = False
intro_videos = [
os.path.join(VIDEO_PATH, "weaseltron_logo.bk2"),
os.path.join(VIDEO_PATH, "sony_studios_reel.bk2"),
os.path.join(VIDEO_PATH, "nixxes_logo.bk2"),
os.path.join(VIDEO_PATH, "Logo.bk2"),
os.path.join(VIDEO_PATH, "guerilla_logo.bk2")
]
def navigate_options_menu() -> None:
"""Simulate inputs to navigate to options menu"""
logging.info("Navigating to options menu")
user.press("esc")
time.sleep(0.2)
user.press("enter")
time.sleep(0.2)
user.press("q")
time.sleep(0.2)
user.keyDown("tab")
time.sleep(5)
user.keyUp("tab")
def run_benchmark() -> tuple[float]:
"""Run the benchmark"""
logging.info("Removing intro videos")
remove_files(intro_videos)
logging.info("Starting game")
exec_steam_run_command(STEAM_GAME_ID)
setup_start_time = time.time()
am = ArtifactManager(LOG_DIRECTORY)
time.sleep(10)
# Make sure the game started correctly
result = kerasService.look_for_word("locate", 10, 5)
if not result:
logging.info("Could not find prompt to open menu!")
sys.exit(1)
# Navigate to display menu
user.press("esc")
time.sleep(1)
user.press("enter")
time.sleep(1)
user.press("q")
time.sleep(1)
user.press("q")
time.sleep(1)
# Verify that we have navigated to the video settings menu and take a screenshot
if kerasService.wait_for_word(word="aspect", timeout=30, interval=1) is None:
logging.info("Did not find the video settings menu. Did the menu get stuck?")
sys.exit(1)
am.take_screenshot("video.png", ArtifactType.CONFIG_IMAGE, "picture of video settings")
# Navigate to graphics menu
user.press("e")
time.sleep(1)
if kerasService.wait_for_word(word="vsync", timeout=30, interval=1) is None:
logging.info("Did not find the graphics settings menu. Did the menu get stuck?")
sys.exit(1)
am.take_screenshot("graphics_1.png", ArtifactType.CONFIG_IMAGE, "first picture of graphics settings")
# We check for a keyword that indicates DLSS is active because this changes how we navigate the menu
if kerasService.wait_for_word(word="sharpness", timeout=10, interval=1) is None:
logging.info("No DLSS Settings Detected")
# Scroll down graphics menu
press_n_times("down", 15, 0.2)
else:
logging.info("DLSS Settings Detected")
# Scroll down graphics menu
press_n_times("down", 17, 0.2)
if kerasService.wait_for_word(word="volumetric", timeout=30, interval=1) is None:
logging.info("Did not find the keyword 'volumetric'. Did the the menu scroll correctly?")
sys.exit(1)
am.take_screenshot("graphics_2.png", ArtifactType.CONFIG_IMAGE, "second picture of graphics settings")
# Scroll down graphics menu
press_n_times("down", 15, 0.2)
if kerasService.wait_for_word(word="hdr", timeout=30, interval=1) is None:
logging.info("Did not find the keyword 'hdr'. Did the the menu scroll correctly?")
sys.exit(1)
am.take_screenshot("graphics_3.png", ArtifactType.CONFIG_IMAGE, "third picture of graphics settings")
# Launch the benchmark
user.keyDown("tab")
time.sleep(5)
user.keyUp("tab")
setup_end_time = time.time()
elapsed_setup_time = round((setup_end_time - setup_start_time), 2)
logging.info("Setup took %s seconds", elapsed_setup_time)
result = kerasService.wait_for_word("performance", interval=0.2, timeout=30)
if not result:
logging.info(
"Performance graph was not found! Could not mark the start time.")
sys.exit(1)
test_start_time = time.time()
# Wait for benchmark to complete
time.sleep(112)
# Wait for results screen to display info
result = kerasService.wait_for_word("lost", interval=0.1, timeout=11)
if not result:
logging.info(
"Didn't see signal lost. Could not mark the proper end time!")
test_end_time = round(time.time() - 2)
result = kerasService.wait_for_word("benchmark", interval=0.5, timeout=15)
if not result:
logging.info(
"Results screen was not found! Did harness not wait long enough? Or test was too long?")
sys.exit(1)
# Give results screen time to fill out, then save screenshot and config file
time.sleep(2)
am.take_screenshot("result.png", ArtifactType.RESULTS_IMAGE, "screenshot of benchmark result")
am.copy_file(LOCAL_USER_SETTINGS, ArtifactType.CONFIG_TEXT, "config file")
elapsed_test_time = round((test_end_time - test_start_time), 2)
logging.info("Benchmark took %s seconds", elapsed_test_time)
terminate_processes(PROCESS_NAME)
am.create_manifest()
return test_start_time, test_end_time
setup_log_directory(LOG_DIRECTORY)
logging.basicConfig(filename=f'{LOG_DIRECTORY}/harness.log',
format=DEFAULT_LOGGING_FORMAT,
datefmt=DEFAULT_DATE_FORMAT,
level=logging.DEBUG)
console = logging.StreamHandler()
formatter = logging.Formatter(DEFAULT_LOGGING_FORMAT)
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
args = get_args()
kerasService = KerasService(args.keras_host, args.keras_port)
try:
start_time, end_time = run_benchmark()
height, width = get_resolution(LOCAL_USER_SETTINGS)
report = {
"resolution": format_resolution(width, height),
"start_time": seconds_to_milliseconds(start_time),
"end_time": seconds_to_milliseconds(end_time),
"version": get_build_id(STEAM_GAME_ID)
}
write_report_json(LOG_DIRECTORY, "report.json", report)
except Exception as e:
logging.error("Something went wrong running the benchmark!")
logging.exception(e)
terminate_processes(PROCESS_NAME)
sys.exit(1)

93
horizonzdr/hzdr_utils.py Normal file
View File

@@ -0,0 +1,93 @@
"""Utility functions supporting Returnal test script."""
from argparse import ArgumentParser
import re
import winreg
input_file = 'input.reg'
config_file = 'config_registry.txt'
hive = winreg.KEY_CURRENT_USER
subkey = r"SOFTWARE\Guerilla Games\Horizon Zero Dawn Remastered\Graphics"
def export_registry_key(hive, subkey, input_file):
try:
with winreg.OpenKey(hive,subkey) as reg_key:
with open(input_file, 'w') as reg_file:
reg_file.write(f"Windows Registry Editor Version 5.00\n\n")
reg_file.write(f"[{subkey}]\n")
try:
index = 0
while True:
value_name, value_data, value_type = winreg.EnumValue(reg_key, index)
if value_type == winreg.REG_DWORD:
value_data = f"dword:{value_data:08x}"
elif value_type == winreg.REG_SZ:
value_data = f'"{value_data}"'
elif value_type == winreg.REG_QWORD:
value_data = f"qword:{value_data:0x16x}"
else:
value_data = f'"{value_data}"'
reg_file.write(f'"{value_name}"={value_data}\n')
index += 1
except OSError:
pass
except WindowsError as e:
print(f"Failed to open the registry key: {e}")
def convert_dword_to_decimal(dword_hex):
return int(dword_hex, 16)
def process_registry_file(input_file, config_file):
export_registry_key()
with open(input_file, 'r') as file:
lines = file.readlines()
modified_lines = []
dword_pattern = re.compile(r'^(\"[^\"]+\")=dword:([0-9a-fA-F]+)', re.IGNORECASE)
for line in lines:
match = dword_pattern.search(line)
if match:
key = match.group(1)
hex_value = match.group(2)
decimal_value = convert_dword_to_decimal(hex_value)
modified_line = f'{key}={decimal_value}\n'
modified_lines.append(modified_line)
else:
modified_lines.append(line)
with open(config_file, 'w') as file:
file.writelines(modified_lines)
def get_resolution(config_file: str) -> tuple[int]:
"""Retrieve the resolution from local configuration files."""
width_pattern = re.compile(r"ResolutionSizeX=(\d+)")
height_pattern = re.compile(r"ResolutionSizeY=(\d+)")
width = 0
height = 0
with open(config_file, encoding="utf-8") as file:
lines = file.readlines()
for line in lines:
width_match = width_pattern.match(line)
height_match = height_pattern.match(line)
if width_match:
width = width_match.group(1)
if height_match:
height = height_match.group(1)
return (height, width)
def get_args() -> any:
"""Get command line arguments"""
parser = ArgumentParser()
parser.add_argument(
"--kerasHost", dest="keras_host", help="Host for Keras OCR service", required=True)
parser.add_argument(
"--kerasPort", dest="keras_port", help="Port for Keras OCR service", required=True)
return parser.parse_args()

9
horizonzdr/manifest.yaml Normal file
View File

@@ -0,0 +1,9 @@
friendly_name: "Horizon Zero Dawn Remastered"
executable: "hzdr.py"
process_name: "HorizonZeroDawnRemastered.exe"
output_dir: "run"
options:
- name: kerasHost
type: input
- name: kerasPort
type: input