Skip to content

Commit

Permalink
rename IParallelCommand to IPureCommand & generate view methods for i…
Browse files Browse the repository at this point in the history
…mmutable container properties
  • Loading branch information
sicusa committed Apr 27, 2024
1 parent c6968b6 commit f3d2b8f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 52 deletions.
139 changes: 100 additions & 39 deletions Sia.CodeGenerators/SiaPropertyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var compTypeParams = componentType.TypeParameterList;

if (componentTypes.Add(componentType)) {
GenerateViewTypeMainDecl(source, compTypeStr, compTypeParams);
GenerateViewMainDecl(source, compTypeStr, compTypeParams);
source.WriteLine();
}

GeneratePropertyCommands(
source, info.Property, compTypeStr, compTypeParams);

source.WriteLine();
GenerateViewTypeProperty(source, info.Property);
GenerateViewProperty(source, info.Property);
}
}

Expand Down Expand Up @@ -152,27 +152,42 @@ public static void GeneratePropertyCommands(

if (info.GetArgument("GenerateAddItemCommand", true)) {
source.WriteLine();
var command = "Add" + itemName;
GenerateImmutableContainerCommand(
source, info, componentType, componentTypeParams,
"Add" + itemName,
source, info, componentType, componentTypeParams, command,
$"{keyType} Key, {valueType} Value",
".Add(Key, Value);");
source.WriteLine();
GenerateImmutableContainerViewMethods(
source, info, componentType, componentTypeParams, command,
$"{keyType} key, {valueType} value",
"key, value");
}
if (info.GetArgument("GenerateSetItemCommand", true)) {
source.WriteLine();
var command = "Set" + itemName;
GenerateImmutableContainerCommand(
source, info, componentType, componentTypeParams,
"Set" + itemName,
source, info, componentType, componentTypeParams, command,
$"{keyType} Key, {valueType} Value",
".SetItem(Key, Value);");
source.WriteLine();
GenerateImmutableContainerViewMethods(
source, info, componentType, componentTypeParams, command,
$"{keyType} key, {valueType} value",
"key, value");
}
if (info.GetArgument("GenerateRemoveItemCommand", true)) {
source.WriteLine();
var command = "Remove" + itemName;
GenerateImmutableContainerCommand(
source, info, componentType, componentTypeParams,
"Remove" + itemName,
source, info, componentType, componentTypeParams, command,
$"{keyType} Key",
".Remove(Key);");
source.WriteLine();
GenerateImmutableContainerViewMethods(
source, info, componentType, componentTypeParams, command,
$"{keyType} key",
"key");
}
break;

Expand All @@ -181,11 +196,16 @@ public static void GeneratePropertyCommands(

if (info.GetArgument("GenerateAddItemCommand", true)) {
source.WriteLine();
var command = "Enqueue" + itemName;
GenerateImmutableContainerCommand(
source, info, componentType, componentTypeParams,
"Enqueue" + itemName,
source, info, componentType, componentTypeParams, command,
$"{valueType} Value",
".Enqueue(Value);");
source.WriteLine();
GenerateImmutableContainerViewMethods(
source, info, componentType, componentTypeParams, command,
$"{valueType} value",
"value");
}
break;

Expand All @@ -194,42 +214,61 @@ public static void GeneratePropertyCommands(

if (info.GetArgument("GenerateAddItemCommand", true)) {
source.WriteLine();
var command = "Push" + itemName;
GenerateImmutableContainerCommand(
source, info, componentType, componentTypeParams,
"Push" + itemName,
source, info, componentType, componentTypeParams, command,
$"{valueType} Value",
".Push(Value);");
source.WriteLine();
GenerateImmutableContainerViewMethods(
source, info, componentType, componentTypeParams, command,
$"{valueType} value",
"value");
}
break;

case "HashSet":
case "List":
case "Array":
valueType = info.TypeArguments[0];

if (info.GetArgument("GenerateAddItemCommand", true)) {
source.WriteLine();
var command = "Add" + itemName;
GenerateImmutableContainerCommand(
source, info, componentType, componentTypeParams,
"Add" + itemName,
source, info, componentType, componentTypeParams, command,
$"{valueType} Value",
".Add(Value);");
source.WriteLine();
GenerateImmutableContainerViewMethods(
source, info, componentType, componentTypeParams, command,
$"{valueType} value",
"value");
}
if (immutableType != "HashSet" && info.GetArgument("GenerateSetItemCommand", true)) {
source.WriteLine();
var command = "Set" + itemName;
GenerateImmutableContainerCommand(
source, info, componentType, componentTypeParams,
"Set" + itemName,
source, info, componentType, componentTypeParams, command,
$"int Index, {valueType} Value",
".SetItem(Index, Value);");
source.WriteLine();
GenerateImmutableContainerViewMethods(
source, info, componentType, componentTypeParams, command,
$"int index, {valueType} value",
"index, value");
}
if (info.GetArgument("GenerateRemoveItemCommand", true)) {
source.WriteLine();
var command = "Remove" + itemName;
GenerateImmutableContainerCommand(
source, info, componentType, componentTypeParams,
"Remove" + itemName,
source, info, componentType, componentTypeParams, command,
$"{valueType} Value",
".Remove(Value);");
source.WriteLine();
GenerateImmutableContainerViewMethods(
source, info, componentType, componentTypeParams, command,
$"{valueType} value",
"value");
}
break;
}
Expand All @@ -255,7 +294,7 @@ void WriteComponentType()
source.Write(info.DisplayType);
source.Write(" Value) : global::Sia.IReconstructableCommand<");
source.Write(commandName);
source.Write(">, global::Sia.IParallelCommand<");
source.Write(">, global::Sia.IPureCommand<");
WriteComponentType();
source.Write(">, global::Sia.IPropertyCommand<");
source.Write(info.DisplayType);
Expand All @@ -281,26 +320,19 @@ void WriteComponentType()

source.WriteLine("public void Execute(global::Sia.World _, in global::Sia.EntityRef target)");
source.Indent++;
source.WriteLine("=> ExecuteOnParallel(target);");
source.Write("=> Execute(ref target.Get<");
WriteComponentType();
source.WriteLine(">());");
source.Indent--;

source.Write("public void Execute(global::Sia.World _, in global::Sia.EntityRef target, ref ");
WriteComponentType();
source.WriteLine(" component)");
source.Indent++;
source.WriteLine("=> ExecuteOnParallel(ref component);");
source.WriteLine("=> Execute(ref component);");
source.Indent--;

source.WriteLine("public void ExecuteOnParallel(in global::Sia.EntityRef target)");
source.Indent++;
source.Write("=> target.Get<");
WriteComponentType();
source.Write(">().");
source.Write(info.Name);
source.WriteLine(" = Value;");
source.Indent--;

source.Write("public void ExecuteOnParallel(ref ");
source.Write("public void Execute(ref ");
WriteComponentType();
source.WriteLine(" component)");
source.Indent++;
Expand Down Expand Up @@ -359,23 +391,20 @@ void WriteComponentType()
source.Write("public void Execute(global::Sia.World _, in global::Sia.EntityRef target, ref ");
WriteComponentType();
source.WriteLine(" component)");
source.WriteLine('{');
source.Indent++;

source.Write("component.");
source.Indent++;
source.Write("=> component.");
source.Write(info.Name);
source.Write(" = component.");
source.Write(info.Name);
source.WriteLine(call);

source.Indent--;
source.WriteLine("}");

source.Indent--;
source.WriteLine("}");
}

public static void GenerateViewTypeMainDecl(
public static void GenerateViewMainDecl(
IndentedTextWriter source, string componentType, TypeParameterListSyntax? componentTypeParams = null)
{
void WriteComponentType()
Expand Down Expand Up @@ -407,7 +436,7 @@ void WriteComponentType()
source.WriteLine("}");
}

public static void GenerateViewTypeProperty(
public static void GenerateViewProperty(
IndentedTextWriter source, PropertyInfo info, TypeParameterListSyntax? componentTypeParams = null)
{
source.WriteLine("public readonly ref partial struct View");
Expand Down Expand Up @@ -447,4 +476,36 @@ public static void GenerateViewTypeProperty(
source.Indent--;
source.WriteLine("}");
}

public static void GenerateImmutableContainerViewMethods(
IndentedTextWriter source, PropertyInfo info,
string componentType, TypeParameterListSyntax? componentTypeParams,
string commandName, string arguments, string parameters)
{
source.WriteLine("public readonly ref partial struct View");
source.WriteLine("{");
source.Indent++;

source.Write("public void ");
source.Write(commandName);
source.Write('(');
source.Write(arguments);
source.WriteLine(')');

source.Indent++;
source.Write("=> _world.Modify(entity, ref _component, new ");
source.Write(componentType);
if (componentTypeParams != null) {
WriteTypeParameters(source, componentTypeParams);
}
source.Write(".");
source.Write(commandName);
source.Write('(');
source.Write(parameters);
source.WriteLine("));");
source.Indent--;

source.Indent--;
source.WriteLine("}");
}
}
4 changes: 2 additions & 2 deletions Sia.CodeGenerators/SiaTemplateGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ static IEnumerable<PropertyInfo> GetProperties(INamedTypeSymbol symbol)

if (properties.Length != 0) {
source.WriteLine();
SiaPropertyGenerator.GenerateViewTypeMainDecl(source, compName, templateType.TypeParameterList);
SiaPropertyGenerator.GenerateViewMainDecl(source, compName, templateType.TypeParameterList);

foreach (var propInfo in properties) {
source.WriteLine();
SiaPropertyGenerator.GeneratePropertyCommands(
source, propInfo, compName, templateType.TypeParameterList);

source.WriteLine();
SiaPropertyGenerator.GenerateViewTypeProperty(source, propInfo);
SiaPropertyGenerator.GenerateViewProperty(source, propInfo);
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions Sia/Worlds/Interfaces/IParallelCommand.cs

This file was deleted.

6 changes: 6 additions & 0 deletions Sia/Worlds/Interfaces/IPureCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Sia;

public interface IPureCommand<TComponent> : ICommand<TComponent>
{
void Execute(ref TComponent component);
}

0 comments on commit f3d2b8f

Please sign in to comment.