Skip to content

Commit

Permalink
Fix custom counter support:tm:
Browse files Browse the repository at this point in the history
  • Loading branch information
NuggoDEV committed Feb 18, 2024
1 parent 8468b0a commit 7b8c995
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Counters+/Counters+.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@
<Private>False</Private>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<Private>False</Private>
<HintPath>$(BeatSaberDir)\Libs\Newtonsoft.Json.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<SpecificVersion>True</SpecificVersion>
</Reference>
<Reference Include="SemVer, Version=1.2.2.0, Culture=neutral, PublicKeyToken=a89bb7dc6f7a145c, processorArchitecture=MSIL">
<Private>False</Private>
Expand Down Expand Up @@ -234,6 +234,7 @@
<Compile Include="Counters\Spinometer.cs" />
<Compile Include="Custom\CustomConfigModel.cs" />
<Compile Include="Custom\CustomCounter.cs" />
<Compile Include="Custom\CustomCounterFeature.cs" />
<Compile Include="Harmony\CoreGameHUDControllerPatch.cs" />
<Compile Include="Installers\CoreInstaller.cs" />
<Compile Include="Installers\CountersInstaller.cs" />
Expand Down
101 changes: 101 additions & 0 deletions Counters+/Custom/CustomCounterFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using IPA.Loader;
using IPA.Loader.Features;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;

namespace CountersPlus.Custom
{
public class CustomCounterFeature : Feature
{
private Dictionary<PluginMetadata, CustomCounter> incompleteCustomCounters = new Dictionary<PluginMetadata, CustomCounter>(0);

protected override bool Initialize(PluginMetadata meta, JObject featureData)
{
CustomCounter counter;
try
{
counter = featureData.ToObject<CustomCounter>();
}
catch (Exception e)
{
InvalidMessage = $"Invalid data: {e}";
return false;
}

incompleteCustomCounters.Add(meta, counter);
return true;
}

public override void AfterInit(PluginMetadata meta)
{
if (incompleteCustomCounters.TryGetValue(meta, out CustomCounter counter))
{
if (!TryLoadType(ref counter.CounterType, meta, counter.CounterLocation))
{
Plugin.Logger.Error($"Failed to load a Type from the provided CounterLocation for {counter.Name}.");
return;
}
if (counter.BSML != null && counter.BSML.HasType && !TryLoadType(ref counter.BSML.HostType, meta, counter.BSML.Host))
{
Plugin.Logger.Error($"Failed to load a Type from the provided BSML Host for {counter.Name}.");
return;
}

Plugin.LoadedCustomCounters.Add(counter);
Plugin.Logger.Notice($"Loaded a Custom Counter ({counter.Name}).");
}
else
{
Plugin.Logger.Critical(@"A plugin has a defined Custom Counter, but Initialise was somehow not called.
How the hell did we even get here?");
}
}

private bool TryLoadType(ref Type typeToLoad, PluginMetadata meta, string location)
{
// totally didn't yoink this from BSIPA's ConfigProviderFeature
try
{
typeToLoad = meta.Assembly.GetType(location);
}
catch (ArgumentException)
{
InvalidMessage = $"Invalid type name {location}";
return false;
}
catch (Exception e) when (e is FileNotFoundException || e is FileLoadException || e is BadImageFormatException)
{
string filename;

switch (e)
{
case FileNotFoundException fn:
filename = fn.FileName;
goto hasFilename;
case FileLoadException fl:
filename = fl.FileName;
goto hasFilename;
case BadImageFormatException bi:
filename = bi.FileName;
hasFilename:
InvalidMessage = $"Could not find {filename} while loading type";
break;
default:
InvalidMessage = $"Error while loading type: {e}";
break;
}

return false;
}
catch (Exception e) // Is this unnecessary? Maybe.
{
InvalidMessage = $"An unknown error occured: {e}";
return false;
}

return true;
}
}
}
4 changes: 2 additions & 2 deletions Counters+/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.3.4")]
[assembly: AssemblyFileVersion("2.3.4")]
[assembly: AssemblyVersion("2.3.5")]
[assembly: AssemblyFileVersion("2.3.5")]
10 changes: 8 additions & 2 deletions Counters+/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
"id": "Counters+",
"name": "Counters+",
"author": "Caeden117",
"version": "2.3.4",
"version": "2.3.5",
"description": "A suite of enhancements for Beat Saber's UI.",
"icon": "CountersPlus.UI.Images.Logo.png",
"gameVersion": "1.34.2",
"dependsOn": {
"BSIPA": "^4.3.2",
"BeatSaberMarkupLanguage": "^1.8.1",
"SiraUtil": "^3.1.6"
},
"features": {
"IPA.DefineFeature": {
"name": "CountersPlus.CustomCounter",
"type": "CountersPlus.Custom.CustomCounterFeature"
}
}
}
}

0 comments on commit 7b8c995

Please sign in to comment.