-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
247 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
Data/MineSharp.Data/Framework/INameAndProtocolNumberIndexedData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using MineSharp.Core.Common; | ||
|
||
namespace MineSharp.Data.Framework; | ||
|
||
/// <summary> | ||
/// Interface to implement indexed data, where a single entry does not | ||
/// contain any other data besides a name and a corresponding protocol number. | ||
/// </summary> | ||
public interface INameAndProtocolNumberIndexedData | ||
{ | ||
/// <summary> | ||
/// The number of data entries | ||
/// </summary> | ||
public int Count { get; } | ||
|
||
/// <summary> | ||
/// Return the protocol number associated with the given identifier. | ||
/// </summary> | ||
/// <param name="name"></param> | ||
/// <returns></returns> | ||
public int GetProtocolId(Identifier name); | ||
|
||
/// <summary> | ||
/// Return the <see cref="Identifier"/> associated with the given protocol number. | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
public Identifier GetName(int id); | ||
} | ||
|
||
/// <inheritdoc cref="INameAndProtocolNumberIndexedData"/> | ||
/// <typeparam name="TEnum"></typeparam> | ||
public interface INameAndProtocolNumberIndexedData<TEnum> : INameAndProtocolNumberIndexedData | ||
{ | ||
/// <summary> | ||
/// Return the protocol number associated with the given <typeparamref name="TEnum"/> | ||
/// </summary> | ||
/// <param name="type"></param> | ||
/// <returns></returns> | ||
public int GetProtocolId(TEnum type); | ||
|
||
/// <summary> | ||
/// Return <typeparam name="TEnum"></typeparam> associated with the given protocol number. | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
public TEnum GetType(int id); | ||
} |
82 changes: 82 additions & 0 deletions
82
Data/MineSharp.Data/Internal/NameAndProtocolNumberIndexedData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Collections.Frozen; | ||
using MineSharp.Core.Common; | ||
using MineSharp.Data.Framework; | ||
using MineSharp.Data.Framework.Providers; | ||
|
||
namespace MineSharp.Data.Internal; | ||
|
||
internal class NameAndProtocolNumberIndexedData(IDataProvider<IReadOnlyDictionary<Identifier, int>> provider) | ||
: IndexedData<IReadOnlyDictionary<Identifier, int>>(provider), INameAndProtocolNumberIndexedData | ||
{ | ||
public int Count { get; private set; } | ||
|
||
protected IReadOnlyDictionary<int, Identifier>? ProtocolNumberToName; | ||
protected IReadOnlyDictionary<Identifier, int>? NameToProtocolNumber; | ||
|
||
protected override void InitializeData(IReadOnlyDictionary<Identifier, int> data) | ||
{ | ||
Count = data.Count; | ||
NameToProtocolNumber = data.ToFrozenDictionary(); | ||
ProtocolNumberToName = NameToProtocolNumber.ToFrozenDictionary(x => x.Value, x => x.Key); | ||
} | ||
|
||
public int GetProtocolId(Identifier name) | ||
{ | ||
if (!Loaded) | ||
{ | ||
Load(); | ||
} | ||
|
||
return NameToProtocolNumber![name]; | ||
} | ||
|
||
public Identifier GetName(int id) | ||
{ | ||
if (!Loaded) | ||
{ | ||
Load(); | ||
} | ||
|
||
return ProtocolNumberToName![id]; | ||
} | ||
} | ||
|
||
internal class NameAndProtocolNumberIndexedData<TEnum>(IDataProvider<IReadOnlyDictionary<Identifier, int>> provider) | ||
: NameAndProtocolNumberIndexedData(provider), INameAndProtocolNumberIndexedData<TEnum> | ||
where TEnum : struct, Enum | ||
{ | ||
private readonly EnumNameLookup<TEnum> enumNameLookup = new(); | ||
private IReadOnlyDictionary<int, TEnum>? protocolNumberToType; | ||
private IReadOnlyDictionary<TEnum, int>? typeToProtocolNumber; | ||
|
||
protected override void InitializeData(IReadOnlyDictionary<Identifier, int> data) | ||
{ | ||
base.InitializeData(data); | ||
|
||
protocolNumberToType = ProtocolNumberToName! | ||
.ToFrozenDictionary( | ||
x => x.Key, | ||
x => enumNameLookup.FromName(NameUtils.GetParticleName(x.Value.Name))); | ||
typeToProtocolNumber = protocolNumberToType.ToFrozenDictionary(x => x.Value, x => x.Key); | ||
} | ||
|
||
public int GetProtocolId(TEnum type) | ||
{ | ||
if (!Loaded) | ||
{ | ||
Load(); | ||
} | ||
|
||
return typeToProtocolNumber![type]; | ||
} | ||
|
||
public TEnum GetType(int id) | ||
{ | ||
if (!Loaded) | ||
{ | ||
Load(); | ||
} | ||
|
||
return protocolNumberToType![id]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using MineSharp.Core.Common; | ||
using MineSharp.Core.Common.Particles; | ||
Check failure on line 2 in Data/MineSharp.Data/Particles/ParticleData.cs GitHub Actions / build
|
||
using MineSharp.Data.Framework; | ||
using MineSharp.Data.Framework.Providers; | ||
using MineSharp.Data.Internal; | ||
|
||
namespace MineSharp.Data.Particles; | ||
|
||
internal class ParticleData(IDataProvider<IReadOnlyDictionary<Identifier, int>> provider) | ||
: NameAndProtocolNumberIndexedData<ParticleType>(provider), IParticleData; | ||
Check failure on line 10 in Data/MineSharp.Data/Particles/ParticleData.cs GitHub Actions / build
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Frozen; | ||
using MineSharp.Core.Common; | ||
using MineSharp.Data.Framework.Providers; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace MineSharp.Data.Particles; | ||
|
||
internal class ParticleDataProvider : IDataProvider<IReadOnlyDictionary<Identifier, int>> | ||
{ | ||
private readonly JArray token; | ||
|
||
public ParticleDataProvider(JToken token) | ||
{ | ||
if (token.Type != JTokenType.Array) | ||
{ | ||
throw new ArgumentException("Expected the token to be an array"); | ||
} | ||
|
||
this.token = (token as JArray)!; | ||
} | ||
|
||
public IReadOnlyDictionary<Identifier, int> GetData() | ||
{ | ||
return token | ||
.Select(FromToken) | ||
.ToFrozenDictionary(x => x.Key, x => x.Value); | ||
} | ||
|
||
private static KeyValuePair<Identifier, int> FromToken(JToken token) | ||
{ | ||
var name = (string)token["name"]!; | ||
var id = (int)token["id"]!; | ||
|
||
return new (Identifier.Parse(name), id); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Data/MineSharp.SourceGenerator/Generators/ParticleGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using MineSharp.SourceGenerator.Utils; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace MineSharp.SourceGenerator.Generators; | ||
|
||
public class ParticleGenerator | ||
{ | ||
private readonly Generator typeGenerator = new("particles", GetName, "ParticleType", "Particles"); | ||
|
||
public Task Run(MinecraftDataWrapper wrapper) | ||
{ | ||
return Task.WhenAll( | ||
typeGenerator.Generate(wrapper)); | ||
} | ||
|
||
private static string GetName(JToken token) | ||
{ | ||
var name = (string)token.SelectToken("name")!; | ||
return NameUtils.GetParticleName(name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.