using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEditor.PackageManager; using System.IO; using Unity.Plastic.Newtonsoft.Json; using System; using Unity.Plastic.Newtonsoft.Json.Linq; namespace HIKKY.VketCloudSDK.AddOn { public class ExternalPackageImporter { public static bool SetPackageVersion(string packageName, string newVersion) { string path = Path.Combine(Application.dataPath, "..", "Packages", "manifest.json"); if (File.Exists(path)) { string jsonText = File.ReadAllText(path); try { JObject json = JObject.Parse(jsonText); JToken dependencies = json["dependencies"]; if (dependencies[packageName] != null) { dependencies[packageName] = newVersion; File.WriteAllText(path, json.ToString()); return true; } else { UnityEngine.Debug.LogError($"Package {packageName} not found."); } } catch (JsonException e) { UnityEngine.Debug.LogError("Failed to parse or update manifest.json: " + e.Message); } } else { UnityEngine.Debug.LogError("manifest.json not found."); } return false; } public static void AddScopedRegistry(ScopedRegistry pScopeRegistry, string packageName, string version) { var manifestPath = Path.Combine(Application.dataPath, "..", "Packages/manifest.json"); var manifestJson = File.ReadAllText(manifestPath); var manifest = JsonConvert.DeserializeObject(manifestJson); if (manifest.scopedRegistries.Find(n => n.name == pScopeRegistry.name) == null) { Debug.Log($"{pScopeRegistry.name}_Importing"); manifest.scopedRegistries.Add(pScopeRegistry); // Add or update the package in the dependencies if (manifest.dependencies.ContainsKey(packageName)) { manifest.dependencies[packageName] = version; } else { manifest.dependencies.Add(packageName, version); } File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifest, Formatting.Indented)); } else { return; } //foreach (var scope in pScopeRegistry.scopes) // Client.Add(scope); AssetDatabase.Refresh(); } public class ScopedRegistry { public string name; public string url; public string[] scopes; } public class ManifestJson { public Dictionary dependencies = new Dictionary(); public List scopedRegistries = new List(); } } }