Skip to content

Commit

Permalink
Allow handle creation without specifying a local_path on Linux
Browse files Browse the repository at this point in the history
It'll only succeed for Morrowind as all other games need a local path
and it can't be detected on Linux.
  • Loading branch information
Ortham committed Aug 20, 2023
1 parent 5796fc4 commit 2ea8290
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
62 changes: 55 additions & 7 deletions ffi/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ pub unsafe extern "C" fn lo_create_handle(

let load_order: Box<dyn WritableLoadOrder>;
if local_path.is_null() {
#[cfg(not(windows))]
return error(
LIBLO_ERROR_INVALID_ARGS,
"A local data path must be supplied on non-Windows platforms",
);

#[cfg(windows)]
match GameSettings::new(game_id, game_path) {
Ok(x) => load_order = x.into_load_order(),
Err(x) => return handle_error(x),
Expand Down Expand Up @@ -473,4 +466,59 @@ mod tests {
assert_eq!(LIBLO_ERROR_INVALID_ARGS, result);
}
}

#[test]
fn lo_create_handle_should_always_accept_a_null_local_path_if_game_is_morrowind() {
let mut handle: lo_game_handle = std::ptr::null_mut();
let game_path = CString::new(".").unwrap();

unsafe {
let result = lo_create_handle(
&mut handle,
LIBLO_GAME_TES3,
game_path.as_ptr(),
std::ptr::null(),
);
lo_destroy_handle(handle);

assert_eq!(LIBLO_OK, result);
}
}

#[test]
fn lo_create_handle_should_accept_a_null_local_path_if_game_is_not_morrowind() {
let mut handle: lo_game_handle = std::ptr::null_mut();
let game_path = CString::new(".").unwrap();

unsafe {
let result = lo_create_handle(
&mut handle,
LIBLO_GAME_TES4,
game_path.as_ptr(),
std::ptr::null(),
);
lo_destroy_handle(handle);

assert_eq!(LIBLO_OK, result);
}
}

#[test]
#[cfg(not(windows))]
fn lo_create_handle_should_not_accept_a_null_local_path_if_game_is_not_morrowind() {
let mut handle: lo_game_handle = std::ptr::null_mut();
let game_path = CString::new(".").unwrap();

unsafe {
let result = lo_create_handle(
&mut handle,
LIBLO_GAME_TES4,
game_path.as_ptr(),
std::ptr::null(),
);
lo_destroy_handle(handle);

assert_eq!(LIBLO_ERROR_INVALID_ARGS, result);
}
}
}
10 changes: 10 additions & 0 deletions src/game_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ impl GameSettings {
GameSettings::with_local_path(game_id, game_path, &local_path)
}

#[cfg(not(windows))]
pub fn new(game_id: GameId, game_path: &Path) -> Result<GameSettings, Error> {
if appdata_folder_name(game_id, game_path).is_some() {
Err(Error::NoLocalAppData)
} else {
// It doesn't matter what local_path is passed in, as it isn't used.
GameSettings::with_local_path(game_id, game_path, &PathBuf::new())
}
}

pub fn with_local_path(
game_id: GameId,
game_path: &Path,
Expand Down

0 comments on commit 2ea8290

Please sign in to comment.