roketto-launch/Models/TileModel.cs

259 lines
6.4 KiB
C#
Raw Normal View History

2024-08-06 20:27:08 +00:00
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; }
2024-08-06 20:27:08 +00:00
public override string ToString() => this.Title ?? string.Empty;
2024-08-06 20:27:08 +00:00
[JsonIgnore]
public string CleanProcessFilename
2024-08-06 20:27:08 +00:00
{
get
{
return (string.IsNullOrWhiteSpace(this.ProcessFilename) ? string.Empty : ResolvePath(this.ProcessFilename));
}
}
2024-08-06 20:27:08 +00:00
[JsonIgnore]
public string CleanProcessArgument
{
get
{
return (string.IsNullOrWhiteSpace(this.ProcessArgument) ? string.Empty : ResolvePath(this.ProcessArgument));
}
}
2024-08-06 20:27:08 +00:00
[JsonIgnore]
public string CleanProcessWorkingDirectory
{
get
{
return (string.IsNullOrWhiteSpace(this.ProcessWorkingDirectory) ? string.Empty : ResolvePath(this.ProcessWorkingDirectory));
}
2024-08-06 20:27:08 +00:00
}
private Tuple<string, string> SplitPath(string path, string needle)
2024-08-06 20:27:08 +00:00
{
if (string.IsNullOrWhiteSpace(path))
2024-08-06 20:27:08 +00:00
{
return null;
}
if (string.IsNullOrWhiteSpace(needle))
{
return null;
2024-08-06 20:27:08 +00:00
}
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;
}
2024-08-06 20:27:08 +00:00
string[] dirList = new string[0];
try
{
dirList = System.IO.Directory.GetDirectories(parts.Item1, "*", System.IO.SearchOption.TopDirectoryOnly);
2024-08-06 20:27:08 +00:00
}
catch
{
// do nothing
}
if (dirList.Length <= 0)
{
return null;
2024-08-06 20:27:08 +00:00
}
return System.IO.Path.GetFileName(dirList[0]);
2024-08-06 20:27:08 +00:00
}
private string GetLastPath(string value)
2024-08-06 20:27:08 +00:00
{
var parts = SplitPath(value, "%LAST%");
if (parts == null)
2024-08-06 20:27:08 +00:00
{
return null;
2024-08-06 20:27:08 +00:00
}
string[] dirList = new string[0];
try
{
dirList = System.IO.Directory.GetDirectories(parts.Item1, "*", System.IO.SearchOption.TopDirectoryOnly);
2024-08-06 20:27:08 +00:00
}
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)
2024-08-06 20:27:08 +00:00
{
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;
2024-08-06 20:27:08 +00:00
}
}
}