diff --git a/Cargo.lock b/Cargo.lock index 474043c..ffec203 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -442,7 +442,7 @@ dependencies = [ [[package]] name = "gmsv_workshop" -version = "2.2.6" +version = "2.2.7" dependencies = [ "gmod", "gmod-lzma", diff --git a/Cargo.toml b/Cargo.toml index c24ead6..aece8b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gmsv_workshop" -version = "2.2.6" +version = "2.2.7" edition = "2021" authors = ["William Venner "] publish = false diff --git a/src/callbacks.rs b/src/callbacks.rs index 8451d0a..7ff4bd6 100644 --- a/src/callbacks.rs +++ b/src/callbacks.rs @@ -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 } diff --git a/src/lib.rs b/src/lib.rs index 18e34a0..a5e87b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,10 +11,10 @@ mod workshop; mod callbacks; -use std::cell::{RefCell, Cell}; +use std::cell::Cell; thread_local! { - static STEAM: RefCell = RefCell::new(workshop::Steam::init()); + static STEAM: workshop::Steam = workshop::Steam::init(); static LUA: Cell> = Cell::new(None); } @@ -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); }); @@ -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); }); diff --git a/src/workshop.rs b/src/workshop.rs index 6d1a220..c6fd195 100644 --- a/src/workshop.rs +++ b/src/workshop.rs @@ -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 { @@ -129,7 +129,7 @@ pub mod downloads { } impl Steam { - pub fn download(&mut self, workshop_id: PublishedFileId, callback: Option) { + pub fn download(&self, workshop_id: PublishedFileId, callback: Option) { let lua = crate::lua(); let ugc = self.server.ugc(); @@ -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; @@ -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 { @@ -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); } @@ -221,13 +219,14 @@ 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 @@ -235,6 +234,10 @@ pub mod downloads { }); }); + for (callback, workshop_id, folder) in queue { + self::callback(lua, Some(callback), workshop_id, Some(folder)); + } + 0 } } @@ -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)] @@ -388,8 +391,8 @@ use super::*; pub struct Steam { pub server: ManuallyDrop, pub callbacks: ManuallyDrop>, - pub pending: HashMap, - pub queued: HashMap>, + pub pending: RefCell>, + pub queued: RefCell>>, } impl Steam { pub fn init() -> Steam { @@ -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 + } } }