123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- 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
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : BaseWindow
- {
- List<InstallEntity> installEntitys = new List<InstallEntity>();
- 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<InstallEntity>();
- var files = new List<InstallEntity>();
- var packages = new List<UpgradePackage>();
- List<InstallEntity> 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<UpgradePackage> packages, JObject jsonRoot, PackagePlatform packagePlatform, PackageType packageType , string androidClientVersion, string filePath)
- {
- var descriptionEncrypt = string.Empty;
- if (packageType == PackageType.Client)
- {
- var cnDescription = jsonRoot.GetJPropertyValueByPath<string>($"$.{packagePlatform}.ChineseDescription");
- var usDescription = jsonRoot.GetJPropertyValueByPath<string>($"$.{packagePlatform}.EnglishDescription");
- var portugalDescription = jsonRoot.GetJPropertyValueByPath<string>($"$.{packagePlatform}.PortugalDescription");
- var rumaniaDescription = jsonRoot.GetJPropertyValueByPath<string>($"$.{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);
- }
- });
- }
- /// <summary>
- /// 获取指定驱动器的剩余空间总大小(单位为MB)
- /// </summary>
- 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")}");
- }
- }
- }
- }
- /// <summary>
- /// 打开选择文件对话框
- /// </summary>
- /// <param name="defaultExt"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|