| using System; |
| using System; |
| using System.Collections.Generic; |
| using System.Collections.Generic; |
| using PlatformTools.Core; |
| using PlatformTools.Core; |
| |
| |
| public class PrivilegeManager |
| public class PrivilegeManager |
| { |
| { |
| public static UserType ToUserType(PrivilegeManager.Platform platform) |
| public static UserType ToUserType(PrivilegeManager.Platform platform) |
| { |
| { |
| if (platform == PrivilegeManager.Platform.Xbox) |
| if (platform == PrivilegeManager.Platform.Xbox) |
| { |
| { |
| return UserType.XboxLive; |
| return UserType.XboxLive; |
| } |
| } |
| return UserType.CrossNetworkUser; |
| return UserType.CrossNetworkUser; |
| } |
| } |
| |
| |
| public static bool HasPrivilegeData |
| public static bool HasPrivilegeData |
| { |
| { |
| get |
| get |
| { |
| { |
| return PrivilegeManager.privilegeData != null; |
| return PrivilegeManager.privilegeData != null; |
| } |
| } |
| } |
| } |
| |
| |
| public static ulong PlatformUserId |
| public static ulong PlatformUserId |
| { |
| { |
| get |
| get |
| { |
| { |
| if (PrivilegeManager.privilegeData != null) |
| if (PrivilegeManager.privilegeData != null) |
| { |
| { |
| return PrivilegeManager.privilegeData.Value.platformUserId; |
| return PrivilegeManager.privilegeData.Value.platformUserId; |
| } |
| } |
| ZLog.LogError("Can't get PlatformUserId before the privilege manager has been initialized!"); |
| ZLog.LogError("Can't get PlatformUserId before the privilege manager has been initialized!"); |
| return 0UL; |
| return 0UL; |
| } |
| } |
| } |
| } |
| |
| |
| public static void SetPrivilegeData(PrivilegeData privilegeData) |
| public static void SetPrivilegeData(PrivilegeData privilegeData) |
| { |
| { |
| if (privilegeData.platformCanAccess == null) |
| if (privilegeData.platformCanAccess == null) |
| { |
| { |
| string text = "The platformCanAccess delegate cannot be null!"; |
| string text = "The platformCanAccess delegate cannot be null!"; |
| ZLog.LogError(text); |
| ZLog.LogError(text); |
| throw new ArgumentException(text); |
| throw new ArgumentException(text); |
| } |
| } |
| . | |
| PrivilegeManager.s_networkUserId = null; |
| PrivilegeManager.privilegeData = new PrivilegeData?(privilegeData); |
| PrivilegeManager.privilegeData = new PrivilegeData?(privilegeData); |
| } |
| } |
| |
| |
| public static void ResetPrivilegeData() |
| public static void ResetPrivilegeData() |
| { |
| { |
| . | |
| PrivilegeManager.s_networkUserId = null; |
| PrivilegeManager.privilegeData = null; |
| PrivilegeManager.privilegeData = null; |
| } |
| } |
| |
| |
| public static string GetNetworkUserId() |
| public static string GetNetworkUserId() |
| { |
| { |
| . | return string.Format("{0}{1}", PrivilegeManager.GetPlatformPrefix(PrivilegeManager.GetCurrentPlatform()), PrivilegeManager.PlatformUserId); |
| if (string.IsNullOrEmpty(PrivilegeManager.s_networkUserId)) |
| |
| { |
| |
| PrivilegeManager.s_networkUserId = string.Format("{0}{1}", PrivilegeManager.GetPlatformPrefix(PrivilegeManager.GetCurrentPlatform()), PrivilegeManager.PlatformUserId); |
| |
| } |
| |
| return PrivilegeManager.s_networkUserId; |
| } |
| } |
| |
| |
| public static PrivilegeManager.Platform GetCurrentPlatform() |
| public static PrivilegeManager.Platform GetCurrentPlatform() |
| { |
| { |
| return PrivilegeManager.Platform.Steam; |
| return PrivilegeManager.Platform.Steam; |
| } |
| } |
| |
| |
| public static string GetPlatformName(PrivilegeManager.Platform platform) |
| public static string GetPlatformName(PrivilegeManager.Platform platform) |
| { |
| { |
| return string.Format("{0}", platform); |
| return string.Format("{0}", platform); |
| } |
| } |
| |
| |
| public static string GetPlatformPrefix(PrivilegeManager.Platform platform) |
| public static string GetPlatformPrefix(PrivilegeManager.Platform platform) |
| { |
| { |
| . | return PrivilegeManager.GetPlatformName(platform) + "_"; |
| string text; |
| |
| if (PrivilegeManager.s_platformPrefix.TryGetValue(platform, out text)) |
| |
| { |
| |
| return text; |
| |
| } |
| |
| text = PrivilegeManager.GetPlatformName(platform) + "_"; |
| |
| PrivilegeManager.s_platformPrefix.Add(platform, text); |
| |
| return text; |
| } |
| } |
| |
| |
| public static void FlushCache() |
| public static void FlushCache() |
| { |
| { |
| PrivilegeManager.Cache.Clear(); |
| PrivilegeManager.Cache.Clear(); |
| } |
| } |
| |
| |
| public static bool CanAccessOnlineMultiplayer |
| public static bool CanAccessOnlineMultiplayer |
| { |
| { |
| get |
| get |
| { |
| { |
| if (PrivilegeManager.privilegeData != null) |
| if (PrivilegeManager.privilegeData != null) |
| { |
| { |
| return PrivilegeManager.privilegeData.Value.canAccessOnlineMultiplayer; |
| return PrivilegeManager.privilegeData.Value.canAccessOnlineMultiplayer; |
| } |
| } |
| ZLog.LogError("Can't check \"CanAccessOnlineMultiplayer\" privilege before the privilege manager has been initialized!"); |
| ZLog.LogError("Can't check \"CanAccessOnlineMultiplayer\" privilege before the privilege manager has been initialized!"); |
| return false; |
| return false; |
| } |
| } |
| } |
| } |
| |
| |
| public static bool CanViewUserGeneratedContentAll |
| public static bool CanViewUserGeneratedContentAll |
| { |
| { |
| get |
| get |
| { |
| { |
| if (PrivilegeManager.privilegeData != null) |
| if (PrivilegeManager.privilegeData != null) |
| { |
| { |
| return PrivilegeManager.privilegeData.Value.canViewUserGeneratedContentAll; |
| return PrivilegeManager.privilegeData.Value.canViewUserGeneratedContentAll; |
| } |
| } |
| ZLog.LogError("Can't check \"CanViewUserGeneratedContentAll\" privilege before the privilege manager has been initialized!"); |
| ZLog.LogError("Can't check \"CanViewUserGeneratedContentAll\" privilege before the privilege manager has been initialized!"); |
| return false; |
| return false; |
| } |
| } |
| } |
| } |
| |
| |
| public static bool CanCrossplay |
| public static bool CanCrossplay |
| { |
| { |
| get |
| get |
| { |
| { |
| if (PrivilegeManager.privilegeData != null) |
| if (PrivilegeManager.privilegeData != null) |
| { |
| { |
| return PrivilegeManager.privilegeData.Value.canCrossplay; |
| return PrivilegeManager.privilegeData.Value.canCrossplay; |
| } |
| } |
| ZLog.LogError("Can't check \"CanCrossplay\" privilege before the privilege manager has been initialized!"); |
| ZLog.LogError("Can't check \"CanCrossplay\" privilege before the privilege manager has been initialized!"); |
| return false; |
| return false; |
| } |
| } |
| } |
| } |
| |
| |
| public static void CanViewUserGeneratedContent(string user, CanAccessResult canViewUserGeneratedContentResult) |
| public static void CanViewUserGeneratedContent(string user, CanAccessResult canViewUserGeneratedContentResult) |
| { |
| { |
| PrivilegeManager.CanAccess(PrivilegeManager.Permission.ViewTargetUserCreatedContent, user, canViewUserGeneratedContentResult); |
| PrivilegeManager.CanAccess(PrivilegeManager.Permission.ViewTargetUserCreatedContent, user, canViewUserGeneratedContentResult); |
| } |
| } |
| |
| |
| public static void CanCommunicateWith(string user, CanAccessResult canCommunicateWithResult) |
| public static void CanCommunicateWith(string user, CanAccessResult canCommunicateWithResult) |
| { |
| { |
| PrivilegeManager.CanAccess(PrivilegeManager.Permission.CommunicateUsingText, user, canCommunicateWithResult); |
| PrivilegeManager.CanAccess(PrivilegeManager.Permission.CommunicateUsingText, user, canCommunicateWithResult); |
| } |
| } |
| |
| |
| private static void CanAccess(PrivilegeManager.Permission permission, string platformUser, CanAccessResult canAccessResult) |
| private static void CanAccess(PrivilegeManager.Permission permission, string platformUser, CanAccessResult canAccessResult) |
| { |
| { |
| PrivilegeManager.User user = PrivilegeManager.ParseUser(platformUser); |
| PrivilegeManager.User user = PrivilegeManager.ParseUser(platformUser); |
| PrivilegeManager.PrivilegeLookupKey key = new PrivilegeManager.PrivilegeLookupKey(permission, user); |
| PrivilegeManager.PrivilegeLookupKey key = new PrivilegeManager.PrivilegeLookupKey(permission, user); |
| PrivilegeManager.Result result; |
| PrivilegeManager.Result result; |
| if (PrivilegeManager.Cache.TryGetValue(key, out result)) |
| if (PrivilegeManager.Cache.TryGetValue(key, out result)) |
| { |
| { |
| canAccessResult(result); |
| canAccessResult(result); |
| return; |
| return; |
| } |
| } |
| if (PrivilegeManager.privilegeData != null) |
| if (PrivilegeManager.privilegeData != null) |
| { |
| { |
| if (user.id == PrivilegeManager.PlatformUserId) |
| if (user.id == PrivilegeManager.PlatformUserId) |
| { |
| { |
| canAccessResult(PrivilegeManager.Result.Allowed); |
| canAccessResult(PrivilegeManager.Result.Allowed); |
| return; |
| return; |
| } |
| } |
| PrivilegeManager.privilegeData.Value.platformCanAccess(permission, user, delegate(PrivilegeManager.Result res) |
| PrivilegeManager.privilegeData.Value.platformCanAccess(permission, user, delegate(PrivilegeManager.Result res) |
| { |
| { |
| PrivilegeManager.CacheAndDeliverResult(res, canAccessResult, key); |
| PrivilegeManager.CacheAndDeliverResult(res, canAccessResult, key); |
| }); |
| }); |
| return; |
| return; |
| } |
| } |
| else |
| else |
| { |
| { |
| ZLog.LogError("Can't check \"" + permission.ToString() + "\" privilege before the privilege manager has been initialized!"); |
| ZLog.LogError("Can't check \"" + permission.ToString() + "\" privilege before the privilege manager has been initialized!"); |
| CanAccessResult canAccessResult2 = canAccessResult; |
| CanAccessResult canAccessResult2 = canAccessResult; |
| if (canAccessResult2 == null) |
| if (canAccessResult2 == null) |
| { |
| { |
| return; |
| return; |
| } |
| } |
| canAccessResult2(PrivilegeManager.Result.Failed); |
| canAccessResult2(PrivilegeManager.Result.Failed); |
| return; |
| return; |
| } |
| } |
| } |
| } |
| |
| |
| private static void CacheAndDeliverResult(PrivilegeManager.Result res, CanAccessResult canAccessResult, PrivilegeManager.PrivilegeLookupKey key) |
| private static void CacheAndDeliverResult(PrivilegeManager.Result res, CanAccessResult canAccessResult, PrivilegeManager.PrivilegeLookupKey key) |
| { |
| { |
| if (res != PrivilegeManager.Result.Failed) |
| if (res != PrivilegeManager.Result.Failed) |
| { |
| { |
| PrivilegeManager.Cache[key] = res; |
| PrivilegeManager.Cache[key] = res; |
| } |
| } |
| canAccessResult(res); |
| canAccessResult(res); |
| } |
| } |
| |
| |
| public static PrivilegeManager.User ParseUser(string platformUser) |
| public static PrivilegeManager.User ParseUser(string platformUser) |
| { |
| { |
| PrivilegeManager.User user = new PrivilegeManager.User(PrivilegeManager.Platform.Unknown, 0UL); |
| PrivilegeManager.User user = new PrivilegeManager.User(PrivilegeManager.Platform.Unknown, 0UL); |
| string[] array = platformUser.Split('_', StringSplitOptions.None); |
| string[] array = platformUser.Split('_', StringSplitOptions.None); |
| ulong num2; |
| ulong num2; |
| if (array.Length == 2) |
| if (array.Length == 2) |
| { |
| { |
| ulong num; |
| ulong num; |
| if (ulong.TryParse(array[1], out num)) |
| if (ulong.TryParse(array[1], out num)) |
| { |
| { |
| if (array[0] == PrivilegeManager.GetPlatformName(PrivilegeManager.Platform.Steam)) |
| if (array[0] == PrivilegeManager.GetPlatformName(PrivilegeManager.Platform.Steam)) |
| { |
| { |
| user = new PrivilegeManager.User(PrivilegeManager.Platform.Steam, num); |
| user = new PrivilegeManager.User(PrivilegeManager.Platform.Steam, num); |
| } |
| } |
| else if (array[0] == PrivilegeManager.GetPlatformName(PrivilegeManager.Platform.Xbox)) |
| else if (array[0] == PrivilegeManager.GetPlatformName(PrivilegeManager.Platform.Xbox)) |
| { |
| { |
| user = new PrivilegeManager.User(PrivilegeManager.Platform.Xbox, num); |
| user = new PrivilegeManager.User(PrivilegeManager.Platform.Xbox, num); |
| } |
| } |
| else if (array[0] == PrivilegeManager.GetPlatformName(PrivilegeManager.Platform.PlayFab)) |
| else if (array[0] == PrivilegeManager.GetPlatformName(PrivilegeManager.Platform.PlayFab)) |
| { |
| { |
| user = new PrivilegeManager.User(PrivilegeManager.Platform.PlayFab, num); |
| user = new PrivilegeManager.User(PrivilegeManager.Platform.PlayFab, num); |
| } |
| } |
| } |
| } |
| } |
| } |
| else if (array.Length == 1 && ulong.TryParse(array[0], out num2)) |
| else if (array.Length == 1 && ulong.TryParse(array[0], out num2)) |
| { |
| { |
| user = new PrivilegeManager.User(PrivilegeManager.Platform.Steam, num2); |
| user = new PrivilegeManager.User(PrivilegeManager.Platform.Steam, num2); |
| } |
| } |
| return user; |
| return user; |
| } |
| } |
| |
| |
| public static PrivilegeManager.Platform ParsePlatform(string platformString) |
| public static PrivilegeManager.Platform ParsePlatform(string platformString) |
| { |
| { |
| PrivilegeManager.Platform platform; |
| PrivilegeManager.Platform platform; |
| if (Enum.TryParse<PrivilegeManager.Platform>(platformString, out platform)) |
| if (Enum.TryParse<PrivilegeManager.Platform>(platformString, out platform)) |
| { |
| { |
| return platform; |
| return platform; |
| } |
| } |
| ZLog.LogError("Failed to parse platform!"); |
| ZLog.LogError("Failed to parse platform!"); |
| return PrivilegeManager.Platform.Unknown; |
| return PrivilegeManager.Platform.Unknown; |
| } |
| } |
| |
| |
| private static readonly Dictionary<PrivilegeManager.PrivilegeLookupKey, PrivilegeManager.Result> Cache = new Dictionary<PrivilegeManager.PrivilegeLookupKey, PrivilegeManager.Result>(); |
| private static readonly Dictionary<PrivilegeManager.PrivilegeLookupKey, PrivilegeManager.Result> Cache = new Dictionary<PrivilegeManager.PrivilegeLookupKey, PrivilegeManager.Result>(); |
| |
| |
| private static PrivilegeData? privilegeData; |
| private static PrivilegeData? privilegeData; |
| . | |
| |
| |
| private static string s_networkUserId; |
| |
| |
| |
| private static Dictionary<PrivilegeManager.Platform, string> s_platformPrefix = new Dictionary<PrivilegeManager.Platform, string>(); |
| |
| |
| public enum Platform |
| public enum Platform |
| { |
| { |
| Unknown, |
| Unknown, |
| Steam, |
| Steam, |
| Xbox, |
| Xbox, |
| PlayFab, |
| PlayFab, |
| None |
| None |
| } |
| } |
| |
| |
| public struct User |
| public struct User |
| { |
| { |
| public User(PrivilegeManager.Platform p, ulong i) |
| public User(PrivilegeManager.Platform p, ulong i) |
| { |
| { |
| this.platform = p; |
| this.platform = p; |
| this.id = i; |
| this.id = i; |
| } |
| } |
| |
| |
| public override string ToString() |
| public override string ToString() |
| { |
| { |
| return string.Format("{0}_{1}", this.platform, this.id); |
| return string.Format("{0}_{1}", this.platform, this.id); |
| } |
| } |
| |
| |
| public readonly PrivilegeManager.Platform platform; |
| public readonly PrivilegeManager.Platform platform; |
| |
| |
| public readonly ulong id; |
| public readonly ulong id; |
| } |
| } |
| |
| |
| public enum Result |
| public enum Result |
| { |
| { |
| Allowed, |
| Allowed, |
| NotAllowed, |
| NotAllowed, |
| Failed |
| Failed |
| } |
| } |
| |
| |
| public enum Permission |
| public enum Permission |
| { |
| { |
| CommunicateUsingText, |
| CommunicateUsingText, |
| ViewTargetUserCreatedContent |
| ViewTargetUserCreatedContent |
| } |
| } |
| |
| |
| private struct PrivilegeLookupKey |
| private struct PrivilegeLookupKey |
| { |
| { |
| internal PrivilegeLookupKey(PrivilegeManager.Permission p, PrivilegeManager.User u) |
| internal PrivilegeLookupKey(PrivilegeManager.Permission p, PrivilegeManager.User u) |
| { |
| { |
| this.permission = p; |
| this.permission = p; |
| this.user = u; |
| this.user = u; |
| } |
| } |
| |
| |
| internal readonly PrivilegeManager.Permission permission; |
| internal readonly PrivilegeManager.Permission permission; |
| |
| |
| internal readonly PrivilegeManager.User user; |
| internal readonly PrivilegeManager.User user; |
| } |
| } |
| } |
| } |
| |
| |