1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
namespace AdvancedInspector
{
/// <summary>
/// When a property is flagged this way, a help box is added after the inspector's field.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
public class HelpAttribute : Attribute, IRuntimeAttribute<HelpAttribute>
{
public const string IsNull = "HelpAttribute.IsValueNull";
public const string IsNullOrEmpty = "HelpAttribute.IsStringNullOrEmpty";
public const string IsMatch = "HelpAttribute.IsRegexMatch";
public delegate HelpAttribute HelpDelegate();
public delegate HelpAttribute HelpStaticDelegate(HelpAttribute help, object instance, object value);
private HelpType type;
/// <summary>
/// Help type.
/// Displays a specific icon.
/// </summary>
public HelpType Type
{
get { return type; }
set { type = value; }
}
private string message;
/// <summary>
/// Help message.
/// </summary>
public string Message
{
get { return message; }
set { message = value; }
}
private HelpPosition position = HelpPosition.After;
/// <summary>
/// By default, the helpbox is drawn after the field.
/// If this is false, it is drawn before the field.
/// </summary>
public HelpPosition Position
{
get { return position; }
set { position = value; }
}
private string regex;
/// <summary>
/// When using the IsRegex conditional, this string is used as a regular expresion.
/// </summary>
public string Regex
{
get { return regex; }
set { regex = value; }
}
#region IRuntime Implementation
private string methodName = "";
public string MethodName
{
get { return methodName; }
}
public Type Template
{
get { return typeof(HelpDelegate); }
}
public Type TemplateStatic
{
get { return typeof(HelpStaticDelegate); }
}
private List<Delegate> delegates = new List<Delegate>();
public List<Delegate> Delegates
{
get { return delegates; }
set { delegates = value; }
}
public HelpAttribute Invoke(int index, object instance, object value)
{
if (delegates.Count == 0 || index >= delegates.Count)
return this;
try
{
if (delegates[index].Target == null)
{
return delegates[0].DynamicInvoke(this, instance, value) as HelpAttribute;
}
else
{
return delegates[0].DynamicInvoke() as HelpAttribute;
}
}
catch (Exception e)
{
if (e is TargetInvocationException)
e = ((TargetInvocationException)e).InnerException;
Debug.LogError(string.Format("Invoking a method failed while trying to retrieve a Help attribute. The exception was \"{0}\".", e.Message));
return null;
}
}
#endregion
public HelpAttribute(string methodName)
: this(methodName, HelpType.None, HelpPosition.After, "") { }
public HelpAttribute(string methodName, HelpType type, string message)
: this(methodName, type, HelpPosition.After, message) { }
public HelpAttribute(HelpType type, string message)
: this("", type, HelpPosition.After, message) { }
public HelpAttribute(HelpType type, HelpPosition position, string message)
: this("", type, position, message) { }
public HelpAttribute(string methodName, HelpType type, HelpPosition position, string message)
{
this.methodName = methodName;
this.type = type;
this.position = position;
this.message = message;
}
public HelpAttribute(Delegate method)
{
this.delegates.Add(method);
}
private static HelpAttribute IsValueNull(HelpAttribute help, object instance, object value)
{
if (value == null || (value is UnityEngine.Object && ((UnityEngine.Object)value) == null))
{
return help;
}
return null;
}
private static HelpAttribute IsStringNullOrEmpty(HelpAttribute help, object instance, object value)
{
if (value is string && string.IsNullOrEmpty((string)value))
return help;
return null;
}
private static HelpAttribute IsRegexMatch(HelpAttribute help, object instance, object value)
{
if (value == null)
return null;
string text = value.ToString();
if (System.Text.RegularExpressions.Regex.IsMatch(text, help.regex))
return help;
return null;
}
}
/// <summary>
/// Because the internal enum for help display is Editor only.
/// </summary>
public enum HelpType
{
None = 0,
Info = 1,
Warning = 2,
Error = 3,
}
/// <summary>
/// The position where the help box should be placed.
/// </summary>
public enum HelpPosition
{
After,
Before
}
}
|