using Microsoft.Win32;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PackingPress.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace PackingPress
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : BaseWindow
{
List installEntitys = new List();
private string pcClientFilePath = string.Empty;
public MainWindow()
{
funcInOutHelper.AddDelegate(DecompressionFinish);
funcInOutHelper.AddDelegate(CreatePackageFinish);
funcInOutHelper.AddDelegate(HideWindow);
funcInOutHelper.AddDelegate(BingControl);
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (GetHardDiskFreeSpace() < 1024)
{
MessageBox.Show(TranslateHelper.Translate("Diskfull"));
return;
}
WaitProcessWindow processWindow = new WaitProcessWindow();
processWindow.Show();
Task.Run(() =>
{
funcInOutHelper.RunDelegate("HideWindow");
var directorys = new List();
var files = new List();
var packages = new List();
List selectItems = dtgShow.SelectedCells.Select(f => f.Item as InstallEntity).Distinct().ToList();
if (selectItems.Count == 0)
{
selectItems = installEntitys;
}
bool isNeedOldAndroid = true, isNeedOldPC = true, isNeedOldAgent = true, isNeedOldMac = true, isNeedOldvBox = true;
SettingJsonGet jObjectSystemConfig = new SettingJsonGet(AppDomain.CurrentDomain.BaseDirectory + "\\DescriptionConfig.json");
var jsonRoot = (JObject)jObjectSystemConfig.GetJsonRoot();
foreach (var item in selectItems)
{
var entity = item;
if (entity != null && !string.IsNullOrWhiteSpace(entity.Path))
{
if (entity != null && entity.IsDirectory)
{
directorys.Add(entity);
}
else
{
files.Add(entity);
}
string pcClientVersion = RegexHelper.MatchSub(entity.Path, "Flyinsono_Windows_([0-9.]*).exe");
if (!string.IsNullOrWhiteSpace(pcClientVersion))
{
pcClientFilePath = entity.FullPath;
AddPackageFile(packages, jsonRoot, PackagePlatform.PC, PackageType.Client, pcClientVersion, entity.FullPath);
isNeedOldPC = false;
continue;
}
string vboxClientVersion = RegexHelper.MatchSub(entity.Path, "Flyinsono_SonoPost_([0-9.]*).apk");
if (!string.IsNullOrWhiteSpace(vboxClientVersion))
{
AddPackageFile(packages, jsonRoot, PackagePlatform.Android, PackageType.Agent, vboxClientVersion, entity.FullPath);
isNeedOldvBox = false;
continue;
}
string androidClientVersion = RegexHelper.MatchSub(entity.Path, "Flyinsono_Android_([0-9.]*).apk");
if (!string.IsNullOrWhiteSpace(androidClientVersion))
{
AddPackageFile(packages, jsonRoot, PackagePlatform.Android, PackageType.Client, androidClientVersion, entity.FullPath);
isNeedOldAndroid = false;
continue;
}
string agentVersion = RegexHelper.MatchSub(entity.Path, "AgentSetup_([0-9.]*).exe");
if (!string.IsNullOrWhiteSpace(agentVersion))
{
AddPackageFile(packages, jsonRoot, PackagePlatform.PC, PackageType.Agent, agentVersion, entity.FullPath);
isNeedOldAgent = false;
continue;
}
string macClientVersion = RegexHelper.MatchSub(entity.Path, "Flyinsono_Mac_([0-9.]*).pkg");
if (!string.IsNullOrWhiteSpace(macClientVersion))
{
AddPackageFile(packages, jsonRoot, PackagePlatform.Mac, PackageType.Client, macClientVersion, entity.FullPath);
isNeedOldMac = false;
continue;
}
}
}
if (isNeedOldAgent || isNeedOldAndroid || isNeedOldPC || isNeedOldMac || isNeedOldvBox)
{
UpgradePackageReader upgradePackageReader = new UpgradePackageReader("bakupgrade.pak");
foreach (var package in upgradePackageReader.Packages)
{
switch (package.Platform)
{
case PackagePlatform.Android:
if (package.Type == PackageType.Agent)
{
if (isNeedOldvBox)
{
packages.Add(new UpgradePackage(package.Platform, package.Type, package.Version, package.Description, package.FileData));
}
}
else
{
if (isNeedOldAndroid)
{
packages.Add(new UpgradePackage(package.Platform, package.Type, package.Version, package.Description, package.FileData));
}
}
break;
case PackagePlatform.PC:
if (isNeedOldPC)
{
packages.Add(new UpgradePackage(package.Platform, package.Type, package.Version, package.Description, package.FileData));
}
break;
case PackagePlatform.Mac:
if (isNeedOldMac)
{
packages.Add(new UpgradePackage(package.Platform, package.Type, package.Version, package.Description, package.FileData));
}
break;
}
if (isNeedOldAgent && package.Type == PackageType.Agent)
{
packages.Add(new UpgradePackage(package.Platform, package.Type, package.Version, package.Description, package.FileData));
}
}
}
if (files.Count > 0)
{
var writer = new UpgradePackageWriter("bakupgrade.pak");
foreach (var upgradePackage in packages)
{
writer.AddPackage(upgradePackage);
}
writer.Save();
}
var systemPackageDir = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemPackage");
var uFilePackageDir = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UFilePackage");
Directory.CreateDirectory(systemPackageDir);
CreateUpgradePackage(systemPackageDir);
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.Start();
if (files.Count > 0)
{
File.Copy(
System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bakupgrade.pak"),
System.IO.Path.Combine(systemPackageDir, "upgrade.pak"),
true);
}
directorys.ForEach(directory =>
{
ps.StandardInput.WriteLine($"xcopy \"{directory.FullPath}\" \"{ AppDomain.CurrentDomain.BaseDirectory}\\SystemPackage\\{directory.Path}\\\" /c/e/q/y/j/r");
});
files.ForEach(fileInfo =>
{
if (RegexHelper.IsMatch(fileInfo.Path, "Flyinsono_Windows_([0-9.]*).exe")) { return; }
if (RegexHelper.IsMatch(fileInfo.Path, "Flyinsono_SonoPost_([0-9.]*).apk")) { return; }
Directory.CreateDirectory(uFilePackageDir);
File.Copy(
fileInfo.FullPath,
System.IO.Path.Combine(uFilePackageDir, fileInfo.Path),
true);
});
ps.StandardInput.WriteLine($"exit");
var result = ps.StandardOutput.ReadToEnd();
}
Thread.Sleep(8000);
var ufilePackageZip = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UFilePackage.zip");
if (File.Exists(ufilePackageZip))
{
File.Delete(ufilePackageZip);
}
var systemPackageZip = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemPackage.zip");
if (File.Exists(systemPackageZip))
{
File.Delete(systemPackageZip);
}
if (Directory.Exists(systemPackageDir))
{
ZipHelper.Zips(
systemPackageDir,
systemPackageZip
);
}
if (Directory.Exists(uFilePackageDir))
{
ZipHelper.Zips(
uFilePackageDir,
ufilePackageZip
);
}
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.Start();
ps.StandardInput.WriteLine($"rd /s /q \"{ AppDomain.CurrentDomain.BaseDirectory}\\SystemPackage\\\"");
ps.StandardInput.WriteLine($"rd /s /q \"{ AppDomain.CurrentDomain.BaseDirectory}\\UFilePackage\\\"");
ps.StandardInput.WriteLine($"rd /s /q \"{ AppDomain.CurrentDomain.BaseDirectory}\\InstallPackage\\\"");
ps.StandardInput.WriteLine($"exit");
var result = ps.StandardOutput.ReadToEnd();
}
}).ContinueWith(f=> {
if (f.IsFaulted)
{
FileHelper.AppendWriteFile("ErrorInfo.txt", JsonConvert.SerializeObject(f.Exception.InnerExceptions.Select(ex => ex.Message + "|" + ex.StackTrace + ";")));
}
if (f.IsCompleted)
{
funcInOutHelper.RunDelegate("CreatePackageFinish", processWindow);
}
});
}
private void CreateUpgradePackage(string upgradeFolder)
{
var upgradeFilesCreater = new UpgradeFilesCreater();
if (File.Exists(pcClientFilePath))
{
upgradeFilesCreater.CreateUpgradePackage(pcClientFilePath, upgradeFolder);
}
}
private void AddPackageFile(List packages, JObject jsonRoot, PackagePlatform packagePlatform, PackageType packageType , string androidClientVersion, string filePath)
{
var descriptionEncrypt = string.Empty;
if (packageType == PackageType.Client)
{
var cnDescription = jsonRoot.GetJPropertyValueByPath($"$.{packagePlatform}.ChineseDescription");
var usDescription = jsonRoot.GetJPropertyValueByPath($"$.{packagePlatform}.EnglishDescription");
var portugalDescription = jsonRoot.GetJPropertyValueByPath($"$.{packagePlatform}.PortugalDescription");
var rumaniaDescription = jsonRoot.GetJPropertyValueByPath($"$.{packagePlatform}.RumaniaDescription");
descriptionEncrypt = "[{\"Language\":\"Chinese\",\"Content\":\"" +
DesBuilder.Encrypt(HttpUtility.UrlDecode(cnDescription)) +
"\"},{\"Language\":\"English\",\"Content\":\"" +
DesBuilder.Encrypt(HttpUtility.UrlDecode(usDescription)) +
"\"},{\"Language\":\"Portuguese\",\"Content\":\"" +
DesBuilder.Encrypt(HttpUtility.UrlDecode(portugalDescription)) +
"\"},{\"Language\":\"Romanian\",\"Content\":\"" +
DesBuilder.Encrypt(HttpUtility.UrlDecode(rumaniaDescription)) +
"\"}]";
}
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
byte[] fileBytes = new byte[fileStream.Length];
fileStream.Read(fileBytes, 0, (int)fileStream.Length);
packages.Add(new UpgradePackage(packagePlatform, packageType, new Version(androidClientVersion), descriptionEncrypt, fileBytes));
}
}
private void DtgShow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
PackageDescriptionWindow packageDescriptionWindow = new PackageDescriptionWindow();
packageDescriptionWindow.Show();
}
private void Decompression_Click(object sender, RoutedEventArgs e)
{
if (GetHardDiskFreeSpace() < 1024)
{
MessageBox.Show(TranslateHelper.Translate("Diskfull"));
return;
}
WaitProcessWindow processWindow = new WaitProcessWindow();
processWindow.Show();
Task.Run(() =>
{
funcInOutHelper.RunDelegate("HideWindow");
string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory);
foreach (var file in files)
{
var extensionName = System.IO.Path.GetExtension(file);
if (extensionName.ToLower() == ".zip" || extensionName.ToLower() == ".rar")
{
if (!Directory.Exists($"{AppDomain.CurrentDomain.BaseDirectory}\\InstallPackage"))
{
ZipHelper.Decompression(file, $"{AppDomain.CurrentDomain.BaseDirectory}\\InstallPackage");
}
break;
}
}
string[] installFiles = Directory.GetFiles($"{AppDomain.CurrentDomain.BaseDirectory}\\InstallPackage");
string[] installDirectories = Directory.GetDirectories($"{AppDomain.CurrentDomain.BaseDirectory}\\InstallPackage");
installEntitys.Clear();
foreach (var installFile in installFiles)
{
if (string.IsNullOrWhiteSpace(installFile)) { continue; }
if (installFile.IndexOf("InstallerMaker.exe", StringComparison.Ordinal) > -1) { continue; }
if (installFile.IndexOf("UpgradePackageBuilder.exe", StringComparison.Ordinal) > -1) { continue; }
if (installFile.IndexOf("AgentSetupFull_", StringComparison.Ordinal) > -1) { continue; }
if (installFile.IndexOf("StationSetup_", StringComparison.Ordinal) > -1) { continue; }
if (installFile.IndexOf("Vestris.ResourceLib.dll", StringComparison.Ordinal) > -1) { continue; }
if (installFile.IndexOf("ClientUpgrade_Release", StringComparison.Ordinal) > -1) { continue; }
if (installFile.IndexOf("PersonalFlyinsono_Release", StringComparison.Ordinal) > -1) { continue; }
if (installFile.IndexOf("vCarotidInstallFiles.zip", StringComparison.Ordinal) > -1) { continue; }
installEntitys.Add(new InstallEntity { IsDirectory = false, FullPath = installFile, Path = installFile.Substring(installFile.LastIndexOf('\\') + 1) });
}
foreach (var installDirectory in installDirectories)
{
if (string.IsNullOrWhiteSpace(installDirectory)) { continue; }
if (installDirectory.IndexOf("TestClient", StringComparison.Ordinal) > -1) { continue; }
if (installDirectory.IndexOf("Terminal", StringComparison.Ordinal) > -1) { continue; }
if (installDirectory.IndexOf("Station", StringComparison.Ordinal) > -1) { continue; }
if (installDirectory.IndexOf("License", StringComparison.Ordinal) > -1) { continue; }
if (installDirectory.IndexOf("Agent", StringComparison.Ordinal) > 1) { continue; }
if (installDirectory.IndexOf("ClickOnceSetup", StringComparison.Ordinal) > -1) { continue; }
if (installDirectory.IndexOf("IPLocationServerTool", StringComparison.Ordinal) > -1) { continue; }
if (installDirectory.IndexOf("EncryptTool", StringComparison.Ordinal) > -1) { continue; }
if (installDirectory.IndexOf("UploadClient", StringComparison.Ordinal) > -1) { continue; }
if (Regex.IsMatch(installDirectory,"PackingPress$")) { continue; }
if (installDirectory.IndexOf("TranslateTool", StringComparison.Ordinal) > -1) { continue; }
var entity = new InstallEntity { IsDirectory = true, FullPath = installDirectory, Path = installDirectory.Substring(installDirectory.LastIndexOf('\\') + 1) };
if (entity.Path == "Client")
{
if (Directory.Exists(installDirectory + "\\Mobile\\Android\\"))
{
var androidApk = Directory.GetFiles(installDirectory + "\\Mobile\\Android\\");
foreach (var apkpath in androidApk)
{
if (RegexHelper.IsMatch(apkpath, "Flyinsono_Android_([0-9.]*).apk"))
{
FileInfo androidApkInfo = new FileInfo(apkpath);
var androidFile = new InstallEntity { IsDirectory = false, FullPath = installDirectory + $"\\Mobile\\Android\\{androidApkInfo.Name}", Path = androidApkInfo.Name };
installEntitys.Add(androidFile);
break;
}
}
}
continue;
}
else if (entity.Path == "SONOPOST")
{
if (Directory.Exists(installDirectory))
{
var androidApk = Directory.GetFiles(installDirectory);
foreach (var apkpath in androidApk)
{
if (RegexHelper.IsMatch(apkpath, "Flyinsono_SonoPost_([0-9.]*).apk"))
{
FileInfo androidApkInfo = new FileInfo(apkpath);
var androidFile = new InstallEntity { IsDirectory = false, FullPath = installDirectory + $"\\{androidApkInfo.Name}", Path = androidApkInfo.Name };
installEntitys.Add(androidFile);
break;
}
}
}
continue;
}
installEntitys.Add(entity);
}
funcInOutHelper.RunDelegate("BingControl");
}).ContinueWith(f =>
{
if (f.IsCompleted)
{
funcInOutHelper.RunDelegate("DecompressionFinish", processWindow);
}
});
}
///
/// 获取指定驱动器的剩余空间总大小(单位为MB)
///
private long GetHardDiskFreeSpace()
{
var strHardDiskName = RegexHelper.MatchSub(AppDomain.CurrentDomain.BaseDirectory,"^([a-zA-Z]:\\\\).*");
long freeSpace = new long();
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == strHardDiskName)
{
freeSpace = drive.TotalFreeSpace / (1024 * 1024);
}
}
return freeSpace;
}
private object BingControl(object obj)
{
dtgShow.ItemsSource = null;
dtgShow.ItemsSource = installEntitys;
return obj;
}
private object HideWindow(object obj)
{
this.Hide();
return null;
}
private object DecompressionFinish(object processWindow)
{
this.Show();
((WaitProcessWindow)processWindow).Close();
MessageBox.Show($"{TranslateHelper.Translate("DecompressionFinish")}!");
return null;
}
private object CreatePackageFinish(object processWindow)
{
this.Show();
((WaitProcessWindow)processWindow).Close();
MessageBox.Show($"{TranslateHelper.Translate("PackagingFinish")}!");
return null;
}
private void DtgShow_DragEnter(object sender, DragEventArgs e)
{
}
private void SetDescription_Click(object sender, RoutedEventArgs e)
{
PackageDescriptionWindow packageDescriptionWindow = new PackageDescriptionWindow();
packageDescriptionWindow.Show();
}
private void UploadSetting_Click(object sender, RoutedEventArgs e)
{
using (var stream = OpenFileBrowserDialog("conf"))
{
if (stream != Stream.Null)
{
var serverSetting = AppDomain.CurrentDomain.BaseDirectory + "\\InstallPackage\\Server\\Settings\\Server\\Setting.conf";
if (!File.Exists(serverSetting))
{
MessageBox.Show($"{TranslateHelper.Translate("CannotFindSettingFile")}");
return;
}
using (FileStream fileStream = new FileStream(serverSetting, FileMode.Create))
{
byte[] readBytes = new byte[4096];
int readCount;
do
{
readCount = stream.Read(readBytes, 0, readBytes.Length);
fileStream.Write(readBytes, 0, readCount);
} while (readCount > 0);
MessageBox.Show($"{TranslateHelper.Translate("UploadSuccess")}");
}
}
}
}
///
/// 打开选择文件对话框
///
///
///
private static Stream OpenFileBrowserDialog(string defaultExt = "")
{
System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
if (!string.IsNullOrWhiteSpace(defaultExt))
{
fileDialog.DefaultExt = defaultExt;
fileDialog.Filter = $"*.{defaultExt}|*.{defaultExt}";
}
System.Windows.Forms.DialogResult result = fileDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return Stream.Null;
}
var fileStream = fileDialog.OpenFile();
return fileStream;
}
}
}