<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tim</title>
    <description>The latest articles on DEV Community by Tim (@vars1ty).</description>
    <link>https://dev.to/vars1ty</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3959339%2Ffa6ea1d7-5e3a-4796-8bb4-f99bfab2575e.jpeg</url>
      <title>DEV Community: Tim</title>
      <link>https://dev.to/vars1ty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vars1ty"/>
    <language>en</language>
    <item>
      <title>Making Star Stable 1 Multiplayer</title>
      <dc:creator>Tim</dc:creator>
      <pubDate>Sat, 06 Jun 2026 22:54:59 +0000</pubDate>
      <link>https://dev.to/vars1ty/making-star-stable-1-multiplayer-3nnm</link>
      <guid>https://dev.to/vars1ty/making-star-stable-1-multiplayer-3nnm</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;For those who aren't familiar with the &lt;em&gt;Star Stable&lt;/em&gt; titles, they are split into sections:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Star Stable [1, 2, 3, 4] - Singleplayer games&lt;/li&gt;
&lt;li&gt;Star Stable &lt;strong&gt;Online&lt;/strong&gt; - Modern-day Multiplayer game&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Star Stable &lt;strong&gt;Online&lt;/strong&gt; is built on the base of the singleplayer variants, which all take place in a different 4-season climate.&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;not&lt;/strong&gt; a copy-paste-friendly version, and isn't aimed at that either.&lt;br&gt;
While the games are abandonware and you could spin a custom server yourself, it is in a legal gray-zone.&lt;/p&gt;

