Skip to content

Commit

Permalink
merge branch main into feature/pathfinder
Browse files Browse the repository at this point in the history
unfortunately, not much is working anymore
  • Loading branch information
psu-de committed Jul 27, 2024
2 parents 3efa7b9 + 9947b89 commit f6c1fdf
Show file tree
Hide file tree
Showing 410 changed files with 15,248 additions and 11,323 deletions.
198 changes: 180 additions & 18 deletions .editorconfig

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- Minecraft version: [e.g. 1.20.4]
- Server brand: [e.g. Vanilla, Paper, Spigot, ...]

**Additional context**
Add any other context about the problem here.
9 changes: 8 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
MineSharp 0.1.5:
- fixed bugs in MineSharp.Physics
- dynamically download and load data
- a lot of under the hood changes, improving minesharp
- fixed command parsing
- hopefully fixed stuck in connect() method

MineSharp 0.1.4:
- Upgrade MineSharp.fNbt to 1.0.3
- Support Minecraft Java 1.20.3 & 1.20.4
Expand All @@ -18,4 +25,4 @@ MineSharp 0.1.3:
- added Serverbound SetHeldItemPacket
- added EquipItem() methods
- replaced MiNet.fNbt with MineSharp.fNbt
- Support Minecraft Java 1.20.2
- Support Minecraft Java 1.20.2
6 changes: 3 additions & 3 deletions Components/MineSharp.Auth/Cache/CacheManager.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace MineSharp.Auth.Cache;
namespace MineSharp.Auth.Cache;

