Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 8d648fe

Browse files
committed
1.6.9
* Fix for games where patching Cursor methods fails. * Added backup attempt for loading Cursor module if not present. * HashSet collections are now supported by CacheList * try/catch for loading Mod Config
1 parent 835a817 commit 8d648fe

20 files changed

+292
-167
lines changed

src/CppExplorer.cs

Lines changed: 14 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Explorer
1313
public class CppExplorer : MelonMod
1414
{
1515
public const string NAME = "CppExplorer";
16-
public const string VERSION = "1.6.8";
16+
public const string VERSION = "1.6.9";
1717
public const string AUTHOR = "Sinai";
1818
public const string GUID = "com.sinai.cppexplorer";
1919

@@ -24,51 +24,30 @@ public static bool ShowMenu
2424
get => m_showMenu;
2525
set => SetShowMenu(value);
2626
}
27-
private static bool m_showMenu;
28-
29-
public static bool ForceUnlockMouse
30-
{
31-
get => m_forceUnlock;
32-
set => SetForceUnlock(value);
33-
}
34-
private static bool m_forceUnlock;
35-
private static CursorLockMode m_lastLockMode;
36-
private static bool m_lastVisibleState;
37-
private static bool m_currentlySettingCursor = false;
38-
39-
public static bool ShouldForceMouse => ShowMenu && ForceUnlockMouse;
27+
public static bool m_showMenu;
4028

4129
private static void SetShowMenu(bool show)
4230
{
4331
m_showMenu = show;
44-
UpdateCursorControl();
45-
}
46-
47-
private static void SetForceUnlock(bool unlock)
48-
{
49-
m_forceUnlock = unlock;
50-
UpdateCursorControl();
32+
CursorControl.UpdateCursorControl();
5133
}
5234

5335
public override void OnApplicationStart()
5436
{
5537
Instance = this;
5638

39+
// First, load config
5740
ModConfig.OnLoad();
5841

42+
// Setup InputHelper class (UnityEngine.Input)
5943
InputHelper.Init();
6044

45+
// Create CppExplorer modules
6146
new MainMenu();
6247
new WindowManager();
6348

64-
// Get current cursor state and enable cursor
65-
m_lastLockMode = Cursor.lockState;
66-
m_lastVisibleState = Cursor.visible;
67-
68-
// Enable ShowMenu and ForceUnlockMouse
69-
// (set m_showMenu directly to not call UpdateCursorState twice)
70-
m_showMenu = true;
71-
ForceUnlockMouse = true;
49+
// Init cursor control
50+
CursorControl.Init();
7251

7352
MelonLogger.Log($"CppExplorer {VERSION} initialized.");
7453
}
@@ -89,109 +68,26 @@ public override void OnUpdate()
8968

9069
if (ShowMenu)
9170
{
92-
// Check Force-Unlock input
93-
if (InputHelper.GetKeyDown(KeyCode.LeftAlt))
94-
{
95-
ForceUnlockMouse = !ForceUnlockMouse;
96-
}
71+
CursorControl.Update();
72+
InspectUnderMouse.Update();
9773

9874
MainMenu.Instance.Update();
9975
WindowManager.Instance.Update();
100-
InspectUnderMouse.Update();
10176
}
10277
}
10378

10479
public override void OnGUI()
10580
{
10681
if (!ShowMenu) return;
10782

83+
var origSkin = GUI.skin;
84+
GUI.skin = UIStyles.WindowSkin;
85+
10886
MainMenu.Instance.OnGUI();
10987
WindowManager.Instance.OnGUI();
11088
InspectUnderMouse.OnGUI();
111-
}
112-
113-
private static void UpdateCursorControl()
114-
{
115-
m_currentlySettingCursor = true;
116-
if (ShouldForceMouse)
117-
{
118-
Cursor.lockState = CursorLockMode.None;
119-
Cursor.visible = true;
120-
}
121-
else
122-
{
123-
Cursor.lockState = m_lastLockMode;
124-
Cursor.visible = m_lastVisibleState;
125-
}
126-
m_currentlySettingCursor = false;
127-
}
128-
129-
// Force mouse to stay unlocked and visible while UnlockMouse and ShowMenu are true.
130-
// Also keep track of when anything else tries to set Cursor state, this will be the
131-
// value that we set back to when we close the menu or disable force-unlock.
13289

