math-suite/Source/Launcher/Modules.cs

104 lines
3.6 KiB
C#
Raw Normal View History

2018-02-05 23:24:46 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DynamicLink
{
public static class Modules
{
private static bool foundSolution = false;
private static List<ModuleInternal> items = new List<ModuleInternal>();
public static List<ModuleInternal> Items
{
get { return items; }
}
#region Useful routines
public static void RegisterModule(string name, string path, string description = null, System.Drawing.Icon icon = null)
{
if (!IsRegistered(name))
items.Add(new ModuleInternal() { Name = name, Description = description, Path = path, Icon = icon });
else if (foundSolution == true)
{
int index = 0;
for (int i = 0; i < items.Count; i++)
if (items[i].Name == name) index = i;
// If new file is an object file, don't allow
if (path.ToLower().Contains("obj")) return;
// If it is an object file, it won't work, replace it
if (items[index].Path.ToLower().Contains("obj"))
{
// Replace
items[index] = new ModuleInternal() { Name = name, Description = description, Path = path, Icon = icon };
return;
}
// If newer, replace
DateTime neww = System.IO.File.GetCreationTime(path);
DateTime old = System.IO.File.GetCreationTime(items[index].Path);
if (old < neww)
items[index] = new ModuleInternal() { Name = name, Description = description, Path = path, Icon = icon };
}
}
public static bool IsRegistered(string name)
{
var list = items.Where(x => x.Name == name);
return (list.Count() != 0);
}
public static void ExecuteModule(string name, string args = "")
{
var list = items.Where(x => x.Name == name);
if (list.Count() == 0) return;
System.Diagnostics.Process.Start(list.First().Path, args);
}
#endregion
#region Collect data
private static string[] CollectData_GetFileList()
{
List<string> files = new List<string>();
// Try to find files in the current solution
string dir = System.Reflection.Assembly.GetExecutingAssembly().Location;
int find = dir.IndexOf("TibisMathematicsSuite\\");
// Success
if (find != -1)
{
string temp = dir.Remove(find) + "TibisMathematicsSuite\\";
files.AddRange (System.IO.Directory.GetFiles(temp, "*.exe", System.IO.SearchOption.AllDirectories));
foundSolution = true;
}
// Fail, just add current directory
else files.AddRange(System.IO.Directory.GetFiles(System.IO.Directory.GetCurrentDirectory(), "*.exe", System.IO.SearchOption.AllDirectories));
return files.ToArray();
}
public static void CollectData()
{
string[] list = CollectData_GetFileList();
foreach (var i in list)
{
System.Diagnostics.FileVersionInfo info = System.Diagnostics.FileVersionInfo.GetVersionInfo(i);
if (info.ProductName == "Tibi's Mathematics Suite")
RegisterModule(info.FileDescription, i, info.Comments, System.Drawing.Icon.ExtractAssociatedIcon(i));
}
}
#endregion
}
}