Skip to content

Commit

Permalink
Refactor away BorrowMut issues
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamVenner committed Mar 7, 2024
1 parent 046520b commit be1ead9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gmsv_workshop"
version = "2.2.6"
version = "2.2.7"
edition = "2021"
authors = ["William Venner <[email protected]>"]
publish = false
Expand Down
2 changes: 1 addition & 1 deletion src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct CallbackManager {
impl CallbackManager {
extern "C-unwind" fn poll(_lua: gmod::lua::State) -> i32 {
crate::STEAM.with(|steam| {
steam.borrow_mut().callbacks.run_call_results();
steam.callbacks.run_call_results();
});
0
}
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
mod workshop;
mod callbacks;

use std::cell::{RefCell, Cell};
use std::cell::Cell;

thread_local! {
static STEAM: RefCell<workshop::Steam> = RefCell::new(workshop::Steam::init());
static STEAM: workshop::Steam = workshop::Steam::init();
static LUA: Cell<Option<gmod::lua::State>> = Cell::new(None);
}

Expand Down Expand Up @@ -52,7 +52,6 @@ unsafe extern "C-unwind" fn download(lua: gmod::lua::State) -> i32 {
};

STEAM.with(|steam| {
let mut steam = steam.borrow_mut();
steam.download(steamworks::PublishedFileId(workshop_id as _), callback);
});

Expand Down Expand Up @@ -80,7 +79,6 @@ unsafe extern "C-unwind" fn file_info(lua: gmod::lua::State) -> i32 {
};

STEAM.with(|steam| {
let mut steam = steam.borrow_mut();
steam.file_info(steamworks::PublishedFileId(workshop_id), callback);
});

Expand Down
35 changes: 18 additions & 17 deletions src/workshop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use gmod::lua::LuaReference;
use std::{collections::HashMap, mem::ManuallyDrop, path::PathBuf};
use std::{cell::RefCell, collections::HashMap, mem::ManuallyDrop, path::PathBuf};
use steamworks::PublishedFileId;

macro_rules! check_installed {
Expand Down Expand Up @@ -129,7 +129,7 @@ pub mod downloads {
}

impl Steam {
pub fn download(&mut self, workshop_id: PublishedFileId, callback: Option<LuaReference>) {
pub fn download(&self, workshop_id: PublishedFileId, callback: Option<LuaReference>) {
let lua = crate::lua();
let ugc = self.server.ugc();

Expand All @@ -155,7 +155,7 @@ pub mod downloads {
lua.pop();
}

self.queued.insert(workshop_id, callback);
self.queued.borrow_mut().insert(workshop_id, callback);

println!("[gmsv_workshop] Queued {}", workshop_id);
return;
Expand All @@ -180,7 +180,7 @@ pub mod downloads {
println!("[gmsv_workshop] Downloading {}", workshop_id);

if let Some(callback) = callback {
self.pending.insert(workshop_id, callback);
self.pending.borrow_mut().insert(workshop_id, callback);
}

unsafe {
Expand All @@ -197,13 +197,11 @@ pub mod downloads {

extern "C-unwind" fn process_queued(lua: gmod::lua::State) -> i32 {
crate::STEAM.with(|steam| {
let mut steam = steam.borrow_mut();

if !steam.server.is_logged_in() {
return 0;
}

for (workshop_id, callback) in std::mem::take(&mut steam.queued) {
for (workshop_id, callback) in steam.queued.take() {
steam.download(workshop_id, callback);
}

Expand All @@ -221,20 +219,25 @@ pub mod downloads {
}

unsafe extern "C-unwind" fn poll(lua: gmod::lua::State) -> i32 {
let mut queue = Vec::new();

crate::STEAM.with(|steam| {
let mut steam = steam.borrow_mut();
let ugc = steam.server.ugc();

steam.pending.drain_filter(|workshop_id, callback| {
steam.pending.borrow_mut().drain_filter(|workshop_id, callback| {
if let Some(folder) = check_installed!(ugc, *workshop_id) {
self::callback(lua, Some(*callback), *workshop_id, Some(folder));
queue.push((*callback, *workshop_id, folder));
true
} else {
false
}
});
});

for (callback, workshop_id, folder) in queue {
self::callback(lua, Some(callback), workshop_id, Some(folder));
}

0
}
}
Expand Down Expand Up @@ -361,7 +364,7 @@ use super::*;
}

impl Steam {
pub fn file_info(&mut self, workshop_id: PublishedFileId, callback: LuaReference) {
pub fn file_info(&self, workshop_id: PublishedFileId, callback: LuaReference) {
let ugc = self.server.ugc();

#[cfg(debug_assertions)]
Expand All @@ -388,8 +391,8 @@ use super::*;
pub struct Steam {
pub server: ManuallyDrop<steamworks::Server>,
pub callbacks: ManuallyDrop<steamworks::SingleClient<steamworks::ServerManager>>,
pub pending: HashMap<PublishedFileId, LuaReference>,
pub queued: HashMap<PublishedFileId, Option<LuaReference>>,
pub pending: RefCell<HashMap<PublishedFileId, LuaReference>>,
pub queued: RefCell<HashMap<PublishedFileId, Option<LuaReference>>>,
}
impl Steam {
pub fn init() -> Steam {
Expand All @@ -399,13 +402,11 @@ impl Steam {
})
};

let steam = Steam {
Steam {
pending: Default::default(),
queued: Default::default(),
server: ManuallyDrop::new(server),
callbacks: ManuallyDrop::new(callbacks)
};

steam
}
}
}

0 comments on commit be1ead9

Please sign in to comment.