forked from ScriptedEvents/ScriptedEvents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptInfoVariables.cs
More file actions
159 lines (130 loc) · 5.61 KB
/
ScriptInfoVariables.cs
File metadata and controls
159 lines (130 loc) · 5.61 KB
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
namespace ScriptedEvents.Variables.Booleans
{
using System;
#pragma warning disable SA1402 // File may only contain a single type
using System.Linq;
using ScriptedEvents.API.Enums;
using ScriptedEvents.API.Extensions;
using ScriptedEvents.API.Modules;
using ScriptedEvents.Structures;
using ScriptedEvents.Variables.Interfaces;
public class ScriptInfoVariables : IVariableGroup
{
/// <inheritdoc/>
public string GroupName => "Script Info";
/// <inheritdoc/>
public IVariable[] Variables { get; } = new IVariable[]
{
new VariableExists(),
new IsScriptRunning(),
new This(),
};
}
public class This : IStringVariable, INeedSourceVariable, IArgumentVariable
{
/// <inheritdoc/>
public string Name => "{THIS}";
/// <inheritdoc/>
public string Description => "Returns information about the script.";
/// <inheritdoc/>
public Argument[] ExpectedArguments => new[]
{
new OptionsArgument("mode", false,
new("AUTORUN", "'TRUE' or 'FALSE' depending on if the script is set to auto-run when the round restarts."),
new("CALLER", "The name of the script that called this script."),
new("CONTEXT", $"The context in which the script was executed. Valid options are: {string.Join(", ", ((ExecuteContext[])Enum.GetValues(typeof(ExecuteContext))).Where(r => r is not ExecuteContext.None))}"),
new("DEBUG", $"'TRUE' or 'FALSE' depending on if the script is in debug mode."),
new("DURATION", $"The amount of time (in seconds) the script has been running."),
new("NAME", "The name of the script."),
new("PATH", "The path to the script on the local directory."),
new("VARIABLES", "Lists variable names available to this script only.")),
};
/// <inheritdoc/>
public string Value
{
get
{
if (Arguments.Length == 0) return Source.Name;
string mode = Arguments[0].ToUpper();
return mode switch
{
"AUTORUN" => Source.HasFlag("AUTORUN").ToUpper(),
"CALLER" => Source.CallerScript is not null ? Source.CallerScript.Name : "NONE",
"CONTEXT" => Source.Context.ToString(),
"DEBUG" => Source.Debug.ToUpper(),
"DURATION" => Source.RunDuration.TotalSeconds.ToString(),
"NAME" => Source.Name,
"PATH" => Source.FilePath ?? "N/A",
"VARIABLES" => Source.UniqueVariables.Keys.Concat(Source.UniquePlayerVariables.Keys).ToArray().Length != 0 ? string.Join(", ", Source.UniqueVariables.Keys.Concat(Source.UniquePlayerVariables.Keys)) : "NONE",
_ => throw new ArgumentException("Invalid mode.", mode)
};
}
}
/// <inheritdoc/>
public string[] RawArguments { get; set; }
/// <inheritdoc/>
public object[] Arguments { get; set; }
/// <inheritdoc/>
public Script Source { get; set; }
}
public class VariableExists : IBoolVariable, IArgumentVariable, INeedSourceVariable
{
/// <inheritdoc/>
public string Name => "{VEXISTS}";
/// <inheritdoc/>
public string ReversedName => "{!VEXISTS}";
/// <inheritdoc/>
public string Description => "Whether or not the variable with the given name exists in the current context.";
/// <inheritdoc/>
public string[] RawArguments { get; set; }
/// <inheritdoc/>
public object[] Arguments { get; set; }
/// <inheritdoc/>
public Argument[] ExpectedArguments => new[]
{
new Argument("variableName", typeof(string), "The name of the variable.", true),
};
/// <inheritdoc/>
public Script Source { get; set; }
/// <inheritdoc/>
public bool Value
{
get
{
if (Arguments.Length < 1) throw new ArgumentException("No variable provided");
if (VariableSystemV2.TryGetPlayers(RawArguments[0], Source, out _, requireBrackets: false))
return true;
if (VariableSystemV2.TryGetVariable(RawArguments[0], Source, out _, false, true))
return true;
return false;
}
}
}
public class IsScriptRunning : IBoolVariable, IArgumentVariable
{
/// <inheritdoc/>
public string Name => "{ISRUNNING}";
/// <inheritdoc/>
public string ReversedName => "{!ISRUNNING}";
/// <inheritdoc/>
public string Description => "Whether or not a specific script is running.";
/// <inheritdoc/>
public string[] RawArguments { get; set; }
/// <inheritdoc/>
public object[] Arguments { get; set; }
/// <inheritdoc/>
public Argument[] ExpectedArguments => new[]
{
new Argument("scriptName", typeof(string), "The name of the script.", true),
};
/// <inheritdoc/>
public bool Value
{
get
{
string scriptName = (string)Arguments[0];
return MainPlugin.ScriptModule.RunningScripts.Any(scr => scr.Key.Name == scriptName && scr.Value.IsRunning);
}
}
}
}