Skip to content

Commit

Permalink
added ParticleData.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Aug 19, 2024
1 parent ca90da8 commit 45ead60
Show file tree
Hide file tree
Showing 22 changed files with 247 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Data/MineSharp.Data.Tests/TestVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using MineSharp.Core.Common.Enchantments;
using MineSharp.Core.Common.Entities;
using MineSharp.Core.Common.Items;
using MineSharp.Core.Common.Particles;
using MineSharp.Data.Protocol;

namespace MineSharp.Data.Tests;
Expand Down Expand Up @@ -46,6 +47,7 @@ public void TestLoadData()
data.Materials.GetMultiplier(Material.Shovel, ItemType.StoneSword);
data.Protocol.GetPacketId(PacketType.CB_Play_Login);
data.Recipes.ByItem(ItemType.DiamondShovel);
data.Particles.GetProtocolId(ParticleType.Composter);
data.Windows.ById(0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Data/MineSharp.Data/Blocks/BlockProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static BlockInfo FromToken(JToken dataToken, IItemData items)

return new(
id,
BlockTypeLookup.FromName(NameUtils.GetBiomeName(name)),
BlockTypeLookup.FromName(NameUtils.GetBlockName(name)),
name,
displayName,
hardness,
Expand Down
6 changes: 6 additions & 0 deletions Data/MineSharp.Data/Framework/DataInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MineSharp.Core.Common.Enchantments;
using MineSharp.Core.Common.Entities;
using MineSharp.Core.Common.Items;
using MineSharp.Core.Common.Particles;

Check failure on line 8 in Data/MineSharp.Data/Framework/DataInterfaces.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Particles' does not exist in the namespace 'MineSharp.Core.Common' (are you missing an assembly reference?)

Check failure on line 8 in Data/MineSharp.Data/Framework/DataInterfaces.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Particles' does not exist in the namespace 'MineSharp.Core.Common' (are you missing an assembly reference?)
using MineSharp.Core.Common.Protocol;
using MineSharp.Core.Common.Recipes;
using MineSharp.Core.Geometry;
Expand Down Expand Up @@ -185,3 +186,8 @@ public interface IWindowData
/// <returns></returns>
public WindowInfo ByName(Identifier name);
}

/// <summary>
/// Interface for implementing particle data
/// </summary>
public interface IParticleData : INameAndProtocolNumberIndexedData<ParticleType>;

Check failure on line 193 in Data/MineSharp.Data/Framework/DataInterfaces.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ParticleType' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 193 in Data/MineSharp.Data/Framework/DataInterfaces.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ParticleType' could not be found (are you missing a using directive or an assembly reference?)
48 changes: 48 additions & 0 deletions Data/MineSharp.Data/Framework/INameAndProtocolNumberIndexedData.cs
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 Data/MineSharp.Data/Internal/NameAndProtocolNumberIndexedData.cs
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];
}
}
5 changes: 5 additions & 0 deletions Data/MineSharp.Data/Internal/NameUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public static string GetGameState(string name)
{
return CommonGetName(name);
}

public static string GetParticleName(string name)
{
return CommonGetName(name);
}