133-
[HarmonyPatch(typeof(Cursor), nameof(Cursor.lockState), MethodType.Setter)]
134-
public class Cursor_set_lockState
135-
{
136-
[HarmonyPrefix]
137-
public static void Prefix(ref CursorLockMode value)
138-
{
139-
if (!m_currentlySettingCursor)
140-
{
141-
m_lastLockMode = value;
142-
143-
if (ShouldForceMouse)
144-
{
145-
value = CursorLockMode.None;
146-
}
147-
}
148-
}
149-
}
150-
151-
[HarmonyPatch(typeof(Cursor), nameof(Cursor.visible), MethodType.Setter)]
152-
public class Cursor_set_visible
153-
{
154-
[HarmonyPrefix]
155-
public static void Prefix(ref bool value)
156-
{
157-
if (!m_currentlySettingCursor)
158-
{
159-
m_lastVisibleState = value;
160-
161-
if (ShouldForceMouse)
162-
{
163-
value = true;
164-
}
165-
}
166-
}
167-
}
168-
169-
// Make it appear as though UnlockMouse is disabled to the rest of the application.
170-
171-
[HarmonyPatch(typeof(Cursor), nameof(Cursor.lockState), MethodType.Getter)]
172-
public class Cursor_get_lockState
173-
{
174-
[HarmonyPostfix]
175-
public static void Postfix(ref CursorLockMode __result)
176-
{
177-
if (ShouldForceMouse)
178-
{
179-
__result = m_lastLockMode;
180-
}
181-
}
182-
}
183-
184-
[HarmonyPatch(typeof(Cursor), nameof(Cursor.visible), MethodType.Getter)]
185-
public class Cursor_get_visible
186-
{
187-
[HarmonyPostfix]
188-
public static void Postfix(ref bool __result)
189-
{
190-
if (ShouldForceMouse)
191-
{
192-
__result = m_lastVisibleState;
193-
}
194-
}
90+
GUI.skin = origSkin;
19591
}
19692
}
19793
}

