pub struct Game {
pub layout: MemoryLayoutImpl<DllGameMemory>,
pub memory: Arc<DllGameMemory>,
pub base_slot: <DllGameMemory as GameMemory>::Slot,
/* private fields */
}
Expand description
An SM64 API that uses a traditional frame advance / save state model.
See crate level docs for more details on which API to use.
§Example
use wafel_api::Game;
let mut game = unsafe { Game::new("libsm64/sm64_us.dll") };
let power_on = game.save_state();
game.advance_n(1500);
assert_eq!(game.read("gCurrLevelNum"), game.constant("LEVEL_BOWSER_1"));
game.load_state(&power_on);
assert_eq!(game.frame(), 0);
for frame in 0..1000 {
if frame % 2 == 1 {
game.write("gControllerPads[0].button", game.constant("START_BUTTON"));
}
game.advance();
}
game.advance_n(500);
assert_eq!(
game.read("gCurrLevelNum"),
game.constant("LEVEL_CASTLE_GROUNDS")
);
Fields§
§layout: MemoryLayoutImpl<DllGameMemory>
§memory: Arc<DllGameMemory>
§base_slot: <DllGameMemory as GameMemory>::Slot
Implementations§
Source§impl Game
impl Game
Sourcepub unsafe fn new(dll_path: &str) -> Self
pub unsafe fn new(dll_path: &str) -> Self
Load a libsm64 DLL.
§Panics
Panics if the DLL fails to open, probably because the file doesn’t exist or the DLL isn’t a proper libsm64 DLL.
§Safety
This method is inherently unsafe:
- If the DLL image is modified (either on disk before load or in memory) from anywhere except this Game, this is UB.
- The DLL can easily execute arbitrary code.
Sourcepub unsafe fn try_new(dll_path: &str) -> Result<Self, Error>
pub unsafe fn try_new(dll_path: &str) -> Result<Self, Error>
Load a libsm64 DLL.
Returns an error if the DLL fails to open, probably because the file doesn’t exist or the DLL isn’t a proper libsm64 DLL.
§Safety
This method is inherently unsafe:
- If the DLL image is modified (either on disk before load or in memory) from anywhere except this Game, this is UB.
- The DLL can easily execute arbitrary code.
Sourcepub fn base_address(&self) -> usize
pub fn base_address(&self) -> usize
Sourcepub fn read(&self, path: &str) -> Value
pub fn read(&self, path: &str) -> Value
Read a value from memory.
See the crate documentation for the path syntax.
§Panics
Panics if the path fails to compile or reading from memory fails.
Sourcepub fn try_read(&self, path: &str) -> Result<Value, Error>
pub fn try_read(&self, path: &str) -> Result<Value, Error>
Read a value from memory.
See the crate documentation for the path syntax.
Returns an error if the path fails to compile or reading from memory fails.
Sourcepub fn read_string_at(&self, address: Address) -> Vec<u8>
pub fn read_string_at(&self, address: Address) -> Vec<u8>
Read a null terminated string from memory at the given address.
§Panics
Panics if reading from memory fails.
Sourcepub fn try_read_string_at(&self, address: Address) -> Result<Vec<u8>, Error>
pub fn try_read_string_at(&self, address: Address) -> Result<Vec<u8>, Error>
Read a null terminated string from memory at the given address.
Returns an error if reading from memory fails.
Sourcepub fn address(&self, path: &str) -> Option<Address>
pub fn address(&self, path: &str) -> Option<Address>
Find the address of a path.
This method returns None
if ?
is used in the path and the expression before
?
evaluates to a null pointer.
See the crate documentation for the path syntax.
§Panics
Panics if the path fails to compile or reading from memory fails.
Sourcepub fn try_address(&self, path: &str) -> Result<Option<Address>, Error>
pub fn try_address(&self, path: &str) -> Result<Option<Address>, Error>
Find the address of a path.
This method returns None
if ?
is used in the path and the expression before
?
evaluates to a null pointer.
See the crate documentation for the path syntax.
Returns an error if the path fails to compile or reading from memory fails.
Sourcepub fn address_to_symbol(&self, address: Address) -> Option<String>
pub fn address_to_symbol(&self, address: Address) -> Option<String>
Return the name of the global variable at the given address.
Returns None if no global variable is at the address.
Sourcepub fn data_type(&self, path: &str) -> DataType
pub fn data_type(&self, path: &str) -> DataType
Return a simplified description of the type of the given variable.
See the crate documentation for the path syntax.
§Panics
Panics if the path fails to compile or type resolution fails.
Sourcepub fn try_data_type(&self, path: &str) -> Result<DataType, Error>
pub fn try_data_type(&self, path: &str) -> Result<DataType, Error>
Return a simplified description of the type of the given variable.
See the crate documentation for the path syntax.
§Panics
Panics if the path fails to compile or type resolution fails.
Sourcepub fn write(&mut self, path: &str, value: Value)
pub fn write(&mut self, path: &str, value: Value)
Write a value to memory.
See the crate documentation for the path syntax.
§Panics
Panics if the data path fails to compile or the write fails.
Sourcepub fn try_write(&mut self, path: &str, value: Value) -> Result<(), Error>
pub fn try_write(&mut self, path: &str, value: Value) -> Result<(), Error>
Write a value to memory.
See the crate documentation for the path syntax.
Returns an error if the data path fails to compile or the write fails.
Sourcepub fn try_set_input(&mut self, input: Input) -> Result<(), Error>
pub fn try_set_input(&mut self, input: Input) -> Result<(), Error>
Set the game’s controller input for the current frame using Input.
Returns an error if the write fails.
Sourcepub fn save_state(&self) -> SaveState
pub fn save_state(&self) -> SaveState
Create a save state using the current game state.
Sourcepub fn load_state(&mut self, state: &SaveState)
pub fn load_state(&mut self, state: &SaveState)
Sourcepub fn try_load_state(&mut self, state: &SaveState) -> Result<(), Error>
pub fn try_load_state(&mut self, state: &SaveState) -> Result<(), Error>
Load a save state.
Returns an error if the save state was produced by a different Game instance.
Sourcepub fn constant(&self, name: &str) -> Value
pub fn constant(&self, name: &str) -> Value
Return the value of the macro constant or enum variant with the given name.
§Panics
Panics if the constant doesn’t exist. Unless the name has a typo, it is likely that either Wafel is out of date or it is just a limitation of how Wafel obtains constants from the source.
Sourcepub fn try_constant(&self, name: &str) -> Result<Value, Error>
pub fn try_constant(&self, name: &str) -> Result<Value, Error>
Return the value of the macro constant or enum variant with the given name.
Returns an error if the constant doesn’t exist. Unless the name has a typo, it is likely that either Wafel is out of date or it is just a limitation of how Wafel obtains constants from the source.
Sourcepub fn try_render(&self, config: &VizConfig) -> Result<VizScene, Error>
pub fn try_render(&self, config: &VizConfig) -> Result<VizScene, Error>
Render the game to a VizScene object, which can be displayed using [wafel_viz].
Returns an error if rendering fails (most likely a bug in [wafel_viz]).
Sourcepub fn mario_action_names(&self) -> HashMap<u32, String>
pub fn mario_action_names(&self) -> HashMap<u32, String>
Return a mapping from Mario action values to their name (e.g. ACT_IDLE
).
Sourcepub fn frame_log(&self) -> Vec<HashMap<String, Value>>
pub fn frame_log(&self) -> Vec<HashMap<String, Value>>
Read the Wafel frame log for the previous frame advance.
§Panics
Panics if reading the frame log fails, e.g. it contains an invalid event type.
Sourcepub fn try_frame_log(&self) -> Result<Vec<HashMap<String, Value>>, Error>
pub fn try_frame_log(&self) -> Result<Vec<HashMap<String, Value>>, Error>
Read the Wafel frame log for the previous frame advance.
Returns an error if reading the frame log fails, e.g. it contains an invalid event type.
Sourcepub fn try_surfaces(&self) -> Result<Vec<Surface>, Error>
pub fn try_surfaces(&self) -> Result<Vec<Surface>, Error>
Read the currently loaded surfaces.
Returns an error if the read fails.
Sourcepub fn object_hitboxes(&self) -> Vec<ObjectHitbox>
pub fn object_hitboxes(&self) -> Vec<ObjectHitbox>
Sourcepub fn try_object_hitboxes(&self) -> Result<Vec<ObjectHitbox>, Error>
pub fn try_object_hitboxes(&self) -> Result<Vec<ObjectHitbox>, Error>
Read the hitboxes for active objects.
Returns an error if the read fails.