📄 liveupdate.cs
字号:
namespace Imps.Client.Pc
{
using Imps.Client.Base;
using Imps.Client.Core;
using Imps.Client.Resource;
using Imps.Client.Utils;
using Imps.Utils;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
internal class LiveUpdate
{
private bool _canLogin = true;
private double _checkSpan;
private static DateTime _checkTime;
private static Imps.Client.Pc.LiveUpdate _instance;
private static bool _isNotifiedUser = false;
private string _liveupdateUrl;
private IFrameworkWindow _mainWnd;
private WorkPathInfo _workPath = new WorkPathInfo();
private static object locker = new object();
private static object signal = new object();
public event EventHandler<UpdateInfo> OnCheckComplete;
private void AsyncCheck(bool forceCheck)
{
try
{
Thread thread = null;
if (forceCheck)
{
thread = new Thread(new ThreadStart(this.ForceCheck));
thread.Name = "Imps.Client.Pc CheckUpdate.Force";
thread.IsBackground = true;
thread.Priority = ThreadPriority.Highest;
}
else
{
thread = new Thread(new ThreadStart(this.AutoCheck));
thread.Name = "Imps.Client.Pc CheckUpdate.Auto";
thread.IsBackground = true;
thread.Priority = ThreadPriority.Lowest;
}
thread.Start();
}
catch
{
this._canLogin = true;
}
}
private void AutoCheck()
{
this.Check(false);
}
private bool CanLogin()
{
this.Check(true);
return this._canLogin;
}
private void Check(bool forceCheck)
{
if (IsDisabledLiveUpdate())
{
this._canLogin = true;
}
else
{
UpdateInfo e = null;
lock (locker)
{
DateTime time1 = _checkTime;
TimeSpan span = (TimeSpan) (DateTime.Now - _checkTime);
if ((forceCheck && (span.TotalHours < 1.0)) || (!forceCheck && (span.TotalMilliseconds < this._checkSpan)))
{
return;
}
try
{
this._liveupdateUrl = (Imps.Client.Core.FixedClientSetting.Instance.Debug_LiveupdateUrl ?? string.Empty).Trim();
ClientLogger.WriteGeneral("检查系统更新……");
if (string.IsNullOrEmpty(this._liveupdateUrl))
{
HttpWebRequest request = ConnectionFactory.CreateHttpWebRequest(Imps.Client.Core.FixedClientSetting.Instance.NavigatorServerUri, this._mainWnd.AccountManager.CurrentUser, true, false);
request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
using (Stream stream = request.GetRequestStream())
{
XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
writer.WriteStartElement("config");
writer.WriteStartElement("client");
writer.WriteAttributeString("type", "PC");
writer.WriteAttributeString("version", Imps.Client.Core.Configuration.FixedClientSetting.Version);
writer.WriteAttributeString("platform", EnvHelper.GetOsString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
}
using (HttpWebResponse response = ((HttpWebResponse) request.GetResponse()))
{
using (Stream stream2 = response.GetResponseStream())
{
XmlReader reader = XmlReader.Create(stream2);
if (reader.ReadToFollowing("pc-live-update"))
{
this._liveupdateUrl = reader["value"];
}
}
}
}
if (!this._liveupdateUrl.EndsWith("/"))
{
this._liveupdateUrl = this._liveupdateUrl + "/";
}
if (string.IsNullOrEmpty(this._liveupdateUrl))
{
return;
}
string url = this._liveupdateUrl + "UpdateInfo.ashx" + "?Version=" + HttpWebRequestHelper.UrlEncode(Imps.Client.Core.FixedClientSetting.Instance.Version) + "&Switch=" + Imps.Client.Core.FixedClientSetting.Instance.Debug_LiveUpdateSwitch;
Downloader downloader = new Downloader();
string fullPath = StaticTools.GetFullPath(this.WorkPath.SvrInfoFile);
this.CheckConfigFile(fullPath);
StaticTools.LogMessage(this.WorkPath.LogFile, "开始下载ServerVersion.xml");
downloader.Download(url, fullPath);
StaticTools.LogMessage(this.WorkPath.LogFile, "成功下载ServerVersion.xml");
if (System.IO.File.Exists(fullPath))
{
e = new UpdateInfo(fullPath);
}
else
{
e = null;
}
_checkTime = DateTime.Now;
if ((e == null) || (e.Type != UpdateType.force))
{
this._canLogin = true;
}
else
{
this._canLogin = false;
}
}
catch (Exception exception)
{
ClientLogger.WriteGeneral("检查更新失败", exception.ToString(), 20);
StaticTools.LogError(this.WorkPath.LogFile, exception);
this._liveupdateUrl = string.Empty;
e = null;
try
{
System.IO.File.Delete(StaticTools.GetFullPath(this.WorkPath.SvrInfoFile));
System.IO.File.Delete(StaticTools.GetFullPath(this.WorkPath.SvrInfoFile + ".downloading"));
}
catch
{
}
this._canLogin = true;
return;
}
}
if (((e != null) && (e.TargetVersion != HttpWebRequestHelper.UrlEncode(Imps.Client.Core.FixedClientSetting.Instance.Version))) && (this.OnCheckComplete != null))
{
FuncDispatcher.InvokeEventHandlerInUiThread<UpdateInfo>(this, this.OnCheckComplete, e);
}
else
{
StaticTools.LogMessage(this.WorkPath.LogFile, "不需要进行更新");
try
{
System.IO.File.Delete(StaticTools.GetFullPath(this.WorkPath.SvrInfoFile));
System.IO.File.Delete(StaticTools.GetFullPath(this.WorkPath.SvrInfoFile + ".target"));
System.IO.File.Delete(StaticTools.GetFullPath(this.WorkPath.SvrInfoFile + ".Downloading"));
System.IO.File.Delete(StaticTools.GetFullPath(this.WorkPath.ClientUpdateFile));
System.IO.File.Delete(StaticTools.GetFullPath(this.WorkPath.ClientUpdateFile + ".Downloading"));
}
catch
{
}
}
}
}
private void CheckConfigFile(string svrConfigName)
{
string directoryName = Path.GetDirectoryName(svrConfigName);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
if (!System.IO.File.Exists(svrConfigName))
{
using (XmlTextWriter writer = new XmlTextWriter(svrConfigName, Encoding.UTF8))
{
writer.WriteStartDocument();
writer.WriteStartElement("LiveUpdate");
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
}
public static void Create(IFrameworkWindow mainWnd)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -