Skip to content

Commit

Permalink
Add check to see if a project in recent list exists before opening
Browse files Browse the repository at this point in the history
  • Loading branch information
CatmanFan committed Feb 14, 2025
1 parent ad16ed4 commit 332e95b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 52 deletions.
92 changes: 81 additions & 11 deletions FriishProduce/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,66 @@ private void OpenProject_Click(object sender, EventArgs e)
}
}

public bool CleanupRecent()
{
const int max = 10;
bool modified = false;

// Clean missing projects if there are any
// ********
for (int x = 0; x < max; x++)
{
var prop = Program.Config.paths.GetType().GetProperty($"recent_{x:D2}");
var path = prop.GetValue(Program.Config.paths, null)?.ToString();

if (path != null && !File.Exists(path))
{
prop.SetValue(Program.Config.paths, null);
modified = true;
}
}

// Clean duplicate projects if there are any
// ********
for (int x = 0; x < max; x++)
{
var prop1 = Program.Config.paths.GetType().GetProperty($"recent_{x:D2}");
var path1 = prop1.GetValue(Program.Config.paths, null)?.ToString();

for (int y = x; y < max; y++)
{
var prop2 = Program.Config.paths.GetType().GetProperty($"recent_{y:D2}");
var path2 = prop2.GetValue(Program.Config.paths, null)?.ToString();

if (path1 == path2 && path2 != null && x != y)
{
prop2.SetValue(Program.Config.paths, null);
modified = true;
}
}
}

// Resort slots in case of empty ones
// ********
for (int i = 0; i < max - 1; i++)
{
var prop1 = Program.Config.paths.GetType().GetProperty($"recent_{i:D2}");

if (prop1.GetValue(Program.Config.paths, null)?.ToString() == null)
{
var prop2 = Program.Config.paths.GetType().GetProperty($"recent_{i + 1:D2}");

prop1.SetValue(Program.Config.paths, prop2.GetValue(Program.Config.paths, null));
prop2.SetValue(Program.Config.paths, null);

modified = true;
}
}

if (modified) Program.Config.Save();
return modified;
}

public void RefreshRecent()
{
open_recent.MenuItems.Clear();
Expand Down Expand Up @@ -594,22 +654,32 @@ private void OpenProject(string[] files)
{
foreach (var file in files)
{
var project = new Project();
if (!File.Exists(file))
{
MessageBox.Show(string.Format(Program.Lang.Msg(11, 1), Path.GetFileName(file)));
if (CleanupRecent())
RefreshRecent();
}

try
else
{
using Stream stream = File.Open(file, FileMode.Open);
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
project = (Project)binaryFormatter.Deserialize(stream);
var project = new Project();

if (project.ProjectPath != file) project.ProjectPath = file;
try
{
using Stream stream = File.Open(file, FileMode.Open);
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
project = (Project)binaryFormatter.Deserialize(stream);

addTab(project.Platform, project);
}
if (project.ProjectPath != file) project.ProjectPath = file;

catch
{
MessageBox.Show(string.Format(Program.Lang.Msg(17, 1), Path.GetFileName(file)), MessageBox.Buttons.Ok, MessageBox.Icons.Error);
addTab(project.Platform, project);
}

catch
{
MessageBox.Show(string.Format(Program.Lang.Msg(17, 1), Path.GetFileName(file)), MessageBox.Buttons.Ok, MessageBox.Icons.Error);
}
}
}
}
Expand Down
44 changes: 3 additions & 41 deletions FriishProduce/ProjectForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,51 +354,13 @@ private void SetRecentProjects(string project)
}

Program.Config.paths.recent_00 = project;
modified = true;
}

// Clean duplicate projects if there are any
// ********
for (int x = 0; x < max; x++)
{
var prop1 = Program.Config.paths.GetType().GetProperty($"recent_{x:D2}");
var path1 = prop1.GetValue(Program.Config.paths, null)?.ToString();

for (int y = x; y < max; y++)
{
var prop2 = Program.Config.paths.GetType().GetProperty($"recent_{y:D2}");
var path2 = prop2.GetValue(Program.Config.paths, null)?.ToString();

if (path1 == path2 && path2 != null && x != y)
{
prop2.SetValue(Program.Config.paths, null);
modified = true;
}
}
}

// Resort slots in case of empty ones
// ********
for (int i = 0; i < max - 1; i++)
{
var prop1 = Program.Config.paths.GetType().GetProperty($"recent_{i:D2}");

if (prop1.GetValue(Program.Config.paths, null)?.ToString() == null)
{
var prop2 = Program.Config.paths.GetType().GetProperty($"recent_{i + 1:D2}");

prop1.SetValue(Program.Config.paths, prop2.GetValue(Program.Config.paths, null));
prop2.SetValue(Program.Config.paths, null);
Program.Config.Save();

modified = true;
}
modified = true;
}

if (modified)
{
Program.Config.Save();
if (Program.MainForm.CleanupRecent() || modified)
Program.MainForm.RefreshRecent();
}
}

public void SaveProject(string path)
Expand Down

0 comments on commit 332e95b

Please sign in to comment.