Skip to content

Commit 1fb30f3

Browse files
committed
v1.0 (BepInEx)
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.
1 parent 3a200fb commit 1fb30f3

11 files changed

Lines changed: 463 additions & 214 deletions

File tree

.gitmodules

Lines changed: 0 additions & 6 deletions
This file was deleted.

Distance.NitronicHUD/Assets.cs

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace Distance.NitronicHUD
7+
{
8+
public class Assets
9+
{
10+
private string _filePath = null;
11+
12+
private string RootDirectory { get; }
13+
private string FileName { get; set; }
14+
private string FilePath => _filePath ?? Path.Combine(Path.Combine(RootDirectory, "Assets"), FileName);
15+
16+
public object Bundle { get; private set; }
17+
18+
private Assets() { }
19+
20+
/// <summary>
21+
/// Attempts to construct a Unity AssetBundle via a Centrifuge Type Bridge.
22+
/// You will have to cast the Bundle property to Unity's AssetBundle type for usage.
23+
/// </summary>
24+
/// <param name="fileName">Filename/path relative to mod's private asset directory.</param>
25+
public Assets(string fileName, bool fromRootDirectory)
26+
{
27+
RootDirectory = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);
28+
FileName = fileName;
29+
string RootFilePath = Path.Combine(RootDirectory, FileName);
30+
31+
if (fromRootDirectory)
32+
{
33+
if(!File.Exists(RootFilePath))
34+
{
35+
Mod.Log.LogInfo($"Couldn't find requested asset bundle at {RootFilePath}");
36+
return;
37+
}
38+
}
39+
else
40+
{
41+
if (!File.Exists(FilePath))
42+
{
43+
Mod.Log.LogInfo($"Couldn't find requested asset bundle at {FilePath}");
44+
return;
45+
}
46+
}
47+
48+
if (fromRootDirectory)
49+
{
50+
Bundle = Load(RootFilePath);
51+
}
52+
else
53+
{
54+
Bundle = Load();
55+
}
56+
}
57+
58+
/// <summary>
59+
/// Attempts to construct a Unity AssetBundle via a Centrifuge Type Bridge.
60+
/// You will have to cast the Bundle property to Unity's AssetBundle type for usage.
61+
/// </summary>
62+
/// <param name="filePath">An absolute path to the AssetBundle</param>
63+
public static Assets FromUnsafePath(string filePath)
64+
{
65+
if (!File.Exists(filePath))
66+
{
67+
Mod.Log.LogInfo($"Could not find requested asset bundle at {filePath}");
68+
return null;
69+
}
70+
71+
var ret = new Assets
72+
{
73+
_filePath = filePath,
74+
FileName = Path.GetFileName(filePath)
75+
};
76+
ret.Bundle = ret.Load();
77+
78+
if (ret.Bundle == null)
79+
return null;
80+
81+
return ret;
82+
}
83+
84+
private object Load()
85+
{
86+
try
87+
{
88+
var assetBundle = AssetBundleBridge.LoadFrom(FilePath);
89+
Mod.Log.LogInfo($"Loaded asset bundle {FilePath}");
90+
91+
return assetBundle;
92+
}
93+
catch (Exception ex)
94+
{
95+
Mod.Log.LogInfo(ex);
96+
return null;
97+
}
98+
}
99+
100+
private object Load(string filePath)
101+
{
102+
try
103+
{
104+
var assetBundle = AssetBundleBridge.LoadFrom(filePath);
105+
Mod.Log.LogInfo($"Loaded asset bundle {filePath}");
106+
107+
return assetBundle;
108+
}
109+
catch (Exception ex)
110+
{
111+
Mod.Log.LogInfo($"Could not load asset bundle {filePath}");
112+
Mod.Log.LogInfo(ex);
113+
return null;
114+
}
115+
}
116+
}
117+
118+
internal static class AssetBundleBridge
119+
{
120+
public static Type AssetBundleType => Kernel.FindTypeByFullName(
121+
"UnityEngine.AssetBundle",
122+
"UnityEngine"
123+
);
124+
125+
private static MethodInfo LoadFromFile => AssetBundleType.GetMethod(
126+
"LoadFromFile",
127+
new[] { typeof(string) }
128+
);
129+
130+
public static object LoadFrom(string path)
131+
{
132+
return LoadFromFile.Invoke(null, new[] { path });
133+
}
134+
}
135+
136+
internal static class Kernel
137+
{
138+
internal static Type FindTypeByFullName(string fullName, string assemblyFilter)
139+
{
140+
var assemblies = AppDomain.CurrentDomain.GetAssemblies()
141+
.Where(a => a.GetName().Name.Contains(assemblyFilter));
142+
143+
foreach (var asm in assemblies)
144+
{
145+
var type = asm.GetTypes().FirstOrDefault(t => t.FullName == fullName);
146+
147+
if (type == null)
148+
continue;
149+
150+
return type;
151+
}
152+
153+
Mod.Log.LogInfo($"Type {fullName} wasn't found in the main AppDomain at this moment.");
154+
throw new Exception($"Type {fullName} wasn't found in the main AppDomain at this moment.");
155+
}
156+
}
157+
}