internal static class CacheManager
{
public static string GetCachePath()
{
string baseFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var baseFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return Path.Join(baseFolder, "MineSharp");
}

public static string Get(string cache)
{
string path = Path.Join(GetCachePath(), cache);
var path = Path.Join(GetCachePath(), cache);

if (!Directory.Exists(path))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using MineSharp.Core.Exceptions;
using MineSharp.Core.Exceptions;

namespace MineSharp.Auth.Exceptions;

/// <summary>
/// Thrown when MineSharp could not authenticate.
/// Thrown when MineSharp could not authenticate.
/// </summary>
/// <param name="message"></param>
public class MineSharpAuthException(string message) : MineSharpException(message);
5 changes: 2 additions & 3 deletions Components/MineSharp.Auth/Json/CertificateBlob.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using Newtonsoft.Json;
using Newtonsoft.Json;

namespace MineSharp.Auth.Json;

#pragma warning disable CS8618
internal class CertificateBlob
{
[JsonProperty("expiresAt")] public string ExpiresAt;
[JsonProperty("KeyPair")] public KeyPairBlob KeyPair;

[JsonProperty("publicKeySignature")] public string PublicKeySignature;

[JsonProperty("publicKeySignatureV2")] public string PublicKeySignatureV2;

[JsonProperty("expiresAt")] public string ExpiresAt;

[JsonProperty("refreshedAfter")] public string RefreshedAfter;
}

Expand Down
2 changes: 1 addition & 1 deletion Components/MineSharp.Auth/Json/JoinServerBlob.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;

namespace MineSharp.Auth.Json;

Expand Down
66 changes: 39 additions & 27 deletions Components/MineSharp.Auth/MicrosoftAuth.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using CmlLib.Core.Auth;
using CmlLib.Core.Auth.Microsoft;
using CmlLib.Core.Auth.Microsoft.MsalClient;
Expand All @@ -7,47 +11,54 @@
using MineSharp.Auth.Responses;
using MineSharp.Core.Common;
using NLog;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;

namespace MineSharp.Auth;

/// <summary>
/// Login to Microsoft services.
/// Login to Microsoft services.
/// </summary>
public static class MicrosoftAuth
{
private static readonly string ClientID = "3dff3eb7-2830-4d92-b2cb-033c3f47dce0";
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();


/// <summary>
/// How to handle microsoft msal authentication.
/// How to handle microsoft msal authentication.
/// </summary>
public delegate void DeviceCodeHandler(DeviceCodeResult deviceCode);

private static readonly string ClientId = "3dff3eb7-2830-4d92-b2cb-033c3f47dce0";
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

/// <summary>
/// Obtain a valid minecraft session using a Microsoft Account
/// Obtain a valid minecraft session using a Microsoft Account
/// </summary>
/// <param name="username">The username, only used for caching.</param>
/// <param name="handler">
/// When the user has to login in the browser, handler() is called. It should open up a browser window and show the user the deviceCode.UserCode
/// If none is provided, the link will open up in the default browser and the device code is written to the console
/// When the user has to login in the browser, handler() is called. It should open up a browser window and show the
/// user the deviceCode.UserCode
/// If none is provided, the link will open up in the default browser and the device code is written to the console
/// </param>
/// <param name="api"></param>
/// <returns>A Session instance</returns>
public static async Task<Session> Login(string username, DeviceCodeHandler? handler = null, MinecraftApi? api = null)
public static async Task<Session> Login(string username, DeviceCodeHandler? handler = null,
MinecraftApi? api = null)
{
handler ??= DefaultDeviceCodeHandler;
api ??= new MinecraftApi();
api ??= new();

var cacheFolder = GetCacheForUser(username);

var cacheSettings = new MsalCacheSettings() { CacheDir = cacheFolder };

var app = await MsalMinecraftLoginHelper.BuildApplicationWithCache(ClientID, cacheSettings);
var cacheSettings = new MsalCacheSettings
{
CacheDir = cacheFolder,
CacheFileName = "MineSharp.Secrets.txt",
KeyChainServiceName = "MineSharp",
LinuxKeyRingSchema = "MineSharp",
LinuxKeyRingLabel = "MSAL tokens for MineSharp",
LinuxKeyRingCollection = "default", // default means persistent storage
KeyChainAccountName = username, // used for mac key identification
LinuxKeyRingAttr2 = new("Username", username) // used for linux key identification
};

var app = await MsalMinecraftLoginHelper.BuildApplicationWithCache(ClientId, cacheSettings);
var loginHandler = new LoginHandlerBuilder()
.WithCachePath(Path.Join(cacheFolder, "session.json"))
.ForJavaEdition()
Expand All @@ -59,12 +70,12 @@ public static async Task<Session> Login(string username, DeviceCodeHandler? hand
.Build();

MSession mSession;
bool cached = false;
var cached = false;
try
{
var result = await loginHandler.LoginFromCache();
mSession = result.GameSession;
cached = true;
cached = true;
}
catch (Exception)
{
Expand All @@ -76,7 +87,7 @@ public static async Task<Session> Login(string username, DeviceCodeHandler? hand
{
if (!cached) // If the cached session is invalid, try to get a new session
{
throw new MineSharpAuthException("Could not login to a valid session");
throw new MineSharpAuthException("could not obtain a valid session");
}

var result = await loginHandler.LoginFromOAuth();
Expand All @@ -86,14 +97,14 @@ public static async Task<Session> Login(string username, DeviceCodeHandler? hand
var certificates = PlayerCertificate.Deserialize(cacheFolder);
if (certificates == null || certificates.RequiresRefresh())
{
Logger.Debug($"Fetching new certificates.");
Logger.Debug("Fetching new certificates.");
certificates = await api.FetchCertificates(mSession.AccessToken!);
certificates.Serialize(cacheFolder);
}

return new Session(
return new(
mSession.Username!,
UUID.Parse(mSession.UUID!),
Uuid.Parse(mSession.UUID!),
mSession.ClientToken!,
mSession.AccessToken!,
true,
Expand All @@ -103,8 +114,8 @@ public static async Task<Session> Login(string username, DeviceCodeHandler? hand
private static string GetCacheForUser(string username)
{
var filename = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(username)));
var cache = CacheManager.Get("Sessions");
var path = Path.Join(cache, filename);
var cache = CacheManager.Get("Sessions");
var path = Path.Join(cache, filename);

if (!Directory.Exists(path))
{
Expand All @@ -122,9 +133,10 @@ private static void DefaultDeviceCodeHandler(DeviceCodeResult result)
}

/// <summary>
/// Open URL
/// Open URL
/// </summary>
/// <param name="url">Url to open</param>

// https://stackoverflow.com/a/43232486/13228835
private static void OpenUrl(string url)
{
Expand Down
70 changes: 35 additions & 35 deletions Components/MineSharp.Auth/MineSharp.Auth.csproj
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<LangVersion>12</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Title>MineSharp.Auth</Title>
<Authors>psu-xde</Authors>
<Description>Authentication package for MineSharp.</Description>
<PackageProjectUrl>https://minesharp.io</PackageProjectUrl>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/psu-de/MineSharp</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>minecraft, authentication, msal</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<LangVersion>12</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Title>MineSharp.Auth</Title>
<Authors>psu-xde</Authors>
<Description>Authentication package for MineSharp.</Description>
<PackageProjectUrl>https://minesharp.io</PackageProjectUrl>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/psu-de/MineSharp</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>minecraft, authentication, msal</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\MineSharp.Core\MineSharp.Core.csproj" />
<ItemGroup>
<ProjectReference Include="..\..\MineSharp.Core\MineSharp.Core.csproj"/>

<None Include="README.md" Pack="true" PackagePath="\" />
<None Include="LICENSE" Pack="true" PackagePath="\" />
<None Include="icon.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\"/>
<None Include="LICENSE" Pack="true" PackagePath="\"/>
<None Include="icon.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="CmlLib.Core.Auth.Microsoft" Version="2.2.0" />
<PackageReference Include="CmlLib.Core.Auth.Microsoft.MsalClient" Version="2.0.0" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CmlLib.Core.Auth.Microsoft" Version="2.2.0"/>
<PackageReference Include="CmlLib.Core.Auth.Microsoft.MsalClient" Version="2.0.0"/>
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
</ItemGroup>

</Project>
Loading

0 comments on commit f6c1fdf

Please sign in to comment.