using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; namespace Unity.Services.Mediation.Adapters.Editor { [Serializable] class MediationInfo { public MediationInfo() { SdkInfo = new SdkInfo(); Adapters = new AdapterInfo[0]; } public SdkInfo SdkInfo; public AdapterInfo[] Adapters; public override string ToString() { return $"MediationInfo: {EditorJsonUtility.ToJson(this)}"; } } [Serializable] class SdkInfo { /// /// SDK Identifier /// public string Identifier; /// /// SDK Display Name /// public string DisplayName; /// /// SDK Android artifact /// public string AndroidArtifact; /// /// SDK iOS Pod /// public string IosPod; /// /// Package version /// public string PackageVersion; /// /// Sdk version /// public string SdkVersion; } /// /// Information for a single Mediation Adapter /// [Serializable] class AdapterInfo { AdapterInfo() { } /// /// Identifier for this Adapter /// public string Identifier; /// /// Identifier for this Adapter's network on the unity dashboard /// public string DashboardId; /// /// Adapter Display Name /// public string DisplayName; /// /// Adapter Android artifact /// public string AndroidArtifact; /// /// Adapter iOS Pod /// public string IosPod; /// /// Installed Version for this Adapter /// public VersionInfo InstalledVersion; /// /// Versions Available for this Adapter /// public VersionInfo[] Versions; /// /// Repositories required for this Adapter /// public string[] Repositories; /// /// Returns Adapter Info /// /// string with adapter info public override string ToString() { return $"AdapterInfo: {EditorJsonUtility.ToJson(this)}"; } public override bool Equals(object obj) { if (!(obj is AdapterInfo other)) return false; return Equals(other); } protected bool Equals(AdapterInfo other) { return Identifier == other.Identifier && DashboardId == other.DashboardId && DisplayName == other.DisplayName && AndroidArtifact == other.AndroidArtifact && IosPod == other.IosPod && Equals(InstalledVersion, other.InstalledVersion) && ArrayUtility.ArrayEquals(Versions, other.Versions) && ArrayUtility.ArrayEquals(Repositories, other.Repositories); } public override int GetHashCode() { unchecked { var hashCode = (Identifier != null ? Identifier.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (DashboardId != null ? DashboardId.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (DisplayName != null ? DisplayName.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (AndroidArtifact != null ? AndroidArtifact.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (IosPod != null ? IosPod.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (InstalledVersion != null ? InstalledVersion.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Versions != null ? Versions.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (Repositories != null ? Repositories.GetHashCode() : 0); return hashCode; } } } /// /// Information for a single Mediation Adapter Version /// [Serializable] class VersionInfo { /// /// Default Identifier value for an adapter set to latest supported /// internal const string k_Latest = "latest"; /// /// Identifier for the version ie: 0.0.1 /// public string Identifier; /// /// Value to display to the user /// public string DisplayName; /// /// Returns optimistic version range for a given semantic type /// The format type /// The version being formatted /// Returns a formatted version in the optimistic form /// public static string OptimisticVersion(SemanticVersioningType type, string version) { var semanticVersioningFormatter = SemanticVersioningFactory.Formatter(type); return semanticVersioningFormatter.OptimisticVersion(version); } /// /// Returns true is the Identifier is set to latest supported /// public bool IsLatestSupported() { return Identifier == k_Latest; } /// /// Returns the presentation of the version required by the resolver xml for the given semantic versioning type /// The type of format /// Returns the version adhering to the given format /// public string Version(SemanticVersioningType type) { var semanticVersioningFormatter = SemanticVersioningFactory.Formatter(type); return IsLatestSupported() ? semanticVersioningFormatter.LatestVersionIdentifier() : Identifier; } public override bool Equals(object obj) { if (!(obj is VersionInfo other)) return false; return Equals(other); } protected bool Equals(VersionInfo other) { return Identifier == other.Identifier; } public override int GetHashCode() { return (Identifier != null ? Identifier.GetHashCode() : 0); } } /// /// Holds and Manages Adapter Information for Unity Mediation /// static class MediationSdkInfo { const string k_InfoFile = "MediationSdkInfo.json"; internal static MediationInfo s_MediationInfo; internal static AdaptersDependencyGenerator s_Generator = new AdaptersDependencyGenerator(); public static event Action AdaptersChanged { add => s_Generator.AdaptersChanged += value; remove => s_Generator.AdaptersChanged -= value; } static void LoadSdkInfoIfNeeded() { if (s_MediationInfo == null) { var infoPath = FindMediationSdkInfoPath(); if (infoPath == null) { throw new FileNotFoundException($"Can't find {k_InfoFile}"); } var infoJson = File.ReadAllText(infoPath); s_MediationInfo = new MediationInfo(); EditorJsonUtility.FromJsonOverwrite(infoJson, s_MediationInfo); } } internal static string FindMediationSdkInfoPath() { // Search for file in these folders. // TODO: search only in mediation after release var searchFolders = new[] { "Packages/com.unity.services.mediation", "Packages/com.unity.accelerate", "Assets" }; string sdkInfoPath = null; foreach (var folder in searchFolders) { try { sdkInfoPath = Directory.GetFiles(folder, k_InfoFile, SearchOption.AllDirectories).FirstOrDefault(); if (sdkInfoPath != null) { break; } } catch (Exception) { // ignored } } return sdkInfoPath; } public static SdkInfo GetSdkInfo() { LoadSdkInfoIfNeeded(); return s_MediationInfo.SdkInfo; } /// /// Get a list of all available adapters compatible with the current Mediation SDK /// /// Returns a List of AdapterInfo public static List GetAllAdapters() { LoadSdkInfoIfNeeded(); return new List(s_MediationInfo.Adapters); } /// /// Get a list of adapters currently in use with Mediation SDK /// /// Returns a List of AdapterInfo public static List GetInstalledAdapters(bool generateXml = true) { return s_Generator.GetInstalledAdapters(generateXml); } /// /// Installs or updates an adapter to specified version. /// If the adapter is already installed under a different version, the version will be updated. /// If version is not specified, latest available version will be installed. /// /// The identifier of the adapter /// Version of the adapter /// Thrown if combination of identifier/version is not valid public static void Install(string identifier, VersionInfo versionInfo = null) { s_Generator.InstallAdapter(identifier, versionInfo); } /// /// Remove an existing adapter from Mediation. /// If adapter is already uninstalled, no action will take place. /// /// The identifier of the adapter /// Thrown if identifier is not valid public static void Uninstall(string identifier) { s_Generator.UninstallAdapter(identifier); } /// /// Apply changes. /// Will cause the xml dependency file to be imported. /// If resolve is set to true, dependency resolution will be triggered. /// public static void Apply(bool resolve = false) { s_Generator.Apply(resolve); } } }