public static string GetPacketName(string name, string direction, string ns)
{
Expand Down
11 changes: 11 additions & 0 deletions Data/MineSharp.Data/MinecraftData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using MineSharp.Data.Items;
using MineSharp.Data.Language;
using MineSharp.Data.Materials;
using MineSharp.Data.Particles;
using MineSharp.Data.Protocol;
using MineSharp.Data.Recipes;
using MineSharp.Data.Windows;
Expand Down Expand Up @@ -50,6 +51,7 @@ private MinecraftData(
IRecipeData recipes,
IWindowData windows,
ILanguageData language,
IParticleData particles,
MinecraftVersion version)
{
Biomes = biomes;
Expand All @@ -64,6 +66,7 @@ private MinecraftData(
Recipes = recipes;
Windows = windows;
Language = language;
Particles = particles;
Version = version;
}

Expand Down Expand Up @@ -126,6 +129,11 @@ private MinecraftData(
/// The language data provider for this version
/// </summary>
public ILanguageData Language { get; }

/// <summary>
/// The particle data for this version
/// </summary>
public IParticleData Particles { get; }

/// <summary>
/// The Minecraft version of this instance
Expand Down Expand Up @@ -165,6 +173,7 @@ public static async Task<MinecraftData> FromVersion(string version)
var materialsToken = await LoadAsset("materials", versionToken);
var recipesToken = await LoadAsset("recipes", versionToken);
var languageToken = await LoadAsset("language", versionToken);
var particleToken = await LoadAsset("particles", versionToken);

var biomes = new BiomeData(new BiomeProvider(biomeToken));
var items = new ItemData(new ItemProvider(itemsToken));
Expand All @@ -177,6 +186,7 @@ public static async Task<MinecraftData> FromVersion(string version)
var materials = new MaterialData(new MaterialsProvider(materialsToken, items));
var recipes = new RecipeData(new(recipesToken, items));
var language = new LanguageData(new LanguageProvider(languageToken));
var particles = new ParticleData(new ParticleDataProvider(particleToken));
var windows = GetWindowData(minecraftVersion);

var data = new MinecraftData(
Expand All @@ -192,6 +202,7 @@ public static async Task<MinecraftData> FromVersion(string version)
recipes,
windows,
language,
particles,
minecraftVersion);

LoadedData.Add(version, data);
Expand Down
10 changes: 10 additions & 0 deletions Data/MineSharp.Data/Particles/ParticleData.cs
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

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Particles' does not exist in the namespace 'MineSharp.Core.Common' (are you missing an assembly reference?)

Check failure on line 2 in Data/MineSharp.Data/Particles/ParticleData.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Particles' does not exist in the namespace 'MineSharp.Core.Common' (are you missing an assembly reference?)
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

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ParticleType' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 10 in Data/MineSharp.Data/Particles/ParticleData.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ParticleType' could not be found (are you missing a using directive or an assembly reference?)
36 changes: 36 additions & 0 deletions Data/MineSharp.Data/Particles/ParticleDataProvider.cs
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 Data/MineSharp.SourceGenerator/Generators/ParticleGenerator.cs
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<LangVersion>12</LangVersion>
<IsPackable>false</IsPackable>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Data/MineSharp.SourceGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{
new BiomeGenerator().Run(data), new BlockGenerator().Run(data), new EffectGenerator().Run(data),
new EnchantmentGenerator().Run(data), new EntityGenerator().Run(data), new ItemGenerator().Run(data),
new ProtocolGenerator().Run(data)
new ProtocolGenerator().Run(data), new ParticleGenerator().Run(data)
};

if (Directory.Exists(DirectoryUtils.GetSourceDirectory()))
Expand Down
5 changes: 5 additions & 0 deletions Data/MineSharp.SourceGenerator/Utils/NameUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public static string GetGameState(string name)
return CommonGetName(name);
}

public static string GetParticleName(string name)
{
return CommonGetName(name);
}

public static string GetPacketName(string name, string direction, string ns)
{
direction = direction == "toClient" ? "CB" : "SB";
Expand Down
4 changes: 2 additions & 2 deletions MineSharp.Core/Common/Biomes/BiomeCategory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// This File is generated by MineSharp.SourceGenerator and should not be modified. //
///////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -26,7 +26,7 @@ public enum BiomeCategory
Mushroom = 15,
Underground = 16,
Nether = 17,
TheEnd = 18
TheEnd = 18,
}

#pragma warning restore CS1591
4 changes: 2 additions & 2 deletions MineSharp.Core/Common/Biomes/BiomeType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// This File is generated by MineSharp.SourceGenerator and should not be modified. //
///////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -71,7 +71,7 @@ public enum BiomeType
EndBarrens = 60,
MangroveSwamp = 61,
DeepDark = 62,
CherryGrove = 63
CherryGrove = 63,
}

#pragma warning restore CS1591
4 changes: 2 additions & 2 deletions MineSharp.Core/Common/Blocks/BlockType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// This File is generated by MineSharp.SourceGenerator and should not be modified. //
///////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -1068,7 +1068,7 @@ public enum BlockType
Crafter = 1057,
TrialSpawner = 1058,
Vault = 1059,
HeavyCore = 1060
HeavyCore = 1060,
}

#pragma warning restore CS1591
Loading

0 comments on commit 45ead60

Please sign in to comment.