Program.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using Pomelo;
  2. using System;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.Dynamic;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using CommonLang.Log;
  10. using System.Runtime.InteropServices;
  11. using System.Xml;
  12. using CommonLang.Xml;
  13. using System.Xml.Linq;
  14. using CommonLang.Vector;
  15. using System.Text;
  16. using System.Security.Cryptography;
  17. using CommonAI.Data;
  18. using CommonLang;
  19. using CommonAI.Zone.Helper;
  20. namespace test
  21. {
  22. public class Program
  23. {
  24. private static bool isRunning = false;
  25. private static bool isTest = false;
  26. public static void Main(string[] args)
  27. {
  28. try
  29. {
  30. DisbleQuickEditMode();
  31. dynamic intput = new ExpandoObject();
  32. isRunning = true;
  33. // -- ZoneConfig
  34. var zoneConfig = new ZoneConfig();
  35. zoneConfig.startPath = Environment.CurrentDirectory;
  36. zoneConfig.assetPath = GetConfigAsString("zoneConfig.assetPath");
  37. intput.zoneConfig = zoneConfig;
  38. // -- logConfig
  39. var logConfig = new LogConfig();
  40. logConfig.serverId = GetConfigAsString("game.server.id");
  41. logConfig.outputPath = GetConfigAsString("logConfig.outputPath");
  42. logConfig.level = GetConfigLoglevel("logConfig.level");
  43. string socketPort = "socket_" + GetConfigAsString("fastStreamConfig.port");
  44. //log4j默认的配置文件名,其实也不能修改
  45. string sourceFile = "log4net";// GetConfigAsString("logConfig.configFile");
  46. logConfig.configFile = sourceFile + ".config";
  47. //根据服务器id,重载log文件配置
  48. string logExt = string.IsNullOrEmpty(logConfig.serverId) ? socketPort : logConfig.serverId;
  49. ReloadLogConfig(sourceFile, logConfig.outputPath + logExt, logConfig.configFile);
  50. LoggerFactory.SetFactory(new PomeloLog(logConfig));
  51. if (isTest)
  52. {
  53. testLog(logConfig);
  54. return;
  55. }
  56. intput.logConfig = logConfig;
  57. // -- iceConfig
  58. var iceConfig = new IceConfig();
  59. iceConfig.host = GetConfigAsString("iceConfig.host");
  60. iceConfig.port = GetConfigAsInt("iceConfig.port");
  61. iceConfig.isTraceNetwork = GetConfigAsBool("iceConfig.isTraceNetwork");
  62. iceConfig.isTraceProtocol = GetConfigAsBool("iceConfig.isTraceProtocol");
  63. iceConfig.isWarnConnections = GetConfigAsBool("iceConfig.isWarnConnections");
  64. intput.iceConfig = iceConfig;
  65. GlobalData.GAME_BS_TEST = GetConfigAsBool("game.bstest") || (logConfig.serverId.Equals("10004") && iceConfig.port == 9910);
  66. // -- fastStreamConfig
  67. var fastStreamConfig = new FastStreamConfig();
  68. fastStreamConfig.port = GetConfigAsInt("fastStreamConfig.port");
  69. intput.fastStreamConfig = fastStreamConfig;
  70. Task.WaitAll(new EdgeJS().init(intput));
  71. }
  72. catch (Exception e)
  73. {
  74. isRunning = false;
  75. Console.WriteLine(e);//捕获异常,输出异常信息
  76. Console.ReadKey();
  77. return;
  78. }
  79. //
  80. StartCommandConsoleThread();
  81. // -- 启动gameServer
  82. var startBatFile = GetConfigAsString("gameServer.start.bat");
  83. if (!string.IsNullOrEmpty(startBatFile))
  84. {
  85. var start = new ProcessStartInfo();
  86. start.WorkingDirectory = Path.GetDirectoryName(startBatFile);
  87. start.FileName = startBatFile;
  88. start.CreateNoWindow = false;
  89. start.UseShellExecute = false;
  90. start.RedirectStandardInput = false;
  91. start.RedirectStandardOutput = false;
  92. Process pro = null;
  93. try
  94. {
  95. pro = Process.Start(start);
  96. pro.WaitForExit();
  97. }
  98. catch (Exception e)
  99. {
  100. Console.WriteLine(e.Message);//捕获异常,输出异常信息
  101. }
  102. finally
  103. {
  104. pro.Close();
  105. }
  106. }
  107. isRunning = false;
  108. }
  109. public static void testLog(LogConfig logConfig)
  110. {
  111. Logger defLog = LoggerFactory.GetDefLogger();
  112. Logger testLog = LoggerFactory.GetLogger("test");
  113. defLog.Debug("debug def");
  114. defLog.Info("info def");
  115. defLog.Warn("warn def");
  116. defLog.Error("error def");
  117. defLog.DebugFormat("{0} format","def debug");
  118. defLog.InfoFormat("{0} format", "def info");
  119. defLog.WarnFormat("{0} format", "def warn");
  120. defLog.ErrorFormat("{0} format", "def error");
  121. testLog.Debug("debug test");
  122. testLog.Info("info test");
  123. testLog.Warn("warn test");
  124. testLog.Error("error test");
  125. testLog.DebugFormat("{0} format", "test debug");
  126. testLog.InfoFormat("{0} format", "test info");
  127. testLog.WarnFormat("{0} format", "test warn");
  128. testLog.ErrorFormat("{0} format", "test error");
  129. }
  130. #region 关闭控制台 快速编辑模式、插入模式
  131. const int STD_INPUT_HANDLE = -10;
  132. const uint ENABLE_QUICK_EDIT_MODE = 0x0040;
  133. const uint ENABLE_INSERT_MODE = 0x0020;
  134. [DllImport("kernel32.dll", SetLastError = true)]
  135. internal static extern IntPtr GetStdHandle(int hConsoleHandle);
  136. [DllImport("kernel32.dll", SetLastError = true)]
  137. internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint mode);
  138. [DllImport("kernel32.dll", SetLastError = true)]
  139. internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint mode);
  140. public static void DisbleQuickEditMode()
  141. {
  142. IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE);
  143. uint mode;
  144. GetConsoleMode(hStdin, out mode);
  145. mode &= ~ENABLE_QUICK_EDIT_MODE;//移除快速编辑模式
  146. mode &= ~ENABLE_INSERT_MODE; //移除插入模式
  147. SetConsoleMode(hStdin, mode);
  148. }
  149. #endregion
  150. public static void StartCommandConsoleThread()
  151. {
  152. new Thread(()=>
  153. {
  154. while(isRunning)
  155. {
  156. var cmd = Console.ReadLine();
  157. if(cmd == "cls")
  158. {
  159. Console.Clear();
  160. }
  161. }
  162. Thread.Sleep(100);
  163. }).Start();
  164. }
  165. private static void LoglevelConfigErr(string errString)
  166. {
  167. Console.ForegroundColor = ConsoleColor.Red;
  168. Console.WriteLine("/*********************************************************");
  169. Console.WriteLine("*logConfig.level配置错误,logConfig.level=" + errString);
  170. Console.WriteLine("*请检查xmds-battle-server.exe.config文件配置");
  171. Console.WriteLine("**********************************************************");
  172. Console.ForegroundColor = ConsoleColor.White;
  173. }
  174. //吐血。。。一顿操作猛如虎, 最后还得二次转换,
  175. //只加几个简单的转换
  176. private static uint GetConfigLoglevel(string key)
  177. {
  178. uint level = LoggerLevel.INFO;
  179. String logLevel = GetConfigAsString(key);
  180. if (logLevel == null)
  181. {
  182. LoglevelConfigErr(logLevel);
  183. return LoggerLevel.INFO;
  184. }
  185. else
  186. {
  187. logLevel = logLevel.ToUpper();
  188. }
  189. switch (logLevel)
  190. {
  191. case "DEBUG":
  192. level = LoggerLevel.DEBUG | LoggerLevel.INFO | LoggerLevel.WARNNING | LoggerLevel.ERROR;
  193. break;
  194. case "INFO":
  195. level = LoggerLevel.INFO | LoggerLevel.WARNNING | LoggerLevel.ERROR;
  196. break;
  197. case "WARNNING":
  198. level = LoggerLevel.WARNNING | LoggerLevel.ERROR;
  199. break;
  200. case "ERROR":
  201. level = LoggerLevel.ERROR;
  202. break;
  203. default:
  204. level = LoggerLevel.ERROR;
  205. LoglevelConfigErr(logLevel);
  206. break;
  207. }
  208. return level;
  209. }
  210. private static void ReloadLogConfig(string fileName, string path, string saveFileName)
  211. {
  212. try
  213. {
  214. XmlDocument xmlDoc = XmlUtil.LoadXML(fileName);
  215. XmlNode xmlNode = xmlDoc.SelectSingleNode("log4net");
  216. XmlNodeList nodeList = xmlNode.ChildNodes;
  217. for (int i = 0; i < nodeList.Count; i++)
  218. {
  219. XmlNode nodeTemp = nodeList.Item(i);
  220. if (nodeTemp == null || !nodeTemp.Name.Equals("appender"))
  221. {
  222. continue;
  223. }
  224. XmlNode fileAttribute = nodeTemp.SelectSingleNode("file");
  225. if (fileAttribute != null)
  226. {
  227. XmlAttributeCollection attribute = fileAttribute.Attributes;
  228. if (attribute != null && attribute.Count == 1)
  229. {
  230. XmlNode fileNode = attribute.Item(0);
  231. //perf, debug, info, warn, err
  232. if (!fileNode.InnerText.EndsWith(".log"))
  233. {
  234. fileNode.InnerText = path + "/" + fileNode.InnerText + ".log";
  235. }
  236. }
  237. }
  238. }
  239. xmlDoc.Save(saveFileName);
  240. }
  241. catch(Exception e)
  242. {
  243. throw e;
  244. }
  245. }
  246. public static int GetConfigAsInt(string key)
  247. {
  248. var str = ConfigurationManager.AppSettings[key];
  249. if (string.IsNullOrEmpty(str))
  250. {
  251. return 0;
  252. }
  253. return int.Parse(str);
  254. }
  255. public static string GetConfigAsString(string key)
  256. {
  257. return ConfigurationManager.AppSettings[key];
  258. }
  259. public static bool GetConfigAsBool(string key)
  260. {
  261. var str = ConfigurationManager.AppSettings[key];
  262. if (string.IsNullOrEmpty(str))
  263. {
  264. return false;
  265. }
  266. return bool.Parse(str);
  267. }
  268. }
  269. }