1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * This file is part of libloadorder
 *
 * Copyright (C) 2017 Oliver Hamlet
 *
 * libloadorder is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * libloadorder is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with libloadorder. If not, see <http://www.gnu.org/licenses/>.
 */

use std::error::Error;
use std::panic::catch_unwind;
use std::ptr;

use libc::{c_char, c_uint, size_t};

use super::lo_game_handle;
use constants::*;
use helpers::{error, handle_error, to_c_string_array, to_str, to_str_vec};

/// Gets the list of currently active plugins.
///
/// If no plugins are active, the value pointed to by `plugins` will be null and `num_plugins` will
/// point to zero.
///
/// Returns `LIBLO_OK` if successful, otherwise a `LIBLO_ERROR_*` code is returned.
#[no_mangle]
pub unsafe extern "C" fn lo_get_active_plugins(
    handle: lo_game_handle,
    plugins: *mut *mut *mut c_char,
    num_plugins: *mut size_t,
) -> c_uint {
    catch_unwind(|| {
        if handle.is_null() || plugins.is_null() || num_plugins.is_null() {
            return error(LIBLO_ERROR_INVALID_ARGS, "Null pointer passed");
        }
        let handle = match (*handle).read() {
            Err(e) => return error(LIBLO_ERROR_POISONED_THREAD_LOCK, e.description()),
            Ok(h) => h,
        };

        *plugins = ptr::null_mut();
        *num_plugins = 0;

        let active_plugins = handle.active_plugin_names();

        if active_plugins.is_empty() {
            return LIBLO_OK;
        }

        match to_c_string_array(&active_plugins) {
            Ok((pointer, size)) => {
                *plugins = pointer;
                *num_plugins = size;
            }
            Err(x) => return error(x, "A filename contained a null byte"),
        }

        LIBLO_OK
    }).unwrap_or(LIBLO_ERROR_PANICKED)
}

/// Sets the list of currently active plugins.
///
/// Replaces the current active plugins list with the plugins in the given array. The replacement
/// list must be valid. If, for Skyrim or Fallout 4, a plugin to be activated does not have a
/// defined load order position, this function will append it to the load order. If multiple such
/// plugins exist, they are appended in the order they are given.
///
/// Returns `LIBLO_OK` if successful, otherwise a `LIBLO_ERROR_*` code is returned.
#[no_mangle]
pub unsafe extern "C" fn lo_set_active_plugins(
    handle: lo_game_handle,
    plugins: *const *const c_char,
    num_plugins: size_t,
) -> c_uint {
    catch_unwind(|| {
        if handle.is_null() || plugins.is_null() {
            return error(LIBLO_ERROR_INVALID_ARGS, "Null pointer passed");
        }
        let mut handle = match (*handle).write() {
            Err(e) => return error(LIBLO_ERROR_POISONED_THREAD_LOCK, e.description()),
            Ok(h) => h,
        };

        let plugins: Vec<&str> = match to_str_vec(plugins, num_plugins) {
            Ok(x) => x,
            Err(x) => return error(x, "A filename contained a null byte"),
        };

        if let Err(x) = handle.set_active_plugins(&plugins) {
            return handle_error(x);
        }

        if let Err(x) = handle.save() {
            return handle_error(x);
        }

        LIBLO_OK
    }).unwrap_or(LIBLO_ERROR_PANICKED)
}

/// Activates or deactivates a given plugin.
///
/// If `active` is true, the plugin will be activated. If `active` is false, the plugin will be
/// deactivated.
///
/// When activating a plugin that is ghosted, the ".ghost" extension is removed. If a plugin is
/// already in its target state, ie. a plugin to be activated is already activate, or a plugin to
/// be deactivated is already inactive, no changes are made.
///
/// Returns `LIBLO_OK` if successful, otherwise a `LIBLO_ERROR_*` code is returned.
#[no_mangle]
pub unsafe extern "C" fn lo_set_plugin_active(
    handle: lo_game_handle,
    plugin: *const c_char,
    active: bool,
) -> c_uint {
    catch_unwind(|| {
        if handle.is_null() || plugin.is_null() {
            return error(LIBLO_ERROR_INVALID_ARGS, "Null pointer passed");
        }
        let mut handle = match (*handle).write() {
            Err(e) => return error(LIBLO_ERROR_POISONED_THREAD_LOCK, e.description()),
            Ok(h) => h,
        };

        let plugin = match to_str(plugin) {
            Ok(x) => x,
            Err(x) => return error(x, "The filename contained a null byte"),
        };

        let result = if active {
            handle.activate(plugin)
        } else {
            handle.deactivate(plugin)
        };

        if let Err(x) = result {
            return handle_error(x);
        }

        if let Err(x) = handle.save() {
            return handle_error(x);
        }

        LIBLO_OK
    }).unwrap_or(LIBLO_ERROR_PANICKED)
}

/// Checks if a given plugin is active.
///
/// Outputs `true` if the plugin is active, and false otherwise.
///
/// Returns `LIBLO_OK` if successful, otherwise a `LIBLO_ERROR_*` code is returned.
#[no_mangle]
pub unsafe extern "C" fn lo_get_plugin_active(
    handle: lo_game_handle,
    plugin: *const c_char,
    result: *mut bool,
) -> c_uint {
    catch_unwind(|| {
        if handle.is_null() || plugin.is_null() || result.is_null() {
            return error(LIBLO_ERROR_INVALID_ARGS, "Null pointer passed");
        }
        let handle = match (*handle).read() {
            Err(e) => return error(LIBLO_ERROR_POISONED_THREAD_LOCK, e.description()),
            Ok(h) => h,
        };

        let plugin = match to_str(plugin) {
            Ok(x) => x,
            Err(x) => return error(x, "The filename contained a null byte"),
        };

        *result = handle.is_active(plugin);

        LIBLO_OK
    }).unwrap_or(LIBLO_ERROR_PANICKED)
}