Skip to content

Commit 5ce09b0

Browse files
committed
Merge branch 'dev'
2 parents f5c8d17 + ee2753a commit 5ce09b0

File tree

15 files changed

+149
-146
lines changed

15 files changed

+149
-146
lines changed

CmlLib/CmlLib.csproj

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net462;net5.0</TargetFrameworks>
4+
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
55
<LangVersion>8.0</LangVersion>
66
<Nullable>enable</Nullable>
7-
<Version>3.3.2</Version>
7+
<Version>3.3.3</Version>
88
<Description>Minecraft Launcher Library for .NET
99
Support all version, forge, optifine
1010
</Description>
@@ -19,22 +19,28 @@ Support all version, forge, optifine
1919
<Authors>AlphaBs</Authors>
2020
<PackageReleaseNotes />
2121
<PackageId>CmlLib.Core</PackageId>
22+
23+
<WeaverConfiguration Condition="'$(Configuration)' == 'Debug'">
24+
<Weavers>
25+
<MethodTimer/>
26+
</Weavers>
27+
</WeaverConfiguration>
2228
</PropertyGroup>
2329

2430
<ItemGroup>
2531
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.1">
2632
<PrivateAssets>all</PrivateAssets>
2733
</PackageReference>
28-
<PackageReference Include="Fody" Version="6.5.1">
34+
<PackageReference Include="Fody" Version="6.5.3">
2935
<PrivateAssets>all</PrivateAssets>
3036
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3137
</PackageReference>
3238
<PackageReference Include="LZMA-SDK" Version="19.0.0" />
3339
<PackageReference Include="MethodTimer.Fody" Version="3.2.0">
3440
<PrivateAssets>all</PrivateAssets>
3541
</PackageReference>
36-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
37-
<PackageReference Include="SharpZipLib" Version="1.2.0" />
42+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
43+
<PackageReference Include="SharpZipLib" Version="1.3.3" />
3844
<None Include="../icon.png" Pack="true" Visible="false" PackagePath="" />
3945
</ItemGroup>
4046

CmlLib/Core/Auth/MLogin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ private MLoginResponse parseSession(string json, string? clientToken)
109109
AccessToken = job["accessToken"]?.ToString(),
110110
UUID = profile["id"]?.ToString(),
111111
Username = profile["name"]?.ToString(),
112+
UserType = "Mojang",
112113
ClientToken = clientToken
113114
};
114115