src/CppExplorer.csproj

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="CppExplorer.cs" />
9494
<Compile Include="Extensions\ReflectionExtensions.cs" />
9595
<Compile Include="Helpers\InputHelper.cs" />
96+
<Compile Include="Menu\CursorControl.cs" />
9697
<Compile Include="Tests\TestClass.cs" />
9798
<Compile Include="UnstripFixes\GUIUnstrip.cs" />
9899
<Compile Include="UnstripFixes\ScrollViewStateUnstrip.cs" />
@@ -101,24 +102,24 @@
101102
<Compile Include="Helpers\ReflectionHelpers.cs" />
102103
<Compile Include="Helpers\UIHelpers.cs" />
103104
<Compile Include="Helpers\UnityHelpers.cs" />
104-
<Compile Include="MainMenu\InspectUnderMouse.cs" />
105+
<Compile Include="Menu\MainMenu\InspectUnderMouse.cs" />
105106
<Compile Include="CachedObjects\CacheObjectBase.cs" />
106107
<Compile Include="UnstripFixes\SliderHandlerUnstrip.cs" />
107108
<Compile Include="UnstripFixes\UnstripExtensions.cs" />
108-
<Compile Include="Windows\ResizeDrag.cs" />
109-
<Compile Include="Windows\TabViewWindow.cs" />
110-
<Compile Include="Windows\UIWindow.cs" />
111-
<Compile Include="MainMenu\Pages\ConsolePage.cs" />
112-
<Compile Include="MainMenu\Pages\Console\REPL.cs" />
113-
<Compile Include="MainMenu\Pages\Console\REPLHelper.cs" />
114-
<Compile Include="MainMenu\Pages\WindowPage.cs" />
115-
<Compile Include="Windows\WindowManager.cs" />
116-
<Compile Include="MainMenu\MainMenu.cs" />
117-
<Compile Include="Windows\GameObjectWindow.cs" />
118-
<Compile Include="Windows\ReflectionWindow.cs" />
119-
<Compile Include="MainMenu\Pages\ScenePage.cs" />
120-
<Compile Include="MainMenu\Pages\SearchPage.cs" />
121-
<Compile Include="Helpers\UIStyles.cs" />
109+
<Compile Include="Menu\Windows\ResizeDrag.cs" />
110+
<Compile Include="Menu\Windows\TabViewWindow.cs" />
111+
<Compile Include="Menu\Windows\UIWindow.cs" />
112+
<Compile Include="Menu\MainMenu\Pages\ConsolePage.cs" />
113+
<Compile Include="Menu\MainMenu\Pages\Console\REPL.cs" />
114+
<Compile Include="Menu\MainMenu\Pages\Console\REPLHelper.cs" />
115+
<Compile Include="Menu\MainMenu\Pages\WindowPage.cs" />
116+
<Compile Include="Menu\Windows\WindowManager.cs" />
117+
<Compile Include="Menu\MainMenu\MainMenu.cs" />
118+
<Compile Include="Menu\Windows\GameObjectWindow.cs" />
119+
<Compile Include="Menu\Windows\ReflectionWindow.cs" />
120+
<Compile Include="Menu\MainMenu\Pages\ScenePage.cs" />
121+
<Compile Include="Menu\MainMenu\Pages\SearchPage.cs" />
122+
<Compile Include="Menu\UIStyles.cs" />
122123
<Compile Include="Properties\AssemblyInfo.cs" />
123124
</ItemGroup>
124125
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

src/Helpers/InputHelper.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ private static bool TryManuallyLoadInput()
8888
{
8989
MelonLogger.Log("UnityEngine.Input is null, trying to load manually....");
9090

91-
if ((TryLoad("UnityEngine.InputLegacyModule.dll") || TryLoad("UnityEngine.CoreModule.dll")) && Input != null)
91+
if ((ReflectionHelpers.LoadModule("UnityEngine.InputLegacyModule.dll") || ReflectionHelpers.LoadModule("UnityEngine.CoreModule.dll"))
92+
&& Input != null)
9293
{
9394
MelonLogger.Log("Ok!");
9495
return true;
@@ -98,23 +99,6 @@ private static bool TryManuallyLoadInput()
9899
MelonLogger.Log("Could not load Input module!");
99100
return false;
100101
}
101-
102-
bool TryLoad(string module)
103-
{
104-
var path = $@"MelonLoader\Managed\{module}";
105-
if (!File.Exists(path)) return false;
106-
107-
try
108-
{
109-
Assembly.Load(File.ReadAllBytes(path));
110-
return true;
111-
}
112-
catch (Exception e)
113-
{
114-
MelonLogger.Log(e.GetType() + ", " + e.Message);
115-
return false;
116-
}
117-
}
118102
}
119103
}
120104
}

src/Helpers/ReflectionHelpers.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Reflection;
6+
using System.IO;
7+
using MelonLoader;
68
using UnhollowerBaseLib;
79
using UnhollowerRuntimeLib;
810
using UnityEngine;
@@ -122,6 +124,23 @@ public static Type[] GetAllBaseTypes(object obj)
122124
return list.ToArray();
123125
}
124126

127+
public static bool LoadModule(string module)
128+
{
129+
var path = $@"MelonLoader\Managed\{module}";
130+
if (!File.Exists(path)) return false;
131+
132+
try
133+
{
134+
Assembly.Load(File.ReadAllBytes(path));
135+
return true;
136+
}
137+
catch (Exception e)
138+
{
139+
MelonLogger.Log(e.GetType() + ", " + e.Message);
140+
return false;
141+
}
142+
}
143+
125144
public static string ExceptionToString(Exception e)
126145
{
127146
if (IsFailedGeneric(e))

0 commit comments

Comments
 (0)