123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- using System;
- using System.Diagnostics;
- using System.IO.Compression;
- namespace FisTools
- {
- public class FFmpegException : Exception
- {
- public FFmpegException(int exitCode, Exception innerException = null)
- : base($"Exit Code: {exitCode}.\nSee FFmpeg Log for more info.", innerException) { }
- }
- public class FFmpegService
- {
- private string _ffmpegPath = "ffmpeg";
- public void SetFFmpegPath(string ffmpegPath)
- {
- _ffmpegPath = ffmpegPath;
- }
- public void StartFFmpeg(out Process process, string arguments)
- {
- process = new Process
- {
- StartInfo =
- {
- FileName = _ffmpegPath,
- Arguments = arguments,
- UseShellExecute = false,
- Verb = "runas",
- CreateNoWindow = true,
- RedirectStandardError = true,
- RedirectStandardInput = true
- },
- EnableRaisingEvents = true
- };
- process.Start();
- process.BeginErrorReadLine();
- }
- public void ForceCloseFFmpegIfStillAlive()
- {
- var fmpegProcess = Process.GetProcessesByName("ffmpeg");
- foreach (var f in fmpegProcess)
- {
- f.Kill();
- }
- }
- }
- public class LoaderCenter
- {
- private string _processName=string.Empty;
- public void StartProcessWithFileName(string filePath, string arguments,string processName)
- {
- _processName = processName;
- var process = new Process
- {
- StartInfo =
- {
- FileName = filePath,
- Arguments = arguments,
- UseShellExecute = false,
- Verb = "runas",
- CreateNoWindow = true,
- RedirectStandardError = true,
- RedirectStandardInput = true
- },
- };
- process.Start();
- }
- public void StartProcessWithWorkingDirectory(string filePath,string workingDirectory)
- {
- var process = new Process
- {
- StartInfo =
- {
- FileName = filePath,
- WorkingDirectory = workingDirectory,
- UseShellExecute = false,
- CreateNoWindow = true,
- RedirectStandardOutput = true,
- Verb = "runas",
- },
- };
- process.Start();
- }
- /// <summary>
- /// 校验APP是否已存在,已存在则直接唤起
- /// </summary>
- /// <returns></returns>
- public bool CheckAppIsInvalid(Action<string> logger)
- {
- var id = Process.GetCurrentProcess().Id;
- Process[] processesByNames = Process.GetProcessesByName("fis");
- bool appInvalid=false;
- foreach (var pn in processesByNames)
- {
- //如果存在同名但不同id的进程
- if (pn.Id != id)
- {
- logger.Invoke("Got another fis process");
- var threads = pn.Threads;
- foreach (ProcessThread t in threads)
- {
- logger.Invoke("Exist process status is:");
- logger.Invoke(t.ThreadState.ToString());
- var appHangup = t.ThreadState == System.Diagnostics.ThreadState.Unknown
- || t.ThreadState == System.Diagnostics.ThreadState.Terminated
- || t.ThreadState == System.Diagnostics.ThreadState.Wait;
-
- if (appHangup)
- {
- logger.Invoke("Kill the existing hang up process");
- //如果存在挂起线程,则杀死
- KillProcessWithGivenName("fis", false);
- }
- if (t.ThreadState == System.Diagnostics.ThreadState.Running)
- {
- appInvalid = true;
- }
- }
- }
- }
- return appInvalid;
- }
- /// <summary>
- /// 打开守护进程
- /// </summary>
- public void OpenFlyinsonoDaemon()
- {
- var process = Process.GetProcessesByName("FlyinsonoDaemon.exe").FirstOrDefault();
- if (process == null)
- {
- var flyinsonoDaemonPath = "C:\\vinno\\daemon\\FlyinsonoDaemon.exe";
- ToolManager.Instance.LoaderCenter.StartProcessDirectly(flyinsonoDaemonPath);
- }
- }
- public void StartProcessDirectly(string filePath)
- {
- var process = new Process
- {
- StartInfo =
- {
- FileName = filePath,
- UseShellExecute = false,
- Verb = "runas",
- CreateNoWindow = true,
- RedirectStandardError = true,
- },
- EnableRaisingEvents = true
- };
- process.Start();
- }
- public void StartProcessWithUI(string filePath)
- {
- var process = new Process
- {
- StartInfo =
- {
- FileName = filePath,
- },
- };
- process.Start();
- }
- public void ForceCloseIfStillAlive()
- {
- if (!string.IsNullOrEmpty(_processName))
- {
- var fmpegProcess = Process.GetProcessesByName(_processName);
- foreach (var f in fmpegProcess)
- {
- f.Kill();
- }
- }
- }
- public void KillProcessWithGivenName(string processName,bool killSelf=true)
- {
- var processes = Process.GetProcessesByName(processName);
- foreach (Process process in processes)
- {
- if (process.ProcessName == processName)
- {
- if (killSelf)
- {
- process.Kill();
- process.WaitForExit();
- }
- else if(process.Id != Process.GetCurrentProcess().Id)
- {
- process.Kill();
- process.WaitForExit();
- }
- }
- }
- }
- }
- // public class AutoStartHelper
- // {
- // /// <summary>
- // /// App开机自启动 Reg key
- // /// </summary>
- // private const string AppAutoStartRegistryKey = "FisAutoStart";
- // /// <summary>
- // /// 当前用户注册表开启启动路径
- // /// </summary>
- // private const string RegPath = @"Software\Microsoft\Windows\CurrentVersion\Run";
- // /// <summary>
- // /// 设置APP开机自启动
- // /// </summary>
- // /// <returns></returns>
- // public bool SetAppAutoStart()
- // {
- // ///默认选择Release下的loader.exe
- // var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "loader.exe");
- //#if DEBUG
- // ///Debug模式下启动fis.exe
- // path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fis.exe");
- //#endif
- // return SetAutoStartByPath(path, AppAutoStartRegistryKey);
- // }
- // /// <summary>
- // /// 取消APP开机自启动
- // /// </summary>
- // /// <returns></returns>
- // public bool AbortAppAutoStart()
- // {
- // return AbortAutoByRegKey(AppAutoStartRegistryKey);
- // }
- // /// <summary>
- // /// 根据指定的key取消开机自启动
- // /// </summary>
- // /// <param name="regkey"></param>
- // /// <returns></returns>
- // public bool AbortAutoByRegKey(string regkey)
- // {
- // try
- // {
- // var currentUserRegKey = Registry.CurrentUser;
- // var startUpKey = currentUserRegKey.CreateSubKey(RegPath);
- // startUpKey.DeleteValue(regkey, false);
- // startUpKey.Close();
- // currentUserRegKey.Close();
- // return true;
- // }
- // catch (Exception ex)
- // {
- // return false;
- // }
- // }
- // /// <summary>
- // /// 根据给定开机自启动文件路径和注册表的key设置开启自启动
- // /// </summary>
- // /// <param name="regkey"></param>
- // /// <returns></returns>
- // public bool SetAutoStartByPath(string path, string regKey)
- // {
- // try
- // {
- // var currentUserKey = Registry.CurrentUser;
- // var startUpKey = currentUserKey.CreateSubKey(RegPath);
- // startUpKey.SetValue(regKey, path);
- // startUpKey.Close();
- // currentUserKey.Close();
- // return true;
- // }
- // catch (Exception)
- // {
- // return false;
- // }
- // }
- // }
- public class UpgradeCenter
- {
- private int _totalEntrysCount;
- private int _finishedCount;
- private const int ExpirationCount = 90;
- private Action<double>? _onProgressChanged;
- public void ExecutePartUpgrade(Action<string> logger)
- {
- logger.Invoke($"Part upgrade");
- ToolManager.Instance.LoaderCenter.KillProcessWithGivenName("fis");
- var currentDir = AppDomain.CurrentDomain.BaseDirectory;
- var fisDir = Directory.GetParent(currentDir)!.Parent!.FullName;
- logger($"Fis dir is {fisDir}");
- var webZipPath = Path.Combine(fisDir, "App", "flyinsono");
- var updateCacheDir = Path.Combine(fisDir, "UpgradCache");
- var filesToCopy = Directory.GetFiles(updateCacheDir);
- foreach (var file in filesToCopy)
- {
- var ext = Path.GetExtension(file);
- if (ext != ".zip")
- {
- logger($"Skipped none zip file for part upgrade");
- continue;
- }
- var fileName = Path.GetFileName(file);
- var fileDest = Path.Combine(webZipPath, fileName);
- File.Copy(file, fileDest, true);
- File.Delete(file);
- }
- //var fisFileName = Path.Combine(fisDir, "fis.exe");
- //ToolManager.Instance.LoaderCenter.StartProcessDirectly(fisFileName);
- }
- public void ExecuteFullUpgrade(Action<string> logger, Action<double>? onProgressChanged = null)
- {
- logger.Invoke($"Full upgrade");
- _onProgressChanged = onProgressChanged;
- ToolManager.Instance.LoaderCenter.KillProcessWithGivenName("fis");
- var currentDir = AppDomain.CurrentDomain.BaseDirectory;
- var fisDir = Directory.GetParent(currentDir)!.Parent!.FullName;
- logger($"Fis dir is {fisDir}");
- var filesToCopy = Directory.GetFiles(currentDir);
- foreach (var file in filesToCopy)
- {
- var ext = Path.GetExtension(file);
- logger($"File name {file}");
- if (ext != ".zip")
- {
- logger($"None zip file skipped {file}");
- continue;
- }
- var fileName = Path.GetFileName(file);
- if (!fileName.Contains("fis_package"))
- {
- logger($"Zip file skipped {file}");
- continue;
- }
- logger($"Do extract {file} begin");
- var fileNameWithoutExt = Path.GetFileNameWithoutExtension(file);
- var filePathToSkip = $"{fileNameWithoutExt}/";
- logger("Begin to extract");
- Task.Run(() =>
- {
- var waitCount = 0;
- while (true)
- {
- waitCount++;
- Thread.Sleep(1000);
- if (waitCount == 90)
- {
- _onProgressChanged?.Invoke(2.0);
- break;
- }
- var progress = Math.Round((double)_finishedCount / (double)_totalEntrysCount, 2);
- _onProgressChanged?.Invoke(progress);
- if (progress == 1)
- {
- break;
- }
- }
- });
- using (ZipArchive archive = ZipFile.OpenRead(file))
- {
- try
- {
- var entrys = archive.Entries;
- _totalEntrysCount = entrys.Count;
- var filesEntries = entrys.Where(c => c.Name != "");
- foreach (ZipArchiveEntry fileEntry in filesEntries)
- {
- var entrySubPath = fileEntry.FullName.Replace(filePathToSkip, "");
- string entryPath = Path.Combine(fisDir, entrySubPath);
- if (fileEntry.Name.Contains("libHarfBuzzSharp") || fileEntry.Name.Contains("libSkiaSharp"))
- {
- _finishedCount++;
- continue;
- }
- try
- {
- CreateDirWithFileName(fileEntry.FullName, fisDir);
- fileEntry.ExtractToFile(entryPath, true);
- }
- catch (Exception ex)
- {
- logger($"Extract single file error {ex}");
- }
- logger($"{fileEntry.FullName} extract success.");
- _finishedCount++;
- }
- }
- catch (Exception ex)
- {
- logger($"Extract error {ex}");
- }
- }
- logger("End to extract");
- logger($"Do extract {file} end");
- logger($"Do delete {file} begin");
- File.Delete(file);
- logger($"Do delete {file} end");
- }
- logger($"Do restart begin");
- var fisFileName = Path.Combine(fisDir, "fis.exe");
- if (_onProgressChanged == null)
- {
- ToolManager.Instance.LoaderCenter.StartProcessDirectly(fisFileName);
- }
- logger($"Do restart end");
- }
- private void CreateDirWithFileName(string fullName, string fisDir)
- {
- var filePathElements = fullName.Split("/");
- if (filePathElements.Count() == 1)
- {
- return;
- }
- var fullpath = "";
- foreach (var filePath in filePathElements)
- {
- if (filePath.Contains("."))
- {
- continue;
- }
- fullpath = Path.Combine(fullpath, filePath);
- var path = Path.Combine(fisDir, fullpath);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- }
- }
- }
- public class ToolManager
- {
- private ToolManager()
- {
- }
- private FFmpegService _ffmpegService;
- private LoaderCenter _loaderCenter;
- //private AutoStartHelper _autoStartHelper;
- private UpgradeCenter _upgradeCenter;
- static private ToolManager _instance;
- public static ToolManager Instance
- {
- get{
- if (_instance == null)
- {
- _instance = new ToolManager();
- }
- return _instance;
- }
- }
- public LoaderCenter LoaderCenter
- {
- get {
- if (_loaderCenter == null)
- {
- _loaderCenter = new LoaderCenter();
- }
- return _loaderCenter;
- }
- }
- public FFmpegService FFmpegService
- {
- get
- {
- if (_ffmpegService == null)
- {
- _ffmpegService = new FFmpegService();
- }
- return _ffmpegService;
- }
- }
- public UpgradeCenter UpgradeCenter
- {
- get
- {
- if (_upgradeCenter == null)
- {
- _upgradeCenter = new UpgradeCenter();
- }
- return _upgradeCenter;
- }
- }
- //public AutoStartHelper AutoStartHelper
- //{
- // get {
- // if (_autoStartHelper == null)
- // {
- // _autoStartHelper = new AutoStartHelper();
- // }
- // return _autoStartHelper;
- // }
- //}
- }
-
- }
|