Skip to content

Commit e6fee29

Browse files
committed
fix/files lost
fix the lost files
1 parent 1fad875 commit e6fee29

File tree

4 files changed

+355
-47
lines changed

4 files changed

+355
-47
lines changed

PCL2.Neo/Models/Minecraft/Java.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
4-
using PCL2.Neo.Models.Minecraft.JavaSearcher;
4+
using System.Runtime.InteropServices;
55

6-
namespace PCL2.Neo.Models.Minecraft
6+
namespace PCL2.Neo.Models.Minecraft.Java
77
{
88
/// <summary>
99
/// 测试
1010
/// </summary>
1111
public class Java
1212
{
13-
public static async Task<List<JavaEntity>> SearchJava()
13+
public static async Task<IEnumerable<JavaEntity>> SearchJava()
1414
{
15-
var javaList = new List<JavaEntity>();
15+
//switch (Environment.OSVersion.Platform)
16+
//{
17+
// case PlatformID.Win32NT:
18+
// javaList.AddRange(await JavaSearcher.Windows.SearchJava());
19+
// break;
20+
// case PlatformID.Unix:
21+
// javaList.AddRange(Unix.SerachJavaForLinuxAsync());
22+
// break;
23+
// case PlatformID.MacOSX:
24+
// break;
25+
// default:
26+
// throw new PlatformNotSupportedException();
27+
//}
1628

17-
switch (Environment.OSVersion.Platform)
29+
// warning: Environment.OSVersion.Platform will have different performance in different .net planform
30+
// detail: .net type : | system | performance
31+
// .net framewrok: macos | Not Support
32+
// .net Core <= 3.1 macos | Unix
33+
// .net 5+ macos | macosx
34+
// this problenm is fixed by follow code
35+
36+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
1837
{
19-
case PlatformID.Win32NT:
20-
javaList.AddRange(await PCL2.Neo.Models.Minecraft.JavaSearcher.Windows.SearchJava());
21-
break;
22-
case PlatformID.Unix:
23-
javaList.AddRange(Unix.SerachJava());
24-
break;
25-
default:
26-
throw new PlatformNotSupportedException();
38+
return await Windows.SearchJavaAsync(); // TODO: Read setting to get whether full search or not.
39+
}
40+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
41+
{
42+
return await Unix.SearchJava();
43+
}
44+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
45+
{
46+
return await Unix.SearchJava();
47+
}
48+
else
49+
{
50+
throw new PlatformNotSupportedException();
2751
}
28-
29-
return javaList;
3052
}
3153
}
32-
}
54+
}

PCL2.Neo/Models/Minecraft/JavaData.cs

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1-
using System;
21
using System.Diagnostics;
32
using System.IO;
43
using System.Text.RegularExpressions;
54

6-
namespace PCL2.Neo.Models.Minecraft
5+
namespace PCL2.Neo.Models.Minecraft.Java
76
{
8-
internal record JavaExist
9-
{
10-
public required bool IsExist { get; set; }
11-
12-
public required string Path { get; set; }
13-
}
147
public class JavaEntity(string path)
158
{
16-
public string Path = path.EndsWith('\\') ? path : path + '\\';
9+
public string Path = path;
1710

1811
public bool IsUseable = true;
1912

13+
private void JavaInfoInit()
14+
{
15+
// set version
16+
var regexMatch = Regex.Match(Output, """version "([\d._]+)""");
17+
var match = Regex.Match(regexMatch.Success ? regexMatch.Groups[1].Value : string.Empty,
18+
@"^(\d+)\.");
19+
_version = match.Success ? int.Parse(match.Groups[1].Value) : 0;
20+
21+
if (_version == 1)
22+
{
23+
// java version 8
24+
match = Regex.Match(regexMatch.Groups[1].Value, @"^1\.(\d+)\.");
25+
_version = match.Success ? int.Parse(match.Groups[1].Value) : 0;
26+
}
27+
28+
// set bit
29+
regexMatch = Regex.Match(Output, @"\b(\d+)-Bit\b"); // get bit
30+
_is64Bit = (regexMatch.Success ? regexMatch.Groups[1].Value : string.Empty) == "64";
31+
32+
// delete output
33+
_output = null;
34+
}
35+
2036
private int? _version;
2137

2238
public int Version
@@ -28,26 +44,15 @@ public int Version
2844
return _version.Value;
2945
}
3046

31-
// get java version code
32-
var javaVersionMatch = Regex.Match(Output, """version "([\d._]+)""");
33-
var match = Regex.Match(javaVersionMatch.Success ? javaVersionMatch.Groups[1].Value : string.Empty,
34-
@"^(\d+)\.");
35-
_version = match.Success ? int.Parse(match.Groups[1].Value) : 0;
36-
37-
if (_version == 1)
38-
{
39-
// java version 8
40-
match = Regex.Match(javaVersionMatch.Groups[1].Value, @"^1\.(\d+)\.");
41-
_version = match.Success ? int.Parse(match.Groups[1].Value) : 0;
47+
// java info init
48+
JavaInfoInit();
4249

43-
return _version.Value;
44-
}
45-
46-
return _version.Value;
50+
return _version!.Value;
4751
}
4852
}
49-
public string JavaExe => Path + "java.exe";
50-
public string JavaWExe => Path + "javaw.exe";
53+
54+
public string JavaExe => System.IO.Path.Combine(Path, "java.exe");
55+
public string JavaWExe => System.IO.Path.Combine(Path, "javaw.exe");
5156

5257
private string? _output;
5358

@@ -65,7 +70,23 @@ private string Output
6570
}
6671
}
6772

