From 90a0718861a5d3b9bae09450918de430c17a9439 Mon Sep 17 00:00:00 2001 From: IzawGithub Date: Sun, 2 Mar 2025 13:20:28 +0100 Subject: [PATCH 1/2] Remove use of `once_cell` dependency. --- crates/lovely-core/src/log.rs | 5 +- crates/lovely-core/src/patch/vars.rs | 5 +- crates/lovely-core/src/sys.rs | 71 +++++++++++++++------------- crates/lovely-unix/src/lib.rs | 12 ++--- crates/lovely-win/src/lib.rs | 11 ++--- 5 files changed, 52 insertions(+), 52 deletions(-) diff --git a/crates/lovely-core/src/log.rs b/crates/lovely-core/src/log.rs index 15d51355..50daba2b 100644 --- a/crates/lovely-core/src/log.rs +++ b/crates/lovely-core/src/log.rs @@ -1,7 +1,7 @@ -use std::sync::RwLock; use std::path::Path; use std::io::Write; use std::fs::{self, File}; +use std::sync::{OnceLock, RwLock}; use chrono::Local; @@ -9,9 +9,8 @@ use chrono::Local; pub use log::{info, error, warn, debug, trace, LevelFilter}; use log::{Level, Log, Metadata, Record, SetLoggerError}; -use once_cell::sync::OnceCell; -static LOGGER: OnceCell = OnceCell::new(); +static LOGGER: OnceLock = OnceLock::new(); struct LovelyLogger { use_console: bool, diff --git a/crates/lovely-core/src/patch/vars.rs b/crates/lovely-core/src/patch/vars.rs index 74452466..04d44e55 100644 --- a/crates/lovely-core/src/patch/vars.rs +++ b/crates/lovely-core/src/patch/vars.rs @@ -1,13 +1,12 @@ -use std::collections::HashMap; +use std::{collections::HashMap, sync::LazyLock}; -use once_cell::sync::Lazy; use regex_lite::{Regex, Captures}; /// Apply valid var interpolations to the provided line. /// Interpolation targets are of form {{lovely:VAR_NAME}}. pub fn apply_var_interp(line: &mut String, vars: &HashMap) { // Cache the compiled regex. - let re: Lazy = Lazy::new(|| Regex::new(r"\{\{lovely:(\w+)\}\}").unwrap()); + let re: LazyLock = LazyLock::new(|| Regex::new(r"\{\{lovely:(\w+)\}\}").unwrap()); let line_replaced = re.replace_all(line, |captures: &Captures| { let (_, [var]) = captures.extract(); diff --git a/crates/lovely-core/src/sys.rs b/crates/lovely-core/src/sys.rs index 5fa917ae..2d46e8d9 100644 --- a/crates/lovely-core/src/sys.rs +++ b/crates/lovely-core/src/sys.rs @@ -2,12 +2,12 @@ use std::{ collections::VecDeque, ffi::{c_void, CString}, ptr, slice, + sync::LazyLock, }; use itertools::Itertools; use libloading::{Library, Symbol}; use log::info; -use once_cell::sync::Lazy; pub const LUA_GLOBALSINDEX: isize = -10002; @@ -17,61 +17,64 @@ pub const LUA_TBOOLEAN: isize = 1; pub type LuaState = c_void; #[cfg(target_os = "windows")] -pub static LUA_LIB: Lazy = Lazy::new(|| unsafe { Library::new("lua51.dll").unwrap() }); +pub static LUA_LIB: LazyLock = + LazyLock::new(|| unsafe { Library::new("lua51.dll").unwrap() }); #[cfg(target_os = "macos")] -pub static LUA_LIB: Lazy = - Lazy::new(|| unsafe { Library::new("../Frameworks/Lua.framework/Versions/A/Lua").unwrap() }); +pub static LUA_LIB: LazyLock = LazyLock::new(|| unsafe { + Library::new("../Frameworks/Lua.framework/Versions/A/Lua").unwrap() +}); -pub static lua_call: Lazy> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_call").unwrap() }); +pub static lua_call: LazyLock> = + LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_call").unwrap() }); #[cfg(target_os = "linux")] -pub static LUA_LIB: Lazy = - Lazy::new(|| unsafe { Library::new("libluajit-5.1.so.2").unwrap() }); +pub static LUA_LIB: LazyLock = + LazyLock::new(|| unsafe { Library::new("libluajit-5.1.so.2").unwrap() }); -pub static lua_pcall: Lazy< +pub static lua_pcall: LazyLock< Symbol isize>, -> = Lazy::new(|| unsafe { LUA_LIB.get(b"lua_pcall").unwrap() }); +> = LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_pcall").unwrap() }); -pub static lua_getfield: Lazy> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_getfield").unwrap() }); +pub static lua_getfield: LazyLock> = + LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_getfield").unwrap() }); -pub static lua_setfield: Lazy> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_setfield").unwrap() }); +pub static lua_setfield: LazyLock> = + LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_setfield").unwrap() }); -pub static lua_gettop: Lazy isize>> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_gettop").unwrap() }); +pub static lua_gettop: LazyLock isize>> = + LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_gettop").unwrap() }); -pub static lua_settop: Lazy isize>> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_settop").unwrap() }); +pub static lua_settop: LazyLock isize>> = + LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_settop").unwrap() }); -pub static lua_pushvalue: Lazy> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_pushvalue").unwrap() }); +pub static lua_pushvalue: LazyLock> = + LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_pushvalue").unwrap() }); -pub static lua_pushcclosure: Lazy< +pub static lua_pushcclosure: LazyLock< Symbol, -> = Lazy::new(|| unsafe { LUA_LIB.get(b"lua_pushcclosure").unwrap() }); +> = LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_pushcclosure").unwrap() }); -pub static lua_tolstring: Lazy< +pub static lua_tolstring: LazyLock< Symbol *const char>, -> = Lazy::new(|| unsafe { LUA_LIB.get(b"lua_tolstring").unwrap() }); +> = LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_tolstring").unwrap() }); -pub static lua_toboolean: Lazy bool>> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_toboolean").unwrap() }); +pub static lua_toboolean: LazyLock bool>> = + LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_toboolean").unwrap() }); -pub static lua_topointer: Lazy< +pub static lua_topointer: LazyLock< Symbol *const c_void>, -> = Lazy::new(|| unsafe { LUA_LIB.get(b"lua_topointer").unwrap() }); +> = LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_topointer").unwrap() }); -pub static lua_type: Lazy isize>> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_type").unwrap() }); +pub static lua_type: LazyLock isize>> = + LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_type").unwrap() }); -pub static lua_typename: Lazy *const char>> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_typename").unwrap() }); +pub static lua_typename: LazyLock< + Symbol *const char>, +> = LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_typename").unwrap() }); -pub static lua_isstring: Lazy isize>> = - Lazy::new(|| unsafe { LUA_LIB.get(b"lua_isstring").unwrap() }); +pub static lua_isstring: LazyLock isize>> = + LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_isstring").unwrap() }); /// Load the provided buffer as a lua module with the specified name. /// # Safety diff --git a/crates/lovely-unix/src/lib.rs b/crates/lovely-unix/src/lib.rs index 1715d75a..2f0392ee 100644 --- a/crates/lovely-unix/src/lib.rs +++ b/crates/lovely-unix/src/lib.rs @@ -1,16 +1,16 @@ use lovely_core::sys::{LuaState, LUA_LIB}; +use std::sync::{LazyLock, OnceLock}; use std::{env, ptr::null}; use std::panic; use lovely_core::log::*; use lovely_core::Lovely; -use once_cell::sync::{Lazy, OnceCell}; -static RUNTIME: OnceCell = OnceCell::new(); +static RUNTIME: OnceLock = OnceLock::new(); -static RECALL: Lazy< +static RECALL: LazyLock< unsafe extern "C" fn(*mut LuaState, *const u8, isize, *const u8, *const u8) -> u32, -> = Lazy::new(|| unsafe { *LUA_LIB.get(b"luaL_loadbufferx").unwrap() }); +> = LazyLock::new(|| unsafe { *LUA_LIB.get(b"luaL_loadbufferx").unwrap() }); #[no_mangle] #[allow(non_snake_case)] @@ -20,7 +20,7 @@ unsafe extern "C" fn luaL_loadbuffer( size: isize, name_ptr: *const u8, ) -> u32 { - let rt = RUNTIME.get_unchecked(); + let rt = RUNTIME.get().unwrap_unchecked(); rt.apply_buffer_patches(state, buf_ptr, size, name_ptr, null()) } @@ -33,7 +33,7 @@ unsafe extern "C" fn luaL_loadbufferx( name_ptr: *const u8, mode_ptr: *const u8, ) -> u32 { - let rt = RUNTIME.get_unchecked(); + let rt = RUNTIME.get().unwrap_unchecked(); rt.apply_buffer_patches(state, buf_ptr, size, name_ptr, mode_ptr) } diff --git a/crates/lovely-win/src/lib.rs b/crates/lovely-win/src/lib.rs index ec34c73b..6f99bede 100644 --- a/crates/lovely-win/src/lib.rs +++ b/crates/lovely-win/src/lib.rs @@ -1,6 +1,7 @@ use std::env; use std::ffi::c_void; use std::panic; +use std::sync::{LazyLock, OnceLock}; use itertools::Itertools; use lovely_core::log::*; @@ -8,8 +9,6 @@ use lovely_core::sys::LuaState; use lovely_core::Lovely; use lovely_core::LOVELY_VERSION; -use once_cell::sync::Lazy; -use once_cell::sync::OnceCell; use retour::static_detour; use widestring::U16CString; use windows::core::{s, w, PCWSTR}; @@ -18,14 +17,14 @@ use windows::Win32::System::Console::{AllocConsole, SetConsoleTitleW}; use windows::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryW}; use windows::Win32::UI::WindowsAndMessaging::{MessageBoxW, MESSAGEBOX_STYLE}; -static RUNTIME: OnceCell = OnceCell::new(); +static RUNTIME: OnceLock = OnceLock::new(); static_detour! { pub static LuaLoadbufferx_Detour: unsafe extern "C" fn(*mut LuaState, *const u8, isize, *const u8,*const u8) -> u32; } -static WIN_TITLE: Lazy = - Lazy::new(|| U16CString::from_str(format!("Lovely {LOVELY_VERSION}")).unwrap()); +static WIN_TITLE: LazyLock = + LazyLock::new(|| U16CString::from_str(format!("Lovely {LOVELY_VERSION}")).unwrap()); unsafe extern "C" fn lua_loadbufferx_detour( state: *mut LuaState, @@ -34,7 +33,7 @@ unsafe extern "C" fn lua_loadbufferx_detour( name_ptr: *const u8, mode_ptr: *const u8, ) -> u32 { - let rt = RUNTIME.get_unchecked(); + let rt = RUNTIME.get().unwrap_unchecked(); rt.apply_buffer_patches(state, buf_ptr, size, name_ptr, mode_ptr) } From 12dbea21fbc1effdf01c347525b3fb98cf391fdf Mon Sep 17 00:00:00 2001 From: IzawGithub Date: Sun, 2 Mar 2025 13:31:57 +0100 Subject: [PATCH 2/2] Remove `once_cell` from `Cargo` manifest. --- Cargo.lock | 3 --- crates/lovely-core/Cargo.toml | 1 - crates/lovely-unix/Cargo.toml | 1 - crates/lovely-win/Cargo.toml | 1 - 4 files changed, 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6de0d3e5..acffa63a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -389,7 +389,6 @@ dependencies = [ "libc", "libloading", "log", - "once_cell", "regex-cursor", "regex-lite", "serde", @@ -406,7 +405,6 @@ version = "0.1.0" dependencies = [ "ctor", "lovely-core", - "once_cell", ] [[package]] @@ -417,7 +415,6 @@ dependencies = [ "itertools", "libc", "lovely-core", - "once_cell", "retour", "widestring", "windows", diff --git a/crates/lovely-core/Cargo.toml b/crates/lovely-core/Cargo.toml index 133361ae..1feef11e 100644 --- a/crates/lovely-core/Cargo.toml +++ b/crates/lovely-core/Cargo.toml @@ -8,7 +8,6 @@ dirs = "5.0.1" getargs = "0.5.0" libc = "0.2.153" libloading = "0.8.3" -once_cell = "1.19.0" toml = "0.8.10" serde = { version = "1.0.197", features = ["derive"] } sha2 = "0.10.8" diff --git a/crates/lovely-unix/Cargo.toml b/crates/lovely-unix/Cargo.toml index 684cd70e..42fb7b5d 100644 --- a/crates/lovely-unix/Cargo.toml +++ b/crates/lovely-unix/Cargo.toml @@ -10,5 +10,4 @@ crate-type = ["cdylib"] [dependencies] lovely-core = { version ="0.7.1", path = "../lovely-core" } -once_cell = "1.19.0" ctor = "0.2.7" diff --git a/crates/lovely-win/Cargo.toml b/crates/lovely-win/Cargo.toml index a7f307c3..37648da7 100644 --- a/crates/lovely-win/Cargo.toml +++ b/crates/lovely-win/Cargo.toml @@ -12,7 +12,6 @@ lovely-core = { version ="0.7.1", path = "../lovely-core" } itertools = "0.13.0" libc = "0.2.141" -once_cell = "1.19.0" widestring = "1.0.2"