Guest User

Untitled

a guest
Oct 18th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. /*
  2. * BotSharp - An Anarchy Online ChatBot
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License only.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  16. * USA
  17. */
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Text;
  21. using System.IO;
  22. using BotSharp.Library;
  23. using System.Diagnostics;
  24. using BotSharp.Library.xml;
  25. using Demoder.Common.Serialization;
  26. using Demoder.Common;
  27. using Demoder.Common.Cache;
  28. using System.Linq;
  29. using BotSharp.Launcher.DataClasses;
  30. using System.Reflection;
  31. using BotSharp.Library.API;
  32.  
  33. namespace BotSharp.Launcher
  34. {
  35. internal class Program
  36. {
  37. internal static int ConsoleWidth = (int)Math.Round((double)Console.LargestWindowWidth * (double)0.8, 0);
  38. private const string DefaultPluginsDir = "Plugins";
  39. private const string DefaultBotsDir = "Bots";
  40. private const string DefaultLauncherConfigPath = "Launcher.Config.xml";
  41.  
  42. private static Dictionary<string, BotCore> botcores = new Dictionary<string, BotCore>();
  43. private static bool running = true;
  44. private static string botDir;
  45. private static string launcherConfigPath;
  46.  
  47. private static LauncherConfig launcherConfig = null;
  48.  
  49. static void Main(string[] args)
  50. {
  51. //Set console options. Mono will fail on window size settings, therefore: try/catch
  52. try
  53. {
  54. Console.Title = "Bot# - Version: Alpha";
  55. Console.WindowWidth = ConsoleWidth;
  56. Console.BufferWidth = ConsoleWidth;
  57. Console.WindowHeight = 40;
  58. Console.BufferHeight = 100;
  59. }
  60. catch
  61. {
  62.  
  63. }
  64. // Output debug from Vha.Net
  65. #if TRACE
  66. Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
  67. #endif
  68. ParseCommandLineArgs(args);
  69.  
  70. launcherConfig = Xml.Deserialize<LauncherConfig>(new FileInfo(launcherConfigPath), false);
  71. if (launcherConfig == null) { launcherConfig = new LauncherConfig(); }
  72.  
  73. ConsoleFormatting.WriteAboutBox();
  74. Assembly plugins;
  75. using (PluginCompiler pcomp = new PluginCompiler())
  76. {
  77. plugins = pcomp.Compile();
  78. }
  79. if (!Directory.Exists(botDir) || (Directory.GetDirectories(botDir).Length == 0))
  80. {
  81. Directory.CreateDirectory(Path.Combine(botDir,"ExampleBot"));
  82. }
  83.  
  84. // Setup BotConnector
  85. BotConnector.SetUseableIPs(launcherConfig.IPAddresses);
  86. Tuple<Dimension,string>[] superAdmins = launcherConfig.SuperAdministrators.Select(e=> new Tuple<Dimension,string>(e.Dimension, e.Nickname)).ToArray();
  87.  
  88. foreach (string dir in Directory.GetDirectories(botDir, "*", SearchOption.TopDirectoryOnly))
  89. {
  90. AppDomain botDomain = AppDomain.CreateDomain(dir);
  91. botDomain.SetData("dir", dir);
  92. botDomain.SetData("pluginAssemlyLocation", plugins.Location);
  93. botDomain.DoCallBack(new CrossAppDomainDelegate(StartBot));
  94. }
  95. while (running)
  96. {
  97. string input = Console.ReadLine();
  98. if (input == "exit"){ running = false; }
  99. }
  100. }
  101.  
  102. public static void StartBot()
  103. {
  104. BotCore bc = new BotCore(new DirectoryInfo(AppDomain.CurrentDomain.GetData("dir").ToString()),
  105. new Cache(new DirectoryInfo(@"Cache")), new Tuple<Dimension, string>[] { });
  106. bc.LoadPlugins(Assembly.LoadFile(AppDomain.CurrentDomain.GetData("pluginAssemlyLocation").ToString()));
  107. bc.Connect();
  108. }
  109.  
  110. private static void ParseCommandLineArgs(string[] args)
  111. {
  112. CommandLineParameters clp = new CommandLineParameters(args);
  113.  
  114. // Bot directory
  115. botDir = clp.Argument("botdir");
  116. if (string.IsNullOrWhiteSpace(botDir))
  117. {
  118. botDir = DefaultBotsDir;
  119. if (!Directory.Exists(botDir))
  120. {
  121. Directory.CreateDirectory(botDir);
  122. }
  123. }
  124.  
  125. // Plugin directory
  126. string plugindir = clp.Argument("plugindir");
  127. if (!String.IsNullOrWhiteSpace(plugindir))
  128. {
  129. string[] plugindirs = plugindir.Split(',');
  130. PluginCompiler.PluginDirectories.AddRange(plugindirs);
  131. }
  132. if (!PluginCompiler.PluginDirectories.Any())
  133. {
  134. PluginCompiler.PluginDirectories.Add(DefaultPluginsDir);
  135. if (!Directory.Exists(DefaultPluginsDir))
  136. {
  137. Directory.CreateDirectory(DefaultPluginsDir);
  138. }
  139. }
  140.  
  141. // Launcher config
  142. string launcherConfig = clp.Argument("launcherconfig");
  143. if (!String.IsNullOrWhiteSpace(launcherConfig))
  144. {
  145. launcherConfigPath = launcherConfig;
  146. }
  147. else
  148. {
  149. launcherConfigPath = DefaultLauncherConfigPath;
  150. }
  151. }
  152. }
  153. }
Add Comment
Please, Sign In to add comment