Skip to content

Commit

Permalink
Improve BlockStateBuilders (#415)
Browse files Browse the repository at this point in the history
* 👍

* Update BlocksGenerator.BlockStateBuilder.cs
  • Loading branch information
Tides authored Jan 18, 2024
1 parent c65013d commit d7c0ec2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
23 changes: 18 additions & 5 deletions Obsidian.API/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System.Diagnostics;
using System.Transactions;

namespace Obsidian.API.Utilities;
public static partial class Extensions
{
public static List<string> GetStateValues(this string key, Dictionary<string, string[]> valueStores)
public static List<string> GetStateValues(this int[] indexes, Dictionary<string, string[]> valueStores)
{
var list = new List<string>();
var vals = key.Split("-");

var count = 0;
foreach (var (_, values) in valueStores)
{
var index = int.Parse(vals[count++]);
var index = indexes[count++];

var value = values[index];

Expand All @@ -21,7 +21,20 @@ public static List<string> GetStateValues(this string key, Dictionary<string, st
return list;
}

public static string GetIndexFromArray(this string[] array, string value)
public static int GetIndexFromJaggedArray(this int[][] array, int[] value)
{
for(int i = 0; i < array.Length; i++)
{
var child = array[i];

if (Enumerable.SequenceEqual(child, value))
return i;
}

return -1;
}

public static int GetIndexFromArray(this string[] array, string value)
{
var propertyValue = bool.TryParse(value, out _) ? value.ToLower() : value;

Expand All @@ -31,7 +44,7 @@ public static string GetIndexFromArray(this string[] array, string value)
for(int i = 0; i < array.Length; i++)
{
if (array[i] == propertyValue)
return $"{i}";
return i;
}

throw new UnreachableException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Obsidian.SourceGenerators.Registry.Models;
using System.Reflection;

namespace Obsidian.SourceGenerators.Registry;
public partial class BlocksGenerator
Expand All @@ -20,17 +21,19 @@ private static void GenerateValueStore(CodeBuilder stateBuilder, BlockProperty[]

private static void GeneratePossibleStates(CodeBuilder stateBuilder, Dictionary<int, List<string>> stateValues, BlockProperty[] properties)
{
stateBuilder.Statement("private Dictionary<string, int> possibleStates = new()");
stateBuilder.Indent().Append("private static ReadOnlySpan<int> StateIds => [");
foreach (var key in stateValues.Keys)
stateBuilder.Append($"{key},");

var list = new List<string>();
foreach (var kv in stateValues)
{
var key = kv.Key;
var values = kv.Value;
stateBuilder.Append("];").Line();

list.Clear();
stateBuilder.Indent().Append("private static int[][] StatePropertyIndexes => [");

foreach (var values in stateValues.Values)
{
var count = 0;

stateBuilder.Append("[");
foreach (var value in values)
{
var property = properties[count++];
Expand All @@ -42,16 +45,15 @@ private static void GeneratePossibleStates(CodeBuilder stateBuilder, Dictionary<

var index = GetPropertyIndex(property.Values, value);

list.Add(index);
stateBuilder.Append($"{index},");
}

stateBuilder.Indent().Append("{ ").Append($"\"{string.Join("-", list)}\", {key}").Append(" },").Line();
stateBuilder.Append("],");
}

stateBuilder.EndScope(true);
stateBuilder.Append("];").Line();
}

private static string GetPropertyIndex(string[] array, string value)
private static int GetPropertyIndex(string[] array, string value)
{
var propertyValue = bool.TryParse(value, out _) ? value.ToLower() : value;

Expand All @@ -61,7 +63,7 @@ private static string GetPropertyIndex(string[] array, string value)
for (int i = 0; i < array.Length; i++)
{
if (array[i] == propertyValue)
return $"{i}";
return i;
}

throw new InvalidOperationException();
Expand All @@ -71,9 +73,10 @@ private static void SetStateFromIdMethod(string fullName, CodeBuilder stateBuild
{
stateBuilder.Line().Line().Method($"public {fullName}(int currentStateId)");

stateBuilder.Line("var (key, _) = this.possibleStates.First(x => x.Value == currentStateId);");
stateBuilder.Line("var arrayIndex = StateIds.IndexOf(currentStateId);");
stateBuilder.Line("var stateIndexesResult = StatePropertyIndexes[arrayIndex];");

stateBuilder.Line("var values = key.GetStateValues(this.valueStore);");
stateBuilder.Line("var values = stateIndexesResult.GetStateValues(this.valueStore);");

var count = 0;
foreach (var property in properties)
Expand Down Expand Up @@ -199,7 +202,7 @@ private static void BuildStateFinder(CodeBuilder stateBuilder, BlockProperty[] p
}
}

stateBuilder.Statement("var list = new List<string>()");
stateBuilder.Line().Line("int[] rawValue = [");

foreach (var property in properties)
{
Expand All @@ -209,8 +212,10 @@ private static void BuildStateFinder(CodeBuilder stateBuilder, BlockProperty[] p
stateBuilder.Line($"this.valueStore[\"{name}\"].GetIndexFromArray(this.{sanitizedName}.ToString()),");
}

stateBuilder.EndScope(true).Line();
stateBuilder.Line("];");

stateBuilder.Line().Line($"var stateIndex = StatePropertyIndexes.GetIndexFromJaggedArray(rawValue);");

stateBuilder.Line().Line($"var stateId = this.possibleStates[string.Join(\"-\", list)];");
stateBuilder.Line().Line($"var stateId = StateIds[stateIndex];");
}
}

0 comments on commit d7c0ec2

Please sign in to comment.