|
| 1 | +using System; |
| 2 | +using UnityEngine; |
| 3 | +using UnityEditor; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +#if UNITY_EDITOR |
| 7 | +namespace LootLocker.Admin |
| 8 | +{ |
| 9 | + |
| 10 | + [ExecuteInEditMode] |
| 11 | + public class AttributionHandler : MonoBehaviour |
| 12 | + { |
| 13 | + [InitializeOnLoadMethod] |
| 14 | + static void SubscribeToEventOnStartup() |
| 15 | + { |
| 16 | + // Only subscribe if needed |
| 17 | + if (EditorPrefs.GetBool("attributionChecked") == false) |
| 18 | + { |
| 19 | + ProjectSettings.APIKeyEnteredEvent += EventFired; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + private static void EventFired() |
| 24 | + { |
| 25 | + // Only in Editor |
| 26 | + if(Application.isPlaying) |
| 27 | + { |
| 28 | + return; |
| 29 | + } |
| 30 | + // New ServerManager |
| 31 | + LootLockerServerManager.CheckInit(); |
| 32 | + |
| 33 | + // Disable logging |
| 34 | + LootLockerConfig.current.currentDebugLevel = LootLockerConfig.DebugLevel.Off; |
| 35 | + |
| 36 | + string apiKey = string.IsNullOrWhiteSpace(LootLockerConfig.current.apiKey) ? "invalidGameAPIKey" : LootLockerConfig.current.apiKey; |
| 37 | + SendAttributionEvent(apiKey, (response) => { |
| 38 | + if(response.success) |
| 39 | + { |
| 40 | + // Send attribution event |
| 41 | + UnityEditor.VSAttribution.VSAttribution.SendAttributionEvent("Entered API Key", "LootLocker", response.hash); |
| 42 | + |
| 43 | + DestroyImmediate(LootLockerServerManager.I.gameObject); |
| 44 | + |
| 45 | + // Unsubscribe |
| 46 | + ProjectSettings.APIKeyEnteredEvent -= EventFired; |
| 47 | + // Re-enable logging |
| 48 | + LootLockerConfig.current.currentDebugLevel = LootLockerConfig.DebugLevel.All; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + DestroyImmediate(LootLockerServerManager.I.gameObject); |
| 53 | + |
| 54 | + // Unsubscribe |
| 55 | + ProjectSettings.APIKeyEnteredEvent -= EventFired; |
| 56 | + // Re-enable logging |
| 57 | + LootLockerConfig.current.currentDebugLevel = LootLockerConfig.DebugLevel.All; |
| 58 | + |
| 59 | + // Resubscribe in 1 seconds |
| 60 | + Task task = ResetAttributionCheckAfterXSeconds(1); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + private static async Task ResetAttributionCheckAfterXSeconds(int seconds) |
| 66 | + { |
| 67 | + await Task.Delay((int)(seconds*1000f)); |
| 68 | + |
| 69 | + EditorPrefs.SetBool("attributionChecked", false); |
| 70 | + ProjectSettings.APIKeyEnteredEvent += EventFired; |
| 71 | + } |
| 72 | + |
| 73 | + public static void SendAttributionEvent(string gameApiKey, Action<AttributionResponse> onComplete) |
| 74 | + { |
| 75 | + VerifyAttributionRequest verifyRequest = new VerifyAttributionRequest(gameApiKey); |
| 76 | + LootLockerAPIManager.Verify(verifyRequest, onComplete); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public class AttributionResponse : LootLockerResponse |
| 81 | + { |
| 82 | + public string hash; |
| 83 | + } |
| 84 | + public class VerifyAttributionRequest |
| 85 | + { |
| 86 | + public string game_key; |
| 87 | + public VerifyAttributionRequest(string gameApiKey) |
| 88 | + { |
| 89 | + this.game_key = gameApiKey; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public partial class LootLockerAPIManager |
| 94 | + { |
| 95 | + public static void Verify(VerifyAttributionRequest data, Action<AttributionResponse> onComplete) |
| 96 | + { |
| 97 | + string json = ""; |
| 98 | + if (data == null) return; |
| 99 | + else json = LootLockerJson.SerializeObject(data); |
| 100 | + |
| 101 | + EndPointClass endPoint = new EndPointClass("game/attribution/unity", LootLockerHTTPMethod.POST); |
| 102 | + |
| 103 | + LootLockerServerRequest.CallAPI(endPoint.endPoint, endPoint.httpMethod, json, (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); }, false, LootLockerEnums.LootLockerCallerRole.Base); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | +#endif |
0 commit comments