Improved mod examples

This commit is contained in:
Chomp
2025-02-08 10:02:50 +00:00
parent efd8b360de
commit c579695f43
10 changed files with 167 additions and 24 deletions
@@ -3,14 +3,15 @@ using Core.Models.Logging;
using Core.Models.Utils;
using SptCommon.Annotations;
namespace ExampleMods.Mods;
namespace ExampleMods.Mods._1Logging;
[Injectable]
public class Logging : IPostSptLoadMod
public class Logging : IPostSptLoadMod // Using this interface means our mod will run AFTER SPT has finished loading
{
// Our logger we create in the constructor below
private readonly ISptLogger<Logging> _logger;
// Constructor - Inject a 'ISptLogger' with your mods Class in the diamond brackets
// Constructor - Inject a 'ISptLogger' with your mods Class inside the diamond brackets
public Logging(
ISptLogger<Logging> logger
)
@@ -4,14 +4,15 @@ using Core.Models.Utils;
using Core.Services;
using SptCommon.Annotations;
namespace ExampleMods.Mods;
namespace ExampleMods.Mods._2EditDatabase;
[Injectable]
public class EditDatabaseValues : IPostDBLoadMod
public class EditDatabaseValues : IPostDBLoadMod // Using this interface means our mod will run AFTER the SPT database has finished loading but BEFORE SPT code has run
{
private readonly DatabaseService _databaseService;
private readonly ISptLogger<EditDatabaseValues> _logger;
// Our constructor
public EditDatabaseValues(
DatabaseService databaseService,
ISptLogger<EditDatabaseValues> logger
@@ -35,7 +36,7 @@ public class EditDatabaseValues : IPostDBLoadMod
// Let's edit the hideout so it's easier to upgrade the lavatory
EditHideout();
// Lets edit the default scav to
// Lets edit the default scav (assault.json) to have different settings
EditScavSettings();
// Lets edit Customs
@@ -5,7 +5,7 @@ using Core.Models.Utils;
using Core.Servers;
using SptCommon.Annotations;
namespace ExampleMods.Mods;
namespace ExampleMods.Mods._3EditSptConfig;
[Injectable]
public class EditConfigs : IPostDBLoadMod
@@ -42,7 +42,7 @@ public class EditConfigs : IPostDBLoadMod
public void PostDBLoad()
{
// Let's edit the weather config to make the season winter
// Let's edit the weather config to force the season to winter
_weatherConfig.OverrideSeason = Season.WINTER;
// Let's edit the hideout config to Make all crafts take 60 seconds
@@ -51,7 +51,7 @@ public class EditConfigs : IPostDBLoadMod
// Let's edit the hideout config to Make all upgrades take 60 seconds
_hideoutConfig.OverrideBuildTimeSeconds = 60;
// Let's edit the airdrop config to Make weapon/armor drops really common
// Let's edit the airdrop config to Make weapon/armor drops REALLY common
_airdropConfig.AirdropTypeWeightings[SptAirdropTypeEnum.weaponArmor] = 999;
// Let's edit the airdrop config to Make weapon/armor drops always have 3 sealed weapon crates
@@ -62,7 +62,7 @@ public class EditConfigs : IPostDBLoadMod
// Let's make PMCs always mail you when they kill you
_pmcChatResponseConfig.Killer.ResponseChancePercent = 100;
// Let's make quest rewards sent to you via mail last for over a week if you have an unheard profile
// Let's make quest rewards sent to you via mail last for over a week for unheard profiles
_questConfig.MailRedeemTimeHours["unheard_edition"] = 168;
// Let's make the interchange bot cap huge
@@ -73,7 +73,11 @@ public class EditConfigs : IPostDBLoadMod
// Let's make the conversion rate of scavs to pmcs 100% on factory day
var factory4DayConversionSettings = _pmcConfig.ConvertIntoPmcChance["factory4_day"];
// We get assault bot settings for factory day
var assaultConversionSettings = factory4DayConversionSettings["assault"];
// Set min and max to 100%
assaultConversionSettings.Min = 100;
assaultConversionSettings.Max = 100;
@@ -0,0 +1,42 @@
using Core.Models.External;
using Core.Models.Utils;
using Core.Utils;
using SptCommon.Annotations;
namespace ExampleMods.Mods._4ReadCustomJson5Config
{
[Injectable]
public class ReadJson5Config: IPreSptLoadMod
{
private readonly ISptLogger<ReadJson5Config> _logger;
private readonly FileUtil _fileUtil;
private readonly JsonUtil _jsonUtil;
public ReadJson5Config(
ISptLogger<ReadJson5Config> logger,
FileUtil fileUtil,
JsonUtil jsonUtil)
{
_logger = logger;
_fileUtil = fileUtil;
_jsonUtil = jsonUtil;
}
public void PreSptLoad()
{
// Read the content of the config file into a string
var rawContent = _fileUtil.ReadFile("config.json5");
// 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);
_logger.Success($"Read property: 'ExampleProperty' from config with value: {config.ExampleProperty}");
}
}
// This class should represent your config structure
public class ModConfig
{
public string ExampleProperty { get; set; }
}
}
@@ -0,0 +1,4 @@
{
// Comment goes here, wow
"ExampleProperty": "boop"
}
@@ -0,0 +1,45 @@
using Core.Models.External;
using Core.Models.Utils;
using Core.Utils;
using SptCommon.Annotations;
namespace ExampleMods.Mods._5ReadCustomJsonConfig
{
[Injectable]
public class ReadJsonConfig : IPreSptLoadMod
{
private readonly ISptLogger<ReadJsonConfig> _logger;
private readonly FileUtil _fileUtil;
private readonly JsonUtil _jsonUtil;
public ReadJsonConfig(
ISptLogger<ReadJsonConfig> logger,
FileUtil fileUtil,
JsonUtil jsonUtil)
{
_logger = logger;
_fileUtil = fileUtil;
_jsonUtil = jsonUtil;
}
public void PreSptLoad()
{
// Read the content of the config file into a string
var rawContent = _fileUtil.ReadFile("config.json");
// 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);
_logger.Success($"Read property: 'ExampleProperty' from config with value: {config.ExampleProperty}");
}
}
// This class should represent your config structure
public class ModConfig
{
public string ExampleProperty
{
get; set;
}
}
}
@@ -0,0 +1,3 @@
{
"ExampleProperty": "boop"
}
@@ -0,0 +1,34 @@
using Core.Models.Utils;
using Core.Servers;
using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace ExampleMods.Mods._6ReplaceMethod
{
[Injectable(InjectableTypeOverride = typeof(Watermark))]
public class ReplaceMethod: Watermark
{
public ReplaceMethod(
ISptLogger<Watermark> logger,
ConfigServer configServer,
LocalisationService localisationService,
WatermarkLocale watermarkLocale)
: base(logger, configServer, localisationService, watermarkLocale)
{
_configServer = configServer;
_localisationService = localisationService;
_watermarkLocale = watermarkLocale;
_logger = logger;
}
public override void Initialize()
{
// We add a log message to the init method
_logger.Success("This is a watermark mod override!");
// This runs the original method (optional)
base.Initialize();
}
}
}
+13 -12
View File
@@ -4,21 +4,22 @@ using Core.Services;
using Core.Utils;
using SptCommon.Annotations;
namespace ExampleMods.Mods.Override;
namespace ExampleMods.Mods;
[Injectable(InjectableTypeOverride = typeof(Watermark))]
public class WatermarkOverride(
ISptLogger<Watermark> _logger,
ConfigServer _configServer,
LocalisationService _localisationService,
WatermarkLocale _watermarkLocale
) : Watermark(
_logger,
_configServer,
_localisationService,
_watermarkLocale
) // was testing overriding with primary constructors, works fine from what i can see
public class WatermarkOverride : Watermark // was testing overriding with primary constructors, works fine from what i can see
{
public WatermarkOverride(ISptLogger<Watermark> logger,
ConfigServer configServer,
LocalisationService localisationService,
WatermarkLocale watermarkLocale)
: base(logger,
configServer,
localisationService,
watermarkLocale)
{
}
public override void Initialize()
{
_logger.Success("This is a watermark mod override!");
+10 -2
View File
@@ -23,7 +23,9 @@ public class ModDllLoader
// load both
// if either is missing Throw Warning and skip
var modDirectories = Directory.GetDirectories(ModPath);
var modDirectories = GetSortedModDirectories();
// TODO: Handle package.json 'Dependencies' property
foreach (var modDirectory in modDirectories)
{
@@ -40,7 +42,13 @@ public class ModDllLoader
return mods;
}
private static SptMod? LoadMod(string path)
private static string[] GetSortedModDirectories()
{
// TODO: add system to read from package.json and order mod paths by their LoadBefore/LoadAfter properties
return Directory.GetDirectories(ModPath);
}
private static SptMod LoadMod(string path)
{
var result = new SptMod();
var asmCount = 0;