4 Commits

4 changed files with 87 additions and 26 deletions

View File

@@ -1,24 +1,22 @@
using BepInEx; using BepInEx;
using BepInEx.Configuration; using BepInEx.Configuration;
using HarmonyLib; using HarmonyLib;
using ServerSync; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine; using UnityEngine;
using Newtonsoft.Json;
[BepInPlugin(ModGUID, ModName, ModVersion)] [BepInPlugin(ModGUID, ModName, ModVersion)]
public class MxValheimMod : BaseUnityPlugin public class MxValheimMod : BaseUnityPlugin
{ {
public static MxValheimMod Instance; // Singleton reference
private const string ModGUID = "ovh.mxdev.mxvalheim"; private const string ModGUID = "ovh.mxdev.mxvalheim";
private const string ModName = "MxValheim"; private const string ModName = "MxValheim";
private const string ModVersion = "1.3.0"; private const string ModVersion = "1.3.0";
private static readonly ConfigSync configSync = new ConfigSync(ModGUID)
{
DisplayName = ModName,
CurrentVersion = ModVersion,
MinimumRequiredVersion = ModVersion
};
public static ConfigEntry<bool> Config_Locked; public static ConfigEntry<bool> Config_Locked;
public static ConfigEntry<int> Config_OreMultiplier; public static ConfigEntry<int> Config_OreMultiplier;
public static ConfigEntry<float> Config_rangeMultiplier; public static ConfigEntry<float> Config_rangeMultiplier;
@@ -26,30 +24,53 @@ public class MxValheimMod : BaseUnityPlugin
public static ConfigEntry<bool> Config_rainDamage; public static ConfigEntry<bool> Config_rainDamage;
public static ConfigEntry<float> Config_boatSpeed; public static ConfigEntry<float> Config_boatSpeed;
private static string WeightConfigPath => Path.Combine(Paths.ConfigPath, "mxvalheim.custom_weights.json");
public static Dictionary<string, float> WeightSettings = new Dictionary<string, float>();
void Awake() void Awake()
{ {
Config_Locked = BindSyncConfig("General", "Lock Configuration", true, "If obsession is on, only admins can change settings."); Instance = this;
configSync.AddLockingConfigEntry(Config_Locked);
Config_OreMultiplier = BindSyncConfig("General","OreMultiplier",3,"How many items should drop for every 1 ore/scrap found."); Config_OreMultiplier = Config.Bind("General","OreMultiplier",3,"How many items should drop for every 1 ore/scrap found.");
Config_rangeMultiplier = BindSyncConfig("General", "CraftingRangeMultiplier",2.0f,"Multiplier for the workbench build/crafting range. Default is 2x."); Config_rangeMultiplier = Config.Bind("General", "CraftingRangeMultiplier",2.0f,"Multiplier for the workbench build/crafting range. Default is 2x.");
Config_bowDrawSpeedBonusPerLevel = BindSyncConfig("General", "BowDrawSpeedBonusPercentPerLevel", 1.0f, "Shorten the bow draw speed by this percent for every bow upgrade level."); Config_bowDrawSpeedBonusPerLevel = Config.Bind("General", "BowDrawSpeedBonusPercentPerLevel", 1.0f, "Shorten the bow draw speed by this percent for every bow upgrade level.");
Config_rainDamage = BindSyncConfig("General", "RainDamage", true, "Set to true to stop rain damage, false to return to vanilla behavior."); Config_rainDamage = Config.Bind("General", "RainDamage", true, "Set to true to stop rain damage, false to return to vanilla behavior.");
Config_boatSpeed = BindSyncConfig("General", "BoatSpeedMultiplier", 2.0f, "Your boat/raft will move without wind at a speed multiplied by this value."); Config_boatSpeed = Config.Bind("General", "BoatSpeedMultiplier", 2.0f, "Your boat/raft will move without wind at a speed multiplied by this value.");
LoadJsonConfig();
Harmony harmony = new Harmony(ModGUID); Harmony harmony = new Harmony(ModGUID);
harmony.PatchAll(); harmony.PatchAll();
} }
/// <summary> private void Update()
/// Helper method to bind a config and register it for server syncing.
/// </summary>
private ConfigEntry<T> BindSyncConfig<T>(string group, string name, T value, string description, bool synchronizedSetting = true)
{ {
ConfigEntry<T> configEntry = this.Config.Bind(group, name, value, description); // Only check while in the game world to save resources
if (ObjectDB.instance == null) return;
SyncedConfigEntry<T> syncedConfigEntry = configSync.AddConfigEntry(configEntry);
syncedConfigEntry.SynchronizedConfig = synchronizedSetting;
return configEntry; }
private bool LoadJsonConfig()
{
try
{
if (!File.Exists(WeightConfigPath))
{
WeightSettings = new Dictionary<string, float> { { "Wood", 1.0f }, { "Stone", 1.0f } };
File.WriteAllText(WeightConfigPath, JsonConvert.SerializeObject(WeightSettings, Newtonsoft.Json.Formatting.Indented));
return true;
}
string json = File.ReadAllText(WeightConfigPath);
WeightSettings = JsonConvert.DeserializeObject<Dictionary<string, float>>(json);
Logger.LogInfo($"Successfully parsed {WeightSettings.Count} items.");
return true;
}
catch (Exception ex)
{
Logger.LogWarning($"Could not read JSON (might be busy): {ex.Message}");
return false;
}
} }
} }

