SDM_LethalCompany_Mod/ScarletMansion/ScarletMansion/Configs/Items.cs

89 lines
4.1 KiB
C#

using BepInEx.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ScarletMansion.Configs {
public partial class ConfigManager<T> : SyncedInstance<T> where T: ConfigManager<T> {
public class ConfigEntryBundleItem : ConfigEntryBundleBase {
public ConfigEntryBundle<bool> enabled;
public ConfigEntryBundleMinMax<int> valueRange;
private static string _valueMessage(string itemName, string minmax) => $"The {minmax} scrap value of {itemName}. Lethal Company multiplies all scrap values by 0.4.";
public ConfigEntryBundleItem(string section, string keyPrefix, string itemName, int baseValueMin, int baseValueMax, ConfigEntryBundleExtraParameters extra = null){
var enabledDesc = $"If disabled, {itemName} is disabled from spawning.";
enabled = new ConfigEntryBundle<bool>(section, $"{keyPrefix}Enabled", true, enabledDesc, extra);
valueRange = new ConfigEntryBundleMinMax<int>(
section,
$"Value Min",
$"Value Max",
baseValueMin,
baseValueMax,
_valueMessage(itemName, "minimum"),
_valueMessage(itemName, "maximum"),
extra,
new AcceptableValueRange<int>(1, 400)
);
}
public override void Bind(ConfigFile cfg, T instance, FieldPropertyInfo memberInfo, object memberTarget) {
var type = memberInfo.GetFieldPropertyType();
var enabledProp = type.GetField("enabled", BindingFlags.Public | BindingFlags.Instance);
var valueRangeProp = type.GetField("valueRange", BindingFlags.Public | BindingFlags.Instance);
enabled.Bind(cfg, instance, new FieldPropertyInfo(enabledProp), memberInfo.GetValue(memberTarget));
if (valueRange.min.defaultValue > 0) valueRange.Bind(cfg, instance, new FieldPropertyInfo(valueRangeProp), memberInfo.GetValue(memberTarget));
}
public override IEnumerable<ConfigEntryBase> GetConfigs() {
yield return enabled.config;
if (valueRange.min.defaultValue > 0) {
foreach(var a in valueRange.GetConfigs()) {
yield return a;
}
}
}
}
public class ConfigEntryBundleScrapItem : ConfigEntryBundleItem {
public ConfigEntryBundle<int> spawnWeight;
public ConfigEntryBundle<bool> spawnOnAllMoons;
public ConfigEntryBundleScrapItem(string section, string itemName, int baseValueMin, int baseValueMax, int baseSpawnWeight, ConfigEntryBundleExtraParameters extra = null) : base(section, string.Empty, itemName, baseValueMin, baseValueMax, extra) {
var weightDesc = $"The {itemName}'s spawn weight. Calculating spawn chance (%) is difficult as the total scrap weight for each moon varies from ~600 to ~850.";
var spawnDesc = $"If enabled, the {itemName} scrap item will spawn on all moons regardless if SDM loaded.";
spawnWeight = new ConfigEntryBundle<int>(section, "Spawn Weight Base", baseSpawnWeight, weightDesc, extra, new AcceptableValueRange<int>(0, 999));
spawnOnAllMoons = new ConfigEntryBundle<bool>(section, "Spawn On All Moons", false, spawnDesc, extra);
}
public override void Bind(ConfigFile cfg, T instance, FieldPropertyInfo memberInfo, object memberTarget) {
base.Bind(cfg, instance, memberInfo, memberTarget);
var type = memberInfo.GetFieldPropertyType();
var weightDescProp = type.GetField("spawnWeight", BindingFlags.Public | BindingFlags.Instance);
var spawnDescProp = type.GetField("spawnOnAllMoons", BindingFlags.Public | BindingFlags.Instance);
spawnWeight.Bind(cfg, instance, new FieldPropertyInfo(weightDescProp), memberInfo.GetValue(memberTarget));
spawnOnAllMoons.Bind(cfg, instance, new FieldPropertyInfo(spawnDescProp), memberInfo.GetValue(memberTarget));
}
public override IEnumerable<ConfigEntryBase> GetConfigs() {
foreach(var a in base.GetConfigs()) {
yield return a;
}
yield return spawnWeight.config;
yield return spawnOnAllMoons.config;
}
}
}
}