Skip to content

Commit

Permalink
Follow Semantic Versioning and provide APIs for other mods to get ver…
Browse files Browse the repository at this point in the history
…sion of GR
  • Loading branch information
Hagb committed Feb 3, 2025
1 parent 4afe82f commit fe4dc57
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 45 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ cargo-features = ["profile-rustflags"]
name = "giuroll"
authors = ["Giufin (2023~2024)", "Hagb (Junyu Guo) <[email protected]> (2024)"]
license = "MIT"
version = "0.6.18-0-alpha1-hagb"
# Increase DLL_REVISION in build.rs when pre-release version is bumped or removed with fixed major.minor.patch version, and
# reset DLL_REVISION when major.minor.patch version is bumped.
version = "0.6.18-alpha.2"
edition = "2021"
build = "build.rs"
description = "A network rollback mod for 東方非想天則 / Touhou 12.3 Hisoutensoku, with also support for replay rewind and takeover"
Expand Down Expand Up @@ -39,6 +41,7 @@ features = [
ilhook = { path = "ilhookmod" }
mininip = { path = "mininip" } #"1.3.1"
winapi = { version = "0.3.9", features = ["d3d9"] }
version-compare = { version = "0.2.0" }

fern = { version = "0.6.2", optional = true }
humantime = { version = "2.1.0", optional = true }
Expand Down
71 changes: 40 additions & 31 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use winres::{VersionInfo, WindowsResource};

extern crate winres;

static VERSION_REMARK: Option<&str> = Some("(fork by Hagb)");
static DLL_REVISION: u16 = 2;
fn main() {
let mut res = WindowsResource::new();
if cfg!(unix) {
Expand All @@ -15,33 +17,25 @@ fn main() {
res.set_windres_path("/usr/bin/i686-w64-mingw32-windres");
}

if let Some(version_pre) = env::var("CARGO_PKG_VERSION_PRE")
let mut version = 0_u64;
version |= env::var("CARGO_PKG_VERSION_MAJOR")
.unwrap()
.splitn(2, "-")
.next()
{
let mut version = 0_u64;
version |= env::var("CARGO_PKG_VERSION_MAJOR")
.unwrap()
.parse()
.unwrap_or(0)
<< 48;
version |= env::var("CARGO_PKG_VERSION_MINOR")
.unwrap()
.parse()
.unwrap_or(0)
<< 32;
version |= env::var("CARGO_PKG_VERSION_PATCH")
.unwrap()
.parse()
.unwrap_or(0)
<< 16;
version |= version_pre.parse().unwrap_or(0);
res.set_version_info(VersionInfo::FILEVERSION, version);
res.set_version_info(VersionInfo::PRODUCTVERSION, version);
} else {
panic!();
}
.parse::<u64>()
.unwrap()
<< 48;
version |= env::var("CARGO_PKG_VERSION_MINOR")
.unwrap()
.parse::<u64>()
.unwrap()
<< 32;
version |= env::var("CARGO_PKG_VERSION_PATCH")
.unwrap()
.parse::<u64>()
.unwrap()
<< 16;
version |= DLL_REVISION as u64;
res.set_version_info(VersionInfo::FILEVERSION, version);
res.set_version_info(VersionInfo::PRODUCTVERSION, version);

res.set(
"LegalCopyright",
Expand All @@ -60,12 +54,27 @@ fn main() {
"FileDescription",
env::var("CARGO_PKG_DESCRIPTION").unwrap().as_str(),
);
if let Ok(repo) = env::var("SOURCE_URL") {
res.set(
"ProductVersion",
format!("{} ({})", env::var("CARGO_PKG_VERSION").unwrap(), repo).as_str(),
);
res.set(
"ProductVersion",
format!(
"{}{}{}",
env::var("CARGO_PKG_VERSION").unwrap(),
match VERSION_REMARK {
Some(remark) => " ".to_string() + &remark,
None => "".to_string(),
},
env::var("SOURCE_URL")
.and_then(|x| Ok(format!(" ({})", x).to_string()))
.unwrap_or("".to_string())
)
.as_str(),
);

println!("cargo:rustc-env=DLL_REVISION={}", DLL_REVISION);
if let Some(remark) = VERSION_REMARK {
println!("cargo:rustc-env=VERSION_REMARK={}", remark);
}
println!("cargo:rustc-env=DLL_VERSION={}", version);

if let Err(e) = res.compile() {
eprintln!("{}", e);
Expand Down
75 changes: 62 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use netcode::{Netcoder, NetworkPacket};
//use notify::{RecursiveMode, Watcher};
use rollback::{Rollbacker, DUMP_FRAME_TIME, LAST_M_LEN, MEMORY_LEAK};
use sound::RollbackSoundManager;
use winapi::ctypes::c_char;
use windows::core::PCWSTR;
use windows::Win32::Foundation::HANDLE;
use windows::Win32::System::LibraryLoader::GetModuleFileNameW;
Expand Down Expand Up @@ -222,6 +223,61 @@ unsafe fn tamper_jmp_relative_opr<T: Sized>(dst: *mut c_void, src: T) -> T {
return ret;
}

use version_compare::{Cmp, Version};
const VERSION_STR: &str = env!("CARGO_PKG_VERSION");

/// Compare GR version with version_string, following Semantic Versioning 2.0.0 (https://semver.org/).
/// It returns false if version_string is an invalid version string, or
/// returns true and assign *result = 0 (GR version = version_str), -1 (GR version < version_str), or 1 (GR version > version_str), if version_str is valid
#[no_mangle]
pub unsafe extern "C" fn compareVersionString(
version_string: *const c_char,
result: *mut i32,
) -> bool {
if let Ok(version_str) = std::ffi::CStr::from_ptr(version_string).to_str() {
if let Some(ver) = version_compare::Version::from(version_str) {
*result = match Version::from(VERSION_STR).unwrap().compare(ver) {
Cmp::Eq => 0,
Cmp::Lt => -1,
Cmp::Gt => 1,
_ => unreachable!(),
};
return true;
}
}
return false;
}

/// Returns version string
#[no_mangle]
pub extern "C" fn getVersionString() -> *const c_char {
static VERSION_CSTRING: Mutex<Option<std::ffi::CString>> = Mutex::new(None);
let mut string = VERSION_CSTRING.lock().unwrap();
if string.is_none() {
*string = Some(std::ffi::CString::new(VERSION_STR).unwrap());
}
string.as_ref().unwrap().as_ptr()
}

/// Return GR version with given version, where version values consist of four 16 bit words, e.g.
/// `MAJOR << 48 | MINOR << 32 | PATCH << 16 | RELEASE`.
#[no_mangle]
pub unsafe extern "C" fn getVersion() -> u64 {
return env!("DLL_VERSION").parse::<u64>().unwrap();
}

/// Compare GR version with given version, where version values consist of four 16 bit words, e.g.
/// `MAJOR << 48 | MINOR << 32 | PATCH << 16 | RELEASE`.
/// It returns 0 (GR version = version_str), -1 (GR version < version_str), or 1 (GR version > version_str).
#[no_mangle]
pub unsafe extern "C" fn compareVersion(version: u64) -> i32 {
match env!("DLL_VERSION").parse::<u64>().unwrap().cmp(&version) {
std::cmp::Ordering::Equal => 0,
std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Greater => 1,
}
}

#[no_mangle]
pub extern "C" fn getPriority() -> i32 {
1000
Expand Down Expand Up @@ -301,8 +357,6 @@ static SOKU_LOOP_EVENT: Mutex<Option<isize>> = Mutex::new(None);
static TARGET_OFFSET: AtomicI32 = AtomicI32::new(0);
//static TARGET_OFFSET_COUNT: AtomicI32 = AtomicI32::new(0);

const VER: &str = env!("CARGO_PKG_VERSION");

unsafe extern "cdecl" fn skip(_a: *mut ilhook::x86::Registers, _b: usize, _c: usize) {}

//static SOUNDS_THAT_DID_HAPPEN: Mutex<BTreeMap<usize, Vec<usize>>> = Mutex::new(BTreeMap::new());
Expand Down Expand Up @@ -841,20 +895,15 @@ fn truer_exec(filename: PathBuf, pretend_to_be_vanilla: bool) -> Result<(), Stri
}

#[allow(unused_mut)]
let mut verstr: String = VER.to_string();
let mut verstr: String = VERSION_STR.to_string();
if let Some(remark) = option_env!("VERSION_REMARK") {
verstr += " ";
verstr += remark;
}
#[cfg(feature = "lowframetest")]
{
verstr += "-low_frame_test"
verstr += " low_frame_test"
};
#[cfg(feature = "cn")]
{
match verstr.strip_suffix("-unofficial") {
Some(s) => {
verstr = s.to_string();
}
_ => {}
}
}
if f62_enabled {
verstr += " CN";
}
Expand Down

0 comments on commit fe4dc57

Please sign in to comment.