aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaro Perämaa <[email protected]>2021-05-03 16:37:25 +0300
committerGitHub <[email protected]>2021-05-03 16:37:25 +0300
commit8088dd40de97c6c224b06a9cf484adb2cb373c05 (patch)
tree70f810db6165786773bfc636f1dbfda8dc7ed0d4
parentMerge pull request #49 from WilliamVenner/more_ugc_bindings (diff)
parentUpdate src/lib.rs (diff)
downloadsteamworks-rs-8088dd40de97c6c224b06a9cf484adb2cb373c05.tar.xz
steamworks-rs-8088dd40de97c6c224b06a9cf484adb2cb373c05.zip
Merge pull request #48 from WilliamVenner/steamid_utils
Add SteamID utils (`AccountId::from_raw`, `AccountId::raw`) and `SteamId::steamid32`
-rw-r--r--src/lib.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index ed373ef..54781e5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -390,6 +390,13 @@ impl SteamId {
AccountId(bits.m_comp.m_unAccountID())
}
}
+
+ /// Returns the formatted SteamID32 string for this steam id.
+ pub fn steamid32(&self) -> String {
+ let account_id = self.account_id().raw();
+ let last_bit = account_id & 1;
+ format!("STEAM_0:{}:{}", last_bit, (account_id >> 1))
+ }
}
/// A user's account id
@@ -397,6 +404,24 @@ impl SteamId {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct AccountId(pub(crate) u32);
+impl AccountId {
+ /// Creates an `AccountId` from a raw 32 bit value.
+ ///
+ /// May be useful for deserializing account ids from
+ /// a network or save format.
+ pub fn from_raw(id: u32) -> AccountId {
+ AccountId(id)
+ }
+
+ /// Returns the raw 32 bit value of the steam id
+ ///
+ /// May be useful for serializing steam ids over a
+ /// network or to a save format.
+ pub fn raw(&self) -> u32 {
+ self.0
+ }
+}
+
/// A game id
///
/// Combines `AppId` and other information
@@ -472,4 +497,13 @@ mod tests {
::std::thread::sleep(::std::time::Duration::from_millis(100));
}
}
+
+ #[test]
+ fn steamid_test() {
+ let steamid = SteamId(76561198040894045);
+ assert_eq!("STEAM_0:1:40314158", steamid.steamid32());
+
+ let steamid = SteamId(76561198174976054);
+ assert_eq!("STEAM_0:0:107355163", steamid.steamid32());
+ }
}