Skip to content

Commit

Permalink
v1.0 (BepInEx)
Browse files Browse the repository at this point in the history
Other than moving to BepInEx, I made it the scale values were adjusted a bit because it looked weird.
Oh, and this loses the functionality of previewing the menu and the submenu organization since that's dependent on the ModConfigurationManager
I probably broke the workflow again lol.
  • Loading branch information
Tribow committed Jan 14, 2025
1 parent 3a200fb commit 1fb30f3
Show file tree
Hide file tree
Showing 11 changed files with 463 additions and 214 deletions.
6 changes: 0 additions & 6 deletions .gitmodules

This file was deleted.

157 changes: 157 additions & 0 deletions Distance.NitronicHUD/Assets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Distance.NitronicHUD
{
public class Assets
{
private string _filePath = null;

private string RootDirectory { get; }
private string FileName { get; set; }
private string FilePath => _filePath ?? Path.Combine(Path.Combine(RootDirectory, "Assets"), FileName);

public object Bundle { get; private set; }

private Assets() { }

/// <summary>
/// Attempts to construct a Unity AssetBundle via a Centrifuge Type Bridge.
/// You will have to cast the Bundle property to Unity's AssetBundle type for usage.
/// </summary>
/// <param name="fileName">Filename/path relative to mod's private asset directory.</param>
public Assets(string fileName, bool fromRootDirectory)
{
RootDirectory = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
FileName = fileName;
string RootFilePath = Path.Combine(RootDirectory, FileName);

if (fromRootDirectory)
{
if(!File.Exists(RootFilePath))
{
Mod.Log.LogInfo($"Couldn't find requested asset bundle at {RootFilePath}");
return;
}
}
else
{
if (!File.Exists(FilePath))
{
Mod.Log.LogInfo($"Couldn't find requested asset bundle at {FilePath}");
return;
}
}

if (fromRootDirectory)
{
Bundle = Load(RootFilePath);
}
else
{
Bundle = Load();
}
}

/// <summary>
/// Attempts to construct a Unity AssetBundle via a Centrifuge Type Bridge.
/// You will have to cast the Bundle property to Unity's AssetBundle type for usage.
/// </summary>
/// <param name="filePath">An absolute path to the AssetBundle</param>
public static Assets FromUnsafePath(string filePath)
{
if (!File.Exists(filePath))
{
Mod.Log.LogInfo($"Could not find requested asset bundle at {filePath}");
return null;
}

var ret = new Assets
{
_filePath = filePath,
FileName = Path.GetFileName(filePath)
};
ret.Bundle = ret.Load();

if (ret.Bundle == null)
return null;

return ret;
}

private object Load()
{
try
{
var assetBundle = AssetBundleBridge.LoadFrom(FilePath);
Mod.Log.LogInfo($"Loaded asset bundle {FilePath}");

return assetBundle;
}
catch (Exception ex)
{
Mod.Log.LogInfo(ex);
return null;
}
}

private object Load(string filePath)
{
try
{
var assetBundle = AssetBundleBridge.LoadFrom(filePath);
Mod.Log.LogInfo($"Loaded asset bundle {filePath}");

return assetBundle;
}
catch (Exception ex)
{
Mod.Log.LogInfo($"Could not load asset bundle {filePath}");
Mod.Log.LogInfo(ex);
return null;
}
}
}

internal static class AssetBundleBridge
{
public static Type AssetBundleType => Kernel.FindTypeByFullName(
"UnityEngine.AssetBundle",
"UnityEngine"
);

private static MethodInfo LoadFromFile => AssetBundleType.GetMethod(
"LoadFromFile",
new[] { typeof(string) }
);

public static object LoadFrom(string path)
{
return LoadFromFile.Invoke(null, new[] { path });
}
}

internal static class Kernel
{
internal static Type FindTypeByFullName(string fullName, string assemblyFilter)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => a.GetName().Name.Contains(assemblyFilter));

foreach (var asm in assemblies)
{
var type = asm.GetTypes().FirstOrDefault(t => t.FullName == fullName);

if (type == null)
continue;

return type;
}

Mod.Log.LogInfo($"Type {fullName} wasn't found in the main AppDomain at this moment.");
throw new Exception($"Type {fullName} wasn't found in the main AppDomain at this moment.");
}
}
}
19 changes: 13 additions & 6 deletions Distance.NitronicHUD/Distance.NitronicHUD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony, Version=2.5.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\HarmonyX.2.5.1\lib\net35\0Harmony.dll</HintPath>
<Reference Include="0Harmony">
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Distance\BepInEx\core\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp, Version=9.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\Assembly-CSharp.dll</HintPath>
<HintPath>..\..\publicized_assemblies\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="BepInEx">
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Distance\BepInEx\core\BepInEx.dll</HintPath>
</Reference>
<Reference Include="Centrifuge.Distance, Version=3.0.7868.41513, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\Centrifuge.Distance.dll</HintPath>
Expand Down Expand Up @@ -75,25 +78,29 @@
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\UnityEngine.dll</HintPath>
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Distance\Distance_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.Networking, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\UnityEngine.Networking.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\UnityEngine.UI.dll</HintPath>
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Distance\Distance_Data\Managed\UnityEngine.UI.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Assets.cs" />
<Compile Include="ConfigurationLogic.cs" />
<Compile Include="Data\VisualCountdownDigit.cs" />
<Compile Include="Data\VisualDisplayContent.cs" />
<Compile Include="Mod.cs" />
<Compile Include="Flags.cs" />
<Compile Include="Harmony\Assembly-CSharp\StatsManager\OnEventPlayerAdded.cs" />
<Compile Include="Patches\Assembly-CSharp\GameManager\Awake.cs" />
<Compile Include="Patches\Assembly-CSharp\StatsManager\OnEventPlayerAdded.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scripts\VisualCountdown.cs" />
<Compile Include="Scripts\VisualDisplay.cs" />
<Compile Include="Utilities.cs" />
<Compile Include="Vehicle.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Distance.NitronicHUD.targets" />
Expand Down
19 changes: 19 additions & 0 deletions Distance.NitronicHUD/Flags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Events.Level;
using Events.LevelEditor;
using Events.RaceEnd;
using Events.Scene;
using System.Linq;

