Skip to content

Commit

Permalink
Merge pull request #80 from ethangreen-dev/fix-invalid-lua-print
Browse files Browse the repository at this point in the history
Fix reverse output of Lua log messages
  • Loading branch information
ethangreen-dev authored Sep 26, 2024
2 parents fd43d01 + 735d116 commit 554817c
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions crates/lovely-core/src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{
ffi::{c_void, CString},
ptr, slice,
collections::VecDeque, ffi::{c_void, CString}, ptr, slice
};

use itertools::Itertools;
use libc::FILE;
use libloading::{Library, Symbol};
use log::info;
Expand Down Expand Up @@ -117,28 +117,29 @@ pub unsafe fn load_module<F: Fn(*mut LuaState, *const u8, isize, *const u8, *con
/// Native lua API access. It's unsafe, it's unchecked, it will probably eat your firstborn.
pub unsafe extern "C" fn override_print(state: *mut LuaState) -> isize {
let argc = lua_gettop(state);
let mut out = String::new();
let mut out = VecDeque::new();

for i in 0..argc {
for _ in 0..argc {
let mut str_len = 0_isize;
let arg_str = lua_tolstring(state, -1, &mut str_len);
if arg_str.is_null() {
out.push_str("[G] nil");
continue;
}

let str_buf = slice::from_raw_parts(arg_str as *const u8, str_len as _);
let arg_str = String::from_utf8_lossy(str_buf);
let arg_str = match arg_str.is_null() {
true => String::from("nil"),
false => {
let str_buf = slice::from_raw_parts(arg_str as *const u8, str_len as _);
String::from_utf8_lossy(str_buf).to_string()
}
};

if i > 1 {
out.push('\t');
}

out.push_str(&format!("[G] {arg_str}"));
out.push_front(arg_str);
lua_settop(state, -(1) - 1);
}

info!("{out}");
let msg = out
.into_iter()
.join("\t");

info!("[G] {msg}");

0
}
Expand Down

0 comments on commit 554817c

Please sign in to comment.