This commit is contained in:
NotAKidoS 2023-04-11 23:34:08 -05:00
parent d318611246
commit 66943235c1
8 changed files with 277 additions and 0 deletions

147
PropUndoButton/Main.cs Normal file
View file

@ -0,0 +1,147 @@
using ABI_RC.Core.AudioEffects;
using ABI_RC.Core.Savior;
using ABI_RC.Core.Util;
using MelonLoader;
using System.Reflection;
using UnityEngine;
namespace NAK.Melons.UndoPropButton;
// https://pixabay.com/sound-effects/selection-sounds-73225/
public class PropUndoButton : MelonMod
{
private static readonly MelonPreferences_Category Category =
MelonPreferences.CreateCategory(nameof(PropUndoButton));
public static readonly MelonPreferences_Entry<bool> EntryEnabled =
Category.CreateEntry("Enabled", true, description: "Toggle Undo Prop Button.");
public static readonly MelonPreferences_Entry<bool> EntryUseSFX =
Category.CreateEntry("Use SFX", true, description: "Enable or disable undo SFX.");
// audio clip names, InterfaceAudio adds "PropUndo_" prefix
public const string sfx_spawn = "PropUndo_sfx_spawn";
public const string sfx_undo = "PropUndo_sfx_undo";
public const string sfx_warn = "PropUndo_sfx_warn";
public override void OnInitializeMelon()
{
HarmonyInstance.Patch( // prop spawn sfx
typeof(CVRSyncHelper).GetMethod(nameof(CVRSyncHelper.SpawnProp)),
null,
new HarmonyLib.HarmonyMethod(typeof(PropUndoButton).GetMethod(nameof(OnSpawnProp), BindingFlags.NonPublic | BindingFlags.Static))
);
HarmonyInstance.Patch( // desktop input patch so we don't run in menus/gui
typeof(InputModuleMouseKeyboard).GetMethod(nameof(InputModuleMouseKeyboard.UpdateInput)),
null,
new HarmonyLib.HarmonyMethod(typeof(PropUndoButton).GetMethod(nameof(OnUpdateInput), BindingFlags.NonPublic | BindingFlags.Static))
);
SetupDefaultAudioClips();
}
private void SetupDefaultAudioClips()
{
// PropUndo and audio folders do not exist, create them if dont exist yet
string path = Application.streamingAssetsPath + "/Cohtml/UIResources/GameUI/mods/PropUndo/audio/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
LoggerInstance.Msg("Created PropUndo/audio directory!");
}
// copy embedded resources to this folder if they do not exist
string[] clipNames = { "sfx_spawn.wav", "sfx_undo.wav", "sfx_warn.wav" };
foreach (string clipName in clipNames)
{
string clipPath = Path.Combine(path, clipName);
if (!File.Exists(clipPath))
{
// read the clip data from embedded resources
byte[] clipData = null;
string resourceName = "PropUndoButton.SFX." + clipName;
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
clipData = new byte[stream.Length];
stream.Read(clipData, 0, clipData.Length);
}
// write the clip data to the file
using (FileStream fileStream = new FileStream(clipPath, FileMode.CreateNew))
{
fileStream.Write(clipData, 0, clipData.Length);
}
LoggerInstance.Msg("Placed missing sfx in audio folder: " + clipName);
}
}
}
private static void OnUpdateInput()
{
if (Input.GetKeyDown(KeyCode.Z))
{
DeleteLatestSpawnable();
}
}
private static void OnSpawnProp()
{
if (!EntryEnabled.Value) return;
if (!MetaPort.Instance.worldAllowProps || !MetaPort.Instance.settings.GetSettingsBool("ContentFilterPropsEnabled", false))
{
PlayAudioModule(sfx_warn);
return;
}
if (GetAllProps().Count >= 20)
{
PlayAudioModule(sfx_warn);
return;
}
PlayAudioModule(sfx_spawn);
}
private static void DeleteLatestSpawnable()
{
if (!EntryEnabled.Value) return;
var propData = GetLatestProp();
if (propData == null)
{
PlayAudioModule(sfx_warn);
return;
}
if (propData.Spawnable == null)
{
propData.Recycle();
// what should i do here?
return;
}
propData.Spawnable.Delete();
PlayAudioModule(sfx_undo);
}
private static void PlayAudioModule(string module)
{
if (!EntryUseSFX.Value) return;
InterfaceAudio.PlayModule(module);
}
private static CVRSyncHelper.PropData GetLatestProp()
{
// should already be sorted by spawn order
return CVRSyncHelper.Props.LastOrDefault((CVRSyncHelper.PropData match) => match.SpawnedBy == MetaPort.Instance.ownerId);
}
private static List<CVRSyncHelper.PropData> GetAllProps()
{
// im not storing the count because there is good chance itll desync from server
return CVRSyncHelper.Props.FindAll((CVRSyncHelper.PropData match) => match.SpawnedBy == MetaPort.Instance.ownerId);
}
}

