Skip to content

Commit 1ac73e2

Browse files
authored
Merge pull request #5 from LaoSparrow/whitecat346-main
修复 MyColor 编译错误 && 修复 linux 下 java 搜索
2 parents 94afc0d + 16388c2 commit 1ac73e2

File tree

5 files changed

+110
-59
lines changed

5 files changed

+110
-59
lines changed

PCL2.Neo/Helpers/ThemeHelper.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,18 @@ public void Refresh(ThemeVariant themeVariant)
5555
brushTitle.GradientStops.Add(new GradientStop
5656
{
5757
Offset = 0,
58-
Color = new MyColor().FromHsl2(_colorHue - _colorHueTopbarDelta, _colorSat, 48 + _colorLightAdjust)
58+
Color = MyColor.FromHsl2(_colorHue - _colorHueTopbarDelta, _colorSat,
59+
48 + _colorLightAdjust)
5960
});
6061
brushTitle.GradientStops.Add(new GradientStop
6162
{
62-
Offset = 0.5,
63-
Color = new MyColor().FromHsl2(_colorHue, _colorSat, 54 + _colorLightAdjust)
63+
Offset = 0.5, Color = MyColor.FromHsl2(_colorHue, _colorSat, 54 + _colorLightAdjust)
6464
});
6565
brushTitle.GradientStops.Add(new GradientStop
6666
{
6767
Offset = 1,
68-
Color = new MyColor().FromHsl2(_colorHue + _colorHueTopbarDelta, _colorSat, 48 + _colorLightAdjust)
68+
Color = MyColor.FromHsl2(_colorHue + _colorHueTopbarDelta, _colorSat,
69+
48 + _colorLightAdjust)
6970
});
7071

7172
_mainWindow.NavBackgroundBorder.Background = brushTitle;
@@ -90,17 +91,18 @@ public void Refresh(ThemeVariant themeVariant)
9091
brushBackground.GradientStops.Add(new GradientStop
9192
{
9293
Offset = -0.1,
93-
Color = new MyColor().FromHsl2(_colorHue - 20, Math.Min(60, _colorSat) * 0.5, 80 * lightAdjust)
94+
Color = MyColor.FromHsl2(_colorHue - 20, Math.Min(60, _colorSat) * 0.5f,
95+
80 * lightAdjust)
9496
});
9597
brushBackground.GradientStops.Add(new GradientStop
9698
{
97-
Offset = 0.4,
98-
Color = new MyColor().FromHsl2(_colorHue, _colorSat * 0.9, 90 * lightAdjust)
99+
Offset = 0.4, Color = MyColor.FromHsl2(_colorHue, _colorSat * 0.9f, 90 * lightAdjust)
99100
});
100101
brushBackground.GradientStops.Add(new GradientStop
101102
{
102103
Offset = 1.1,
103-
Color = new MyColor().FromHsl2(_colorHue + 20, Math.Min(60, _colorSat) * 0.5, 80 * lightAdjust)
104+
Color = MyColor.FromHsl2(_colorHue + 20, Math.Min(60, _colorSat) * 0.5f,
105+
80 * lightAdjust)
104106
});
105107

106108
_mainWindow.MainBorder.Background = brushBackground;

