Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove the cache as it's redundant
  • Loading branch information
toebeann committed Sep 1, 2023
1 parent 6d19ec5 commit ca138aa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 132 deletions.
74 changes: 0 additions & 74 deletions Patcher/Cache.cs

This file was deleted.

51 changes: 5 additions & 46 deletions Patcher/Patcher.cs
Expand Up @@ -21,11 +21,9 @@ public static class Patcher
#endregion

private static readonly Assembly assembly = Assembly.GetExecutingAssembly();

private static readonly string cachePath = Path.Combine(Paths.CachePath, $"{Path.GetFileNameWithoutExtension(assembly.Location)}.dat");
private static readonly string globalGameManagersPath = Path.Combine(Directory.GetParent(Paths.ManagedPath).FullName, "globalgamemanagers");

private static readonly ManualLogSource logger = Logger.CreateLogSource("UnityAudioPatcher");

private static readonly ConfigFile config = new(Path.Combine(Paths.ConfigPath, BepinexConfigData.FileName), true);

private static readonly ConfigEntry<bool> configPatchEnabled = config.Bind(
Expand Down Expand Up @@ -76,19 +74,6 @@ public static class Patcher
.Select(name => name.Trim())
.Distinct();

private static Cache GetCache()
{
try
{
return Cache.LoadFromFile(cachePath);
}
catch
{
logger.LogDebug($"Cache not found or data invalid, creatig new cache at {Path.GetFullPath(cachePath)}");
return new();
}
}

private static bool HasUnityAudioReferences()
{
var excludedAssemblyPrefixes = new[]
Expand All @@ -98,7 +83,7 @@ private static bool HasUnityAudioReferences()
};

return AssemblySearchPaths
.Where(path => Directory.Exists(path))
.Where(Directory.Exists)
.SelectMany(path => Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories))
.Distinct()
.Where(path => excludedAssemblyPrefixes.All(prefix => !Path.GetFileNameWithoutExtension(path).StartsWith(prefix)))
Expand Down Expand Up @@ -127,18 +112,6 @@ private static bool HasUnityAudioReferences()
_ => !HasUnityAudioReferences()
};

private static void UpdateAndSaveGlobalGameManagersCache()
{
var cache = GetCache();
cache.GlobalGameManagers = new()
{
UnityAudioDisabled = IntendedDisableAudioSetting(),
LastWriteTimestampTicks = File.GetLastWriteTimeUtc(globalGameManagersPath).Ticks
};
cache.SaveToFile(cachePath);
logger.LogInfo("Cache updated.");
}

// this is our entry method into the patch
public static void Finish()
{
Expand All @@ -158,17 +131,6 @@ private static void PatchGame()

logger.LogInfo($"{(intendedDisableAudioSetting ? "Disabling" : "Enabling")} Unity Audio...");

// check the cache to check the unity audio setting from the last time we inspected/wrote to the file
// if the write time of the file matches the timestamp from the cache AND the setting in the cache matches
// the intended setting, skip patching as it would be redundant
var cache = GetCache();
if (cache.GlobalGameManagers.LastWriteTimestampTicks == File.GetLastWriteTimeUtc(globalGameManagersPath).Ticks &&
cache.GlobalGameManagers.UnityAudioDisabled == intendedDisableAudioSetting)
{
logger.LogInfo($"Cached Unity Audio is already {(intendedDisableAudioSetting ? "disabled" : "enabled")}, skipping patching.");
return;
}

try
{
using var classPackageStream = assembly.GetManifestResourceStream(assembly.GetManifestResourceNames().Single(name => name.ToLowerInvariant().EndsWith("classdata.tpk")));
Expand All @@ -181,10 +143,9 @@ private static void PatchGame()
var baseField = manager.GetBaseField(ggmInstance, audioManager);
var disableAudio = baseField["m_DisableAudio"];

if (disableAudio.AsBool == intendedDisableAudioSetting) // if setting in ggm already matches intended, just update cache and exit
if (disableAudio.AsBool == intendedDisableAudioSetting) // if setting in ggm already matches intended, just exit
{
logger.LogInfo($"Unity Audio is already {(intendedDisableAudioSetting ? "disabled" : "enabled")}, updating cache and skipping patching.");
UpdateAndSaveGlobalGameManagersCache();
logger.LogInfo($"Unity Audio is already {(intendedDisableAudioSetting ? "disabled" : "enabled")}, skipping patching.");
return;
}

Expand All @@ -207,9 +168,7 @@ private static void PatchGame()
File.Delete(globalGameManagersPath);
File.Move(tempPath, globalGameManagersPath);

// log success and update cache
logger.LogMessage($"Unity Audio successfully {(intendedDisableAudioSetting ? "disabled" : "enabled")}, updating cache.");
UpdateAndSaveGlobalGameManagersCache();
logger.LogMessage($"Unity Audio successfully {(intendedDisableAudioSetting ? "disabled" : "enabled")}.");
}
catch (Exception exception)
{
Expand Down
16 changes: 4 additions & 12 deletions Patcher/Patcher.csproj
Expand Up @@ -12,28 +12,27 @@
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<LangVersion>10</LangVersion>
<LangVersion>latest</LangVersion>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<DebugSymbols>false</DebugSymbols>
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<Target Name="ZipBuild" AfterTargets="ILRepack">
<Copy SourceFiles="$(TargetPath)" DestinationFolder="..\bin\zip\BepInEx\patchers\Tobey\UnityAudio\" />
Expand All @@ -43,12 +42,8 @@
<Reference Include="System">
<Private>False</Private>
</Reference>
<Reference Include="System.Runtime.Serialization">
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Cache.cs" />
<Compile Include="Patcher.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
Expand All @@ -72,9 +67,6 @@
<Version>0.10.4</Version>
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>13.0.3</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
Expand Down

0 comments on commit ca138aa

Please sign in to comment.