Add project files.
This commit is contained in:
54
ModVersionChecker/utils/TimeUtils.cs
Normal file
54
ModVersionChecker/utils/TimeUtils.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ModVersionChecker.utils
|
||||
{
|
||||
public class TimeUtils
|
||||
{
|
||||
public static long GetUnixTimeMillis(DateTime? dateTime)
|
||||
{
|
||||
DateTime dt = dateTime ?? DateTime.UtcNow;
|
||||
return (long)(dt - new DateTime(1970, 1, 1)).TotalMilliseconds;
|
||||
}
|
||||
|
||||
public static DateTime FromUnixTimeMillis(long unixTimeMillis)
|
||||
{
|
||||
return new DateTime(1970, 1, 1).AddMilliseconds(unixTimeMillis);
|
||||
}
|
||||
|
||||
public static string ToFriendlyTime(long millisecods)
|
||||
{
|
||||
DateTime dateTime = FromUnixTimeMillis(millisecods);
|
||||
return ToFriendlyTime(dateTime);
|
||||
}
|
||||
|
||||
public static string ToFriendlyTime(DateTime dateTime)
|
||||
{
|
||||
// Use UTC for consistency with LiteDB if needed
|
||||
DateTime now = DateTime.UtcNow;
|
||||
|
||||
|
||||
TimeSpan span = now - dateTime;
|
||||
|
||||
// Handle future dates (optional)
|
||||
if (span.TotalSeconds < 0)
|
||||
return "In the future";
|
||||
|
||||
if (span.TotalSeconds < 60)
|
||||
return $"{(int)span.TotalSeconds} seconds ago";
|
||||
if (span.TotalMinutes < 60)
|
||||
return $"{(int)span.TotalMinutes} minute{(span.TotalMinutes < 2 ? "" : "s")} ago";
|
||||
if (span.TotalHours < 24)
|
||||
return $"{(int)span.TotalHours} hour{(span.TotalHours < 2 ? "" : "s")} ago";
|
||||
if (span.TotalDays < 30)
|
||||
return $"{(int)span.TotalDays} day{(span.TotalDays < 2 ? "" : "s")} ago";
|
||||
if (span.TotalDays < 365)
|
||||
return $"{(int)(span.TotalDays / 30)} month{(span.TotalDays / 30 < 2 ? "" : "s")} ago";
|
||||
|
||||
return $"{(int)(span.TotalDays / 365)} year{(span.TotalDays / 365 < 2 ? "" : "s")} ago";
|
||||
}
|
||||
}
|
||||
}
|
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