123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Windows;
- using vCloud.Windows.ForceUpgrade.Common;
- using Vinno.IUS.Common.Network;
- namespace vCloud.Windows.ForceUpgrade
- {
- /// <summary>
- /// App.xaml 的交互逻辑
- /// </summary>
- public partial class App : Application
- {
- private static string UpgradeVersion { get; set; } = "New Version";
- private static string ServerUrl { get; set; }
- Mutex mutex;
- protected override void OnStartup(StartupEventArgs e)
- {
- Thread.Sleep(1000);//等待其他程序退出;
- bool ret;
- mutex = new Mutex(true, "vCloud.Windows.ForceUpgrade", out ret);
- if (!ret)
- {
- Environment.Exit(0);
- }
- else
- {
- IsChinese = e.Args.Contains("-zh");
- InitLanguage();
- foreach (var arg in e.Args)
- {
- var matchValue = Regex.Match(arg, "-v([^ ]*)");
- if (matchValue.Success && matchValue.Groups.Count > 1)
- {
- UpgradeVersion = matchValue.Groups[1].Value;
- }
- }
- CommonInitializer.Initialize(Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory) + "\\Vinno.VCloud");
- }
- try
- {
- if (!IsChinese)
- {
- ServerUrl = GetClientSettings(out string language);
- if (language == "Chinese")
- {
- IsChinese = true;
- InitLanguage();
- }
- }
- var ectype = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "vCloud.Windows.exe");
- var original = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "vCloud.Windows.ForceUpgrade.exe");
- if (File.Exists(ectype) && !File.Exists(original))
- {
- File.Copy(ectype, original, true);
- Process.Start(new ProcessStartInfo()
- {
- WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
- Verb = "runas",
- FileName = "vCloud.Windows.ForceUpgrade.exe",
- UseShellExecute = false
- });
- Thread.Sleep(100);
- Environment.Exit(0);
- }
- else
- {
- using (Process ps = new Process())
- {
- ps.StartInfo.FileName = $"cmd.exe";
- ps.StartInfo.UseShellExecute = false;
- ps.StartInfo.RedirectStandardOutput = true;
- ps.StartInfo.RedirectStandardInput = true;
- ps.StartInfo.CreateNoWindow = true;
- ps.StartInfo.Verb = "runas";
- ps.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
- ps.Start();
- ps.StandardInput.WriteLine($"{Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory)}:");
- ps.StandardInput.WriteLine($"cd { AppDomain.CurrentDomain.BaseDirectory }");
- ps.StandardInput.WriteLine($"taskkill /f /t /im vCloud.Windows.exe");
- ps.WaitForExit(100);
- ps.StandardInput.WriteLine($"exit");
- ps.Close();
- }
- }
- }
- catch (Exception ex)
- {
- File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"), $"{ex}");
- }
- }
- private void InitLanguage()
- {
- TranslateHelper.CurrentLanguage = IsChinese ? TranslateHelper.ChineseLanguageKey : TranslateHelper.EnglishLanguageKey;
- InitializeLanguage(TranslateHelper.CurrentLanguage);
- }
- /// <summary>
- /// 读取配置
- /// </summary>
- /// <param name="language"></param>
- /// <returns></returns>
- private static string GetClientSettings(out string language)
- {
- var serverUrlValue = string.Empty;
- language = "English";
- var userSetting = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings", "Client", "UserCommonSetting.conf");
- var defalutSetting = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings", "Client", "Setting.conf");
- string userSettingContent = File.Exists(userSetting) ? File.ReadAllText(userSetting) : string.Empty;
- string defalutSettingContent = File.Exists(defalutSetting) ? File.ReadAllText(defalutSetting) : string.Empty;
- JToken userSettingJToken = !string.IsNullOrWhiteSpace(userSettingContent) ? JToken.Parse(userSettingContent) : null;
- JToken defalutSettingJToken = !string.IsNullOrWhiteSpace(defalutSettingContent) ? JToken.Parse(defalutSettingContent) : null;
- JToken languageToken = null;
- if (userSettingJToken != null)
- {
- //获取语言设置
- languageToken = userSettingJToken.SelectToken("$.UserCommonSetting.Language");
- if (languageToken != null)
- {
- var languageTokenValue = languageToken?.Value<string>();
- if (string.IsNullOrWhiteSpace(languageTokenValue))
- {
- languageToken = userSettingJToken.SelectToken("$.GeneralInfo.Language.DefaultValue");
- if (languageToken != null)
- {
- language = languageToken.Value<string>();
- }
- }
- }
- //获取ServerUrl设置
- var selectToken = userSettingJToken.SelectToken("$.UserCommonSetting.Server.DefaultValue");
- if (selectToken != null)
- {
- var selectItem = selectToken?.Value<string>();
- var serverUrlItem = userSettingJToken.SelectToken($"$.UserCommonSetting.['{selectItem}']");
- serverUrlValue = serverUrlItem?.Value<string>();
- }
- }
- if (defalutSettingJToken != null)
- {
- //获取语言设置
- if (languageToken == null)
- {
- languageToken = defalutSettingJToken.SelectToken("$.GeneralInfo.Language.DefaultValue");
- if (languageToken != null)
- {
- language = languageToken.Value<string>();
- }
- }
- //获取ServerUrl设置
- if (string.IsNullOrWhiteSpace(serverUrlValue))
- {
- var selectToken = userSettingJToken.SelectToken("$.ConnectInfo.Server.DefaultValue");
- if (selectToken != null)
- {
- var selectItem = selectToken?.Value<string>();
- var serverUrlItem = userSettingJToken.SelectToken($"$.ConnectInfo.['{selectItem}']");
- serverUrlValue = serverUrlItem?.Value<string>();
- }
- }
- }
- return serverUrlValue;
- }
- private void InitializeLanguage(string currentLanguage)
- {
- var assemblyName = GetType().Assembly.GetName().Name;
- foreach (var supportedLanguage in TranslateHelper.SupportedLanguages)
- {
- var resourceName = $"{assemblyName}.Common.Languages.{supportedLanguage}.json";
- using (var languageStream = GetType().Assembly.GetManifestResourceStream(resourceName))
- {
- if (languageStream != null)
- {
- var reader = new StreamReader(languageStream);
- var content = reader.ReadToEnd();
- TranslateHelper.AddLanguageResource(supportedLanguage, content);
- }
- }
- }
- if (TranslateHelper.SupportedLanguages.FirstOrDefault(v => v == currentLanguage) != null)
- {
- TranslateHelper.CurrentLanguage = currentLanguage;
- }
- else
- {
- TranslateHelper.CurrentLanguage = TranslateHelper.SupportedLanguages.FirstOrDefault();
- }
- }
- /// <summary>
- /// 是否是中文环境
- /// </summary>
- public static bool IsChinese { get; set; } = false;
- /// <summary>
- /// 更新版本号
- /// </summary>
- public static string GetVersion
- {
- get
- {
- return UpgradeVersion;
- }
- }
- public static string GetServerUrl
- {
- get
- {
- return ServerUrl;
- }
- }
- /// <summary>
- /// 语言
- /// </summary>
- public static bool GetIsChinese
- {
- get
- {
- return IsChinese;
- }
- }
- public static string GetTitle => TranslateHelper.Translate("Updating");
- public static string GetFinishShow => TranslateHelper.Translate("UpdateCompleted");
- public static string GetOfflineShow => TranslateHelper.Translate("OffLine");
- public static string UpgradeError => TranslateHelper.Translate("UpgradeError");
- public static string PartFilesFail => TranslateHelper.Translate("PartFilesFail");
- }
- }
|