58 lines
1.9 KiB
C#
58 lines
1.9 KiB
C#
using ModVersionChecker.data.model;
|
|
using System.Text.Json;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace ModVersionChecker.utils
|
|
{
|
|
public static class VersionUtils
|
|
{
|
|
public static string GetCurrentVersion(AppConfig app, FsModPathConfig config)
|
|
{
|
|
var versionConfig = app.FsFields;
|
|
var packageName = versionConfig["msfs2024"]["package"];
|
|
var fsPath = config.Path;
|
|
var fsFile = config.File;
|
|
var fsFileType = config.FileType;
|
|
var fsKey = config.Key;
|
|
|
|
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}");
|
|
}
|
|
}
|
|
}
|
|
} |