Skip to content

Commit

Permalink
Add SVS_BGMLoader (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco authored Sep 4, 2024
1 parent 82a1c86 commit 28a79cd
Show file tree
Hide file tree
Showing 5 changed files with 526 additions and 2 deletions.
10 changes: 9 additions & 1 deletion BepisPlugins.sln
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HC_ConfigurationManager", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HC_BGMLoader", "src\HC_BGMLoader\HC_BGMLoader.csproj", "{C4B11A58-93BC-45FD-99F9-30AF3E6AF294}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVS_ConfigurationManager", "src\SVS_ConfigurationManager\SVS_ConfigurationManager.csproj", "{035907C1-0012-4581-81A4-4D0857EFE025}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SVS_ConfigurationManager", "src\SVS_ConfigurationManager\SVS_ConfigurationManager.csproj", "{035907C1-0012-4581-81A4-4D0857EFE025}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVS_BGMLoader", "src\SVS_BGMLoader\SVS_BGMLoader.csproj", "{7589ADA2-68F4-4404-BE7F-BCEBF04DA99D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -363,6 +365,10 @@ Global
{035907C1-0012-4581-81A4-4D0857EFE025}.Debug|Any CPU.Build.0 = Debug|Any CPU
{035907C1-0012-4581-81A4-4D0857EFE025}.Release|Any CPU.ActiveCfg = Release|Any CPU
{035907C1-0012-4581-81A4-4D0857EFE025}.Release|Any CPU.Build.0 = Release|Any CPU
{7589ADA2-68F4-4404-BE7F-BCEBF04DA99D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7589ADA2-68F4-4404-BE7F-BCEBF04DA99D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7589ADA2-68F4-4404-BE7F-BCEBF04DA99D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7589ADA2-68F4-4404-BE7F-BCEBF04DA99D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -440,6 +446,7 @@ Global
{D00F1931-BE72-42ED-934B-E03DD1378257} = {66C8608F-4585-4E2C-8E8C-F4FDCC2CE382}
{C4B11A58-93BC-45FD-99F9-30AF3E6AF294} = {BF4829EE-3EC0-4B70-8E8C-26C7C4E9BD43}
{035907C1-0012-4581-81A4-4D0857EFE025} = {66C8608F-4585-4E2C-8E8C-F4FDCC2CE382}
{7589ADA2-68F4-4404-BE7F-BCEBF04DA99D} = {BF4829EE-3EC0-4B70-8E8C-26C7C4E9BD43}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D3F10C8B-FD6C-43DA-94B4-1D9688BBE11B}
Expand Down Expand Up @@ -490,6 +497,7 @@ Global
src\Shared\Shared.projitems*{737a4054-4a2b-48e9-842a-a38d24361435}*SharedItemsImports = 4
src\Core_ExtensibleSaveFormat\Core_ExtensibleSaveFormat.projitems*{747a4e61-34f4-4790-a418-bb2cb03450e3}*SharedItemsImports = 4
src\Shared\Shared.projitems*{747a4e61-34f4-4790-a418-bb2cb03450e3}*SharedItemsImports = 4
src\Shared\Shared.projitems*{7589ada2-68f4-4404-be7f-bcebf04da99d}*SharedItemsImports = 5
src\Core_Sideloader\Core_Sideloader.projitems*{75c19265-0ba8-4373-9254-22fb6102788d}*SharedItemsImports = 4
src\Core_Sideloader_Studio\Core_Sideloader_Studio.projitems*{75c19265-0ba8-4373-9254-22fb6102788d}*SharedItemsImports = 4
src\Shared\Shared.projitems*{75c19265-0ba8-4373-9254-22fb6102788d}*SharedItemsImports = 4
Expand Down
1 change: 0 additions & 1 deletion src/HC_BGMLoader/HC_BGMLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
</Target>

<ItemGroup>
<!--BepInEx.ConfigurationManager is a BepInEx5 package and there's a compatibility warning about .NET version, but it actually works -->
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.668" />
<PackageReference Include="BepInEx.Unity.Mono.Preloader" Version="6.0.0-be.668" />
<PackageReference Include="IllusionLibs.HoneyCome.Assembly-CSharp" Version="2023.99.12.15" />
Expand Down
74 changes: 74 additions & 0 deletions src/SVS_BGMLoader/Core.BGMLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using BepisPlugins;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace BGMLoader
{
/// <summary>
/// Place .wav files in BepInEx/plugins/introclips folder to load them in place of startup sounds
/// </summary>
[BepInProcess(Constants.GameProcessName)]
[BepInPlugin(GUID, PluginName, Version)]
public class BGMLoader : BasePlugin
{
public const string GUID = "com.bepis.bgmloader";
public const string PluginName = "BGM Loader";
public const string Version = Metadata.PluginsVersion;
public static string IntroClipsDirectory = Path.Combine(Paths.PluginPath, "introclips");

private static ManualLogSource Logger = null!;
private static FileInfo[] _clips = null!;

public override void Load()
{
Logger = Log;

Task.Run(() =>
{
try
{
var dir = Directory.CreateDirectory(IntroClipsDirectory);
_clips = dir.GetFiles("*.wav", SearchOption.AllDirectories);
Logger.LogInfo("Found " + _clips.Length + " custom intro clips");
Harmony.CreateAndPatchAll(typeof(BGMLoader), GUID);
}
catch (Exception e)
{
Logger.LogError("Failed to load custom intro clips - " + e);
}
});
}

[HarmonyPrefix]
[HarmonyPatch(typeof(Manager.Sound), nameof(Manager.Sound.Play), typeof(Manager.Sound.Type), typeof(AudioClip), typeof(float))]
private static void TitleCallOverride(Manager.Sound.Type type, ref AudioClip clip)
{
if (type == Manager.Sound.Type.SystemSE && clip.name != null && Regex.IsMatch(clip.name, @"^sv_\d\d\d_se_\d\d\d_\d\d\d$"))
{
try
{
// Need to make sure it's not some other sound effect, this seems to work (map000 is title screen itself)
var sceneName = SceneManager.GetActiveScene().name;
if (sceneName != "Logo" && sceneName != "map000") return;

var pick = _clips[UnityEngine.Random.Range(0, _clips.Length)];
var clipData = File.ReadAllBytes(pick.FullName);

// BUG Some clips are not giving sound, but they are playing
clip = WavUtility.ToAudioClip(clipData); // slowest step mostly because of AudioClip calls

Logger.LogInfo("Playing custom intro clip - " + pick.Name);
}
catch (Exception e)
{
Logger.LogError("Failed to play custom intro clip - " + e);
}
}
}
}
}
38 changes: 38 additions & 0 deletions src/SVS_BGMLoader/SVS_BGMLoader.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>BGMLoader</RootNamespace>
<OutputPath>..\..\bin\BepInEx\plugins\SVS_BepisPlugins\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<DebugType>embedded</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
<DefineConstants>$(DefineConstants);SVS</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
<DefineConstants>$(DefineConstants);SVS</DefineConstants>
</PropertyGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Delete Files="$(OutputPath)\SVS_BGMLoader.deps.json" />
</Target>

<ItemGroup>
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.668" />
<PackageReference Include="BepInEx.Unity.Mono.Preloader" Version="6.0.0-be.668" />
<PackageReference Include="IllusionLibs.SamabakeScramble.Assembly-CSharp" Version="2024.8.30.1" />
<PackageReference Include="IllusionLibs.SamabakeScramble.Il2Cppmscorlib" Version="2024.8.30.1" />
<PackageReference Include="IllusionLibs.SamabakeScramble.UnityEngine" Version="2021.3.33.1" />
<PackageReference Include="IllusionLibs.SamabakeScramble.UnityEngine.AudioModule" Version="2021.3.33.1" />
<PackageReference Include="IllusionLibs.SamabakeScramble.UnityEngine.UI" Version="2021.3.33.1" />
</ItemGroup>

<Import Project="..\Shared\Shared.projitems" Label="Shared" />
</Project>
Loading

0 comments on commit 28a79cd

Please sign in to comment.