Skip to content

Commit

Permalink
- Implemented StateFlux Attribute to store States with Attribute
Browse files Browse the repository at this point in the history
- "FluxAttribute" is now deprecated, use "MethodFluxAttribute"
- modified script template to MonoFlux, adding StateFLux in comments
  • Loading branch information
xavierarpa committed Nov 15, 2023
1 parent c7c8dc7 commit 3583a1f
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 36 deletions.
5 changes: 3 additions & 2 deletions Runtime/FluxAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ namespace Kingdox.UniFlux
/// Class FluxAttribute, a custom attribute that mark a method to be subscribed in a flux.
/// AllowMultiple is false to keep legibility
///</summary>
[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false)]
public class FluxAttribute : System.Attribute
[Obsolete("Naming has been changed, use instead [MethodFlux]")]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class FluxAttribute : Attribute
{
///<summary>
/// Key provided to the attribute's constructor.
Expand Down
39 changes: 39 additions & 0 deletions Runtime/MethodFluxAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright (c) 2023 Xavier Arpa López Thomas Peter ('Kingdox')
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma warning disable CS0618
using System;
namespace Kingdox.UniFlux
{
///<summary>
/// Class FluxAttribute, a custom attribute that mark a method to be subscribed in a flux.
/// AllowMultiple is false to keep legibility
///</summary>
public class MethodFluxAttribute : FluxAttribute
{
public MethodFluxAttribute(object key) : base(key)
{
// Nada temporalmente
}
}
}
//TODO: C# 11 allow Attribute<T>, instead of object key

11 changes: 11 additions & 0 deletions Runtime/MethodFluxAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 54 additions & 29 deletions Runtime/MonoFluxExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Kingdox.UniFlux.Core.Internal;
namespace Kingdox.UniFlux
{
///<summary>
Expand All @@ -39,8 +41,10 @@ internal static class MonoFluxExtension
internal static readonly string m_type_flux_method = nameof(Core.Internal.Flux<object>.Store);
//
internal static readonly Type m_type_fluxparam = typeof(Core.Internal.FluxParam<,>);
internal static readonly Type m_type_fluxparam_state = typeof(Core.Internal.FluxState<,>);
internal static readonly Type m_type_fluxparam_delegate = typeof(Action<>);
internal static readonly string m_type_fluxparam_method = nameof(Core.Internal.FluxParam<object,object>.Store);
internal static readonly string m_type_fluxparam_method_state = nameof(Core.Internal.FluxState<object,object>.Store);
//
internal static readonly Type m_type_fluxreturn = typeof(Core.Internal.FluxReturn<,>);
internal static readonly Type m_type_fluxreturn_delegate = typeof(Func<>);
Expand Down Expand Up @@ -95,36 +99,57 @@ internal static void Subscribe(this MonoFlux monoflux, in bool condition)
throw new System.Exception($"Error '{methods[i].Name}' : Theres more than one parameter, please set 1 or 0 parameter. (if you need to add more than 1 argument use Tuples or create a struct, record o class...)");
}
#endif
switch ((_Parameters.Length.Equals(1), !methods[i].ReturnType.Equals(m_type_void)))

if(m_methods[methods[i]] is StateFluxAttribute _stateFlux ) // IsState ?
{
case (false, false): // Flux
m_type_flux
.MakeGenericType(m_methods[methods[i]].key.GetType())
.GetMethod(m_type_flux_method, m_bindingflag_all)
.Invoke( null, new object[]{ m_methods[methods[i]].key, methods[i].CreateDelegate(m_type_flux_delegate, monoflux), condition})
;
break;
case (true, false): // FluxParam
m_type_fluxparam
.MakeGenericType(m_methods[methods[i]].key.GetType(), _Parameters[0].ParameterType)
.GetMethod(m_type_fluxparam_method, m_bindingflag_all)
.Invoke( null, new object[]{ m_methods[methods[i]].key, methods[i].CreateDelegate(m_type_fluxparam_delegate.MakeGenericType(_Parameters[0].ParameterType), monoflux), condition})
;
break;
case (false, true): //FluxReturn
m_type_fluxreturn
.MakeGenericType(m_methods[methods[i]].key.GetType(), methods[i].ReturnType)
.GetMethod(m_type_fluxreturn_method, m_bindingflag_all)
.Invoke( null, new object[]{ m_methods[methods[i]].key, methods[i].CreateDelegate(m_type_fluxreturn_delegate.MakeGenericType(methods[i].ReturnType), monoflux), condition})
;
break;
case (true, true): //FluxParamReturn
m_type_fluxparamreturn
.MakeGenericType(m_methods[methods[i]].key.GetType(), _Parameters[0].ParameterType, methods[i].ReturnType)
.GetMethod(m_type_fluxparamreturn_method, m_bindingflag_all)
.Invoke( null, new object[]{ m_methods[methods[i]].key, methods[i].CreateDelegate(m_type_fluxparamreturn_delegate.MakeGenericType(_Parameters[0].ParameterType, methods[i].ReturnType), monoflux), condition})
;
break;
switch ((_Parameters.Length.Equals(1), !methods[i].ReturnType.Equals(m_type_void)))
{
case (true, false):
m_type_fluxparam_state
.MakeGenericType(m_methods[methods[i]].key.GetType(), _Parameters[0].ParameterType)
.GetMethod(m_type_fluxparam_method_state, m_bindingflag_all)
.Invoke( null, new object[]{ m_methods[methods[i]].key, methods[i].CreateDelegate(m_type_fluxparam_delegate.MakeGenericType(_Parameters[0].ParameterType), monoflux), condition})
;
break;
case (false, false): throw new Exception($"Error '{methods[i].Name}' : not treated as StateFluxAttribute, Add Parameter");
case (true, true): throw new Exception($"Error '{methods[i].Name}' : not treated as StateFluxAttribute, Remove Return value");
case (false, true): throw new Exception($"Error '{methods[i].Name}' : not treated as StateFluxAttribute, Add Parameter, Remove Return value");
default : throw new Exception($"Error '{methods[i].Name}' : not treated as StateFluxAttribute");
}
}
else // IsNormal ?
{
switch ((_Parameters.Length.Equals(1), !methods[i].ReturnType.Equals(m_type_void)))
{
case (false, false): // Flux
m_type_flux
.MakeGenericType(m_methods[methods[i]].key.GetType())
.GetMethod(m_type_flux_method, m_bindingflag_all)
.Invoke( null, new object[]{ m_methods[methods[i]].key, methods[i].CreateDelegate(m_type_flux_delegate, monoflux), condition})
;
break;
case (true, false): // FluxParam
m_type_fluxparam
.MakeGenericType(m_methods[methods[i]].key.GetType(), _Parameters[0].ParameterType)
.GetMethod(m_type_fluxparam_method, m_bindingflag_all)
.Invoke( null, new object[]{ m_methods[methods[i]].key, methods[i].CreateDelegate(m_type_fluxparam_delegate.MakeGenericType(_Parameters[0].ParameterType), monoflux), condition})
;
break;
case (false, true): //FluxReturn
m_type_fluxreturn
.MakeGenericType(m_methods[methods[i]].key.GetType(), methods[i].ReturnType)
.GetMethod(m_type_fluxreturn_method, m_bindingflag_all)
.Invoke( null, new object[]{ m_methods[methods[i]].key, methods[i].CreateDelegate(m_type_fluxreturn_delegate.MakeGenericType(methods[i].ReturnType), monoflux), condition})
;
break;
case (true, true): //FluxParamReturn
m_type_fluxparamreturn
.MakeGenericType(m_methods[methods[i]].key.GetType(), _Parameters[0].ParameterType, methods[i].ReturnType)
.GetMethod(m_type_fluxparamreturn_method, m_bindingflag_all)
.Invoke( null, new object[]{ m_methods[methods[i]].key, methods[i].CreateDelegate(m_type_fluxparamreturn_delegate.MakeGenericType(_Parameters[0].ParameterType, methods[i].ReturnType), monoflux), condition})
;
break;
}
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions Runtime/StateFluxAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright (c) 2023 Xavier Arpa López Thomas Peter ('Kingdox')
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
namespace Kingdox.UniFlux
{
///<summary>
/// Class StateFluxAttribute, is like MethodFluxAttribute but stores the last saved data
///</summary>
public class StateFluxAttribute : MethodFluxAttribute
{
public StateFluxAttribute(object key) : base(key)
{
// Nada temporalmente
}
}
}
//TODO: C# 11 allow Attribute<T>, instead of object key
11 changes: 11 additions & 0 deletions Runtime/StateFluxAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion ScriptTemplates/80-Kingdox__MonoFlux-MonoFlux.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ using Kingdox.UniFlux;
public sealed class #SCRIPTNAME# : MonoFlux
{
//protected override void OnFlux(bool condition) {}
//[Flux()] private void Method() => ;
//[MethodFlux()] private void Method() => ;
//[StateFlux()] private void State( Type state) => ;
}
3 changes: 0 additions & 3 deletions ScriptTemplates/80-Kingdox__Service-Service.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ namespace Service
{

}
}
public static partial class #SCRIPTNAME# // Key
{
public static partial class Key
{
private const string _#SCRIPTNAME# = nameof(#SCRIPTNAME#) + ".";
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.xavierarpa.uniflux",
"displayName": "UniFlux",
"version": "1.3.3",
"version": "1.4.0",
"author": "Xavier Arpa",
"unity": "2019.3",
"documentation": "README.md",
Expand Down

0 comments on commit 3583a1f

Please sign in to comment.