Skip to content

Commit

Permalink
Prevent crashes/exceptions when changing graphics presets
Browse files Browse the repository at this point in the history
 - Can still get graphical artifacts but they seem to recover
 - Can further prevent issues by only allowing opening menu/toggling graphics settings when game is in clickwait state, but not done in this commit
  • Loading branch information
drojf committed Feb 17, 2024
1 parent a813a24 commit b1cddb5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Assets.Scripts.Core.AssetManagement/AssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,12 @@ public void CleanUpTextures()
}
}

public void InvalidateTextureCache()
{
textureReferences = new Dictionary<string, TextureReference>();
Resources.UnloadUnusedAssets();
}

public void DebugOutputTextureReferenceStatus()
{
StringBuilder stringBuilder = new StringBuilder();
Expand Down
25 changes: 23 additions & 2 deletions Assets.Scripts.Core.Scene/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections;
using System.IO;
using UnityEngine;
using MOD.Scripts.UI;
using MOD.Scripts.Core;

namespace Assets.Scripts.Core.Scene
{
Expand Down Expand Up @@ -803,11 +805,17 @@ private void SetPrimaryTexture(string texName, out string texturePath)
else
{
Texture2D x = MODSceneController.LoadTextureWithFilters(layerID, texName, out texturePath);
lastTexturePath = texturePath;

// Check if texture is OK. If texture couldn't be loaded, return without doing anything
if (x == null)
{
throw new Exception("Failed to load texture: " + texName);
string errorString = $"Failed to load texture: {texName}";
MODLogger.Log(errorString, true);
MODToaster.Show(errorString, toastDuration: 10);
return;
}

lastTexturePath = texturePath;
if (primary != null)
{
AssetManager.Instance.ReleaseTexture(PrimaryName, primary);
Expand Down Expand Up @@ -872,8 +880,21 @@ public void HideLayer()
targetAngle = 0f;
}

// This function is only used by the mod!
public void ReloadTexture()
{
if(primary == null)
{
Debug.Log($"WARNING: Attempted to reload texture on layer {LayerID} but primary was null!");
return;
}

if(string.IsNullOrWhiteSpace(PrimaryName))
{
Debug.Log($"WARNING: Attempted to reload texture on layer {LayerID} but PrimaryName was null or whitespace!");
return;
}

if (PrimaryName == string.Empty)
{
// PrimaryName is set to string.Empty when Layer's texture is released -
Expand Down
3 changes: 3 additions & 0 deletions Assets.Scripts.Core.Scene/SceneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,11 @@ public void GetScreenshot(Action<Texture2D> onFinishAction)
StartCoroutine(GetScreenshotCoroutine(onFinishAction));
}

// This function is only used by the mod!
public void ReloadAllImages()
{
AssetManager.Instance.InvalidateTextureCache();

Layer[] componentsInChildren = Panel1.GetComponentsInChildren<Layer>();
foreach (Layer layer in componentsInChildren)
{
Expand Down
13 changes: 12 additions & 1 deletion MOD.Scripts.Core.Scene/MODSceneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,18 @@ public static Texture2D LoadTextureWithFilters(int? maybeLayer, string textureNa
watch.Stop();
MODUtility.FlagMonitorOnlyLog("Loaded " + textureName + " in " + watch.ElapsedMilliseconds + "ms (texture load only)");

if(!maybeLayer.HasValue)
if (texture == null)
{
texturePath = null;
return null;
}

if (texture.name == null)
{
MODLogger.Log($"WARNING: texture name is null when loading texture {textureName}", true);
}

if (!maybeLayer.HasValue)
{
MODUtility.FlagMonitorOnlyLog($">>> Not applying filter to {textureName} as provided layer is null");
return texture;
Expand Down

0 comments on commit b1cddb5

Please sign in to comment.