View File

@@ -10,6 +10,7 @@
<RootNamespace>MxValheim</RootNamespace> <RootNamespace>MxValheim</RootNamespace>
<AssemblyName>MxValheim</AssemblyName> <AssemblyName>MxValheim</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<LangVersion>12.0</LangVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
</PropertyGroup> </PropertyGroup>
@@ -43,8 +44,9 @@
<Reference Include="BepInEx"> <Reference Include="BepInEx">
<HintPath>E:\SteamLibrary\steamapps\common\Valheim\BepInEx\core\BepInEx.dll</HintPath> <HintPath>E:\SteamLibrary\steamapps\common\Valheim\BepInEx\core\BepInEx.dll</HintPath>
</Reference> </Reference>
<Reference Include="ServerSync"> <Reference Include="gui_framework, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" />
<HintPath>C:\Users\blood\Downloads\ServerSync.dll</HintPath> <Reference Include="Newtonsoft.Json">
<HintPath>E:\SteamLibrary\steamapps\common\Valheim\BepInEx\plugins\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
@@ -54,6 +56,7 @@
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="Unity.TextMeshPro, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Reference Include="UnityEngine"> <Reference Include="UnityEngine">
<HintPath>E:\SteamLibrary\steamapps\common\Valheim\Valheim_Data\Managed\UnityEngine.dll</HintPath> <HintPath>E:\SteamLibrary\steamapps\common\Valheim\Valheim_Data\Managed\UnityEngine.dll</HintPath>
</Reference> </Reference>
@@ -69,9 +72,13 @@
<Compile Include="MxValheim.cs" /> <Compile Include="MxValheim.cs" />
<Compile Include="Patch\Bow.cs" /> <Compile Include="Patch\Bow.cs" />
<Compile Include="Patch\CraftingStation.cs" /> <Compile Include="Patch\CraftingStation.cs" />
<Compile Include="Patch\Items.cs" />
<Compile Include="Patch\Ores.cs" /> <Compile Include="Patch\Ores.cs" />
<Compile Include="Patch\WearNTear.cs" /> <Compile Include="Patch\WearNTear.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Analyzer Include="E:\SteamLibrary\steamapps\common\Valheim\Valheim_Data\Managed\assembly_guiutils.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

33
MxValheim/Patch/Items.cs Normal file
View File

@@ -0,0 +1,33 @@
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace MxValheim.Patch
{
internal class Items
{
[HarmonyPatch(typeof(ObjectDB), nameof(ObjectDB.Awake))]
public static class ObjectDB_Patch
{
public static void Postfix()
{
if (ObjectDB.instance == null) return;
int count = 0;
foreach (GameObject item in ObjectDB.instance.m_items)
{
ItemDrop itemDrop = item.GetComponent<ItemDrop>();
if (itemDrop != null && MxValheimMod.WeightSettings.TryGetValue(item.name, out float newWeight))
{
itemDrop.m_itemData.m_shared.m_weight = newWeight;
count++;
}
}
}
}
}
}

View File

@@ -4,7 +4,7 @@
Official Mx Valheim Mod. Official Mx Valheim Mod.
## Features ## Features
- **NEW** Now using ServerSync when on a server for configurations syncing. - Tweak individual item(s) weight in "BepInEx\config\mxvalheim.custom_weights.json".
- Ore drop multiplier. (Value available in the generated config.) - Ore drop multiplier. (Value available in the generated config.)
- Workbench crafting range multiplier. (Value available in the generated config.) - Workbench crafting range multiplier. (Value available in the generated config.)
- Reduce Bow draw time for each upgrade level. (Value available in the generated config.) - Reduce Bow draw time for each upgrade level. (Value available in the generated config.)