Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/lovely-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions crates/lovely-core/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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;

// Exports for convenience.
pub use log::{info, error, warn, debug, trace, LevelFilter};

use log::{Level, Log, Metadata, Record, SetLoggerError};
use once_cell::sync::OnceCell;

static LOGGER: OnceCell<LovelyLogger> = OnceCell::new();
static LOGGER: OnceLock<LovelyLogger> = OnceLock::new();

struct LovelyLogger {
use_console: bool,
Expand Down
5 changes: 2 additions & 3 deletions crates/lovely-core/src/patch/vars.rs
Original file line number Diff line number Diff line change
@@ -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<String, String>) {
// Cache the compiled regex.
let re: Lazy<Regex> = Lazy::new(|| Regex::new(r"\{\{lovely:(\w+)\}\}").unwrap());
let re: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\{\{lovely:(\w+)\}\}").unwrap());

let line_replaced = re.replace_all(line, |captures: &Captures| {
let (_, [var]) = captures.extract();
Expand Down
71 changes: 37 additions & 34 deletions crates/lovely-core/src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,61 +17,64 @@ pub const LUA_TBOOLEAN: isize = 1;
pub type LuaState = c_void;

#[cfg(target_os = "windows")]
pub static LUA_LIB: Lazy<Library> = Lazy::new(|| unsafe { Library::new("lua51.dll").unwrap() });
pub static LUA_LIB: LazyLock<Library> =
LazyLock::new(|| unsafe { Library::new("lua51.dll").unwrap() });

#[cfg(target_os = "macos")]
pub static LUA_LIB: Lazy<Library> =
Lazy::new(|| unsafe { Library::new("../Frameworks/Lua.framework/Versions/A/Lua").unwrap() });
pub static LUA_LIB: LazyLock<Library> = LazyLock::new(|| unsafe {
Library::new("../Frameworks/Lua.framework/Versions/A/Lua").unwrap()
});

pub static lua_call: Lazy<Symbol<unsafe extern "C" fn(*mut LuaState, isize, isize)>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_call").unwrap() });
pub static lua_call: LazyLock<Symbol<unsafe extern "C" fn(*mut LuaState, isize, isize)>> =
LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_call").unwrap() });

#[cfg(target_os = "linux")]
pub static LUA_LIB: Lazy<Library> =
Lazy::new(|| unsafe { Library::new("libluajit-5.1.so.2").unwrap() });
pub static LUA_LIB: LazyLock<Library> =
LazyLock::new(|| unsafe { Library::new("libluajit-5.1.so.2").unwrap() });

pub static lua_pcall: Lazy<
pub static lua_pcall: LazyLock<
Symbol<unsafe extern "C" fn(*mut LuaState, isize, isize, isize) -> 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<Symbol<unsafe extern "C" fn(*mut LuaState, isize, *const char)>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_getfield").unwrap() });
pub static lua_getfield: LazyLock<Symbol<unsafe extern "C" fn(*mut LuaState, isize, *const char)>> =
LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_getfield").unwrap() });

pub static lua_setfield: Lazy<Symbol<unsafe extern "C" fn(*mut LuaState, isize, *const char)>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_setfield").unwrap() });
pub static lua_setfield: LazyLock<Symbol<unsafe extern "C" fn(*mut LuaState, isize, *const char)>> =
LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_setfield").unwrap() });

pub static lua_gettop: Lazy<Symbol<unsafe extern "C" fn(*mut LuaState) -> isize>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_gettop").unwrap() });
pub static lua_gettop: LazyLock<Symbol<unsafe extern "C" fn(*mut LuaState) -> isize>> =
LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_gettop").unwrap() });

pub static lua_settop: Lazy<Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> isize>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_settop").unwrap() });
pub static lua_settop: LazyLock<Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> isize>> =
LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_settop").unwrap() });

pub static lua_pushvalue: Lazy<Symbol<unsafe extern "C" fn(*mut LuaState, isize)>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_pushvalue").unwrap() });
pub static lua_pushvalue: LazyLock<Symbol<unsafe extern "C" fn(*mut LuaState, isize)>> =
LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_pushvalue").unwrap() });

pub static lua_pushcclosure: Lazy<
pub static lua_pushcclosure: LazyLock<
Symbol<unsafe extern "C" fn(*mut LuaState, *const c_void, isize)>,
> = 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<unsafe extern "C" fn(*mut LuaState, isize, *mut isize) -> *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<Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> bool>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_toboolean").unwrap() });
pub static lua_toboolean: LazyLock<Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> bool>> =
LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_toboolean").unwrap() });

pub static lua_topointer: Lazy<
pub static lua_topointer: LazyLock<
Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> *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<Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> isize>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_type").unwrap() });
pub static lua_type: LazyLock<Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> isize>> =
LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_type").unwrap() });

pub static lua_typename: Lazy<Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> *const char>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_typename").unwrap() });
pub static lua_typename: LazyLock<
Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> *const char>,
> = LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_typename").unwrap() });

pub static lua_isstring: Lazy<Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> isize>> =
Lazy::new(|| unsafe { LUA_LIB.get(b"lua_isstring").unwrap() });
pub static lua_isstring: LazyLock<Symbol<unsafe extern "C" fn(*mut LuaState, isize) -> isize>> =
LazyLock::new(|| unsafe { LUA_LIB.get(b"lua_isstring").unwrap() });

/// Load the provided buffer as a lua module with the specified name.
/// # Safety
Expand Down
1 change: 0 additions & 1 deletion crates/lovely-unix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ crate-type = ["cdylib"]
[dependencies]
lovely-core = { version ="0.7.1", path = "../lovely-core" }

once_cell = "1.19.0"
dobby-rs = "0.1.0"
ctor = "0.2.7"
13 changes: 6 additions & 7 deletions crates/lovely-unix/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use lovely_core::log::*;
use lovely_core::sys::{LuaState, LUA_LIB};
use std::{env, ffi::c_void, mem, panic};
use std::{env, ffi::c_void, mem, panic, sync::{LazyLock, OnceLock}};

use lovely_core::Lovely;
use once_cell::sync::{Lazy, OnceCell};

static RUNTIME: OnceCell<Lovely> = OnceCell::new();
static RUNTIME: OnceLock<Lovely> = 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 {
> = LazyLock::new(|| unsafe {
let lua_loadbufferx: unsafe extern "C" fn(
*mut LuaState,
*const u8,
Expand All @@ -33,7 +32,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, std::ptr::null())
}

Expand All @@ -44,7 +43,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)
}

Expand Down
1 change: 0 additions & 1 deletion crates/lovely-win/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down
11 changes: 5 additions & 6 deletions crates/lovely-win/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::env;
use std::ffi::c_void;
use std::panic;
use std::sync::{LazyLock, OnceLock};

use itertools::Itertools;
use lovely_core::log::*;
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};
Expand All @@ -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<Lovely> = OnceCell::new();
static RUNTIME: OnceLock<Lovely> = 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<U16CString> =
Lazy::new(|| U16CString::from_str(format!("Lovely {LOVELY_VERSION}")).unwrap());
static WIN_TITLE: LazyLock<U16CString> =
LazyLock::new(|| U16CString::from_str(format!("Lovely {LOVELY_VERSION}")).unwrap());

unsafe extern "C" fn lua_loadbufferx_detour(
state: *mut LuaState,
Expand All @@ -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)
}

Expand Down