rust3ds / ctru-rs

Rust wrapper for libctru
https://rust3ds.github.io/ctru-rs/
Other
122 stars 18 forks source link

Use `userAppInit` and `userAppExit` for runtime setup and takedown #53

Closed FenrirWolf closed 1 year ago

FenrirWolf commented 2 years ago

Fun fact, libctru supports life before and after main in the appInit and appExit functions. You can hook into this system by defining extern "C" fn userAppInit() and extern "C" fn userAppExit() functions and putting any* library-level setup/teardown code you want in them.

*Well, some things in the Rust stdlib aren't safe to call before or after main. But 3DS service initialization and even panic hook registration should be fine to do there.

Meziu commented 2 years ago

Oh, this means we could avoid the call to ctru::init at the start of programs.

Edit: seems like we can't. The issue is obviously with linking, the symbols aren't escalated since believed to be unused... having #[used(linker)] available for functions too is a priority, most of our toolchain is based on that.

FenrirWolf commented 2 years ago

Hmm, that's unfortunate. It's also possible to override appInit and appExit themselves, and I wonder if that would work better since libctru calls those unconditionally. I'd try it out myself but I haven't worked out what the build process for the project is yet

AzureMarker commented 2 years ago

One downside of using this feature is that then users can't use this, since the symbol would already be defined.

AzureMarker commented 2 years ago

I'd try it out myself but I haven't worked out what the build process for the project is yet

It's pretty much take the rust-horizon rustc fork, compile locally and add to rustup, then use cargo-3ds for building and stuff. Plus the devkitpro installation. We should probably write this up somewhere.

FenrirWolf commented 2 years ago

We should probably write this up somewhere.

That would be good. I've already run into issues getting the thing to build so a place to talk about that would be nice too

Meziu commented 2 years ago

One downside of using this feature is that then users can't use this, since the symbol would already be defined.

We could add our own safe hook, though for now it doesn’t matter.

Meziu commented 2 years ago

We should probably write this up somewhere.

That would be good. I've already run into issues getting the thing to build so a place to talk about that would be nice too

We were waiting for Rust to merge our changes before making wikis, this way people would be able to just use cargo 3ds on a nightly toolchain and be able to compile.

AzureMarker commented 2 years ago

We could enable and use the GitHub discussions feature in the meantime.

Meziu commented 1 year ago

It may work now, but due to the uncertainty of using the std outside the main and wanting to leave the possibility of customization to the user, this issue is now obsolete. Didn't think I'd ever close this! 😄