Skip to content

Commit 58068d7

Browse files
committed
Verbose, I am. On coffee, I am. Harmony, weird is.
1 parent d3904cc commit 58068d7

9 files changed

Lines changed: 72 additions & 27 deletions

File tree

PawMapLoader.sln.DotSettings.user

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASceneConfig_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fd315a5a932e64f7bb1a737657fe1cdb4303400_003Fdb_003F801f95b6_003FSceneConfig_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
180180
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASceneContext_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fd315a5a932e64f7bb1a737657fe1cdb4303400_003F85_003Fdfe74bd7_003FSceneContext_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
181181
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASpawnManager_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fd315a5a932e64f7bb1a737657fe1cdb4303400_003F90_003Fe6a9ad9b_003FSpawnManager_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
182+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AString_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fde80aed0bd3646409e8bfb15c101f005db800_003F2c_003F1e910277_003FString_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
182183
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;
183184
&lt;Assembly Path="/home/rocky/steam/steamapps/common/Pawperty Damage/MelonLoader/Il2CppAssemblies/Assembly-CSharp.dll" /&gt;
184185
&lt;/AssemblyExplorer&gt;</s:String></wpf:ResourceDictionary>

PawMapLoader/Init/InitMelon.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using MelonLoader;
2+
using PawMapLoader.Res;
13
using PawMapLoader.Res.Enum;
24
using PawMapLoader.Res.Json;
35

@@ -7,17 +9,20 @@ public class Init
79
{
810
public static void InitMelon()
911
{
12+
MelonLogger.Msg("InitMelon Called.");
13+
MelonLogger.Msg("Patching stuff...");
1014
var harmonyInstance = new HarmonyLib.Harmony("space.rkx.pawmaploader");
1115
harmonyInstance.PatchAll();
16+
MelonLogger.Msg("Patching complete.");
1217

1318
LevelDataProvider.WaitForDataProvider();
1419
}
1520

1621
public static void InitMaps()
1722
{
18-
Res.FileManagement.EnsureCustomMapsDirectory();
23+
FileManagement.EnsureCustomMapsDirectory();
1924
MapJson.Read();
20-
Res.AssetManager.LoadMapData();
25+
AssetManager.LoadMapData();
2126
}
2227
}
2328
}

PawMapLoader/Res/AssetManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ public static void LoadMapData()
3333

3434
MelonLogger.Msg("Adding " + pawMap.AssetFile + " level data to list.");
3535
levels.Add(sceneConfig);
36+
MelonLogger.Msg("Added " + pawMap.AssetFile + " level data to list.");
3637
}
3738
catch (Exception e)
3839
{
39-
MelonLogger.Error("Could not load PawBox:\n"+e);
40+
MelonLogger.Error("Could not add data:\n"+e);
4041
}
4142
}
4243
LevelDataProvider.Instance._levels = levels.ToArray();

