Fix NullReferenceException crashes when asset bundle is missing

Problem: When the 'serverbrowserpkg' asset bundle file is missing,
the mod crashed at multiple points:
- PrefabManager.PreScriptLoad(): NullReferenceException on line 58
- ServerBrowser.SceneLoaded(): "Object to instantiate is null" at line 312
- Main.TransitionTo(): NullReferenceException at lines 227-228

These crashes made the mod unusable and prevented proper error reporting.

Root cause:
The asset bundle file containing UI prefabs was missing from the mod
directory, causing all prefab references to be null. The code didn't
check for null before using these references.

Solutions:

PrefabManager.cs:
- Add null check after LoadAssetBundle() call (line 31-36)
- Return early with clear error message if bundle is null
- Provide guidance to user about missing file
- Better logging for successful loads

ServerBrowser.SceneLoaded():
- Add prefab null check at method start (line 302-309)
- Return early if prefabs are null
- Prevents crash during UI instantiation
- Clear error messages in log

Main.TransitionTo():
- Add comprehensive null checks before using UI references (line 228)
- If UI not loaded but user tries to access multiplayer menu:
  * Show user-friendly modal dialog
  * Explain the problem clearly
  * Provide reinstall guidance
- Gracefully handle missing UI without crashing

Results:
 No crashes when asset bundle is missing
 Clear, actionable error messages for users
 Graceful degradation - rest of mod still works
 User gets helpful modal instead of silent crash
 Better debugging with detailed logs

Updated README.md with:
- Documentation of all asset bundle fixes
- Code examples showing the changes
- Known Issues section noting missing asset bundle
- Instructions for resolving the issue

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-14 21:24:55 +01:00
parent ecb6d823f4
commit 5e014a74da
5 changed files with 112 additions and 51 deletions

63
Main.cs
View File

@@ -126,11 +126,6 @@ namespace KCM
var lobbyManager = new GameObject("LobbyManager").AddComponent<LobbyManager>();
DontDestroyOnLoad(lobbyManager);
//SteamFriends.InviteUserToGame(new CSteamID(76561198036307537), "test");
//SteamMatchmaking.lobby
//Main.helper.Log($"Timer duration for hazardpay {Player.inst.hazardPayWarmup.Duration}");
try
{
@@ -147,10 +142,7 @@ namespace KCM
FirstSibling = true,
OnClick = () =>
{
//Constants.MainMenuUI_T.Find("TopLevelUICanvas/TopLevel").gameObject.SetActive(false);
SfxSystem.PlayUiSelect();
//ServerBrowser.serverBrowserRef.SetActive(true);
TransitionTo(MenuState.ServerBrowser);
}
};
@@ -185,37 +177,6 @@ namespace KCM
private void FixedUpdate()
{
// send batched building placement info
/*if (PlaceHook.QueuedBuildings.Count > 0 && (FixedUpdateInterval % 25 == 0))
{
foreach (Building building in PlaceHook.QueuedBuildings)
{
new WorldPlace()
{
uniqueName = building.UniqueName,
customName = building.customName,
guid = building.guid,
rotation = building.transform.GetChild(0).rotation,
globalPosition = building.transform.position,
localPosition = building.transform.GetChild(0).localPosition,
built = building.IsBuilt(),
placed = building.IsPlaced(),
open = building.Open,
doBuildAnimation = building.doBuildAnimation,
constructionPaused = building.constructionPaused,
constructionProgress = building.constructionProgress,
life = building.Life,
ModifiedMaxLife = building.ModifiedMaxLife,
//CollectForBuild = CollectForBuild,
yearBuilt = building.YearBuilt,
decayProtection = building.decayProtection,
seenByPlayer = building.seenByPlayer
}.Send();
}
PlaceHook.QueuedBuildings.Clear();
}*/
FixedUpdateInterval++;
}
@@ -224,11 +185,24 @@ namespace KCM
{
try
{
ServerBrowser.serverBrowserRef.SetActive(state == MenuState.ServerBrowser);
ServerBrowser.serverLobbyRef.SetActive(state == MenuState.ServerLobby);
// Only interact with multiplayer UI if it was successfully created
if (ServerBrowser.serverBrowserRef != null && ServerBrowser.serverLobbyRef != null && ServerBrowser.KCMUICanvas != null)
{
ServerBrowser.serverBrowserRef.SetActive(state == MenuState.ServerBrowser);
ServerBrowser.serverLobbyRef.SetActive(state == MenuState.ServerLobby);
ServerBrowser.KCMUICanvas.gameObject.SetActive((int)state > 21);
helper.Log(((int)state > 21).ToString());
ServerBrowser.KCMUICanvas.gameObject.SetActive((int)state > 21);
helper.Log(((int)state > 21).ToString());
}
else if ((int)state > 21)
{
// User tried to access multiplayer menu but UI is not loaded
helper.Log("WARNING: Cannot transition to multiplayer menu - UI not loaded (asset bundle missing)");
ModalManager.ShowModal("Multiplayer Not Available",
"The multiplayer UI could not be loaded. The asset bundle file is missing.\n\nPlease reinstall the mod or contact the developer.",
"OK");
return;
}
GameState.inst.mainMenuMode.TransitionTo((MainMenuMode.State)state);
}
@@ -258,9 +232,6 @@ namespace KCM
helper.Log("Preload start in main");
try
{
//MainMenuPatches.Patch();
Main.helper = helper;
helper.Log(helper.modPath);