rigela_utils/
library.rs

1/*
2 * Copyright (c) 2024. The RigelA open source project team and
3 * its contributors reserve all rights.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and limitations under the License.
12 */
13
14use crate::fs::get_rigela_program_directory;
15use log::error;
16use std::{
17    fs::{create_dir, OpenOptions},
18    io::Write,
19    path::PathBuf,
20};
21
22/**
23 获取RigelA库目录的路径。
24 */
25pub fn get_rigela_library_path() -> PathBuf {
26    let path = get_rigela_program_directory().join("libs");
27    if !path.exists() {
28        create_dir(&path).unwrap();
29    }
30    path
31}
32
33/**
34安装一个动态库文件到指定的目录。
35`path` 动态库路径。
36`lib_bin` 库的二进制数据。
37*/
38pub fn setup_library(path: &PathBuf, lib_bin: &[u8]) {
39    if path.exists() {
40        return;
41    }
42
43    match OpenOptions::new().write(true).create(true).open(&path) {
44        Ok(mut f) => match f.write_all(lib_bin) {
45            Ok(_) => {}
46            Err(e) => error!("Can't setup the `{}` library. {}", path.display(), e),
47        },
48        Err(e) => error!("Can't setup the `{}` library. {}", path.display(), e),
49    }
50}
51
52#[macro_export]
53macro_rules! call_proc {
54    ($module:expr,$name:ident,$def:ty,$($arg:expr),*) => {{
55        let f = get_proc_address($module, stringify!($name));
56        if !f.is_none() {
57            unsafe {
58                let r = (&*((&f) as *const FARPROC as *const $def)) ($($arg),*);
59                Some(r)
60            }
61        } else {
62            None
63        }
64    }};
65}