using System; using System.Threading; using UnityEditor; using UnityEditor.PackageManager; using UnityEditor.PackageManager.Requests; using UnityEngine; namespace TapjoyEditor { public static class EDMResolver { const string k_UnityMediationPackage = "com.unity.services.mediation"; const string k_EDMPackageName = "com.google.external-dependency-manager"; const string k_DoNotAskAgain = "Unity.Mediation.MobileDependencyResolver.DoNotAskAgain"; const string k_DialogTitle = "External Dependency Manager required"; const string k_DialogText = "Offerwall requires External Dependency Manager to resolve native dependencies.\n" + "Would you like to install it via Package Manager?"; const string k_ButtonInstall = "Install"; const string k_ButtonCancel = "Cancel"; const string k_ButtonDontAskAgain = "Ignore - Do not ask again"; static readonly string[] k_EDMSelectableVersions = new[] { "1.2.183", "1.2.182" }; public static string SelectedVersion { get; set; } static AddRequest s_AddRequest; [InitializeOnLoadMethod] static void LoadEditorCallback() { EditorApplication.delayCall += PromptAndInstallEdmIfNeeded; } public static void PromptAndInstallEdmIfNeeded() { if (IsPackageInManifest(k_UnityMediationPackage)) return; if (Type.GetType("Google.VersionHandler, Google.VersionHandler") != null) return; if (EditorPrefs.GetBool(k_DoNotAskAgain)) return; int result = EditorUtility.DisplayDialogComplex( k_DialogTitle, k_DialogText, k_ButtonInstall, k_ButtonCancel, k_ButtonDontAskAgain); switch (result) { case 0: EDMSelectionEditorWindow.ShowWindow(selectedVersion => { SelectedVersion = selectedVersion; if (string.IsNullOrEmpty(SelectedVersion)) return; EditorApplication.delayCall += () => { StartUpmInstallWithVersion(SelectedVersion); }; }, k_EDMSelectableVersions); break; case 1: break; case 2: EditorPrefs.SetBool(k_DoNotAskAgain, true); break; } } static bool IsPackageInManifest(string pkgName) { try { var manifest = System.IO.File.ReadAllText("Packages/manifest.json"); return manifest.Contains($"\"{pkgName}\""); } catch (Exception) { Debug.LogWarning("[Tapjoy] Could not read Packages/manifest.json"); return false; } } static void StartUpmInstallWithVersion(string versionOrTag) { if (string.IsNullOrEmpty(versionOrTag)) return; EditorApplication.quitting += () => EditorPrefs.DeleteKey(k_DoNotAskAgain); var gitUrl = BuildEdmGitUrl(versionOrTag); s_AddRequest = Client.Add(gitUrl); EditorApplication.update += Progress; } static string BuildEdmGitUrl(string versionOrTag) { if (string.IsNullOrEmpty(versionOrTag)) { var fallbackVersion = (k_EDMSelectableVersions != null && k_EDMSelectableVersions.Length > 0) ? k_EDMSelectableVersions[0] : null; if (string.IsNullOrEmpty(fallbackVersion)) throw new InvalidOperationException("No selectable EDM version is available."); versionOrTag = fallbackVersion; } var tag = versionOrTag.StartsWith("v", StringComparison.Ordinal) ? versionOrTag : "v" + versionOrTag; return "https://github.com/googlesamples/unity-jar-resolver.git?path=/upm#" + tag; } static void Progress() { if (!s_AddRequest.IsCompleted) return; EditorApplication.update -= Progress; if (s_AddRequest.Status == StatusCode.Success) { Debug.Log($"[Tapjoy] External Dependency Manager installed: {s_AddRequest.Result.packageId}"); } else { string err = s_AddRequest.Error?.message ?? "Unknown error"; Debug.LogError($"[Tapjoy] Failed to install EDM: {err}"); EditorUtility.DisplayDialog("EDM Install Failed", err, "OK"); } } public static int InstallEdmViaUpmHeadless() { if (IsPackageInManifest(k_EDMPackageName) || Type.GetType("Google.VersionHandler, Google.VersionHandler") != null) { Debug.Log("[Tapjoy] EDM already present. Skipping UPM install."); return 0; } AddRequest request; try { var headlessGitUrl = BuildEdmGitUrl(null); request = Client.Add(headlessGitUrl); } catch (Exception ex) { Debug.LogError($"[Tapjoy] Exception starting EDM UPM install: {ex}"); EditorApplication.Exit(1); return 1; } var start = DateTime.UtcNow; const int timeoutSeconds = 300; while (!request.IsCompleted) { if ((DateTime.UtcNow - start).TotalSeconds > timeoutSeconds) { Debug.LogError("[Tapjoy] EDM UPM install timed out."); EditorApplication.Exit(1); return 1; } Thread.Sleep(100); } if (request.Status == StatusCode.Success) { Debug.Log($"[Tapjoy] External Dependency Manager installed via UPM: {request.Result.packageId}"); return 0; } else { string err = request.Error?.message ?? "Unknown error"; Debug.LogError($"[Tapjoy] Failed to install EDM via UPM: {err}"); EditorApplication.Exit(1); return 1; } } } }