1
Fork 0
mirror of https://github.com/thegeneralist01/Scene-Manager-DevRepo synced 2026-01-11 15:40:29 +01:00

Paths are no longer fully imported on plugin load

This commit is contained in:
Rich Dunne 2021-07-07 07:25:27 -06:00
parent e73e75c1df
commit aac76cb79d

View file

@ -6,9 +6,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using Rage; using Rage;
using SceneManager.Managers; using SceneManager.Managers;
using SceneManager.Paths; using System.IO;
using System;
using System.Windows.Forms;
namespace SceneManager.Menus namespace SceneManager.Menus
{ {
@ -16,8 +14,7 @@ namespace SceneManager.Menus
{ {
internal static UIMenu Menu = new UIMenu("Scene Manager", "~o~Import Path Menu"); internal static UIMenu Menu = new UIMenu("Scene Manager", "~o~Import Path Menu");
internal static UIMenuItem Import { get; } = new UIMenuItem("Import", "Import the selected paths."); internal static UIMenuItem Import { get; } = new UIMenuItem("Import", "Import the selected paths.");
internal static List<string> ImportedFileNames { get; } = new List<string>();
private static List<string> _HasBeenImported = new List<string>();
internal static void Initialize() internal static void Initialize()
{ {
@ -32,11 +29,12 @@ namespace SceneManager.Menus
internal static void Build() internal static void Build()
{ {
Menu.Clear(); Menu.Clear();
PathManager.ImportPaths();
foreach(KeyValuePair<string, List<Path>> kvp in PathManager.ImportedPaths) GetFileNamesForPathsToImport();
foreach(string fileName in ImportedFileNames)
{ {
var menuItem = new UIMenuCheckboxItem(kvp.Key, false); var menuItem = new UIMenuCheckboxItem(fileName, false);
if(!_HasBeenImported.Contains(kvp.Key)) if (!PathManager.LoadedFiles.Contains(fileName))
{ {
menuItem.LeftBadge = UIMenuItem.BadgeStyle.Star; menuItem.LeftBadge = UIMenuItem.BadgeStyle.Star;
} }
@ -57,56 +55,59 @@ namespace SceneManager.Menus
{ {
if(selectedItem == Import) if(selectedItem == Import)
{ {
var checkboxItems = Menu.MenuItems.Where(x => x.GetType() == typeof(UIMenuCheckboxItem)); var checkboxItems = Menu.MenuItems.Where(x => x.GetType() == typeof(UIMenuCheckboxItem)).Cast<UIMenuCheckboxItem>();
foreach(UIMenuCheckboxItem menuItem in checkboxItems) var checkedItems = checkboxItems.Where(x => x.Checked);
foreach(var menuItem in checkedItems)
{ {
if(menuItem.Checked) var importedPaths = PathManager.ImportPathsFromFile(menuItem.Text);
if (importedPaths != null)
{ {
var pathsFromFile = PathManager.ImportedPaths.FirstOrDefault(x => x.Key == menuItem.Text).Value; PathManager.LoadImportedPaths(importedPaths, menuItem.Text);
foreach(Path path in pathsFromFile)
{
if(PathManager.Paths.Any(x => x != null && x.Name == path.Name))
{
Game.DisplayHelp($"A path with the name ~b~{path.Name} ~w~already exists. Do you want to replace it? ~{Keys.Y.GetInstructionalId()}~ or ~{Keys.N.GetInstructionalId()}~");
GameFiber.Sleep(100);
GameFiber.SleepUntil(() => Game.IsKeyDown(Keys.Y) || Game.IsKeyDown(Keys.N), 8300);
if(Game.IsKeyDown(Keys.Y))
{
var pathToReplace = PathManager.Paths.First(x => x.Name == path.Name);
var pathToReplaceIndex = Array.IndexOf(PathManager.Paths, pathToReplace);
pathToReplace.Delete();
PathManager.Paths[pathToReplaceIndex] = path;
path.Load();
Rage.Native.NativeFunction.Natives.CLEAR_ALL_HELP_MESSAGES();
_HasBeenImported.Add(PathManager.ImportedPaths.FirstOrDefault(x => x.Key == menuItem.Text).Key);
continue;
}
else
{
Game.DisplayNotification($"~o~Scene Manager ~y~[Import]\n~w~Path ~b~{path.Name} ~w~was not imported.");
Rage.Native.NativeFunction.Natives.CLEAR_ALL_HELP_MESSAGES();
continue;
}
}
var firstNullPathIndex = Array.IndexOf(PathManager.Paths, PathManager.Paths.First(x => x == null));
PathManager.Paths[firstNullPathIndex] = path;
path.Load();
_HasBeenImported.Add(PathManager.ImportedPaths.FirstOrDefault(x => x.Key == menuItem.Text).Key);
}
var numberOfNonNullPaths = PathManager.Paths.Where(x => x != null).Count();
Game.LogTrivial($"{menuItem.Text} added to paths collection. Paths count: {numberOfNonNullPaths}");
Menu.RefreshIndex();
} }
} }
Menu.RefreshIndex();
MenuManager.BuildMenus(); MenuManager.BuildMenus();
Menu.Visible = true; Menu.Visible = true;
} }
} }
private static void GetFileNamesForPathsToImport()
{
ImportedFileNames.Clear();
// Check if Saved Paths directory exists
var GAME_DIRECTORY = Directory.GetCurrentDirectory();
var SAVED_PATHS_DIRECTORY = GAME_DIRECTORY + "\\plugins\\SceneManager\\Saved Paths\\";
if (!Directory.Exists(SAVED_PATHS_DIRECTORY))
{
Game.LogTrivial($"Directory '\\plugins\\SceneManager\\Saved Paths' does not exist. No paths available to import.");
return;
}
// Check if any XML files are available to import from Saved Paths
var savedPathFiles = Directory.GetFiles(SAVED_PATHS_DIRECTORY, "*.xml");
if (savedPathFiles.Length == 0)
{
Game.LogTrivial($"No saved paths found.");
return;
}
else
{
Game.LogTrivial($"{savedPathFiles.Length} path(s) available to import.");
}
// Import file names
foreach (string file in savedPathFiles)
{
string fileName = System.IO.Path.GetFileNameWithoutExtension(file);
Game.LogTrivial($"File: {fileName}");
ImportedFileNames.Add(fileName);
}
Game.LogTrivial($"Successfully populated menu with {ImportedFileNames.Count} file(s).");
}
private static void ImportPathMenu_OnCheckboxChange(UIMenu sender, UIMenuCheckboxItem checkboxItem, bool Checked) private static void ImportPathMenu_OnCheckboxChange(UIMenu sender, UIMenuCheckboxItem checkboxItem, bool Checked)
{ {
var checkboxItems = Menu.MenuItems.Where(x => x.GetType() == typeof(UIMenuCheckboxItem)); var checkboxItems = Menu.MenuItems.Where(x => x.GetType() == typeof(UIMenuCheckboxItem));