diff --git a/TrolleybusTrailerMod/Patcher.cs b/TrolleybusTrailerMod/Patcher.cs new file mode 100644 index 0000000..262d799 --- /dev/null +++ b/TrolleybusTrailerMod/Patcher.cs @@ -0,0 +1,37 @@ +using System; +using System.Reflection; +using ColossalFramework.UI; +using HarmonyLib; +using UnityEngine; + +namespace TrolleybusTrailerMod { + public static class Patcher { + private const string HarmonyId = "krzychu124.trolleybus.trailer.ai"; + private static bool patched; + public static void PatchAll() { + try { + Harmony harmony = new Harmony(HarmonyId); + harmony.PatchAll(Assembly.GetExecutingAssembly()); + Debug.Log("[Trolleybus Trailer AI] Patches applied correctly"); + patched = true; + } catch (Exception e) { + Debug.LogError("[Trolleybus Trailer AI] Exception while patching game:\n" + e); + UIView.library + .ShowModal( + "ExceptionPanel") + .SetMessage( + "Trolleybus Trailer AI ", + "Something went wrong while patching. :(\nMod will not work correctly. Contact author for support.", + true); + } + } + + public static void UnPatchAll() { + if (!patched) return; + Harmony harmony = new Harmony(HarmonyId); + harmony.UnpatchAll(HarmonyId ); + patched = false; + Debug.Log("[Trolleybus Trailer AI] UnPatched successfully"); + } + } +} \ No newline at end of file diff --git a/TrolleybusTrailerMod/Properties/AssemblyInfo.cs b/TrolleybusTrailerMod/Properties/AssemblyInfo.cs index 8f1de02..dd1ab0e 100644 --- a/TrolleybusTrailerMod/Properties/AssemblyInfo.cs +++ b/TrolleybusTrailerMod/Properties/AssemblyInfo.cs @@ -31,4 +31,4 @@ // 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("1.0.*")] \ No newline at end of file +[assembly: AssemblyVersion("1.1.1.*")] \ No newline at end of file diff --git a/TrolleybusTrailerMod/TrolleybusTrailerMod.cs b/TrolleybusTrailerMod/TrolleybusTrailerMod.cs index 228eb12..431dc78 100644 --- a/TrolleybusTrailerMod/TrolleybusTrailerMod.cs +++ b/TrolleybusTrailerMod/TrolleybusTrailerMod.cs @@ -1,44 +1,23 @@ -using System; -using System.Reflection; -using ColossalFramework.UI; -using HarmonyLib; +using CitiesHarmony.API; using ICities; using UnityEngine; namespace TrolleybusTrailerMod { - public class TrolleybusTrailerMod: IUserMod { + public class TrolleybusTrailerMod : IUserMod { public string Name { get; } = "Trolleybus Trailer AI"; public string Description { get; } = "Support Trolleybus trailers with poles at any trailer"; - public readonly string HARMONY_ID = "krzychu124.trolleybus.trailer.ai"; - private Harmony _harmony; - public void OnEnabled() { - if (CitiesHarmony.API.HarmonyHelper.IsHarmonyInstalled) { - CitiesHarmony.API.HarmonyHelper.DoOnHarmonyReady(() => { - Debug.Log("[Trolleybus Trailer AI] Applying patches..."); - _harmony = new Harmony(HARMONY_ID); - try { - _harmony.PatchAll(Assembly.GetExecutingAssembly()); - Debug.Log("[Trolleybus Trailer AI] Patches applied correctly"); - } catch (Exception e) { - Debug.LogError("[Trolleybus Trailer AI] Exception while patching game:\n" + e); - UIView.library - .ShowModal( - "ExceptionPanel") - .SetMessage( - "Trolleybus Trailer AI ", - "Something went wrong while patching. :(\nMod will not work correctly. Contact author for support.", - true); - } + HarmonyHelper.DoOnHarmonyReady(() => { + Debug.Log("[Trolleybus Trailer AI] Try Patch..."); + Patcher.PatchAll(); }); - } } public void OnDisabled() { - if (_harmony != null) { - Debug.Log("[Trolleybus Trailer AI] UnPatching..."); - _harmony.UnpatchAll(HARMONY_ID); + if (HarmonyHelper.IsHarmonyInstalled) { + Debug.Log("[Trolleybus Trailer AI] Try UnPatch..."); + Patcher.UnPatchAll(); } } } diff --git a/TrolleybusTrailerMod/TrolleybusTrailerMod.csproj b/TrolleybusTrailerMod/TrolleybusTrailerMod.csproj index 38fb5aa..75ed91f 100644 --- a/TrolleybusTrailerMod/TrolleybusTrailerMod.csproj +++ b/TrolleybusTrailerMod/TrolleybusTrailerMod.csproj @@ -32,15 +32,15 @@ 4 - - ..\packages\Lib.Harmony.2.0.1\lib\net35\0Harmony.dll + + ..\packages\Lib.Harmony.2.0.4\lib\net35\0Harmony.dll True ..\dependencies\Assembly-CSharp.dll - - ..\packages\CitiesHarmony.API.1.0.5\lib\net35\CitiesHarmony.API.dll + + ..\packages\CitiesHarmony.API.1.0.6\lib\net35\CitiesHarmony.API.dll True @@ -65,6 +65,7 @@ + diff --git a/TrolleybusTrailerMod/packages.config b/TrolleybusTrailerMod/packages.config index 3c972cf..552a66e 100644 --- a/TrolleybusTrailerMod/packages.config +++ b/TrolleybusTrailerMod/packages.config @@ -1,5 +1,5 @@  - - + + \ No newline at end of file