Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set environment variables and throw errors from scripts #105

Merged
merged 5 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 81 additions & 58 deletions EOBot/ArgumentsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public enum ArgsError
InvalidSimultaneousNumberOfBots,
InvalidWaitFlag,
InvalidInitDelay,
InvalidPath
InvalidPath,
InvalidScriptArgs,
AutoConnectRequired
}

public class ArgumentsParser
Expand All @@ -42,80 +44,101 @@ public class ArgumentsParser

public List<string> UserArgs { get; internal set; }

public bool ExtendedHelp { get; private set; }


public ArgumentsParser(string[] args)
{
InitDelay = 1100;

Error = ArgsError.NoError;

if (args.Length < 5)
if ((!args.Contains("--") && args.Select(x => x.ToLower()).Contains("help")) ||
(args.Contains("--") && args.TakeWhile(x => x != "--").Select(x => x.ToLower()).Contains("help")))
{
Error = ArgsError.WrongNumberOfArgs;
return;
ExtendedHelp = true;
}

for (int i = 0; i < args.Length; i++)
else
{
var arg = args[i];

if (arg == "--")
for (int i = 0; i < args.Length; i++)
{
UserArgs = new List<string>();
for (i = i + 1; i < args.Length; i++)
var arg = args[i];

if (arg == "--")
{
UserArgs.Add(args[i]);
UserArgs = new List<string>();
for (i = i + 1; i < args.Length; i++)
{
UserArgs.Add(args[i]);
}
break;
}
break;
}

var pair = arg.ToLower().Split('=');
var pair = arg.ToLower().Split('=');

if (pair.Length != 2)
{
Error = ArgsError.BadFormat;
return;
if (pair.Length != 2)
{
Error = ArgsError.BadFormat;
return;
}

switch (pair[0])
{
case "script":
if (!File.Exists(pair[1]))
{
Error = ArgsError.InvalidPath;
return;
}
ScriptFile = pair[1];
break;
case "autoconnect":
AutoConnect = bool.Parse(pair[1]);
break;
case "host":
ParseHost(pair[1]);
break;
case "port":
if (!ParsePort(pair[1]))
return;
break;
case "bots":
if (!ParseNumBots(pair))
return;
break;
case "initdelay":
if (!ParseInitDelay(pair[1]))
return;
break;
case "account":
Account = pair[1];
break;
case "password":
Password = pair[1];
break;
case "character":
Character = pair[1];
break;
default:
Error = ArgsError.BadFormat;
return;
}
}

switch (pair[0])
if (ScriptFile == null)
{
case "script":
if (!File.Exists(pair[1]))
{
Error = ArgsError.InvalidPath;
return;
}
ScriptFile = pair[1];
break;
case "autoconnect":
AutoConnect = bool.Parse(pair[1]);
break;
case "host":
ParseHost(pair[1]);
break;
case "port":
if (!ParsePort(pair[1]))
return;
break;
case "bots":
if (!ParseNumBots(pair))
return;
break;
case "initdelay":
if (!ParseInitDelay(pair[1]))
return;
break;
case "account":
Account = pair[1];
break;
case "password":
Password = pair[1];
break;
case "character":
Character = pair[1];
break;
default:
Error = ArgsError.BadFormat;
return;
if (Host == null || Port == 0 || NumBots == 0 || Account == null || Password == null || Character == null)
{
Error = ArgsError.WrongNumberOfArgs;
}
else if (UserArgs != null || !AutoConnect)
{
Error = ArgsError.InvalidScriptArgs;
}
}
else if (NumBots > 1 && ScriptFile != null && !AutoConnect)
{
Error = ArgsError.AutoConnectRequired;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions EOBot/EOBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="IBot.cs" />
<Compile Include="IBotFactory.cs" />
<Compile Include="Interpreter\BotInterpreter.cs" />
<Compile Include="Interpreter\BotScriptErrorException.cs" />
<Compile Include="Interpreter\BotToken.cs" />
<Compile Include="Interpreter\BotTokenParser.cs" />
<Compile Include="Interpreter\BotTokenType.cs" />
Expand Down
13 changes: 13 additions & 0 deletions EOBot/Interpreter/BotScriptErrorException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace EOBot.Interpreter
{
public class BotScriptErrorException : Exception
{
public BotScriptErrorException(string message)
: base(message) { }

public BotScriptErrorException(string message, BotToken token)
: base($"Error at line {token.LineNumber} column {token.Column}: {message}") { }
}
}
15 changes: 9 additions & 6 deletions EOBot/Interpreter/BuiltInIdentifierConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public void SetupBuiltInFunctions()
_state.SymbolTable[PredefinedIdentifiers.SLEEP_FUNC] = Readonly(new VoidFunction<int>(PredefinedIdentifiers.SLEEP_FUNC, param1 => Thread.Sleep(param1)));
_state.SymbolTable[PredefinedIdentifiers.TIME_FUNC] = Readonly(new Function<string>(PredefinedIdentifiers.TIME_FUNC, () => DateTime.Now.ToLongTimeString()));
_state.SymbolTable[PredefinedIdentifiers.OBJECT_FUNC] = Readonly(new Function<ObjectVariable>(PredefinedIdentifiers.OBJECT_FUNC, () => new ObjectVariable()));
_state.SymbolTable[PredefinedIdentifiers.SETENV_FUNC] = Readonly(new VoidFunction<string, string>(PredefinedIdentifiers.SETENV_FUNC, (varName, varValue) => Environment.SetEnvironmentVariable(varName, varValue, EnvironmentVariableTarget.User)));
_state.SymbolTable[PredefinedIdentifiers.GETENV_FUNC] = Readonly(new Function<string, string>(PredefinedIdentifiers.GETENV_FUNC, varName => Environment.GetEnvironmentVariable(varName, EnvironmentVariableTarget.User)));
_state.SymbolTable[PredefinedIdentifiers.ERROR_FUNC] = Readonly(new VoidFunction<string>(PredefinedIdentifiers.ERROR_FUNC, message => throw new BotScriptErrorException(message)));

BotDependencySetup();
_state.SymbolTable[PredefinedIdentifiers.CONNECT_FUNC] = Readonly(new AsyncVoidFunction<string, int>(PredefinedIdentifiers.CONNECT_FUNC, ConnectAsync));
Expand All @@ -56,12 +59,12 @@ public void SetupBuiltInFunctions()
}
public void SetupBuiltInVariables()
{
_state.SymbolTable[PredefinedIdentifiers.HOST] = (true, new StringVariable(_parsedArgs.Host));
_state.SymbolTable[PredefinedIdentifiers.PORT] = (true, new IntVariable(_parsedArgs.Port));
_state.SymbolTable[PredefinedIdentifiers.USER] = (true, new StringVariable(_parsedArgs.Account));
_state.SymbolTable[PredefinedIdentifiers.PASS] = (true, new StringVariable(_parsedArgs.Password));
_state.SymbolTable[PredefinedIdentifiers.BOTINDEX] = (true, new IntVariable(_botIndex));
_state.SymbolTable[PredefinedIdentifiers.ARGS] = (true, new ArrayVariable(
_state.SymbolTable[PredefinedIdentifiers.HOST] = Readonly(new StringVariable(_parsedArgs.Host));
_state.SymbolTable[PredefinedIdentifiers.PORT] = Readonly(new IntVariable(_parsedArgs.Port));
_state.SymbolTable[PredefinedIdentifiers.USER] = Readonly(new StringVariable(_parsedArgs.Account));
_state.SymbolTable[PredefinedIdentifiers.PASS] = Readonly(new StringVariable(_parsedArgs.Password));
_state.SymbolTable[PredefinedIdentifiers.BOTINDEX] = Readonly(new IntVariable(_botIndex));
_state.SymbolTable[PredefinedIdentifiers.ARGS] = Readonly(new ArrayVariable(
(_parsedArgs.UserArgs ?? new List<string>()).Select(x => new StringVariable(x)).Cast<IVariable>().ToList()));

// default to version 0.0.28
Expand Down
Loading