Skip to content
This repository was archived by the owner on Apr 21, 2024. It is now read-only.

Commit 51b7e3a

Browse files
author
BuildTools
committed
Loads from zip files
SplotchUtils has various utility features MORE EVENTS cleaned up stuff
1 parent ddbb5d8 commit 51b7e3a

File tree

7 files changed

+144
-19
lines changed

7 files changed

+144
-19
lines changed

Event.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public abstract class PlayerEvent : GameEvent
243243

244244
public SlimeController GetSlimeController()
245245
{
246-
return Splotch.GetSlimeControllers()[GetPlayer().Id - 1];
246+
return SplotchUtils.GetSlimeControllers()[GetPlayer().Id - 1];
247247
}
248248
}
249249

@@ -307,7 +307,7 @@ public override Player GetPlayer()
307307
/// <summary>
308308
/// Gets the PlayerBody object of the event
309309
/// </summary>
310-
/// <returns>The PlayerBodt object</returns>
310+
/// <returns>The PlayerBody object</returns>
311311
public PlayerBody GetPlayerBody()
312312
{
313313
return _playerBody;
@@ -326,9 +326,8 @@ public static void Patch(ref PlayerBody __instance)
326326

327327
namespace Splotch.Event.GameEvents
328328
{
329-
public abstract class GameEvent : Event, Cancellable
329+
public abstract class GameEvent : Event
330330
{
331-
public bool Cancelled { get; set; } = false;
332331

333332
}
334333
}

Loader.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using UnityEngine.SceneManagement;
22
using System.Reflection;
33
using Splotch.Event;
4+
using Splotch;
5+
using UnityEngine;
6+
using System;
7+
using System.Diagnostics;
48

59
namespace Splotch.Loader
610
{
@@ -16,9 +20,6 @@ struct SplotchConfigContainer
1620
public int someValue;
1721
}
1822

19-
20-
21-
2223
public static bool enteredScene = false;
2324

2425
/// <summary>
@@ -37,21 +38,29 @@ public static void OnEnterScene(Scene scene, LoadSceneMode loadSceneMode)
3738
Patcher.DoPatching();
3839
ModLoader.ModLoader.LoadMods();
3940
EventManager.Load();
41+
42+
GameObject obj = new GameObject("Unloader", new Type[] { typeof(UnLoader) });
4043
}
4144

4245
/// <summary>
4346
/// The main entrypoint for Splotch, called by Doorstop.
4447
/// </summary>
4548
public static void Main()
4649
{
47-
Splotch.Config.CreateConfigAndLoadSplotchConfig();
50+
Config.CreateConfigAndLoadSplotchConfig();
4851

4952
if (!Config.LoadedSplotchConfig.splotchEnabled)
50-
{
5153
return;
52-
}
5354

5455
SceneManager.sceneLoaded += SceneLoaded;
56+
57+
58+
}
59+
60+
internal static void GameExit()
61+
{
62+
ModLoader.ModLoader.UnloadMods();
63+
Logger.Log("Finished unloading!");
5564
}
5665

5766
/// <summary>
@@ -69,4 +78,24 @@ private static void SceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
6978
BaseGuiModifications.RunMainMenuModifications();
7079
}
7180
}
81+
82+
class UnLoader : MonoBehaviour
83+
{
84+
public static UnLoader Instance
85+
{
86+
get;
87+
set;
88+
}
89+
90+
void Awake()
91+
{
92+
DontDestroyOnLoad(transform.gameObject);
93+
Instance = this;
94+
}
95+
96+
public void OnApplicationQuit()
97+
{
98+
Loader.GameExit();
99+
}
100+
}
72101
}

ModLoader.cs

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using YamlDotNet.Serialization.NamingConventions;
44
using System.Reflection;
55
using System;
6+
using HarmonyLib;
7+
using ICSharpCode.SharpZipLib.Core;
8+
using ICSharpCode.SharpZipLib.Zip;
69

