Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * BotSharp - An Anarchy Online ChatBot
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License only.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
- */
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using BotSharp.Library;
- using System.Diagnostics;
- using BotSharp.Library.xml;
- using Demoder.Common.Serialization;
- using Demoder.Common;
- using Demoder.Common.Cache;
- using System.Linq;
- using BotSharp.Launcher.DataClasses;
- using System.Reflection;
- using BotSharp.Library.API;
- namespace BotSharp.Launcher
- {
- internal class Program
- {
- internal static int ConsoleWidth = (int)Math.Round((double)Console.LargestWindowWidth * (double)0.8, 0);
- private const string DefaultPluginsDir = "Plugins";
- private const string DefaultBotsDir = "Bots";
- private const string DefaultLauncherConfigPath = "Launcher.Config.xml";
- private static Dictionary<string, BotCore> botcores = new Dictionary<string, BotCore>();
- private static bool running = true;
- private static string botDir;
- private static string launcherConfigPath;
- private static LauncherConfig launcherConfig = null;
- static void Main(string[] args)
- {
- //Set console options. Mono will fail on window size settings, therefore: try/catch
- try
- {
- Console.Title = "Bot# - Version: Alpha";
- Console.WindowWidth = ConsoleWidth;
- Console.BufferWidth = ConsoleWidth;
- Console.WindowHeight = 40;
- Console.BufferHeight = 100;
- }
- catch
- {
- }
- // Output debug from Vha.Net
- #if TRACE
- Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
- #endif
- ParseCommandLineArgs(args);
- launcherConfig = Xml.Deserialize<LauncherConfig>(new FileInfo(launcherConfigPath), false);
- if (launcherConfig == null) { launcherConfig = new LauncherConfig(); }
- ConsoleFormatting.WriteAboutBox();
- Assembly plugins;
- using (PluginCompiler pcomp = new PluginCompiler())
- {
- plugins = pcomp.Compile();
- }
- if (!Directory.Exists(botDir) || (Directory.GetDirectories(botDir).Length == 0))
- {
- Directory.CreateDirectory(Path.Combine(botDir,"ExampleBot"));
- }
- // Setup BotConnector
- BotConnector.SetUseableIPs(launcherConfig.IPAddresses);
- Tuple<Dimension,string>[] superAdmins = launcherConfig.SuperAdministrators.Select(e=> new Tuple<Dimension,string>(e.Dimension, e.Nickname)).ToArray();
- foreach (string dir in Directory.GetDirectories(botDir, "*", SearchOption.TopDirectoryOnly))
- {
- AppDomain botDomain = AppDomain.CreateDomain(dir);
- botDomain.SetData("dir", dir);
- botDomain.SetData("pluginAssemlyLocation", plugins.Location);
- botDomain.DoCallBack(new CrossAppDomainDelegate(StartBot));
- }
- while (running)
- {
- string input = Console.ReadLine();
- if (input == "exit"){ running = false; }
- }
- }
- public static void StartBot()
- {
- BotCore bc = new BotCore(new DirectoryInfo(AppDomain.CurrentDomain.GetData("dir").ToString()),
- new Cache(new DirectoryInfo(@"Cache")), new Tuple<Dimension, string>[] { });
- bc.LoadPlugins(Assembly.LoadFile(AppDomain.CurrentDomain.GetData("pluginAssemlyLocation").ToString()));
- bc.Connect();
- }
- private static void ParseCommandLineArgs(string[] args)
- {
- CommandLineParameters clp = new CommandLineParameters(args);
- // Bot directory
- botDir = clp.Argument("botdir");
- if (string.IsNullOrWhiteSpace(botDir))
- {
- botDir = DefaultBotsDir;
- if (!Directory.Exists(botDir))
- {
- Directory.CreateDirectory(botDir);
- }
- }
- // Plugin directory
- string plugindir = clp.Argument("plugindir");
- if (!String.IsNullOrWhiteSpace(plugindir))
- {
- string[] plugindirs = plugindir.Split(',');
- PluginCompiler.PluginDirectories.AddRange(plugindirs);
- }
- if (!PluginCompiler.PluginDirectories.Any())
- {
- PluginCompiler.PluginDirectories.Add(DefaultPluginsDir);
- if (!Directory.Exists(DefaultPluginsDir))
- {
- Directory.CreateDirectory(DefaultPluginsDir);
- }
- }
- // Launcher config
- string launcherConfig = clp.Argument("launcherconfig");
- if (!String.IsNullOrWhiteSpace(launcherConfig))
- {
- launcherConfigPath = launcherConfig;
- }
- else
- {
- launcherConfigPath = DefaultLauncherConfigPath;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment