From 17cce0d9cc8b832976670eae63ff12e08634d4d3 Mon Sep 17 00:00:00 2001 From: "E. S." <39125931+ClonkAndre@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:46:10 +0200 Subject: [PATCH] Additions, fixes and changes - Added "Budget" and "Settings" tab items in the Simple Traffic Loader ImGui window. - Fixed bug that wouldn't allow a group to unload. - Changed some smaller things. --- .../SimpleTrafficLoader/Classes/Logging.cs | 4 + .../SimpleTrafficLoader/Main.cs | 192 ++++++++++++------ 2 files changed, 130 insertions(+), 66 deletions(-) diff --git a/SimpleTrafficLoader/SimpleTrafficLoader/Classes/Logging.cs b/SimpleTrafficLoader/SimpleTrafficLoader/Classes/Logging.cs index 83792f6..382582d 100644 --- a/SimpleTrafficLoader/SimpleTrafficLoader/Classes/Logging.cs +++ b/SimpleTrafficLoader/SimpleTrafficLoader/Classes/Logging.cs @@ -5,7 +5,11 @@ namespace SimpleTrafficLoader.Classes internal class Logging { +#if DEBUG public static bool EnableDebugLogging = true; +#else + public static bool EnableDebugLogging = false; +#endif public static void Log(string str, params object[] args) { diff --git a/SimpleTrafficLoader/SimpleTrafficLoader/Main.cs b/SimpleTrafficLoader/SimpleTrafficLoader/Main.cs index 3d0efd0..eccbb6e 100644 --- a/SimpleTrafficLoader/SimpleTrafficLoader/Main.cs +++ b/SimpleTrafficLoader/SimpleTrafficLoader/Main.cs @@ -150,6 +150,7 @@ public class Main : Script private string currentZone; // Group stuff + private bool didAutomaticallyLoadDefaultGroup; private string currentlyLoadedGroupFileName; private bool preventGroupLoading; @@ -160,6 +161,10 @@ public class Main : Script #region Constructor public Main() { + // Lists + loadedSpawnGroups = new List(); + availableSpawnGroupFiles = new List(); + // IV-SDK .NET stuff WaitTickInterval = 1000; Uninitialize += Main_Uninitialize; @@ -171,51 +176,17 @@ public Main() #endregion #region Methods - private void LoadGroupFromFile(string fileName) + private void ReloadSettings() { - try - { - if (loadedSpawnGroups != null) - loadedSpawnGroups.Clear(); - - string path = string.Format("{0}\\Groups\\{1}", ScriptResourceFolder, fileName); - - if (!File.Exists(path)) - { - if (fileName != "Default.json") - { - Logging.LogWarning("Could not find the '{0}' file which contains the vehicle groups that should load. Simple Traffic Loader might not work as expected. Please choose another group to load, or restore this group. Trying to load default group instead.", fileName); - LoadGroupFromFile("Default.json"); - } - else - { - Logging.LogWarning("Could not find the default group file (Default.json). Simple Traffic Loader might not work as expected as this group is required. Make sure to restore this group or redownload if necessary.", fileName); - } - - return; - } - - loadedSpawnGroups = Helper.ConvertJsonStringToObject>(File.ReadAllText(path)); - - if (loadedSpawnGroups.Count == 0) - { - Logging.LogWarning("No groups were loaded."); - } - else - { - // Prepare loaded groups - loadedSpawnGroups.ForEach(x => x.Prepare()); - - Logging.Log("Loaded {0} groups!", loadedSpawnGroups.Count); - } - - currentlyLoadedGroupFileName = fileName; - } - catch (Exception ex) + if (Settings.Load()) { - Logging.LogError("Failed to load group '{0}'. Details: {1}", fileName, ex); + ModSettings.Load(Settings); + Logging.Log("Settings file of Simple Traffic Loader was reloaded!"); } + else + Logging.LogWarning("Could not reload the settings file of Simple Traffic Loader! File might not exist."); } + private void SaveCurrentlyLoadedGroupToFile() { try @@ -383,15 +354,8 @@ private void UnloadGroup(SpawnGroup group) if (group == null) return; - // Create unload lambda action - Action groupUnloadAction = () => - { - // Unload models of this group - group.UnloadModels(); - }; - - // Unload group instantly - groupUnloadAction.Invoke(); + // Unload models of this group + group.UnloadModels(); } private void LoadGroups(string forZone) @@ -662,6 +626,56 @@ private void ShowSubtitleMessageFIXED(uint time, string str, params string[] arg #endregion #region Functions + private bool LoadGroupFromFile(string fileName) + { + try + { + if (loadedSpawnGroups != null) + loadedSpawnGroups.Clear(); + + string path = string.Format("{0}\\Groups\\{1}", ScriptResourceFolder, fileName); + + if (!File.Exists(path)) + { + if (fileName != "Default.json") + { + Logging.LogWarning("Could not find the '{0}' file which contains the vehicle groups that should load. Simple Traffic Loader might not work as expected. Please choose another group to load, or restore this group. Trying to load default group instead.", fileName); + return LoadGroupFromFile("Default.json"); + } + else + { + Logging.LogWarning("Could not find the default group file (Default.json). Simple Traffic Loader might not work as expected as this group is required. Make sure to restore this group or redownload if necessary.", fileName); + } + + return false; + } + + loadedSpawnGroups = Helper.ConvertJsonStringToObject>(File.ReadAllText(path)); + + if (loadedSpawnGroups.Count == 0) + { + Logging.LogWarning("No groups were loaded."); + } + else + { + // Prepare loaded groups + loadedSpawnGroups.ForEach(x => x.Prepare()); + + Logging.Log("Loaded {0} groups!", loadedSpawnGroups.Count); + } + + currentlyLoadedGroupFileName = fileName; + + return true; + } + catch (Exception ex) + { + Logging.LogError("Failed to load group '{0}'. Details: {1}", fileName, ex); + } + + return false; + } + /// /// Will check the "Groups" folder for available groups and adds them to a list of available groups. /// @@ -679,7 +693,7 @@ private bool CheckAvailableSpawnGroups() if (files.Length == 0) { - Logging.LogWarning("There are no spawn groups available. The mod will abort now. Redownload if neccessary."); + Logging.LogWarning("There are no spawn groups available."); return false; } @@ -840,7 +854,7 @@ private SpawnGroup[] FindGroupsByArea(string zone, bool nonEqualCheck, bool hasT // If no matching island for area found then return otherwise continue with area check if (matches == 0) - return false; + return nonEqualCheck; } // Check if specified neighborhoods match @@ -880,11 +894,7 @@ private void Main_Uninitialize(object sender, EventArgs e) private void Main_Initialized(object sender, EventArgs e) { // Check and add available spawn groups - if (!CheckAvailableSpawnGroups()) - { - Abort(); - return; - } + CheckAvailableSpawnGroups(); // Load settings ModSettings.Load(Settings); @@ -906,16 +916,20 @@ private void Main_OnImGuiRendering(IntPtr devicePtr, ImGuiIV_DrawingContext ctx) { if (ImGuiIV.BeginTabBar("SimpleTrafficLoaderTabBar")) { +#if DEBUG DebugTabItem(); +#endif GroupsTabItem(); + BudgetTabItem(); + SettingsTabItem(); } ImGuiIV.EndTabBar(); } ImGuiIV.End(); } +#if DEBUG private void DebugTabItem() { -#if DEBUG if (ImGuiIV.BeginTabItem("DEBUG##SimpleTrafficLoaderTI")) { ImGuiIV.SeparatorText("Debugging"); @@ -923,7 +937,8 @@ private void DebugTabItem() ImGuiIV.TextUnformatted("Current Day State: {0}", NativeWorld.GetDayState()); ImGuiIV.Spacing(); - ImGuiIV.SeparatorText("Lists"); + ImGuiIV.SeparatorText("Groups"); + ImGuiIV.TextUnformatted("Did Automatically Load Default Group: {0}", didAutomaticallyLoadDefaultGroup); ImGuiIV.TextUnformatted("Added Groups: {0}", loadedSpawnGroups.Count); ImGuiIV.Spacing(); @@ -939,8 +954,9 @@ private void DebugTabItem() ImGuiIV.EndTabItem(); } -#endif } +#endif + private void GroupsTabItem() { if (ImGuiIV.BeginTabItem("Groups##SimpleTrafficLoaderTI")) @@ -964,7 +980,7 @@ private void GroupsTabItem() ImGuiIV.Spacing(); ImGuiIV.SeparatorText("The Groups"); - ImGuiIV.TextUnformatted("Available groups to load"); + ImGuiIV.TextUnformatted("Available group files to load"); ImGuiIV.HelpMarker(string.Format("Lists all group files which are located within the 'Groups' directory.{0}" + "Clicking on an item in this list will load the group from file.", Environment.NewLine)); @@ -976,7 +992,11 @@ private void GroupsTabItem() string groupFileName = availableSpawnGroupFiles[i]; if (ImGuiIV.Selectable(groupFileName)) - LoadGroupFromFile(groupFileName); + { + // If loading failed, reload available group files + if (!LoadGroupFromFile(groupFileName)) + CheckAvailableSpawnGroups(); + } } ImGuiIV.EndCombo(); @@ -987,11 +1007,14 @@ private void GroupsTabItem() if (ImGuiIV.Button("Refresh")) CheckAvailableSpawnGroups(); - if (ImGuiIV.Button("Save currently loaded group to file")) - SaveCurrentlyLoadedGroupToFile(); - ImGuiIV.Spacing(2); + if (loadedSpawnGroups.Count != 0) + { + if (ImGuiIV.Button("Save currently loaded group to file")) + SaveCurrentlyLoadedGroupToFile(); + } + if (ImGuiIV.Button("Create new group")) loadedSpawnGroups.Add(new SpawnGroup()); @@ -1218,6 +1241,40 @@ private void GroupsTabItem() } } + private void BudgetTabItem() + { + if (ImGuiIV.BeginTabItem("Budget##SimpleTrafficLoaderTI")) + { + ImGuiIV.TextUnformatted("Current Vehicle Budget: {0} bytes {1}", IVStreaming.VehicleModelBudget, ModSettings.AutomaticallyDetermineVehicleBudget ? "(Automatically determined)" : ""); + + ImGuiIV.EndTabItem(); + } + } + private void SettingsTabItem() + { + if (ImGuiIV.BeginTabItem("Settings##SimpleTrafficLoaderTI")) + { + if (ImGuiIV.Button("Reload Settings")) + ReloadSettings(); + + ImGuiIV.SeparatorText("The Settings"); + + ImGuiIV.Spacing(); + ImGuiIV.TextUnformatted("General"); + + ImGuiIV.CheckBox("UnloadAllGroupsWhenModUnloads", ref ModSettings.UnloadAllGroupsWhenModUnloads); + ImGuiIV.SliderInt("MaxLoadedGroups", ref ModSettings.MaxLoadedGroups, 0, 100); + ImGuiIV.InputText("LoadGroupByDefault", ref ModSettings.LoadGroupByDefault); + ImGuiIV.CheckBox("ForceLoadModels", ref ModSettings.ForceLoadModels); + + ImGuiIV.Spacing(); + ImGuiIV.TextUnformatted("Budget"); + + ImGuiIV.CheckBox("AutomaticallyDetermineVehicleBudget", ref ModSettings.AutomaticallyDetermineVehicleBudget); + + ImGuiIV.EndTabItem(); + } + } private void Main_WaitTick(object sender, EventArgs e) { @@ -1247,8 +1304,11 @@ private void Main_Tick(object sender, EventArgs e) return; // Load groups if not loaded yet - if (loadedSpawnGroups == null) + if (!didAutomaticallyLoadDefaultGroup) + { LoadGroupFromFile(ModSettings.LoadGroupByDefault); + didAutomaticallyLoadDefaultGroup = true; + } // Get player stuff int playerIndex = CONVERT_INT_TO_PLAYERINDEX(GET_PLAYER_ID());