CodeWriter and pain
This commit is contained in:
1 parent
a409054750
commit
85ac63425c
89 files changed
+1439
-1461
No files matched your search
@@ -16,7 +16,7 @@ namespace Code.Infrastructure.App
|
||||
[Inject] public CameraStateMachine Camera { get; set; }
|
||||
}
|
||||
|
||||
[Generators.StateMachine(typeof(AppStateMachineContext),typeof(AppStates))]
|
||||
[global::StateMachine(typeof(AppStateMachineContext),typeof(AppStates))]
|
||||
public partial class AppStateMachine
|
||||
{
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Code.Example;
|
||||
using Reflex.Core;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
using Code.Infrastructure.Camera;
|
||||
using Code.Infrastructure.Generators;
|
||||
using Code.Infrastructure.SceneLoader;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Code.Infrastructure.SceneLoader;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
using Code.Infrastructure.Camera;
|
||||
using Code.Infrastructure.Generators;
|
||||
using Code.Infrastructure.SceneLoader;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Code.Infrastructure.Generators;
|
||||
|
||||
|
||||
namespace Code.Infrastructure.App.States
|
||||
{
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Code.Infrastructure.Generators;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Infrastructure.Camera
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Code.Infrastructure.Generators;
|
||||
using Code.Infrastructure.Input;
|
||||
using Reflex.Attributes;
|
||||
using UnityEngine.AddressableAssets;
|
||||
@@ -46,7 +45,7 @@ namespace Code.Infrastructure.Camera
|
||||
[Inject] public EventBus.EventBus EventBus;
|
||||
}
|
||||
|
||||
[Generators.StateMachine(typeof(CameraStateMachineContext), typeof(CameraMode))]
|
||||
[global::StateMachine(typeof(CameraStateMachineContext), typeof(CameraMode))]
|
||||
public partial class CameraStateMachine
|
||||
{
|
||||
public UnityEngine.Camera Camera => StateMachineContext.cameraLoadedData.Camera;
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
using Code.Infrastructure.Generators;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Infrastructure.Camera.CameraStates
|
||||
{
|
||||
[State(typeof(CameraMode),typeof(CameraStateMachineContext), true)]
|
||||
// [State(typeof(CameraMode),typeof(CameraStateMachineContext), true)]
|
||||
public partial class NoneCameraState : BaseCameraState
|
||||
{
|
||||
public NoneCameraState(CameraStateMachineContext context, EventBus.EventBus eventBus) : base(context, eventBus)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
using UnityEngine.AddressableAssets;
|
||||
|
||||
namespace Code.Infrastructure.Configs
|
||||
{
|
||||
public interface IConfigLocator
|
||||
{
|
||||
AssetReference TryGetConfigAssetReference<T>();
|
||||
string TryGetManyConfigAssetReferences<T>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 251b70a9c67d4c108c8a512ed10a3c09
|
||||
timeCreated: 1767125435
|
||||
@@ -1,49 +1,50 @@
|
||||
using System;
|
||||
|
||||
namespace Code.Infrastructure.Generators
|
||||
public class SceneIdEnum : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
public class ToConfig : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
public class Config : Attribute
|
||||
{
|
||||
public class StateMachine : Attribute
|
||||
public bool Multiple;
|
||||
public string MenuName;
|
||||
public Config(string menuName, bool multiple = false)
|
||||
{
|
||||
public Type StateMachineContextType;
|
||||
public Type StateMachineEnumType;
|
||||
Multiple = multiple;
|
||||
MenuName = menuName;
|
||||
}
|
||||
}
|
||||
|
||||
public StateMachine(Type stateMachineContextType, Type stateMachineEnumType)
|
||||
{
|
||||
StateMachineContextType = stateMachineContextType;
|
||||
StateMachineEnumType = stateMachineEnumType;
|
||||
}
|
||||
}
|
||||
|
||||
public class State : Attribute
|
||||
{
|
||||
public Type StateEnum;
|
||||
public Type StateMachineContext;
|
||||
public bool SilentBitch; // Поэтому нейминг переменных очень важен
|
||||
public class StateRaisingEvent : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
public State(Type stateEnum,Type stateMachineContext, bool silentBitch = false)
|
||||
{
|
||||
StateEnum = stateEnum;
|
||||
StateMachineContext = stateMachineContext;
|
||||
SilentBitch = silentBitch;
|
||||
}
|
||||
}
|
||||
public class StateRaisingEvent : Attribute
|
||||
{
|
||||
}
|
||||
public class Config : Attribute
|
||||
public class State : Attribute
|
||||
{
|
||||
public Type StateEnum;
|
||||
public Type StateMachineContext;
|
||||
public bool SilentBitch; // Поэтому нейминг переменных очень важен
|
||||
|
||||
public State(Type stateEnum,Type stateMachineContext, bool silentBitch = false)
|
||||
{
|
||||
public bool Multiple;
|
||||
public string MenuName;
|
||||
public Config(string menuName, bool multiple = false)
|
||||
{
|
||||
Multiple = multiple;
|
||||
MenuName = menuName;
|
||||
}
|
||||
StateEnum = stateEnum;
|
||||
StateMachineContext = stateMachineContext;
|
||||
SilentBitch = silentBitch;
|
||||
}
|
||||
public class ToConfig : Attribute
|
||||
{
|
||||
}
|
||||
public class SceneIdEnum : Attribute
|
||||
{
|
||||
}
|
||||
|
||||
public class StateMachine : Attribute
|
||||
{
|
||||
public Type StateMachineContextType;
|
||||
public Type StateMachineEnumType;
|
||||
|
||||
public StateMachine(Type stateMachineContextType, Type stateMachineEnumType)
|
||||
{
|
||||
StateMachineContextType = stateMachineContextType;
|
||||
StateMachineEnumType = stateMachineEnumType;
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
using System.Linq;
|
||||
using Unity.VisualScripting;
|
||||
using UnityCodeGen;
|
||||
using UnityEditor;
|
||||
|
||||
namespace Code.Infrastructure.Generators
|
||||
{
|
||||
[Generator]
|
||||
public class ConfigGenerator : ICodeGenerator
|
||||
{
|
||||
public void Execute(GeneratorContext context)
|
||||
{
|
||||
var configs = TypeCache.GetTypesWithAttribute<Config>();
|
||||
|
||||
var configsCode = "";
|
||||
var configAddCode = "";
|
||||
var configReferencesCode = "";
|
||||
var configReferencesUsingsCode = "";
|
||||
var configOfConfigsCode = "";
|
||||
|
||||
foreach (var cfg in configs)
|
||||
{
|
||||
var configFields = "";
|
||||
var configUsings = "";
|
||||
|
||||
var mem = cfg.GetFields();
|
||||
var members = mem.Where(m => m.HasAttribute<ToConfig>()).ToList();
|
||||
foreach (var m in members)
|
||||
{
|
||||
configUsings += $"using {m.FieldType.Namespace};\n";
|
||||
configFields += $"public {m.FieldType.FullName} {m.Name};\n";
|
||||
}
|
||||
|
||||
var attribute = cfg.GetAttribute<Config>();
|
||||
|
||||
configUsings += @"
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
";
|
||||
var configCode = $@"
|
||||
#pragma warning disable CS0105
|
||||
{configUsings}
|
||||
namespace {cfg.Namespace}{{
|
||||
#pragma warning restore CS0105
|
||||
[CreateAssetMenu(fileName = ""{cfg.Name}Config"", menuName = ""{attribute.MenuName}"")]
|
||||
public class {cfg.Name}Config : ScriptableObject
|
||||
{{
|
||||
{configFields}
|
||||
}}
|
||||
}}
|
||||
";
|
||||
context.AddCode($"{cfg.Name}Config.Generated.cs", configCode);
|
||||
|
||||
configReferencesUsingsCode += $"using {cfg.Namespace};\n";
|
||||
|
||||
if (attribute.Multiple)
|
||||
{
|
||||
configReferencesCode += $"public string {cfg.Name};\n";
|
||||
configAddCode += $@"
|
||||
_multipleConfigReferences.Add(typeof({cfg.FullName}Config), systemConfigsReferences.{cfg.Name});
|
||||
";
|
||||
}
|
||||
else
|
||||
{
|
||||
configReferencesCode += $"public AssetReference {cfg.Name};\n";
|
||||
configAddCode += $@"
|
||||
_singleConfigReferences.Add(typeof({cfg.FullName}Config), systemConfigsReferences.{cfg.Name});
|
||||
";
|
||||
}
|
||||
}
|
||||
configsCode += $@"
|
||||
#pragma warning disable CS0105
|
||||
{configReferencesUsingsCode}
|
||||
using UnityEngine;
|
||||
#pragma warning restore CS0105
|
||||
|
||||
namespace Code.Infrastructure.Configs
|
||||
{{
|
||||
|
||||
public partial class ConfigLocator
|
||||
{{
|
||||
partial void LoadConfigs()
|
||||
{{
|
||||
SystemConfigsReferences systemConfigsReferences = Resources.Load<SystemConfigsReferences>(""SystemConfigsReferences"");
|
||||
{configAddCode}
|
||||
//_singleConfigReferences.Add(typeof(ScenesAssetReferences), systemConfigsReferences.scenesAssetReferences);
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
";
|
||||
context.AddCode($"ConfigLocator.Generated.cs", configsCode);
|
||||
|
||||
configOfConfigsCode += $@"
|
||||
#pragma warning disable CS0105
|
||||
{configReferencesUsingsCode}
|
||||
using UnityEngine.AddressableAssets;
|
||||
#pragma warning restore CS0105
|
||||
|
||||
namespace Code.Infrastructure.Configs{{
|
||||
public partial class SystemConfigsReferences
|
||||
{{
|
||||
{configReferencesCode}
|
||||
}}
|
||||
}}
|
||||
";
|
||||
context.AddCode($"SystemConfigsReferences.Generated.cs", configOfConfigsCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 026403be8eae4546adcb407308031c1d
|
||||
timeCreated: 1765660396
|
||||
@@ -1,148 +0,0 @@
|
||||
using System.Linq;
|
||||
using Unity.VisualScripting;
|
||||
using UnityCodeGen;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Infrastructure.Generators
|
||||
{
|
||||
[Generator]
|
||||
public class SceneLoaderGenerator : ICodeGenerator
|
||||
{
|
||||
public void Execute(GeneratorContext context)
|
||||
{
|
||||
var Enum = TypeCache.GetTypesWithAttribute<SceneIdEnum>();
|
||||
|
||||
var loaderCode = "";
|
||||
var dictionaryCode = "";
|
||||
var interfacesCode = "";
|
||||
var interfacesUsingsCode = $@"
|
||||
using Code.Infrastructure.EventBus;
|
||||
";
|
||||
|
||||
foreach (var en in Enum)
|
||||
{
|
||||
if (!en.IsEnum) return;
|
||||
|
||||
var members = en.GetFields();
|
||||
foreach (var member in members)
|
||||
{
|
||||
if(member.FieldType.FullName != en.FullName) continue;
|
||||
interfacesCode += $@"
|
||||
public interface IOn{member.Name}SceneLoadedHandler : IGlobalSubscriber
|
||||
{{
|
||||
void On{member.Name}SceneLoaded();
|
||||
}}
|
||||
";
|
||||
dictionaryCode += $@"
|
||||
|
||||
_scenesActions.Add(SceneId.{member.Name},() => _eventBus.RaiseEvent<IOn{member.Name}SceneLoadedHandler>(h=> h.On{member.Name}SceneLoaded()));
|
||||
";
|
||||
}
|
||||
}
|
||||
|
||||
loaderCode = $@"
|
||||
#pragma warning disable CS0105
|
||||
{interfacesUsingsCode}
|
||||
#pragma warning restore CS0105
|
||||
|
||||
namespace Code.Infrastructure.SceneLoader
|
||||
{{
|
||||
public partial class SceneLoader{{
|
||||
partial void SetupScenesInfo(){{
|
||||
{dictionaryCode}
|
||||
}}
|
||||
}}
|
||||
{interfacesCode}
|
||||
}}
|
||||
";
|
||||
context.AddCode($"SceneLoader.Generated.cs", loaderCode);
|
||||
// var configsCode = "";
|
||||
// var configCode = "";
|
||||
// var configUsings = "";
|
||||
// var configFields = "";
|
||||
// var configAddCode = "";
|
||||
// var configReferencesCode = "";
|
||||
// var configReferencesUsingsCode = "";
|
||||
// var configOfConfigsCode = "";
|
||||
//
|
||||
// foreach (var cfg in configs)
|
||||
// {
|
||||
// var mem = cfg.GetFields();
|
||||
// var members = mem.Where(m => m.HasAttribute<ToConfig>()).ToList();
|
||||
// foreach (var m in members)
|
||||
// {
|
||||
// configUsings += $"using {m.FieldType.Namespace};\n";
|
||||
// configFields += $"public {m.FieldType.FullName} {m.Name};\n";
|
||||
// }
|
||||
//
|
||||
// var attribute = cfg.GetAttribute<Config>();
|
||||
//
|
||||
// configUsings += @"
|
||||
// using UnityEngine;
|
||||
// using UnityEngine.AddressableAssets;
|
||||
// ";
|
||||
// configCode = $@"
|
||||
// {configUsings}
|
||||
// namespace {cfg.Namespace}{{
|
||||
// [CreateAssetMenu(fileName = ""{cfg.Name}Config"", menuName = ""{attribute.MenuName}"")]
|
||||
// public class {cfg.Name}Config : ScriptableObject
|
||||
// {{
|
||||
// {configFields}
|
||||
// }}
|
||||
// }}
|
||||
// ";
|
||||
// context.AddCode($"{cfg.Name}Config.Generated.cs", configCode);
|
||||
//
|
||||
// configReferencesUsingsCode += $"using {cfg.Namespace};\n";
|
||||
//
|
||||
// if (attribute.Multiple)
|
||||
// {
|
||||
// configReferencesCode += $"public String {cfg.Name};\n";
|
||||
// configAddCode += $@"
|
||||
// _multipleConfigReferences.Add(typeof({cfg.FullName}), systemConfigsReferences.{cfg.Name});
|
||||
// ";
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// configReferencesCode += $"public AssetReference {cfg.Name};\n";
|
||||
// configAddCode += $@"
|
||||
// _singleConfigReferences.Add(typeof({cfg.FullName}Config), systemConfigsReferences.{cfg.Name});
|
||||
// ";
|
||||
// }
|
||||
// }
|
||||
// configsCode += $@"
|
||||
// {configReferencesUsingsCode}
|
||||
// using UnityEngine;
|
||||
//
|
||||
// namespace Code.Infrastructure.Configs
|
||||
// {{
|
||||
//
|
||||
// public partial class ConfigLocator
|
||||
// {{
|
||||
// partial void LoadConfigs()
|
||||
// {{
|
||||
// SystemConfigsReferences systemConfigsReferences = Resources.Load<SystemConfigsReferences>(""SystemConfigsReferences"");
|
||||
// {configAddCode}
|
||||
// //_singleConfigReferences.Add(typeof(ScenesAssetReferences), systemConfigsReferences.scenesAssetReferences);
|
||||
// }}
|
||||
// }}
|
||||
// }}
|
||||
// ";
|
||||
// context.AddCode($"ConfigLocator.Generated.cs", configsCode);
|
||||
//
|
||||
// configOfConfigsCode += $@"
|
||||
// {configReferencesUsingsCode}
|
||||
// using UnityEngine.AddressableAssets;
|
||||
//
|
||||
// namespace Code.Infrastructure.Configs{{
|
||||
// public partial class SystemConfigsReferences
|
||||
// {{
|
||||
// {configReferencesCode}
|
||||
// }}
|
||||
// }}
|
||||
// ";
|
||||
// context.AddCode($"SystemConfigsReferences.Generated.cs", configOfConfigsCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 92ec56ffd46d4a569bb9a7d436790439
|
||||
timeCreated: 1766423754
|
||||
@@ -1,131 +0,0 @@
|
||||
using System.Linq;
|
||||
using Unity.VisualScripting;
|
||||
using UnityCodeGen;
|
||||
using UnityEditor;
|
||||
|
||||
namespace Code.Infrastructure.Generators
|
||||
{
|
||||
[Generator]
|
||||
public class StateMachineGenerator : ICodeGenerator
|
||||
{
|
||||
public void Execute(GeneratorContext context)
|
||||
{
|
||||
GenerateStates(context);
|
||||
|
||||
GenerateStateMachine(context);
|
||||
}
|
||||
|
||||
private void GenerateStateMachine(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>().StateMachineContext.FullName ==
|
||||
attribute.StateMachineContextType.FullName);
|
||||
|
||||
var statesCode = "";
|
||||
var statesNamespacesCode = "";
|
||||
|
||||
foreach (var st in states)
|
||||
{
|
||||
statesNamespacesCode += $@"
|
||||
using {st.Namespace};
|
||||
";
|
||||
statesCode += $@"
|
||||
var state{st.Name} = new {st.Name}(StateMachineContext,eventBus);
|
||||
States.Add(state{st.Name}.myState, state{st.Name});
|
||||
";
|
||||
}
|
||||
|
||||
var stateMachineCode =
|
||||
$@"
|
||||
// <auto-generated/>
|
||||
#pragma warning disable CS0105
|
||||
using Code.Infrastructure.StateMachine;
|
||||
{statesNamespacesCode}
|
||||
#pragma warning restore CS0105
|
||||
namespace {sm.Namespace}
|
||||
{{
|
||||
|
||||
public partial class {sm.Name} : AbstractStateMachine<{attribute.StateMachineEnumType.FullName},{attribute.StateMachineContextType.FullName}>
|
||||
{{
|
||||
|
||||
public {sm.Name}({attribute.StateMachineContextType.FullName} context, EventBus.EventBus eventBus) : base(context)
|
||||
{{
|
||||
{statesCode}
|
||||
}}
|
||||
|
||||
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;
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
|
||||
";
|
||||
|
||||
context.AddCode($"{sm.FullName}.Generated.cs", stateMachineCode);
|
||||
}
|
||||
}
|
||||
|
||||
private void GenerateStates(GeneratorContext context)
|
||||
{
|
||||
var states = TypeCache.GetTypesWithAttribute<State>();
|
||||
|
||||
var statesCode = "";
|
||||
foreach (var st in states)
|
||||
{
|
||||
var attribute = st.GetAttribute<State>();
|
||||
|
||||
var fireStatesCode = "";
|
||||
if(st.HasAttribute<StateRaisingEvent>()) fireStatesCode = $@"
|
||||
protected override void _fireEnterState()
|
||||
{{
|
||||
EventBus.RaiseEvent<IEnterStateHandle<{st.FullName}>>(h => h.EnterState());
|
||||
}}
|
||||
|
||||
protected override void _fireExitState()
|
||||
{{
|
||||
EventBus.RaiseEvent<IExitStateHandle<{st.FullName}>>(h => h.ExitState());
|
||||
}}
|
||||
";
|
||||
var genetics = "";
|
||||
if(!attribute.SilentBitch) genetics = $@" : BaseState<{attribute.StateEnum.FullName}, {attribute.StateMachineContext.FullName}>";
|
||||
|
||||
statesCode = $@"
|
||||
|
||||
// retard-generated
|
||||
|
||||
using Code.Infrastructure.StateMachine;
|
||||
|
||||
namespace {st.Namespace}
|
||||
{{
|
||||
public partial class {st.Name} {genetics}
|
||||
{{
|
||||
{fireStatesCode}
|
||||
}}
|
||||
}}
|
||||
";
|
||||
|
||||
|
||||
context.AddCode($"{st.FullName}.Generated.cs", statesCode);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9fb4b6961cd0471d8183511316c15000
|
||||
timeCreated: 1765563824
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Code.Infrastructure.Generators;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Code.Infrastructure.Generators;
|
||||
|
||||
namespace Code.Infrastructure.SceneLoader
|
||||
{
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Code.Infrastructure.Generators;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using Code.Infrastructure.App;
|
||||
|
||||
namespace Code.Infrastructure.StateMachine
|
||||
{
|
||||
public abstract class BaseState<TEnum, TStateMachineContext>
|
||||
|
||||
Reference in new issue
Block a user