using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace VRCX
{
///
/// The class responsible for launching user-defined applications when VRChat opens/closes.
///
public class AutoAppLaunchManager
{
public static AutoAppLaunchManager Instance { get; private set; }
public static readonly string VRChatProcessName = "VRChat";
public bool Enabled = false;
/// Whether or not to kill child processes when VRChat closes.
public bool KillChildrenOnExit = true;
public readonly string AppShortcutDirectory;
private DateTime startTime = DateTime.Now;
private Dictionary startedProcesses = new Dictionary();
private static readonly byte[] shortcutSignatureBytes = { 0x4C, 0x00, 0x00, 0x00 }; // signature for ShellLinkHeader\
static AutoAppLaunchManager()
{
Instance = new AutoAppLaunchManager();
}
public AutoAppLaunchManager()
{
AppShortcutDirectory = Path.Combine(Program.AppDataDirectory, "startup");
if (!Directory.Exists(AppShortcutDirectory))
{
Directory.CreateDirectory(AppShortcutDirectory);
}
ProcessMonitor.Instance.ProcessStarted += OnProcessStarted;
ProcessMonitor.Instance.ProcessExited += OnProcessExited;
}
private void OnProcessExited(MonitoredProcess monitoredProcess)
{
if (startedProcesses.Count == 0 || !monitoredProcess.HasName(VRChatProcessName))
return;
if (KillChildrenOnExit)
KillChildProcesses();
}
private void OnProcessStarted(MonitoredProcess monitoredProcess)
{
if (!Enabled || !monitoredProcess.HasName(VRChatProcessName) || monitoredProcess.Process.StartTime < startTime)
return;
if (KillChildrenOnExit)
KillChildProcesses();
var shortcutFiles = FindShortcutFiles(AppShortcutDirectory);
foreach (var file in shortcutFiles)
{
if (!IsChildProcessRunning(file))
{
StartChildProcess(file);
}
}
}
///
/// Kills all running child processes.
///
internal void KillChildProcesses()
{
foreach (var pair in startedProcesses)
{
var process = pair.Value;
if (!process.HasExited)
process.Kill();
}
startedProcesses.Clear();
}
///
/// Starts a new child process.
///
/// The path.
internal void StartChildProcess(string path)
{
var process = Process.Start(path);
startedProcesses.Add(path, process);
}
///
/// Updates the child processes list.
/// Removes any processes that have exited.
///
internal void UpdateChildProcesses()
{
foreach (var pair in startedProcesses.ToList())
{
var process = pair.Value;
if (process.HasExited)
startedProcesses.Remove(pair.Key);
}
}
///
/// Checks to see if a given file matches a current running child process.
///
/// The path.
///
/// true if child process running; otherwise, false.
///
internal bool IsChildProcessRunning(string path)
{
return startedProcesses.ContainsKey(path);
}
internal void Init()
{
// What are you lookin at?
}
internal void Exit()
{
Enabled = false;
KillChildProcesses();
}
///
/// Finds windows shortcut files in a given folder.
///
/// The folder path.
/// An array of shortcut paths. If none, then empty.
private static string[] FindShortcutFiles(string folderPath)
{
DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
FileInfo[] files = directoryInfo.GetFiles();
List ret = new List();
foreach (FileInfo file in files)
{
if (IsShortcutFile(file.FullName))
{
ret.Add(file.FullName);
}
}
return ret.ToArray();
}
///
/// Determines whether the specified file path is a shortcut by checking the file header.
///
/// The file path.
/// true if the given file path is a shortcut, otherwise false
private static bool IsShortcutFile(string filePath)
{
byte[] headerBytes = new byte[4];
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
if (fileStream.Length >= 4)
{
fileStream.Read(headerBytes, 0, 4);
}
}
return headerBytes.SequenceEqual(shortcutSignatureBytes);
}
}
}