CmlLib/Core/Auth/MSession.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ public class MSession
66
{
77
public MSession() { }
88

9-
public MSession(string? username, string? accesstoken, string? uuid)
9+
public MSession(string? username, string? accessToken, string? uuid)
1010
{
1111
Username = username;
12-
AccessToken = accesstoken;
12+
AccessToken = accessToken;
1313
UUID = uuid;
1414
}
1515

@@ -21,6 +21,8 @@ public MSession(string? username, string? accesstoken, string? uuid)
2121
public string? UUID { get; set; }
2222
[JsonProperty("clientToken")]
2323
public string? ClientToken { get; set; }
24+
25+
public string? UserType { get; set; }
2426

2527
public bool CheckIsValid()
2628
{
@@ -35,7 +37,8 @@ public static MSession GetOfflineSession(string username)
3537
{
3638
Username = username,
3739
AccessToken = "access_token",
38-
UUID = "user_uuid",
40+
UUID = "user_uuid",
41+
UserType = "Mojang",
3942
ClientToken = null
4043
};
4144
}

CmlLib/Core/Downloader/DownloadFileChangedEventArgs.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ public enum MFile { Runtime, Library, Resource, Minecraft, Others }
88

99
public class DownloadFileChangedEventArgs : EventArgs
1010
{
11-
public DownloadFileChangedEventArgs(MFile kind, object source, string? filename, int total, int progressed)
11+
public DownloadFileChangedEventArgs(MFile type, object source, string? filename, int total, int progressed)
1212
{
13-
FileKind = kind;
13+
FileKind = type;
14+
FileType = type;
1415
FileName = filename;
1516
TotalFileCount = total;
1617
ProgressedFileCount = progressed;
1718
Source = source;
1819
}
1920

21+
// FileType and FileKind is exactly same.
22+
public MFile FileType { get; private set; }
2023
public MFile FileKind { get; private set; }
2124
public string? FileName { get; private set; }
2225
public int TotalFileCount { get; private set; }

CmlLib/Core/Launcher/MLaunch.cs

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class MLaunch
1212
{
1313
private const int DefaultServerPort = 25565;
1414

15-
public static readonly string SupportVersion = "1.16.6";
15+
public static readonly string SupportVersion = "1.17.1";
1616
public readonly static string[] DefaultJavaParameter =
1717
{
1818
"-XX:+UnlockExperimentalVMOptions",
@@ -21,6 +21,7 @@ public class MLaunch
2121
"-XX:G1ReservePercent=20",
2222
"-XX:MaxGCPauseMillis=50",
2323
"-XX:G1HeapRegionSize=16M"
24+
// "-Xss1M"
2425
};
2526

2627
public MLaunch(MLaunchOption option)
@@ -48,33 +49,8 @@ public Process GetProcess()
4849
return mc;
4950
}
5051

51-
[MethodTimer.Time]
52-
public string[] CreateArg()
52+
private string createClassPath(MVersion version)
5353
{
54-
MVersion version = LaunchOption.GetStartVersion();
55-
56-
var args = new List<string>();
57-
58-
// Common JVM Arguments
59-
if (LaunchOption.JVMArguments != null)
60-
args.AddRange(LaunchOption.JVMArguments);
61-
else
62-
{
63-
args.AddRange(DefaultJavaParameter);
64-
65-
if (LaunchOption.MaximumRamMb > 0)
66-
args.Add("-Xmx" + LaunchOption.MaximumRamMb + "m");
67-
68-
if (LaunchOption.MinimumRamMb > 0)
69-
args.Add("-Xms" + LaunchOption.MinimumRamMb + "m");
70-
}
71-
72-
if (!string.IsNullOrEmpty(LaunchOption.DockName))
73-
args.Add("-Xdock:name=" + handleEmpty(LaunchOption.DockName));
74-
if (!string.IsNullOrEmpty(LaunchOption.DockIcon))
75-
args.Add("-Xdock:icon=" + handleEmpty(LaunchOption.DockIcon));
76-
77-
// Version-specific JVM Arguments
7854
var classpath = new List<string>(version.Libraries?.Length ?? 1);
7955

8056
if (version.Libraries != null)
@@ -89,34 +65,36 @@ public string[] CreateArg()
8965
classpath.Add(minecraftPath.GetVersionJarPath(version.Jar));
9066

9167
var classpathStr = IOUtil.CombinePath(classpath.ToArray());
68+
return classpathStr;
69+
}
9270

71+
private string createNativePath(MVersion version)
72+
{
9373
var native = new MNative(minecraftPath, version);
9474
native.CleanNatives();
9575
var nativePath = native.ExtractNatives();
76+
return nativePath;
77+
}
9678

97-
var jvmdict = new Dictionary<string, string?>
79+
[MethodTimer.Time]
80+
public string[] CreateArg()
81+
{
82+
MVersion version = LaunchOption.GetStartVersion();
83+
var args = new List<string>();
84+
85+
var classpath = createClassPath(version);
86+
var nativePath = createNativePath(version);
87+
var session = LaunchOption.GetSession();
88+
89+
var argDict = new Dictionary<string, string?>
9890
{
91+
{ "library_directory", minecraftPath.Library },
9992
{ "natives_directory", nativePath },
10093
{ "launcher_name", useNotNull(LaunchOption.GameLauncherName, "minecraft-launcher") },
10194
{ "launcher_version", useNotNull(LaunchOption.GameLauncherVersion, "2") },
102-
{ "classpath", classpathStr }
103-
};
104-
105-
if (version.JvmArguments != null)
106-
args.AddRange(Mapper.MapInterpolation(version.JvmArguments, jvmdict));
107-
else
108-
{
109-
args.Add("-Djava.library.path=" + handleEmpty(nativePath));
110-
args.Add("-cp " + classpathStr);
111-
}
112-
113-
if (!string.IsNullOrEmpty(version.MainClass))
114-
args.Add(version.MainClass);
115-
116-
// Game Arguments
117-
MSession session = LaunchOption.GetSession();
118-
var gameDict = new Dictionary<string, string?>
119-
{
95+
{ "classpath_separator", Path.PathSeparator.ToString() },
96+
{ "classpath", classpath },
97+
12098
{ "auth_player_name" , session.Username },
12199
{ "version_name" , version.Id },
122100
{ "game_directory" , minecraftPath.BasePath },
@@ -125,18 +103,55 @@ public string[] CreateArg()
125103
{ "auth_uuid" , session.UUID },
126104
{ "auth_access_token", session.AccessToken },
127105
{ "user_properties" , "{}" },
128-
{ "user_type" , "Mojang" },
106+
{ "user_type" , session.UserType ?? "Mojang" },
129107
{ "game_assets" , minecraftPath.GetAssetLegacyPath(version.AssetId ?? "legacy") },
130108
{ "auth_session" , session.AccessToken },
131109
{ "version_type" , useNotNull(LaunchOption.VersionType, version.TypeStr) }
132110
};
133111

112+
// JVM argument
113+
114+
// version-specific jvm arguments
115+
if (version.JvmArguments != null)
116+
args.AddRange(Mapper.MapInterpolation(version.JvmArguments, argDict));
117+
118+
// default jvm arguments
119+
if (LaunchOption.JVMArguments != null)
120+
args.AddRange(LaunchOption.JVMArguments);
121+
else
122+
{
123+
if (LaunchOption.MaximumRamMb > 0)
124+
args.Add("-Xmx" + LaunchOption.MaximumRamMb + "m");
125+
126+
if (LaunchOption.MinimumRamMb > 0)
127+
args.Add("-Xms" + LaunchOption.MinimumRamMb + "m");
128+
129+
args.AddRange(DefaultJavaParameter);
130+
}
131+
132+
if (version.JvmArguments == null)
133+
{
134+
args.Add("-Djava.library.path=" + handleEmpty(nativePath));
135+
args.Add("-cp " + classpath);
136+
}
137+
138+
// for macOS
139+
if (!string.IsNullOrEmpty(LaunchOption.DockName))
140+
args.Add("-Xdock:name=" + handleEmpty(LaunchOption.DockName));
141+
if (!string.IsNullOrEmpty(LaunchOption.DockIcon))
142+
args.Add("-Xdock:icon=" + handleEmpty(LaunchOption.DockIcon));
143+
144+
// main class
145+
if (!string.IsNullOrEmpty(version.MainClass))
146+
args.Add(version.MainClass);
147+
148+
// game arguments
134149
if (version.GameArguments != null)
135-
args.AddRange(Mapper.MapInterpolation(version.GameArguments, gameDict));
150+
args.AddRange(Mapper.MapInterpolation(version.GameArguments, argDict));
136151
else if (!string.IsNullOrEmpty(version.MinecraftArguments))
137-
args.AddRange(Mapper.MapInterpolation(version.MinecraftArguments.Split(' '), gameDict));
152+
args.AddRange(Mapper.MapInterpolation(version.MinecraftArguments.Split(' '), argDict));
138153

139-
// Options
154+
// options
140155
if (!string.IsNullOrEmpty(LaunchOption.ServerIp))
141156
{
142157
args.Add("--server " + handleEmpty(LaunchOption.ServerIp));

CmlLib/Core/Launcher/MNative.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using CmlLib.Core.Version;
1+
using System;
2+
using System.Diagnostics;
3+
using CmlLib.Core.Version;
24
using CmlLib.Utils;
35
using System.IO;
46

@@ -25,22 +27,16 @@ public string ExtractNatives()
2527

2628
foreach (var item in version.Libraries)
2729
{
28-
try
30+
// do not ignore exception
31+
if (item.IsRequire && item.IsNative && !string.IsNullOrEmpty(item.Path))
2932
{
30-
if (item.IsRequire && item.IsNative && !string.IsNullOrEmpty(item.Path))
33+
string zPath = Path.Combine(gamePath.Library, item.Path);
34+
if (File.Exists(zPath))
3135
{
32-
string zPath = Path.Combine(gamePath.Library, item.Path);
33-
if (File.Exists(zPath))
34-
{
35-
var z = new SharpZip(zPath);
36-
z.Unzip(path);
37-
}
36+
var z = new SharpZip(zPath);
37+
z.Unzip(path);
3838
}
3939
}
40-
catch
41-
{
42-
// ignore invalid native library file
43-
}
4440
}
4541

4642
return path;

CmlLib/Core/MRule.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,8 @@ static MRule()
2525

2626
private static string getOSName()
2727
{
28-
// Environment.OSVersion.Platform does not work in NET Core
29-
#if NETCOREAPP
30-
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
31-
return OSX;
32-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
33-
return Windows;
34-
else
35-
return Linux;
36-
#elif NETFRAMEWORK
28+
// RuntimeInformation does not work in .NET Framework
29+
#if NETFRAMEWORK
3730
var osType = Environment.OSVersion.Platform;
3831

3932
if (osType == PlatformID.MacOSX)
@@ -42,6 +35,13 @@ private static string getOSName()
4235
return Linux;
4336
else
4437
return Windows;
38+
#else
39+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
40+
return OSX;
41+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
42+
return Windows;
43+
else
44+
return Linux;
4545
#endif
4646
}
4747

CmlLib/Core/Mapper.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace CmlLib.Core
77
{
88
public static class Mapper
99
{
10-
private static Regex argBracket = new Regex(@"\$?\{(.*?)}");
10+
private static readonly Regex argBracket = new Regex(@"\$?\{(.*?)}");
1111

1212
public static string[] Map(string[] arg, Dictionary<string, string?> dicts, string prepath)
1313
{
@@ -51,33 +51,24 @@ public static string[] MapPathString(string[] arg, string prepath)
5151

5252
public static string Interpolation(string str, Dictionary<string, string?> dicts)
5353
{
54-
var sb = new StringBuilder(str);
55-
56-
var offset = 0;
57-
var m = argBracket.Matches(str);
58-
foreach (Match? item in m)
54+
str = argBracket.Replace(str, new MatchEvaluator((match =>
5955
{
60-
if (item == null || item.Groups.Count < 2)
61-
continue;
62-
63-
var outGroup = item.Groups[0];
64-
65-
string key = item.Groups[1].Value;
66-
string? value;
56+
if (match.Groups.Count < 2)
57+
return match.Value;
6758

68-
if (dicts.TryGetValue(key, out value))
59+
var key = match.Groups[1].Value;
60+
if (dicts.TryGetValue(key, out string? value))
6961
{
7062
if (value == null)
71-
value = "";
72-
73-
replaceByPos(sb, value, outGroup.Index + offset, outGroup.Length);
63+
value = "";
7464

75-
if (outGroup.Length != value.Length)
76-
offset = value.Length - outGroup.Length;
65+
return value;
7766
}
78-
}
7967

80-
return sb.ToString();
68+
return match.Value;
69+
})));
70+
71+
return str;
8172
}
8273

8374
public static string ToFullPath(string str, string prepath)

0 commit comments

Comments
 (0)