PawMapLoader/Res/Enum/LevelDataProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ public class LevelDataProvider
88
{
99
public static void WaitForDataProvider()
1010
{
11+
MelonLogger.Msg("Waiting for data provider...");
1112
MelonCoroutines.Start(ldpw());
1213
IEnumerator ldpw()
1314
{
1415
while (Il2CppGame.LevelDataProvider._instance == null)
1516
{
1617
yield return new WaitForSeconds(0.1f);
1718
}
19+
MelonLogger.Msg("Level data provider found.");
1820
Init.InitMaps();
1921
}
2022
}

PawMapLoader/Res/FileManagement.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static void EnsureCustomMapsDirectory()
2929

3030
public static string ReturnMapsJson()
3131
{
32+
MelonLogger.Msg("Getting \"maps.json\"...");
3233
return File.Exists(customMapsJsonFile) ? File.ReadAllText(customMapsJsonFile) : "{\"PawMapFileVersion\": 1, \"PawMaps\": [ ]}";
3334
}
3435

@@ -38,6 +39,8 @@ public static Stream OpenMapFile(string assetString)
3839
string assetPath = Path.Combine(customMapsDirectory, assetString + ".pawbox");
3940
if (File.Exists(assetPath))
4041
{
42+
// For some reason we can't just use the regular way of loading bundles.
43+
// Game crashes if we do. So we open a stream instead.
4144
return Il2CppSystem.IO.File.Open(assetPath, FileMode.Open);
4245
}
4346
MelonLogger.Error("Map File Not Found: " + assetPath);

PawMapLoader/Res/HarmonyPatches.cs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using Il2CppEffects;
55
using Il2CppGame;
66
using Il2CppLoadingScreen;
7+
using Il2CppSystem.IO;
78
using MelonLoader;
8-
using PawMapLoader.Res.Abstractions;
99
using UnityEngine;
1010
using UnityEngine.SceneManagement;
1111

@@ -17,45 +17,75 @@ public static class GameManager_StartGame_Patch
1717
[HarmonyPrefix]
1818
public static bool Prefix(GameManager __instance)
1919
{
20+
if (Store.FirePrevention.IsGameStarted) return true;
21+
22+
Store.FirePrevention.IsGameStarted = true;
23+
24+
MelonLogger.Msg("Get Selected Scene");
2025
string scenename = ConfigManager.Instance.Level.Scene.SceneName;
2126
Store.IsMapCustom = false;
2227
if (scenename == "AtroCity" || scenename == "DownTown") return true;
28+
29+
MelonLogger.Msg(scenename + " is custom.");
30+
Stream stream = null;
2331
try
2432
{
2533
Store.IsMapCustom = true;
34+
2635
MelonLogger.Msg("Loading " + scenename);
27-
var stream = FileManagement.OpenMapFile(scenename);
36+
MelonLogger.Msg("| Opening stream...");
37+
stream = FileManagement.OpenMapFile(scenename);
38+
39+
MelonLogger.Msg("|| Done.");
40+
MelonLogger.Msg("| Loading From Stream...");
2841
Store.LoadedAssetBundle = AssetBundle.LoadFromStream(stream);
42+
43+
MelonLogger.Msg("|| Done.");
2944
MelonLogger.Msg("Loaded " + scenename);
30-
stream.Close();
31-
stream.Dispose();
45+
MelonLogger.Msg("| Disposing stream...");
46+
stream?.Close();
47+
48+
MelonLogger.Msg("|| Closed.");
49+
stream?.Dispose();
50+
51+
MelonLogger.Msg("|| Disposed.");
3252
}
3353
catch (Exception e)
3454
{
55+
Store.FirePrevention.IsGameStarted = false;
56+
3557
MelonLogger.Error("Failed to load bundle " + e);
58+
stream?.Close();
59+
stream?.Dispose();
3660
}
3761

3862
return true;
3963
}
4064
}
4165

42-
[HarmonyPatch(typeof(GameManager), nameof(GameManager.GoToMainMenu))]
43-
public static class GameManager_GoToMainMenu_Patch
66+
[HarmonyPatch(typeof(GameManager), nameof(GameManager.OnLobbySceneLoaded))]
67+
public static class GameManager_OnLobbySceneLoaded_Patch
4468
{
4569
[HarmonyPrefix]
4670
public static bool Prefix(GameManager __instance)
4771
{
72+
if (!Store.FirePrevention.IsGameStarted) return true;
73+
Store.FirePrevention.IsGameStarted = false;
74+
Store.FirePrevention.HasBlockConfig = false;
75+
76+
MelonLogger.Msg("Unloading Scene Assetbundle");
4877
if (!Store.IsMapCustom) return true;
4978
Store.LoadedAssetBundle?.Unload(true);
5079
Store.LoadedAssetBundle = null;
80+
MelonLogger.Msg("Done.");
5181
return true;
5282
}
5383
}
5484