View file

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<None Remove="SFX\sfx_spawn.wav" />
<None Remove="SFX\sfx_undo.wav" />
<None Remove="SFX\sfx_warn.wav" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="SFX\sfx_spawn.wav" />
<EmbeddedResource Include="SFX\sfx_undo.wav" />
<EmbeddedResource Include="SFX\sfx_warn.wav" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\MelonLoader\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp-firstpass">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\Assembly-CSharp-firstpass.dll</HintPath>
</Reference>
<Reference Include="MelonLoader">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\MelonLoader\MelonLoader.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.InputLegacyModule">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\ChilloutVR_Data\Managed\UnityEngine.InputLegacyModule.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="Deploy" AfterTargets="Build">
<Copy SourceFiles="$(TargetPath)" DestinationFolder="C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\Mods\" />
<Message Text="Copied $(TargetPath) to C:\Program Files (x86)\Steam\steamapps\common\ChilloutVR\Mods\" Importance="high" />
</Target>
</Project>

View file

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PropUndoButton", "PropUndoButton.csproj", "{240E33F2-E4DD-458D-8E79-96073872CE65}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{240E33F2-E4DD-458D-8E79-96073872CE65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{240E33F2-E4DD-458D-8E79-96073872CE65}.Debug|Any CPU.Build.0 = Debug|Any CPU
{240E33F2-E4DD-458D-8E79-96073872CE65}.Release|Any CPU.ActiveCfg = Release|Any CPU
{240E33F2-E4DD-458D-8E79-96073872CE65}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C8F281CB-C5D8-4C4B-A7F6-9F1702C436EB}
EndGlobalSection
EndGlobal

View file

@ -0,0 +1,32 @@
using NAK.Melons.UndoPropButton.Properties;
using MelonLoader;
using System.Reflection;
[assembly: AssemblyVersion(AssemblyInfoParams.Version)]
[assembly: AssemblyFileVersion(AssemblyInfoParams.Version)]
[assembly: AssemblyInformationalVersion(AssemblyInfoParams.Version)]
[assembly: AssemblyTitle(nameof(NAK.Melons.UndoPropButton))]
[assembly: AssemblyCompany(AssemblyInfoParams.Author)]
[assembly: AssemblyProduct(nameof(NAK.Melons.UndoPropButton))]
[assembly: MelonInfo(
typeof(NAK.Melons.UndoPropButton.PropUndoButton),
nameof(NAK.Melons.UndoPropButton),
AssemblyInfoParams.Version,
AssemblyInfoParams.Author,
downloadLink: "https://github.com/NotAKidOnSteam/UndoPropButton"
)]
[assembly: MelonGame("Alpha Blend Interactive", "ChilloutVR")]
[assembly: MelonPlatform(MelonPlatformAttribute.CompatiblePlatforms.WINDOWS_X64)]
[assembly: MelonPlatformDomain(MelonPlatformDomainAttribute.CompatibleDomains.MONO)]
[assembly: MelonOptionalDependencies("BTKUILib")]
[assembly: HarmonyDontPatchAll]
namespace NAK.Melons.UndoPropButton.Properties;
internal static class AssemblyInfoParams
{
public const string Version = "1.0.0";
public const string Author = "NotAKidoS";
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,23 @@
{
"_id": -1,
"name": "PropUndoButton",
"modversion": "1.0.0",
"gameversion": "2022r170",
"loaderversion": "0.5.7",
"modtype": "Mod",
"author": "NotAKidoS",
"description": "Press Z to undo latest spawned prop. Includes SFX for prop spawn, undo, and warning, which can be disabled in settings.",
"searchtags": [
"prop",
"undo",
"bind",
"button"
],
"requirements": [
"None"
],
"downloadlink": "https://github.com/NotAKidOnSteam/PropUndoButton/releases/download/v1.0.0/PropUndoButton.dll",
"sourcelink": "https://github.com/NotAKidOnSteam/PropUndoButton/",
"changelog": "- Initial Release",
"embedcolor": "#00FFFF"
}