Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of RuneheimWeapons v1.0.2
plugins/RuneheimWeapons.dll
Decompiled a day agousing System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Text; using BepInEx; using BepInEx.Configuration; using BepInEx.Logging; using HarmonyLib; using Microsoft.CodeAnalysis; using UnityEngine; using UnityEngine.Rendering; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")] [assembly: AssemblyCompany("Daytreee")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright (c) 2026 Daytreee. All rights reserved. Not open source: no reuse, decompiling or redistribution without permission.")] [assembly: AssemblyFileVersion("1.0.2.0")] [assembly: AssemblyInformationalVersion("1.0.2")] [assembly: AssemblyProduct("Runeheim Weapons")] [assembly: AssemblyTitle("RuneheimWeapons")] [assembly: AssemblyVersion("1.0.2.0")] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } internal static class RhSafe { private static class Holder<F> { public static F Value; } private static ManualLogSource _log; private static readonly HashSet<string> _said = new HashSet<string>(); private const int MaxReports = 300; private static ManualLogSource Log { get { if (_log == null) { _log = Logger.CreateLogSource(typeof(RhSafe).Assembly.GetName().Name + " (checks)"); } return _log; } } private static void Warn(string key, string text) { try { lock (_said) { if (_said.Count >= 300 || !_said.Add(key)) { return; } } Log.LogWarning((object)text); } catch { } } private static string Where(string file, int line) { return Path.GetFileName(file) + ":" + line; } public static void Swallowed(Exception e, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0, [CallerMemberName] string member = "") { string text = Where(file, line); object obj; if (e == null) { obj = null; } else { string? stackTrace = e.StackTrace; obj = ((stackTrace == null) ? null : stackTrace.Split(new char[1] { '\n' })[0]?.Trim()); } if (obj == null) { obj = ""; } string text2 = (string)obj; Warn("catch " + text, "Handled error in " + member + " (" + text + "): " + e?.GetType().Name + ": " + e?.Message + " " + text2); } private static ref F Dummy<T, F>(T instance) { return ref Holder<F>.Value; } private static ref F DummyStatic<F>() { return ref Holder<F>.Value; } public static FieldRef<T, F> FieldRef<T, F>(string name, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) { try { return AccessTools.FieldRefAccess<T, F>(name); } catch (Exception ex) { Warn("field " + typeof(T).FullName + "." + name, typeof(T).Name + "." + name + " (as " + typeof(F).Name + ") can't be read (" + Where(file, line) + "): " + ex.GetBaseException().Message + ". What uses it is switched off."); return Dummy<T, F>; } } public static FieldRef<T, F> FieldRef<T, F>(FieldInfo field, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) { try { if (field != null) { return AccessTools.FieldRefAccess<T, F>(field); } } catch (Exception ex) { Warn("field " + field?.DeclaringType?.FullName + "." + field?.Name, field?.DeclaringType?.Name + "." + field?.Name + " can't be read (" + Where(file, line) + "): " + ex.GetBaseException().Message + ". What uses it is switched off."); return Dummy<T, F>; } Warn("field null " + Where(file, line), "A field looked up at " + Where(file, line) + " wasn't found. What uses it is switched off."); return Dummy<T, F>; } public static FieldRef<F> StaticFieldRef<F>(Type type, string name, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) { try { FieldInfo fieldInfo = ((type != null) ? AccessTools.Field(type, name) : null); if (fieldInfo != null) { return AccessTools.StaticFieldRefAccess<F>(fieldInfo); } Warn("static " + type?.FullName + "." + name, type?.Name + "." + name + " wasn't found (" + Where(file, line) + "). What uses it is switched off."); } catch (Exception ex) { Warn("static " + type?.FullName + "." + name, type?.Name + "." + name + " (as " + typeof(F).Name + ") can't be read (" + Where(file, line) + "): " + ex.GetBaseException().Message + ". What uses it is switched off."); } return DummyStatic<F>; } public static D MethodDelegate<D>(MethodInfo method, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0) where D : Delegate { try { if (method != null) { return AccessTools.MethodDelegate<D>(method, (object)null, true); } Warn("method null " + Where(file, line), "A method looked up at " + Where(file, line) + " wasn't found. What uses it is switched off."); } catch (Exception ex) { Warn("method " + method?.DeclaringType?.FullName + "." + method?.Name, method?.DeclaringType?.Name + "." + method?.Name + " can't be called (" + Where(file, line) + "): " + ex.GetBaseException().Message + ". What uses it is switched off."); } return null; } } namespace RuneheimWeapons { [BepInPlugin("daytreee.runeheimweapons", "Runeheim Weapons", "1.0.2")] public class Plugin : BaseUnityPlugin { [HarmonyPatch(typeof(ObjectDB), "Awake")] private static class AfterObjectDB { [HarmonyPostfix] [HarmonyPriority(200)] private static void Postfix() { Swap.ApplyAll(); } } [HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")] private static class AfterCopy { [HarmonyPostfix] [HarmonyPriority(200)] private static void Postfix() { Swap.ApplyAll(); } } public const string Guid = "daytreee.runeheimweapons"; public const string ModName = "Runeheim Weapons"; public const string Version = "1.0.2"; internal static ManualLogSource Log; internal static ConfigEntry<bool> Enabled; private static string dir; private void Awake() { //IL_008b: Unknown result type (might be due to invalid IL or missing references) Log = ((BaseUnityPlugin)this).Logger; dir = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location); Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Use the retextured weapons and shields (restart the game after changing)."); if (!Enabled.Value) { Log.LogInfo((object)"Off: the game's own weapons."); return; } try { Data.Load(dir); } catch (Exception ex) { Log.LogError((object)("Couldn't read the weapon data: " + ex.Message)); return; } new Harmony("daytreee.runeheimweapons").PatchAll(typeof(Plugin).Assembly); } } public class NodeData { public string Path; public int[] Index; public Vector3[] Pos; public Vector3[] Nrm; public Vector2[] Uv; public string[] SubMat; public bool[] SubGlow; public int[][] SubIdx; public Mesh Mesh; } public class ItemData { public string Prefab; public string Texture; public bool KeepsLayout; public Dictionary<string, string> Maps = new Dictionary<string, string>(); public List<NodeData> Nodes = new List<NodeData>(); } public static class Data { public static readonly Dictionary<string, ItemData> Items = new Dictionary<string, ItemData>(); private static string root; private static AssetBundle bundle; private static readonly Dictionary<string, Texture2D> textures = new Dictionary<string, Texture2D>(); private static readonly MethodInfo loadImage = Type.GetType("UnityEngine.ImageConversion, UnityEngine.ImageConversionModule")?.GetMethod("LoadImage", new Type[3] { typeof(Texture2D), typeof(byte[]), typeof(bool) }); public static void Load(string dir) { //IL_016f: Unknown result type (might be due to invalid IL or missing references) //IL_0174: Unknown result type (might be due to invalid IL or missing references) root = dir; using (BinaryReader binaryReader = new BinaryReader(File.OpenRead(Path.Combine(dir, "meshes.bin")))) { if (Encoding.ASCII.GetString(binaryReader.ReadBytes(4)) != "RHWM") { throw new Exception("not a Runeheim Weapons data file"); } uint num = binaryReader.ReadUInt32(); uint num2 = binaryReader.ReadUInt32(); for (uint num3 = 0u; num3 < num2; num3++) { ItemData itemData = new ItemData { Prefab = Str(binaryReader), Texture = Str(binaryReader) }; if (num >= 2) { itemData.KeepsLayout = (binaryReader.ReadByte() & 1) != 0; int num4 = binaryReader.ReadByte(); for (int i = 0; i < num4; i++) { string key = Str(binaryReader); itemData.Maps[key] = Str(binaryReader); } } uint num5 = binaryReader.ReadUInt32(); for (uint num6 = 0u; num6 < num5; num6++) { NodeData nodeData = new NodeData { Path = Str(binaryReader) }; nodeData.Index = new int[binaryReader.ReadUInt32()]; for (int j = 0; j < nodeData.Index.Length; j++) { nodeData.Index[j] = (int)binaryReader.ReadUInt32(); } int num7 = (int)binaryReader.ReadUInt32(); nodeData.Pos = V3(binaryReader, num7); nodeData.Nrm = V3(binaryReader, num7); nodeData.Uv = (Vector2[])(object)new Vector2[num7]; for (int k = 0; k < num7; k++) { nodeData.Uv[k] = new Vector2(binaryReader.ReadSingle(), binaryReader.ReadSingle()); } int num8 = (int)binaryReader.ReadUInt32(); nodeData.SubMat = new string[num8]; nodeData.SubGlow = new bool[num8]; nodeData.SubIdx = new int[num8][]; for (int l = 0; l < num8; l++) { nodeData.SubMat[l] = Str(binaryReader); nodeData.SubGlow[l] = binaryReader.ReadByte() != 0; int[] array = new int[binaryReader.ReadUInt32()]; for (int m = 0; m < array.Length; m++) { array[m] = (int)binaryReader.ReadUInt32(); } nodeData.SubIdx[l] = array; } itemData.Nodes.Add(nodeData); } Items[itemData.Prefab] = itemData; } } string text = Path.Combine(dir, "runeheimweapons.textures"); if (File.Exists(text)) { bundle = AssetBundle.LoadFromFile(text); } Plugin.Log.LogInfo((object)string.Format("{0} weapons and shields ready ({1}).", Items.Count, ((Object)(object)bundle != (Object)null) ? "texture bundle" : "png textures")); } private static string Str(BinaryReader r) { return Encoding.UTF8.GetString(r.ReadBytes(r.ReadUInt16())); } private static Vector3[] V3(BinaryReader r, int n) { //IL_001f: Unknown result type (might be due to invalid IL or missing references) //IL_0024: Unknown result type (might be due to invalid IL or missing references) Vector3[] array = (Vector3[])(object)new Vector3[n]; for (int i = 0; i < n; i++) { array[i] = new Vector3(r.ReadSingle(), r.ReadSingle(), r.ReadSingle()); } return array; } public static Texture2D Texture(string file) { //IL_006c: Unknown result type (might be due to invalid IL or missing references) //IL_0072: Expected O, but got Unknown if (textures.TryGetValue(file, out var value)) { return value; } string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file); if ((Object)(object)bundle != (Object)null) { value = bundle.LoadAsset<Texture2D>(fileNameWithoutExtension); } if ((Object)(object)value == (Object)null && !fileNameWithoutExtension.Contains("__")) { string path = Path.Combine(Path.Combine(root, "textures"), file); if (File.Exists(path)) { value = new Texture2D(2, 2, (TextureFormat)4, true); if (loadImage != null && (bool)loadImage.Invoke(null, new object[3] { value, File.ReadAllBytes(path), false })) { value.Compress(true); value.Apply(true, true); } else { value = null; } } } if ((Object)(object)value != (Object)null) { ((Object)value).name = "rhw_" + fileNameWithoutExtension; ((Texture)value).wrapMode = (TextureWrapMode)1; ((Texture)value).anisoLevel = 4; } textures[file] = value; return value; } public static Mesh MeshOf(NodeData nd, string label) { //IL_0015: Unknown result type (might be due to invalid IL or missing references) //IL_001a: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Expected O, but got Unknown if ((Object)(object)nd.Mesh != (Object)null) { return nd.Mesh; } Mesh val = new Mesh { name = "rhw_" + label }; if (nd.Pos.Length > 65535) { val.indexFormat = (IndexFormat)1; } val.vertices = nd.Pos; val.normals = nd.Nrm; val.uv = nd.Uv; val.subMeshCount = nd.SubIdx.Length; for (int i = 0; i < nd.SubIdx.Length; i++) { val.SetTriangles(nd.SubIdx[i], i); } val.RecalculateBounds(); val.RecalculateTangents(); val.UploadMeshData(true); return nd.Mesh = val; } } public static class Swap { private static readonly HashSet<int> done = new HashSet<int>(); private static readonly Dictionary<(int, string), Material> mats = new Dictionary<(int, string), Material>(); private static readonly string[] OtherUvMaps = new string[8] { "_BumpMap", "_MetallicGlossMap", "_OcclusionMap", "_EmissionMap", "_DetailAlbedoMap", "_DetailNormalMap", "_ParallaxMap", "_SpecGlossMap" }; private static Texture2D clear; private static Texture2D Clear { get { //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Expected O, but got Unknown if ((Object)(object)clear != (Object)null) { return clear; } clear = new Texture2D(4, 4, (TextureFormat)4, false) { name = "rhw_clear" }; Color32[] pixels = (Color32[])(object)new Color32[16]; clear.SetPixels32(pixels); clear.Apply(false, true); return clear; } } public static void ApplyAll() { ObjectDB instance = ObjectDB.instance; if ((Object)(object)instance == (Object)null || instance.m_items == null || instance.m_items.Count == 0) { return; } int num = 0; int num2 = 0; foreach (ItemData value in Data.Items.Values) { GameObject itemPrefab = instance.GetItemPrefab(value.Prefab); if ((Object)(object)itemPrefab == (Object)null) { continue; } if (!done.Add(((Object)itemPrefab).GetInstanceID())) { num++; continue; } try { if (Apply(itemPrefab, value)) { num++; } else { num2++; } } catch (Exception ex) { num2++; Plugin.Log.LogWarning((object)(value.Prefab + ": " + ex.Message)); } } Plugin.Log.LogInfo((object)($"Retextured {num} weapons and shields" + ((num2 > 0) ? $", {num2} left as the game's (see the warnings above)" : "") + ".")); } private static Transform Find(Transform root, NodeData nd) { Transform val = root; int[] index = nd.Index; foreach (int num in index) { if ((Object)(object)val == (Object)null || num >= val.childCount) { val = null; break; } val = val.GetChild(num); } string text = nd.Path.Substring(nd.Path.LastIndexOf('/') + 1); if ((Object)(object)val != (Object)null && ((Object)val).name == text) { return val; } if (nd.Path.Length != 0) { return root.Find(nd.Path); } return root; } private static bool Apply(GameObject prefab, ItemData it) { //IL_00f9: Unknown result type (might be due to invalid IL or missing references) //IL_00fe: Unknown result type (might be due to invalid IL or missing references) //IL_0102: Unknown result type (might be due to invalid IL or missing references) //IL_0107: Unknown result type (might be due to invalid IL or missing references) //IL_0110: Unknown result type (might be due to invalid IL or missing references) //IL_0115: Unknown result type (might be due to invalid IL or missing references) //IL_012d: Unknown result type (might be due to invalid IL or missing references) //IL_0134: Unknown result type (might be due to invalid IL or missing references) //IL_0139: Unknown result type (might be due to invalid IL or missing references) //IL_013e: Unknown result type (might be due to invalid IL or missing references) //IL_0191: Unknown result type (might be due to invalid IL or missing references) //IL_01a0: Unknown result type (might be due to invalid IL or missing references) //IL_01af: Unknown result type (might be due to invalid IL or missing references) //IL_01be: Unknown result type (might be due to invalid IL or missing references) //IL_014d: Unknown result type (might be due to invalid IL or missing references) //IL_0154: Unknown result type (might be due to invalid IL or missing references) //IL_0159: Unknown result type (might be due to invalid IL or missing references) //IL_015e: Unknown result type (might be due to invalid IL or missing references) Texture2D val = Data.Texture(it.Texture); if ((Object)(object)val == (Object)null) { Plugin.Log.LogWarning((object)(it.Prefab + ": texture missing")); return false; } bool result = false; foreach (NodeData node in it.Nodes) { Transform val2 = Find(prefab.transform, node); MeshFilter val3 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).GetComponent<MeshFilter>() : null); MeshRenderer val4 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).GetComponent<MeshRenderer>() : null); if ((Object)(object)val3 == (Object)null || (Object)(object)val4 == (Object)null || (Object)(object)val3.sharedMesh == (Object)null) { Plugin.Log.LogWarning((object)(it.Prefab + ": no mesh at '" + node.Path + "'")); continue; } Mesh val5 = Data.MeshOf(node, it.Prefab + "_" + ((Object)val2).name); Bounds bounds = val3.sharedMesh.bounds; Bounds bounds2 = val5.bounds; Vector3 val6 = ((Bounds)(ref bounds)).size; float num = Mathf.Max(0.002f, ((Vector3)(ref val6)).magnitude * 0.02f); val6 = ((Bounds)(ref bounds)).center - ((Bounds)(ref bounds2)).center; if (!(((Vector3)(ref val6)).magnitude > num)) { val6 = ((Bounds)(ref bounds)).size - ((Bounds)(ref bounds2)).size; if (!(((Vector3)(ref val6)).magnitude > num)) { Material[] sharedMaterials = ((Renderer)val4).sharedMaterials; Material[] array = (Material[])(object)new Material[node.SubMat.Length]; for (int i = 0; i < array.Length; i++) { Material val7 = null; Material[] array2 = sharedMaterials; foreach (Material val8 in array2) { if ((Object)(object)val8 != (Object)null && Clean(((Object)val8).name) == node.SubMat[i]) { val7 = val8; break; } } if ((Object)(object)val7 == (Object)null) { val7 = ((sharedMaterials.Length != 0) ? sharedMaterials[Mathf.Min(i, sharedMaterials.Length - 1)] : null); } array[i] = ((node.SubGlow[i] || (Object)(object)val7 == (Object)null) ? val7 : Retex(val7, val, it)); } val3.sharedMesh = val5; ((Renderer)val4).sharedMaterials = array; result = true; continue; } } Plugin.Log.LogWarning((object)$"{it.Prefab}/{node.Path}: model doesn't line up (game {((Bounds)(ref bounds)).center}/{((Bounds)(ref bounds)).size}, ours {((Bounds)(ref bounds2)).center}/{((Bounds)(ref bounds2)).size}); left as is"); } return result; } private static string Clean(string n) { return n.Replace(" (Instance)", "").Trim(); } private static Material Retex(Material src, Texture2D tex, ItemData it) { //IL_0025: Unknown result type (might be due to invalid IL or missing references) //IL_002a: Unknown result type (might be due to invalid IL or missing references) //IL_004c: Expected O, but got Unknown //IL_0054: Unknown result type (might be due to invalid IL or missing references) //IL_005f: Unknown result type (might be due to invalid IL or missing references) (int, string) key = (((Object)src).GetInstanceID(), it.Prefab); if (mats.TryGetValue(key, out var value)) { return value; } value = new Material(src) { name = Clean(((Object)src).name) + "_rhw_" + it.Prefab }; value.mainTexture = (Texture)(object)tex; value.mainTextureScale = Vector2.one; value.mainTextureOffset = Vector2.zero; if (it.KeepsLayout) { return mats[key] = value; } string[] otherUvMaps = OtherUvMaps; foreach (string text in otherUvMaps) { if (value.HasProperty(text) && !((Object)(object)value.GetTexture(text) == (Object)null)) { string value2; Texture2D val2 = (it.Maps.TryGetValue(text, out value2) ? Data.Texture(value2) : null); value.SetTexture(text, (Texture)(object)val2); if ((Object)(object)val2 == (Object)null) { TurnOff(value, text); } } } if (value.HasProperty("_StyleTex") && (Object)(object)value.GetTexture("_StyleTex") != (Object)null) { value.SetTexture("_StyleTex", (Texture)(object)Clear); } return mats[key] = value; } private static void TurnOff(Material m, string map) { //IL_0082: Unknown result type (might be due to invalid IL or missing references) switch (map) { case "_BumpMap": m.DisableKeyword("_NORMALMAP"); break; case "_MetallicGlossMap": m.DisableKeyword("_METALLICGLOSSMAP"); m.DisableKeyword("_USEGLOSSMAP_ON"); break; case "_SpecGlossMap": m.DisableKeyword("_SPECGLOSSMAP"); break; case "_EmissionMap": m.DisableKeyword("_EMISSION"); if (m.HasProperty("_EmissionColor")) { m.SetColor("_EmissionColor", Color.black); } break; } } public static void Dump() { ObjectDB instance = ObjectDB.instance; if ((Object)(object)instance == (Object)null) { return; } foreach (ItemData value in Data.Items.Values) { GameObject itemPrefab = instance.GetItemPrefab(value.Prefab); if ((Object)(object)itemPrefab == (Object)null) { Plugin.Log.LogInfo((object)("[dump] " + value.Prefab + ": not in ObjectDB")); continue; } Renderer[] componentsInChildren = itemPrefab.GetComponentsInChildren<Renderer>(true); foreach (Renderer val in componentsInChildren) { Material[] sharedMaterials = val.sharedMaterials; foreach (Material val2 in sharedMaterials) { if ((Object)(object)val2 == (Object)null) { continue; } StringBuilder stringBuilder = new StringBuilder(); string[] texturePropertyNames = val2.GetTexturePropertyNames(); foreach (string text in texturePropertyNames) { Texture texture = val2.GetTexture(text); if ((Object)(object)texture != (Object)null) { stringBuilder.Append(text).Append('=').Append(((Object)texture).name) .Append(' '); } } Plugin.Log.LogInfo((object)$"[dump] {value.Prefab} {((object)val).GetType().Name} '{((Object)val).name}' mat '{((Object)val2).name}' shader '{((Object)val2.shader).name}' {stringBuilder}"); } } } } } }