C#:
//Reference: 0Harmony
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Collections;
using System.Reflection;
using System.Linq;
using Harmony;
using System;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Furnace Full Smelt", "Aspect.dev", "0.0.2")]
public class FurnaceFullSmelt : RustPlugin
{
private static HarmonyInstance __harmonyInstance;
private static FurnaceFullSmelt _plugin;
private ConfigData _config;
private void OnServerInitialized()
{
_plugin = this;
__harmonyInstance = HarmonyInstance.Create(Name + "Patches");
__harmonyInstance.PatchAll();
}
private void Unload()
{
_plugin = null;
__harmonyInstance.UnpatchAll(__harmonyInstance.Id);
__harmonyInstance = null;
}
[HarmonyPatch(typeof(BaseOven), nameof(BaseOven.StartCooking))]
private static class BaseOvenSpeedPatch
{
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
if (!_plugin._config.DynamicSmeltingSpeed) return instructions;
List<CodeInstruction> list = instructions.ToList();
int index = list.FindIndex(i => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "InvokeRepeating");
if (index == -1) return instructions;
index -= 1;
list[index].operand = AccessTools.Method(typeof(FacepunchBehaviour), nameof(FacepunchBehaviour.Invoke), new Type[] { typeof(Action), typeof(float) });
return list;
}
}
[HarmonyPatch(typeof(BaseOven), nameof(BaseOven.Cook))]
private static class BaseOvenSpeedHelperPatch
{
[HarmonyPostfix]
private static void Postfix(BaseOven __instance)
{
if (!_plugin._config.DynamicSmeltingSpeed) return;
int used = 0;
int num = __instance._inputSlotIndex + __instance.inputSlots;
for (int i = __instance._inputSlotIndex; i <= num; i++)
{
if (__instance.inventory.GetSlot(i) != null) used++;
}
if (!__instance.HasFlag(BaseEntity.Flags.On)) return;
if (used == 0) __instance.Invoke(new Action(__instance.Cook), 0.5f);
else __instance.Invoke(new Action(__instance.Cook), 0.5f * used / __instance.inputSlots);
}
}
[HarmonyPatch(typeof(BaseOven), nameof(BaseOven.GetTemperature))]
private static class BaseOvenFullSlotPatch
{
[HarmonyPrefix]
private static bool Prefix(BaseOven __instance, ref float __result)
{
if (__instance.HasFlag(BaseEntity.Flags.On)) __result = __instance.cookingTemperature;
else __result = 15f;
return false;
}
}
public class ConfigData
{
public bool DynamicSmeltingSpeed = false;
}
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<ConfigData>();
if (_config == null) LoadDefaultConfig();
}
catch (Exception ex)
{
PrintError($"The configuration file is corrupted. \n{ex}");
LoadDefaultConfig();
}
SaveConfig();
}
protected override void LoadDefaultConfig()
{
_config = new ConfigData();
}
protected override void SaveConfig() => Config.WriteObject(_config);
}
}