Skip to content

Commit

Permalink
up 拆分核心
Browse files Browse the repository at this point in the history
  • Loading branch information
Coloryr committed Jun 18, 2024
1 parent 296f7e5 commit 5c53db2
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 70 deletions.
16 changes: 8 additions & 8 deletions src/ColorMC.Core/Helpers/AddGameHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static class AddGameHelper
/// <param name="request"></param>
/// <param name="overwirte"></param>
/// <returns></returns>
public static async Task<AddGameRes> AddGame(AddGameArg arg)
public static async Task<GameRes> AddGame(AddGameArg arg)
{
if (string.IsNullOrWhiteSpace(arg.Local))
{
Expand Down Expand Up @@ -103,15 +103,15 @@ public static async Task<AddGameRes> AddGame(AddGameArg arg)
});
if (game == null)
{
return new AddGameRes
return new GameRes
{
State = false
};
}

await game.CopyFile(arg.Local, arg.Unselect);

return new AddGameRes
return new GameRes
{
State = false,
Game = game
Expand All @@ -125,7 +125,7 @@ public static async Task<AddGameRes> AddGame(AddGameArg arg)
/// <param name="type">类型</param>
/// <param name="type">名字</param>
/// <param name="type">群组</param>
public static async Task<AddGameRes> InstallZip(InstallZipArg arg)
public static async Task<GameRes> InstallZip(InstallZipArg arg)
{
GameSettingObj? game = null;
bool import = false;
Expand Down Expand Up @@ -445,7 +445,7 @@ public static async Task<AddGameRes> InstallZip(InstallZipArg arg)
await game.Remove(arg.Request);
}
arg.Update2?.Invoke(CoreRunState.End);
return new AddGameRes { State = import, Game = game };
return new GameRes { State = import, Game = game };
}

