ZeroLauncher/Manager/UpDateManager.cs
2024-03-09 18:29:16 +08:00

119 lines
3.5 KiB
C#

using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using Zerolauncher.Defender;
namespace Zerolauncher.Manager
{
class DownloadTask
{
public static bool state = false;
public static string url = "";
}
class UpDateManager
{
public static bool isCheeking = false;
public static void DoCheckUpdate(bool checkMain=true, bool needEngine = false)
{
if (isCheeking) return;
isCheeking=true;
bool needMian= false;
if(checkMain)
{
string filePath = Process.GetCurrentProcess().MainModule.FileName;
string? now_bit;
using (SHA256 sha256 = SHA256.Create())
{
using (FileStream fileStream = File.OpenRead(filePath))
{
byte[] hashBytes = sha256.ComputeHash(fileStream);
now_bit = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
}
}
needMian = now_bit == CacheSha.GetM() && now_bit != null;
}
if(!needEngine)
{
string? now_bit;
using (SHA256 sha256 = SHA256.Create())
{
using (FileStream fileStream = File.OpenRead(@"ZeroEngine.exe"))
{
byte[] hashBytes = sha256.ComputeHash(fileStream);
now_bit = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
}
}
needEngine = now_bit == CacheSha.GetE() && now_bit != null;
}
if(needEngine && needMian) { isCheeking = false; return; }
DownloadTask.state = true;
if(needEngine == needMian) DownloadTask.url = UpDateData.full_packet_url;
else DownloadTask.url = UpDateData.mini_packet_url;
isCheeking = false;
}
public static void DownLoad(string Url, string FileName, bool has)
{
bool Value = false;
WebResponse response = null;
Stream stream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
response = request.GetResponse();
stream = response.GetResponseStream();
if (!response.ContentType.ToLower().StartsWith("text/"))
{
Value = SaveBinaryFile(response, FileName);
}
}
catch (Exception err)
{
string aa = err.ToString();
}
}
private static bool SaveBinaryFile(WebResponse response, string FileName)
{
bool Value = true;
byte[] buffer = new byte[1024];
try
{
if (File.Exists(FileName))
File.Delete(FileName);
Stream outStream = File.Create(FileName);
Stream inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0, buffer.Length);
if (l > 0)
outStream.Write(buffer, 0, l);
}
while (l > 0);
outStream.Close();
inStream.Close();
}
catch
{
Value = false;
}
return Value;
}
}
}