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

Full support for runtime cross platform #98

Merged
merged 4 commits into from
Nov 30, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ jobs:
id: previoustag
uses: "WyriHaximus/github-action-get-previous-tag@v1"
with:
fallback: 0.0.0
- name: Fix version tag
run: echo ::set-env name=PREVIOUS_TAG::${{ steps.previoustag.outputs.tag }}
fallback: 1.0.0
- name: Restore packages
run: dotnet restore ProjBobcat/ProjBobcat.sln
- name: Build the project
run: dotnet build ProjBobcat/ProjBobcat.sln -c Release --no-restore
- name: Create the package
run: $tag="${{ steps.previoustag.outputs.tag }}".replace("v",""); dotnet pack ProjBobcat/ProjBobcat.sln -c Release -p:PackageVersion="$tag-(CI)${{ github.sha }}-beta" --no-build --no-restore
run: $tag="${{ steps.previoustag.outputs.tag }}".replace("v",""); dotnet pack ProjBobcat/ProjBobcat.sln -c Release -p:PackageVersion="$tag-CI${{ github.sha }}-beta" --no-build --no-restore
- name: Publish the package to NuGet.org
run: dotnet nuget push -k ${{secrets.NUGET_KEY}} -s https://api.nuget.org/v3/index.json "ProjBobcat/ProjBobcat/bin/Release/*.nupkg"
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: test

on:
push:
branches: [master]
pull_request:
branches: [master]

Expand Down
51 changes: 27 additions & 24 deletions ProjBobcat/ProjBobcat/Class/Helper/DeepJavaSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace ProjBobcat.Class.Helper;

Expand All @@ -12,27 +10,34 @@ public static class DeepJavaSearcher
public static async IAsyncEnumerable<string> DeepSearch(string drive, string fileName)
{
var result = new HashSet<string>();
var psi = new ProcessStartInfo(Constants.WhereCommand)
{
#if WINDOWS
ArgumentList =
ProcessStartInfo psi;
if (OperatingSystem.IsWindows())
psi = new ProcessStartInfo(Constants.WhereCommand)
{
"/R",
$"{drive}\\",
fileName
},
#elif OSX || LINUX
ArgumentList =
ArgumentList =
{
"/R",
$"{drive}\\",
fileName
},
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
else
psi = new ProcessStartInfo(Constants.WhereCommand)
{
"/b",
fileName
},
#endif
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false
};
ArgumentList =
{
"/b",
fileName
},
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false
};

var process = Process.Start(psi);
var isFailed = false;
Expand Down Expand Up @@ -67,7 +72,6 @@ public static async IAsyncEnumerable<string> DeepSearch(string drive, string fil

public static async IAsyncEnumerable<string> DeepSearch()
{
#if WINDOWS
if (OperatingSystem.IsWindows())
{
var drives = Platforms.Windows.SystemInfoHelper.GetLogicalDrives();
Expand All @@ -76,10 +80,9 @@ public static async IAsyncEnumerable<string> DeepSearch()
await foreach (var path in DeepSearch(drive, Constants.JavaExecutable))
yield return path;
}
#elif OSX || LINUX

if (OperatingSystem.IsMacOS() || OperatingSystem.IsLinux())
await foreach (var path in DeepSearch(string.Empty, Constants.JavaExecutable))
yield return path;
#endif
}
}
20 changes: 8 additions & 12 deletions ProjBobcat/ProjBobcat/Class/Helper/GamePathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,13 @@ public static string GetLauncherAccountPath()
/// <returns></returns>
public static string OfficialLauncherGamePath()
{
#if WINDOWS
var basePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
#elif OSX
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var basePath = Path.Combine(path, "Library", "Application Support");
#elif LINUX
var basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
#endif

basePath = Path.Combine(basePath, ".minecraft");

return basePath;
if (OperatingSystem.IsWindows())
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ".minecraft");
if (OperatingSystem.IsMacOS())
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library",
"Application Support", ".minecraft");
if (OperatingSystem.IsLinux())
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), ".minecraft");
return string.Empty;
}
}
30 changes: 13 additions & 17 deletions ProjBobcat/ProjBobcat/Class/Helper/SystemInfoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,40 @@ public static string GetSystemArch()
[SupportedOSPlatform(nameof(OSPlatform.OSX))]
public static bool IsRunningUnderTranslation()
{
#if WINDOWS
if (OperatingSystem.IsWindows())
return Platforms.Windows.SystemInfoHelper.IsRunningUnderTranslation();
#elif OSX

if (OperatingSystem.IsMacOS())
return Platforms.MacOS.SystemInfoHelper.IsRunningUnderTranslation();
#endif

return false;
}

public static CPUInfo? GetProcessorUsage()
{
#if WINDOWS
if (OperatingSystem.IsWindows())
return Platforms.Windows.SystemInfoHelper.GetWindowsCpuUsage();
#elif OSX

if (OperatingSystem.IsMacOS())
return Platforms.MacOS.SystemInfoHelper.GetOSXCpuUsage();
#elif LINUX

if (OperatingSystem.IsLinux())
return Platforms.Linux.SystemInfoHelper.GetLinuxCpuUsage();
#endif

return null;
}

public static MemoryInfo? GetMemoryUsage()
{
#if WINDOWS
if (OperatingSystem.IsWindows())
return Platforms.Windows.SystemInfoHelper.GetWindowsMemoryStatus();
#elif OSX

if (OperatingSystem.IsMacOS())
return Platforms.MacOS.SystemInfoHelper.GetOsxMemoryStatus();
#elif LINUX

if (OperatingSystem.IsLinux())
return Platforms.Linux.SystemInfoHelper.GetLinuxMemoryStatus();
#endif

return null;
}