Distance.NitronicHUD/Distance.NitronicHUD.csproj

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<Reference Include="0Harmony, Version=2.5.1.0, Culture=neutral, processorArchitecture=MSIL">
36-
<HintPath>..\packages\HarmonyX.2.5.1\lib\net35\0Harmony.dll</HintPath>
35+
<Reference Include="0Harmony">
36+
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Distance\BepInEx\core\0Harmony.dll</HintPath>
3737
</Reference>
3838
<Reference Include="Assembly-CSharp, Version=9.0.0.0, Culture=neutral, processorArchitecture=MSIL">
39-
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\Assembly-CSharp.dll</HintPath>
39+
<HintPath>..\..\publicized_assemblies\Assembly-CSharp.dll</HintPath>
40+
</Reference>
41+
<Reference Include="BepInEx">
42+
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Distance\BepInEx\core\BepInEx.dll</HintPath>
4043
</Reference>
4144
<Reference Include="Centrifuge.Distance, Version=3.0.7868.41513, Culture=neutral, processorArchitecture=MSIL">
4245
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\Centrifuge.Distance.dll</HintPath>
@@ -75,25 +78,29 @@
7578
<Reference Include="System.Data" />
7679
<Reference Include="System.Xml" />
7780
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
78-
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\UnityEngine.dll</HintPath>
81+
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Distance\Distance_Data\Managed\UnityEngine.dll</HintPath>
7982
</Reference>
8083
<Reference Include="UnityEngine.Networking, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
8184
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\UnityEngine.Networking.dll</HintPath>
8285
</Reference>
8386
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
84-
<HintPath>..\packages\Centrifuge.GameSupport.Distance.3.0.7868.41513\lib\net35\UnityEngine.UI.dll</HintPath>
87+
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Distance\Distance_Data\Managed\UnityEngine.UI.dll</HintPath>
8588
</Reference>
8689
</ItemGroup>
8790
<ItemGroup>
91+
<Compile Include="Assets.cs" />
8892
<Compile Include="ConfigurationLogic.cs" />
8993
<Compile Include="Data\VisualCountdownDigit.cs" />
9094
<Compile Include="Data\VisualDisplayContent.cs" />
9195
<Compile Include="Mod.cs" />
9296
<Compile Include="Flags.cs" />
93-
<Compile Include="Harmony\Assembly-CSharp\StatsManager\OnEventPlayerAdded.cs" />
97+
<Compile Include="Patches\Assembly-CSharp\GameManager\Awake.cs" />
98+
<Compile Include="Patches\Assembly-CSharp\StatsManager\OnEventPlayerAdded.cs" />
9499
<Compile Include="Properties\AssemblyInfo.cs" />
95100
<Compile Include="Scripts\VisualCountdown.cs" />
96101
<Compile Include="Scripts\VisualDisplay.cs" />
102+
<Compile Include="Utilities.cs" />
103+
<Compile Include="Vehicle.cs" />
97104
</ItemGroup>
98105
<ItemGroup>
99106
<None Include="Distance.NitronicHUD.targets" />

Distance.NitronicHUD/Flags.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Events.Level;
44
using Events.LevelEditor;
55
using Events.RaceEnd;
6+
using Events.Scene;
67
using System.Linq;
78

89
namespace Distance.NitronicHUD
@@ -50,6 +51,19 @@ internal static void SubscribeEvents()
5051
EnterPlayMode.Subscribe(OnLevelEditorEnterPlayMode);
5152
EnterEditorMode.Subscribe(OnLevelEditorEnterEditorMode);
5253
UninitializeOptimizations.Subscribe(OnLevelUninitializeOptimizations);
54+
BeginSceneSwitchFadeOut.Subscribe(OnSceneChanged);
55+
}
56+
57+
internal static void UnsubscribeEvents()
58+
{
59+
PauseToggled.Unsubscribe(OnGamePauseToggled);
60+
ModeInitialized.Unsubscribe(OnGameModeInitialized);
61+
ModeStarted.Unsubscribe(OnGameModeModeStarted);
62+
LocalCarHitFinish.Unsubscribe(OnRaceEndLocalCarHitFinish);
63+
EnterPlayMode.Unsubscribe(OnLevelEditorEnterPlayMode);
64+
EnterEditorMode.Unsubscribe(OnLevelEditorEnterEditorMode);
65+
UninitializeOptimizations.Unsubscribe(OnLevelUninitializeOptimizations);
66+
BeginSceneSwitchFadeOut.Unsubscribe(OnSceneChanged);
5367
}
5468

5569
private static void OnLevelUninitializeOptimizations(UninitializeOptimizations.Data data)
@@ -88,5 +102,10 @@ private static void OnGamePauseToggled(PauseToggled.Data data)
88102
{
89103
IsPaused = data.paused_;
90104
}
105+
106+
private static void OnSceneChanged(BeginSceneSwitchFadeOut.Data data)
107+
{
108+
IsModeStarted = false;
109+
}
91110
}
92111
}

0 commit comments

Comments
 (0)