BizHawkClient: Add command to get size of memory domain (#4439)

* Mega Man 2: Remove mm2 commands from client if rom size too small
This commit is contained in:
Bryce Wilson 2025-01-11 23:03:31 -08:00 committed by GitHub
parent 70942eda8c
commit 4edca0ce54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 2 deletions

View File

@ -121,6 +121,14 @@ Response:
Expected Response Type: `HASH_RESPONSE` Expected Response Type: `HASH_RESPONSE`
- `MEMORY_SIZE`
Returns the size in bytes of the specified memory domain.
Expected Response Type: `MEMORY_SIZE_RESPONSE`
Additional Fields:
- `domain` (`string`): The name of the memory domain to check
- `GUARD` - `GUARD`
Checks a section of memory against `expected_data`. If the bytes starting Checks a section of memory against `expected_data`. If the bytes starting
at `address` do not match `expected_data`, the response will have `value` at `address` do not match `expected_data`, the response will have `value`
@ -216,6 +224,12 @@ Response:
Additional Fields: Additional Fields:
- `value` (`string`): The returned hash - `value` (`string`): The returned hash
- `MEMORY_SIZE_RESPONSE`
Contains the size in bytes of the specified memory domain.
Additional Fields:
- `value` (`number`): The size of the domain in bytes
- `GUARD_RESPONSE` - `GUARD_RESPONSE`
The result of an attempted `GUARD` request. The result of an attempted `GUARD` request.
@ -376,6 +390,15 @@ request_handlers = {
return res return res
end, end,
["MEMORY_SIZE"] = function (req)
local res = {}
res["type"] = "MEMORY_SIZE_RESPONSE"
res["value"] = memory.getmemorydomainsize(req["domain"])
return res
end,
["GUARD"] = function (req) ["GUARD"] = function (req)
local res = {} local res = {}
local expected_data = base64.decode(req["expected_data"]) local expected_data = base64.decode(req["expected_data"])

View File

@ -151,7 +151,7 @@ async def ping(ctx: BizHawkContext) -> None:
async def get_hash(ctx: BizHawkContext) -> str: async def get_hash(ctx: BizHawkContext) -> str:
"""Gets the system name for the currently loaded ROM""" """Gets the hash value of the currently loaded ROM"""
res = (await send_requests(ctx, [{"type": "HASH"}]))[0] res = (await send_requests(ctx, [{"type": "HASH"}]))[0]
if res["type"] != "HASH_RESPONSE": if res["type"] != "HASH_RESPONSE":
@ -160,6 +160,16 @@ async def get_hash(ctx: BizHawkContext) -> str:
return res["value"] return res["value"]
async def get_memory_size(ctx: BizHawkContext, domain: str) -> int:
"""Gets the size in bytes of the specified memory domain"""
res = (await send_requests(ctx, [{"type": "MEMORY_SIZE", "domain": domain}]))[0]
if res["type"] != "MEMORY_SIZE_RESPONSE":
raise SyncError(f"Expected response of type MEMORY_SIZE_RESPONSE but got {res['type']}")
return res["value"]
async def get_system(ctx: BizHawkContext) -> str: async def get_system(ctx: BizHawkContext) -> str:
"""Gets the system name for the currently loaded ROM""" """Gets the system name for the currently loaded ROM"""
res = (await send_requests(ctx, [{"type": "SYSTEM"}]))[0] res = (await send_requests(ctx, [{"type": "SYSTEM"}]))[0]

View File

@ -214,10 +214,19 @@ class MegaMan2Client(BizHawkClient):
last_wily: Optional[int] = None # default to wily 1 last_wily: Optional[int] = None # default to wily 1
async def validate_rom(self, ctx: "BizHawkClientContext") -> bool: async def validate_rom(self, ctx: "BizHawkClientContext") -> bool:
from worlds._bizhawk import RequestFailedError, read from worlds._bizhawk import RequestFailedError, read, get_memory_size
from . import MM2World from . import MM2World
try: try:
if (await get_memory_size(ctx.bizhawk_ctx, "PRG ROM")) < 0x3FFB0:
if "pool" in ctx.command_processor.commands:
ctx.command_processor.commands.pop("pool")
if "request" in ctx.command_processor.commands:
ctx.command_processor.commands.pop("request")
if "autoheal" in ctx.command_processor.commands:
ctx.command_processor.commands.pop("autoheal")
return False
game_name, version = (await read(ctx.bizhawk_ctx, [(0x3FFB0, 21, "PRG ROM"), game_name, version = (await read(ctx.bizhawk_ctx, [(0x3FFB0, 21, "PRG ROM"),
(0x3FFC8, 3, "PRG ROM")])) (0x3FFC8, 3, "PRG ROM")]))
if game_name[:3] != b"MM2" or version != bytes(MM2World.world_version): if game_name[:3] != b"MM2" or version != bytes(MM2World.world_version):