Skip to content

Commit

Permalink
Merge pull request #165 from DreamEnderKing/dev
Browse files Browse the repository at this point in the history
feat: 🎨 Add the funtion to update user's code
  • Loading branch information
ONLOX authored Mar 30, 2024
2 parents c13e2ca + 057e06c commit 2964748
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 55 deletions.
2 changes: 1 addition & 1 deletion installer/Data/ConfigFileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ public LanguageOption Language
}
}


public int LaunchID
{
get => file.LaunchID;
Expand Down Expand Up @@ -363,6 +362,7 @@ public Command Commands
com = value;
if (temp != value)
OnMemoryChanged?.Invoke(this, new EventArgs());
com.OnMemoryChanged += (_, _) => OnMemoryChanged?.Invoke(this, new EventArgs());
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions installer/Data/MD5FileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@

namespace installer.Data
{
public class MD5FileData
public class MD5DataFile
{
// TO DO: 将Local_Data中与MD5FileData相关部分移动到本文件下并建立引用
public Dictionary<string, string> Data { get; set; } = new Dictionary<string, string>();
public Version Version = new Version(1, 0, 0, 0);
public string Description { get; set; }
= "The Description of the current version.";
public string BugFixed { get; set; }
= "Bugs had been fixed.";
public string BugGenerated { get; set; }
= "New bugs found in the new version.";
}
}
69 changes: 61 additions & 8 deletions installer/Model/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO.Compression;
using System.Formats.Tar;
using installer.Data;
using installer.Services;

namespace installer.Model
{
Expand All @@ -31,6 +32,7 @@ public class UserInfo
public string StartName = "maintest.exe"; // 启动的程序名
public Local_Data Data; // 本地文件管理器
public Tencent_Cos Cloud; // THUAI7 Cos桶
public Version CurrentVersion { get => Data.CurrentVersion; set => Data.CurrentVersion = value; }

public HttpClient Client = new HttpClient();
public EEsast Web; // EEsast服务器
Expand All @@ -50,10 +52,10 @@ public enum UpdateStatus
public string UserEmail { get => Web.Email; }
public Data.Command Commands
{
get => Data.FileHashData.Command;
get => Data.Config.Commands;
set
{
Data.FileHashData.Command = value;
Data.Config.Commands = value;
}
}
public enum UsingOS { Win, Linux, OSX };
Expand Down Expand Up @@ -122,7 +124,6 @@ public Downloader()
Web.Token_Changed += SaveToken;
LoggerBinding();

string? temp;
if (Data.Config.Remembered)
{
Username = Data.Config.UserName;
Expand Down Expand Up @@ -291,16 +292,67 @@ public bool CheckUpdate()
Status = UpdateStatus.hash_computing;
Data.ScanDir();
Status = UpdateStatus.success;
return Data.MD5Update.Count != 0;
return Data.MD5Update.Count != 0 || CurrentVersion < Data.FileHashData.Version;
}

/// <summary>
/// 更新文件
/// </summary>
public void Update()
public int Update()
{
int result = 0;
if (CheckUpdate())
{
// 处理AI.cpp/AI.py合并问题
if (CurrentVersion < Data.FileHashData.Version)
{
var c = CurrentVersion;
var v = Data.FileHashData.Version;
Status = UpdateStatus.downloading;
var p = Path.Combine(Data.Config.InstallPath, "Templates");
var tocpp = Cloud.DownloadFileAsync(Path.Combine(p, $"v{c}.cpp.t"),
$"./Template/v{c}.cpp.t");
var topy = Cloud.DownloadFileAsync(Path.Combine(p, $"v{c}.py.t"),
$"./Template/v{c}.py.t");
var tncpp = Cloud.DownloadFileAsync(Path.Combine(p, $"v{v}.cpp.t"),
$"./Template/v{v}.cpp.t");
var tnpy = Cloud.DownloadFileAsync(Path.Combine(p, $"v{v}.py.t"),
$"./Template/v{v}.py.t");
Task.WaitAll(tocpp, topy, tncpp, tnpy);
if (Directory.GetFiles(p).Count() == 4)
{
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 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))
using (var w = new StreamWriter(f))
{
w.Write(s);
w.Flush();
}
result |= 1;
}
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 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))
using (var w = new StreamWriter(f))
{
w.Write(s);
w.Flush();
}
result |= 2;
}
}
}
downloadFailed.Clear();

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 @@ -320,16 +372,17 @@ public void Update()
if (Data.MD5Update.Count == 0)
{
Status = UpdateStatus.success;
return;
return result;
}
}
}
else
{
Status = UpdateStatus.success;
return;
return result;
}
Status = UpdateStatus.error;
return result;
}

/// <summary>
Expand Down Expand Up @@ -388,7 +441,7 @@ public void UploadFiles(int player_id)
lang = "unknown";
break;
}
Web.UploadFiles(Client, Path.Combine(Data.Config.InstallPath, Data.UserCodePath), lang, $"player_{player_id}").Wait();
Web.UploadFiles(Client, Data.LangEnabled[Commands.Language].Item2, lang, $"player_{player_id}").Wait();
}
#endregion
}
Expand Down
66 changes: 22 additions & 44 deletions installer/Model/Local_Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,14 @@

