Skip to content

Commit 6252a17

Browse files
committed
feat: add common files
1 parent 8de28be commit 6252a17

File tree

6 files changed

+244
-0
lines changed

6 files changed

+244
-0
lines changed

Auth/MSession.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace CmlLib.Core.Auth;
4+
5+
public class MSession
6+
{
7+
public MSession() { }
8+
9+
public MSession(string? username, string? accessToken, string? uuid)
10+
{
11+
Username = username;
12+
AccessToken = accessToken;
13+
UUID = uuid;
14+
}
15+
16+
[JsonPropertyName("username")]
17+
public string? Username { get; set; }
18+
[JsonPropertyName("session")]
19+
public string? AccessToken { get; set; }
20+
[JsonPropertyName("uuid")]
21+
public string? UUID { get; set; }
22+
[JsonPropertyName("clientToken")]
23+
public string? ClientToken { get; set; }
24+
[JsonPropertyName("userType")]
25+
public string? UserType { get; set; }
26+
[JsonPropertyName("xuid")]
27+
public string? Xuid { get; set; }
28+
29+
public bool CheckIsValid()
30+
{
31+
return !string.IsNullOrEmpty(Username)
32+
&& !string.IsNullOrEmpty(AccessToken)
33+
&& !string.IsNullOrEmpty(UUID);
34+
}
35+
36+
public static MSession CreateLegacyOfflineSession(string username)
37+
{
38+
return new MSession
39+
{
40+
Username = username,
41+
AccessToken = "access_token",
42+
UUID = "user_uuid",
43+
UserType = "Mojang",
44+
ClientToken = null
45+
};
46+
}
47+
48+
public static MSession CreateOfflineSession(string username)
49+
{
50+
return new MSession
51+
{
52+
Username = username,
53+
AccessToken = "access_token",
54+
UUID = Guid.NewGuid().ToString().Replace("-", ""), // create random valid UUID
55+
UserType = "msa",
56+
ClientToken = null
57+
};
58+
}
59+
60+
// legacy api
61+
[Obsolete("Use CreateOfflineSession(\"username\") instead.")]
62+
public static MSession GetOfflineSession(string username) => CreateOfflineSession(username);
63+
}

ByteProgress.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace CmlLib.Core;
2+
3+
public readonly struct ByteProgress
4+
{
5+
public ByteProgress(long totalBytes, long progressedBytes)
6+
{
7+
TotalBytes = totalBytes;
8+
ProgressedBytes = progressedBytes;
9+
}
10+
11+
public readonly long TotalBytes;
12+
public readonly long ProgressedBytes;
13+
14+
public double ToRatio() => (double)ProgressedBytes / TotalBytes;
15+
16+
public static ByteProgress operator +(ByteProgress a, ByteProgress b)
17+
{
18+
return new ByteProgress(a.TotalBytes + b.TotalBytes, a.ProgressedBytes + b.ProgressedBytes);
19+
}
20+
21+
public static ByteProgress operator -(ByteProgress a, ByteProgress b)
22+
{
23+
return new ByteProgress(a.TotalBytes - b.TotalBytes, a.ProgressedBytes - b.ProgressedBytes);
24+
}
25+
}

CmlLib.Core.Commons.csproj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>12.0</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<OutputType>Library</OutputType>
8+
<Nullable>enable</Nullable>
9+
<Version>4.0.0-beta.1</Version>
10+
<Copyright>Copyright (c) 2024 AlphaBs</Copyright>
11+
<PackageProjectUrl>https://github.com/CmlLib/CmlLib.Core.Commons</PackageProjectUrl>
12+
<RepositoryUrl>https://github.com/CmlLib/CmlLib.Core.Commons</RepositoryUrl>
13+
<PackageIcon>icon.png</PackageIcon>
14+
<RepositoryType>git</RepositoryType>
15+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
16+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
17+
<Authors>AlphaBs</Authors>
18+
<PackageReleaseNotes />
19+
<PackageId>CmlLib.Core.Commons</PackageId>
20+
</PropertyGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="System.Text.Json" Version="8.0.3" />
24+
<None Include="icon.png" Pack="true" Visible="false" PackagePath="" />
25+
</ItemGroup>
26+
27+
</Project>