710
namespace Splotch.Loader.ModLoader
811
{
@@ -11,19 +14,39 @@ namespace Splotch.Loader.ModLoader
1114
/// </summary>
1215
internal static class ModLoader
1316
{
14-
const string MOD_FOLDER_PATH = "splotch_mods";
15-
const string MOD_INFO_FILE_NAME = "modinfo.yaml";
17+
static readonly string MOD_FOLDER_PATH = "splotch_mods";
18+
static readonly string MOD_INFO_FILE_NAME = "modinfo.yaml";
19+
static readonly string UNZIPPED_MOD_TEMP_FOLDER = @"splotch_mods\temp";
20+
1621
public static DirectoryInfo modFolderDirectory;
22+
public static DirectoryInfo modFolderTempDirectory;
1723
/// <summary>
1824
/// Called by the <c>Loader</c>, loads all detected mods and logs any issues encountered during loading.
1925
/// </summary>
2026
internal static void LoadMods()
2127
{
2228
Logger.Log("Starting to load mods...");
23-
int modCountLoaded = 0;
24-
int modCountTot = 0;
25-
modFolderDirectory = Directory.CreateDirectory(MOD_FOLDER_PATH);
26-
foreach (DirectoryInfo modFolder in modFolderDirectory.GetDirectories())
29+
int modCountLoaded = 0;
30+
int modCountTot = 0;
31+
modFolderDirectory = Directory.CreateDirectory(MOD_FOLDER_PATH);
32+
modFolderTempDirectory = Directory.CreateDirectory(UNZIPPED_MOD_TEMP_FOLDER);
33+
34+
ZipConstants.DefaultCodePage = System.Text.Encoding.UTF8.CodePage;
35+
foreach (FileInfo modZipFile in modFolderDirectory.GetFiles())
36+
{
37+
if (modZipFile.Extension.ToLower() == ".zip")
38+
{
39+
Logger.Debug($"Unzipped mod {modZipFile.Name} detected!");
40+
41+
FastZip fastZip = new FastZip();
42+
fastZip.ExtractZip(modZipFile.FullName, modFolderTempDirectory.CreateSubdirectory(
43+
Path.GetFileNameWithoutExtension(modZipFile.Name)
44+
).FullName, null);
45+
}
46+
}
47+
48+
49+
foreach (DirectoryInfo modFolder in modFolderDirectory.GetDirectories().AddRangeToArray(modFolderTempDirectory.GetDirectories()))
2750
{
2851
modCountTot++;
2952
string modFolderPath = modFolder.FullName;
@@ -70,6 +93,37 @@ internal static void LoadMods()
7093

7194
Logger.Log($"Loaded {modCountLoaded}/{modCountTot} mods successfully!");
7295
}
96+
97+
public static void RecursiveDelete(DirectoryInfo baseDir)
98+
{
99+
if (!baseDir.Exists)
100+
return;
101+
102+
foreach (var dir in baseDir.EnumerateDirectories())
103+
{
104+
RecursiveDelete(dir);
105+
}
106+
var files = baseDir.GetFiles();
107+
foreach (var file in files)
108+
{
109+
file.IsReadOnly = false;
110+
File.SetAttributes(file.FullName, FileAttributes.Normal);
111+
file.Delete();
112+
}
113+
baseDir.Delete();
114+
}
115+
116+
internal static void UnloadMods()
117+
{
118+
Logger.Log("Unloading mods...");
119+
foreach (var loadedMod in ModManager.loadedMods)
120+
{
121+
loadedMod.splotchMod.OnUnload();
122+
Logger.Debug($"Unloaded {loadedMod.name}");
123+
}
124+
Logger.Debug("Clearing temp...");
125+
RecursiveDelete(modFolderTempDirectory);
126+
}
73127
}
74128

75129
/// <summary>
@@ -148,7 +202,9 @@ internal bool LoadMod(string modFolder)
148202
{
149203
string dllAbsolutePath = Path.Combine(modFolder, dll);
150204
Logger.Debug($"Loading {dllAbsolutePath}");
151-
assembly = Assembly.LoadFile(dllAbsolutePath);
205+
206+
assembly = Assembly.Load(File.ReadAllBytes(dllAbsolutePath));
207+
152208
Logger.Debug($"Loaded {assembly}");
153209
Type assemblyEntrypoint = assembly.GetType(className);
154210
if (assemblyEntrypoint == null)

Splotch.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<Compile Include="ModLoader.cs" />
5050
<Compile Include="ModManager.cs" />
5151
<Compile Include="Properties\AssemblyInfo.cs" />
52-
<Compile Include="Splotch.cs" />
52+
<Compile Include="SplotchUtils.cs" />
5353
<Compile Include="SplotchMod.cs" />
5454
<Compile Include="VersionChecker.cs" />
5555
</ItemGroup>
@@ -80,6 +80,9 @@
8080
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Bopl Battle\BoplBattle_Data\Managed\Facepunch.Steamworks.Win64.dll</HintPath>
8181
<Private>False</Private>
8282
</Reference>
83+
<Reference Include="ICSharpCode.SharpZipLib, Version=0.85.4.369, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
84+
<HintPath>packages\ICSharpCode.SharpZipLib.dll.0.85.4.369\lib\net20\ICSharpCode.SharpZipLib.dll</HintPath>
85+
</Reference>
8386
<Reference Include="Microsoft.CSharp" />
8487
<Reference Include="Mono.Cecil, Version=0.11.4.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
8588
<HintPath>packages\Mono.Cecil.0.11.4\lib\net40\Mono.Cecil.dll</HintPath>

SplotchMod.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@ internal void Setup(ModInfo modInfo)
2121
/// Runs as soon as the mod is loaded after Splotch is done loading.
2222
/// </summary>
2323
public abstract void OnLoad();
24+
public void OnUnload()
25+
{
26+
27+
}
2428
}
2529
}

Splotch.cs renamed to SplotchUtils.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,34 @@
77

88
namespace Splotch
99
{
10-
public static class Splotch
10+
/// <summary>
11+
/// A utility class with general static functions
12+
/// </summary>
13+
public static class SplotchUtils
1114
{
15+
/// <summary>
16+
/// Gets the GameSessionHandler
17+
/// </summary>
18+
/// <returns>the GameSessionHandler or null if it isn't instantized</returns>
1219
public static GameSessionHandler GetGameSessionHandler()
1320
{
1421
FieldInfo selfRefField = typeof(GameSessionHandler).GetField("selfRef", BindingFlags.Static | BindingFlags.NonPublic);
1522
return selfRefField.GetValue(null) as GameSessionHandler;
1623
}
1724

25+
/// <summary>
26+
/// Gets the slime controllers
27+
/// </summary>
28+
/// <returns>A list of slime controllers</returns>
1829
public static SlimeController[] GetSlimeControllers()
1930
{
2031
FieldInfo slimeControllersField = typeof(GameSessionHandler).GetField("slimeControllers", BindingFlags.Instance | BindingFlags.NonPublic);
2132
return slimeControllersField.GetValue(GetGameSessionHandler()) as SlimeController[];
2233
}
2334

35+
/// <summary>
36+
/// Gives access to the private field <c>gameOver</c> in <c>GameSessionHandler</c>
37+
/// </summary>
2438
public static bool IsGameOver
2539
{
2640
get
@@ -35,18 +49,37 @@ public static bool IsGameOver
3549
}
3650
}
3751

52+
/// <summary>
53+
/// Gives access to the private function <c>prepareNextLevel</c> in <c>GameSessionHandler</c>
54+
/// </summary>
3855
public static void PrepareNextLevel()
3956
{
4057
MethodInfo prepareNextLevelMethod = typeof(GameSessionHandler).GetMethod("prepareNextlevel", BindingFlags.Instance | BindingFlags.NonPublic);
4158
prepareNextLevelMethod.Invoke(GetGameSessionHandler(), null);
4259
}
4360

61+
/// <summary>
62+
/// Retrieves the <c>Player</c> corresponding to a <c>PlayerBody</c>
63+
/// </summary>
64+
/// <param name="playerBody">The <c>PlayerBody</c> object</param>
65+
/// <returns>The corresponding <c>Player</c> object</returns>
4466
public static Player GetPlayerFromPlayerBody(PlayerBody playerBody)
4567
{
4668
FieldInfo type = typeof(PlayerBody).GetField("idHolder", BindingFlags.NonPublic | BindingFlags.Instance);
4769
IPlayerIdHolder idHolder = (IPlayerIdHolder)type.GetValue(playerBody);
4870
Player player = PlayerHandler.Get().GetPlayer(idHolder.GetPlayerId());
4971
return player;
5072
}
73+
74+
/// <summary>
75+
/// Retrieves the <c>PlayerPhysics</c> corresponding to a <c>PlayerBody</c>
76+
/// </summary>
77+
/// <param name="playerBody">The <c>PlayerBody</c> object</param>
78+
/// <returns>The corresponding <c>PlayerPhysics</c> object</returns>
79+
public static PlayerPhysics GetPlayerPhysicsFromPlayerBody(PlayerBody playerBody)
80+
{
81+
FieldInfo physicsField = typeof(PlayerBody).GetField("physics", BindingFlags.NonPublic | BindingFlags.Instance);
82+
return physicsField.GetValue(playerBody) as PlayerPhysics;
83+
}
5184
}
5285
}

packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="HarmonyX" version="2.10.2" targetFramework="net472" />
4+
<package id="ICSharpCode.SharpZipLib.dll" version="0.85.4.369" targetFramework="net472" />
45
<package id="Mono.Cecil" version="0.11.4" targetFramework="net472" />
56
<package id="MonoMod.RuntimeDetour" version="22.3.23.4" targetFramework="net472" />
67
<package id="MonoMod.Utils" version="22.3.23.4" targetFramework="net472" />

0 commit comments

Comments
 (0)