Add project files.

This commit is contained in:
Jose Conde
2025-09-04 10:14:30 +02:00
parent a7a404148c
commit 94e6ef651e
54 changed files with 3134 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
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}");
}
}
}
}