Skip to content

Commit

Permalink
Merge pull request #178 from DreamEnderKing/dev
Browse files Browse the repository at this point in the history
Prepare the installer for publication.
  • Loading branch information
ONLOX authored Apr 6, 2024
2 parents f81b3ad + 84528ee commit 9887042
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 119 deletions.
18 changes: 13 additions & 5 deletions installer/Data/ConfigFileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Maui;

namespace installer.Data
{
Expand Down Expand Up @@ -42,7 +43,7 @@ public record ConfigDataFile
public string MD5DataPath { get; set; } = ".\\hash.json";
public string InstallPath { get; set; } = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"THUAI7"
"THUAI7", "Data"
);
public string Token { get; set; } = string.Empty;
public string UserName { get; set; } = string.Empty;
Expand Down Expand Up @@ -202,17 +203,20 @@ public class ConfigData
{
public ConfigData(string? p = null, bool autoSave = true)
{
path = string.IsNullOrEmpty(p) ? Path.Combine(
var dataDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"THUAI7.json") : p;
"THUAI7");
if (!Directory.Exists(dataDir))
Directory.CreateDirectory(dataDir);
path = string.IsNullOrEmpty(p) ? Path.Combine(dataDir, "config.json") : p;
file = new ConfigDataFile();
com = new Command(file.Commands);
ReadFile();

if (autoSave)
OnMemoryChanged += (_, _) => SaveFile();
watcher = new FileSystemWatcher(Directory.GetParent(path)?.FullName ?? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
watcher.Filter = "THUAI*.json";
watcher = new FileSystemWatcher(Directory.GetParent(path)?.FullName ?? dataDir);
watcher.Filter = "*.json";
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
Expand Down Expand Up @@ -251,6 +255,10 @@ public void ReadFile()
com.OnMemoryChanged += (_, _) => OnMemoryChanged?.Invoke(this, new EventArgs());
}

public void ReadFileOnWindows()
{
}

public void SaveFile()
{
file.Commands = com.file;
Expand Down
2 changes: 1 addition & 1 deletion installer/Data/MD5FileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ public class MD5DataFile
public string BugGenerated { get; set; }
= "New bugs found in the new version.";
}
}
}
18 changes: 14 additions & 4 deletions installer/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Storage;
using installer.ViewModel;
using installer.Model;
using System.Text;
using System.Diagnostics;
using Microsoft.Maui.LifecycleEvents;

namespace installer
{
Expand All @@ -19,14 +23,20 @@ public static class MauiProgram
public static string SecretKey = "***";
public static MauiApp CreateMauiApp()
{

// read SecretID & SecretKey from filePath for debug
var filePath = @"D:\SecretKey.csv";
var filePath = Debugger.IsAttached ? "D:\\Secret.csv" : Path.Combine(AppContext.BaseDirectory, "Secret.csv");
var lines = File.ReadAllLines(filePath);
if (lines.Length > 0)
{
SecretID = lines[1];
SecretKey = lines[2];
var param = new CspParameters();
param.KeyContainerName = lines[0];
using (var rsa = new RSACryptoServiceProvider(param))
{
var b1 = Convert.FromBase64String(lines[1]);
var b2 = Convert.FromBase64String(lines[2]);
SecretID = Encoding.ASCII.GetString(rsa.Decrypt(b1, false));
SecretKey = Encoding.ASCII.GetString(rsa.Decrypt(b2, false));
}
}

var builder = MauiApp.CreateBuilder();
Expand Down
119 changes: 69 additions & 50 deletions installer/Model/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class UserInfo
public Logger LogError;
public enum UpdateStatus
{
success, unarchieving, downloading, hash_computing, error
success, unarchieving, downloading, hash_computing, exiting, error
}
public UpdateStatus Status = UpdateStatus.success; // 当前工作状态

Expand Down Expand Up @@ -80,6 +80,7 @@ public Downloader()
Data = new Local_Data();
Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.log"));
LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.error.log"));
Exceptions = new ExceptionStack(LogError, this);
if ((Log.LastRecordTime != DateTime.MinValue && DateTime.Now.Month != Log.LastRecordTime.Month)
|| (LogError.LastRecordTime != DateTime.MinValue && DateTime.Now.Month != LogError.LastRecordTime.Month))
{
Expand All @@ -91,11 +92,6 @@ public Downloader()
File.Delete(tarPath);
if (File.Exists(tarPath + ".gz"))
File.Delete(tarPath + ".gz");
Data.Log.Dispose();
Data.LogError.Dispose();
Data.Exceptions.logger.Dispose();
Log.Dispose();
LogError.Dispose();
TarFile.CreateFromDirectory(Data.LogPath, tarPath, false);
using (FileStream tar = File.Open(tarPath, FileMode.Open))
using (FileStream gz = File.Create(tarPath + ".gz"))
Expand All @@ -108,13 +104,7 @@ public Downloader()
{
File.Delete(log);
}
Data.Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "LocalData.log"));
Data.LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "LocalData.error.log"));
Data.Exceptions = new ExceptionStack(Data.LogError);
Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.log"));
LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.error.log"));
}
Exceptions = new ExceptionStack(LogError, this);
Route = Data.Config.InstallPath;
Cloud = new Tencent_Cos("1319625962", "ap-beijing", "bucket1",
LoggerProvider.FromFile(Path.Combine(Data.LogPath, "TencentCos.log")),
Expand Down Expand Up @@ -219,15 +209,6 @@ public void Install(string? path = null)
deleteTask(new DirectoryInfo(Data.Config.InstallPath));

Data.ResetInstallPath(Data.Config.InstallPath);
Cloud.Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "TencentCos.log"));
Cloud.LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "TencentCos.error.log"));
Cloud.Exceptions = new ExceptionStack(Cloud.LogError, Cloud);
Web.Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "EESAST.log"));
Web.LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "EESAST.error.log"));
Web.Exceptions = new ExceptionStack(Web.LogError, Web);
Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.log"));
LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.error.log"));
Exceptions = new ExceptionStack(LogError, this);
LoggerBinding();

