Skip to content

Commit

Permalink
Merge pull request #147 from kristoff3r/format
Browse files Browse the repository at this point in the history
Run `cargo fmt`
  • Loading branch information
kristoff3r authored Nov 29, 2023
2 parents 356ab50 + 201ae30 commit ce20641
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 44 deletions.
28 changes: 12 additions & 16 deletions src/user_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,10 @@ impl<Manager> UserStats<Manager> {

/// Asynchronously fetch the data for the percentage of players who have received each achievement
/// for the current game globally.
///
///
/// You must have called `request_current_stats()` and it needs to return successfully via its
/// callback prior to calling this!*
///
///
/// **Note: Not sure if this is applicable, as the other achievement functions requiring
/// `request_current_stats()` don't specifically need it to be called in order for them to complete
/// successfully. Maybe it autoruns via `Client::init()/init_app()` somehow?*
Expand All @@ -306,9 +306,8 @@ impl<Manager> UserStats<Manager> {
F: FnOnce(Result<GameId, SteamError>) + 'static + Send,
{
unsafe {
let api_call = sys::SteamAPI_ISteamUserStats_RequestGlobalAchievementPercentages(
self.user_stats,
);
let api_call =
sys::SteamAPI_ISteamUserStats_RequestGlobalAchievementPercentages(self.user_stats);
register_call_result::<sys::GlobalAchievementPercentagesReady_t, _, _>(
&self.inner,
api_call,
Expand Down Expand Up @@ -473,13 +472,11 @@ impl<Manager> UserStats<Manager> {
/// This is used for iterating through all of the achievements with GetAchievementName.
///
/// Returns 0 if the current App ID has no achievements.
///
///
/// *Note: Returns an error for AppId `480` (Spacewar)!*
pub fn get_num_achievements(&self) -> Result<u32,()> {
pub fn get_num_achievements(&self) -> Result<u32, ()> {
unsafe {
let num = sys::SteamAPI_ISteamUserStats_GetNumAchievements(
self.user_stats,
);
let num = sys::SteamAPI_ISteamUserStats_GetNumAchievements(self.user_stats);
if num != 0 {
Ok(num)
} else {
Expand All @@ -489,19 +486,18 @@ impl<Manager> UserStats<Manager> {
}

/// Returns an array of all achievement names for the current AppId.
///
///
/// Returns an empty string for an achievement name if `iAchievement` is not a valid index,
/// and the current AppId must have achievements.
pub fn get_achievement_names(&self) -> Option<Vec<String>> {
let num = self.get_num_achievements().expect("Failed to get number of achievements");
let num = self
.get_num_achievements()
.expect("Failed to get number of achievements");
let mut names = Vec::new();

for i in 0..num {
unsafe {
let name = sys::SteamAPI_ISteamUserStats_GetAchievementName(
self.user_stats,
i
);
let name = sys::SteamAPI_ISteamUserStats_GetAchievementName(self.user_stats, i);

let c_str = CStr::from_ptr(name).to_string_lossy().into_owned();

Expand Down
46 changes: 18 additions & 28 deletions src/user_stats/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ impl<M> AchievementHelper<'_, M> {
Err(())
}
}

/// Returns the percentage of users who have unlocked the specified achievement.
///
///
/// You must have called `request_global_achievement_percentages()` and it needs to return
/// successfully via its callback prior to calling this.
///
///
/// *Note: Always returns an error for AppId `480` (Spacewar)!
/// Other AppIds work fine though.*
///
///
/// # Example
///
/// ```no_run
Expand All @@ -109,7 +109,7 @@ impl<M> AchievementHelper<'_, M> {
/// let user_stats = client.user_stats();
/// let achievement = user_stats.achievement("WIN_THE_GAME");
/// let ach_percent = achievement.get_achievement_achieved_percent().expect("Failed to get achievement percentage");
///
///
/// println!("{}",ach_percent);
/// } else {
/// println!("Error requesting global achievement percentages");
Expand Down Expand Up @@ -138,24 +138,24 @@ impl<M> AchievementHelper<'_, M> {
///
/// This receives the value from a dictionary/map keyvalue store, so you must provide one
/// of the following keys:
///
///
/// - `"name"` to retrive the localized achievement name in UTF8
/// - `"desc"` to retrive the localized achievement description in UTF8
/// - `"hidden"` for retrieving if an achievement is hidden. Returns `"0"` when not hidden,
/// `"1"` when hidden
///
///
/// This localization is provided based on the games language if it's set, otherwise it
/// checks if a localization is available for the users Steam UI Language. If that fails
/// too, then it falls back to english.
///
///
/// This function returns the value as a `string` upon success if all of the following
/// conditions are met; otherwise, an empty string: `""`.
///
///
/// - `request_current_stats()` has completed and successfully returned its callback.
/// - The specified achievement exists in App Admin on the Steamworks website, and the
/// changes are published.
/// - The specified `pchKey` is valid.
///
///
/// # Example
///
/// ```no_run
Expand Down Expand Up @@ -186,10 +186,10 @@ impl<M> AchievementHelper<'_, M> {
}

/// Gets the icon for an achievement.
///
///
/// The image is returned as a handle to be used with `ISteamUtils::GetImageRGBA` to get
/// the actual image data.*
///
///
/// **Note: This is handled within the function. Returns a `Vec<u8>` buffer on success,
/// which can be converted into the image data and saved to disk (e.g. via external RGBA to image crate).*
pub fn get_achievement_icon(&self) -> Option<Vec<u8>> {
Expand All @@ -200,30 +200,20 @@ impl<M> AchievementHelper<'_, M> {
self.name.as_ptr() as *const _,
);
if img == 0 {
return None
return None;
}
let mut width = 0;
let mut height = 0;
if !sys::SteamAPI_ISteamUtils_GetImageSize(
utils,
img,
&mut width,
&mut height
) {
return None
if !sys::SteamAPI_ISteamUtils_GetImageSize(utils, img, &mut width, &mut height) {
return None;
}
assert_eq!(width, 64);
assert_eq!(height, 64);
let mut dest = vec![0; 64 * 64 * 4];
if !sys::SteamAPI_ISteamUtils_GetImageRGBA(
utils,
img,
dest.as_mut_ptr(),
64 * 64 * 4
) {
return None
if !sys::SteamAPI_ISteamUtils_GetImageRGBA(utils, img, dest.as_mut_ptr(), 64 * 64 * 4) {
return None;
}
Some(dest)
}
}
}
}

0 comments on commit ce20641

Please sign in to comment.