Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed Oct 4, 2021
2 parents f5c8d17 + ee2753a commit 5ce09b0
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 146 deletions.
18 changes: 12 additions & 6 deletions CmlLib/CmlLib.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net462;net5.0</TargetFrameworks>
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<Version>3.3.2</Version>
<Version>3.3.3</Version>
<Description>Minecraft Launcher Library for .NET
Support all version, forge, optifine
</Description>
Expand All @@ -19,22 +19,28 @@ Support all version, forge, optifine
<Authors>AlphaBs</Authors>
<PackageReleaseNotes />
<PackageId>CmlLib.Core</PackageId>

<WeaverConfiguration Condition="'$(Configuration)' == 'Debug'">
<Weavers>
<MethodTimer/>
</Weavers>
</WeaverConfiguration>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Fody" Version="6.5.1">
<PackageReference Include="Fody" Version="6.5.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="LZMA-SDK" Version="19.0.0" />
<PackageReference Include="MethodTimer.Fody" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SharpZipLib" Version="1.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<None Include="../icon.png" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions CmlLib/Core/Auth/MLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private MLoginResponse parseSession(string json, string? clientToken)
AccessToken = job["accessToken"]?.ToString(),
UUID = profile["id"]?.ToString(),
Username = profile["name"]?.ToString(),
UserType = "Mojang",
ClientToken = clientToken
};