string zp = Path.Combine(Data.Config.InstallPath, "THUAI7.tar.gz");
Expand All @@ -248,6 +229,14 @@ public void Install(string? path = null)
else
{
Status = UpdateStatus.success;
if (DeviceInfo.Platform == DevicePlatform.WinUI)
{
Process.Start(new ProcessStartInfo()
{
Arguments = Data.Config.InstallPath,
FileName = "explorer.exe"
});
}
}
}

Expand All @@ -261,21 +250,13 @@ public void ResetInstallPath(string newPath)
var installPath = Data.Config.InstallPath.EndsWith(Path.DirectorySeparatorChar) ? Data.Config.InstallPath[0..-1] : Data.Config.InstallPath;
if (newPath != installPath)
{
Log.Dispose(); LogError.Dispose(); Exceptions.logger.Dispose();
Cloud.Log.Dispose(); Cloud.LogError.Dispose(); Cloud.Exceptions.logger.Dispose();
Web.Log.Dispose(); Web.LogError.Dispose(); Web.Exceptions.logger.Dispose();
Data.ResetInstallPath(newPath);

Cloud.Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "TencentCos.log"));
Cloud.LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "TencentCos.error.log"));
Cloud.Exceptions = new ExceptionStack(Cloud.LogError, Cloud);
Web.Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "EESAST.log"));
Web.LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "EESAST.error.log"));
Web.Exceptions = new ExceptionStack(Web.LogError, Web);
Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.log"));
LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.error.log"));
Exceptions = new ExceptionStack(LogError, this);
LoggerBinding();
if (Cloud.Log is FileLogger) ((FileLogger)Cloud.Log).Path = Path.Combine(Data.LogPath, "TencentCos.log");
if (Cloud.LogError is FileLogger) ((FileLogger)Cloud.LogError).Path = Path.Combine(Data.LogPath, "TencentCos.error.log");
if (Web.LogError is FileLogger) ((FileLogger)Web.LogError).Path = Path.Combine(Data.LogPath, "EESAST.log");
if (Web.LogError is FileLogger) ((FileLogger)Web.LogError).Path = Path.Combine(Data.LogPath, "EESAST.error.log");
if (Log is FileLogger) ((FileLogger)Log).Path = Path.Combine(Data.LogPath, "Main.log");
if (LogError is FileLogger) ((FileLogger)LogError).Path = Path.Combine(Data.LogPath, "Main.error.log");
}
Update();
}
Expand Down Expand Up @@ -303,28 +284,48 @@ public int Update()
int result = 0;
if (CheckUpdate())
{
// 处理AI.cpp/AI.py合并问题
if (CurrentVersion < Data.FileHashData.Version)
// 如果Major版本号不一致,说明启动器本身需要更新,返回结果为16
if (CurrentVersion.Major < Data.FileHashData.Version.Major)
{
var dataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "THUAI7");
var local = Path.Combine(dataDir, "Cache", $"setup_thuai7installer_{Data.FileHashData.Version.Major}.exe");
Status = UpdateStatus.downloading;
var i = Cloud.DownloadFileAsync(local, $"Setup/{Data.FileHashData.Version.Major}.exe").Result;
if (i >= 0)
{
Status = UpdateStatus.exiting;
Process.Start(local);
return 16;
}
else
{
// 下载失败
return -1;
}
}
// 如果Minor版本号不一致,说明AI.cpp/AI.py有改动
// 返回结果为Flags,1: AI.cpp升级;2: AI.py升级
else if (CurrentVersion.Minor < Data.FileHashData.Version.Minor)
{
var c = CurrentVersion;
var v = Data.FileHashData.Version;
var c = $"{CurrentVersion.Major}_{CurrentVersion.Minor}";
var v = $"{Data.FileHashData.Version.Major}_{Data.FileHashData.Version.Minor}";
Status = UpdateStatus.downloading;
var p = Path.Combine(Data.Config.InstallPath, "Templates");
var tocpp = Cloud.DownloadFileAsync(Path.Combine(p, $"v{c}.cpp.t"),
var tocpp = Cloud.DownloadFileAsync(Path.Combine(p, $"{c}.cpp.t"),
$"./Template/v{c}.cpp.t");
var topy = Cloud.DownloadFileAsync(Path.Combine(p, $"v{c}.py.t"),
var topy = Cloud.DownloadFileAsync(Path.Combine(p, $"{c}.py.t"),
$"./Template/v{c}.py.t");
var tncpp = Cloud.DownloadFileAsync(Path.Combine(p, $"v{v}.cpp.t"),
var tncpp = Cloud.DownloadFileAsync(Path.Combine(p, $"{v}.cpp.t"),
$"./Template/v{v}.cpp.t");
var tnpy = Cloud.DownloadFileAsync(Path.Combine(p, $"v{v}.py.t"),
var tnpy = Cloud.DownloadFileAsync(Path.Combine(p, $"{v}.py.t"),
$"./Template/v{v}.py.t");
Task.WaitAll(tocpp, topy, tncpp, tnpy);
if (Directory.GetFiles(p).Count() == 4)
if (tocpp.Result >= 0 && topy.Result >= 0 && tncpp.Result >= 0 && tnpy.Result >= 0)
{
if (Data.LangEnabled[LanguageOption.cpp].Item1)
{
var so = FileService.ReadToEnd(Path.Combine(p, $"v{c}.cpp.t"));
var sn = FileService.ReadToEnd(Path.Combine(p, $"v{v}.cpp.t"));
var so = FileService.ReadToEnd(Path.Combine(p, $"{c}.cpp.t"));
var sn = FileService.ReadToEnd(Path.Combine(p, $"{v}.cpp.t"));
var sa = FileService.ReadToEnd(Data.LangEnabled[LanguageOption.cpp].Item2);
var s = FileService.MergeUserCode(sa, so, sn);
using (var f = new FileStream(Data.LangEnabled[LanguageOption.cpp].Item2 + ".temp", FileMode.Create))
Expand All @@ -334,11 +335,19 @@ public int Update()
w.Flush();
}
result |= 1;
if (DeviceInfo.Platform == DevicePlatform.WinUI)
{
Process.Start(new ProcessStartInfo()
{
Arguments = Directory.GetParent(Data.LangEnabled[LanguageOption.cpp].Item2)?.FullName,
FileName = "explorer.exe"
});
}
}
if (Data.LangEnabled[LanguageOption.python].Item1)
{
var so = FileService.ReadToEnd(Path.Combine(p, $"v{c}.py.t"));
var sn = FileService.ReadToEnd(Path.Combine(p, $"v{v}.py.t"));
var so = FileService.ReadToEnd(Path.Combine(p, $"{c}.py.t"));
var sn = FileService.ReadToEnd(Path.Combine(p, $"{v}.py.t"));
var sa = FileService.ReadToEnd(Data.LangEnabled[LanguageOption.python].Item2);
var s = FileService.MergeUserCode(sa, so, sn);
using (var f = new FileStream(Data.LangEnabled[LanguageOption.python].Item2 + ".temp", FileMode.Create))
Expand All @@ -348,11 +357,20 @@ public int Update()
w.Flush();
}
result |= 2;
if (DeviceInfo.Platform == DevicePlatform.WinUI)
{
Process.Start(new ProcessStartInfo()
{
Arguments = Directory.GetParent(Data.LangEnabled[LanguageOption.python].Item2)?.FullName,
FileName = "explorer.exe"
});
}
}
}
}
downloadFailed.Clear();

// 后两位留给其他更新,更新成功后返回值Flags增加0x8
Status = UpdateStatus.downloading;
Cloud.DownloadQueueAsync(Data.Config.InstallPath,
from item in Data.MD5Update where item.state != System.Data.DataRowState.Added select item.name,
Expand All @@ -372,17 +390,18 @@ public int Update()
if (Data.MD5Update.Count == 0)
{
Status = UpdateStatus.success;
result |= 8;
return result;
}
}
}
else
{
Status = UpdateStatus.success;
return result;
return 0;
}
Status = UpdateStatus.error;
return result;
return -1;
}

/// <summary>
Expand Down
Loading

0 comments on commit 9887042

Please sign in to comment.