Skip to content

Commit 7afafed

Browse files
authored
Add support for asynchronous code (#25)
This allows you to define an asynchronous `main` function instead of the poll based `update` function, which allows you to more easily keep state between individual ticks of the runtime. Unfortunately the most efficient implementation isn't possible yet on stable Rust, as we are blocked by the following two features: - [`type_alias_impl_trait`](rust-lang/rust#63063) - [`const_async_blocks`](rust-lang/rust#85368) For now we have to use a workaround that is less efficient by calling the `main` function at runtime and allocating it onto a new WebAssembly page. Here is a full example of how an auto splitter could look like using the `async_main` macro: Usage on stable Rust: ```rust async_main!(stable); ``` Usage on nightly Rust: ```rust async_main!(nightly); ``` The asynchronous main function itself: ```rust async fn main() { // TODO: Set up some general state and settings. loop { let process = Process::wait_attach("explorer.exe").await; process.until_closes(async { // TODO: Load some initial information from the process. loop { // TODO: Do something on every tick. next_tick().await; } }).await; } } ```
1 parent 74bf3d6 commit 7afafed

File tree

4 files changed

+545
-10
lines changed

4 files changed

+545
-10
lines changed

.github/workflows/build.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,18 @@ jobs:
3636
if: matrix.install_target != ''
3737
run: rustup target add ${{ matrix.target }}
3838

39-
- name: Build
39+
- name: Build (No Default Features)
40+
run: |
41+
cargo build --no-default-features --target ${{ matrix.target }}
42+
43+
- name: Build (Default Features)
4044
run: |
4145
cargo build --target ${{ matrix.target }}
4246
47+
- name: Build (All Features)
48+
run: |
49+
cargo build --all-features --target ${{ matrix.target }}
50+
4351
clippy:
4452
name: Check clippy lints
4553
runs-on: ubuntu-latest

README.md

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
# <img src="https://raw.githubusercontent.com/LiveSplit/LiveSplit/master/LiveSplit/Resources/Icon.png" alt="LiveSplit" height="42" width="45" align="top"/> asr
22

3-
Helper crate to write auto splitters for LiveSplit One's auto splitting runtime.
43

5-
## Example
4+
Helper crate to write auto splitters for LiveSplit One's auto splitting
5+
runtime.
6+
7+
There are two ways of defining an auto splitter.
8+
9+
## Defining an `update` function
10+
11+
You can define an `update` function that will be called every frame. This is
12+
the simplest way to define an auto splitter. The function must have the
13+
following signature:
14+
```rust
15+
#[no_mangle]
16+
pub extern "C" fn update() {}
17+
```
18+
19+
The advantage of this approach is that you have full control over what
20+
happens on every tick of the runtime. However, it's much harder to keep
21+
state around as you need to store all state in global variables as you need
22+
to return out of the function on every tick.
23+
24+
### Example
625

726
```rust
827
#[no_mangle]
928
pub extern "C" fn update() {
10-
if let Some(process) = Process::attach("Notepad.exe") {
29+
if let Some(process) = Process::attach("explorer.exe") {
1130
asr::print_message("Hello World!");
12-
if let Ok(address) = process.get_module_address("Notepad.exe") {
31+
if let Ok(address) = process.get_module_address("explorer.exe") {
1332
if let Ok(value) = process.read::<u32>(address) {
1433
if value > 0 {
1534
asr::timer::start();
@@ -20,6 +39,74 @@ pub extern "C" fn update() {
2039
}
2140
```
2241

42+
## Defining an asynchronous `main` function
43+
44+
You can use the `async_main` macro to define an asynchronous `main`
45+
function.
46+
47+
Similar to using an `update` function, it is important to constantly yield
48+
back to the runtime to communicate that the auto splitter is still alive.
49+
All asynchronous code that you await automatically yields back to the
50+
runtime. However, if you want to write synchronous code, such as the main
51+
loop handling of a process on every tick, you can use the
52+
`next_tick` function to yield back to the runtime and
53+
continue on the next tick.
54+
55+
The main low level abstraction is the `retry` function, which wraps any code
56+
that you want to retry until it succeeds, yielding back to the runtime between
57+
each try.
58+
59+
So if you wanted to attach to a Process you could for example write:
60+
61+
```rust
62+
let process = retry(|| Process::attach("MyGame.exe")).await;
63+
```
64+
65+
This will try to attach to the process every tick until it succeeds. This
66+
specific example is exactly how the `Process::wait_attach` method is
67+
implemented. So if you wanted to attach to any of multiple processes, you could
68+
for example write:
69+
70+
```rust
71+
let process = retry(|| {
72+
["a.exe", "b.exe"].into_iter().find_map(Process::attach)
73+
}).await;
74+
```
75+
76+
### Example
77+
78+
Here is a full example of how an auto splitter could look like using the
79+
`async_main` macro:
80+
81+
Usage on stable Rust:
82+
```rust
83+
async_main!(stable);
84+
```
85+
86+
Usage on nightly Rust:
87+
```rust
88+
#![feature(type_alias_impl_trait, const_async_blocks)]
89+
90+
async_main!(nightly);
91+
```
92+
93+
The asynchronous main function itself:
94+
```rust
95+
async fn main() {
96+
// TODO: Set up some general state and settings.
97+
loop {
98+
let process = Process::wait_attach("explorer.exe").await;
99+
process.until_closes(async {
100+
// TODO: Load some initial information from the process.
101+
loop {
102+
// TODO: Do something on every tick.
103+
next_tick().await;
104+
}
105+
}).await;
106+
}
107+
}
108+
```
109+
23110
## License
24111

25112
Licensed under either of

0 commit comments

Comments
 (0)