namespace installer.Model
{
public record VersionID
{
public VersionID(int major, int minor, int build, int revision)
{
(Major, Minor, Build, Revision) = (major, minor, build, revision);
}
public int Major, Minor, Build, Revision;
public static bool operator >(VersionID left, VersionID right)
{
return (left.Major > right.Major) |
(left.Major == right.Major && left.Minor > right.Minor) |
(left.Major == right.Major && left.Minor == right.Minor && left.Build > right.Build) |
(left.Major == right.Major && left.Minor == right.Minor && left.Build == right.Build && left.Revision > right.Revision);
}
public static bool operator <(VersionID left, VersionID right)
{
return (left.Major < right.Major) |
(left.Major == right.Major && left.Minor < right.Minor) |
(left.Major == right.Major && left.Minor == right.Minor && left.Build < right.Build) |
(left.Major == right.Major && left.Minor == right.Minor && left.Build == right.Build && left.Revision < right.Revision);
}
}
public class MD5DataFile
{
public Dictionary<string, string> Data { get; set; } = new Dictionary<string, string>();
public Command Command { get; set; } = new Command();
public VersionID Version = new VersionID(1, 0, 0, 0);
public string Description { get; set; }
= "The Description of the current version.";
public string BugFixed { get; set; }
= "Bugs had been fixed.";
public string BugGenerated { get; set; }
= "New bugs found in the new version.";
}

public class Local_Data
{
public string ConfigPath; // 标记路径记录文件THUAI7.json的路径
public string MD5DataPath; // 标记MD5本地缓存文件的路径
public string UserCodePostfix = "cpp"; // 用户文件后缀(.cpp/.py)
public MD5DataFile FileHashData = new MD5DataFile();
public ConfigData Config;
public string UserCodePath
{
get => Path.Combine(Config.InstallPath,
$"???{Path.DirectorySeparatorChar}AI{UserCodePostfix}");
}
public Version CurrentVersion;
public Dictionary<LanguageOption, (bool, string)> LangEnabled;
public string LogPath { get => Path.Combine(Config.InstallPath, "Logs"); }
public ConcurrentDictionary<string, string> MD5Data
{
Expand Down Expand Up @@ -96,12 +57,14 @@ public Local_Data()
if (!File.Exists(MD5DataPath))
SaveMD5Data();
ReadMD5Data();
CurrentVersion = FileHashData.Version;
MD5Update.Clear();
}
else
{
MD5DataPath = Path.Combine(Config.InstallPath, $".{Path.DirectorySeparatorChar}hash.json");
Config.MD5DataPath = $".{Path.DirectorySeparatorChar}hash.json";
CurrentVersion = FileHashData.Version;
SaveMD5Data();
}
RememberMe = (Config.Remembered && Convert.ToBoolean(Config.Remembered));
Expand All @@ -114,6 +77,7 @@ public Local_Data()
Config.MD5DataPath = Config.InstallPath;
MD5DataPath = Path.Combine(Config.InstallPath, $".{Path.DirectorySeparatorChar}hash.json");
Config.MD5DataPath = $".{Path.DirectorySeparatorChar}hash.json";
CurrentVersion = FileHashData.Version;
SaveMD5Data();
}
}
Expand All @@ -123,6 +87,7 @@ public Local_Data()
var dir = Directory.CreateDirectory(Path.Combine(AppContext.BaseDirectory, "THUAI7"));
Config.InstallPath = dir.FullName;
MD5DataPath = Path.Combine(Config.InstallPath, $".{Path.DirectorySeparatorChar}hash.json");
CurrentVersion = FileHashData.Version;
Config.MD5DataPath = $".{Path.DirectorySeparatorChar}hash.json";
SaveMD5Data();
}
Expand All @@ -139,6 +104,11 @@ public Local_Data()
Log = LoggerProvider.FromFile(Path.Combine(LogPath, "LocalData.log"));
LogError = LoggerProvider.FromFile(Path.Combine(LogPath, "LocalData.error.log"));
Exceptions = new ExceptionStack(LogError, this);
LangEnabled = new Dictionary<LanguageOption, (bool, string)>();
foreach (var a in typeof(LanguageOption).GetEnumValues())
{
LangEnabled.Add((LanguageOption)a, (false, string.Empty));
}
}

~Local_Data()
Expand Down Expand Up @@ -219,7 +189,7 @@ public void ReadMD5Data()
}
r.Close(); r.Dispose();
}
catch (JsonException e)
catch (JsonException)
{
// Json反序列化失败,考虑重新创建MD5数据库
r.Close(); r.Dispose();
Expand Down Expand Up @@ -251,6 +221,7 @@ public void SaveMD5Data()
{
try
{
FileHashData.Version = CurrentVersion;
using (FileStream fs = new FileStream(MD5DataPath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter sw = new StreamWriter(fs))
{
Expand Down Expand Up @@ -289,7 +260,7 @@ public void ScanDir()
{
string cur = stack.Pop();
files.AddRange(from f in Directory.GetFiles(cur)
where !IsUserFile(f)
where !IsUserFile(f, LangEnabled)
select f);
foreach (var d in Directory.GetDirectories(cur))
stack.Push(d);
Expand Down Expand Up @@ -326,7 +297,6 @@ public void ScanDir()
SaveMD5Data();
}


public static bool IsUserFile(string filename)
{
if (filename.Contains("git") || filename.Contains("bin") || filename.Contains("obj"))
Expand All @@ -344,6 +314,14 @@ public static bool IsUserFile(string filename)
return false;
}

public static bool IsUserFile(string filename, Dictionary<LanguageOption, (bool, string)> dict)
{
if (filename.Contains("AI.cpp"))
dict[LanguageOption.cpp] = (true, filename);
if (filename.Contains("AI.py"))
dict[LanguageOption.python] = (true, filename);
return IsUserFile(filename);
}
public static int CountFile(string folder, string? root = null)
{
int result = (from f in Directory.EnumerateFiles(folder)
Expand Down

0 comments on commit 2964748

Please sign in to comment.