summaryrefslogtreecommitdiff
path: root/Runtime/Export/GameCenterServices.cs
blob: 2a140c31e4480c1ec01a5eeb928b46a1ad19ed88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace UnityEngine.SocialPlatforms.GameCenter
{
#if ENABLE_GAMECENTER
	using UnityEngine.SocialPlatforms.Impl;
	
	[StructLayout (LayoutKind.Sequential)]
	internal struct GcUserProfileData
	{
		public string userName;
		public string userID;
		public int isFriend;
		public Texture2D image;
		
		public UserProfile ToUserProfile()
		{
			return new UserProfile(userName, userID, (isFriend==1?true:false), UserState.Offline, image);
		}
		
		public void AddToArray(ref UserProfile[] array, int number)
		{
			if (array.Length > number && number >= 0)
				array[number] = ToUserProfile();
			else
				Debug.Log("Index number out of bounds when setting user data");
		}
	}

	[StructLayout (LayoutKind.Sequential)]
	internal struct GcAchievementDescriptionData
	{
		public string m_Identifier;
		public string m_Title;
		public Texture2D m_Image;
		public string m_AchievedDescription;
		public string m_UnachievedDescription;
		public int m_Hidden;
		public int m_Points;
		
		public AchievementDescription ToAchievementDescription()
		{
			return new AchievementDescription(
				m_Identifier, 
				m_Title, 
				m_Image, 
				m_AchievedDescription, 
				m_UnachievedDescription, 
				m_Hidden == 0 ? false : true,
				m_Points);
		}
	}

	[StructLayout (LayoutKind.Sequential)]
	internal struct GcAchievementData
	{
		public string m_Identifier;
		public double m_PercentCompleted;
		public int m_Completed;
		public int m_Hidden;
		public int m_LastReportedDate;
		
		public Achievement ToAchievement()
		{
			return new Achievement(
				m_Identifier, 
				m_PercentCompleted, 
				m_Completed == 0 ? false : true, 
				m_Hidden == 0 ? false : true, 
				new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(m_LastReportedDate));
		}
	}

	[StructLayout (LayoutKind.Sequential)]
	internal struct GcScoreData
	{
		public string m_Category;
		public int m_ValueLow;
		public int m_ValueHigh;
		public int m_Date;
		public string m_FormattedValue;
		public string m_PlayerID;
		public int m_Rank;
		
		public Score ToScore()
		{
			return new Score(
				m_Category, 
				(((Int64)m_ValueHigh) << 32) + m_ValueLow,
				m_PlayerID,
				new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(m_Date),
				m_FormattedValue, 
				m_Rank);
		}
	}
	
	public partial class GameCenterPlatform : ISocialPlatform
	{
		static Action<bool> s_AuthenticateCallback;
		static Action<bool> s_FriendsCallback;
		static Action<IAchievementDescription[]> s_AchievementDescriptionLoaderCallback;
		static Action<IAchievement[]> s_AchievementLoaderCallback;
		static Action<bool> s_ProgressCallback;
		static Action<bool> s_ScoreCallback;
		static Action<IScore[]> s_ScoreLoaderCallback;
		static Action<bool> s_LeaderboardCallback;
		static Action<IUserProfile[]> s_UsersCallback;
		static AchievementDescription[] s_adCache = new AchievementDescription[0];
		static UserProfile[] s_friends = new UserProfile[0];
		static UserProfile[] s_users = new UserProfile[0];
		static Action<bool> s_ResetAchievements;
		private static LocalUser m_LocalUser;
		private static List<GcLeaderboard> m_GcBoards = new List<GcLeaderboard>();
		
		static void ClearAchievementDescriptions(int size)
		{
			if (s_adCache == null || s_adCache.Length != size)
				s_adCache = new AchievementDescription[size];
		}
		
		static void SetAchievementDescription(GcAchievementDescriptionData data, int number)
		{
			s_adCache[number] = data.ToAchievementDescription();
		}
		
		static void SetAchievementDescriptionImage(Texture2D texture, int number)
		{
			if (s_adCache.Length <= number || number < 0)
			{
				Debug.Log("Achievement description number out of bounds when setting image");
				return;
			}
			s_adCache[number].SetImage(texture);
		}
		
		static void TriggerAchievementDescriptionCallback()
		{
			if (s_AchievementDescriptionLoaderCallback != null && s_adCache != null)
			{
				if (s_adCache.Length == 0)
					Debug.Log("No achivevement descriptions returned");
				s_AchievementDescriptionLoaderCallback(s_adCache);
			}
		}
		
		static void AuthenticateCallbackWrapper(int result)
		{
			if (s_AuthenticateCallback != null)
			{
				PopulateLocalUser();
				s_AuthenticateCallback((result == 1 ? true : false));
			}
		}
		
		static void ClearFriends(int size)
		{
			SafeClearArray(ref s_friends, size);
		}
		
		static void SetFriends(GcUserProfileData data, int number)
		{
			data.AddToArray(ref s_friends, number);
		}
		
		static void SetFriendImage(Texture2D texture, int number)
		{
			SafeSetUserImage(ref s_friends, texture, number);
		}
		
		static void TriggerFriendsCallbackWrapper(int result)
		{
			if (s_friends != null)
				m_LocalUser.SetFriends(s_friends);
			if (s_FriendsCallback != null)
				s_FriendsCallback((result == 1 ? true : false));
		}
		
		static void AchievementCallbackWrapper(GcAchievementData[] result)
		{
			if (s_AchievementLoaderCallback != null)
			{
				if (result.Length == 0)
					Debug.Log("No achivevements returned");
				Achievement[] migrated = new Achievement[result.Length];
				for (int i = 0; i < result.Length; ++i)
					migrated[i] = result[i].ToAchievement();
				s_AchievementLoaderCallback(migrated);
			}
		}
		
		static void ProgressCallbackWrapper(bool success)
		{
			if (s_ProgressCallback != null)
				s_ProgressCallback(success);
		}
		
		static void ScoreCallbackWrapper(bool success)
		{
			if (s_ScoreCallback != null)
				s_ScoreCallback(success);
		}
		
		static void ScoreLoaderCallbackWrapper(GcScoreData[] result)
		{
			if (s_ScoreLoaderCallback != null)
			{
				Score[] migrated = new Score[result.Length];
				for (int i = 0; i < result.Length; ++i)
					migrated[i] = result[i].ToScore();
				s_ScoreLoaderCallback(migrated);
			}
		}
		
		void ISocialPlatform.LoadFriends(ILocalUser user, Action<bool> callback)
		{
			s_FriendsCallback = callback;
			Internal_LoadFriends();
		}
					
		void ISocialPlatform.Authenticate(ILocalUser user, Action<bool> callback)
		{
			s_AuthenticateCallback = callback;
			Internal_Authenticate();
		}
		
		public ILocalUser localUser
		{
			get
			{
				if (m_LocalUser == null)
					m_LocalUser = new LocalUser();
				
				if (Internal_Authenticated() && m_LocalUser.id == "0")
					PopulateLocalUser();
				return m_LocalUser;
			}
		}
		
		private static void PopulateLocalUser()
		{
			m_LocalUser.SetAuthenticated(Internal_Authenticated());
			m_LocalUser.SetUserName(Internal_UserName());
			m_LocalUser.SetUserID(Internal_UserID());
			m_LocalUser.SetUnderage(Internal_Underage());
			m_LocalUser.SetImage(Internal_UserImage());
		}
		
		public void LoadAchievementDescriptions(Action<IAchievementDescription[]> callback)
		{
			if (!VerifyAuthentication())
			{
				callback(new AchievementDescription[0]);
				return;
			}
			s_AchievementDescriptionLoaderCallback = callback;
			Internal_LoadAchievementDescriptions();
		}
		
		// TODO: This doesn't really work with the static callback wrapper, multiple progresses
		// could be reported at the same time.
		public void ReportProgress(string id, double progress, Action<bool> callback)
		{
			if (!VerifyAuthentication())
			{
				callback(false);
				return;
			}
			s_ProgressCallback = callback;
			Internal_ReportProgress(id, progress);
		}
		
		public void LoadAchievements(Action<IAchievement[]> callback)
		{
			if (!VerifyAuthentication())
			{
				callback(new Achievement[0]);
				return;
			}
			s_AchievementLoaderCallback = callback;
			Internal_LoadAchievements();
		}
		
		public void ReportScore(Int64 score, string board, Action<bool> callback)
		{
			if (!VerifyAuthentication())
			{
				callback(false);
				return;
			}
			s_ScoreCallback = callback;
			Internal_ReportScore(score, board);
		}
		
		public void LoadScores(string category, Action<IScore[]> callback)
		{
			if (!VerifyAuthentication())
			{
				callback(new Score[0]);
				return;
			}
			s_ScoreLoaderCallback = callback;
			Internal_LoadScores(category);
		}
		
		public void LoadScores(ILeaderboard board, Action<bool> callback)
		{
			if (!VerifyAuthentication())
			{
				callback(false);
				return;
			}
			s_LeaderboardCallback = callback;
			Leaderboard genericBoard = (Leaderboard)board;
			GcLeaderboard gcBoard = new GcLeaderboard(genericBoard);
			m_GcBoards.Add(gcBoard);
			if (genericBoard.GetUserFilter().Length > 0)
				gcBoard.Internal_LoadScoresWithUsers(board.id, (int)board.timeScope, genericBoard.GetUserFilter());
			else
				gcBoard.Internal_LoadScores(board.id, board.range.from, board.range.count, (int)board.userScope, (int)board.timeScope);
		}
		
		static void LeaderboardCallbackWrapper(bool success)
		{
			if (s_LeaderboardCallback != null)
				s_LeaderboardCallback(success);
		}
		
		public bool GetLoading(ILeaderboard board)
		{
			if (!VerifyAuthentication()) return false;
			foreach (GcLeaderboard gcBoard in m_GcBoards)
				if (gcBoard.Contains((Leaderboard)board))
					return gcBoard.Loading();
			return false;
		}
		
		private bool VerifyAuthentication()
		{
			if (!localUser.authenticated)
			{
				Debug.Log ("Must authenticate first");
				return false;
			}
			return true;
		}
		
		public void ShowAchievementsUI()
		{
			Internal_ShowAchievementsUI();
		}
		
		public void ShowLeaderboardUI()
		{
			Internal_ShowLeaderboardUI();
		}
		
		static void ClearUsers(int size)
		{
			SafeClearArray(ref s_users, size);
		}
		
		static void SetUser(GcUserProfileData data, int number)
		{
			data.AddToArray(ref s_users, number);
		}
		
		static void SetUserImage(Texture2D texture, int number)
		{
			SafeSetUserImage(ref s_users, texture, number);
		}
		
		static void TriggerUsersCallbackWrapper()
		{
			if (s_UsersCallback != null)
				s_UsersCallback(s_users);
		}
		
		public void LoadUsers(string[] userIds, Action<IUserProfile[]> callback)
		{
			if (!VerifyAuthentication())
			{
				callback(new UserProfile[0]);
				return;
			}
			s_UsersCallback = callback;
			Internal_LoadUsers(userIds);
		}
		
		private static void SafeSetUserImage(ref UserProfile[] array, Texture2D texture, int number)
		{
			if (array.Length <= number || number < 0)
			{
				Debug.Log("Invalid texture when setting user image");
				texture = new Texture2D(76, 76);
			}
			if (array.Length > number && number >= 0)
				array[number].SetImage(texture);
			else
				Debug.Log("User number out of bounds when setting image");
		}
		
		private static void SafeClearArray(ref UserProfile[] array, int size)
		{
			if (array == null || array.Length != size)
				array = new UserProfile[size];
		}
		
		public ILeaderboard CreateLeaderboard()
		{
			Leaderboard lb = new Leaderboard();
			return (ILeaderboard)lb;
		}
		
		public IAchievement CreateAchievement()
		{
			Achievement achoo = new Achievement();
			return (IAchievement)achoo;
		}
		
		static void TriggerResetAchievementCallback(bool result)
		{
			if (s_ResetAchievements != null)
				s_ResetAchievements(result);
		}
		

	}
#else
		public class GameCenterPlatform : UnityEngine.SocialPlatforms.Local
		{
			static public void ResetAllAchievements(Action<bool> callback)
			{
				Debug.Log ("ResetAllAchievements - no effect in editor");
				callback(true);
			}
		
			static public void ShowDefaultAchievementCompletionBanner(bool value)
			{
				Debug.Log ("ShowDefaultAchievementCompletionBanner - no effect in editor");
			}
		
			static public void ShowLeaderboardUI(string leaderboardID, TimeScope timeScope)
			{
				Debug.Log ("ShowLeaderboardUI - no effect in editor");
			}
		}
#endif
}