Skip to content
Open
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
44 changes: 22 additions & 22 deletions crates/lovely-core/benches/patches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn pattern_patches_no_match() -> &'static [PatternPatch] {
match_indent: false,
times: None,
overwrite: false,
name: None,
name: None, silent: false,
},
PatternPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -49,7 +49,7 @@ fn pattern_patches_no_match() -> &'static [PatternPatch] {
match_indent: false,
times: None,
overwrite: false,
name: None,
name: None, silent: false,
},
PatternPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -59,7 +59,7 @@ fn pattern_patches_no_match() -> &'static [PatternPatch] {
match_indent: false,
times: None,
overwrite: false,
name: None,
name: None, silent: false,
},
PatternPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -69,7 +69,7 @@ fn pattern_patches_no_match() -> &'static [PatternPatch] {
match_indent: false,
times: None,
overwrite: false,
name: None,
name: None, silent: false,
},
]
})
Expand All @@ -87,7 +87,7 @@ fn pattern_patches_with_match() -> &'static [PatternPatch] {
match_indent: false,
times: Some(1),
overwrite: false,
name: None,
name: None, silent: false,
},
PatternPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -97,7 +97,7 @@ fn pattern_patches_with_match() -> &'static [PatternPatch] {
match_indent: false,
times: Some(5),
overwrite: false,
name: None,
name: None, silent: false,
},
PatternPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -107,7 +107,7 @@ fn pattern_patches_with_match() -> &'static [PatternPatch] {
match_indent: false,
times: Some(1),
overwrite: false,
name: None,
name: None, silent: false,
},
PatternPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -117,7 +117,7 @@ fn pattern_patches_with_match() -> &'static [PatternPatch] {
match_indent: false,
times: Some(2),
overwrite: false,
name: None,
name: None, silent: false,
},
]
})
Expand All @@ -136,7 +136,7 @@ fn regex_patches_no_match() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: None,
verbose: false,
name: None,
name: None, silent: false,
},
RegexPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -147,7 +147,7 @@ fn regex_patches_no_match() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: None,
verbose: false,
name: None,
name: None, silent: false,
},
RegexPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -158,7 +158,7 @@ fn regex_patches_no_match() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: None,
verbose: false,
name: None,
name: None, silent: false,
},
RegexPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -169,7 +169,7 @@ fn regex_patches_no_match() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: None,
verbose: false,
name: None,
name: None, silent: false,
},
]
})
Expand All @@ -188,7 +188,7 @@ fn regex_patches_with_match() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: Some(1),
verbose: false,
name: None,
name: None, silent: false,
},
RegexPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -199,7 +199,7 @@ fn regex_patches_with_match() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: Some(5),
verbose: false,
name: None,
name: None, silent: false,
},
RegexPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -210,7 +210,7 @@ fn regex_patches_with_match() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: Some(1),
verbose: false,
name: None,
name: None, silent: false,
},
RegexPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -221,7 +221,7 @@ fn regex_patches_with_match() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: Some(2),
verbose: false,
name: None,
name: None, silent: false,
},
]
})
Expand All @@ -239,7 +239,7 @@ fn pattern_patches_position() -> &'static [PatternPatch] {
match_indent: false,
times: Some(1),
overwrite: false,
name: None,
name: None, silent: false,
},
PatternPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -249,7 +249,7 @@ fn pattern_patches_position() -> &'static [PatternPatch] {
match_indent: false,
times: Some(1),
overwrite: false,
name: None,
name: None, silent: false,
},
PatternPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -259,7 +259,7 @@ fn pattern_patches_position() -> &'static [PatternPatch] {
match_indent: false,
times: Some(1),
overwrite: false,
name: None,
name: None, silent: false,
},
]
})
Expand All @@ -278,7 +278,7 @@ fn regex_patches_position() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: Some(1),
verbose: false,
name: None,
name: None, silent: false,
},
RegexPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -289,7 +289,7 @@ fn regex_patches_position() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: Some(1),
verbose: false,
name: None,
name: None, silent: false,
},
RegexPatch {
target: Target::Single("sample_buffer.txt".to_string()),
Expand All @@ -300,7 +300,7 @@ fn regex_patches_position() -> &'static [RegexPatch] {
line_prepend: String::new(),
times: Some(1),
verbose: false,
name: None,
name: None, silent: false,
},
]
})
Expand Down
42 changes: 29 additions & 13 deletions crates/lovely-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ use patch::{ModulePatch, Patch};
use regex_lite::Regex;

use sys::{check_lua_string, LuaFunc, LuaLib, LuaState, LuaStateTrait, LUA};
use wildmatch::WildMatch;

use crate::dump::{write_dump, PatchDebug};
use crate::patch::Target;
use crate::dump::{PatchDebug, write_dump};

pub mod chunk_vec_cursor;
pub mod dump;
Expand Down Expand Up @@ -311,16 +312,23 @@ impl Lovely {

// Apply patches onto this buffer.
let res = patch_table.apply_patches(name, buf_str, state);
if res.is_err() {
state.push(res.unwrap_err());
if let Err(err) = res {
state.push(err);
// NOTE: Not really a great error but it doesn't handle the correcter errors right.
return 3; // LUA_ERRSYNTAX
}
let (patched, debug) = res.unwrap();

write_dump(&self.mod_dir, "game-dump", &pretty_name, &patched, &PatchDebug::new(name));
write_dump(&self.mod_dir, "dump", &pretty_name, &patched, &debug);

if self.dump_all || !debug.entries.iter().all(|x| x.regions.is_empty()) {
write_dump(
&self.mod_dir,
"game-dump",
&pretty_name,
&patched,
&PatchDebug::new(name),
);
write_dump(&self.mod_dir, "dump", &pretty_name, &patched, &debug);
}
(self.loadbuffer)(state, patched.as_ptr(), patched.len(), name_ptr, mode_ptr)
}
}
Expand All @@ -336,9 +344,9 @@ unsafe extern "C" fn apply_patches(lua_state: *mut LuaState) -> c_int {
let binding = RUNTIME.get().unwrap().patch_table.read().unwrap();
if binding.needs_patching(&buf_name) {
let res = binding.apply_patches(&buf_name, &buf, lua_state);
if res.is_err() {
if let Err(err) = res {
lua_state.push(false);
lua_state.push(res.unwrap_err());
lua_state.push(err);
num = 2;
return;
}
Expand All @@ -360,19 +368,27 @@ unsafe extern "C" fn apply_patches(lua_state: *mut LuaState) -> c_int {
impl Target {
pub fn can_apply(&self, target: &str) -> bool {
match self {
Self::Single(str) => str == target,
Self::Multi(strs) => strs.iter().any(|x| x == target),
Self::Single(str) => WildMatch::new(str).matches(target),
Self::Multi(strs) => strs.iter().any(|x| WildMatch::new(x).matches(target)),
}
}

pub fn insert_into(&self, targets: &mut HashSet<String>) {
pub fn insert_into(&self, targets: &mut (HashSet<String>, Vec<WildMatch>)) {
match self {
Self::Single(str) => {
targets.insert(str.clone());
if str.contains('?') || str.contains('*') {
targets.1.push(WildMatch::new(str));
} else {
targets.0.insert(str.clone());
}
}
Self::Multi(strs) => {
for target in strs.iter() {
targets.insert(target.clone());
if target.contains('?') || target.contains('*') {
targets.1.push(WildMatch::new(target));
} else {
targets.0.insert(target.clone());
}
}
}
}
Expand Down
Loading
Loading