using ModVersionChecker.model; using System.Text.Json; using System.Text.RegularExpressions; namespace ModVersionChecker.utils { public static class VersionUtils { public static string GetCurrentVersion(App app, TypeConfig typeConfig) { var versionConfig = app.Fields; var packageName = versionConfig["package"]; var fsPath = typeConfig.ConfigValues.GetValueOrDefault("path", ""); var fsFile = typeConfig.ConfigValues.GetValueOrDefault("filename"); var fsFileType = typeConfig.ConfigValues.GetValueOrDefault("filetype", ""); var fsKey = typeConfig.ConfigValues.GetValueOrDefault("jsonkey", ""); var filePath = Path.GetFullPath(Path.Combine(fsPath, packageName, fsFile)); if (!File.Exists(filePath)) { return ""; // Fallback } try { var content = File.ReadAllText(filePath).Trim(); if (string.IsNullOrEmpty(content)) { throw new Exception($"Empty file: {filePath}"); } using var jsonDoc = JsonDocument.Parse(content); var element = jsonDoc.RootElement; foreach (var key in fsKey.Split('.')) { if (!element.TryGetProperty(key, out var nextElement)) { throw new Exception($"JSON key '{key}' not found in {filePath}"); } element = nextElement; } if (element.ValueKind != JsonValueKind.String) { throw new Exception($"JSON value for '{fsKey}' is not a string in {filePath}"); } var version = element.GetString()!; return version; } catch (Exception ex) { throw new Exception($"Error reading or processing file '{filePath}': {ex.Message}"); } } } }