namespace Distance.NitronicHUD
Expand Down Expand Up @@ -50,6 +51,19 @@ internal static void SubscribeEvents()
EnterPlayMode.Subscribe(OnLevelEditorEnterPlayMode);
EnterEditorMode.Subscribe(OnLevelEditorEnterEditorMode);
UninitializeOptimizations.Subscribe(OnLevelUninitializeOptimizations);
BeginSceneSwitchFadeOut.Subscribe(OnSceneChanged);
}

internal static void UnsubscribeEvents()
{
PauseToggled.Unsubscribe(OnGamePauseToggled);
ModeInitialized.Unsubscribe(OnGameModeInitialized);
ModeStarted.Unsubscribe(OnGameModeModeStarted);
LocalCarHitFinish.Unsubscribe(OnRaceEndLocalCarHitFinish);
EnterPlayMode.Unsubscribe(OnLevelEditorEnterPlayMode);
EnterEditorMode.Unsubscribe(OnLevelEditorEnterEditorMode);
UninitializeOptimizations.Unsubscribe(OnLevelUninitializeOptimizations);
BeginSceneSwitchFadeOut.Unsubscribe(OnSceneChanged);
}

private static void OnLevelUninitializeOptimizations(UninitializeOptimizations.Data data)
Expand Down Expand Up @@ -88,5 +102,10 @@ private static void OnGamePauseToggled(PauseToggled.Data data)
{
IsPaused = data.paused_;
}

private static void OnSceneChanged(BeginSceneSwitchFadeOut.Data data)
{
IsModeStarted = false;
}
}
}
Loading

0 comments on commit 1fb30f3

Please sign in to comment.