CmlLib.Core.Commons.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CmlLib.Core.Commons", "CmlLib.Core.Commons.csproj", "{559D4E64-5F2F-42CE-8CA8-1075890EE3F1}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{559D4E64-5F2F-42CE-8CA8-1075890EE3F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{559D4E64-5F2F-42CE-8CA8-1075890EE3F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{559D4E64-5F2F-42CE-8CA8-1075890EE3F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{559D4E64-5F2F-42CE-8CA8-1075890EE3F1}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {64817A12-9DCD-4BFA-BB81-1C5A75CDD4E0}
24+
EndGlobalSection
25+
EndGlobal

MinecraftPath.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace CmlLib.Core;
4+
5+
public class MinecraftPath
6+
{
7+
public static readonly string
8+
MacDefaultPath = Environment.GetEnvironmentVariable("HOME") + "/Library/Application Support/minecraft",
9+
LinuxDefaultPath = Environment.GetEnvironmentVariable("HOME") + "/.minecraft",
10+
WindowsDefaultPath = Environment.GetEnvironmentVariable("appdata") + "\\.minecraft";
11+
12+
public static string GetOSDefaultPath()
13+
{
14+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
15+
return MacDefaultPath;
16+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
17+
return WindowsDefaultPath;
18+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
19+
return LinuxDefaultPath;
20+
else
21+
return Environment.CurrentDirectory;
22+
}
23+
24+
public MinecraftPath() : this(GetOSDefaultPath())
25+
{
26+
27+
}
28+
29+
public MinecraftPath(string basePath) : this(basePath, basePath)
30+
{
31+
32+
}
33+
34+
public MinecraftPath(string basePath, string basePathForAssets)
35+
{
36+
BasePath = NormalizePath(basePath);
37+
38+
Library = NormalizePath(BasePath + "/libraries");
39+
Versions = NormalizePath(BasePath + "/versions");
40+
Resource = NormalizePath(BasePath + "/resources");
41+
42+
Runtime = NormalizePath(BasePath + "/runtime");
43+
Assets = NormalizePath(basePathForAssets + "/assets");
44+
}
45+
46+
public void CreateDirs()
47+
{
48+
Dir(BasePath);
49+
Dir(Library);
50+
Dir(Versions);
51+
Dir(Resource);
52+
Dir(Runtime);
53+
Dir(Assets);
54+
}
55+
56+
public string BasePath { get; set; }
57+
public string Library { get; set; }
58+
public string Versions { get; set; }
59+
public string Resource { get; set; }
60+
public string Assets { get; set; }
61+
public string Runtime { get; set; }
62+
63+
public virtual string GetIndexFilePath(string assetId)
64+
=> NormalizePath($"{Assets}/indexes/{assetId}.json");
65+
66+
public virtual string GetAssetObjectPath(string assetId)
67+
=> NormalizePath($"{Assets}/objects");
68+
69+
public virtual string GetAssetLegacyPath(string assetId)
70+
=> NormalizePath($"{Assets}/virtual/legacy");
71+
72+
public virtual string GetLogConfigFilePath(string configId)
73+
=> NormalizePath($"{Assets}/log_configs/{configId}" + (!configId.EndsWith(".xml") ? ".xml" : ""));
74+
75+
public virtual string GetVersionJarPath(string id)
76+
=> NormalizePath($"{Versions}/{id}/{id}.jar");
77+
78+
public virtual string GetVersionJsonPath(string id)
79+
=> NormalizePath($"{Versions}/{id}/{id}.json");
80+
81+
public virtual string GetNativePath(string id)
82+
=> NormalizePath($"{Versions}/{id}/natives");
83+
84+
public override string ToString()
85+
{
86+
return BasePath;
87+
}
88+
89+
protected static string Dir(string path)
90+
{
91+
string p = NormalizePath(path);
92+
if (!Directory.Exists(p))
93+
Directory.CreateDirectory(p);
94+
95+
return p;
96+
}
97+
98+
protected static string NormalizePath(string path)
99+
{
100+
return Path.GetFullPath(path)
101+
.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)
102+
.TrimEnd(Path.DirectorySeparatorChar);
103+
}
104+
}

icon.png

4.67 KB
Loading

0 commit comments

Comments
 (0)