Add project files.
This commit is contained in:
58
ModVersionChecker/utils/VersionUtils.cs
Normal file
58
ModVersionChecker/utils/VersionUtils.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user