This commit is contained in:
Chomp
2025-01-14 11:53:56 +00:00
11 changed files with 120 additions and 185 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ public class WeatherHelper
/// </summary>
/// <param name="currentDate">(new Date())</param>
/// <returns>Date object of current in-raid time</returns>
public DateTime GetInRaidTime(double? timestamp = null)
public DateTime GetInRaidTime(long? timestamp = null)
{
// tarkov time = (real time * 7 % 24 hr) + 3 hour
var russiaOffsetMilliseconds = _timeUtil.GetHoursAsSeconds(3) * 1000;
+10 -7
View File
@@ -14,16 +14,19 @@ public class ConfigServer
{
protected ILogger _logger;
protected JsonUtil _jsonUtil;
protected FileUtil _fileUtil;
protected Dictionary<string, object> configs = new();
protected readonly string[] acceptableFileExtensions = [".json", ".jsonc"];
protected readonly string[] acceptableFileExtensions = ["json", "jsonc"];
public ConfigServer(
ILogger logger,
JsonUtil jsonUtil
JsonUtil jsonUtil,
FileUtil fileUtil
)
{
_logger = logger;
_jsonUtil = jsonUtil;
_fileUtil = fileUtil;
Initialize();
}
@@ -45,13 +48,13 @@ public class ConfigServer
// Get all filepaths
var filepath = "./assets/configs/";
var files = Directory.GetFiles(filepath);
var files = _fileUtil.GetFiles(filepath);
// Add file content to result
foreach (var file in files)
if (acceptableFileExtensions.Contains(Path.GetExtension(file)))
if (acceptableFileExtensions.Contains(_fileUtil.GetFileExtension(file)))
{
var fileContent = File.ReadAllText(file);
var fileContent = _fileUtil.ReadFile(file);
var type = GetConfigTypeByFilename(file);
var deserializedContent = _jsonUtil.Deserialize(fileContent, type);
@@ -61,7 +64,7 @@ public class ConfigServer
throw new Exception($"Server will not run until the: {file} config error mentioned above is fixed");
}
configs[$"spt-{Path.GetFileNameWithoutExtension(file)}"] = deserializedContent;
configs[$"spt-{_fileUtil.StripExtension(file)}"] = deserializedContent;
}
/** TODO: deal with this:
@@ -77,7 +80,7 @@ public class ConfigServer
private Type GetConfigTypeByFilename(string filename)
{
var type = Enum.GetValues<ConfigTypes>()
.First(en => en.GetValue().Contains(Path.GetFileNameWithoutExtension(filename)));
.First(en => en.GetValue().Contains(_fileUtil.StripExtension(filename)));
return type.GetConfigType();
}
}
+39 -13
View File
@@ -1,10 +1,8 @@
using System.Text.Json;
using Core.Annotations;
using Core.Utils;
using Core.Utils;
using Core.Utils.Extensions;
namespace Core.Services;
[Injectable(InjectionType.Singleton)]
public class I18nService
{
private List<string> _locales;
@@ -12,29 +10,39 @@ public class I18nService
private string _defaultLocale;
private string _directory;
private JsonUtil _jsonUtil;
private FileUtil _fileUtil;
private string _setLocale;
private Dictionary<string, Dictionary<string, string>> _loadedLocales = new();
public I18nService(JsonUtil jsonUtil, List<string> locales, Dictionary<string, string> fallbacks, string defaultLocale, string directory)
public I18nService(
FileUtil fileUtil,
JsonUtil jsonUtil,
List<string> locales,
Dictionary<string, string> fallbacks,
string defaultLocale,
string directory
)
{
_locales = locales;
_fallbacks = fallbacks;
_defaultLocale = defaultLocale;
_directory = directory;
_jsonUtil = jsonUtil;
_fileUtil = fileUtil;
Initialize();
}
private void Initialize()
{
var files = Directory.GetFiles(_directory, "*.json");
if (files.Length == 0)
var files = _fileUtil.GetFiles(_directory, true).Where(f => _fileUtil.GetFileExtension(f) == "json").ToList();
if (files.Count == 0)
throw new Exception($"Localisation files in directory {_directory} not found.");
foreach (var file in files)
_loadedLocales.Add(Path.GetFileNameWithoutExtension(file),
_jsonUtil.Deserialize<Dictionary<string, string>>(File.ReadAllText(file)) ?? new Dictionary<string, string>());
_loadedLocales.Add(_fileUtil.StripExtension(file),
_jsonUtil.Deserialize<Dictionary<string, string>>(_fileUtil.ReadFile(file)) ??
new Dictionary<string, string>());
if (!_loadedLocales.ContainsKey(_defaultLocale))
throw new Exception($"The default locale '{_defaultLocale}' does not exist on the loaded locales.");
@@ -53,7 +61,8 @@ public class I18nService
{
var foundFallbackLocale = fallback.First().Value;
if (!_loadedLocales.ContainsKey(foundFallbackLocale))
throw new Exception($"Locale '{locale}' was not defined, and the found fallback locale did not match any of the loaded locales.");
throw new Exception(
$"Locale '{locale}' was not defined, and the found fallback locale did not match any of the loaded locales.");
_setLocale = foundFallbackLocale;
}
@@ -75,9 +84,26 @@ public class I18nService
return value;
}
public string GetLocalised(string key, params object[] args)
public string GetLocalised(string key, object? args)
{
// TODO: Deal with arguments
return GetLocalised(key);
var rawLocalizedString = GetLocalised(key);
if (args == null)
return rawLocalizedString;
if (args is string value)
{
return rawLocalizedString.Replace("%s", value);
}
else
{
foreach (var propertyInfo in args.GetType().GetProperties())
{
var localizedName = $"{{{{{propertyInfo.GetJsonName()}}}}}";
if (rawLocalizedString.Contains(localizedName))
{
rawLocalizedString.Replace(localizedName, propertyInfo.GetValue(args, null)?.ToString() ?? string.Empty);
}
}
return rawLocalizedString;
}
}
}
+3 -1
View File
@@ -19,7 +19,8 @@ public class LocalisationService
RandomUtil randomUtil,
DatabaseServer databaseServer,
LocaleService localeService,
JsonUtil jsonUtil
JsonUtil jsonUtil,
FileUtil fileUtil
)
{
_logger = logger;
@@ -27,6 +28,7 @@ public class LocalisationService
_databaseServer = databaseServer;
_localeService = localeService;
_i18nService = new I18nService(
fileUtil,
jsonUtil,
localeService.GetServerSupportedLocales(),
localeService.GetLocaleFallbacks(),
+5
View File
@@ -25,6 +25,11 @@ public class FileUtil
return Path.GetExtension(path).Replace(".", "");
}
public string GetFileName(string path)
{
return Path.GetFileName(path);
}
public string StripExtension(string path, bool keepPath = false)
{
return keepPath ? path.Split('.').First() : Path.GetFileNameWithoutExtension(path);
+6 -119
View File
@@ -43,18 +43,16 @@ public class ImporterUtil
foreach (var file in files)
{
if (_fileUtil.GetFileExtension(file) != "json") continue;
if (filesToIgnore.Contains(Path.GetFileName(file).ToLower())) continue;
if (filesToIgnore.Contains(_fileUtil.GetFileName(file).ToLower())) continue;
tasks.Add(
Task.Factory.StartNew(() =>
{
// const filename = this.vfs.stripExtension(file);
// const filePathAndName = `${filepath}${file}`;
var fileData = File.ReadAllText(file);
var fileData = _fileUtil.ReadFile(file);
if (onReadCallback != null)
onReadCallback(file, fileData);
var setMethod = GetSetMethod(
Path.GetFileNameWithoutExtension(file).ToLower(),
_fileUtil.StripExtension(file).ToLower(),
loadedType,
out var propertyType,
out var isDictionary
@@ -66,7 +64,7 @@ public class ImporterUtil
onObjectDeserialized(file, fileDeserialized);
setMethod.Invoke(result,
isDictionary ? [Path.GetFileNameWithoutExtension(file), fileDeserialized] : [fileDeserialized]);
isDictionary ? [_fileUtil.StripExtension(file), fileDeserialized] : [fileDeserialized]);
}
catch (Exception e)
{
@@ -116,124 +114,13 @@ public class ImporterUtil
{
var matchedProperty = type.GetProperties()
.FirstOrDefault(prop =>
prop.Name.ToLower() == Path.GetFileNameWithoutExtension(propertyName).ToLower());
prop.Name.ToLower() == _fileUtil.StripExtension(propertyName).ToLower());
if (matchedProperty == null)
throw new Exception(
$"Unable to find property '{Path.GetFileNameWithoutExtension(propertyName)}' for type '{type.Name}'");
$"Unable to find property '{_fileUtil.StripExtension(propertyName)}' for type '{type.Name}'");
propertyType = matchedProperty.PropertyType;
setMethod = matchedProperty.GetSetMethod();
}
return setMethod;
}
/**
* Load files into js objects recursively (synchronous)
* @param filepath Path to folder with files
* @returns
*/
public object LoadRecursive(
string filepath,
Type loadedType,
Action<string, string>? onReadCallback = null,
Action<string, object>? onObjectDeserialized = null
)
{
var result = Activator.CreateInstance(loadedType);
// get all filepaths
var files = Directory.GetFiles(filepath);
var directories = Directory.GetDirectories(filepath);
foreach (var file in files)
if (Path.GetExtension(file) == "json")
{
// const filename = this.vfs.stripExtension(file);
// const filePathAndName = `${filepath}${file}`;
var fileData = File.ReadAllText(file);
onReadCallback(file, fileData);
var matchedProperty = loadedType.GetProperties()
.FirstOrDefault(prop => prop.Name.ToLower() == Path.GetFileNameWithoutExtension(file).ToLower());
if (matchedProperty == null)
throw new Exception($"Unable to find property '{Path.GetFileNameWithoutExtension(file)}' for type '{loadedType.Name}'");
var propertyType = matchedProperty.PropertyType;
var fileDeserialized = _jsonUtil.Deserialize(fileData, propertyType);
onObjectDeserialized(file, fileDeserialized);
matchedProperty.GetSetMethod().Invoke(result, [fileDeserialized]);
}
// deep tree search
foreach (var directory in directories)
{
var matchedProperty = loadedType.GetProperties().FirstOrDefault(prop => prop.Name.ToLower() == directory.ToLower());
if (matchedProperty == null)
throw new Exception($"Unable to find property '{directory}' for type '{loadedType.Name}'");
matchedProperty.GetSetMethod().Invoke(result, [LoadRecursive($"{filepath}{directory}/", matchedProperty.PropertyType)]);
}
// return the result of all async fetch
return result;
}
public async Task<object> LoadAsync(
string filepath,
Type loadedType,
string strippablePath = "",
Action<string, string>? onReadCallback = null,
Action<string, object>? onObjectDeserialized = null
)
{
var result = Activator.CreateInstance(loadedType);
var promises = new List<Task<object>>();
var filesToProcess = new Queue<string>(_fileUtil.GetFiles(filepath, true));
while (filesToProcess.Count != 0)
{
var fileNode = filesToProcess.Dequeue();
if (fileNode == null || _fileUtil.GetFileExtension(fileNode) != "json") continue;
promises.Add(File.ReadAllTextAsync(fileNode).ContinueWith(fd =>
{
onReadCallback(fileNode, fd.Result);
return _jsonUtil.Deserialize(fd.Result, typeof(object));
}));
/*
this.vfs
.readFileAsync(filePathAndName)
.then(async (fileData) => {
onReadCallback(filePathAndName, fileData);
return this.jsonUtil.deserializeWithCacheCheckAsync<any>(fileData, filePathAndName);
})
.then(async (fileDeserialized) => {
onObjectDeserialized(filePathAndName, fileDeserialized);
const strippedFilePath = this.vfs.stripExtension(filePathAndName).replace(filepath, "");
this.placeObject(fileDeserialized, strippedFilePath, result, strippablePath);
})
.then(() => progressWriter.increment()),
);*/
}
//await JSType.Promise<>.all(promises).catch((e) => console.error(e));
return result;
}
/*
protected placeObject<T>(fileDeserialized: any, strippedFilePath: string, result: T, strippablePath: string): void {
const strippedFinalPath = strippedFilePath.replace(strippablePath, "");
let temp = result;
const propertiesToVisit = strippedFinalPath.split("/");
for (let i = 0; i < propertiesToVisit.length; i++) {
const property = propertiesToVisit[i];
if (i === propertiesToVisit.length - 1) {
temp[property] = fileDeserialized;
} else {
if (!temp[property]) {
temp[property] = {};
}
temp = temp[property];
}
}
}*/
}
+38 -39
View File
@@ -12,7 +12,7 @@ public class TimeUtil
/// </summary>
/// <param name="dateTime">The date to format in UTC.</param>
/// <returns>The formatted time as 'HH-MM-SS'.</returns>
public string FormatTime(DateTime dateTime)
public string FormatTime(DateTimeOffset dateTime)
{
var hour = Pad(dateTime.ToUniversalTime().Hour);
var minute = Pad(dateTime.ToUniversalTime().Minute);
@@ -26,7 +26,7 @@ public class TimeUtil
/// </summary>
/// <param name="dateTime">The date to format in UTC.</param>
/// <returns>The formatted date as 'YYYY-MM-DD'.</returns>
public string FormatDate(DateTime dateTime)
public string FormatDate(DateTimeOffset dateTime)
{
var day = Pad(dateTime.ToUniversalTime().Day);
var month = Pad(dateTime.ToUniversalTime().Month);
@@ -41,7 +41,7 @@ public class TimeUtil
/// <returns>The current date as 'YYYY-MM-DD'.</returns>
public string GetDate()
{
return FormatDate(DateTime.Now);
return FormatDate(DateTimeOffset.UtcNow);
}
/// <summary>
@@ -50,7 +50,7 @@ public class TimeUtil
/// <returns>The current time as 'HH-MM-SS'.</returns>
public string GetTime()
{
return FormatTime(DateTime.Now);
return FormatTime(DateTimeOffset.UtcNow);
}
/// <summary>
@@ -59,7 +59,7 @@ public class TimeUtil
/// <returns>The current timestamp in seconds since the Unix epoch in UTC.</returns>
public long GetTimeStamp()
{
return DateTimeOffset.Now.ToUnixTimeSeconds();
return DateTimeOffset.Now.ToUnixTimeMilliseconds();
}
/// <summary>
@@ -67,12 +67,14 @@ public class TimeUtil
/// </summary>
/// <param name="dateTime">datetime to get the time stamp for, if null it uses current date.</param>
/// <returns>Unix epoch for the start of day of the calculated date</returns>
public long GetStartOfDayTimeStamp(DateTime? dateTime)
public long GetStartOfDayTimeStamp(long? timestamp)
{
var now = dateTime ?? DateTime.Now;
return new DateTimeOffset(new DateTime(now.Year, now.Month, now.Day, 0, 0, 0))
.ToUnixTimeSeconds();
DateTime now = timestamp.HasValue
? DateTimeOffset.FromUnixTimeMilliseconds(timestamp.Value).DateTime
: DateTime.Now;
DateTime startOfDay = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
return ((DateTimeOffset)startOfDay).ToUnixTimeMilliseconds();
}
/// <summary>
@@ -82,7 +84,7 @@ public class TimeUtil
/// <returns></returns>
public long GetTimeStampFromNowDays(int daysFromNow)
{
return DateTimeOffset.Now.AddDays(daysFromNow).ToUnixTimeSeconds();
return DateTimeOffset.UtcNow.AddDays(daysFromNow).ToUnixTimeSeconds();
}
/// <summary>
@@ -92,16 +94,17 @@ public class TimeUtil
/// <returns></returns>
public long GetTimeStampFromNowHours(int hoursFromNow)
{
return DateTimeOffset.Now.AddHours(hoursFromNow).ToUnixTimeSeconds();
return DateTimeOffset.UtcNow.AddHours(hoursFromNow).ToUnixTimeSeconds();
}
/// <summary>
/// Gets the current time in UTC in a format suitable for mail in EFT.
/// </summary>
/// <returns>The current time as 'HH:MM' in UTC.</returns>
/// GetTimeMailFormat
public string GetTimeMailFormat()
{
return DateTime.UtcNow.ToString("HH:mm");
return DateTimeOffset.UtcNow.ToString("HH:mm");
}
/// <summary>
@@ -110,7 +113,7 @@ public class TimeUtil
/// <returns>The current date as 'DD.MM.YYYY' in UTC.</returns>
public string GetDateMailFormat()
{
return DateTime.UtcNow.ToString("dd.MM.yyyy");
return DateTimeOffset.UtcNow.ToString("dd.MM.yyyy");
}
/// <summary>
@@ -129,19 +132,14 @@ public class TimeUtil
/// <returns>Time stamp of the next hour in unix time seconds</returns>
public long GetTimeStampOfNextHour()
{
var now = DateTime.UtcNow;
DateTime now = DateTime.Now;
TimeSpan timeUntilNextHour = TimeSpan.FromMinutes(60 - now.Minute)
.Subtract(TimeSpan.FromSeconds(now.Second))
.Subtract(TimeSpan.FromMilliseconds(now.Millisecond));
var time = ((DateTimeOffset)now.Add(timeUntilNextHour)).ToUnixTimeSeconds();
var nextHour = new DateTime(
now.Year,
now.Month,
now.Day,
now.Hour,
0,
0,
DateTimeKind.Utc
).AddHours(1);
return new DateTimeOffset(nextHour).ToUnixTimeSeconds();
return time;
}
/// <summary>
@@ -151,19 +149,20 @@ public class TimeUtil
/// <returns>Timestamp</returns>
public long GetTodayMidnightTimeStamp()
{
var now = DateTime.UtcNow;
var midNight = new DateTime(
now.Year,
now.Month,
now.Day,
0,
0,
0,
DateTimeKind.Utc
);
return new DateTimeOffset(midNight).ToUnixTimeSeconds();
DateTime now = DateTime.Now;
int hours = now.Hour;
int minutes = now.Minute;
// If minutes greater than 0, subtract 1 hour
if (minutes > 0)
{
hours--;
}
// Create a new DateTime with the last full hour, 0 minutes, and 0 seconds
DateTime lastFullHour = new DateTime(now.Year, now.Month, now.Day, hours, 0, 0);
return ((DateTimeOffset)lastFullHour).ToUnixTimeMilliseconds();
}
/// <summary>
+2 -1
View File
@@ -14,7 +14,8 @@ public abstract class AbstractFormatter : ITextFormatter
var logLevel = logEvent.Level.ToString().ToUpper().Substring(0, 4);
var message = logEvent.RenderMessage();
var exception = logEvent.Exception != null ? $"{newLine}{logEvent.Exception}{newLine}{logEvent.Exception.StackTrace}" : "";
var logMessage = ProcessText($"[{timestamp} {logLevel}] {message}{exception}");
var sourceContext = logEvent.Properties["SourceContext"].ToString().Replace("\"", "");
var logMessage = ProcessText($"[{timestamp} {logLevel}][{sourceContext}] {message}{exception}");
output.WriteLine(logMessage);
}
}
+3 -1
View File
@@ -8,6 +8,7 @@ using Core.Models.Spt.Config;
using Core.Servers;
using Core.Utils;
using Serilog;
using Serilog.Events;
namespace Server;
@@ -18,7 +19,7 @@ public static class Program
var assemblies = ModDllLoader.LoadAllMods();
HarmonyBootstrapper.LoadAllPatches(assemblies);
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("appsettings.json", true, true);
CreateAndRegisterLogger(builder);
@@ -64,6 +65,7 @@ public static class Program
builder.Logging.ClearProviders();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Diagnostics", LogEventLevel.Warning)
.CreateLogger();
builder.Logging.AddSerilog(logger);
}
+1
View File
@@ -19,6 +19,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0"/>
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0"/>
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0" />
</ItemGroup>
+12 -3
View File
@@ -1,9 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Default": "Trace",
"System": "Information",
"Microsoft": "Information",
"Microsoft.AspNetCore.Hosting.Diagnostics": "Warning"
}
},
"Serilog": {
@@ -31,6 +32,14 @@
]
}
}
],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "StartsWith(SourceContext, 'TADAADA')"
}
}
]
}
}