Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Add symbolic link tools and Shell command line tool #77

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Helper/CommandLineHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Diagnostics;

namespace ProjBobcat.Class.Helper
{
public static class CommandLineHelper
{
public static string RunCMD(string head, string flags)
{
try
{
ProcessStartInfo i = new()
{
FileName = head,
Arguments = flags,
RedirectStandardOutput = true,
CreateNoWindow = true,
};
using (var proc = Process.Start(i))
{
return proc.StandardOutput.ReadToEnd();
}
}
catch(Exception ex)
{
return ex.ToString();
}
return "null";
}
//
public static string RunBash(string command)
{
///<summary>供Linux和macOS使用的Bash(macOS默认使用zsh)</summary>
///
#if OSX || LINUX

try
{
command = command.Replace("\"", "\\\"");
Process proc = new()
{
StartInfo = new ProcessStartInfo
{
#if OSX
FileName = "/bin/zsh",
#elif LINUX
FileName = "/bin/bash",
#endif
Arguments = $"-c \"{command}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,

}
};
proc.Start();
string returning = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
proc.Dispose();
return returning;
}
catch (Exception ex)
{
return ex.ToString();
}
#endif
return "null";
}
//
public static string RunBashArgs(string commandHead, string[] args)
{
string Arg = "";
foreach(var arg in args)
{
Arg += (" " + arg);
}
return RunBash(commandHead + Arg);
}
}
}
95 changes: 95 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Helper/SymLinkHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
namespace ProjBobcat.Class.Helper
{
public class SymLinkHelper
{
/// <summary>
/// 符号链接创建工具
/// </summary>
private string _defaultPublicResourceLocation;

public bool CreateLink(string targetItem, string? overrideDefaultLocation = null, bool? parseToWindowsPath = null)
{
///<summary>
/// 自动根据当前系统(构建) 选择 符号链接 创建方法.
/// </summary>
/// <param name="targetItem">
/// 需要创建链接到具体版本的资源文件夹的单个文件之路径.(需要包含文件名与后缀)
/// </param>
/// <param name="overrideDefaultLocation">
/// 链接需要被创建到的路径(需要包含文件名与后缀), 可为null, 默认值在创建对象时候创建(强烈建议传一个具体的)
/// </param>
/// <param name="parseToWindowsPath">
/// 可null的值. 为true时将把两个路径中的/转换为\
/// </param>
/// <returns>返回成功与否.</returns>

string ThisDefaultLocation;

if (overrideDefaultLocation is not null) ThisDefaultLocation = overrideDefaultLocation;
else ThisDefaultLocation = _defaultPublicResourceLocation;
// 未验证: 是否可以在不给文件后缀的情况下创建到此目录的默认同名文件... 若不能, 将在后续添加文件名检测和自动拼接.

if(parseToWindowsPath is not null) if ((bool)parseToWindowsPath)
{
ThisDefaultLocation = ThisDefaultLocation.Replace("/", "\\");
targetItem = targetItem.Replace("/", "\\");
}
string? CmdReturn = "";
try {
#if WINDOWS
// 原来从vista就自带mklink了哇 那就不考虑junction工具了.
CmdReturn = CommandLineHelper.RunCMD("mklink", $"/d {targetItem} {ThisDefaultLocation}");
//? 待验证: /d与/h是否: 跨分区, 增加占用, 可被Minecraft识别
#elif OSX
CmdReturn = CommandLineHelper.RunBash($"ln -s {targetItem} {ThisDefaultLocation}");
// 一般而言, mac与linux用户可以不考虑跨分区问题. 可以在确认软链接可以被minecraft识别后直接全部软链接
#elif LINUX
CmdReturn = CommandLineHelper.RunBash($"ln -s {targetItem} {ThisDefaultLocation}");
// 这应该||就好了吧……不确定
#endif
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}

if (CmdReturn.Contains("nvalid")) return false; // 收集可能的错误信息. 但是返回信息可能是不同的语言, 考虑移除...
else return true;
}

public bool RemoveLink(string targetLink)
{
string? CmdReturn = "";
try
{
#if WINDOWS
CmdReturn = CommandLineHelper.RunCMD("erase", $"/Q {targetLink}");
#elif OSX
CmdReturn = CommandLineHelper.RunBash($"rm -rf {targetLink}");
#elif LINUX
CmdReturn = CommandLineHelper.RunBash($"unlink {targetLink}");
#endif
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
if (CmdReturn.Contains("nvalid")) return false; // 同上...
return true;
}

public SymLinkHelper(string defaultLocation)
{
/// <summary>
/// 构造方法
/// </summary>
/// <param name="defaultLocation">
/// 创建对象时候存入默认的存储共享资源(软链创建点)的路径.
/// </param>
_defaultPublicResourceLocation = defaultLocation;
}
}
}
14 changes: 14 additions & 0 deletions ProjBobcat/ProjBobcat/Class/Helper/SystemInfoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,18 @@ static IEnumerable<string> FindJavaInOfficialGamePath()
return null;
}
}

static string GetOsVersion()
{
string OsVersion = "null";
#if WINDOWS
SystemInfo.WindowsSystemVersion vw = new();
OsVersion = vw.ToString();
#elif OSX

#elif LINUX

#endif
return OsVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public override async Task<LaunchResult> LaunchTaskAsync(LaunchSettings? setting
{
Cause = "未找到JRE运行时,可能是输入的路劲为空或出错,亦或是指定的文件并不存在。",
Error = "未找到JRE运行时",
ErrorMessage = "输入的路劲为空或出错,亦或是指定的文件并不存在"
ErrorMessage = "输入的路径为空或出错,亦或是指定的文件并不存在"
}
};

Expand Down Expand Up @@ -409,4 +409,4 @@ void InvokeLaunchLogThenStart(string item, ref TimeSpan time, ref Stopwatch sw)
}

#endregion
}
}
3 changes: 1 addition & 2 deletions ProjBobcat/ProjBobcat/ProjBobcat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ change HttpClientHelper usage
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.18-beta">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="SharpCompress">
<Version>0.33.0</Version>
<PackageReference Include="SharpCompress" Version="0.33.0">
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" '$(IsWindows)'=='true' ">
Expand Down