Skip to content

Commit e4aacad

Browse files
committed
2 parents c3d783f + 2d1a0cf commit e4aacad

File tree

16 files changed

+89
-126
lines changed

16 files changed

+89
-126
lines changed

Code/ArgumentSystem/BaseArguments/Argument.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public abstract class Argument(string name)
66
{
77
public string Name { get; } = name;
88

9+
public bool IsRequired => DefaultValue is null;
10+
911
/// <summary>
1012
/// Allows for this argument to get an unlimited amount of values of this type
1113
/// Every value after this argument also counts towards this one.

Code/ContextSystem/Contexter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ private static Result TryAddResult(
4848
Stack<StatementContext> statementStack,
4949
List<Context> contexts
5050
) {
51-
Result rs = $"Invalid context {context} in line {lineNum}.";
51+
Result rs = $"Invalid context {context}";
5252

53-
Log.Debug($"Trying to add context {context} in line {lineNum}");
53+
Log.Debug($"Trying to add context {context}");
5454

5555
switch (context)
5656
{

Code/ContextSystem/Contexts/MethodContext.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using SER.Code.TokenSystem.Tokens;
1515
using SER.Code.ValueSystem;
1616
using MethodToken = SER.Code.TokenSystem.Tokens.MethodToken;
17-
using ReturningMethod = SER.Code.MethodSystem.BaseMethods.Synchronous.ReturningMethod;
1817

1918
namespace SER.Code.ContextSystem.Contexts;
2019

@@ -53,9 +52,12 @@ public override TryAddTokenRes TryAddToken(BaseToken token)
5352

5453
public override Result VerifyCurrentState()
5554
{
56-
return Result.Assert(_providedArguments >= Method.ExpectedArguments.Count(arg => arg.DefaultValue is null),
55+
return Result.Assert(_providedArguments >= Method.ExpectedArguments.Count(arg => arg.IsRequired),
5756
$"Method '{Method.Name}' is missing required arguments: " +
58-
$"{", ".Join(Method.ExpectedArguments.Skip(_providedArguments).Select(arg => arg.Name))}");
57+
$"{", ".Join(Method.ExpectedArguments
58+
.Skip(_providedArguments)
59+
.Where(arg => arg.IsRequired)
60+
.Select(arg => arg.Name))}");
5961
}
6062

6163
protected override IEnumerator<float> Execute()

Code/Helpers/Compiler/CompilerRequiredAttributes.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

Code/Helpers/Compiler/IsExternalInit.cs.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

Code/Helpers/Compiler/NotNullWhenAttribute.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

Code/Helpers/Extensions/StringExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class StringExtensions
1313
[Pure]
1414
public static string LowerFirst(this string str)
1515
{
16-
return str.Substring(0, 1).ToLower() + str.Substring(1);
16+
return str[0].ToString().ToLower() + str[1..];
1717
}
1818

1919
// python ahh
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using JetBrains.Annotations;
2+
using MEC;
3+
using Newtonsoft.Json.Linq;
4+
using SER.Code.ArgumentSystem.Arguments;
5+
using SER.Code.ArgumentSystem.BaseArguments;
6+
using SER.Code.Helpers.Extensions;
7+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8+
using SER.Code.MethodSystem.MethodDescriptors;
9+
using SER.Code.MethodSystem.Methods.HTTPMethods;
10+
11+
namespace SER.Code.MethodSystem.Methods.DiscordMethods;
12+
13+
[UsedImplicitly]
14+
public class SendDiscordMessageMethod : SynchronousMethod, ICanError
15+
{
16+
public override string Description => "Sends a message using a discord webhook.";
17+
18+
public string[] ErrorReasons => HTTPPostMethod.HttpErrorReasons;
19+
20+
public override Argument[] ExpectedArguments { get; } =
21+
[
22+
new TextArgument("webhook url"),
23+
new TextArgument("message content"),
24+
new TextArgument("webhook name")
25+
{
26+
DefaultValue = new(null, "default")
27+
},
28+
new TextArgument("avatar url")
29+
{
30+
DefaultValue = new(null, "default")
31+
}
32+
];
33+
34+
public override void Execute()
35+
{
36+
var webhookUrl = Args.GetText("webhook url");
37+
var messageContent = Args.GetText("message content");
38+
var webhookName = Args.GetText("webhook name").MaybeNull();
39+
var avatarUrl = Args.GetText("avatar url").MaybeNull();
40+
41+
JObject json = new()
42+
{
43+
["content"] = messageContent
44+
};
45+
46+
if (webhookName != null) json["username"] = webhookName;
47+
if (avatarUrl != null) json["avatar_url"] = avatarUrl;
48+
49+
Timing.RunCoroutine(HTTPPostMethod.SendPost(this, webhookUrl, json.ToString()));
50+
}
51+
}

Code/MethodSystem/Methods/GeneralVariableMethods/GlobalVariableMethod.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using JetBrains.Annotations;
22
using SER.Code.ArgumentSystem.Arguments;
33
using SER.Code.ArgumentSystem.BaseArguments;
4-
using SER.Code.Helpers.Exceptions;
54
using SER.Code.MethodSystem.BaseMethods.Synchronous;
65
using SER.Code.MethodSystem.MethodDescriptors;
7-
using SER.Code.TokenSystem.Tokens.VariableTokens;
86
using SER.Code.VariableSystem;
97

108
namespace SER.Code.MethodSystem.Methods.GeneralVariableMethods;
@@ -21,17 +19,12 @@ public class GlobalVariableMethod : SynchronousMethod, ICanError
2119

2220
public override Argument[] ExpectedArguments { get; } =
2321
[
24-
new TokenArgument<VariableToken>("variable to make global")
22+
new VariableArgument("variable to make global")
2523
];
2624

2725
public override void Execute()
2826
{
29-
var variableToken = Args.GetToken<VariableToken>("variable to make global");
30-
if (variableToken.TryGetVariable().HasErrored(out var error, out var variable))
31-
{
32-
throw new ScriptRuntimeError(this, error);
33-
}
34-
27+
var variable = Args.GetVariable("variable to make global");
3528
VariableIndex.GlobalVariables.RemoveAll(existingVar => existingVar.Name == variable.Name);
3629
VariableIndex.GlobalVariables.Add(variable);
3730
}

Code/MethodSystem/Methods/HTTPMethods/HTTPGetMethod.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ public class HTTPGetMethod : YieldingReferenceReturningMethod<JObject>, ICanErro
1818
public override string Description =>
1919
"Sends a GET request to a provided URL and returns the response as a JSON object.";
2020

21-
public string[] ErrorReasons =>
22-
[
23-
"Fetched value from the URL is not a valid JSON object.",
24-
nameof(UnityWebRequest.Result.ConnectionError),
25-
nameof(UnityWebRequest.Result.DataProcessingError),
26-
nameof(UnityWebRequest.Result.ProtocolError)
21+
public string[] ErrorReasons => [
22+
..HTTPPostMethod.HttpErrorReasons,
23+
"Provided response was not a valid JSON object."
2724
];
2825

2926
public override Argument[] ExpectedArguments { get; } =
@@ -38,12 +35,11 @@ public override IEnumerator<float> Execute()
3835
using UnityWebRequest webRequest = UnityWebRequest.Get(address);
3936

4037
yield return Timing.WaitUntilDone(webRequest.SendWebRequest());
41-
42-
if (webRequest.result != UnityWebRequest.Result.Success)
38+
39+
if (webRequest.error is { } error)
4340
{
44-
throw new ScriptRuntimeError(
45-
this,
46-
$"Address {address} has returned {webRequest.result} ({webRequest.responseCode}): {webRequest.error}"
41+
throw new ScriptRuntimeError(this,
42+
$"Address {address} has returned an error: {error}"
4743
);
4844
}
4945

0 commit comments

Comments
 (0)