68-
public bool IsJre => !File.Exists(Path + "\\javac.exe");
73+
private bool? _isJre = null;
74+
75+
public bool IsJre
76+
{
77+
get
78+
{
79+
if (_isJre != null)
80+
{
81+
return _isJre.Value;
82+
}
83+
84+
var result = File.Exists(Path + "\\javac.exe");
85+
_isJre = result;
86+
return result;
87+
}
88+
}
89+
6990
public bool IsUserImport { set; get; }
7091

7192
private bool? _is64Bit;
@@ -79,9 +100,11 @@ public bool Is64Bit
79100
return _is64Bit.Value;
80101
}
81102

82-
var javaBitMatch = Regex.Match(Output, @"\b(\d+)-Bit\b"); // get bit
83-
_is64Bit = (javaBitMatch.Success ? javaBitMatch.Groups[1].Value : string.Empty) == "64";
84-
return _is64Bit.Value;
103+
104+
// java info init
105+
JavaInfoInit();
106+
107+
return _is64Bit!.Value;
85108
}
86109
}
87110

@@ -100,7 +123,7 @@ private string RunJava()
100123
javaProcess.Start();
101124
javaProcess.WaitForExit();
102125

103-
var output = javaProcess.StandardError.ReadToEnd(); // check stderr have content
126+
var output = javaProcess.StandardError.ReadToEnd();
104127
return output != string.Empty ? output : javaProcess.StandardOutput.ReadToEnd(); // 就是tmd stderr
105128
}
106129
}

PCL2.Neo/Models/Minecraft/Unix.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Collections.Concurrent;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
using System.Threading.Tasks;
8+
9+
namespace PCL2.Neo.Models.Minecraft.Java
10+
{
11+
/// <summary>
12+
/// 处理Unix系统下的java
13+
/// </summary>
14+
internal static class Unix
15+
{
16+
#warning "该方法未经过测试,可能无法正常工作 Unix/SearchJava"
17+
public static async Task<IEnumerable<JavaEntity>> SearchJava() =>
18+
await Task.Run(() => FindJavaExecutablePath().Select(it => new JavaEntity(it)));
19+
20+
private static IEnumerable<string> FindJavaExecutablePath() =>
21+
GetPotentialJavaDir()
22+
.Where(Directory.Exists)
23+
.SelectMany(SearchJavaExecutables)
24+
.Distinct();
25+
26+
private static bool IsValidJavaExecutable(string filePath)
27+
{
28+
// TODO: check execute permission
29+
return File.Exists(filePath);
30+
}
31+
32+
private static IEnumerable<string> SearchJavaExecutables(string basePath)
33+
{
34+
try
35+
{
36+
return Directory
37+
.EnumerateFiles(basePath, "java", new EnumerationOptions
38+
{
39+
RecurseSubdirectories = true,
40+
MaxRecursionDepth = 7,
41+
IgnoreInaccessible = true
42+
})
43+
.Where(IsValidJavaExecutable);
44+
}
45+
catch (Exception)
46+
{
47+
// TODO: Logger handling exceptions
48+
}
49+
50+
return [];
51+
}
52+
53+
private static IEnumerable<string> GetPotentialJavaDir()
54+
{
55+
var paths = new List<string>();
56+
57+
// add path
58+
paths.AddRange(Environment.GetEnvironmentVariable("PATH")?.Split(':') ?? []);
59+
60+
// add system paths
61+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
62+
{
63+
paths.AddRange([
64+
"/usr/lib/jvm",
65+
"/usr/java",
66+
"/opt/java",
67+
"/opt/jdk",
68+
"/opt/jre",
69+
"/usr/local/java",
70+
"/usr/local/jdk",
71+
"/usr/local/jre"
72+
]);
73+
}
74+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
75+
{
76+
paths.AddRange([
77+
"/Library/Java/JavaVirtualMachines",
78+
"/System/Library/Frameworks/JavaVM.framework/Versions", // Older macOS Java installs
79+
"/usr/local/opt", // Homebrew links (e.g., /usr/local/opt/openjdk)
80+
"/opt/homebrew/opt", // Homebrew on Apple Silicon
81+
"/opt/java", // Manual installs sometimes go here too
82+
"/opt/jdk",
83+
"/opt/jre"
84+
]);
85+
}
86+
else
87+
{
88+
// platform not supported
89+
throw new PlatformNotSupportedException();
90+
}
91+
92+
// add home dirs
93+
var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
94+
if (!string.IsNullOrEmpty(homeDir))
95+
{
96+
paths.AddRange([
97+
Path.Combine(homeDir, ".jdks"), // Common for SDKMAN
98+
Path.Combine(homeDir, "java"),
99+
Path.Combine(homeDir, ".java"), // Less common, but possible
100+
Path.Combine(homeDir, "sdks", "java"), // IntelliJ default download location?),
101+
]);
102+
}
103+
104+
// add java home path
105+
var javaHome = Environment.GetEnvironmentVariable("JAVA_HOME");
106+
if (string.IsNullOrEmpty(javaHome) || !Directory.Exists(javaHome))
107+
{
108+
return paths.Distinct();
109+
}
110+
111+
paths.Add(javaHome);
112+
// add java home parent path
113+
var parent =
114+
Path.GetDirectoryName(javaHome.TrimEnd(Path.DirectorySeparatorChar,
115+
Path.AltDirectorySeparatorChar));
116+
if (!string.IsNullOrEmpty(parent) && Directory.Exists(parent) && !paths.Contains(parent))
117+
{
118+
paths.Add(parent);
119+
}
120+
121+
return paths.Distinct();
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)