using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using log4net;
using System;
using System.Linq;
using Loki.Bot;
using Loki.Game;
using Loki.Utilities;
using System.Collections.Generic;
using System.Windows.Documents;
using System.Diagnostics;
using Loki.Game.Objects;
namespace ExamplePlugin
{
internal class ExamplePlugin : IPlugin
{
private static readonly ILog Log = Logger.GetLoggerInstanceForType();
// Do not implement a ctor and do stuff in it.
#region Implementation of IBase
/// <summary>Initializes this plugin.</summary>
public void Initialize()
{
Log.DebugFormat("[ExamplePlugin] Initialize");
// To see if there are specific arguments passed to the program, you can do something like this.
if (CommandLine.Arguments.Exists("myarg1"))
{
var myarg1 = CommandLine.Arguments.Single("myarg1");
Log.DebugFormat("myarg1: {0}", myarg1);
}
ScriptManager.Initialize(null, new List<string> {"Loki.Game"});
}
#endregion
#region Implementation of IAuthored
/// <summary> The name of the plugin. </summary>
public string Name
{
get { return "ExamplePlugin"; }
}
/// <summary> The description of the plugin. </summary>
public string Description
{
get { return "An example plugin for Exilebuddy."; }
}
/// <summary>The author of the plugin.</summary>
public string Author
{
get { return "Bossland GmbH"; }
}
/// <summary>The version of the plugin.</summary>
public Version Version
{
get { return new Version(0, 0, 1, 1); }
}
#endregion
#region Implementation of IRunnable
/// <summary> The plugin start callback. Do any initialization here. </summary>
public void Start()
{
Log.DebugFormat("[ExamplePlugin] Start");
}
Stopwatch _displayStopwatch = Stopwatch.StartNew();
/// <summary> The plugin tick callback. Do any update logic here. </summary>
public void Tick()
{
if(_displayStopwatch.ElapsedMilliseconds > 5000)
{
foreach(var monster in LokiPoe.ObjectManager.GetObjectsByType<Monster>().Where(m=>m.IsActive).OrderBy(m=>m.Distance))
{
Log.DebugFormat("[{2}] {0} {1}", monster.Name, monster.Position, monster.Id);
}
_displayStopwatch.Restart();
}
}
/// <summary> The plugin stop callback. Do any pre-dispose cleanup here. </summary>
public void Stop()
{
Log.DebugFormat("[ExamplePlugin] OnStop");
}
#endregion
#region Implementation of IConfigurable
/// <summary> The routine's settings control. This will be added to the Exilebuddy Settings tab.</summary>
public UserControl Control
{
get
{
using (var fs = new FileStream(@"Plugins\ExamplePlugin\SettingsGui.xaml", FileMode.Open))
{
var root = (UserControl) XamlReader.Load(fs);
// TODO: Your settings binding here.
// TODO: Your settings event handlers here.
var configButton = Wpf.FindControlByName<Button>(root, "ConfigButton");
configButton.Click += ConfigButtonOnClick;
var executePythonButton = Wpf.FindControlByName<Button>(root, "ExecutePythonButton");
executePythonButton.Click += ExecutePythonButtonOnClick;
_pyInputRichTextBox = Wpf.FindControlByName<RichTextBox>(root, "PyInputRichTextBox");
return root;
}
}
}
/// <summary>The settings object. This will be registered in the current configuration.</summary>
public JsonSettings Settings
{
get { return ExamplePluginSettings.Instance; }
}
#endregion
private RichTextBox _pyInputRichTextBox;
private void ExecutePythonButtonOnClick(object sender, RoutedEventArgs routedEventArgs)
{
if (_pyInputRichTextBox == null)
{
Log.ErrorFormat("[ExecutePythonButtonOnClick] _pyInputRichTextBox == null");
return;
}
var myText =
new TextRange(_pyInputRichTextBox.Document.ContentStart, _pyInputRichTextBox.Document.ContentEnd).Text;
ScriptManager.GetStatement(myText)();
}
private void ConfigButtonOnClick(object sender, RoutedEventArgs routedEventArgs)
{
new WinForms().ShowDialog();
}
#region Implementation of IEnableable
/// <summary> The plugin is being enabled.</summary>
public void OnEnable()
{
Log.DebugFormat("[ExamplePlugin] OnEnable");
// Hooks us up into the main bot gui.
LokiPoe.OnGuiTick += OnGuiTick;
}
/// <summary> The plugin is being disabled.</summary>
public void OnDisable()
{
Log.DebugFormat("[ExamplePlugin] OnDisable");
// Removes us from the main bot gui.
LokiPoe.OnGuiTick -= OnGuiTick;
}
#endregion
#region Implementation of IDisposable
/// <summary> </summary>
public void Dispose()
{
}
#endregion
#region Override of Object
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Name + ": " + Description;
}
#endregion
//private bool _start = true;
//private bool _stop;
//private Stopwatch sw = new Stopwatch();
private void OnGuiTick(object sender, GuiTickEventArgs guiTickEventArgs)
{
var window = guiTickEventArgs.Window;
Log.DebugFormat("[ExamplePlugin] OnGuiTick | {0} | {1}", window.Title, Environment.TickCount);
// This shows an example of switching routines from the pluign. The bot must be stopped first.
/*if (!BotManager.IsRunning)
{
var nr = RoutineManager.Routines.FirstOrDefault(r => r.Name == "NullRoutine");
RoutineManager.CurrentRoutine = nr;
}*/
// This shows an example of starting and stopping the bot on demand. This is similar to how
// it was done before, except this is from the gui thread, and not the main bot thread.
/*if (_start)
{
if (!BotManager.IsRunning)
{
BotManager.Start();
_start = false;
_stop = true;
sw.Restart();
}
}*/
/*if (_stop)
{
if (sw.IsRunning && sw.ElapsedMilliseconds > 10000)
{
if (BotManager.IsRunning)
{
BotManager.Stop();
_stop = false;
}
}
}*/
}
}
}