PCL2.Neo/Models/Minecraft/Java/Java.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public static async Task<IEnumerable<JavaEntity>> SearchJava()
3939
}
4040
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
4141
{
42-
return Unix.SearchJava();
42+
return await Unix.SearchJava();
4343
}
4444
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
4545
{
46-
return Unix.SearchJava();
46+
return await Unix.SearchJava();
4747
}
4848
else
4949
{

PCL2.Neo/Models/Minecraft/Java/Unix.cs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
56
using System.Runtime.InteropServices;
7+
using System.Threading.Tasks;
68

79
namespace PCL2.Neo.Models.Minecraft.Java
810
{
911
/// <summary>
1012
/// 处理Unix系统下的java
1113
/// </summary>
12-
internal class Unix
14+
internal static class Unix
1315
{
14-
#warning "该方法未在 Linux 上测试,可能无法正常工作 Unix/SearchJava"
15-
public static IEnumerable<JavaEntity> SearchJava() =>
16-
FindJavaExecutablePath().Select(it => new JavaEntity(it));
16+
#warning "该方法未经过测试,可能无法正常工作 Unix/SearchJava"
17+
public static async Task<IEnumerable<JavaEntity>> SearchJava() =>
18+
await Task.Run(() => FindJavaExecutablePath().Select(it => new JavaEntity(it)));
1719

18-
private static HashSet<string> FindJavaExecutablePath()
19-
{
20-
var foundJava = new HashSet<string>();
21-
22-
GetPontentialJavaDir()
23-
.Where(Directory.Exists)
24-
.ToList().ForEach(it => SearchDirectoryForJava(it, foundJava));
25-
26-
return foundJava;
27-
}
20+
private static IEnumerable<string> FindJavaExecutablePath() =>
21+
GetPotentialJavaDir()
22+
.Where(Directory.Exists)
23+
.SelectMany(SearchJavaExecutables)
24+
.Distinct();
2825

2926
private static bool IsValidJavaExecutable(string filePath)
3027
{
31-
// if (Directory.Exists(filePath))
32-
// return false;
33-
34-
// return !filePath.EndsWith(".jar") && !filePath.EndsWith(".zip") && !filePath.EndsWith(".so") &&
35-
// !filePath.EndsWith(".dylib");
28+
// TODO: check execute permission
3629
return File.Exists(filePath);
3730
}
3831

39-
private static void SearchDirectoryForJava(string basePath, HashSet<string> foundJava)
32+
private static IEnumerable<string> SearchJavaExecutables(string basePath)
4033
{
4134
try
4235
{
43-
var binDirs = Directory.EnumerateDirectories(basePath, "bin", SearchOption.AllDirectories);
44-
binDirs
45-
.SelectMany(binDir => Directory.EnumerateFiles(binDir, "java", SearchOption.TopDirectoryOnly))
46-
.Where(IsValidJavaExecutable)
47-
.ToList().ForEach(it => foundJava.Add(it));
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
4848
}
49-
catch (UnauthorizedAccessException) { }
49+
50+
return [];
5051
}
5152

52-
private static IEnumerable<string> GetPontentialJavaDir()
53+
private static IEnumerable<string> GetPotentialJavaDir()
5354
{
5455
var paths = new List<string>();
55-
var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
56+
57+
// add path
58+
paths.AddRange(Environment.GetEnvironmentVariable("PATH")?.Split(':') ?? []);
5659

5760
// add system paths
5861
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
@@ -87,6 +90,7 @@ private static IEnumerable<string> GetPontentialJavaDir()
8790
}
8891

8992
// add home dirs
93+
var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
9094
if (!string.IsNullOrEmpty(homeDir))
9195
{
9296
paths.AddRange([
@@ -114,7 +118,6 @@ private static IEnumerable<string> GetPontentialJavaDir()
114118
paths.Add(parent);
115119
}
116120

117-
118121
return paths.Distinct();
119122
}
120123
}

PCL2.Neo/Models/MyColor.cs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace PCL2.Neo.Models;
66

7-
public class MyColor
7+
public class MyColor : IEquatable<MyColor>
88
{
99
private Vector4 _color;
1010

@@ -163,9 +163,51 @@ public MyColor(SolidColorBrush brush)
163163
this._color = new Vector4(color.A, color.R, color.G, color.B);
164164
}
165165

166+
// IEquatable
167+
168+
public bool Equals(MyColor? other)
169+
{
170+
if (other is null)
171+
{
172+
return false;
173+
}
174+
175+
if (ReferenceEquals(this, other))
176+
{
177+
return true;
178+
}
179+
180+
return _color.Equals(other._color);
181+
}
182+
183+
public override bool Equals(object? obj)
184+
{
185+
if (obj is null)
186+
{
187+
return false;
188+
}
189+
190+
if (ReferenceEquals(this, obj))
191+
{
192+
return true;
193+
}
194+
195+
if (obj.GetType() != GetType())
196+
{
197+
return false;
198+
}
199+
200+
return Equals((MyColor)obj);
201+
}
202+
203+
public override int GetHashCode()
204+
{
205+
return _color.GetHashCode();
206+
}
207+
166208
// HSL
167209

168-
public double Hue(double v1, double v2, double vH)
210+
public static double Hue(double v1, double v2, double vH)
169211
{
170212
if (vH < 0) vH += 1;
171213
if (vH > 1) vH -= 1;
@@ -175,13 +217,14 @@ public double Hue(double v1, double v2, double vH)
175217
return v1;
176218
}
177219

178-
public MyColor FromHsl(double sH, double sS, double sL)
220+
public static MyColor FromHsl(double sH, double sS, double sL)
179221
{
222+
var color = new MyColor();
180223
if (sS == 0)
181224
{
182-
R = (float)(sL * 2.55);
183-
G = R;
184-
B = R;
225+
color.R = (float)(sL * 2.55);
226+
color.G = color.R;
227+
color.B = color.R;
185228
}
186229
else
187230
{
@@ -190,22 +233,23 @@ public MyColor FromHsl(double sH, double sS, double sL)
190233
double l = sL / 100;
191234
s = l < 0.5 ? s * l + l : s * (1.0 - l) + l;
192235
l = 2 * l - s;
193-
R = (float)(255 * Hue(l, s, h + 1 / 3.0));
194-
G = (float)(255 * Hue(l, s, h));
195-
B = (float)(255 * Hue(l, s, h - 1 / 3.0));
236+
color.R = (float)(255 * Hue(l, s, h + 1 / 3.0));
237+
color.G = (float)(255 * Hue(l, s, h));
238+
color.B = (float)(255 * Hue(l, s, h - 1 / 3.0));
196239
}
197240

198-
A = 255;
199-
return this;
241+
color.A = 255;
242+
return color;
200243
}
201244

202-
public MyColor FromHsl2(double sH, double sS, double sL)
245+
public static MyColor FromHsl2(double sH, double sS, double sL)
203246
{
247+
var color = new MyColor();
204248
if (sS == 0)
205249
{
206-
R = (float)(sL * 2.55);
207-
G = R;
208-
B = R;
250+
color.R = (float)(sL * 2.55);
251+
color.G = color.R;
252+
color.B = color.R;
209253
}
210254
else
211255
{
@@ -226,10 +270,10 @@ public MyColor FromHsl2(double sH, double sS, double sL)
226270

227271
sL = sL < center ? sL / center : 1 + (sL - center) / (100 - center);
228272
sL *= 50;
229-
FromHsl(sH, sS, sL);
273+
color = FromHsl(sH, sS, sL);
230274
}
231275

232-
A = 255;
233-
return this;
276+
color.A = 255;
277+
return color;
234278
}
235279
}

PCL2.NeoTests/Models/MainTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ public class MainTests
88
[TestMethod]
99
public async Task JavaSearchTest()
1010
{
11-
foreach (var javaEntity in await Java.SearchJava()) {
12-
Console.WriteLine(javaEntity.Path);
11+
var result = Minecraft.Java.Java.SearchJava().Result;
12+
foreach (var item in result)
13+
{
14+
Console.WriteLine(item.Path);
1315
}
1416
}
1517
}

0 commit comments

Comments
 (0)