|
| 1 | +use semver::{BuildMetadata, Prerelease}; |
| 2 | + |
| 3 | +use crate::core_types::Dictionary; |
| 4 | +use crate::core_types::FromVariant; |
| 5 | +use crate::object::ownership::Unique; |
| 6 | +use crate::private::{get_api, EngineMethodTable}; |
| 7 | + |
| 8 | +/// Checks the version number of the host Godot instance to see if it matches the generated API. |
| 9 | +/// Returns `true` if the test isn't applicable, or if no mismatch was found. |
| 10 | +#[inline] |
| 11 | +pub fn godot_version_mismatch() -> bool { |
| 12 | + let ret = check_godot_version_mismatch(); |
| 13 | + if !ret { |
| 14 | + godot_warn!(concat!( |
| 15 | + "gdnative-core: GDNative version mismatches may lead to subtle bugs, undefined behavior or crashes at runtime.\n", |
| 16 | + "Apply the 'custom-godot' feature if you want to use current godot-rust with another Godot engine version.", |
| 17 | + )); |
| 18 | + } |
| 19 | + |
| 20 | + ret |
| 21 | +} |
| 22 | + |
| 23 | +#[cfg(feature = "custom-godot")] |
| 24 | +fn check_godot_version_mismatch() -> bool { |
| 25 | + true |
| 26 | +} |
| 27 | + |
| 28 | +#[cfg(not(feature = "custom-godot"))] |
| 29 | +fn check_godot_version_mismatch() -> bool { |
| 30 | + use semver::VersionReq; |
| 31 | + |
| 32 | + let version = if let Some(version) = godot_version() { |
| 33 | + version |
| 34 | + } else { |
| 35 | + godot_warn!("gdnative-core: failed to get version info from the engine."); |
| 36 | + return false; |
| 37 | + }; |
| 38 | + |
| 39 | + let version_req = VersionReq::parse("~3.5.1").unwrap(); |
| 40 | + |
| 41 | + if version_req.matches(&version) { |
| 42 | + true |
| 43 | + } else { |
| 44 | + godot_warn!("This godot-rust version is only compatible with Godot `{version_req}`; detected version `{version}`."); |
| 45 | + false |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +fn godot_version() -> Option<semver::Version> { |
| 50 | + let version = unsafe { |
| 51 | + let api = get_api(); |
| 52 | + let engine = (api.godot_global_get_singleton)(b"Engine\0".as_ptr() as *mut _); |
| 53 | + |
| 54 | + let mut dictionary = sys::godot_dictionary::default(); |
| 55 | + |
| 56 | + (api.godot_method_bind_ptrcall)( |
| 57 | + EngineMethodTable::get(api).get_version_info, |
| 58 | + engine, |
| 59 | + [].as_mut_ptr() as *mut _, |
| 60 | + &mut dictionary as *mut _ as *mut _, |
| 61 | + ); |
| 62 | + |
| 63 | + Dictionary::<Unique>::from_sys(dictionary) |
| 64 | + }; |
| 65 | + |
| 66 | + let major = u64::from_variant(&version.get("major")?).ok()?; |
| 67 | + let minor = u64::from_variant(&version.get("minor")?).ok()?; |
| 68 | + let patch = u64::from_variant(&version.get("patch")?).ok()?; |
| 69 | + |
| 70 | + let pre = version |
| 71 | + .get("status") |
| 72 | + .and_then(|v| { |
| 73 | + let s = String::from_variant(&v).ok()?; |
| 74 | + if s == "stable" { |
| 75 | + return None; |
| 76 | + } |
| 77 | + |
| 78 | + let s = s.chars().map(sanitize_for_semver).collect::<String>(); |
| 79 | + Some(Prerelease::new(&s).expect("string sanitized")) |
| 80 | + }) |
| 81 | + .unwrap_or(Prerelease::EMPTY); |
| 82 | + |
| 83 | + let build = { |
| 84 | + let mut build_metadata = String::new(); |
| 85 | + let mut sep = false; |
| 86 | + |
| 87 | + if let Some(build_name) = version |
| 88 | + .get("build") |
| 89 | + .and_then(|v| String::from_variant(&v).ok()) |
| 90 | + { |
| 91 | + build_metadata.extend(build_name.chars().map(sanitize_for_semver)); |
| 92 | + sep = true; |
| 93 | + }; |
| 94 | + |
| 95 | + if let Some(hash) = version |
| 96 | + .get("hash") |
| 97 | + .and_then(|v| String::from_variant(&v).ok()) |
| 98 | + { |
| 99 | + if sep { |
| 100 | + build_metadata.push('.'); |
| 101 | + } |
| 102 | + build_metadata.extend(hash.chars().map(sanitize_for_semver)); |
| 103 | + }; |
| 104 | + |
| 105 | + if build_metadata.is_empty() { |
| 106 | + BuildMetadata::EMPTY |
| 107 | + } else { |
| 108 | + BuildMetadata::new(&build_metadata).expect("build metadata sanitized") |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + Some(semver::Version { |
| 113 | + major, |
| 114 | + minor, |
| 115 | + patch, |
| 116 | + pre, |
| 117 | + build, |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +fn sanitize_for_semver(s: char) -> char { |
| 122 | + if s.is_ascii_alphanumeric() { |
| 123 | + s |
| 124 | + } else { |
| 125 | + '-' |
| 126 | + } |
| 127 | +} |
0 commit comments