Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
Coloryr committed Apr 14, 2023
1 parent 157f226 commit 518362e
Show file tree
Hide file tree
Showing 22 changed files with 284 additions and 520 deletions.
2 changes: 1 addition & 1 deletion src/ColorMC.Core/CoreMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ColorMC.Core;

public static class ColorMCCore
{
public const string Version = "A16.230413";
public const string Version = "A16.230414";

/// <summary>
/// 错误显示回调
Expand Down
44 changes: 5 additions & 39 deletions src/ColorMC.Gui/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using Avalonia;
using Avalonia.Media;
using ColorMC.Core;
using ColorMC.Core.Net.Downloader;
using ColorMC.Core.Objs;
using ColorMC.Core.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -12,9 +16,6 @@ namespace ColorMC.Gui;
public class ColorMCGui
{
public static string RunDir { get; private set; }
public static Action InitDone { get; private set; }
public static Func<Task<(bool?, string?)>> Check { get; private set; }
public static Action Update { get; private set; }

public const string Font = "resm:ColorMC.Launcher.Resources.MiSans-Normal.ttf?assembly=ColorMC.Launcher#MiSans";

Expand All @@ -30,11 +31,9 @@ public static void Main(string[] args)
{
SystemInfo.Init();

var path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

RunDir = SystemInfo.Os switch
{
OsType.Linux => $"{path}/ColorMC/",
OsType.Linux => $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/ColorMC/",
OsType.MacOS => "/Users/shared/ColorMC/",
_ => AppContext.BaseDirectory
};
Expand All @@ -53,39 +52,6 @@ public static void Main(string[] args)
}
}

public static void SetInit(Action ac)
{
InitDone = ac;
}

public static Task<bool> HaveUpdate(string data)
{
return App.HaveUpdate(data);
}

public static void CheckUpdateFail()
{
if (App.MainWindow == null)
return;

App.MainWindow.Window.Info1.Show(App.GetLanguage("Error13"));
}

public static void Quit()
{
App.Close();
}

public static void SetCheck(Func<Task<(bool?, string?)>> action)
{
Check = action;
}

public static void SetUpdate(Action action)
{
Update = action;
}

public static AppBuilder BuildAvaloniaApp()
{
#if DEBUG
Expand Down
6 changes: 3 additions & 3 deletions src/ColorMC.Gui/Resource/Language/zh-cn.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,6 @@
<String Key="SettingWindow.Tab3.Info1">正在进行更新检查</String>
<String Key="SettingWindow.Tab3.Info2">检测到新版本,是否要更新</String>
<String Key="SettingWindow.Tab3.Info3">已经是最新版了</String>
<String Key="SettingWindow.Tab3.Error1">无法进行更新</String>
<String Key="SettingWindow.Tab3.Error2">无法进行检查</String>
<String Key="SettingWindow.Tab3.Error3">检查更新失败</String>

<String Key="SettingWindow.Tab4.Text1">Jvm参数设置</String>
<String Key="SettingWindow.Tab4.Text2">垃圾回收器设置</String>
Expand Down Expand Up @@ -781,6 +778,7 @@
<String Key="Gui.Info17">导入光影包</String>
<String Key="Gui.Info18">导入结构文件</String>
<String Key="Gui.Info19">是否下载前置Mod</String>
<String Key="Gui.Info20">没有更新消息</String>

<String Key="Warn1">配置为空,旧版配置文件会被覆盖</String>
<String Key="Error1">背景图片加载失败</String>
Expand All @@ -803,6 +801,8 @@
<String Key="Gui.Error18">更新服务器文件失败</String>
<String Key="Gui.Error19">更新服务器文件错误</String>
<String Key="Gui.Error20">世界备份错误</String>
<String Key="Gui.Error21">启动器检查更新错误</String>
<String Key="Gui.Error22">启动器下载更新失败</String>

<String Key="ToolTip.Text1">最小化</String>
<String Key="ToolTip.Text2">最大化</String>
Expand Down
15 changes: 9 additions & 6 deletions src/ColorMC.Gui/UI/Controls/Main/MainControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void DragEnter(object? sender, DragEventArgs e)
Grid2.IsVisible = true;
Label1.Content = App.GetLanguage("Gui.Info6");
}
else if (e.Data.Contains(DataFormats.FileNames))
else if (e.Data.Contains(DataFormats.Files))
{
Grid2.IsVisible = true;
Label1.Content = App.GetLanguage("Gui.Info7");
Expand All @@ -105,13 +105,15 @@ private void Drop(object? sender, DragEventArgs e)
App.ShowUser(str);
}
}
else if (e.Data.Contains(DataFormats.FileNames))
else if (e.Data.Contains(DataFormats.Files))
{
var files = e.Data.GetFileNames();
var files = e.Data.GetFiles();
if (files == null || files.Count() > 1)
return;

var item = files.First();
var item = files.First().GetPath();
if (item == null)
return;
if (item.EndsWith(".zip") || item.EndsWith(".mrpack"))
{
App.ShowAddGame(item);
Expand Down Expand Up @@ -190,7 +192,7 @@ public void GameClose(GameSettingObj obj)
{
Dispatcher.UIThread.Post(() =>
{
if (Obj?.Obj.UUID == obj.UUID)
if (Obj?.Obj?.UUID == obj.UUID)
{
ItemInfo.SetGame(obj);
}
Expand Down Expand Up @@ -243,9 +245,10 @@ public void Opened()
#if !DEBUG
if (ConfigBinding.GetAllConfig().Item1?.Http?.CheckUpdate == true)
{
ColorMCGui.InitDone();
UpdateChecker.Check();
}
#endif
//UpdateChecker.Check();

MotdLoad();

Expand Down
20 changes: 4 additions & 16 deletions src/ColorMC.Gui/UI/Controls/Setting/Tab3Control.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,26 @@ private void Button4_Click(object? sender, RoutedEventArgs e)

private void Button3_Click(object? sender, RoutedEventArgs e)
{
var window = App.FindRoot(VisualRoot);
if (ColorMCGui.Update == null)
{
window.Info.Show(App.GetLanguage("SettingWindow.Tab3.Error1"));
return;
}

ColorMCGui.Update();
UpdateChecker.StartUpdate();
}

private async void Button1_Click(object? sender, RoutedEventArgs e)
{
var window = App.FindRoot(VisualRoot);
if (ColorMCGui.Update == null || ColorMCGui.Check == null)
{
window.Info.Show(App.GetLanguage("SettingWindow.Tab3.Error2"));
return;
}
await window.Info1.Show(App.GetLanguage("SettingWindow.Tab3.Info1"));
var res = await ColorMCGui.Check();
var res = await UpdateChecker.CheckOne();
window.Info1.Close();
if (res.Item1 == null)
{
window.Info.Show(App.GetLanguage("SettingWindow.Tab3.Error3"));
window.Info.Show(App.GetLanguage("Gui.Error21"));
return;
}
else if (res.Item1 == true)
{
var res1 = await window.Info6.ShowWait(App.GetLanguage("SettingWindow.Tab3.Info2"), res.Item2!);
if (!res1)
{
ColorMCGui.Update();
UpdateChecker.StartUpdate();
}
}
else
Expand Down
1 change: 1 addition & 0 deletions src/ColorMC.Gui/UIBinding/BaseBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static void Init()

try
{
UpdateChecker.Init();
Media.Init();
}
catch (Exception e)
Expand Down
159 changes: 159 additions & 0 deletions src/ColorMC.Gui/Updater/UpdateChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using Avalonia.Threading;
using ColorMC.Core;
using ColorMC.Core.Net;
using ColorMC.Core.Net.Downloader;
using ColorMC.Core.Objs;
using ColorMC.Core.Utils;
using ColorMC.Gui.Objs;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ColorMC.Gui;

public static class UpdateChecker
{
//private const string url = "http://localhost/colormc/A16/";
private const string url = "https://colormc.coloryr.com/colormc/A16/";

public static readonly string[] WebSha1s = new string[4] { "", "", "", "" };
private static readonly string[] Sha1s = new string[4] { "", "", "", "" };
private static readonly string[] Local = new string[4] { "", "", "", "" };
public static void Init()
{
Local[0] = Path.GetFullPath($"{ColorMCGui.RunDir}dll/ColorMC.Core.dll");
Local[1] = Path.GetFullPath($"{ColorMCGui.RunDir}dll/ColorMC.Core.pdb");
Local[2] = Path.GetFullPath($"{ColorMCGui.RunDir}dll/ColorMC.Gui.dll");
Local[3] = Path.GetFullPath($"{ColorMCGui.RunDir}dll/ColorMC.Gui.pdb");

for (int a = 0; a < 4; a++)
{
if (File.Exists(Local[a]))
{
using var file = File.OpenRead(Local[a]);
Sha1s[a] = Funtcions.GenSha1(file);
}
}
}

public static async void Check()
{
try
{
var data = await CheckOne();
if (data.Item1 == true)
{
var res = await App.HaveUpdate(data.Item2!);
if (!res)
{
StartUpdate();
}
}
}
catch (Exception e)
{
App.ShowError(App.GetLanguage("Gui.Error21"), e);
}

}

public static async void StartUpdate()
{
var list = new List<DownloadItemObj>()
{
new DownloadItemObj()
{
Name = "ColorMC.Core.dll",
SHA1 = WebSha1s[0],
Url = $"{url}ColorMC.Core.dll",
Local = $"{ColorMCGui.RunDir}dll/ColorMC.Core.dll",
Overwrite = true
},
new DownloadItemObj()
{
Name = "ColorMC.Core.pdb",
SHA1 = WebSha1s[1],
Url = $"{url}ColorMC.Core.pdb",
Local = $"{ColorMCGui.RunDir}dll/ColorMC.Core.pdb",
Overwrite = true
},
new DownloadItemObj()
{
Name = "ColorMC.Gui.dll",
SHA1 = WebSha1s[2],
Url = $"{url}ColorMC.Gui.dll",
Local = $"{ColorMCGui.RunDir}dll/ColorMC.Gui.dll",
Overwrite = true
},
new DownloadItemObj()
{
Name = "ColorMC.Gui.pdb",
SHA1 = WebSha1s[3],
Url = $"{url}ColorMC.Gui.pdb",
Local = $"{ColorMCGui.RunDir}dll/ColorMC.Gui.pdb",
Overwrite = true
}
};

var res = await DownloadManager.Start(list);
if (res)
{
Process.Start($"{(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
"ColorMC.Launcher.exe" : "ColorMC.Launcher")}");
App.Close();
}
else
{
App.ShowError(App.GetLanguage("Gui.Error22"), "");
}
}

public static async Task<(bool?, string?)> CheckOne()
{
try
{
var data = await BaseClient.DownloadClient.GetStringAsync(url + "sha1.json");
var obj = JObject.Parse(data);
if (obj == null)
{
App.ShowError(App.GetLanguage("Gui.Error21"), "Json Error");
return (false, null);
}

WebSha1s[0] = obj["core.dll"]!.ToString();
WebSha1s[1] = obj["core.pdb"]!.ToString();
WebSha1s[2] = obj["gui.dll"]!.ToString();
WebSha1s[3] = obj["gui.pdb"]!.ToString();

Logs.Info($"ColorMC.Core.dll:{Sha1s[0]} Web:{WebSha1s[0]}");
Logs.Info($"ColorMC.Core.pdb:{Sha1s[1]} Web:{WebSha1s[1]}");
Logs.Info($"ColorMC.Gui.dll:{Sha1s[2]} Web:{WebSha1s[2]}");
Logs.Info($"ColorMC.Gui.pdb:{Sha1s[3]} Web:{WebSha1s[3]}");

for (int a = 0; a < 4; a++)
{
if (WebSha1s[a] != Sha1s[a])
{
obj.TryGetValue("text", out var data1);
return (true, data1?.ToString() ?? App.GetLanguage("Gui.Info20"));
}
}

return (false, null);
}
catch (Exception e)
{
App.ShowError(App.GetLanguage("Gui.Error21"), e);
}

return (null, null);
}
}
8 changes: 0 additions & 8 deletions src/ColorMC.Launcher/App.axaml

This file was deleted.

Loading

0 comments on commit 518362e

Please sign in to comment.