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
#![allow(
    non_upper_case_globals,
    non_camel_case_types,
    non_snake_case,
    improper_ctypes,
    clippy::style
)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
include!(concat!(env!("OUT_DIR"), "/api_wrapper.rs"));

#[derive(Debug)]
pub enum InitError {
    VersionMismatch {
        api_type: GDNATIVE_API_TYPES,
        want: godot_gdnative_api_version,
        got: godot_gdnative_api_version,
    },
    Generic {
        message: String,
    },
}

fn map_option_to_init_error<T>(t: Option<T>, message: &'static str) -> Result<T, InitError> {
    match t {
        Some(t) => Ok(t),
        None => Err(InitError::Generic {
            message: message.to_string(),
        }),
    }
}

unsafe fn find_version(
    mut api: *const godot_gdnative_api_struct,
    api_type: GDNATIVE_API_TYPES,
    version_major: u32,
    version_minor: u32,
) -> Option<Result<*const godot_gdnative_api_struct, InitError>> {
    let mut got = None;
    if (*api).type_ as u32 == api_type as u32 {
        while !api.is_null() {
            // The boolean expression below SHOULD always be true;
            // we will double check to be safe.
            if (*api).type_ as u32 == api_type as u32 {
                let (major, minor) = ((*api).version.major, (*api).version.minor);
                if major == version_major && minor == version_minor {
                    return Some(Ok(api));
                } else {
                    got = Some(godot_gdnative_api_version { major, minor });
                }
            }
            api = (*api).next;
        }
    }
    got.map(|got| {
        Err(InitError::VersionMismatch {
            want: godot_gdnative_api_version {
                major: version_major,
                minor: version_minor,
            },
            got,
            api_type,
        })
    })
}

unsafe fn find_api_ptr(
    core_api: *const godot_gdnative_core_api_struct,
    api_type: GDNATIVE_API_TYPES,
    version_major: u32,
    version_minor: u32,
) -> Result<*const godot_gdnative_api_struct, InitError> {
    let mut last_error = None;
    match find_version(
        core_api as *const godot_gdnative_api_struct,
        api_type,
        version_major,
        version_minor,
    ) {
        Some(Ok(api)) => {
            return Ok(api);
        }
        Some(Err(error)) => {
            last_error = Some(error);
        }
        None => {}
    }
    for i in 0..(*core_api).num_extensions {
        match find_version(
            *(*core_api).extensions.offset(i as _),
            api_type,
            version_major,
            version_minor,
        ) {
            Some(Ok(api)) => {
                return Ok(api);
            }
            Some(Err(error)) => {
                last_error = Some(error);
            }
            None => {}
        }
    }
    Err(last_error.unwrap_or(InitError::Generic {
        message: format!("Couldn't find API struct with type {}", api_type),
    }))
}