AppLovinUpmManifest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #if UNITY_2019_2_OR_NEWER
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using AppLovinMax.ThirdParty.MiniJson;
  7. namespace AppLovinMax.Scripts.IntegrationManager.Editor
  8. {
  9. public class AppLovinUpmManifest
  10. {
  11. private const string KeyUrl = "url";
  12. private const string KeyName = "name";
  13. private const string KeyScopes = "scopes";
  14. private const string KeyScopedRegistry = "scopedRegistries";
  15. private Dictionary<string, object> manifest;
  16. private static string ManifestPath
  17. {
  18. get { return Path.Combine(Directory.GetCurrentDirectory(), "Packages/manifest.json"); }
  19. }
  20. // Private constructor to enforce the use of the Load() method
  21. private AppLovinUpmManifest() { }
  22. /// <summary>
  23. /// Creates a new instance of AppLovinUpmManifest and loads the manifest.json file.
  24. /// </summary>
  25. /// <returns>An instance of AppLovinUpmManifest</returns>
  26. public static AppLovinUpmManifest Load()
  27. {
  28. return new AppLovinUpmManifest { manifest = GetManifest() };
  29. }
  30. /// <summary>
  31. /// Adds or updates a scoped registry in the manifest.
  32. /// </summary>
  33. /// <param name="name">The name of the registry</param>
  34. /// <param name="url">The url of the registry</param>
  35. /// <param name="scopes">The scopes of the registry</param>
  36. public void AddOrUpdateRegistry(string name, string url, List<string> scopes)
  37. {
  38. var registry = GetRegistry(name);
  39. if (registry == null)
  40. {
  41. var registries = GetRegistries();
  42. if (registries == null) return;
  43. registries.Add(new Dictionary<string, object>
  44. {
  45. {KeyName, name},
  46. {KeyUrl, url},
  47. {KeyScopes, scopes}
  48. });
  49. return;
  50. }
  51. UpdateRegistry(registry, scopes);
  52. }
  53. /// <summary>
  54. /// Saves the manifest by serializing it back to JSON and writing to file.
  55. /// </summary>
  56. public void Save()
  57. {
  58. var content = Json.Serialize(manifest, true);
  59. File.WriteAllText(ManifestPath, content);
  60. }
  61. /// <summary>
  62. /// Adds a dependency to the manifest.
  63. /// </summary>
  64. /// <param name="packageName">The name of the package to add</param>
  65. /// <param name="version">The version of the package to add</param>
  66. public void AddPackageDependency(string packageName, string version)
  67. {
  68. var manifestDependencies = GetDependencies();
  69. manifestDependencies[packageName] = version;
  70. }
  71. /// <summary>
  72. /// Removes a dependency from the manifest.
  73. /// </summary>
  74. /// <param name="packageName">The name of the package to remove</param>
  75. public void RemovePackageDependency(string packageName)
  76. {
  77. var manifestDependencies = GetDependencies();
  78. manifestDependencies.Remove(packageName);
  79. }
  80. #region Utility
  81. /// <summary>
  82. /// Returns the manifest.json file as a dictionary.
  83. /// </summary>
  84. private static Dictionary<string, object> GetManifest()
  85. {
  86. if (!File.Exists(ManifestPath))
  87. {
  88. throw new Exception("Manifest not Found!");
  89. }
  90. var manifestJson = File.ReadAllText(ManifestPath);
  91. if (string.IsNullOrEmpty(manifestJson))
  92. {
  93. throw new Exception("Manifest is empty!");
  94. }
  95. var deserializedManifest = Json.Deserialize(manifestJson) as Dictionary<string, object>;
  96. if (deserializedManifest == null)
  97. {
  98. throw new Exception("Failed to deserialize manifest");
  99. }
  100. return deserializedManifest;
  101. }
  102. /// <summary>
  103. /// Gets the manifest's dependencies section.
  104. /// </summary>
  105. /// <returns>The dependencies section of the manifest.</returns>
  106. private Dictionary<string, object> GetDependencies()
  107. {
  108. var dependencies = manifest["dependencies"] as Dictionary<string, object>;
  109. if (dependencies == null)
  110. {
  111. throw new Exception("No dependencies found in manifest.");
  112. }
  113. return dependencies;
  114. }
  115. /// <summary>
  116. /// Gets the manifest's registries section. Creates a new registries section if one does not exist.
  117. /// </summary>
  118. /// <returns>The registries section of the manifest.</returns>
  119. private List<object> GetRegistries()
  120. {
  121. EnsureScopedRegistryExists();
  122. return manifest[KeyScopedRegistry] as List<object>;
  123. }
  124. /// <summary>
  125. /// Gets a scoped registry with the given name.
  126. /// </summary>
  127. /// <param name="name">The name of the registry</param>
  128. /// <returns>Returns the registry, or null if it can't be found</returns>
  129. private Dictionary<string, object> GetRegistry(string name)
  130. {
  131. var registries = GetRegistries();
  132. if (registries == null) return null;
  133. return registries
  134. .OfType<Dictionary<string, object>>()
  135. .FirstOrDefault(registry => MaxSdkUtils.GetStringFromDictionary(registry, KeyName).Equals(name));
  136. }
  137. /// <summary>
  138. /// Creates the section for scoped registries in the manifest.json file if it doesn't exist.
  139. /// </summary>
  140. private void EnsureScopedRegistryExists()
  141. {
  142. if (manifest.ContainsKey(KeyScopedRegistry)) return;
  143. manifest.Add(KeyScopedRegistry, new List<object>());
  144. }
  145. /// <summary>
  146. /// Updates a registry to make sure it contains the new scopes.
  147. /// </summary>
  148. /// <param name="registry">The registry to update</param>
  149. /// <param name="newScopes">The scopes we want added to the registry</param>
  150. private static void UpdateRegistry(Dictionary<string, object> registry, List<string> newScopes)
  151. {
  152. var scopes = MaxSdkUtils.GetListFromDictionary(registry, KeyScopes);
  153. if (scopes == null)
  154. {
  155. registry[KeyScopes] = new List<string>(newScopes);
  156. return;
  157. }
  158. // Only add scopes that are not already in the list
  159. var uniqueNewScopes = newScopes.Where(scope => !scopes.Contains(scope)).ToList();
  160. scopes.AddRange(uniqueNewScopes);
  161. }
  162. #endregion
  163. }
  164. }
  165. #endif