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
@@ -3,6 +3,7 @@ using Core.Models.Eft.Common.Tables;
using Core.Models.Spt.Config;
using Core.Models.Spt.Server;
using Core.Utils;
using Core.Utils.Cloners;
namespace _13._1AddTraderWithDynamicAssorts
{
@@ -34,18 +35,14 @@ namespace _13._1AddTraderWithDynamicAssorts
* @param tables database
* @param jsonUtil json utility class
*/
public void AddTraderToDb(TraderBase traderDetailsToAdd, DatabaseTables tables, JsonUtil jsonUtil, object assortJson)
public void AddTraderToDb(TraderBase traderDetailsToAdd, DatabaseTables tables, ICloner cloner, TraderAssort assort)
{
// Create trader data ready to add to database
var traderDataToAdd = new Trader
{
Assort =
jsonUtil.Deserialize<TraderAssort>(
jsonUtil.Serialize(assortJson)), // Deserialise/serialise creates a copy of the json
Base =
jsonUtil.Deserialize<TraderBase>(
jsonUtil.Serialize(traderDetailsToAdd)), // Deserialise/serialise creates a copy of the json
QuestAssort = new Dictionary<string, Dictionary<string, string>> // questassort is empty as trader has no assorts unlocked by quests
Assort = cloner.Clone(assort),
Base = cloner.Clone(traderDetailsToAdd),
QuestAssort = new Dictionary<string, Dictionary<string, string>> // quest assort is empty as trader has no assorts unlocked by quests
{
{ "Started", new Dictionary<string, string>() },
{ "Success", new Dictionary<string, string>() },
@@ -1,4 +1,6 @@
using Core.Models.Eft.Common.Tables;
using Core.Helpers;
using System.Reflection;
using Core.Models.Eft.Common.Tables;
using Core.Models.Enums;
using Core.Models.External;
using Core.Models.Spt.Config;
@@ -7,7 +9,9 @@ using Core.Routers;
using Core.Servers;
using Core.Services;
using Core.Utils;
using Core.Utils.Cloners;
using SptCommon.Annotations;
using Path = System.IO.Path;
namespace _13._1AddTraderWithDynamicAssorts;
@@ -15,12 +19,14 @@ namespace _13._1AddTraderWithDynamicAssorts;
public class AddTraderWithDynamicAssorts : IPostDBLoadMod
{
private readonly ISptLogger<AddTraderWithDynamicAssorts> _logger;
private readonly ModHelper _modHelper;
private readonly HashUtil _hashUtil;
private readonly JsonUtil _jsonUtil;
private readonly FileUtil _fileUtil;
private readonly DatabaseService _databaseService;
private readonly ImageRouter _imageRouter;
private readonly ConfigServer _configServer;
private readonly ICloner _cloner;
private readonly TraderConfig _traderConfig;
private readonly RagfairConfig _ragfairConfig;
@@ -29,20 +35,24 @@ public class AddTraderWithDynamicAssorts : IPostDBLoadMod
public AddTraderWithDynamicAssorts(
ISptLogger<AddTraderWithDynamicAssorts> logger,
ModHelper modHelper,
HashUtil hashUtil,
JsonUtil jsonUtil,
FileUtil fileUtil,
DatabaseService databaseService,
ImageRouter imageRouter,
ConfigServer configServer)
ConfigServer configServer,
ICloner cloner )
{
_logger = logger;
_modHelper = modHelper;
_hashUtil = hashUtil;
_jsonUtil = jsonUtil;
_fileUtil = fileUtil;
_databaseService = databaseService;
_imageRouter = imageRouter;
_configServer = configServer;
_cloner = cloner;
_traderConfig = _configServer.GetConfig<TraderConfig>();
_ragfairConfig = _configServer.GetConfig<RagfairConfig>();
@@ -50,15 +60,14 @@ public class AddTraderWithDynamicAssorts : IPostDBLoadMod
public void PostDBLoad()
{
var modPath = _fileUtil.GetModPath(_modName);
var traderImagePath = $"{modPath}/db/cat.jpg";
var pathToMod = _modHelper.GetAbsolutePathToModFolder(Assembly.GetExecutingAssembly());
var baseJson = _fileUtil.ReadFile($"{modPath}/db/base.json");
var traderBase = _jsonUtil.Deserialize<TraderBase>(baseJson);
var traderImagePath = Path.Combine(pathToMod, "db/cat.jpg");
var traderBase = _modHelper.GetFileFromModFolder<TraderBase>(pathToMod, "db/base.json");
// Create helper class and use it to register our traders image/icon + set its stock refresh time
var addTraderHelper = new AddTraderHelper();
_imageRouter.AddRoute(traderBase.Avatar.Replace(".jpg", ""), System.IO.Path.GetFullPath(traderImagePath));
_imageRouter.AddRoute(traderBase.Avatar.Replace(".jpg", ""), traderImagePath);
addTraderHelper.SetTraderUpdateTime(_traderConfig, traderBase, 3600, 4000);
// Add trader to flea market
@@ -70,8 +79,8 @@ public class AddTraderWithDynamicAssorts : IPostDBLoadMod
addTraderHelper.AddTraderToDb(
traderBase,
_databaseService.GetTables(),
_jsonUtil,
new TraderAssort() {Items = new List<Item>(), BarterScheme = new Dictionary<string, List<List<BarterScheme>>>(), LoyalLevelItems = new Dictionary<string, int>()});
_cloner,
new TraderAssort {Items = [], BarterScheme = new Dictionary<string, List<List<BarterScheme>>>(), LoyalLevelItems = new Dictionary<string, int>()});
_logger.Success("added trader base");
var fluentAssortCreator = new FluentTraderAssortCreator(_logger, _hashUtil);
@@ -3,6 +3,7 @@ using Core.Models.Eft.Common.Tables;
using Core.Models.Spt.Config;
using Core.Models.Spt.Server;
using Core.Utils;
using Core.Utils.Cloners;
namespace _13AddTraderWithAssortJson
{
@@ -34,17 +35,13 @@ namespace _13AddTraderWithAssortJson
* @param tables database
* @param jsonUtil json utility class
*/
public void AddTraderToDb(TraderBase traderDetailsToAdd, DatabaseTables tables, JsonUtil jsonUtil, object assortJson)
public void AddTraderToDb(TraderBase traderDetailsToAdd, DatabaseTables tables, ICloner cloner, TraderAssort assortJson)
{
// Create trader data ready to add to database
var traderDataToAdd = new Trader
{
Assort =
jsonUtil.Deserialize<TraderAssort>(
jsonUtil.Serialize(assortJson)), // Deserialise/serialise creates a copy of the json
Base =
jsonUtil.Deserialize<TraderBase>(
jsonUtil.Serialize(traderDetailsToAdd)), // Deserialise/serialise creates a copy of the json
Assort = cloner.Clone(assortJson), // Clone the data before saving it so we dont mess up any references
Base = cloner.Clone(traderDetailsToAdd),
QuestAssort = new Dictionary<string, Dictionary<string, string>> // questassort is empty as trader has no assorts unlocked by quests
{
{ "Started", new Dictionary<string, string>() },
@@ -1,12 +1,15 @@
using Core.Models.Eft.Common.Tables;
using Core.Helpers;
using System.Reflection;
using Core.Models.Eft.Common.Tables;
using Core.Models.External;
using Core.Models.Spt.Config;
using Core.Models.Utils;
using Core.Routers;
using Core.Servers;
using Core.Services;
using Core.Utils;
using Core.Utils.Cloners;
using SptCommon.Annotations;
using Path = System.IO.Path;
namespace _13AddTraderWithAssortJson;
@@ -14,28 +17,28 @@ namespace _13AddTraderWithAssortJson;
public class AddTraderWithAssortJson : IPostDBLoadMod
{
private readonly ISptLogger<AddTraderWithAssortJson> _logger;
private readonly JsonUtil _jsonUtil;
private readonly FileUtil _fileUtil;
private readonly ModHelper _modHelper;
private readonly DatabaseService _databaseService;
private readonly ImageRouter _imageRouter;
private readonly ConfigServer _configServer;
private readonly ICloner _cloner;
private readonly TraderConfig _traderConfig;
private readonly RagfairConfig _ragfairConfig;
public AddTraderWithAssortJson(
ISptLogger<AddTraderWithAssortJson> logger,
JsonUtil jsonUtil,
FileUtil fileUtil,
ModHelper modHelper,
DatabaseService databaseService,
ImageRouter imageRouter,
ConfigServer configServer)
ConfigServer configServer,
ICloner cloner)
{
_logger = logger;
_jsonUtil = jsonUtil;
_fileUtil = fileUtil;
_modHelper = modHelper;
_databaseService = databaseService;
_imageRouter = imageRouter;
_configServer = configServer;
_cloner = cloner;
_traderConfig = _configServer.GetConfig<TraderConfig>();
_ragfairConfig = _configServer.GetConfig<RagfairConfig>();
@@ -43,17 +46,16 @@ public class AddTraderWithAssortJson : IPostDBLoadMod
public void PostDBLoad()
{
var traderImagePath = "./db/cat.jpg";
var baseJson = _fileUtil.ReadFile("./db/base.json");
var traderBase = _jsonUtil.Deserialize<TraderBase>(baseJson);
var pathToMod = _modHelper.GetAbsolutePathToModFolder(Assembly.GetExecutingAssembly());
var assortJson = _fileUtil.ReadFile("./db/assort.json");
var assort = _jsonUtil.Deserialize<TraderAssort>(assortJson);
var traderImagePath = Path.Combine(pathToMod, "db/cat.jpg");
var traderBase = _modHelper.GetFileFromModFolder<TraderBase>(pathToMod, "db/base.json");
var assort = _modHelper.GetFileFromModFolder<TraderAssort>(pathToMod, "db/assort.json");
// Create helper class and use it to register our traders image/icon + set its stock refresh time
var addTraderHelper = new AddTraderHelper();
_imageRouter.AddRoute(traderBase.Avatar.Replace(".jpg", ""), System.IO.Path.GetFullPath(traderImagePath));
_imageRouter.AddRoute(traderBase.Avatar.Replace(".jpg", ""), traderImagePath);
addTraderHelper.SetTraderUpdateTime(_traderConfig, traderBase, 3600, 4000);
// Add trader to flea market
@@ -72,8 +74,8 @@ public class AddTraderWithAssortJson : IPostDBLoadMod
addTraderHelper.AddTraderToDb(
traderBase,
_databaseService.GetTables(),
_jsonUtil,
assortJson);
_cloner,
assort);
addTraderHelper.AddTraderToLocales(
traderBase,
@@ -1,6 +1,7 @@
using Core.Models.External;
using Core.Helpers;
using System.Reflection;
using Core.Models.External;
using Core.Models.Utils;
using Core.Utils;
using SptCommon.Annotations;
namespace _4ReadCustomJson5Config
@@ -9,28 +10,24 @@ namespace _4ReadCustomJson5Config
public class ReadJson5Config: IPreSptLoadMod
{
private readonly ISptLogger<ReadJson5Config> _logger;
private readonly FileUtil _fileUtil;
private readonly JsonUtil _jsonUtil;
private readonly ModHelper _modHelper;
public ReadJson5Config(
ISptLogger<ReadJson5Config> logger,
FileUtil fileUtil,
JsonUtil jsonUtil)
ModHelper modHelper)
{
_logger = logger;
_fileUtil = fileUtil;
_jsonUtil = jsonUtil;
_modHelper = modHelper;
}
public void PreSptLoad()
{
// Read the content of the config file into a string
var rawContent = _fileUtil.ReadFile("config.json5");
var pathToMod = _modHelper.GetAbsolutePathToModFolder(Assembly.GetExecutingAssembly());
// Take the string above and deserialise it into a config file with a type (defined between the diamond brackets)
var config = _jsonUtil.Deserialize<ModConfig>(rawContent);
// TODO - fix/implement
var json5Config = _modHelper.GetFileFromModFolder<ModConfig>(pathToMod, "config.json5");
_logger.Success($"Read property: 'ExampleProperty' from config with value: {config.ExampleProperty}");
_logger.Success($"Read property: 'ExampleProperty' from config with value: {json5Config.ExampleProperty}");
}
}
@@ -1,4 +1,6 @@
using Core.Models.External;
using System.Reflection;
using Core.Helpers;
using Core.Models.External;
using Core.Models.Utils;
using Core.Utils;
using SptCommon.Annotations;
@@ -9,26 +11,23 @@ namespace _5ReadCustomJsonConfig
public class ReadJsonConfig : IPreSptLoadMod
{
private readonly ISptLogger<ReadJsonConfig> _logger;
private readonly FileUtil _fileUtil;
private readonly JsonUtil _jsonUtil;
private readonly ModHelper _modHelper;
public ReadJsonConfig(
ISptLogger<ReadJsonConfig> logger,
FileUtil fileUtil,
JsonUtil jsonUtil)
ModHelper modHelper)
{
_logger = logger;
_fileUtil = fileUtil;
_jsonUtil = jsonUtil;
_modHelper = modHelper;
}
public void PreSptLoad()
{
// Read the content of the config file into a string
var rawContent = _fileUtil.ReadFile("config.json");
// This will get us the full path to the mod, e.g. C:\spt\user\mods\5ReadCustomJsonConfig-0.0.1
var pathToMod = _modHelper.GetAbsolutePathToModFolder(Assembly.GetExecutingAssembly());
// Take the string above and deserialise it into a config file with a type (defined between the diamond brackets)
var config = _jsonUtil.Deserialize<ModConfig>(rawContent);
// We give the path to the mod folder and the file we want to get, giving us the config, supply the files 'type' between the diamond brackets
var config = _modHelper.GetFileFromModFolder<ModConfig>(pathToMod, "config.json");
_logger.Success($"Read property: 'ExampleProperty' from config with value: {config.ExampleProperty}");
}
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _7UseMultipleClasses
{
Binary file not shown.
Binary file not shown.