Expand Down
9 changes: 6 additions & 3 deletions CmlLib/Core/Auth/MSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public class MSession
{
public MSession() { }

public MSession(string? username, string? accesstoken, string? uuid)
public MSession(string? username, string? accessToken, string? uuid)
{
Username = username;
AccessToken = accesstoken;
AccessToken = accessToken;
UUID = uuid;
}

Expand All @@ -21,6 +21,8 @@ public MSession(string? username, string? accesstoken, string? uuid)
public string? UUID { get; set; }
[JsonProperty("clientToken")]
public string? ClientToken { get; set; }

public string? UserType { get; set; }

public bool CheckIsValid()
{
Expand All @@ -35,7 +37,8 @@ public static MSession GetOfflineSession(string username)
{
Username = username,
AccessToken = "access_token",
UUID = "user_uuid",
UUID = "user_uuid",
UserType = "Mojang",
ClientToken = null
};
}
Expand Down
7 changes: 5 additions & 2 deletions CmlLib/Core/Downloader/DownloadFileChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ public enum MFile { Runtime, Library, Resource, Minecraft, Others }

public class DownloadFileChangedEventArgs : EventArgs
{
public DownloadFileChangedEventArgs(MFile kind, object source, string? filename, int total, int progressed)
public DownloadFileChangedEventArgs(MFile type, object source, string? filename, int total, int progressed)
{
FileKind = kind;
FileKind = type;
FileType = type;
FileName = filename;
TotalFileCount = total;
ProgressedFileCount = progressed;
Source = source;
}

// FileType and FileKind is exactly same.
public MFile FileType { get; private set; }
public MFile FileKind { get; private set; }
public string? FileName { get; private set; }
public int TotalFileCount { get; private set; }
Expand Down
115 changes: 65 additions & 50 deletions CmlLib/Core/Launcher/MLaunch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class MLaunch
{
private const int DefaultServerPort = 25565;

public static readonly string SupportVersion = "1.16.6";
public static readonly string SupportVersion = "1.17.1";
public readonly static string[] DefaultJavaParameter =
{
"-XX:+UnlockExperimentalVMOptions",
Expand All @@ -21,6 +21,7 @@ public class MLaunch
"-XX:G1ReservePercent=20",
"-XX:MaxGCPauseMillis=50",
"-XX:G1HeapRegionSize=16M"
// "-Xss1M"
};

public MLaunch(MLaunchOption option)
Expand Down Expand Up @@ -48,33 +49,8 @@ public Process GetProcess()
return mc;
}

[MethodTimer.Time]
public string[] CreateArg()
private string createClassPath(MVersion version)
{
MVersion version = LaunchOption.GetStartVersion();

var args = new List<string>();

// Common JVM Arguments
if (LaunchOption.JVMArguments != null)
args.AddRange(LaunchOption.JVMArguments);
else
{
args.AddRange(DefaultJavaParameter);

if (LaunchOption.MaximumRamMb > 0)
args.Add("-Xmx" + LaunchOption.MaximumRamMb + "m");

if (LaunchOption.MinimumRamMb > 0)
args.Add("-Xms" + LaunchOption.MinimumRamMb + "m");
}

if (!string.IsNullOrEmpty(LaunchOption.DockName))
args.Add("-Xdock:name=" + handleEmpty(LaunchOption.DockName));
if (!string.IsNullOrEmpty(LaunchOption.DockIcon))
args.Add("-Xdock:icon=" + handleEmpty(LaunchOption.DockIcon));

// Version-specific JVM Arguments
var classpath = new List<string>(version.Libraries?.Length ?? 1);

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

var classpathStr = IOUtil.CombinePath(classpath.ToArray());
return classpathStr;
}

private string createNativePath(MVersion version)
{
var native = new MNative(minecraftPath, version);
native.CleanNatives();
var nativePath = native.ExtractNatives();
return nativePath;
}

var jvmdict = new Dictionary<string, string?>
[MethodTimer.Time]
public string[] CreateArg()
{
MVersion version = LaunchOption.GetStartVersion();
var args = new List<string>();

var classpath = createClassPath(version);
var nativePath = createNativePath(version);
var session = LaunchOption.GetSession();

var argDict = new Dictionary<string, string?>
{
{ "library_directory", minecraftPath.Library },
{ "natives_directory", nativePath },
{ "launcher_name", useNotNull(LaunchOption.GameLauncherName, "minecraft-launcher") },
{ "launcher_version", useNotNull(LaunchOption.GameLauncherVersion, "2") },
{ "classpath", classpathStr }
};

if (version.JvmArguments != null)
args.AddRange(Mapper.MapInterpolation(version.JvmArguments, jvmdict));
else
{
args.Add("-Djava.library.path=" + handleEmpty(nativePath));
args.Add("-cp " + classpathStr);
}

if (!string.IsNullOrEmpty(version.MainClass))
args.Add(version.MainClass);

// Game Arguments
MSession session = LaunchOption.GetSession();
var gameDict = new Dictionary<string, string?>
{
{ "classpath_separator", Path.PathSeparator.ToString() },
{ "classpath", classpath },

{ "auth_player_name" , session.Username },
{ "version_name" , version.Id },
{ "game_directory" , minecraftPath.BasePath },
Expand All @@ -125,18 +103,55 @@ public string[] CreateArg()
{ "auth_uuid" , session.UUID },
{ "auth_access_token", session.AccessToken },
{ "user_properties" , "{}" },
{ "user_type" , "Mojang" },
{ "user_type" , session.UserType ?? "Mojang" },
{ "game_assets" , minecraftPath.GetAssetLegacyPath(version.AssetId ?? "legacy") },
{ "auth_session" , session.AccessToken },
{ "version_type" , useNotNull(LaunchOption.VersionType, version.TypeStr) }
};

// JVM argument

// version-specific jvm arguments
if (version.JvmArguments != null)
args.AddRange(Mapper.MapInterpolation(version.JvmArguments, argDict));

// default jvm arguments
if (LaunchOption.JVMArguments != null)
args.AddRange(LaunchOption.JVMArguments);
else
{
if (LaunchOption.MaximumRamMb > 0)
args.Add("-Xmx" + LaunchOption.MaximumRamMb + "m");

if (LaunchOption.MinimumRamMb > 0)
args.Add("-Xms" + LaunchOption.MinimumRamMb + "m");

args.AddRange(DefaultJavaParameter);
}

if (version.JvmArguments == null)
{
args.Add("-Djava.library.path=" + handleEmpty(nativePath));
args.Add("-cp " + classpath);
}

// for macOS
if (!string.IsNullOrEmpty(LaunchOption.DockName))
args.Add("-Xdock:name=" + handleEmpty(LaunchOption.DockName));
if (!string.IsNullOrEmpty(LaunchOption.DockIcon))
args.Add("-Xdock:icon=" + handleEmpty(LaunchOption.DockIcon));

// main class
if (!string.IsNullOrEmpty(version.MainClass))
args.Add(version.MainClass);

// game arguments
if (version.GameArguments != null)
args.AddRange(Mapper.MapInterpolation(version.GameArguments, gameDict));
args.AddRange(Mapper.MapInterpolation(version.GameArguments, argDict));
else if (!string.IsNullOrEmpty(version.MinecraftArguments))
args.AddRange(Mapper.MapInterpolation(version.MinecraftArguments.Split(' '), gameDict));
args.AddRange(Mapper.MapInterpolation(version.MinecraftArguments.Split(' '), argDict));

// Options
// options
if (!string.IsNullOrEmpty(LaunchOption.ServerIp))
{
args.Add("--server " + handleEmpty(LaunchOption.ServerIp));
Expand Down
22 changes: 9 additions & 13 deletions CmlLib/Core/Launcher/MNative.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CmlLib.Core.Version;
using System;
using System.Diagnostics;
using CmlLib.Core.Version;
using CmlLib.Utils;
using System.IO;

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

foreach (var item in version.Libraries)
{
try
// do not ignore exception
if (item.IsRequire && item.IsNative && !string.IsNullOrEmpty(item.Path))
{
if (item.IsRequire && item.IsNative && !string.IsNullOrEmpty(item.Path))
string zPath = Path.Combine(gamePath.Library, item.Path);
if (File.Exists(zPath))
{
string zPath = Path.Combine(gamePath.Library, item.Path);
if (File.Exists(zPath))
{
var z = new SharpZip(zPath);
z.Unzip(path);
}
var z = new SharpZip(zPath);
z.Unzip(path);
}
}
catch
{
// ignore invalid native library file
}
}

return path;
Expand Down
18 changes: 9 additions & 9 deletions CmlLib/Core/MRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ static MRule()

private static string getOSName()
{
// Environment.OSVersion.Platform does not work in NET Core
#if NETCOREAPP
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return OSX;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return Windows;
else
return Linux;
#elif NETFRAMEWORK
// RuntimeInformation does not work in .NET Framework
#if NETFRAMEWORK
var osType = Environment.OSVersion.Platform;

if (osType == PlatformID.MacOSX)
Expand All @@ -42,6 +35,13 @@ private static string getOSName()
return Linux;
else
return Windows;
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return OSX;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return Windows;
else
return Linux;
#endif
}

Expand Down
33 changes: 12 additions & 21 deletions CmlLib/Core/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CmlLib.Core
{
public static class Mapper
{
private static Regex argBracket = new Regex(@"\$?\{(.*?)}");
private static readonly Regex argBracket = new Regex(@"\$?\{(.*?)}");

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

public static string Interpolation(string str, Dictionary<string, string?> dicts)
{
var sb = new StringBuilder(str);

var offset = 0;
var m = argBracket.Matches(str);
foreach (Match? item in m)
str = argBracket.Replace(str, new MatchEvaluator((match =>
{
if (item == null || item.Groups.Count < 2)
continue;

var outGroup = item.Groups[0];

string key = item.Groups[1].Value;
string? value;
if (match.Groups.Count < 2)
return match.Value;

if (dicts.TryGetValue(key, out value))
var key = match.Groups[1].Value;
if (dicts.TryGetValue(key, out string? value))
{
if (value == null)
value = "";

replaceByPos(sb, value, outGroup.Index + offset, outGroup.Length);
value = "";

if (outGroup.Length != value.Length)
offset = value.Length - outGroup.Length;
return value;
}
}

return sb.ToString();
return match.Value;
})));

return str;
}

public static string ToFullPath(string str, string prepath)
Expand Down
Loading

0 comments on commit 5ce09b0

Please sign in to comment.