Skip to content

Commit 2656539

Browse files
Merge pull request #48 from InfectedBytes/FixTryUnlock
Fix try unlock
2 parents ecef529 + 4d23edb commit 2656539

File tree

103 files changed

+1140
-142
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1140
-142
lines changed

Assets/Plugins/GameJolt/Scripts/API/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public static class Constants
44
{
5-
public const string Version = "2.5.6";
5+
public const string Version = "2.5.7";
66

77
public const string ApiProtocol = "https://";
88
public const string ApiRoot = "api.gamejolt.com/api/game/";

Assets/Plugins/GameJolt/Scripts/API/GameJoltAPI.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,18 @@ public IEnumerator GetRequest(string url, ResponseFormat format, Action<Response
9595
}
9696

9797
float timeout = Time.time + Settings.Timeout;
98-
var request = UnityVersionAbstraction.GetRequest(url, format);
99-
request.SendWebRequest();
100-
while(!request.isDone) {
101-
if(Time.time > timeout) {
102-
request.Abort();
103-
callback(new Response("Timeout for " + url));
104-
yield break;
98+
using(var request = UnityVersionAbstraction.GetRequest(url, format)) {
99+
request.SendWebRequest();
100+
while(!request.isDone) {
101+
if(Time.time > timeout) {
102+
request.Abort();
103+
callback(new Response("Timeout for " + url));
104+
yield break;
105+
}
106+
yield return new WaitForEndOfFrame();
105107
}
106-
yield return new WaitForEndOfFrame();
108+
callback(new Response(request, format));
107109
}
108-
callback(new Response(request, format));
109110
}
110111

111112
public IEnumerator PostRequest(string url, Dictionary<string, string> payload, ResponseFormat format,
@@ -117,18 +118,19 @@ public IEnumerator PostRequest(string url, Dictionary<string, string> payload, R
117118

118119
float timeout = Time.time + Settings.Timeout;
119120

120-
var request = UnityWebRequest.Post(url, payload);
121-
request.SendWebRequest();
122-
while(!request.isDone) {
123-
if(Time.time > timeout) {
124-
request.Abort();
125-
callback(new Response("Timeout for " + url));
126-
yield break;
121+
using(var request = UnityWebRequest.Post(url, payload)) {
122+
request.SendWebRequest();
123+
while(!request.isDone) {
124+
if(Time.time > timeout) {
125+
request.Abort();
126+
callback(new Response("Timeout for " + url));
127+
yield break;
128+
}
129+
yield return new WaitForEndOfFrame();
127130
}
128-
yield return new WaitForEndOfFrame();
129-
}
130131

131-
callback(new Response(request, format));
132+
callback(new Response(request, format));
133+
}
132134
}
133135
#endregion Requests
134136

Assets/Plugins/GameJolt/Scripts/API/Trophies.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,17 @@ public static void TryUnlock(Trophy trophy, Action<TryUnlockResult> callback = n
9292
/// <param name="trophy">The <see cref="Trophy"/> ID.</param>
9393
/// <param name="callback">A callback function accepting a single parameter, an enum indicating success.</param>
9494
public static void TryUnlock(int id, Action<TryUnlockResult> callback = null) {
95+
// early return in case we already know it's unlocked
96+
if(cachedTrophies != null && cachedTrophies.ContainsKey(id) && cachedTrophies[id].Unlocked) {
97+
if(callback != null) callback(TryUnlockResult.AlreadyUnlocked);
98+
return;
99+
}
95100
Get(id, trophy => {
96101
if(trophy == null) { // something went wrong -> signal failure
97102
if(callback != null) callback(TryUnlockResult.Failure);
98103
} else if(!trophy.Unlocked) { // not unlocked yet, so unlock it now
99104
var unlockCallback = callback == null ? (Action<bool>)null : success => callback(success ? TryUnlockResult.Unlocked : TryUnlockResult.Failure);
105+
trophy.Unlocked = true; // already set it as unlocked so if TryUnlock was called multiple times without waiting for a response, we still only show the notification once
100106
Unlock(id, unlockCallback);
101107
} else if(callback != null) { // already unlocked, so if we have a callback, signal success
102108
callback(TryUnlockResult.AlreadyUnlocked);

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased][Unreleased]
66

7+
## [2.5.7][v2_5_7] (2021-08-07)
8+
9+
### Fixed
10+
- `TryUnlock` would still show two notifications if requested faster than the server could reply
11+
- Get and Post requests were not wrapped in a `using` statement
12+
713
## [2.5.6][v2_5_6] (2020-03-06)
814

915
### Fixed
@@ -191,7 +197,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
191197

192198
Initial release
193199

194-
[Unreleased]: https://github.com/InfectedBytes/gj-unity-api/compare/v2.5.6...HEAD
200+
[Unreleased]: https://github.com/InfectedBytes/gj-unity-api/compare/v2.5.7...HEAD
201+
[v2_5_7]: https://github.com/InfectedBytes/gj-unity-api/tree/v2.5.7
195202
[v2_5_6]: https://github.com/InfectedBytes/gj-unity-api/tree/v2.5.6
196203
[v2_5_5]: https://github.com/InfectedBytes/gj-unity-api/tree/v2.5.5
197204
[v2_5_4]: https://github.com/InfectedBytes/gj-unity-api/tree/v2.5.4

Documentation/Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PROJECT_NAME = "Game Jolt Unity API"
3838
# could be handy for archiving the generated documentation or if some version
3939
# control system is used.
4040

41-
PROJECT_NUMBER = v2.5.6
41+
PROJECT_NUMBER = v2.5.7
4242

4343
# Using the PROJECT_BRIEF tag one can provide an optional one line description
4444
# for a project that appears at the top of each page and should give viewer a

Documentation/Pages/tutorial.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Trophies need a signed in user.
147147
**Tip:** Trophies can benefit a lot from caching. Don't forget to enable *Use Caching* in the API settings.
148148

149149
## Unlock
150-
Unlocking a trophy will automatically show a notification. Future version of the API will allow you to turn this off.
150+
Unlocking a trophy will automatically show a notification. But note that if yo ucall this multiple times, you will get multiple notifications. See `TryUnlock` if you only want the notification once.
151151

152152
```
153153
GameJolt.API.Trophies.Unlock(trophyID, (bool success) => {
@@ -162,6 +162,24 @@ GameJolt.API.Trophies.Unlock(trophyID, (bool success) => {
162162
});
163163
```
164164

165+
### TryUnlock
166+
Similar to the normal `Unlock`, but this will only show a notification once and it will also tell you if the trophy was already unlocked.
167+
```
168+
GameJolt.API.Trophies.TryUnlock(trophyID, (TryUnlockResult result) => {
169+
switch(result) {
170+
case TryUnlockResult.Unlocked:
171+
Debug.Log("You've unlocked the trophy!");
172+
break;
173+
case TryUnlockResult.AlreadyUnlocked:
174+
Debug.Log("The trophy was already unlocked");
175+
break;
176+
case TryUnlockResult.Failure:
177+
Debug.LogError("Something went wrong!");
178+
break;
179+
}
180+
});
181+
```
182+
165183
## Show
166184
### Default UI
167185
It's as simple as a single line!

docs/annotated.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/class_game_jolt_1_1_a_p_i_1_1_auto_login_event.html

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/class_game_jolt_1_1_a_p_i_1_1_constants-members.html

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/class_game_jolt_1_1_a_p_i_1_1_constants.html

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)