Skip to content

Commit

Permalink
Additions, fixes and changes
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
ClonkAndre committed Oct 18, 2024
1 parent e811524 commit 17cce0d
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 66 deletions.
4 changes: 4 additions & 0 deletions SimpleTrafficLoader/SimpleTrafficLoader/Classes/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
192 changes: 126 additions & 66 deletions SimpleTrafficLoader/SimpleTrafficLoader/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public class Main : Script
private string currentZone;

// Group stuff
private bool didAutomaticallyLoadDefaultGroup;
private string currentlyLoadedGroupFileName;
private bool preventGroupLoading;

Expand All @@ -160,6 +161,10 @@ public class Main : Script
#region Constructor
public Main()
{
// Lists
loadedSpawnGroups = new List<SpawnGroup>();
availableSpawnGroupFiles = new List<string>();

// IV-SDK .NET stuff
WaitTickInterval = 1000;
Uninitialize += Main_Uninitialize;
Expand All @@ -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<List<SpawnGroup>>(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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<List<SpawnGroup>>(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;
}

/// <summary>
/// Will check the "Groups" folder for available groups and adds them to a list of available groups.
/// </summary>
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -906,24 +916,29 @@ 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");
ImGuiIV.CheckBox("Enable Debug Logging", ref Logging.EnableDebugLogging);
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();
Expand All @@ -939,8 +954,9 @@ private void DebugTabItem()

ImGuiIV.EndTabItem();
}
#endif
}
#endif

private void GroupsTabItem()
{
if (ImGuiIV.BeginTabItem("Groups##SimpleTrafficLoaderTI"))
Expand All @@ -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));
Expand All @@ -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();
Expand All @@ -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());

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 17cce0d

Please sign in to comment.