mirror of
https://github.com/muskit/H3VR-TNH-Quality-of-Life-Improvements.git
synced 2026-06-03 04:34:26 -07:00
Initial commit
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using Mono.Cecil;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MeatKit
|
||||
{
|
||||
public abstract class AssemblyModifier : ScriptableObject
|
||||
{
|
||||
[NonSerialized]
|
||||
public bool Applied = false;
|
||||
|
||||
public abstract void ApplyModification(AssemblyDefinition assembly);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c7e21420a1d84ca5947b2fd9eb9db6d8
|
||||
timeCreated: 1628213934
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using Mono.Cecil;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MeatKit
|
||||
{
|
||||
[CreateAssetMenu(menuName = "MeatKit/Assembly Editors/Enum", fileName = "New Enum Editor")]
|
||||
public class EnumModifier : AssemblyModifier
|
||||
{
|
||||
private const FieldAttributes Attributes =
|
||||
FieldAttributes.Static | FieldAttributes.Literal | FieldAttributes.Public | FieldAttributes.HasDefault;
|
||||
|
||||
[Tooltip("Specify the FULL NAME of the enum you want to change. e.g. Sub.Namespace.Type")]
|
||||
public string EnumName = "FistVR.FireArmRoundType";
|
||||
|
||||
[Tooltip("The new values you want to add to this enum")]
|
||||
public EnumValue[] AddedValues = new EnumValue[0];
|
||||
|
||||
public override void ApplyModification(AssemblyDefinition assembly)
|
||||
{
|
||||
// Try to get this type from the assembly. If it doesn't exist, we can just skip.
|
||||
TypeReference type = assembly.MainModule.GetType(EnumName);
|
||||
if (type == null) return;
|
||||
|
||||
var definition = type.Resolve();
|
||||
if (!definition.IsEnum)
|
||||
{
|
||||
Debug.LogError(EnumName + " is not an enum type!", this);
|
||||
Applied = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the new enum value to the type
|
||||
foreach (var value in AddedValues)
|
||||
definition.Fields.Add(new FieldDefinition(value.Name, Attributes, definition) {Constant = value.Value});
|
||||
|
||||
Applied = true;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct EnumValue
|
||||
{
|
||||
[Tooltip("The name of the new enum value")]
|
||||
public string Name;
|
||||
|
||||
[Tooltip("The new enum value")]
|
||||
public int Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 486e27304e8449efb6c6136482952c4d
|
||||
timeCreated: 1628213734
|
||||
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
|
||||
namespace MeatKit
|
||||
{
|
||||
[CustomEditor(typeof(EnumModifier))]
|
||||
public class EnumModifierEditor : Editor
|
||||
{
|
||||
private static readonly string[] CommonTypes =
|
||||
{
|
||||
"FistVR.FireArmRoundType",
|
||||
"FistVR.FireArmRoundClass",
|
||||
"FistVR.FireArmMagazineType",
|
||||
"FistVR.ItemSpawnerObjectDefinition.ItemSpawnerCategory",
|
||||
"FistVR.SosigEnemyID"
|
||||
};
|
||||
|
||||
private SerializedProperty _addedValues;
|
||||
private EnumModifier _enumModifier;
|
||||
private bool _isCustomType;
|
||||
private int _selectedType;
|
||||
|
||||
// Called when an object of this type is selected
|
||||
private void OnEnable()
|
||||
{
|
||||
// Get our properties and check if this is a common type
|
||||
_addedValues = serializedObject.FindProperty("AddedValues");
|
||||
_enumModifier = (EnumModifier) target;
|
||||
_selectedType = Array.IndexOf(CommonTypes, _enumModifier.EnumName);
|
||||
_isCustomType = _selectedType == -1 || string.IsNullOrEmpty(_enumModifier.EnumName);
|
||||
}
|
||||
|
||||
// Called to draw the inspector GUI
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
// I'll be real I have no idea what this does but the Unity docs had it so I'm not gonna mess with it
|
||||
serializedObject.Update();
|
||||
|
||||
// Use a toggle (checkbox) to determine if we're using a custom type or a commonly used one from the array
|
||||
_isCustomType = EditorGUILayout.Toggle("Custom type", _isCustomType);
|
||||
if (_isCustomType)
|
||||
_enumModifier.EnumName = EditorGUILayout.TextField("Enum name", _enumModifier.EnumName);
|
||||
else
|
||||
{
|
||||
if (_selectedType < 0 || _selectedType >= CommonTypes.Length) _selectedType = 0;
|
||||
_selectedType = EditorGUILayout.Popup("Type", _selectedType, CommonTypes);
|
||||
_enumModifier.EnumName = CommonTypes[_selectedType];
|
||||
}
|
||||
|
||||
// Draw the values field and then save the object
|
||||
EditorGUILayout.PropertyField(_addedValues, true);
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
|
||||
// Suggest to the user that all added enums should be negative
|
||||
if (_enumModifier.AddedValues.Any(x => x.Value >= 0))
|
||||
EditorGUILayout.HelpBox(
|
||||
"Your added enum values should be negative to avoid conflicts with vanilla items.",
|
||||
MessageType.Warning);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4980597d341c4eed913ae30680f3f9f3
|
||||
timeCreated: 1628218472
|
||||
Reference in New Issue
Block a user