diff options
| author | Aaro Perämaa <[email protected]> | 2021-05-03 16:37:25 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-05-03 16:37:25 +0300 |
| commit | 8088dd40de97c6c224b06a9cf484adb2cb373c05 (patch) | |
| tree | 70f810db6165786773bfc636f1dbfda8dc7ed0d4 | |
| parent | Merge pull request #49 from WilliamVenner/more_ugc_bindings (diff) | |
| parent | Update src/lib.rs (diff) | |
| download | steamworks-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.rs | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -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()); + } } |