/// <summary>
Expand All @@ -455,7 +455,7 @@ public static async Task<AddGameRes> InstallZip(InstallZipArg arg)
/// <param name="name">名字</param>
/// <param name="group">群组</param>
/// <returns>结果</returns>
public static async Task<AddGameRes> InstallModrinth(DownloadModrinthArg arg)
public static async Task<GameRes> InstallModrinth(DownloadModrinthArg arg)
{
var file = arg.Data.files.FirstOrDefault(a => a.primary) ?? arg.Data.files[0];
var item = new DownloadItemObj()
Expand Down Expand Up @@ -506,7 +506,7 @@ public static async Task<AddGameRes> InstallModrinth(DownloadModrinthArg arg)
/// <param name="name">名字</param>
/// <param name="group">群组</param>
/// <returns>结果</returns>
public static async Task<AddGameRes> InstallCurseForge(DownloadCurseForgeArg arg)
public static async Task<GameRes> InstallCurseForge(DownloadCurseForgeArg arg)
{
arg.Data.FixDownloadUrl();

Expand All @@ -519,7 +519,7 @@ public static async Task<AddGameRes> InstallCurseForge(DownloadCurseForgeArg arg

var res1 = await DownloadManager.StartAsync([item]);
if (!res1)
return new AddGameRes { State = false };
return new GameRes { State = false };

var res2 = await InstallZip(new InstallZipArg
{
Expand Down
12 changes: 6 additions & 6 deletions src/ColorMC.Core/Helpers/ModPackHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public static async Task<bool> UpdateCurseForgeModPackAsync(UpdateModPackArg arg
/// <param name="group">实例组</param>
/// <returns>Res安装结果
/// Game游戏实例</returns>
public static async Task<AddGameRes> InstallCurseForgeModPackAsync(InstallModPackZipArg arg)
public static async Task<GameRes> InstallCurseForgeModPackAsync(InstallModPackZipArg arg)
{
arg.Update2?.Invoke(CoreRunState.Read);
using ZipFile zFile = new(PathHelper.OpenRead(arg.Zip));
Expand Down Expand Up @@ -472,7 +472,7 @@ public static async Task<AddGameRes> InstallCurseForgeModPackAsync(InstallModPac
});
if (!list.State)
{
return new AddGameRes { Game = game };
return new GameRes { Game = game };
}

game.SaveModInfo();
Expand All @@ -481,7 +481,7 @@ public static async Task<AddGameRes> InstallCurseForgeModPackAsync(InstallModPac

await DownloadManager.StartAsync([.. list.List]);

return new AddGameRes { State = true, Game = game };
return new GameRes { State = true, Game = game };
}

/// <summary>
Expand Down Expand Up @@ -745,7 +745,7 @@ public static async Task<bool> UpdateModrinthModPackAsync(UpdateModPackArg arg)
/// <param name="group">实例组</param>
/// <returns>Res安装结果
/// Game游戏实例</returns>
public static async Task<AddGameRes> InstallModrinthModPackAsync(InstallModPackZipArg arg)
public static async Task<GameRes> InstallModrinthModPackAsync(InstallModPackZipArg arg)
{
arg.Update2?.Invoke(CoreRunState.Read);
using ZipFile zFile = new(PathHelper.OpenRead(arg.Zip));
Expand Down Expand Up @@ -833,7 +833,7 @@ public static async Task<AddGameRes> InstallModrinthModPackAsync(InstallModPackZ

if (game == null)
{
return new AddGameRes { Game = game };
return new GameRes { Game = game };
}

int length = "overrides".Length;
Expand Down Expand Up @@ -873,6 +873,6 @@ public static async Task<AddGameRes> InstallModrinthModPackAsync(InstallModPackZ

await DownloadManager.StartAsync([.. list]);

return new AddGameRes { State = true, Game = game };
return new GameRes { State = true, Game = game };
}
}
1 change: 1 addition & 0 deletions src/ColorMC.Core/Helpers/PathHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Text;
using ColorMC.Core.Objs;
using ColorMC.Core.Utils;
Expand Down
28 changes: 14 additions & 14 deletions src/ColorMC.Core/LaunchPath/JvmPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public static string GetPath(this JavaInfo info)
/// <param name="sha256">验证</param>
/// <param name="url">地址</param>
/// <returns>结果</returns>
public static async Task<InstallRes> InstallAsync(InstallJvmArg arg)
public static async Task<MessageRes> InstallAsync(InstallJvmArg arg)
{
try
{
Remove(arg.Name);
var res = await DownloadAsync(arg.File, arg.Sha256, arg.Url);
if (!res.State)
{
return new InstallRes { Message = LanguageHelper.Get("Core.Jvm.Error5") };
return new MessageRes { Message = LanguageHelper.Get("Core.Jvm.Error5") };
}
arg.Unzip?.Invoke();
res = await UnzipJavaAsync(new UnzipArg
Expand All @@ -83,17 +83,17 @@ public static async Task<InstallRes> InstallAsync(InstallJvmArg arg)
});
if (!res.State)
{
return new InstallRes { Message = res.Message };
return new MessageRes { Message = res.Message };
}
}
catch (Exception e)
{
string text = LanguageHelper.Get("Core.Jvm.Error7");
Logs.Error(text, e);
return new InstallRes { Message = text };
return new MessageRes { Message = text };
}

return new InstallRes { State = true };
return new MessageRes { State = true };
}

/// <summary>
Expand All @@ -103,7 +103,7 @@ public static async Task<InstallRes> InstallAsync(InstallJvmArg arg)
/// <param name="sha256">校验</param>
/// <param name="url">网址</param>
/// <returns>结果</returns>
private static async Task<InstallRes> DownloadAsync(string name, string sha256, string url)
private static async Task<MessageRes> DownloadAsync(string name, string sha256, string url)
{
var item = new DownloadItemObj()
{
Expand All @@ -120,7 +120,7 @@ private static async Task<InstallRes> DownloadAsync(string name, string sha256,
return new();
}

return new InstallRes { State = true, Message = item.Local };
return new MessageRes { State = true, Message = item.Local };
}

/// <summary>
Expand All @@ -146,14 +146,14 @@ private static async Task<InstallRes> DownloadAsync(string name, string sha256,
/// <param name="name">名字</param>
/// <param name="file">文件</param>
/// <returns></returns>
public static async Task<InstallRes> UnzipJavaAsync(UnzipArg arg)
public static async Task<MessageRes> UnzipJavaAsync(UnzipArg arg)
{
string path = BaseDir + Name1 + "/" + arg.Name;
Directory.CreateDirectory(path);
var stream = PathHelper.OpenRead(arg.File);
if (stream == null)
{
return new InstallRes { Message = string.Format(LanguageHelper.Get("Core.Jvm.Error11"), arg.File) };
return new MessageRes { Message = string.Format(LanguageHelper.Get("Core.Jvm.Error11"), arg.File) };
}

(bool, Exception?) res;
Expand Down Expand Up @@ -194,13 +194,13 @@ public static async Task<InstallRes> UnzipJavaAsync(UnzipArg arg)
{
string temp = LanguageHelper.Get("Core.Jvm.Error12");
Logs.Error(temp, res.Item2);
return new InstallRes { Message = temp };
return new MessageRes { Message = temp };
}

var java = Find(path);
if (java == null)
{
return new InstallRes { Message = LanguageHelper.Get("Core.Jvm.Error6") };
return new MessageRes { Message = LanguageHelper.Get("Core.Jvm.Error6") };
}
else
{
Expand Down Expand Up @@ -231,7 +231,7 @@ public static async Task<InstallRes> UnzipJavaAsync(UnzipArg arg)
/// <param name="name">名字</param>
/// <param name="local">路径</param>
/// <returns>结果</returns>
public static InstallRes AddItem(string name, string local)
public static MessageRes AddItem(string name, string local)
{
if (local.StartsWith(BaseDir))
{
Expand All @@ -257,14 +257,14 @@ public static InstallRes AddItem(string name, string local)
Local = local
});
ConfigUtils.Save();
return new InstallRes { State = true, Message = name };
return new MessageRes { State = true, Message = name };
}
else
{
Logs.Info(LanguageHelper.Get("Core.Jvm.Error8"));
}

return new InstallRes { Message = LanguageHelper.Get("Core.Jvm.Error1") };
return new MessageRes { Message = LanguageHelper.Get("Core.Jvm.Error1") };
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ColorMC.Core/LaunchPath/LibrariesPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public static async Task<List<string>> GetLibsAsync(this GameSettingObj obj)
var list3 = new List<string>(list.Values);
if (obj.Loader != Loaders.NeoForge)
{
list3.Add(LibrariesPath.GetGameFile(obj.Version));
list3.Add(GetGameFile(obj.Version));
}

return list3;
Expand Down
9 changes: 5 additions & 4 deletions src/ColorMC.Core/LaunchPath/ToolPath.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using ColorMC.Core.Downloader;
using ColorMC.Core.Helpers;
using ColorMC.Core.Objs;

namespace ColorMC.Core.LaunchPath;

Expand Down Expand Up @@ -31,22 +32,22 @@ public static void Init(string dir)
/// 打开地图编辑器
/// </summary>
/// <returns></returns>
public static async Task<(bool, string?)> OpenMapEditAsync()
public static async Task<MessageRes> OpenMapEditAsync()
{
var item = DownloadItemHelper.BuildMcaselectorItem();
if (!File.Exists(item.Local))
{
var res = await DownloadManager.StartAsync([item]);
if (!res)
{
return (false, LanguageHelper.Get("Core.Tool.Error1"));
return new MessageRes { Message = LanguageHelper.Get("Core.Tool.Error1") };
}
}

var java = JvmPath.FindJava(17);
if (java == null)
{
return (false, LanguageHelper.Get("Core.Tool.Error2"));
return new MessageRes { Message = LanguageHelper.Get("Core.Tool.Error2") };
}

var info = new ProcessStartInfo(java.Path)
Expand All @@ -67,6 +68,6 @@ public static void Init(string dir)
p.BeginOutputReadLine();
p.BeginErrorReadLine();

return (true, null);
return new MessageRes { State = true };
}
}
8 changes: 4 additions & 4 deletions src/ColorMC.Core/Objs/Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,18 @@ public record MakeDownloadNameItemsRes
}

/// <summary>
/// 添加游戏结果
///
/// </summary>
public record AddGameRes
public record GameRes
{
public bool State;
public GameSettingObj? Game;
}

/// <summary>
/// 安装Java结果
///
/// </summary>
public record InstallRes
public record MessageRes
{
public bool State;
public string? Message;
Expand Down
4 changes: 2 additions & 2 deletions src/ColorMC.Gui/UI/Model/GameEdit/GameEditTab5Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ public async Task EditWorld()
Model.Progress(App.Lang("GameEditWindow.Tab5.Info13"));
var res = await ToolPath.OpenMapEditAsync();
Model.ProgressClose();
if (!res.Item1)
if (!res.State)
{
Model.Show(res.Item2!);
Model.Show(res.Message!);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ColorMC.Gui/UIBinding/BaseBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ public static async Task<bool> SetLive2DCore(string local)
{
if (SystemInfo.Os != OsType.Windows)
{
PathUtils.Chmod(file);
PathBinding.Chmod(file);
}

var p = new Process
Expand Down
2 changes: 1 addition & 1 deletion src/ColorMC.Gui/UIBinding/GameBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static async Task<bool> AddGame(string? name, string local, List<string>?
/// <param name="update"></param>
/// <param name="update2"></param>
/// <returns></returns>
public static Task<AddGameRes> AddPack(string dir, PackType type, string? name,
public static Task<GameRes> AddPack(string dir, PackType type, string? name,
string? group, ColorMCCore.ZipUpdate zip, ColorMCCore.Request request,
ColorMCCore.GameOverwirte overwirte, ColorMCCore.PackUpdate update, ColorMCCore.PackState update2)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ColorMC.Gui/UIBinding/JavaBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static JavaInfoObj MakeInfo(string name, JavaInfo item)
};
}

public static async Task<InstallRes> AddJavaZip(string file, string name, ColorMCCore.ZipUpdate zip)
public static async Task<MessageRes> AddJavaZip(string file, string name, ColorMCCore.ZipUpdate zip)
{
return await JvmPath.UnzipJavaAsync(new UnzipArg
{
Expand Down
28 changes: 28 additions & 0 deletions src/ColorMC.Gui/UIBinding/PathBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ namespace ColorMC.Gui.UIBinding;

public static class PathBinding
{
/// <summary>
/// 提升权限
/// </summary>
/// <param name="path">文件</param>
public static void Chmod(string path)
{
try
{
using var p = new Process();
p.StartInfo.FileName = "sh";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();

p.StandardInput.WriteLine("chmod a+x " + path);

p.StandardInput.WriteLine("exit");
p.WaitForExit();
}
catch (Exception e)
{
Logs.Error("chmod error", e);
}
}

/// <summary>
/// 在资源管理器打开路径
/// </summary>
Expand Down
Loading

0 comments on commit 5c53db2

Please sign in to comment.