From 52cb43b0d8c0deebe168b9634e3503d818409e48 Mon Sep 17 00:00:00 2001 From: richard elms Date: Thu, 16 Oct 2025 11:03:49 +0200 Subject: [PATCH 1/2] initial change --- .../maze_runner/Assets/Scripts/Main.cs | 151 +++++++++++++++--- 1 file changed, 132 insertions(+), 19 deletions(-) diff --git a/features/fixtures/maze_runner/Assets/Scripts/Main.cs b/features/fixtures/maze_runner/Assets/Scripts/Main.cs index 06956522e..daafd0f07 100644 --- a/features/fixtures/maze_runner/Assets/Scripts/Main.cs +++ b/features/fixtures/maze_runner/Assets/Scripts/Main.cs @@ -43,9 +43,7 @@ public class Main : MonoBehaviour public IEnumerator Start() { Log("Maze Runner app started"); - yield return GetFixtureConfig(); - #if UNITY_STANDALONE_OSX PreventCrashPopups(); #endif @@ -132,7 +130,7 @@ IEnumerator RunNextMazeCommand() if ("clear_cache".Equals(command.action)) { - ClearUnityCache(); + ClearCache(); } else if ("run_scenario".Equals(command.action)) { @@ -153,46 +151,161 @@ private void CloseFixture() Application.Quit(); } - - private void ClearUnityCache() + private void ClearCache() { #if UNITY_SWITCH return; #endif - if (Directory.Exists(Application.persistentDataPath + "/Bugsnag")) + ClearUnityCache(); + if (Application.platform == RuntimePlatform.Android) { - Directory.Delete(Application.persistentDataPath + "/Bugsnag", true); + ClearAndroidCache(); + return; } if (Application.platform == RuntimePlatform.IPhonePlayer) { - ClearIOSData(); - } - if (Application.platform != RuntimePlatform.Android && - Application.platform != RuntimePlatform.IPhonePlayer) - { - Invoke("CloseFixture", 0.25f); + ClearIOSCache(); + return; } + Invoke("CloseFixture", 0.25f); + } + + private void ClearUnityCache() + { + DeleteTargets(Application.persistentDataPath, new[] { "Bugsnag" }, Array.Empty()); } - public static void ClearIOSData() + public static void ClearIOSCache() { #if UNITY_IOS ClearPersistentData(); #endif } - private static void Log(string msg) + private void ClearAndroidCache() { +#if UNITY_ANDROID try { - Logger.I(msg); + string cacheRoot; + string filesRoot; + GetAndroidRoots(out cacheRoot, out filesRoot); + + DeleteTargets(cacheRoot, new[] { "bugsnag", "StrictModeDiscScenarioFile" }, Array.Empty()); + DeleteTargets(filesRoot, new[] { "background-service-dir" }, new[] { "device-id", "internal-device-id" }); + + ListFolder(cacheRoot, "CACHE"); + ListFolder(filesRoot, "FILES"); } - catch + catch (Exception e) { - // can fail on windows + Log($"[Cleaner] Failed to clear Android persistent data: {e}"); } +#endif + } +#if UNITY_ANDROID + private static void GetAndroidRoots(out string cacheRoot, out string filesRoot) + { + cacheRoot = null; + filesRoot = null; + using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + using (var activity = unityPlayer.GetStatic("currentActivity")) + { + using (var cacheDir = activity.Call("getCacheDir")) + { + cacheRoot = cacheDir.Call("getAbsolutePath"); + } + using (var filesDir = activity.Call("getFilesDir")) + { + filesRoot = filesDir.Call("getAbsolutePath"); + } + } } +#endif + private static void DeleteTargets(string root, string[] dirs, string[] files) + { + foreach (var d in dirs) + { + var p = Path.Combine(root, d); + DeleteDirectoryIfExists(p); + } + foreach (var f in files) + { + var p = Path.Combine(root, f); + DeleteFileIfExists(p); + } + } -} + private static void DeleteDirectoryIfExists(string path) + { + try + { + if (Directory.Exists(path)) + { + LogStatic($"[Cleaner] Deleting dir: {path}"); + Directory.Delete(path, true); + } + else + { + LogStatic($"[Cleaner] Dir not found (skip): {path}"); + } + } + catch (Exception e) + { + LogStatic($"[Cleaner] Could not delete dir {path}: {e.Message}"); + } + } + + private static void DeleteFileIfExists(string path) + { + try + { + if (File.Exists(path)) + { + LogStatic($"[Cleaner] Deleting file: {path}"); + File.Delete(path); + } + else + { + LogStatic($"[Cleaner] File not found (skip): {path}"); + } + } + catch (Exception e) + { + LogStatic($"[Cleaner] Could not delete file {path}: {e.Message}"); + } + } + + private static void ListFolder(string root, string label) + { + try + { + LogStatic($"[Cleaner] Contents of {label} root: {root}"); + if (!Directory.Exists(root)) + { + LogStatic($"[Cleaner] Root does not exist: {root}"); + return; + } + foreach (var entry in Directory.EnumerateFileSystemEntries(root, "*", SearchOption.AllDirectories)) + { + LogStatic(entry); + } + } + catch (Exception e) + { + LogStatic($"[Cleaner] Could not list {label} root {root}: {e.Message}"); + } + } + + private static void LogStatic(string msg) + { + try { Logger.I(msg); } catch { } + } + + private static void Log(string msg) + { + try { Logger.I(msg); } catch { } + } +} \ No newline at end of file From 3310f75164605930dc3b58b9c2150eba994c6600 Mon Sep 17 00:00:00 2001 From: richard elms Date: Thu, 16 Oct 2025 12:05:01 +0200 Subject: [PATCH 2/2] clear android shared prefs --- .../maze_runner/Assets/Scripts/Main.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/features/fixtures/maze_runner/Assets/Scripts/Main.cs b/features/fixtures/maze_runner/Assets/Scripts/Main.cs index daafd0f07..a5c3c5e82 100644 --- a/features/fixtures/maze_runner/Assets/Scripts/Main.cs +++ b/features/fixtures/maze_runner/Assets/Scripts/Main.cs @@ -194,6 +194,8 @@ private void ClearAndroidCache() DeleteTargets(cacheRoot, new[] { "bugsnag", "StrictModeDiscScenarioFile" }, Array.Empty()); DeleteTargets(filesRoot, new[] { "background-service-dir" }, new[] { "device-id", "internal-device-id" }); + ClearAndroidSharedPreferences("com.bugsnag.android"); + ListFolder(cacheRoot, "CACHE"); ListFolder(filesRoot, "FILES"); } @@ -222,6 +224,24 @@ private static void GetAndroidRoots(out string cacheRoot, out string filesRoot) } } } + + private void ClearAndroidSharedPreferences(string name) + { + try + { + using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + using (var activity = unityPlayer.GetStatic("currentActivity")) + using (var prefs = activity.Call("getSharedPreferences", name, 0)) + using (var editor = prefs.Call("edit")) + { + editor.Call("clear").Call("commit"); + } + } + catch (Exception e) + { + Log($"[Cleaner] Failed to clear SharedPreferences '{name}': {e}"); + } + } #endif private static void DeleteTargets(string root, string[] dirs, string[] files)