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
{
///
/// App.xaml 的交互逻辑
///
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);
}
///
/// 读取配置
///
///
///
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();
if (string.IsNullOrWhiteSpace(languageTokenValue))
{
languageToken = userSettingJToken.SelectToken("$.GeneralInfo.Language.DefaultValue");
if (languageToken != null)
{
language = languageToken.Value();
}
}
}
//获取ServerUrl设置
var selectToken = userSettingJToken.SelectToken("$.UserCommonSetting.Server.DefaultValue");
if (selectToken != null)
{
var selectItem = selectToken?.Value();
var serverUrlItem = userSettingJToken.SelectToken($"$.UserCommonSetting.['{selectItem}']");
serverUrlValue = serverUrlItem?.Value();
}
}
if (defalutSettingJToken != null)
{
//获取语言设置
if (languageToken == null)
{
languageToken = defalutSettingJToken.SelectToken("$.GeneralInfo.Language.DefaultValue");
if (languageToken != null)
{
language = languageToken.Value();
}
}
//获取ServerUrl设置
if (string.IsNullOrWhiteSpace(serverUrlValue))
{
var selectToken = userSettingJToken.SelectToken("$.ConnectInfo.Server.DefaultValue");
if (selectToken != null)
{
var selectItem = selectToken?.Value();
var serverUrlItem = userSettingJToken.SelectToken($"$.ConnectInfo.['{selectItem}']");
serverUrlValue = serverUrlItem?.Value();
}
}
}
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();
}
}
///
/// 是否是中文环境
///
public static bool IsChinese { get; set; } = false;
///
/// 更新版本号
///
public static string GetVersion
{
get
{
return UpgradeVersion;
}
}
public static string GetServerUrl
{
get
{
return ServerUrl;
}
}
///
/// 语言
///
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");
}
}