Created modhelper class and updated examples to make use of it

This commit is contained in:
Chomp
2025-02-11 16:22:32 +00:00
parent 51e889e34e
commit 2a258aed35
11 changed files with 103 additions and 70 deletions
+36
View File
@@ -0,0 +1,36 @@
using System.Reflection;
using Core.Utils;
using SptCommon.Annotations;
namespace Core.Helpers
{
[Injectable]
public class ModHelper
{
private readonly FileUtil _fileUtil;
private readonly JsonUtil _jsonUtil;
public ModHelper(
FileUtil fileUtil,
JsonUtil jsonUtil)
{
_fileUtil = fileUtil;
_jsonUtil = jsonUtil;
}
public string GetAbsolutePathToModFolder(Assembly modAssembly)
{
// The full path to the mod folder
return Path.GetDirectoryName(modAssembly.Location);
}
public T GetFileFromModFolder<T>(string pathToFile, string fileName)
{
// Read the content of the config file as a string
var rawContent = _fileUtil.ReadFile(Path.Combine(pathToFile, fileName));
// Take the string above and deserialise it into a config file with a type (defined between the diamond brackets)
return _jsonUtil.Deserialize<T>(rawContent);
}
}
}