aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorWilliam Venner <[email protected]>2021-04-22 19:09:20 +0100
committerWilliam Venner <[email protected]>2021-04-22 19:09:20 +0100
commit2875a209de0dfbee39c45a88620c6f6506cc579b (patch)
treefabeae8c7c13203ed15c43bfbba8a88f12563c45 /src/lib.rs
parentMerge pull request #45 from WilliamVenner/fix/macos_builds (diff)
downloadsteamworks-rs-2875a209de0dfbee39c45a88620c6f6506cc579b.tar.xz
steamworks-rs-2875a209de0dfbee39c45a88620c6f6506cc579b.zip
Add SteamID utils (AccountId::from_raw, AccountId::raw) and SteamId::steamid32
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 1c7cdec..5555e70 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -370,6 +370,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: u8 = if account_id % 2 == 0 { 0 } else { 1 };
+ format!("STEAM_0:{}:{}", last_bit, (account_id >> 1))
+ }
}
/// A user's account id
@@ -377,6 +384,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
@@ -452,4 +477,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());
+ }
}