|
7 | 7 | include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
8 | 8 | include!(concat!(env!("OUT_DIR"), "/api_wrapper.rs"));
|
9 | 9 |
|
10 |
| -unsafe fn find_api_ptr( |
11 |
| - core_api: *const godot_gdnative_core_api_struct, |
| 10 | +pub enum InitError { |
| 11 | + VersionMismatch { |
| 12 | + api_type: GDNATIVE_API_TYPES, |
| 13 | + want: godot_gdnative_api_version, |
| 14 | + got: godot_gdnative_api_version, |
| 15 | + }, |
| 16 | + Generic { |
| 17 | + message: String, |
| 18 | + }, |
| 19 | +} |
| 20 | + |
| 21 | +fn map_option_to_init_error<T>(t: Option<T>, message: &'static str) -> Result<T, InitError> { |
| 22 | + match t { |
| 23 | + Some(t) => Ok(t), |
| 24 | + None => Err(InitError::Generic { |
| 25 | + message: message.to_string(), |
| 26 | + }), |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +unsafe fn find_version( |
| 31 | + mut api: *const godot_gdnative_api_struct, |
12 | 32 | api_type: GDNATIVE_API_TYPES,
|
13 | 33 | version_major: u32,
|
14 | 34 | version_minor: u32,
|
15 |
| -) -> *const godot_gdnative_api_struct { |
16 |
| - let mut api = core_api as *const godot_gdnative_api_struct; |
| 35 | +) -> Option<Result<*const godot_gdnative_api_struct, InitError>> { |
| 36 | + let mut got = None; |
17 | 37 | if (*api).type_ as u32 == api_type as u32 {
|
18 | 38 | while !api.is_null() {
|
19 |
| - if (*api).version.minor == version_minor |
20 |
| - && (*api).version.major == version_major |
21 |
| - // The boolean expression below SHOULD always be true; |
22 |
| - // we will double check to be safe. |
23 |
| - && (*api).type_ as u32 == api_type as u32 |
24 |
| - { |
25 |
| - return api; |
| 39 | + // The boolean expression below SHOULD always be true; |
| 40 | + // we will double check to be safe. |
| 41 | + if (*api).type_ as u32 == api_type as u32 { |
| 42 | + let (major, minor) = ((*api).version.major, (*api).version.minor); |
| 43 | + if major == version_major && minor == version_minor { |
| 44 | + return Some(Ok(api)); |
| 45 | + } else { |
| 46 | + got = Some(godot_gdnative_api_version { major, minor }); |
| 47 | + } |
26 | 48 | }
|
27 | 49 | api = (*api).next;
|
28 | 50 | }
|
29 | 51 | }
|
| 52 | + got.map(|got| { |
| 53 | + Err(InitError::VersionMismatch { |
| 54 | + want: godot_gdnative_api_version { |
| 55 | + major: version_major, |
| 56 | + minor: version_minor, |
| 57 | + }, |
| 58 | + got, |
| 59 | + api_type, |
| 60 | + }) |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +unsafe fn find_api_ptr( |
| 65 | + core_api: *const godot_gdnative_core_api_struct, |
| 66 | + api_type: GDNATIVE_API_TYPES, |
| 67 | + version_major: u32, |
| 68 | + version_minor: u32, |
| 69 | +) -> Result<*const godot_gdnative_api_struct, InitError> { |
| 70 | + let mut last_error = None; |
| 71 | + match find_version( |
| 72 | + core_api as *const godot_gdnative_api_struct, |
| 73 | + api_type, |
| 74 | + version_major, |
| 75 | + version_minor, |
| 76 | + ) { |
| 77 | + Some(Ok(api)) => { |
| 78 | + return Ok(api); |
| 79 | + } |
| 80 | + Some(Err(error)) => { |
| 81 | + last_error = Some(error); |
| 82 | + } |
| 83 | + None => {} |
| 84 | + } |
30 | 85 | for i in 0..(*core_api).num_extensions {
|
31 |
| - let mut extension = |
32 |
| - *(*core_api).extensions.offset(i as _) as *const godot_gdnative_api_struct; |
33 |
| - if (*extension).type_ as u32 == api_type as u32 { |
34 |
| - while !extension.is_null() { |
35 |
| - if (*extension).version.minor == version_minor |
36 |
| - && (*extension).version.major == version_major |
37 |
| - // The boolean expression below SHOULD always be true; |
38 |
| - // we will double check to be safe. |
39 |
| - && (*extension).type_ as u32 == api_type as u32 |
40 |
| - { |
41 |
| - return extension; |
42 |
| - } |
43 |
| - extension = (*extension).next; |
| 86 | + match find_version( |
| 87 | + *(*core_api).extensions.offset(i as _), |
| 88 | + api_type, |
| 89 | + version_major, |
| 90 | + version_minor, |
| 91 | + ) { |
| 92 | + Some(Ok(api)) => { |
| 93 | + return Ok(api); |
| 94 | + } |
| 95 | + Some(Err(error)) => { |
| 96 | + last_error = Some(error); |
44 | 97 | }
|
| 98 | + None => {} |
45 | 99 | }
|
46 | 100 | }
|
47 |
| - panic!( |
48 |
| - "Couldn't find API struct with type {type}, version {version:?}", |
49 |
| - type = api_type, |
50 |
| - version = (version_major, version_minor), |
51 |
| - ); |
| 101 | + Err(last_error.unwrap_or(InitError::Generic { |
| 102 | + message: format!("Couldn't find API struct with type {}", api_type), |
| 103 | + })) |
52 | 104 | }
|
0 commit comments