&lt;p&gt;This also doesn't cover niché features, read the bottom of the page for a small list.&lt;br&gt;
ImGui setup is also not covered, but utilized a custom fork of &lt;code&gt;hudhook&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For this post I'll be solely focusing on &lt;strong&gt;Star Stable 1&lt;/strong&gt;, with these tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Language: Rust&lt;/li&gt;
&lt;li&gt;Decompiler: Ghidra&lt;/li&gt;
&lt;li&gt;Dynamic Analysis: Cheat Engine&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Game Specs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Game Engine: PXEngine (an early 2000's version), written in C/C++&lt;/li&gt;
&lt;li&gt;Scripting Language: PXScript (interpreted)&lt;/li&gt;
&lt;li&gt;Game File Formats: Mostly all custom or packaged in custom ways. All of them are packed inside of &lt;code&gt;data.csa&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Renderer: DirectX 9&lt;/li&gt;
&lt;li&gt;Input Handling: ??? - Weird custom hacky method&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Issue 1: The Renderer
&lt;/h1&gt;

&lt;p&gt;Unlike many other DirectX 9 titles, Star Stable 1 does not utilize a proper rendering method, it instead relies heavily on hardcoded values and a screen resolution.&lt;/p&gt;

&lt;p&gt;To combat this, you can't simply use a WinAPI call to resize the window, the viewport of the game will still remain at the initial fixed size.&lt;/p&gt;
&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;It's a layered solution, working thanks to luck and heavy testing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the to-be-assigned &lt;code&gt;IDirect3DDevice9&lt;/code&gt; instance. This was quite easy as it's stored within a global.&lt;/li&gt;
&lt;li&gt;Hook &lt;code&gt;IDirect3DDevice9::EndScene&lt;/code&gt;

&lt;ol&gt;
&lt;li&gt;On each call we drain our backlogged main-thread callbacks, more on that later.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Hook &lt;code&gt;IDirect3DDevice9::CreateDevice&lt;/code&gt;

&lt;ol&gt;
&lt;li&gt;Patch the &lt;code&gt;D3DPRESENT_PARAMETERS&lt;/code&gt; parameter to a custom width, height and then call &lt;code&gt;SetWindowPos&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Call the original &lt;code&gt;CreateDevice&lt;/code&gt; function and get the result&lt;/li&gt;
&lt;li&gt;Once we've got the result of &lt;code&gt;CreateDevice&lt;/code&gt;, we hook into &lt;code&gt;IDirect3DDevice9::Reset&lt;/code&gt;, ensuring it only happens once via &lt;code&gt;Once&lt;/code&gt;.

&lt;ol&gt;
&lt;li&gt;Once &lt;code&gt;Reset&lt;/code&gt; is called, we make sure to re-patch the &lt;code&gt;D3DPRESENT_PARAMETERS&lt;/code&gt; parameter before calling &lt;code&gt;SetWindowPos&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Return the cached &lt;code&gt;CreateDevice&lt;/code&gt; result from the hooked &lt;code&gt;CreateDevice&lt;/code&gt;, allowing control-flow to continue.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Hook &lt;code&gt;user32::SetWindowPos&lt;/code&gt;

&lt;ol&gt;
&lt;li&gt;On each call we force override the desired width and height to always match what the renderer has been forced to, which is defined via a settings file.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The result is we get a mostly fixed renderer which works with modern displays, high refresh-rate displays and so on.&lt;br&gt;
The hooks are messier than they should be, and that's because modern Windows has worse DirectX 9 "support" (DX9on12) than Linux through Wine.&lt;/p&gt;
&lt;h1&gt;
  
  
  Issue 2: Finding game actors
&lt;/h1&gt;

&lt;p&gt;Almost everything in PXEngine-based games inherit &lt;code&gt;CPXActor&lt;/code&gt;, so we need a way to find them.&lt;/p&gt;

&lt;p&gt;As with many games, it utilizes a tree-like layout of how actors are defined, to put it simply. There's a ton of weird "shortcut" hacks that the engine setup, but we'll ignore those.&lt;/p&gt;

&lt;p&gt;Your players short-path is at &lt;code&gt;global/Player&lt;/code&gt; for example, &lt;code&gt;global&lt;/code&gt; being a shortcut of another actor.&lt;/p&gt;
&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;PXEngine has a function for finding game actors via tree-path, so I just implemented it, added null-checks and return the pointer.&lt;/p&gt;
&lt;h1&gt;
  
  
  Issue 3: Executing game scripts
&lt;/h1&gt;

&lt;p&gt;Game scripts will help with iterating and testing in-game at runtime.&lt;/p&gt;
&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;Luckily this one was also easy to implement and find, which was done by attaching a debugger for when I execute a script and just reversing the right function.&lt;/p&gt;

&lt;p&gt;Now we can do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nn"&gt;CPXActor&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;NULL&lt;/span&gt;&lt;span class="nf"&gt;.execute_script&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"global/Horse.Stop();"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"err"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Issue 4: Abuse the scripting foundation
&lt;/h1&gt;

&lt;p&gt;PXScript is implemented through a "command system" which relies heavily on inheritance and OOP, each command calls back to native code.&lt;/p&gt;

&lt;p&gt;To differ commands against different actor classes, PXEngine splits it apart, each command is assigned an ID bound to a specific Class ID.&lt;br&gt;
If you got for example, &lt;code&gt;CPXSkinMesh&lt;/code&gt; and call &lt;code&gt;Stop&lt;/code&gt; on it, since every actor has an &lt;code&gt;OnCommand&lt;/code&gt; function, it just recursively goes down the tree of inherited actors of the current actor.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Stop&lt;/code&gt; is a command owned by &lt;code&gt;CPXActor&lt;/code&gt;, so it roughly goes like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;CPXSkinMesh::OnCommand&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Lookup ID of &lt;code&gt;Stop&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Not found: fallback to parent actor class' &lt;code&gt;OnCommand&lt;/code&gt; and repeat&lt;/li&gt;
&lt;li&gt;Found: Execute&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's ugly, but it works.&lt;/p&gt;
&lt;h2&gt;
  
  
  What we want out of it
&lt;/h2&gt;

&lt;p&gt;I don't wanna go and find every single native function pointer for everything, and I also don't wanna use PXScript for everything as it's gonna hit a hard barrier eventually and it's not capable of returning native data into Rust, obviously.&lt;/p&gt;

&lt;p&gt;So I want to be able to have something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// &amp;amp;self = CPXActor pointer&lt;/span&gt;
&lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="n"&gt;run_command&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;S&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;AsRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class_name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;S&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;S&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;command_param&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Magic&lt;/span&gt; &lt;span class="n"&gt;Param&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Magic&lt;/span&gt; &lt;span class="n"&gt;Return&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a cut-down version of how it actually looks like, since it's very complex and utilize a lot of code that has shot me in the back from undefined behavior before.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;I looked up the functions for when the engine starts registering a class, a command and when it ends, then hooked all 3 functions and proceeded with:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start Registering a Class:

&lt;ol&gt;
&lt;li&gt;Read the Class Name and ID that's being registered, insert it into a global &lt;code&gt;HashMap&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Store the current-registering Class ID globally via an atomic&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Start Registering a Command:

&lt;ol&gt;
&lt;li&gt;Read the Command string and Command ID, storing it into the &lt;code&gt;HashMap&lt;/code&gt; of the current-registering Actor Class ID.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Create some helper functions to look up a command via Class Name/ID and so on&lt;/li&gt;

&lt;li&gt;Implement &lt;code&gt;run_command&lt;/code&gt; and test it&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;Done, we can now call commands like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="nf"&gt;.run_command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Actor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Start"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"err"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It doesn't work for everything, PXEngine utilizes a lot of weird hacks, but it works for most things and we utilize PXScript (or just implement the native function) when needed.&lt;/p&gt;

&lt;h1&gt;
  
  
  Issue 5: Spawning other players (&amp;amp; horses)
&lt;/h1&gt;

&lt;p&gt;This one may seem easy, and it is quite easy to do via shortcuts.&lt;br&gt;
But doing it from-scratch without utilizing pre-baked objects ("Prefabs" / "Scenes") which are dubbed with the &lt;code&gt;PXO&lt;/code&gt; extension, is a nightmare.&lt;/p&gt;
&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;Luckily for me, the game has both the player and horse saved as pre-baked objects.&lt;/p&gt;

&lt;p&gt;To spawn them though, we have to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Call &lt;code&gt;CreateObject&lt;/code&gt; with the right parameters, and the right Class ID (in this case: &lt;code&gt;FileObject&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Assign the correct "FileObject" resource to the actor, via &lt;code&gt;SetFileObjectName&lt;/code&gt; and loading via &lt;code&gt;FileObjectLoad&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;... Done?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No.&lt;br&gt;
Our player/horse isn't the &lt;code&gt;FileObject&lt;/code&gt;, it's &lt;em&gt;within the &lt;code&gt;FileObject&lt;/code&gt;&lt;/em&gt;, so we have to move it out which can be done via the &lt;code&gt;Move&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;I just chose to move it to a custom-created global actor that was always active and responsible for holding all players (&amp;amp; horses).&lt;/p&gt;

&lt;p&gt;One problem showed up though: The player/horse has physics.&lt;br&gt;
The fix? Just kill the physics via &lt;code&gt;StopPhysics&lt;/code&gt;, problem solved - we don't need it anyways.&lt;/p&gt;

&lt;p&gt;Once all spawned, we rename the spawned player/horse to whatever we want, within bounds of &lt;code&gt;Aa-Zz 0-9&lt;/code&gt; and basic underscores &amp;amp; whitespaces, alongside limiting the name to 32 characters if I can remember correctly.&lt;/p&gt;

&lt;p&gt;Finished Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;CPXPlayer&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Albert"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"err"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;horse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;CPXHorse&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"McHorse"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"err"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Issue 6: Displaying player names
&lt;/h1&gt;

&lt;p&gt;This one took quite a while to solve, because the PXEngine version fo the time had no in-world 3D text, so we had no nametags.&lt;/p&gt;

&lt;p&gt;You could likely wire it yourself, but I chose not to.&lt;br&gt;
I remember cheats usually using &lt;code&gt;WorldToScreen&lt;/code&gt; for ESPs and such, so I started from there.&lt;/p&gt;
&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;Well, finding it wasn't easy, but I found it eventually.&lt;br&gt;
It also enabled me to create the inverse, like &lt;code&gt;ScreenToWorld&lt;/code&gt; and utilizing raycasting, but that's a whole different topic.&lt;/p&gt;

&lt;p&gt;To render nametags, I did this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Calculate the screen-space position of a player, with some offset to get above the head&lt;/li&gt;
&lt;li&gt;Utilize ImGui draw lists to render text over the game viewport itself&lt;/li&gt;
&lt;li&gt;Position the text at the right screen-space position&lt;/li&gt;
&lt;li&gt;Bonus: Make the text fade out if far away, and make it only visible when within screen-bounds &amp;amp; within the looking-direction of the player.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ssp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;CPXScene&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"err"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.world_to_screen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;player_offset_position&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Actual Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Slave = Networked Player, Master = Current Client Player&lt;/span&gt;
&lt;span class="nf"&gt;ardent_ui_renderer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;master_player&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;master_player_camera&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slave_player&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slave_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"err"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Issue 7: Animations
&lt;/h1&gt;

&lt;p&gt;This is relatively simple, there are functions on both the player and horse for playing an animation.&lt;/p&gt;

&lt;p&gt;What wasn't so easy though was finding the current animation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;I just opted for hooking the functions for playing an animation, globally queued a packet for the server and then it sent that off whenever it was free.&lt;/p&gt;

&lt;h1&gt;
  
  
  Issue 8: &lt;em&gt;The Server&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;A game server is always going to be a hassle to get right, that's correct.&lt;br&gt;
But what I didn't expect to run into was beating my head into a wall over the network stacks themselves.&lt;/p&gt;

&lt;p&gt;For reference, I started with TCP, it worked fine at the start but effectively shot itself when the server switched to 60Hz frequency (16.67ms tick-rate).&lt;/p&gt;

&lt;p&gt;So what, switch to UDP? sure, let's try that.&lt;/p&gt;
&lt;h2&gt;
  
  
  UDP
&lt;/h2&gt;

&lt;p&gt;UDP actually did work quite well at the start, but as everyone knows: Raw UDP is FedEx, they don't give a shit and will throw your package through your grandma's window and have you pick it up from her face if it so means saving time.&lt;/p&gt;

&lt;p&gt;This caused problems with player spawning packets not always being received.&lt;br&gt;
Yes I know about ACKs, but at the stage the server was at, it was a nightmare to even try and implement that while also trying to reverse a whole game.&lt;/p&gt;
&lt;h2&gt;
  
  
  Our lord and savior
&lt;/h2&gt;

&lt;p&gt;QUIC, via the &lt;code&gt;noq&lt;/code&gt; crate.&lt;/p&gt;

&lt;p&gt;I've heard of it before, mostly from websites, but it does work for full-fledged servers too (and I'd highly recommend it after this).&lt;/p&gt;
&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Take the old raw UDP code and throw it out the window&lt;/li&gt;
&lt;li&gt;Implement QUIC, it took about a day to transition both the client and server to it&lt;/li&gt;
&lt;li&gt;Test, and shockingly enough: It worked, most of the bugs from before were gone and the leftovers is likely me missing some edge-cases with packet handling as I never finished it.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Send and Receive (S/R) data
&lt;/h2&gt;

&lt;p&gt;For sending data, I first used &lt;code&gt;serde_json&lt;/code&gt; - unique, I know. To be extra unique, I compressed and decompressed all the data.&lt;/p&gt;

&lt;p&gt;This worked, but since I don't like wasting bandwidth, I switched to &lt;code&gt;rkyv&lt;/code&gt; which I've used before.&lt;br&gt;
The transition took approx. 30m to 1 hour, the codebase(s) tried to follow the KISS-principle as much as possible, all code is documented for example.&lt;/p&gt;
&lt;h1&gt;
  
  
  Issue 9: Thread Safety
&lt;/h1&gt;

&lt;p&gt;PXEngine is not thread-safe, and likely never will be (yes, it's alive and renamed to 'Stellar' in Star Stable Online).&lt;/p&gt;

&lt;p&gt;Why was this a problem? oh well, the &lt;strong&gt;client implementation&lt;/strong&gt; was perhaps &lt;strong&gt;asynchronous&lt;/strong&gt;, and what do we get if we mix the two? an error would be the best case, but this was either silent UB or clueless crashes.&lt;/p&gt;
&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;Remember the &lt;code&gt;EndScene&lt;/code&gt; hook and how that's called every game frame? Yeah:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a global Vec/List (the debate never ends) of callbacks to execute at the end of the next frame, type: &lt;code&gt;Box&amp;lt;dyn FnOnce() -&amp;gt; Result&amp;lt;()&amp;gt;&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add callbacks via &lt;code&gt;ArdentMainThreadCallbacks::schedule_callback&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Pop the entire Vec/List when calling &lt;code&gt;ArdentMainThreadCallbacks::execute_callbacks&lt;/code&gt;, if a callback returns an &lt;code&gt;Err&lt;/code&gt; value, then the call-chain is interrupted and error logged to &lt;code&gt;stderr&lt;/code&gt;; another try is made next frame with the remaining callbacks, repeat.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Actually, not really done.&lt;br&gt;
&lt;code&gt;Box&amp;lt;dyn FnOnce() -&amp;gt; Result&amp;lt;()&amp;gt;&amp;gt;&lt;/code&gt; is not thread-safe.&lt;/p&gt;

&lt;p&gt;My fix?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="cd"&gt;/// Ardent hacks `Box&amp;lt;dyn FnOnce() -&amp;gt; anyhow::Result&amp;lt;()&amp;gt;&amp;gt;` for thread-safety.&lt;/span&gt;
&lt;span class="cd"&gt;/// This is classified safe as it's the `FnOnce` entries stored are always called&lt;/span&gt;
&lt;span class="cd"&gt;/// from within `EndScene` on the DX9 renderer, and never anywhere else.&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nf"&gt;ArdentFnOnceSyncHack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;dyn&lt;/span&gt; &lt;span class="nf"&gt;FnOnce&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Safety: See above.&lt;/span&gt;
&lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;Send&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ArdentFnOnceSyncHack&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nb"&gt;Sync&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ArdentFnOnceSyncHack&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No more brat-like compiler errors, and yes; This actually did remove &lt;strong&gt;all thread-safety-linked&lt;/strong&gt; crashes.&lt;/p&gt;

&lt;h1&gt;
  
  
  The End
&lt;/h1&gt;

&lt;p&gt;This was all a &lt;strong&gt;very slimmed-down&lt;/strong&gt; version of how it all played out, in the process of it all I had to develop a ton of systems to account for multiplayer in a singleplayer title, I also had to patch things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Player/Horse Stats - otherwise it would conflict with spawned player/horse stats&lt;/li&gt;
&lt;li&gt;Items - clothes and such have a hardcoded string to lookup the &lt;strong&gt;client player&lt;/strong&gt; when equipped, which makes every networked player apply stuff onto us rather than themselves.

&lt;ul&gt;
&lt;li&gt;This entire system was never finished, it's so badly written and it plagues Star Stable Online to this very day, just that they have &lt;strong&gt;over 10k items all relying on this terrible system&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;This also required me patching the entire actor-finding function for it to even remotely work.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Input handling - by default we'd otherwise be able to steer networked players and horses&lt;/li&gt;

&lt;li&gt;"Insert CD-ROM" checks&lt;/li&gt;

&lt;li&gt;Horse variants - otherwise we'd be out-of-sync with the players horse model and coat&lt;/li&gt;

&lt;li&gt;Proper horse speed - otherwise the horse animations look off if we don't do this&lt;/li&gt;

&lt;li&gt;Tricking the game engine into keeping select "stale resources" loaded - if not, some player items won't be synced and will lead to an error as they're freed from memory&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;and much more. Keep in mind, this was all done in &lt;strong&gt;approx. 2 months&lt;/strong&gt;, spawning from me being bored and just diving head-first into testing my luck.&lt;/p&gt;

&lt;p&gt;Then I also developed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A custom SIMD AABB collision system as I didn't wanna use their own ODE implementation&lt;/li&gt;
&lt;li&gt;Custom game engine editor-like UI, with object spawning/removal/save support&lt;/li&gt;
&lt;li&gt;Custom race system&lt;/li&gt;
&lt;li&gt;Mini PXScript anti-cheat with server-side validation&lt;/li&gt;
&lt;li&gt;DirectX 9 Runtime Texture Swaps &lt;strong&gt;without memory leaks&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;This also offered restoration of swapped textures&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;ImGui theme that nearly 1:1 cloned the games own UI theme&lt;/li&gt;

&lt;li&gt;A modern player and horse client loop, replacing PXEngine's own buggy loops

&lt;ul&gt;
&lt;li&gt;This made input separation and such 20x easier too&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Release
&lt;/h2&gt;

&lt;p&gt;I don't think I will ever release this project public, named "Ardent" if you hadn't figured it out by now.&lt;/p&gt;

&lt;p&gt;Maybe one day I will, but for now it'll keep collecting dust on my PC, with the usual every-now-and-then DM of "Where's Ardent?".&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Rust?
&lt;/h2&gt;

&lt;p&gt;Because I don't like C, and I absolutely hate C++.&lt;br&gt;
Zig? I don't care about it.&lt;/p&gt;

&lt;p&gt;I've been programming in basically only Rust for years now, I think I've written more unsafe Rust than safe.&lt;/p&gt;

&lt;p&gt;That may trigger the &lt;code&gt;forbid_unsafe&lt;/code&gt; cultists, but so be it.&lt;br&gt;
If your gig is to strap a condom over your fingers because you are insecure about your own skills as a low-level programmer, then that's on you.&lt;/p&gt;

&lt;p&gt;A tip for those who wanna learn unsafe Rust: Just start.&lt;br&gt;
Reverse-engineer that childhood game of yours, dive head-first into it and assuming you're determined enough; You'll come back with 7x more experience than you'll ever get from any tutorial or book.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>rust</category>
      <category>networking</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Abusing c_variadic in Rust</title>
      <dc:creator>Tim</dc:creator>
      <pubDate>Sat, 30 May 2026 03:47:47 +0000</pubDate>
      <link>https://dev.to/vars1ty/abusing-cvariadic-in-rust-4hjp</link>
      <guid>https://dev.to/vars1ty/abusing-cvariadic-in-rust-4hjp</guid>
      <description>&lt;h2&gt;
  
  
  What is &lt;code&gt;c_variadic&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;c_variadic&lt;/code&gt; is a recently stabilized feature in Rust that lets you send variadics through C/C++ FFI interop.&lt;/p&gt;

&lt;p&gt;The best example is with &lt;code&gt;int printf(const char* format, ...)&lt;/code&gt;: The &lt;code&gt;...&lt;/code&gt; are your variadics and allows you to pass any number of parameters.&lt;/p&gt;

&lt;p&gt;You can utilize it like this in Rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v_params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="s"&gt;"My magic number is: %d"&lt;/span&gt;&lt;span class="nf"&gt;.as_ptr&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;1234&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Backstory
&lt;/h2&gt;

&lt;p&gt;When I was developing a cheat for a video game I eventually grew sick of users requesting new features; I'm not a machine - I chose to implement the Rune scripting language into it.&lt;/p&gt;

&lt;p&gt;It worked great at the start, did its job and it opened up the potential for custom community-driven sub-cheat menus.&lt;/p&gt;

&lt;p&gt;Although as the project itself shifted to being a cheat framework, this didn't meet my own demands, which were:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Injecting into unprotected games via &lt;code&gt;LoadLibraryA&lt;/code&gt; - ✅&lt;/li&gt;
&lt;li&gt;Hooking ImGui based on the game renderer (using hudhook, great project) - ✅&lt;/li&gt;
&lt;li&gt;Adding WinAPI Memory R/W functions, alongside with pattern scanning and such - ✅&lt;/li&gt;
&lt;li&gt;Adding runtime ImGui UI building into Rune scripting - ✅&lt;/li&gt;
&lt;li&gt;Adding function hooks and function pointer calling into Rune scripting - 💀&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Yeah... 2 challenges from it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How do I add function calling?&lt;/li&gt;
&lt;li&gt;How do I hook functions and call back to Rune?&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;I first tried to tackle the first problem, I could dynamically cast a Rune &lt;code&gt;Value&lt;/code&gt; into its native type by doing loads of type-checking and conversions.&lt;/p&gt;

&lt;p&gt;So I tried to just call a function with 1 parameter using it and casting the desired function pointer into a &lt;code&gt;extern "C" fn(p_0: *const std::ffi::c_void) -&amp;gt; *const std::ffi::c_void&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This worked and the return value was obtainable &amp;amp; sent back to the calling Rune function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multi-parameter functions
&lt;/h3&gt;

&lt;p&gt;Not every function is a single parameter. In fact, almost none are in a game.&lt;/p&gt;

&lt;p&gt;I can collect the parameters from a Rune function by having the user just pass them all into a large &lt;code&gt;Vec&amp;lt;Value&amp;gt;&lt;/code&gt; and then doing type-conversion; that part is solved.&lt;/p&gt;

&lt;p&gt;But how would I call functions with varying amount of parameters? I can't change the extern-syntax at runtime; Rust is typed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abusing c_variadic
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;c_variadic&lt;/code&gt; available, I first tried to just force a random function as &lt;code&gt;unsafe extern "C" fn(...) -&amp;gt; *const std::ffi::c_void&lt;/code&gt; - and prayed.&lt;/p&gt;

&lt;p&gt;The result? &lt;strong&gt;It worked&lt;/strong&gt; but with one caveat: It only works on &lt;strong&gt;64-bit&lt;/strong&gt; and that's mainly because &lt;strong&gt;32-bit&lt;/strong&gt; often use different calling-conventions, like &lt;code&gt;thiscall&lt;/code&gt;, &lt;code&gt;fastcall&lt;/code&gt; and whatnot.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing it in Rune
&lt;/h3&gt;

&lt;p&gt;Rune obviously doesn't have variadics like that, and shouldn't.&lt;/p&gt;

&lt;p&gt;So... how should it be implemented?&lt;br&gt;
Simple: &lt;code&gt;match params_vec.len() { ... }&lt;/code&gt; - account for a &lt;em&gt;n&lt;/em&gt; amount of parameters.&lt;/p&gt;

&lt;p&gt;I opted for &lt;code&gt;15&lt;/code&gt; parameters, making a large match-statement and getting the next parameter from the vector and passing it in.&lt;/p&gt;

&lt;p&gt;The result? &lt;strong&gt;It worked&lt;/strong&gt; - but with the downside that we have absolutely no idea how many parameters a function pointer can actually take, so a user could pass in 10 into a function that only takes 2.&lt;/p&gt;

&lt;p&gt;The solution? Let them blow their foot off. &lt;/p&gt;
&lt;h2&gt;
  
  
  Function Hooks
&lt;/h2&gt;

&lt;p&gt;The first problem was solved, now this... The library I was using for hooking was &lt;code&gt;retour&lt;/code&gt; in case you were wondering btw.&lt;/p&gt;
&lt;h3&gt;
  
  
  Where do hooks go?
&lt;/h3&gt;

&lt;p&gt;This was probably the biggest problem of it all: &lt;strong&gt;Where do they get sent off to?&lt;/strong&gt; - Rune as mentioned, is a VM-based language and we can't just redirect it to a VM pointer.&lt;/p&gt;
&lt;h3&gt;
  
  
  The (ugly) solution
&lt;/h3&gt;

&lt;p&gt;I opted for creating a struct which held data, here's a 1:1 representation of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="cd"&gt;/// Holds the information about a Rune detour.&lt;/span&gt;
&lt;span class="nd"&gt;#[derive(Default)]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;RDetour&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// The ID of the detour.&lt;/span&gt;
    &lt;span class="n"&gt;detour_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="cd"&gt;/// The pointer of which function will be treated as target, and will be redirected to a&lt;/span&gt;
    &lt;span class="cd"&gt;/// determined detour holder from `determine_detour_holder()`.&lt;/span&gt;
    &lt;span class="cd"&gt;/// If `None`, this detour isn't ready to be used and is free to be acquired.&lt;/span&gt;
    &lt;span class="n"&gt;from_ptr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;isize&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="cd"&gt;/// The `RawDetour` instance.&lt;/span&gt;
    &lt;span class="cd"&gt;/// If `None`, this detour isn't ready to be used and is free to be acquired.&lt;/span&gt;
    &lt;span class="n"&gt;detour&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RawDetour&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="cd"&gt;/// Rune function to be called as a callback, should return a `isize` of the original functions&lt;/span&gt;
    &lt;span class="cd"&gt;/// return value as a pointer, or a modified value if needed.&lt;/span&gt;
    &lt;span class="cd"&gt;/// If `None`, this detour isn't ready to be used and is free to be acquired.&lt;/span&gt;
    &lt;span class="n"&gt;rune_function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;SyncFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="cd"&gt;/// Optional paramater to be passed into `rune_function` upon callback.&lt;/span&gt;
    &lt;span class="cd"&gt;/// Can be a structure for example, so that variables can be updated.&lt;/span&gt;
    &lt;span class="n"&gt;opt_param&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ValueWrapper&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may notice one special type: &lt;code&gt;ValueWrapper&lt;/code&gt; - and that's just a standard Rune &lt;code&gt;Value&lt;/code&gt; but with force-implemented &lt;code&gt;Send + Sync&lt;/code&gt;, and that's for one ugly reason: &lt;strong&gt;Every RDetour struct instance was stored globally&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It was behind an Atomic RefCell which helped a ton, but yeah not the code you'd typically wanna be writing.&lt;/p&gt;

&lt;p&gt;To actually have a real, native function that the hook can call back to, I opted for creating a macro that generated &lt;code&gt;35&lt;/code&gt; "detour holder" functions that all registered themselves linearly, which then created a new &lt;code&gt;RDetour&lt;/code&gt; struct instance and saved it globally.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing new hooks from Rune
&lt;/h3&gt;

&lt;p&gt;To install new hooks from within Rune, I ran through these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the first-available detour out of the 35 total ones&lt;/li&gt;
&lt;li&gt;Switch out &lt;code&gt;rune_function&lt;/code&gt; to an user-defined Rune-level callback that the hook would call&lt;/li&gt;
&lt;li&gt;Switch out &lt;code&gt;from_ptr&lt;/code&gt; to what the user specified&lt;/li&gt;
&lt;li&gt;Assign &lt;code&gt;opt_param&lt;/code&gt; if the user has defined a custom Rune &lt;code&gt;Value&lt;/code&gt; to access within &lt;code&gt;rune_function&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create the hook&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If a step failed, then it would log the error visually.&lt;/p&gt;

&lt;h3&gt;
  
  
  Processing the incoming data
&lt;/h3&gt;

&lt;p&gt;Since we have native hook/detour holders, all data goes to native stub-like functions.&lt;/p&gt;

&lt;p&gt;Once data was received, the stub had generated code that would instruct it to look up the hook that was set on its ID.&lt;/p&gt;

&lt;p&gt;That helper function (&lt;code&gt;call_rune_function_on_id&lt;/code&gt;) would then get the Rune callback function and call it with these parameters:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Trampoline pointer the user should call themselves to continue control-flow. Skip it and the function will just be a glorified &lt;code&gt;return;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A 10-param-sized tuple, the closest to a variadic in Rune. The parameters were - you guessed it - obtained by having every stub-function have a &lt;code&gt;c_vardiadic&lt;/code&gt; body&lt;/li&gt;
&lt;li&gt;The optional Rune &lt;code&gt;Value&lt;/code&gt; so the user can utilize it, if desired&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once the user utilized the newly-added &lt;code&gt;fn_ptr_call&lt;/code&gt; function on the trampoline pointer and the right parameters, it would continue on as expected.&lt;/p&gt;

&lt;p&gt;The users were often instructed to see how many parameters the function &lt;em&gt;actually wanted&lt;/em&gt;, and not just blow off the whole hill with 10.&lt;/p&gt;

&lt;p&gt;They often didn't listen (neither did I, I'm lazy) - and it worked for 99% of all cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;While Rust is a safe language by design, it very much is capable of the same things as C/C++ if you know what you are doing.&lt;/p&gt;

&lt;p&gt;Unsafe Rust shouldn't be frowned upon, it should be taught and studied, there's tons of neat tricks that you can utilize.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>ffi</category>
      <category>systems</category>
      <category>software</category>
    </item>
  </channel>
</rss>
