Skip to content

Commit

Permalink
Support Starting Bot Operation Without Interaction On GUI
Browse files Browse the repository at this point in the history
Support loading and starting a bot via arguments to the windows process as described at https://forum.botengine.org/t/running-the-bot-automatically/1837/14?u=viir

+ Support loading a bot from a file given with the `--load-bot-from-file` process argument.
+ Support starting the bot directly with the `--start-bot` process argument.

To use both options, a complete commandline can look like this:
```
Sanderling.exe --load-bot-from-file="C:\Users\John\Desktop\my-script-to-load.txt" --start-bot
```
  • Loading branch information
Viir committed Dec 6, 2018
1 parent 08c920a commit 0fc6b6d
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 2 deletions.
Binary file modified lib/BotSharp.UI.dll
Binary file not shown.
Binary file modified lib/BotSharp.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion src/Sanderling/Sanderling.Exe/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
62 changes: 62 additions & 0 deletions src/Sanderling/Sanderling.Exe/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bib3;
using McMaster.Extensions.CommandLineUtils;
using System;
using System.Linq;
using System.Windows;
Expand All @@ -8,6 +9,22 @@ namespace Sanderling.Exe
{
public partial class App : Application
{
class CLI
{
static public CLI LastInstance;

[Option(Description = "Path to a file to load a bot from when the application starts.", ShortName = "")]
public string LoadBotFromFile { get; }

[Option(Description = "Start the loaded bot directly.", ShortName = "")]
public bool StartBot { get; }

private void OnExecute()
{
LastInstance = this;
}
}

static public Int64 GetTimeStopwatch() => Bib3.Glob.StopwatchZaitMiliSictInt();

public MainWindow Window => base.MainWindow as MainWindow;
Expand All @@ -26,6 +43,15 @@ public partial class App : Application

Sanderling.Script.Impl.HostToScript UIAPI;

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

WriteLogEntryWithTimeNow(new Log.LogEntry { Startup = new Log.StartupLogEntry { Args = e.Args } });

CommandLineApplication.Execute<CLI>(e.Args);
}

public App()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Expand Down Expand Up @@ -109,6 +135,42 @@ void ActivatedFirstTime()
Window?.AddHandler(System.Windows.Controls.Primitives.ButtonBase.ClickEvent, new RoutedEventHandler(ButtonClicked));

TimerConstruct();

Dispatcher.Invoke(ExecuteCommandsFromArguments);
}

void ExecuteCommandsFromArguments()
{
Exception exception = null;

try
{
var botsNavigation = MainControl.BotsNavigation;

var botFileName = CLI.LastInstance?.LoadBotFromFile;

var bot = 0 < botFileName?.Length ? System.IO.File.ReadAllBytes(botFileName) : null;

if (bot != null)
{
if (CLI.LastInstance.StartBot)
botsNavigation.NavigateIntoOperateBot(bot, true);
else
botsNavigation.NavigateIntoPreviewBot(bot);
}
}
catch (Exception e)
{
exception = e;
}

WriteLogEntryWithTimeNow(new Log.LogEntry
{
ExecuteCommandsFromArguments = new Log.ExecuteCommandsFromArgumentsEntry
{
Exception = exception,
},
});
}

void Timer_Tick(object sender, object e)
Expand Down
4 changes: 4 additions & 0 deletions src/Sanderling/Sanderling.Exe/Sanderling.Exe.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
<Reference Include="Jot, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Jot.1.3.11\lib\Jot.dll</HintPath>
</Reference>
<Reference Include="McMaster.Extensions.CommandLineUtils, Version=2.2.5.0, Culture=neutral, PublicKeyToken=6f71cb76b82f055d, processorArchitecture=MSIL">
<HintPath>..\packages\McMaster.Extensions.CommandLineUtils.2.2.5\lib\net45\McMaster.Extensions.CommandLineUtils.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeAnalysis, Version=1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.1.3.2\lib\net45\Microsoft.CodeAnalysis.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -117,6 +120,7 @@
<Private>True</Private>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Composition.AttributedModel, Version=1.0.30.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Composition.1.0.30\lib\portable-net45+win8+wp8+wpa81\System.Composition.AttributedModel.dll</HintPath>
<Private>True</Private>
Expand Down
1 change: 1 addition & 0 deletions src/Sanderling/Sanderling.Exe/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<package id="fasterflect" version="2.1.3" targetFramework="net461" />
<package id="Fody" version="1.29.4" targetFramework="net461" developmentDependency="true" />
<package id="Jot" version="1.3.11" targetFramework="net461" />
<package id="McMaster.Extensions.CommandLineUtils" version="2.2.5" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Common" version="1.3.2" targetFramework="net461" />
Expand Down
2 changes: 1 addition & 1 deletion src/Sanderling/Sanderling.Exe/resource.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ResourceDictionary Source="pack://application:,,,/Sanderling.UI;component/resource.xaml"></ResourceDictionary>

<ResourceDictionary>
<system:String x:Key="AppVersionId">2018-07-30</system:String>
<system:String x:Key="AppVersionId">2018-12-06</system:String>

<BotEngine.UI.ViewModel:AppProperty
x:Key="AppProperty">
Expand Down
14 changes: 14 additions & 0 deletions src/Sanderling/Sanderling/Log/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,19 @@ public class LogEntry
public DateTimeOffset EntryTime;

public string Text;

public StartupLogEntry Startup;

public ExecuteCommandsFromArgumentsEntry ExecuteCommandsFromArguments;
}

public class StartupLogEntry
{
public string[] Args;
}

public class ExecuteCommandsFromArgumentsEntry
{
public Exception Exception;
}
}

0 comments on commit 0fc6b6d

Please sign in to comment.