Elin Decompiled Documentation EA 23.315 Nightly
Loading...
Searching...
No Matches
DramaInvokeDetail Class Reference

Public Member Functions

 DramaInvokeDetail (MethodInfo method, string contract=null)
 
bool SafeInvoke (DramaManager dm, Dictionary< string, string > line, params string[] parameters)
 

Properties

MethodInfo Method [get]
 
string Contract [get]
 
bool NoDiscard [get]
 
bool ParamPassthrough [get]
 

Private Member Functions

void ValidateSignature ()
 
Func< DramaManager, Dictionary< string, string >, string[], bool > BuildDelegate ()
 

Static Private Member Functions

static Expression CreateConvertExpression (Expression expr, ParameterInfo parameter)
 
static MethodInfo GetTryParseMethod (Type type)
 

Private Attributes

Func< DramaManager, Dictionary< string, string >, string[], bool > _compiled
 

Static Private Attributes

static readonly MethodInfo _stringIsNullOrEmpty = typeof(string).GetMethod("IsNullOrEmpty", new Type[1] { typeof(string) })
 
static readonly ConstructorInfo _argumentException = typeof(ArgumentException).GetConstructor(new Type[1] { typeof(string) })
 

Detailed Description

Definition at line 8 of file DramaInvokeDetail.cs.

Constructor & Destructor Documentation

◆ DramaInvokeDetail()

DramaInvokeDetail.DramaInvokeDetail ( MethodInfo  method,
string  contract = null 
)
inline

Definition at line 24 of file DramaInvokeDetail.cs.

25 {
26 Method = method;
27 Contract = contract;
29 }

References Contract, Method, and ValidateSignature().

Member Function Documentation

◆ BuildDelegate()

Func< DramaManager, Dictionary< string, string >, string[], bool > DramaInvokeDetail.BuildDelegate ( )
inlineprivate

Definition at line 51 of file DramaInvokeDetail.cs.

52 {
53 ParameterInfo[] parameters = Method.GetParameters();
54 ParameterExpression parameterExpression = Expression.Parameter(typeof(DramaManager), "dm");
55 ParameterExpression parameterExpression2 = Expression.Parameter(typeof(Dictionary<string, string>), "line");
56 ParameterExpression parameterExpression3 = Expression.Parameter(typeof(string[]), "parameters");
57 List<Expression> list = new List<Expression> { parameterExpression, parameterExpression2 };
58 if (parameters.Length == 3 && parameters[2].ParameterType == typeof(string[]))
59 {
60 list.Add(parameterExpression3);
61 }
62 else if (parameters.Length > 2)
63 {
64 for (int i = 2; i < parameters.Length; i++)
65 {
66 ParameterInfo parameter = parameters[i];
67 int num = i - 2;
68 Expression expr = Expression.Condition(Expression.LessThan(Expression.Constant(num), Expression.ArrayLength(parameterExpression3)), Expression.ArrayIndex(parameterExpression3, Expression.Constant(num)), Expression.Constant(null, typeof(string)));
69 list.Add(CreateConvertExpression(expr, parameter));
70 }
71 }
72 return Expression.Lambda<Func<DramaManager, Dictionary<string, string>, string[], bool>>(Expression.Call(null, Method, list), new ParameterExpression[3] { parameterExpression, parameterExpression2, parameterExpression3 }).Compile();
73 }
static Expression CreateConvertExpression(Expression expr, ParameterInfo parameter)

References CreateConvertExpression(), and Method.

Referenced by SafeInvoke().

◆ CreateConvertExpression()

static Expression DramaInvokeDetail.CreateConvertExpression ( Expression  expr,
ParameterInfo  parameter 
)
inlinestaticprivate

Definition at line 75 of file DramaInvokeDetail.cs.

76 {
77 Type parameterType = parameter.ParameterType;
78 bool isOptional = parameter.IsOptional;
79 object defaultValue = parameter.DefaultValue;
80 string text = $"required action parameter #{parameter.Position - 2} '{parameter.Name}'";
81 if (parameterType == typeof(string))
82 {
83 MethodCallExpression test = Expression.Call(_stringIsNullOrEmpty, expr);
84 if (isOptional)
85 {
86 ConstantExpression ifTrue = Expression.Constant(defaultValue, typeof(string));
87 return Expression.Condition(test, ifTrue, expr);
88 }
89 string value = "#drama " + text + " cannot be empty";
90 UnaryExpression ifTrue2 = Expression.Throw(Expression.New(_argumentException, Expression.Constant(value)), typeof(string));
91 return Expression.Condition(test, ifTrue2, expr);
92 }
93 MethodInfo tryParseMethod = GetTryParseMethod(parameterType);
94 if (tryParseMethod == null)
95 {
96 throw new NotSupportedException("#drama type '" + parameterType.Name + "' does not have a public static TryParse method");
97 }
98 ParameterExpression parameterExpression = Expression.Variable(parameterType, "parsed");
99 Expression test2 = Expression.Call(tryParseMethod, expr, parameterExpression);
100 Expression expression;
101 if (isOptional)
102 {
103 expression = Expression.Constant(defaultValue, parameterType);
104 }
105 else
106 {
107 string value2 = "#drama can't parse " + text + " to type '" + parameterType.Name + "'";
108 expression = Expression.Throw(Expression.New(_argumentException, Expression.Constant(value2)), parameterType);
109 }
110 ConditionalExpression conditionalExpression = Expression.Condition(Expression.Call(_stringIsNullOrEmpty, expr), expression, Expression.Condition(test2, parameterExpression, expression));
111 return Expression.Block(new ParameterExpression[1] { parameterExpression }, conditionalExpression);
112 }
static readonly ConstructorInfo _argumentException
static readonly MethodInfo _stringIsNullOrEmpty
static MethodInfo GetTryParseMethod(Type type)

