roketto-launch/Models/TileModel.cs
Ray aecc5d8b5a Changed main menu icons for RyzStudio ttf-icons
Added file version tracking to improve loading speed
2024-08-07 00:00:05 +01:00

259 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Text.Json.Serialization;
namespace RokettoLaunch.Models
{
public class TileModel
{
public string Title { get; set; }
public string ProcessFilename { get; set; }
public string ProcessArgument { get; set; }
public string ProcessWorkingDirectory { get; set; }
public ProcessWindowStyle ProcessWindowStyle { get; set; } = ProcessWindowStyle.Normal;
public bool ProcessAsAdmin { get; set; } = false;
[JsonConverter(typeof(RyzStudio.Text.Json.JsonPointConverter))]
public Point Position { get; set; }
public bool IsGroup { get; set; } = false;
public List<TileModel> Items { get; set; } = new List<TileModel>();
public string Version { get; set; }
public override string ToString() => this.Title ?? string.Empty;
[JsonIgnore]
public string CleanProcessFilename
{
get
{
return (string.IsNullOrWhiteSpace(this.ProcessFilename) ? string.Empty : ResolvePath(this.ProcessFilename));
}
}
[JsonIgnore]
public string CleanProcessArgument
{
get
{
return (string.IsNullOrWhiteSpace(this.ProcessArgument) ? string.Empty : ResolvePath(this.ProcessArgument));
}
}
[JsonIgnore]
public string CleanProcessWorkingDirectory
{
get
{
return (string.IsNullOrWhiteSpace(this.ProcessWorkingDirectory) ? string.Empty : ResolvePath(this.ProcessWorkingDirectory));
}
}
private Tuple<string, string> SplitPath(string path, string needle)
{
if (string.IsNullOrWhiteSpace(path))
{
return null;
}
if (string.IsNullOrWhiteSpace(needle))
{
return null;
}
if (!path.Contains(needle))
{
return null;
}
string head = path.Substring(0, path.IndexOf(needle));
string tail = path.Substring(path.IndexOf(needle) + needle.Length);
return new Tuple<string, string>(head, tail);
}
private string GetFirstPath(string value)
{
var parts = SplitPath(value, "%FIRST%");
if (parts == null)
{
return null;
}
string[] dirList = new string[0];
try
{
dirList = System.IO.Directory.GetDirectories(parts.Item1, "*", System.IO.SearchOption.TopDirectoryOnly);
}
catch
{
// do nothing
}
if (dirList.Length <= 0)
{
return null;
}
return System.IO.Path.GetFileName(dirList[0]);
}
private string GetLastPath(string value)
{
var parts = SplitPath(value, "%LAST%");
if (parts == null)
{
return null;
}
string[] dirList = new string[0];
try
{
dirList = System.IO.Directory.GetDirectories(parts.Item1, "*", System.IO.SearchOption.TopDirectoryOnly);
}
catch
{
// do nothing
}
if (dirList.Length <= 0)
{
return null;
}
return System.IO.Path.GetFileName(dirList[(dirList.Length - 1)]);
}
private string ResolvePath(string path)
{
var result = Environment.ExpandEnvironmentVariables(path);
if (result.Contains("%FIRST%"))
{
result = ResolveFirstPath(result);
}
if (result.Contains("%LAST%"))
{
result = ResolveLastPath(result);
}
return result;
}
private string ResolveFirstPath(string value)
{
var parts = SplitPath(value, "%FIRST%");
if (parts == null)
{
return value;
}
try
{
if (!System.IO.Directory.Exists(parts.Item1))
{
return value;
}
}
catch (Exception)
{
return value;
}
// Try cached version
if (!string.IsNullOrWhiteSpace(this.Version))
{
var filename = parts.Item1 + this.Version + parts.Item2;
try
{
if (System.IO.File.Exists(filename))
{
return filename;
}
}
catch (Exception)
{
}
}
// Resolve version
var version = GetFirstPath(value);
if (version == null)
{
return value;
}
this.Version = version;
return parts.Item1 + version + parts.Item2;
}
private string ResolveLastPath(string value)
{
var parts = SplitPath(value, "%LAST%");
if (parts == null)
{
return value;
}
try
{
if (!System.IO.Directory.Exists(parts.Item1))
{
return value;
}
}
catch (Exception)
{
return value;
}
// Try cached version
if (!string.IsNullOrWhiteSpace(this.Version))
{
var filename = parts.Item1 + this.Version + parts.Item2;
try
{
if (System.IO.File.Exists(filename))
{
return filename;
}
}
catch (Exception)
{
}
}
// Resolve version
var version = GetLastPath(value);
if (version == null)
{
return value;
}
this.Version = version;
return parts.Item1 + version + parts.Item2;
}
}
}