Skip to content

Commit

Permalink
Fix Autorun.print
Browse files Browse the repository at this point in the history
* Fix it printing in reverse order
* Fix it crashing with functions being input (That shouldn't have happened in the firstplace but now it uses topointer so whatever.)
  • Loading branch information
Vurv78 committed Mar 25, 2022
1 parent 5e2893d commit 6c5fb8c
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions autorun/src/lua/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ pub fn print(l: LuaState) -> i32 {
let mut buf = String::new();

// Walk through args backwards. Every color will affect all of the text prior.
for i in (1..=nargs).rev() {
let mut color: Option<(u8, u8, u8)> = None;
for i in 1..=nargs {
match lua_type(l, i) {
lua::TTABLE => {
lua_rawgeti(l, i, 1);
Expand All @@ -57,6 +58,7 @@ pub fn print(l: LuaState) -> i32 {
lua_pop(l, 1);
continue;
}

let r = luaL_optinteger(l, -1, 255) as u8;

lua_rawgeti(l, i, 2);
Expand All @@ -65,13 +67,19 @@ pub fn print(l: LuaState) -> i32 {
lua_rawgeti(l, i, 3);
let b = luaL_optinteger(l, -1, 255) as u8;

if !buf.is_empty() {
let str = buf.truecolor(r, g, b);
if let Some(col) = color {
// Take all previous text
let str = buf.truecolor(col.0, col.1, col.2);
buf = String::new();

total_buf.push_str(&format!("{str}"));
}
}

color = Some( (r, g, b) );
},
lua::TFUNCTION | lua::TUSERDATA | lua::TLIGHTUSERDATA | lua::TTHREAD => {
let s = lua_topointer(l, i);
buf.push_str( &format!("{:p}", s) );
},
_ => {
let s = lua_tostring(l, i);
let s = unsafe { CStr::from_ptr(s).to_string_lossy() };
Expand All @@ -80,6 +88,11 @@ pub fn print(l: LuaState) -> i32 {
}
}

if let Some(col) = color {
let str = buf.truecolor(col.0, col.1, col.2);
total_buf.push_str(&format!("{str}"));
}

println!("{total_buf}");

0
Expand Down

0 comments on commit 6c5fb8c

Please sign in to comment.