References $, _argumentException, _stringIsNullOrEmpty, and GetTryParseMethod().

Referenced by BuildDelegate().

◆ GetTryParseMethod()

static MethodInfo DramaInvokeDetail.GetTryParseMethod ( Type  type)
inlinestaticprivate

Definition at line 114 of file DramaInvokeDetail.cs.

115 {
116 if (type.IsEnum)
117 {
118 MethodInfo method = typeof(Enum).GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public, null, new Type[2]
119 {
120 typeof(string),
121 type.MakeByRefType()
122 }, null);
123 if (method != null)
124 {
125 return method.MakeGenericMethod(type);
126 }
127 }
128 return type.GetMethod("TryParse", new Type[2]
129 {
130 typeof(string),
131 type.MakeByRefType()
132 });
133 }

Referenced by CreateConvertExpression().

◆ SafeInvoke()

bool DramaInvokeDetail.SafeInvoke ( DramaManager  dm,
Dictionary< string, string >  line,
params string[]  parameters 
)
inline

Definition at line 135 of file DramaInvokeDetail.cs.

136 {
137 if (Method == null)
138 {
139 return true;
140 }
141 try
142 {
143 if (_compiled == null)
144 {
146 }
147 return _compiled(dm, line, parameters);
148 }
149 catch (Exception ex)
150 {
151 ModUtil.LogModError("exception while invoking drama action '" + Method.TryToString() + "'\n" + ex.Message, new FileInfo(dm.path));
152 Debug.LogException(ex);
153 return false;
154 }
155 }
Func< DramaManager, Dictionary< string, string >, string[], bool > BuildDelegate()
Func< DramaManager, Dictionary< string, string >, string[], bool > _compiled

References _compiled, BuildDelegate(), Debug, Method, and DramaManager.path.

◆ ValidateSignature()

void DramaInvokeDetail.ValidateSignature ( )
inlineprivate

Definition at line 31 of file DramaInvokeDetail.cs.

32 {
33 if (!(Method == null))
34 {
35 if (Method.ContainsGenericParameters)
36 {
37 throw new NotSupportedException("#drama action cannot contain generic parameters");
38 }
39 if (!Method.IsStatic)
40 {
41 throw new NotSupportedException("#drama action must be static");
42 }
43 ParameterInfo[] parameters = Method.GetParameters();
44 if (parameters.Length < 2 || parameters[0].ParameterType != typeof(DramaManager) || parameters[1].ParameterType != typeof(Dictionary<string, string>) || Method.ReturnType != typeof(bool))
45 {
46 throw new ArgumentException("#drama action parameters must start with 'DramaManager dm, Dictionary<string, string>' and returns 'bool'");
47 }
48 }
49 }

References Method.

Referenced by DramaInvokeDetail().

Member Data Documentation

◆ _argumentException

readonly ConstructorInfo DramaInvokeDetail._argumentException = typeof(ArgumentException).GetConstructor(new Type[1] { typeof(string) })
staticprivate

Definition at line 14 of file DramaInvokeDetail.cs.

Referenced by CreateConvertExpression().

◆ _compiled

Func<DramaManager, Dictionary<string, string>, string[], bool> DramaInvokeDetail._compiled
private

Definition at line 10 of file DramaInvokeDetail.cs.

Referenced by SafeInvoke().

◆ _stringIsNullOrEmpty

readonly MethodInfo DramaInvokeDetail._stringIsNullOrEmpty = typeof(string).GetMethod("IsNullOrEmpty", new Type[1] { typeof(string) })
staticprivate

Definition at line 12 of file DramaInvokeDetail.cs.

Referenced by CreateConvertExpression().

Property Documentation

◆ Contract

string DramaInvokeDetail.Contract
get

Definition at line 18 of file DramaInvokeDetail.cs.

18{ get; }

Referenced by DramaInvokeDetail().

◆ Method

MethodInfo DramaInvokeDetail.Method
get

Definition at line 16 of file DramaInvokeDetail.cs.

16{ get; }

Referenced by BuildDelegate(), DramaInvokeDetail(), SafeInvoke(), and ValidateSignature().

◆ NoDiscard

bool DramaInvokeDetail.NoDiscard
get

Definition at line 20 of file DramaInvokeDetail.cs.

◆ ParamPassthrough

bool DramaInvokeDetail.ParamPassthrough
get

Definition at line 22 of file DramaInvokeDetail.cs.


The documentation for this class was generated from the following file: