Skip to content

Commit

Permalink
Minor cleanup, dependency assertion.
Browse files Browse the repository at this point in the history
  • Loading branch information
krypto5863 committed Feb 20, 2024
1 parent 4cf708d commit 342d13d
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 254 deletions.
311 changes: 160 additions & 151 deletions Experiment5ParallelMenuLoading/Loaders/GSModMenuLoad.cs

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions Experiment5ParallelMenuLoading/Loaders/ModMenuLoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Threading.Tasks;
using UnityEngine;

namespace ShortMenuLoader
namespace ShortMenuLoader.Loaders
{
internal class ModMenuLoad
{
Expand All @@ -28,7 +28,7 @@ public static IEnumerator ModMenuLoadStart(List<SceneEdit.SMenuItem> menuList, D
{
try
{
if (TMonitor.TryEnter(modFiles, Main.TimeoutLimit.Value))
if (TMonitor.TryEnter(modFiles, ShortMenuLoader.TimeoutLimit.Value))
{
try
{
Expand All @@ -42,20 +42,20 @@ public static IEnumerator ModMenuLoadStart(List<SceneEdit.SMenuItem> menuList, D
}
else
{
Main.PLogger.LogError($"Timed out waiting for mutex to allow entry...");
ShortMenuLoader.PLogger.LogError($"Timed out waiting for mutex to allow entry...");
}
}
catch
{
Main.PLogger.LogError("Couldn't read .mod file at: " + mod);
ShortMenuLoader.PLogger.LogError("Couldn't read .mod file at: " + mod);
}
}
Main.PLogger.LogInfo($"After loading all .mod files, we have allocated: {GC.GetTotalMemory(false) * 0.000001}");
ShortMenuLoader.PLogger.LogInfo($"After loading all .mod files, we have allocated: {GC.GetTotalMemory(false) * 0.000001}");
}));
*/

foreach (var mod in Main.FilesInModFolder.Where(t => t.ToLower().EndsWith(".mod")))
foreach (var mod in ShortMenuLoader.FilesInModFolder.Where(t => t.ToLower().EndsWith(".mod")))
{
var mi2 = new SceneEdit.SMenuItem();
if (InitModMenuItemScript(mi2, mod, out var icon))
Expand All @@ -69,7 +69,7 @@ public static IEnumerator ModMenuLoadStart(List<SceneEdit.SMenuItem> menuList, D
/*
if (servantWorker.IsFaulted)
{
Main.PLogger.LogError($"Servant task failed due to an unexpected error!");
ShortMenuLoader.PLogger.LogError($"Servant task failed due to an unexpected error!");
throw servantWorker.Exception;
}
Expand All @@ -85,11 +85,11 @@ public static IEnumerator ModMenuLoadStart(List<SceneEdit.SMenuItem> menuList, D

if (loaderWorker.IsFaulted)
{
Main.PLogger.LogWarning($"Worker task failed due to an unexpected error! SceneEditInstance is considered a full failure: {loaderWorker.Exception.InnerException.Message}\n{loaderWorker.Exception.InnerException.StackTrace}\n\nwe will try restarting the load task...");
ShortMenuLoader.PLogger.LogWarning($"Worker task failed due to an unexpected error! SceneEditInstance is considered a full failure: {loaderWorker.Exception?.InnerException?.Message}\n{loaderWorker.Exception.InnerException.StackTrace}\n\nwe will try restarting the load task...");

yield return new WaitForSecondsRealtime(2);

Main.SceneEditInstance.StartCoroutine(ModMenuLoadStart(menuList, menuGroupMemberDic));
ShortMenuLoader.SceneEditInstance.StartCoroutine(ModMenuLoadStart(menuList, menuGroupMemberDic));

yield break;
}
Expand All @@ -108,19 +108,19 @@ public static IEnumerator ModMenuLoadStart(List<SceneEdit.SMenuItem> menuList, D

foreach (var mi2 in modIconLoads.Keys)
{
//AccessTools.Method(typeof(SceneEdit), "AddMenuItemToList").Invoke(Main.@this, new object[] { mi2 });
Main.SceneEditInstance.AddMenuItemToList(mi2);
//AccessTools.Method(typeof(SceneEdit), "AddMenuItemToList").Invoke(ShortMenuLoader.@this, new object[] { mi2 });
ShortMenuLoader.SceneEditInstance.AddMenuItemToList(mi2);
//this.AddMenuItemToList(mi2);
menuList.Add(mi2);
if (!Main.SceneEditInstance.m_menuRidDic.ContainsKey(mi2.m_nMenuFileRID))
if (!ShortMenuLoader.SceneEditInstance.m_menuRidDic.ContainsKey(mi2.m_nMenuFileRID))
{
Main.SceneEditInstance.m_menuRidDic.Add(mi2.m_nMenuFileRID, mi2);
ShortMenuLoader.SceneEditInstance.m_menuRidDic.Add(mi2.m_nMenuFileRID, mi2);
}
else
{
Main.SceneEditInstance.m_menuRidDic[mi2.m_nMenuFileRID] = mi2;
ShortMenuLoader.SceneEditInstance.m_menuRidDic[mi2.m_nMenuFileRID] = mi2;
}
//string parentMenuName = AccessTools.Method(typeof(SceneEdit), "GetParentMenuFileName").Invoke(Main.@this, new object[] { mi2 }) as string;
//string parentMenuName = AccessTools.Method(typeof(SceneEdit), "GetParentMenuFileName").Invoke(ShortMenuLoader.@this, new object[] { mi2 }) as string;
var parentMenuName = SceneEdit.GetParentMenuFileName(mi2);
//string parentMenuName = SceneEdit.GetParentMenuFileName(mi2);
if (!string.IsNullOrEmpty(parentMenuName))
Expand All @@ -138,14 +138,14 @@ public static IEnumerator ModMenuLoadStart(List<SceneEdit.SMenuItem> menuList, D
mi2.m_listMember = new List<SceneEdit.SMenuItem> { mi2 };
}

if (Main.BreakInterval.Value < Time.realtimeSinceStartup - Main.Time)
if (ShortMenuLoader.BreakInterval.Value < Time.realtimeSinceStartup - ShortMenuLoader.Time)
{
yield return null;
Main.Time = Time.realtimeSinceStartup;
ShortMenuLoader.Time = Time.realtimeSinceStartup;
}
}
Main.ThreadsDone++;
Main.PLogger.LogInfo($".Mods finished loading at: {Main.WatchOverall.Elapsed}");
ShortMenuLoader.ThreadsDone++;
ShortMenuLoader.PLogger.LogInfo($".Mods finished loading at: {ShortMenuLoader.WatchOverall.Elapsed}");
}

public static bool InitModMenuItemScript(SceneEdit.SMenuItem mi, string fStrModFileName, out byte[] icon)
Expand All @@ -160,7 +160,7 @@ public static bool InitModMenuItemScript(SceneEdit.SMenuItem mi, string fStrModF
var text = binaryReader.ReadString();
if (text != "CM3D2_MOD")
{
Main.PLogger.LogError("InitModMenuItemScript (例外 : ヘッダーファイルが不正です。) The following header for this file indicates that this is not a mod file: " + text + " @ " + fStrModFileName);
ShortMenuLoader.PLogger.LogError("InitModMenuItemScript (例外 : ヘッダーファイルが不正です。) The following header for this file indicates that this is not a mod file: " + text + " @ " + fStrModFileName);

return false;
}
Expand All @@ -179,7 +179,7 @@ public static bool InitModMenuItemScript(SceneEdit.SMenuItem mi, string fStrModF
}
catch
{
Main.PLogger.LogError("(カテゴリがありません。) There is no category called: " + text5 + " @ " + fStrModFileName);
ShortMenuLoader.PLogger.LogError("(カテゴリがありません。) There is no category called: " + text5 + " @ " + fStrModFileName);

return false;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ public static bool InitModMenuItemScript(SceneEdit.SMenuItem mi, string fStrModF
mi.m_strInfo = text4.Replace("《改行》", "\n");
mi.m_strCateName = strCateName;

if (Main.PutMenuFileNameInItemDescription.Value)
if (ShortMenuLoader.PutMenuFileNameInItemDescription.Value)
{
mi.m_strInfo += $"\n\n{Path.GetFileName(fStrModFileName)}";
}
Expand All @@ -223,7 +223,7 @@ public static bool InitModMenuItemScript(SceneEdit.SMenuItem mi, string fStrModF
}
catch
{
Main.PLogger.LogWarning("(カテゴリがありません。) There is no category called: " + mi.m_strCateName + " @ " + fStrModFileName);
ShortMenuLoader.PLogger.LogWarning("(カテゴリがありません。) There is no category called: " + mi.m_strCateName + " @ " + fStrModFileName);
mi.m_mpn = MPN.null_mpn;
}

Expand Down Expand Up @@ -268,7 +268,7 @@ public static bool InitModMenuItemScript(SceneEdit.SMenuItem mi, string fStrModF
}
catch
{
Main.PLogger.LogError("(無限色IDがありません。) There is no infinite color ID called: " + text8 + " @ " + fStrModFileName);
ShortMenuLoader.PLogger.LogError("(無限色IDがありません。) There is no infinite color ID called: " + text8 + " @ " + fStrModFileName);
}
mi.m_pcMultiColorID = pcMultiColorId;
}
Expand All @@ -279,7 +279,7 @@ public static bool InitModMenuItemScript(SceneEdit.SMenuItem mi, string fStrModF
}
catch (Exception ex)
{
Main.PLogger.LogError("InitModMenuItemScript The following MOD item menu file could not be loaded (MODアイテムメニューファイルが読み込めませんでした。) : " + fStrModFileName + "\n\n" + ex.Message + "\n" + ex.StackTrace);
ShortMenuLoader.PLogger.LogError("InitModMenuItemScript The following MOD item menu file could not be loaded (MODアイテムメニューファイルが読み込めませんでした。) : " + fStrModFileName + "\n\n" + ex.Message + "\n" + ex.StackTrace);
return false;
}
return true;
Expand Down
64 changes: 32 additions & 32 deletions Experiment5ParallelMenuLoading/Loaders/VanillaMenuLoad.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using HarmonyLib;
using Newtonsoft.Json;
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using HarmonyLib;
using Newtonsoft.Json;
using UnityEngine;

namespace ShortMenuLoader
namespace ShortMenuLoader.Loaders
{
internal class VanillaMenuLoad
{
Expand All @@ -21,7 +21,7 @@ public static IEnumerator LoadCache(int retry = 0)
{
_cacheLoadDone = false;

if (!Main.SmvdLoaded && Main.UseVanillaCache.Value)
if (!ShortMenuLoader.SmvdLoaded && ShortMenuLoader.UseVanillaCache.Value)
{
var cacheLoader = Task.Factory.StartNew(() =>
{
Expand All @@ -46,20 +46,20 @@ public static IEnumerator LoadCache(int retry = 0)
{
if (cacheLoader.Exception?.InnerException != null)
{
Main.PLogger.LogError(
ShortMenuLoader.PLogger.LogError(
$"There was an error while attempting to load the vanilla cache: \n{cacheLoader.Exception.InnerException.Message}\n{cacheLoader.Exception.InnerException.StackTrace}\n\nAn attempt will be made to restart the load task...");
}

yield return new WaitForSecondsRealtime(5);

Main.PlugInstance.StartCoroutine(LoadCache(++retry));
ShortMenuLoader.PlugInstance.StartCoroutine(LoadCache(++retry));

yield break;
}

if (cacheLoader.Exception?.InnerException != null)
{
Main.PLogger.LogError(
ShortMenuLoader.PLogger.LogError(
$"There was an error while attempting to load the vanilla cache: \n{cacheLoader.Exception.InnerException.Message}\n{cacheLoader.Exception.InnerException.StackTrace}\n\nThis is the 4th attempt to kick-start the task. Cache will be deleted and rebuilt next time.");
}

Expand All @@ -73,7 +73,7 @@ public static IEnumerator LoadCache(int retry = 0)

public static IEnumerator SaveCache(Dictionary<SceneEdit.SMenuItem, string> filesToLoad, int retry = 0)
{
if (Main.SmvdLoaded || !Main.UseVanillaCache.Value)
if (ShortMenuLoader.SmvdLoaded || !ShortMenuLoader.UseVanillaCache.Value)
{
yield break;
}
Expand All @@ -90,7 +90,7 @@ public static IEnumerator SaveCache(Dictionary<SceneEdit.SMenuItem, string> file

File.WriteAllText(CacheFile, JsonConvert.SerializeObject(_menuCache));

Main.PLogger.LogInfo("Finished cleaning and saving the mod cache...");
ShortMenuLoader.PLogger.LogInfo("Finished cleaning and saving the mod cache...");
});

while (cacheSaver.IsCompleted == false)
Expand All @@ -107,19 +107,19 @@ public static IEnumerator SaveCache(Dictionary<SceneEdit.SMenuItem, string> file
{
if (cacheSaver.Exception?.InnerException != null)
{
Main.PLogger.LogError(
ShortMenuLoader.PLogger.LogError(
$"Cache saver task failed due to an unexpected error! SceneEditInstance is considered a minor failure: {cacheSaver.Exception.InnerException.Message}\n{cacheSaver.Exception.InnerException.StackTrace}\n\nAn attempt will be made to restart the task again...");
}

yield return new WaitForSecondsRealtime(5);

Main.PlugInstance.StartCoroutine(SaveCache(filesToLoad, ++retry));
ShortMenuLoader.PlugInstance.StartCoroutine(SaveCache(filesToLoad, ++retry));
}
else
{
if (cacheSaver.Exception?.InnerException != null)
{
Main.PLogger.LogFatal(
ShortMenuLoader.PLogger.LogFatal(
$"Cache saver task failed due to an unexpected error! SceneEditInstance is considered a minor failure: {cacheSaver.Exception.InnerException.Message}\n{cacheSaver.Exception.InnerException.StackTrace}\n\nNo further attempts will be made to start the task again...");

throw cacheSaver.Exception.InnerException;
Expand Down Expand Up @@ -155,7 +155,7 @@ public static IEnumerator VanillaMenuLoadStart(List<SceneEdit.SMenuItem> menuLis

//SceneEditInstance entire for loop is what loads in normal game menus. It's been left relatively untouched.

if (!Main.SmvdLoaded)
if (!ShortMenuLoader.SmvdLoaded)
{
var fileCount = menuDataBase.GetDataSize();

Expand All @@ -181,7 +181,7 @@ public static IEnumerator VanillaMenuLoadStart(List<SceneEdit.SMenuItem> menuLis
VanillaMenuLoaderSmvdCompat.LoadFromSmvdDictionary(ref filesToLoadFromDatabase);
}

if (_cacheLoadDone != true && Main.UseVanillaCache.Value)
if (_cacheLoadDone != true && ShortMenuLoader.UseVanillaCache.Value)
{
yield return new TimedWaitUntil(() => _cacheLoadDone, 0.5f);
}
Expand All @@ -192,7 +192,7 @@ public static IEnumerator VanillaMenuLoadStart(List<SceneEdit.SMenuItem> menuLis
{
string iconFileName = null;

if (_menuCache.ContainsKey(mi.m_strMenuFileName) && Main.UseVanillaCache.Value)
if (_menuCache.ContainsKey(mi.m_strMenuFileName) && ShortMenuLoader.UseVanillaCache.Value)
{
var tempStub = _menuCache[mi.m_strMenuFileName];

Expand All @@ -214,14 +214,14 @@ public static IEnumerator VanillaMenuLoadStart(List<SceneEdit.SMenuItem> menuLis
}
else
{
Main.PLogger.LogWarning("GameData folder was changed! We'll be wiping the vanilla cache clean and rebuilding it now.");
ShortMenuLoader.PLogger.LogWarning("GameData folder was changed! We'll be wiping the vanilla cache clean and rebuilding it now.");
_menuCache = new Dictionary<string, MenuStub>();
}
}

if (string.IsNullOrEmpty(mi.m_strMenuName))
{
//Main.PLogger.LogInfo($"Loading {mi.m_strMenuFileName} from the database as it wasn't in cache...");
//ShortMenuLoader.PLogger.LogInfo($"Loading {mi.m_strMenuFileName} from the database as it wasn't in cache...");
ReadMenuItemDataFromNative(mi, filesToLoadFromDatabase[mi], out iconFileName);
}

Expand All @@ -245,7 +245,7 @@ public static IEnumerator VanillaMenuLoadStart(List<SceneEdit.SMenuItem> menuLis
}
catch (Exception ex)
{
Main.PLogger.LogError(string.Concat("ReadMenuItemDataFromNative Exception(例外):", mi.m_strMenuFileName, "\n\n", ex.Message, " StackTrace/", ex.StackTrace));
ShortMenuLoader.PLogger.LogError(string.Concat("ReadMenuItemDataFromNative Exception(例外):", mi.m_strMenuFileName, "\n\n", ex.Message, " StackTrace/", ex.StackTrace));
}
}

Expand All @@ -257,14 +257,14 @@ public static IEnumerator VanillaMenuLoadStart(List<SceneEdit.SMenuItem> menuLis
foreach (var mi in filesToLoad.Keys)
{
//Added the CRC checks to make this plug compatible with 3.xx
if (!mi.m_bMan && !mi.m_strMenuFileName.Contains("_crc") && !mi.m_strMenuFileName.Contains("crc_") && !GsModMenuLoad.FilesDictionary.ContainsKey(mi.m_strMenuFileName) && Main.SceneEditInstance.editItemTextureCache.IsRegister(mi.m_nMenuFileRID))
if (!mi.m_bMan && !mi.m_strMenuFileName.Contains("_crc") && !mi.m_strMenuFileName.Contains("crc_") && !GsModMenuLoad.FilesDictionary.ContainsKey(mi.m_strMenuFileName) && ShortMenuLoader.SceneEditInstance.editItemTextureCache.IsRegister(mi.m_nMenuFileRID))
{
AccessTools.Method(typeof(SceneEdit), "AddMenuItemToList").Invoke(Main.SceneEditInstance, new object[] { mi });
AccessTools.Method(typeof(SceneEdit), "AddMenuItemToList").Invoke(ShortMenuLoader.SceneEditInstance, new object[] { mi });

menuList.Add(mi);

Main.SceneEditInstance.m_menuRidDic[mi.m_nMenuFileRID] = mi;
var parentMenuName = AccessTools.Method(typeof(SceneEdit), "GetParentMenuFileName").Invoke(Main.SceneEditInstance, new object[] { mi }) as string;
ShortMenuLoader.SceneEditInstance.m_menuRidDic[mi.m_nMenuFileRID] = mi;
var parentMenuName = AccessTools.Method(typeof(SceneEdit), "GetParentMenuFileName").Invoke(ShortMenuLoader.SceneEditInstance, new object[] { mi }) as string;

if (!string.IsNullOrEmpty(parentMenuName))
{
Expand All @@ -284,26 +284,26 @@ public static IEnumerator VanillaMenuLoadStart(List<SceneEdit.SMenuItem> menuLis
};
}

if (Main.BreakInterval.Value < Time.realtimeSinceStartup - Main.Time)
if (ShortMenuLoader.BreakInterval.Value < Time.realtimeSinceStartup - ShortMenuLoader.Time)
{
yield return null;
Main.Time = Time.realtimeSinceStartup;
ShortMenuLoader.Time = Time.realtimeSinceStartup;
}
}
}

Main.ThreadsDone++;
Main.PLogger.LogInfo($"Vanilla menus finished loading at: {Main.WatchOverall.Elapsed}. "
+ (Main.SmvdLoaded == false ?
ShortMenuLoader.ThreadsDone++;
ShortMenuLoader.PLogger.LogInfo($"Vanilla menus finished loading at: {ShortMenuLoader.WatchOverall.Elapsed}. "
+ (ShortMenuLoader.SmvdLoaded == false ?
$"We also spent {waitOnKiss.Elapsed} waiting for an unmodified database to finish loading..."
: $"We also spent {waitOnKiss.Elapsed} waiting for SMVD's Database to load..."));

Main.SceneEditInstance.StartCoroutine(SaveCache(filesToLoad));
ShortMenuLoader.SceneEditInstance.StartCoroutine(SaveCache(filesToLoad));
}

public static void ReadMenuItemDataFromNative(SceneEdit.SMenuItem mi, int menuDataBaseIndex, out string iconStr)
{
if (!Main.SmvdLoaded)
if (!ShortMenuLoader.SmvdLoaded)
{
var menuDataBase = GameMain.Instance.MenuDataBase;
menuDataBase.SetIndex(menuDataBaseIndex);
Expand All @@ -320,7 +320,7 @@ public static void ReadMenuItemDataFromNative(SceneEdit.SMenuItem mi, int menuDa
mi.m_bOld = menuDataBase.GetVersion() < 2000;
iconStr = menuDataBase.GetIconS();

if (Main.UseVanillaCache.Value)
if (ShortMenuLoader.UseVanillaCache.Value)
{
var newStub = new MenuStub
{
Expand All @@ -340,7 +340,7 @@ public static void ReadMenuItemDataFromNative(SceneEdit.SMenuItem mi, int menuDa
_menuCache[mi.m_strMenuFileName] = newStub;
}

if (Main.PutMenuFileNameInItemDescription.Value)
if (ShortMenuLoader.PutMenuFileNameInItemDescription.Value)
{
mi.m_strInfo += $"\n\n{menuDataBase.GetMenuFileName()}";
}
Expand Down
Loading

0 comments on commit 342d13d

Please sign in to comment.