using System; using System.IO; using System.Linq; using SimpleJSON; using UnityEngine; using UnityEngine.UI; // VaM namespaces using MVRScript; public class LinkExtractor : MVRScript { private JSONStorableString _resultJSON; private UIDynamicButton _scanButton; private UIDynamicTextField _outputField; public override void Init() { try { // UI: Button and Text Field _scanButton = CreateButton("Scan for Hub Link"); _scanButton.button.onClick.AddListener(() => ExtractHubLink()); _outputField = CreateTextField(new JSONStorableString("Output", "")); _outputField.height = 200f; } catch (Exception e) { SuperController.LogError($"Init Error: {e}"); } } private void ExtractHubLink() { string resultMessage = "Link not found"; // 1. Get the current atom (character/prop) var atom = containingAtom; if (atom == null) return; // 2. Get the package UID (the .var file identifier) string packageUid = atom.GetStorableIDs() .Select(id => atom.GetStorableByID(id)) .FirstOrDefault(s => s != null && !string.IsNullOrEmpty(s.containingPackage?.uid))?.containingPackage?.uid; if (string.IsNullOrEmpty(packageUid)) { _outputField.text = "Could not determine package UID."; return; } // 3. Locate the meta.json file inside the package // Package paths: Saves/Packages/ or AddonPackages/ string[] searchPaths = { "Saves/Packages", "AddonPackages" }; string metaPath = null; foreach (var basePath in searchPaths) { var potentialPath = Path.Combine(SuperController.savesPath, "..", basePath, packageUid); var metaFilePath = Path.Combine(potentialPath, "meta.json"); if (File.Exists(metaFilePath)) { metaPath = metaFilePath; break; } } // 4. Parse JSON and look for "promotionalLink" or "link" field if (metaPath != null && File.Exists(metaPath)) { try { var jsonText = File.ReadAllText(metaPath); var jsonNode = JSON.Parse(jsonText); // Hub link is typically stored here: if (jsonNode["promotionalLink"] != null) resultMessage = jsonNode["promotionalLink"].Value; else if (jsonNode["link"] != null) resultMessage = jsonNode["link"].Value; if (resultMessage == "Link not found") resultMessage = "promotionalLink field missing."; } catch (Exception e) { resultMessage = $"Parse error: {e.Message}"; } } else { resultMessage = "meta.json not found in package."; } // Display result _outputField.text = resultMessage; SuperController.LogMessage($"Result: {resultMessage}"); } }