diff --git a/Core/Helpers/WeatherHelper.cs b/Core/Helpers/WeatherHelper.cs
index 9912a883..0b3163f4 100644
--- a/Core/Helpers/WeatherHelper.cs
+++ b/Core/Helpers/WeatherHelper.cs
@@ -35,7 +35,7 @@ public class WeatherHelper
///
/// (new Date())
/// Date object of current in-raid time
- 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;
diff --git a/Core/Servers/ConfigServer.cs b/Core/Servers/ConfigServer.cs
index 1231bc25..c7c6097e 100644
--- a/Core/Servers/ConfigServer.cs
+++ b/Core/Servers/ConfigServer.cs
@@ -14,16 +14,19 @@ public class ConfigServer
{
protected ILogger _logger;
protected JsonUtil _jsonUtil;
+ protected FileUtil _fileUtil;
protected Dictionary 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()
- .First(en => en.GetValue().Contains(Path.GetFileNameWithoutExtension(filename)));
+ .First(en => en.GetValue().Contains(_fileUtil.StripExtension(filename)));
return type.GetConfigType();
}
}
diff --git a/Core/Services/I18nService.cs b/Core/Services/I18nService.cs
index f656d50f..dc3b96ef 100644
--- a/Core/Services/I18nService.cs
+++ b/Core/Services/I18nService.cs
@@ -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 _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> _loadedLocales = new();
- public I18nService(JsonUtil jsonUtil, List locales, Dictionary fallbacks, string defaultLocale, string directory)
+ public I18nService(
+ FileUtil fileUtil,
+ JsonUtil jsonUtil,
+ List locales,
+ Dictionary 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>(File.ReadAllText(file)) ?? new Dictionary());
+ _loadedLocales.Add(_fileUtil.StripExtension(file),
+ _jsonUtil.Deserialize>(_fileUtil.ReadFile(file)) ??
+ new Dictionary());
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;
+ }
}
}
diff --git a/Core/Services/LocalisationService.cs b/Core/Services/LocalisationService.cs
index 98c45e73..bb664fb3 100644
--- a/Core/Services/LocalisationService.cs
+++ b/Core/Services/LocalisationService.cs
@@ -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(),
diff --git a/Core/Utils/FileUtil.cs b/Core/Utils/FileUtil.cs
index a8242d4d..99df8e35 100644
--- a/Core/Utils/FileUtil.cs
+++ b/Core/Utils/FileUtil.cs
@@ -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);
diff --git a/Core/Utils/ImporterUtil.cs b/Core/Utils/ImporterUtil.cs
index b16afbd6..953253be 100644
--- a/Core/Utils/ImporterUtil.cs
+++ b/Core/Utils/ImporterUtil.cs
@@ -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? onReadCallback = null,
- Action? 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