Expand All @@ -79,20 +76,19 @@ public static async IAsyncEnumerable<string> FindJava(bool fullSearch = false)
if (fullSearch)
await foreach (var path in DeepJavaSearcher.DeepSearch())
result.Add(path);

#if WINDOWS

if (OperatingSystem.IsWindows())
foreach (var path in Platforms.Windows.SystemInfoHelper.FindJavaWindows())
result.Add(path);
#elif OSX

if (OperatingSystem.IsMacOS())
foreach (var path in Platforms.MacOS.SystemInfoHelper.FindJavaMacOS())
result.Add(path);
#elif LINUX

if (OperatingSystem.IsLinux())
foreach(var path in Platforms.Linux.SystemInfoHelper.FindJavaLinux())
result.Add(path);
#endif

foreach (var path in result)
yield return path;
foreach (var path in FindJavaInOfficialGamePath())
Expand Down
4 changes: 2 additions & 2 deletions ProjBobcat/ProjBobcat/Class/Model/JvmRulesModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public bool IsAllow()
{
if (!string.IsNullOrEmpty(Name) &&
!Name.Equals(Constants.OsSymbol, StringComparison.OrdinalIgnoreCase)) return false;

if (!string.IsNullOrEmpty(Arch) && Arch != SystemInfoHelper.GetSystemArch()) return false;
#if WINDOWS
if (OperatingSystem.IsWindows())
if (!string.IsNullOrEmpty(Version) &&
Version != $"^{Platforms.Windows.SystemInfoHelper.GetWindowsMajorVersion()}\\.")
return false;
#endif

return true;
}
Expand Down
72 changes: 51 additions & 21 deletions ProjBobcat/ProjBobcat/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
namespace ProjBobcat;
using System;
using System.Runtime.InteropServices;

namespace ProjBobcat;

public static class Constants
{
public const string FallBackVersion = "0.0.0";

#if WINDOWS
public const string WhereCommand = "where";
public const string JavaExecutable = "javaw.exe";
public const string JavaExecutablePath = $"bin\\{JavaExecutable}";
public const string JavaExecutableExtension = "exe";
public const string OsSymbol = "windows";
#elif OSX
public const string WhereCommand = "whereis";
public const string JavaExecutable = "java";
public const string JavaExecutablePath = $"Contents/Home/bin/{JavaExecutable}";
public const string JavaExecutableExtension = "*";
public const string OsSymbol = "osx";
#elif LINUX
public const string WhereCommand = "whereis";
public const string JavaExecutable = "java";
public const string JavaExecutablePath = $"bin/{JavaExecutable}";
public const string JavaExecutableExtension = "*";
public const string OsSymbol = "linux";
#endif
}
public static string WhereCommand => OperatingSystem.IsWindows() ? Windows.WhereCommand : Linux.WhereCommand;
public static string JavaExecutable => OperatingSystem.IsWindows() ? Windows.JavaExecutable : Linux.JavaExecutable;

public static string JavaExecutableExtension => OperatingSystem.IsWindows()
? Windows.JavaExecutableExtension
: Linux.JavaExecutableExtension;

public static string JavaExecutablePath => RuntimeInformation.RuntimeIdentifier switch
{
"windows" => Windows.JavaExecutablePath,
"linux" => Linux.JavaExecutablePath,
"osx" => Osx.JavaExecutablePath,
_ => throw new PlatformNotSupportedException("Unknown operating system.")
};

public static string OsSymbol => RuntimeInformation.RuntimeIdentifier switch
{
"windows" => Windows.OsSymbol,
"linux" => Linux.OsSymbol,
"osx" => Osx.OsSymbol,
_ => throw new PlatformNotSupportedException("Unknown operating system.")
};

class Windows
{
public const string WhereCommand = "where";
public const string JavaExecutable = "javaw.exe";
public const string JavaExecutablePath = $"bin\\{JavaExecutable}";
public const string JavaExecutableExtension = "exe";
public const string OsSymbol = "windows";
}

class Linux
{
public const string WhereCommand = "whereis";
public const string JavaExecutable = "java";
public const string JavaExecutablePath = $"bin/{JavaExecutable}";
public const string JavaExecutableExtension = "*";
public const string OsSymbol = "linux";
}

class Osx : Linux
{
public new const string JavaExecutablePath = $"Contents/Home/bin/{JavaExecutable}";
public new const string OsSymbol = "osx";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,12 @@ await Task.Run(() =>
do
{
if (launchWrapper.Process == null) break;

#if WINDOWS

if (OperatingSystem.IsWindows() && OperatingSystem.IsWindowsVersionAtLeast(5))
_ = Windows.Win32.PInvoke.SetWindowText(
new Windows.Win32.Foundation.HWND(launchWrapper.Process.MainWindowHandle),
settings.WindowTitle);
#endif

} while (string.IsNullOrEmpty(launchWrapper.Process?.MainWindowTitle));
});
#pragma warning restore CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace ProjBobcat.Platforms.Linux;

[SupportedOSPlatform(nameof(OSPlatform.Linux))]
public static class DistributionHelper
{
public enum LinuxDistribution
Expand Down
3 changes: 3 additions & 0 deletions ProjBobcat/ProjBobcat/Platforms/Linux/FileHelper.Linux.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace ProjBobcat.Platforms.Linux;

[SupportedOSPlatform(nameof(OSPlatform.Linux))]
public static class FileHelper
{
public static bool Chmod(string filePath, string permissions)
Expand Down
Loading