Skip to content

Commit

Permalink
support reinitialization with a different appid and remove file search
Browse files Browse the repository at this point in the history
  • Loading branch information
ceifa committed Jul 4, 2023
1 parent e1a0e3e commit 5437383
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 32 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,3 @@ Make sure you have the latest [node.js](https://nodejs.org/en/), [Rust](https://
Install dependencies with `npm install` and then run `npm run build:debug` to build the library.

There is no way to build for all targets easily. The good news is that you don't need to. You can develop and test on your current target, and open a PR. When the code is merged to main, a github action will build for all targets and publish a new version.

### Testing Electron

Go to the [test/electron](./test/electron) directory. There, you can run `npm install` and then `npm start` to run the Electron app.

Click "activate overlay" to test the overlay.
2 changes: 1 addition & 1 deletion client.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function init(appId: number): void
export function init(appId?: number | undefined | null): void
export function restartAppIfNecessary(appId: number): boolean
export function runCallbacks(): void
export interface PlayerSteamId {
Expand Down
13 changes: 0 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ let runCallbacksInterval = undefined
* @returns {Omit<Client, 'init' | 'runCallbacks'>}
*/
module.exports.init = (appId) => {
if (!appId) {
try {
const content = require('fs').readFileSync('steam_appid.txt', 'utf8')
if (content) {
appId = parseInt(content)
} else {
throw new Error('steam_appid.txt file is not valid')
}
} catch (e) {
throw new Error('Failed to load steam_appid.txt file')
}
}

const { init: internalInit, runCallbacks, restartAppIfNecessary, ...api } = nativeBinding

internalInit(appId)
Expand Down
11 changes: 11 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ pub fn set_client(client: Client) {
*client_ref = Some(client);
}

pub fn drop_client() {
let mut client_ref = STEAM_CLIENT.lock().unwrap();
*client_ref = None;
}

pub fn get_single() -> &'static SingleClient {
unsafe {
match &STEAM_SINGLE {
Expand All @@ -36,3 +41,9 @@ pub fn set_single(single: SingleClient) {
STEAM_SINGLE = Some(single);
}
}

pub fn drop_single() {
unsafe {
STEAM_SINGLE = None;
}
}
20 changes: 8 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ pub mod client;
extern crate lazy_static;

#[napi]
pub fn init(app_id: u32) -> Result<(), Error> {
pub fn init(app_id: Option<u32>) -> Result<(), Error> {
if client::has_client() {
let initialized_app_id = client::get_client().utils().app_id().0;
if initialized_app_id != app_id {
return Err(Error::from_reason(format!(
"Client already initialized for app id {}",
app_id
)));
} else {
return Ok(());
}
client::drop_single();
client::drop_client();
}

let result = Client::init_app(app_id);
let result = match app_id {
Some(app_id) => Client::init_app(app_id),
None => Client::init(),
};
match result {
Ok((steam_client, steam_single)) => {
steam_client.user_stats().request_current_stats();
Expand All @@ -40,7 +36,7 @@ pub fn restart_app_if_necessary(app_id: u32) -> bool {
steamworks::restart_app_if_necessary(AppId(app_id))
}

#[napi_derive::napi]
#[napi]
pub fn run_callbacks() {
client::get_single().run_callbacks();
}
Expand Down

0 comments on commit 5437383

Please sign in to comment.