Initial commit
This commit is contained in:
commit
662f4a1d05
89 files changed
+8008
No files matched your search
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace Code.Generators
|
||||
{
|
||||
public class StateMachine : Attribute
|
||||
{
|
||||
public Type StateMachineContextType;
|
||||
public Type StateMachineEnumType;
|
||||
|
||||
public StateMachine(Type stateMachineContextType, Type stateMachineEnumType)
|
||||
{
|
||||
StateMachineContextType = stateMachineContextType;
|
||||
StateMachineEnumType = stateMachineEnumType;
|
||||
}
|
||||
}
|
||||
|
||||
public class State : Attribute
|
||||
{
|
||||
public Type StateMachineContextType;
|
||||
public State (Type stateMachineContextType )
|
||||
{
|
||||
StateMachineContextType = stateMachineContextType;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 03e07712b53c4a3d8f6c8033af003369
|
||||
timeCreated: 1765563884
|
||||
@@ -0,0 +1,95 @@
|
||||
using System.Linq;
|
||||
using Code.StateMachine;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEditor;
|
||||
using UnityCodeGen;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Generators
|
||||
{
|
||||
[Generator]
|
||||
public class StateMachineGenerator : ICodeGenerator
|
||||
{
|
||||
public void Execute(GeneratorContext context)
|
||||
{
|
||||
var stateMachines = TypeCache.GetTypesWithAttribute<StateMachine>();
|
||||
|
||||
foreach (var sm in stateMachines)
|
||||
{
|
||||
var attribute = sm.GetAttribute<StateMachine>();
|
||||
|
||||
var states = TypeCache.GetTypesWithAttribute<State>()
|
||||
.Where(st =>
|
||||
st.GetAttribute<State>().StateMachineContextType.FullName ==
|
||||
attribute.StateMachineContextType.FullName);
|
||||
|
||||
var statesCode = "";
|
||||
var statesNamespacesCode = "";
|
||||
|
||||
foreach (var st in states)
|
||||
{
|
||||
var stateAttribute = st.GetAttribute<State>();
|
||||
statesNamespacesCode += $@"
|
||||
using {st.Namespace};
|
||||
";
|
||||
statesCode += $@"
|
||||
var state{st.Name} = new {st.Name}(StateMachineContext);
|
||||
States.Add(state{st.Name}.myState, state{st.Name});
|
||||
";
|
||||
}
|
||||
|
||||
var stateMachineCode =
|
||||
$@"
|
||||
// <auto-generated/>
|
||||
using Code.StateMachine;
|
||||
{statesNamespacesCode}
|
||||
namespace {sm.Namespace}
|
||||
{{
|
||||
|
||||
public partial class {sm.Name} : AbstractStateMachine<{attribute.StateMachineEnumType.FullName},{attribute.StateMachineContextType.FullName}>
|
||||
{{
|
||||
|
||||
public {sm.Name}()
|
||||
{{
|
||||
{statesCode}
|
||||
CurrentBaseState = States[default({attribute.StateMachineEnumType.FullName})];
|
||||
}}
|
||||
|
||||
protected override bool CheckTransitionAllowed({attribute.StateMachineEnumType.FullName} nextState) => AllowedStates.HasFlag(nextState);
|
||||
protected override void ResetAllowedTransitions() => AllowedStates = 0;
|
||||
}}
|
||||
|
||||
public static class {sm.Name}Extensions
|
||||
{{
|
||||
public static AbstractStateMachine<{attribute.StateMachineEnumType.FullName}, {attribute.StateMachineContextType.FullName}> AllowStateTransition(
|
||||
this AbstractStateMachine<{attribute.StateMachineEnumType.FullName}, {attribute.StateMachineContextType.FullName}> stateMachine, {attribute.StateMachineEnumType.FullName} state)
|
||||
{{
|
||||
if (stateMachine == null) return null;
|
||||
|
||||
stateMachine.AllowedStates |= state;
|
||||
return stateMachine;
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
|
||||
";
|
||||
|
||||
// var publicFields = t.GetFields()
|
||||
// .Where(x => x.IsPublic && !x.IsStatic)
|
||||
// .Select(x => $"{x.Name}:{{{x.Name}}}");
|
||||
|
||||
// var toString = string.Join(", ", publicFields);
|
||||
// var code =
|
||||
// $@"// <auto-generated/>
|
||||
// partial class {t.Name}
|
||||
// {{
|
||||
// public override string ToString()
|
||||
// {{
|
||||
// return $""{toString}"";
|
||||
// }}
|
||||
// }}";
|
||||
context.AddCode($"{sm.FullName}.Generated.cs", stateMachineCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9fb4b6961cd0471d8183511316c15000
|
||||
timeCreated: 1765563824
|
||||
Reference in new issue
Block a user