2025-03-28 09:49:56 +08:00

513 lines
21 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SuperMap.RealEstate.Launcher_WinForm;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace SuperMap.RealEstate.Launcher
{
public class LauncherApplication : BackgroundDownload
{
LauncherInfo _LauncherInfo;
string _TempGuidDirectory;
string _TempDirectory;
public LauncherApplication(LauncherInfo launcherInfo)
: base()
{
_TempDirectory = AppDomain.CurrentDomain.BaseDirectory + "Temp\\";
_TempGuidDirectory = _TempDirectory + Guid.NewGuid().ToString() + "\\";
_LauncherInfo = launcherInfo;
LoadingHelper.Init(_TempDirectory);
}
Dictionary<string, string> _DownLoadList = new Dictionary<string, string>();
#region CheckMainVersion
private bool CheckMainVersionEx(StreamReader _StreamReader)
{
string _LineString;
_LineString = _StreamReader.ReadLine();
if (_LineString == "[Error]")
{
StringBuilder _StringBuilder = new StringBuilder();
while (!_StreamReader.EndOfStream)
_StringBuilder.AppendLine(_StreamReader.ReadLine());
ShowError(_StringBuilder.ToString());
return false;
}
if (_LineString != "[Launcher]")
{
ShowError("服务器的Launcher.ini文件首行必须为\"[Launcher]\"");
return false;
}
string[] _KeyValue = _StreamReader.ReadLine().Split(':');
if (_KeyValue.Length != 2 && _KeyValue[0] != "Version")
{
ShowError("服务器的Launcher.ini文件[Launcher]节点异常!");
return false;
}
string _Version = GetFileVersion(Application.ExecutablePath);
if (_KeyValue[1] != GetFileVersion(Application.ExecutablePath))
{
//this.ProgressBarHide();
//#if DEBUG
//MessageBox.Show("主程序版本不一致,点击“确定”将自动升级。\r\n" +
// "本地版本:" + _Version + ";\r\n" +
// "服务版本:" + _KeyValue[1] + ";", "驿商科技", MessageBoxButtons.OK,
// MessageBoxIcon.Warning);
//#endif
this.ProgressBarText = "更新 版本 v" + _KeyValue[1] + "...";
string _UrlAddress = string.Format(LaunchHelper.Launcher_DownLoadUrl,
_LauncherInfo.HostUrl, LaunchHelper.Launcher_FileName);
string _FileName = _TempGuidDirectory + LaunchHelper.Launcher_UpdateFileName;
DownloadFileAsync(_UrlAddress, _FileName);
WaitingForDownloadFileAsyncCompleted();
string _UpdateFileName = AppDomain.CurrentDomain.BaseDirectory + LaunchHelper.Launcher_UpdateFileName;
if (File.Exists(_UpdateFileName))
File.Delete(_UpdateFileName);
File.Move(_FileName, _UpdateFileName);
LaunchHelper.Start(_UpdateFileName, _LauncherInfo.SourceArguments);
return false;
}
_KeyValue = _StreamReader.ReadLine().Split(':');
if (_KeyValue.Length != 2 && _KeyValue[0] != "DateTime")
{
ShowError("服务器的Launcher.ini文件[Launcher]节点未配置DateTime");
return false;
}
//MessageBox.Show(_KeyValue[1]);
return LocalTimeHelper.SetLocalTime(_KeyValue[1]);
}
#endregion
#region CheckCoreFile
private bool CheckCoreFileEx(StreamReader _StreamReader)
{
string _LineString, _FileName, _AppPath;
string[] _KeyValue;
_AppPath = AppDomain.CurrentDomain.BaseDirectory;
_LineString = _StreamReader.ReadLine();
while (!_StreamReader.EndOfStream)
{
if (_LineString.Equals("[Core]", StringComparison.CurrentCultureIgnoreCase))
break;
_LineString = _StreamReader.ReadLine();
}
//[Core]
if (!_LineString.Equals("[Core]", StringComparison.CurrentCultureIgnoreCase))
{
ShowError("无法获取节点[Core],请检查配置!");
return false;
}
//File:
_LineString = _StreamReader.ReadLine();
_DownLoadList.Clear();
while (!string.IsNullOrEmpty(_LineString))
{
_KeyValue = _LineString.Split(':');
_FileName = _AppPath + _KeyValue[1].Replace("Core\\", "Bin\\");
//Version:
_LineString = _StreamReader.ReadLine();
//_KeyValue = _LineString.Split(':');
if (!File.Exists(_FileName) || GetFileVersion(_FileName) != _LineString.Split(':')[1])
{
//Deploy:
_LineString = _StreamReader.ReadLine();
if (!_DownLoadList.ContainsKey(_KeyValue[1]))
{
_DownLoadList.Add(_KeyValue[1], _LineString.Split(':')[1]);
}
}
else
{
//Deploy:
_LineString = _StreamReader.ReadLine();
}
//File:
if (!_StreamReader.EndOfStream)
_LineString = _StreamReader.ReadLine();
else
_LineString = string.Empty;
}
bool _Result = DownLoadFileFromServer(_DownLoadList);
if (_Result)
{
string _CorePath = _TempGuidDirectory + "Core";
if (Directory.Exists(_CorePath))
{
string _BinPath = AppDomain.CurrentDomain.BaseDirectory + "Bin\\";
if (!Directory.Exists(_BinPath))
Directory.CreateDirectory(_BinPath);
string[] _CoreFiles = Directory.GetFiles(_CorePath);
int _LoadMessageHandle = 0;
foreach (string _File in _CoreFiles)
{
this.ProgressBarText = "更新 " + Path.GetFileName(_File) + " ...";
if (_File.EndsWith(LaunchHelper.Loading_FileName))
_LoadMessageHandle = LoadingHelper.SaveMessage();
if (File.Exists(_BinPath + Path.GetFileName(_File)))
File.Delete(_BinPath + Path.GetFileName(_File));
File.Copy(_File, _BinPath + Path.GetFileName(_File), true);
File.Delete(_File);
if (_LoadMessageHandle > 0)
LoadingHelper.LoadMessage(_LoadMessageHandle);
}
Directory.Delete(_TempGuidDirectory + "Core", true);
}
}
return _Result;
}
#endregion
bool DownLoadFileFromServer(Dictionary<string, string> FileDictionary)
{
Progress2Value = 0;
Progress1Value = 100;
SetProgress2Maximum(FileDictionary.Count);
if (FileDictionary.Count == 0)
{
return true;
}
foreach (KeyValuePair<string, string> _KeyValuePair in FileDictionary)
{
Progress1Value = 0;
string _UrlAddress = string.Format(LaunchHelper.Launcher_DownLoadUrl,
_LauncherInfo.HostUrl, _KeyValuePair.Value);
string _FileName = _TempGuidDirectory + _KeyValuePair.Key;
this.ProgressBarText = "下载 " + Path.GetFileName(_FileName) + " ...";
this.DownloadFileAsync(_UrlAddress, _FileName);
this.WaitingForDownloadFileAsyncCompleted();
Progress1Value = 100;
Progress2Value++;
}
return true;
}
#region CheckAppRuns
public bool CheckAppRuns(string FileName)
{
using (StreamReader _StreamReader = File.OpenText(FileName))
{
try
{
//检查主版本
if (!CheckMainVersionEx(_StreamReader))
return false;
//检查核心文件
if (!CheckCoreFileEx(_StreamReader))
return false;
//检查AppRuns
if (!CheckAppRunsEx(_StreamReader))
return false;
}
catch (Exception ex)
{
ShowError(ex.Message);
return false;
}
finally
{
_StreamReader.Close();
File.Delete(FileName);
}
}
return true;
}
private bool CheckAppRunsEx(StreamReader _StreamReader)
{
string _LineString, _Path, _AppPath;
string[] _KeyValue;
_AppPath = AppDomain.CurrentDomain.BaseDirectory;
_DownLoadList.Clear();
_LineString = _StreamReader.ReadLine();
while (!_StreamReader.EndOfStream)
{
if (_LineString.Equals("[AppRuns]", StringComparison.CurrentCultureIgnoreCase))
break;
_LineString = _StreamReader.ReadLine();
}
//[AppRuns]
if (!_LineString.Equals("[AppRuns]", StringComparison.CurrentCultureIgnoreCase))
{
ShowError("无法获取节点[AppRuns],请检查配置!");
return false;
}
//Name:
_LineString = _StreamReader.ReadLine();
_KeyValue = _LineString.Split(':');
if (_LauncherInfo.AppName != _KeyValue[1])
{
ShowError("错误的Name关键字,请检查环境配置!");
return false;
}
//Desc
_LineString = _StreamReader.ReadLine();
//Path
_LineString = _StreamReader.ReadLine();
_AppPath += _LauncherInfo.Type + "\\";
_LauncherInfo.PathName = _AppPath + _LineString.Split(':')[1];
_LineString = _StreamReader.ReadLine();
//File:
_KeyValue = _LineString.Split(':');
_Path = _AppPath + _KeyValue[1];
_LauncherInfo.FileName = _Path;
//Version
_LineString = _StreamReader.ReadLine();
if (!File.Exists(_Path))
{
//Deploy
_LineString = _StreamReader.ReadLine();
_KeyValue = _LineString.Split(':');
_LineString = _LauncherInfo.Type + "\\" + _LauncherInfo.AppName + ".7z";
if (!_DownLoadList.ContainsKey(_LineString))
{
_DownLoadList.Add(_LineString, _KeyValue[1]);
}
}
else
{
//检查版本
if (GetFileVersion(_Path) != _LineString.Split(':')[1])
{
//MessageBox.Show("版本不一致。");
//Deploy
_LineString = _StreamReader.ReadLine();
_KeyValue = _LineString.Split(':');
_LineString = _LauncherInfo.Type + "\\" + _LauncherInfo.AppName + ".7z";
if (!_DownLoadList.ContainsKey(_LineString))
{
_DownLoadList.Add(_LineString, _KeyValue[1]);
}
}
else
{
//Deploy
_LineString = _StreamReader.ReadLine();
}
//[ServerFileCollection]
_LineString = _StreamReader.ReadLine();
int _Number = 1;
while (!_StreamReader.EndOfStream)
{
_LineString = _StreamReader.ReadLine();
if (String.IsNullOrEmpty(_LineString))
continue;
_KeyValue = _LineString.Split(':');
_Path = _AppPath + _KeyValue[0];
_LineString = _LauncherInfo.Type + "\\ServerFile_" + _Number + ".7z";
if (File.Exists(_Path) && _KeyValue[1] == LaunchHelper.GetMD5HashFromFile(_Path))
continue;
if (!_DownLoadList.ContainsKey(_LineString))
{
_LauncherInfo.HashDictionary.Add(_KeyValue[0], _LineString);
_DownLoadList.Add(_LineString, _KeyValue[2]);
_Number++;
}
}
}
bool _Result = DownLoadFileFromServer(_DownLoadList);
if (_Result)
{
string _AppRunsPath = _TempGuidDirectory + _LauncherInfo.Type + "\\" + _LauncherInfo.AppName + ".7z";
if (File.Exists(_AppRunsPath))
{
this.ProgressBarText = "更新 " + _LauncherInfo.AppName;
if (Directory.Exists(_LauncherInfo.PathName))
Directory.Delete(_LauncherInfo.PathName, true);
Compress7z.UnZip(_AppRunsPath, _LauncherInfo.PathName);
//this.ProgressBarText = "更新文件...";
File.Delete(_AppRunsPath);
}
//检查配置
foreach (KeyValuePair<string, string> _KeyValuePair in _LauncherInfo.HashDictionary)
{
_AppRunsPath = _TempGuidDirectory + _KeyValuePair.Value;
this.ProgressBarText = "更新 " + Path.GetFileName(_KeyValuePair.Key);
Compress7z.UnZip(_AppRunsPath, Path.GetDirectoryName(_AppRunsPath));
File.Delete(_AppRunsPath);
_AppRunsPath = AppDomain.CurrentDomain.BaseDirectory + _LauncherInfo.Type + "\\" + _KeyValuePair.Key;
string _SourceFileName = _TempGuidDirectory + _LauncherInfo.Type + "\\" + Path.GetFileName(_AppRunsPath);
File.Copy(_SourceFileName, _AppRunsPath, true);
File.Delete(_SourceFileName);
}
}
return _Result;
}
#endregion
#region CheckInstall
public bool CheckInstall(string FileName)
{
using (StreamReader _StreamReader = File.OpenText(FileName))
{
try
{
//检查主版本
if (!CheckMainVersionEx(_StreamReader))
return false;
//检查核心文件
if (!CheckCoreFileEx(_StreamReader))
return false;
this.ProgressBarText = "加载 组件清单...";
//检查Install
if (!CheckInstallEx(_StreamReader))
return false;
//启动Install
}
catch (Exception ex)
{
ShowError(ex.Message);
return false;
}
finally
{
_StreamReader.Close();
File.Delete(FileName);
}
}
return true;
}
private bool CheckInstallEx(StreamReader _StreamReader)
{
string _LineString, _AppPath;
_AppPath = AppDomain.CurrentDomain.BaseDirectory;
string _Group, _Name;
bool _Include = false;
StringBuilder _StringBuilder, _StringBuilderAll;
_StringBuilderAll = new StringBuilder();
while (!_StreamReader.EndOfStream)
{
_LineString = _StreamReader.ReadLine();
if (!_LineString.Equals("[Install]", StringComparison.CurrentCultureIgnoreCase))
continue;
_Include = false;
_StringBuilder = new StringBuilder();
_LineString = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineString);
_Group = _LineString.Split(':')[1];
_LineString = _StreamReader.ReadLine();
_StringBuilder.AppendLine(_LineString);
_Name = _LineString.Split(':')[1];
if (String.IsNullOrEmpty(_LauncherInfo.GroupName) && String.IsNullOrEmpty(_LauncherInfo.AppName))
_Include = true;
if (!_Include)
{
if (_Group.Equals(_LauncherInfo.GroupName, StringComparison.CurrentCultureIgnoreCase))
_Include = true;
if (_Name.Equals(_LauncherInfo.AppName, StringComparison.CurrentCultureIgnoreCase))
_Include = true;
}
if (!_Include)
continue;
while (!_StreamReader.EndOfStream)
{
_LineString = _StreamReader.ReadLine();
if (String.IsNullOrEmpty(_LineString))
break;
if (_LineString.StartsWith("Deploy:"))
_StringBuilder.AppendLine("Deploy:" + _LauncherInfo.HostUrl + "/SoftWare/" + _LineString.Split(':')[1]);
else
_StringBuilder.AppendLine(_LineString);
}
_StringBuilderAll.AppendLine(_StringBuilder.ToString());
}
//if (_StringBuilderAll.Length > 0)
//{
_LineString = _TempGuidDirectory + "CurrInstall.ini";
using (StreamWriter _StreamWriter = File.CreateText(_LineString))
{
_StreamWriter.Write(_StringBuilderAll.ToString());
_StreamWriter.Close();
}
_LauncherInfo.Arguments = _LineString;
_LauncherInfo.FileName = _AppPath + "Bin\\" + LaunchHelper.Installer_FileName;
return true;
//}
//else
//{
// //File.Delete(_TempGuidDirectory + LaunchHelper.Installer_FileName);
// return true;
//}
}
#endregion
private void ShowError(string message)
{
LoadingHelper.ForceExit();
MessageBox.Show(message, "驿商科技", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private string GetFileVersion(string filePath)
{
if (!File.Exists(filePath))
return "不存在";
FileVersionInfo _FileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
if (String.IsNullOrEmpty(_FileVersionInfo.FileVersion))
return "无版本";
else
return _FileVersionInfo.FileVersion.Split(' ')[0];
}
public void AppRuns()
{
//ProgressBarText = "正在初始化...";
string _UrlAddress = string.Format(LaunchHelper.Launcher_RequestUrl,
_LauncherInfo.HostUrl, _LauncherInfo.Type, _LauncherInfo.AppName);
string _FileName = _TempGuidDirectory + LaunchHelper.Launcher_IniFileName;
ProgressBarText = "检查 版本...";
DownloadFileAsync(_UrlAddress, _FileName);
WaitingForDownloadFileAsyncCompleted();
if (CheckAppRuns(_FileName))
{
this.ProgressBarText = "启动 " + _LauncherInfo.AppName + "...";
_LauncherInfo.ProcessStart();
LoadingHelper.Close();
}
}
public void Installer()
{
//ProgressBarText = "正在初始化...";
string _UrlAddress = string.Format(LaunchHelper.Installer_RequestUrl,
_LauncherInfo.HostUrl, _LauncherInfo.Type);
string _FileName = _TempGuidDirectory + LaunchHelper.Installer_IniFileName;
ProgressBarText = "检查 版本...";
DownloadFileAsync(_UrlAddress, _FileName);
WaitingForDownloadFileAsyncCompleted();
Progress1Value = 100;
if (CheckInstall(_FileName))
{
_LauncherInfo.ProcessStart();
Thread.Sleep(5000);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
//#if DEBUG
//#else
if (Directory.Exists(_TempGuidDirectory))
Directory.Delete(_TempGuidDirectory, true);
//#endif
}
base.Dispose(disposing);
}
}
}