5585
[HarmonyPatch(typeof(LoadingScreenController), nameof(LoadingScreenController.Show))]
5686
public static class LoadingScreenController_Show_Patch
5787
{
58-
[HarmonyPrefix]
88+
[HarmonyPostfix]
5989
public static void Postfix(LoadingScreenController __instance)
6090
{
6191
__instance.Hide();
@@ -68,41 +98,35 @@ public static class BuildingsManager_Init_Patch
6898
[HarmonyPrefix]
6999
public static void Prefix(BuildingsManager __instance)
70100
{
71-
if (AbUe.GetTypeAll<CityBlockGrid>()[0] == null)
101+
if (Store.FirePrevention.HasBlockConfig) return;
102+
if (Store.IsMapCustom)
72103
{
104+
MelonLogger.Msg("Finding SceneConfig");
73105
foreach (var go in SceneManager
74-
.GetSceneByName(GameManager._instance.GameplaySceneName).GetRootGameObjects())
106+
.GetSceneByName(ConfigManager.Instance.Level.Scene.SceneName).GetRootGameObjects())
75107
{
76108
if (go.name == "SceneConfig")
77109
{
110+
MelonLogger.Msg("| \"" + go.name + "\" is SceneConfig.");
78111
var cblock = new GameObject("CityBlock");
79112
cblock.transform.SetParent(go.transform);
80113
cblock.AddComponent<CityBlockGrid>();
114+
Store.FirePrevention.HasBlockConfig = true;
81115
break;
82116
}
117+
MelonLogger.Msg("| \"" + go.name + "\" is not SceneConfig");
83118
}
84-
GameManager._instance.OnGameplaySceneLoaded();
85119
}
86120
}
87121
}
88122

89123
[HarmonyPatch(typeof(GroundDecalController), nameof(GroundDecalController.IsGroundConcrete))]
90124
public static class GroundDecalController_IsGroundConcrete_Patch
91125
{
92-
[HarmonyPrefix]
93-
public static bool Prefix(GroundDecalController __instance, bool __result)
94-
{
95-
if (Store.IsMapCustom)
96-
{
97-
__result = false;
98-
return false;
99-
}
100-
return true;
101-
}
102-
103126
[HarmonyFinalizer]
104127
public static Exception Finalizer(Exception __exception, ref bool __result)
105128
{
129+
106130
if (__exception is IndexOutOfRangeException)
107131
{
108132
__result = false;

PawMapLoader/Res/Json/MapJson.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using MelonLoader;
12
using Newtonsoft.Json;
23

34
namespace PawMapLoader.Res.Json
@@ -6,7 +7,8 @@ public class MapJson
67
{
78
public static void Read()
89
{
9-
Store.Maps = JsonConvert.DeserializeObject<MapList>(PawMapLoader.Res.FileManagement.ReturnMapsJson());
10+
MelonLogger.Msg("Deserializing map json...");
11+
Store.Maps = JsonConvert.DeserializeObject<MapList>(FileManagement.ReturnMapsJson());
1012
}
1113
}
1214
}

PawMapLoader/Res/Store.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,12 @@ public class Store
88
public static MapList Maps;
99
public static bool IsMapCustom = false;
1010
public static AssetBundle LoadedAssetBundle;
11+
12+
// The harmony patches fire twice and I have no idea why.
13+
public class FirePrevention
14+
{
15+
public static bool IsGameStarted = false;
16+
public static bool HasBlockConfig = false;
17+
}
1118
}
1219
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
## TODO
55
- [X] Load a map successfully
66
- [X] Load map but in a lazy way
7-
- [ ] Unload map when exit
7+
- [X] Unload map when exit
88
- [ ] Make components for easy creation of different object types.
99
- [ ] Create an SDK for use in Unity
1010
- [ ] Maybe fix additional things
@@ -23,7 +23,7 @@ Here is a template to get started:
2323
{
2424
"Name": "Testing Map",
2525
"LeaderboardName": "TestingMap",
26-
"AssetFile": "rkxspace.TestMap", // this will load the file "Maps/rkxspace/TestMap.pawbox
26+
"AssetFile": "rkxspace.TestMap", // this will load the file "Maps/rkxspace/TestMap.pawbox"
2727
"MapMetadata": {
2828
"GrowthRateModifier": 1.0, // The multiplier for the growth rate on the map. Down Town is set to 1, Atro City is set to 0.8.
2929
"GrowthShapeKeyStart": 2.0, // Internal value in which to start increasing the growth shape key.

0 commit comments

Comments
 (0)