FormServer.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.Instance;
  3. using CommonAI.Zone.ZoneEditor;
  4. using CommonAIClient.Client;
  5. using CommonAIServer.Connector;
  6. using CommonAIServer.Node;
  7. using CommonFroms;
  8. using CommonFroms.G2D;
  9. using CommonFroms.Utils;
  10. using CommonLang;
  11. using CommonLang.Vector;
  12. using GameEditorPlugin.Win32.BattleClient;
  13. using GameEditorPluginServer.Client;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Drawing;
  18. using System.IO;
  19. using System.Windows.Forms;
  20. namespace GameEditorPluginServer.Server
  21. {
  22. public partial class FormServer : Form
  23. {
  24. private ConsoleOutput console;
  25. private ServerNode server;
  26. private ClientLoader client_loader;
  27. private BattleView battle_view;
  28. private DirectoryInfo dataDir;
  29. public FormServer(DirectoryInfo dataDir, int sceneID, ZoneNodeConfig cfg = null)
  30. {
  31. InitializeComponent();
  32. this.console = new ConsoleOutput();
  33. this.pictureBoxRoomLayer.MouseWheel += new MouseEventHandler(pictureBoxRoomLayer_MouseWheel);
  34. this.Shown += FormServer_Shown;
  35. this.FormClosing += FormServer_FormClosing;
  36. this.FormClosed += FormServer_FormClosed;
  37. this.Disposed += FormServer_Disposed;
  38. this.dataDir = dataDir;
  39. var templates = new EditorTemplates(dataDir.FullName, TemplateManager.MessageCodec);
  40. templates.LoadAllTemplates();
  41. FormLauncher.Templates = templates;
  42. if (cfg == null)
  43. {
  44. cfg = new ZoneNodeConfig();
  45. cfg.GAME_UPDATE_INTERVAL_MS = 1000 / templates.Templates.CFG.SYSTEM_FPS;
  46. cfg.CLIENT_SYNC_OBJECT_IN_RANGE = (int)templates.Templates.CFG.CLIENT_UNIT_MOVE_MODIFY_MAX_RANGE;
  47. cfg.CLIENT_SYNC_OBJECT_OUT_RANGE = (int)(cfg.CLIENT_SYNC_OBJECT_OUT_RANGE * 1.25f);
  48. }
  49. this.server = new ServerNode(templates, cfg, sceneID);
  50. this.client_loader = new ClientLoader(server);
  51. this.server.Node.EnableAOI = chk_EnableAOI.Checked;
  52. this.server.Node.OnZoneStart += Node_OnZoneStart;
  53. this.server.Node.OnZoneStop += Node_OnZoneStop;
  54. this.server.Start();
  55. }
  56. private void FormServer_Disposed(object sender, EventArgs e)
  57. {
  58. server.Dispose();
  59. }
  60. private void FormServer_Shown(object sender, EventArgs e)
  61. {
  62. console.DockTo = this;
  63. console.Show();
  64. }
  65. private void FormServer_FormClosed(object sender, FormClosedEventArgs e)
  66. {
  67. console.Close();
  68. client_loader.DisposeClients();
  69. }
  70. private void FormServer_FormClosing(object sender, FormClosingEventArgs e)
  71. {
  72. }
  73. private void Node_OnZoneStart(BaseZoneNode zone)
  74. {
  75. this.timer2.QueueTask(() =>
  76. {
  77. this.battle_view = new BattleView(server);
  78. this.battle_view.Layer.LayerInit += Layer_LayerInit;
  79. });
  80. }
  81. private void Node_OnZoneStop(BaseZoneNode zone)
  82. {
  83. this.timer2.QueueTask(() =>
  84. {
  85. this.Close();
  86. });
  87. }
  88. private void Layer_LayerInit(CommonAI.ZoneClient.ZoneLayer layer)
  89. {
  90. var bv = battle_view;
  91. if (bv != null)
  92. {
  93. float scale = Math.Max(
  94. ((float)pictureBoxRoomLayer.Width) / bv.Width,
  95. ((float)pictureBoxRoomLayer.Height) / bv.Height);
  96. bv.setCamera(bv.Width / 2, bv.Height / 2);
  97. bv.setCameraScale(scale, scale);
  98. }
  99. this.client_loader.AddRandomClient();
  100. }
  101. private void timer2_Tick(object sender, EventArgs e)
  102. {
  103. if (server.Running)
  104. {
  105. if (server != null)
  106. {
  107. this.Text = "FormServer : " + server.Server.ClientConnectString;
  108. }
  109. if (battle_view != null)
  110. {
  111. battle_view.IsSyncPos = chk_ServerVisible.Checked;
  112. }
  113. if (battle_view != null)
  114. {
  115. battle_view.update();
  116. }
  117. if (chk_ServerVisible.Checked)
  118. {
  119. pictureBoxRoomLayer.Refresh();
  120. }
  121. }
  122. }
  123. //----------------------------------------------------------------------------------------------------------------------
  124. #region __BattleView__
  125. internal class BattleLocalView : BattleLocal
  126. {
  127. private EditorScene mZone;
  128. public BattleLocalView(ServerNode server)
  129. : base(server.Node.DataRoot)
  130. {
  131. this.mZone = server.Node.Zone;
  132. this.Layer.ProcessMessage(new CommonAI.ZoneClient.ClientEnterScene(mZone.Data.ID, mZone.SpaceDivSize, server.Node.Zone.Templates.ResourceVersion));
  133. }
  134. public override EditorScene Zone
  135. {
  136. get { return mZone; }
  137. }
  138. }
  139. internal class BattleView : DisplayBattleLocal<BattleLocalView>
  140. {
  141. public ZoneNode Node { get { return server.Node; } }
  142. public bool IsSyncPos { get; set; }
  143. private ServerNode server;
  144. private long mLastUpdateTime;
  145. public BattleView(ServerNode server)
  146. {
  147. base.ShowGuardRange = false;
  148. base.ShowAttackRange = false;
  149. base.ShowDamageRange = false;
  150. base.ShowName = true;
  151. base.ShowHP = true;
  152. base.ShowSpaceDiv = false;
  153. this.server = server;
  154. base.InitClient(new BattleLocalView(server));
  155. this.server.Node.Zone.OnPostEvent += zone_OnPostEvent;
  156. this.server.Node.Zone.OnUpdate += zone_OnUpdate;
  157. }
  158. private void zone_OnPostEvent(InstanceZone zone, Event e)
  159. {
  160. Battle.onEventHandler(e);
  161. }
  162. private void zone_OnUpdate(CommonAI.Zone.Instance.InstanceZone zone)
  163. {
  164. if (IsSyncPos)
  165. {
  166. SyncPosEvent.UnitPos pos = new SyncPosEvent.UnitPos();
  167. SyncPosEvent.UnitState st = new SyncPosEvent.UnitState();
  168. foreach (CommonAI.Zone.Instance.InstanceZoneObject so in server.Node.Zone.AllObjects)
  169. {
  170. CommonAI.ZoneClient.ZoneObject co = Client.Layer.GetObject(so.ID);
  171. if (co != null)
  172. {
  173. pos = so.GetSyncPos();
  174. co.SyncPos(ref pos);
  175. if (so is InstanceUnit)
  176. {
  177. st = (so as InstanceUnit).GetSyncState();
  178. co.SyncPos(ref st);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. protected override void clientUpdate(int intervalMS)
  185. {
  186. Battle.BeginUpdate(intervalMS);
  187. Battle.Update();
  188. }
  189. public void update()
  190. {
  191. long curTime = CommonLang.CUtils.CurrentTimeMS;
  192. if (mLastUpdateTime == 0)
  193. {
  194. mLastUpdateTime = curTime;
  195. }
  196. int intervalMS = (int)(curTime - mLastUpdateTime);
  197. this.mLastUpdateTime = curTime;
  198. base.update(intervalMS);
  199. base.update_flush();
  200. }
  201. }
  202. //----------------------------------------------------------------------------------------------------------------------
  203. private Vector2 pic_lastMouesDown;
  204. private Vector2 pic_lastCameraPos;
  205. private void pictureBoxRoomLayer_MouseWheel(object sender, MouseEventArgs e)
  206. {
  207. try
  208. {
  209. BattleView bv = battle_view;
  210. if (bv != null)
  211. {
  212. int d = CMath.getDirect(e.Delta);
  213. if (d > 0)
  214. {
  215. float newD = bv.getCameraScale() * 1.1f;
  216. bv.setCameraScale(newD, newD);
  217. }
  218. else if (d < 0)
  219. {
  220. float newD = bv.getCameraScale() / 1.1f;
  221. bv.setCameraScale(newD, newD);
  222. }
  223. }
  224. }
  225. catch (Exception err)
  226. {
  227. Console.WriteLine(err.Message);
  228. }
  229. }
  230. private void pictureBoxRoomLayer_MouseDown(object sender, MouseEventArgs e)
  231. {
  232. pictureBoxRoomLayer.Focus();
  233. BattleView bv = battle_view;
  234. if (bv != null)
  235. {
  236. if (e.Button == MouseButtons.Left)
  237. {
  238. bv.PickObject(
  239. bv.screenToWorldX(e.X),
  240. bv.screenToWorldY(e.Y));
  241. }
  242. else if (e.Button == MouseButtons.Right)
  243. {
  244. pic_lastMouesDown = new Vector2(e.Location.X, e.Location.Y);
  245. pic_lastCameraPos = new Vector2(bv.CameraX, bv.CameraY);
  246. }
  247. }
  248. }
  249. private void pictureBoxRoomLayer_MouseUp(object sender, MouseEventArgs e)
  250. {
  251. pic_lastMouesDown = null;
  252. pic_lastCameraPos = null;
  253. }
  254. private void pictureBoxRoomLayer_MouseMove(object sender, MouseEventArgs e)
  255. {
  256. BattleView bv = battle_view;
  257. if (bv != null)
  258. {
  259. if (e.Button == System.Windows.Forms.MouseButtons.Right)
  260. {
  261. if (pic_lastMouesDown != null)
  262. {
  263. float x = pic_lastCameraPos.X + bv.screenToWorldSizeX(pic_lastMouesDown.X - e.X);
  264. float y = pic_lastCameraPos.Y + bv.screenToWorldSizeY(pic_lastMouesDown.Y - e.Y);
  265. bv.setCamera(x, y);
  266. }
  267. }
  268. }
  269. }
  270. private void pictureBoxRoomLayer_Paint(object sender, PaintEventArgs e)
  271. {
  272. try
  273. {
  274. if (chk_ServerVisible.Checked)
  275. {
  276. BattleView bv = battle_view;
  277. if (bv != null)
  278. {
  279. bv.setWindow(new RectangleF(0, 0, pictureBoxRoomLayer.Width, pictureBoxRoomLayer.Height));
  280. bv.render(e.Graphics);
  281. }
  282. }
  283. }
  284. catch (Exception err)
  285. {
  286. Console.WriteLine(err.Message);
  287. }
  288. }
  289. #endregion
  290. //----------------------------------------------------------------------------------------------------------------------
  291. #region __测试客户端__
  292. private class ClientLoader : TestClientLoader
  293. {
  294. private static int id_gen = 0;
  295. private readonly ServerNode server;
  296. private Random random = new Random();
  297. private List<FormClient> client_list = new List<FormClient>();
  298. public ClientLoader(ServerNode server)
  299. {
  300. this.server = server;
  301. }
  302. public override CommonAI.ZoneServer.CreateUnitInfoR2B GenUnitInfoR2B(int unitID)
  303. {
  304. CommonAI.ZoneServer.CreateUnitInfoR2B ret = new CommonAI.ZoneServer.CreateUnitInfoR2B();
  305. ret.UnitTemplateID = unitID;
  306. return ret;
  307. }
  308. public override Type ZoneFactoryType
  309. {
  310. get { return TemplateManager.Factory.GetType(); }
  311. }
  312. public void DisposeClients()
  313. {
  314. foreach (var f in client_list)
  315. {
  316. f.Dispose();
  317. }
  318. }
  319. public void AddRandomClient()
  320. {
  321. UnitInfo info;
  322. PlayerStartAbilityData start;
  323. var node = server.Node;
  324. if (node.Zone.TryGetTestTemplate(out info, out start, random))
  325. {
  326. var enter = GenUnitInfoR2B(info.ID);
  327. enter.Force = start.START_Force;
  328. var client = new FormTestClient();
  329. client.Init(
  330. server.Config,
  331. info.Name + "_" + id_gen++, // player_uuid
  332. node.SceneID.ToString(),
  333. server.Server.ClientConnectString,
  334. typeof(CommonNetwork.Sockets.NetSession).FullName,
  335. enter);
  336. client_list.Add(client);
  337. client.Show();
  338. }
  339. }
  340. public void AddTestClient()
  341. {
  342. UnitInfo info;
  343. PlayerStartAbilityData start;
  344. var node = server.Node;
  345. if (node.Zone.TryGetTestTemplate(out info, out start, random))
  346. {
  347. var enter = GenUnitInfoR2B(info.ID);
  348. enter.Force = start.START_Force;
  349. FormTestClient.StartLauncher(
  350. server.Config,
  351. info.Name + "_" + id_gen++, // player_uuid
  352. server.Server.ClientConnectString,
  353. node.SceneID,
  354. enter, this, (launcher) =>
  355. {
  356. enter.UnitTemplateID = launcher.UnitTemplateID;
  357. enter.Force = launcher.Force;
  358. var client = new FormTestClient();
  359. client.Init(
  360. server.Config,
  361. launcher.PlayerUUID, // player_uuid
  362. node.SceneID.ToString(),
  363. server.Server.ClientConnectString,
  364. launcher.NetDirver,
  365. enter);
  366. client_list.Add(client);
  367. client.Show();
  368. launcher.Close();
  369. return true;
  370. }).Show();
  371. }
  372. }
  373. }
  374. #endregion
  375. private void btn_AddPlayer_Click(object sender, EventArgs e)
  376. {
  377. client_loader.AddTestClient();
  378. }
  379. private void btn_EmulateDelay_Click(object sender, EventArgs e)
  380. {
  381. int min, max;
  382. server.Server.GetEmulateLaggingMS(out min, out max);
  383. string text = G2DTextDialog.Show(string.Format("{0} - {1}", min, max), "模拟网络延时");
  384. try
  385. {
  386. var kv = text.Split(new char[] { '-' }, 2, StringSplitOptions.RemoveEmptyEntries);
  387. if (kv.Length == 2)
  388. {
  389. min = int.Parse(kv[0]);
  390. max = int.Parse(kv[1]);
  391. server.Server.SetEmulateLaggingMS(min, max);
  392. }
  393. else
  394. {
  395. min = int.Parse(text);
  396. server.Server.SetEmulateLaggingMS(min, min);
  397. }
  398. }
  399. catch (Exception err)
  400. {
  401. MessageBox.Show(err.Message);
  402. }
  403. }
  404. private void btn_Bots_Click(object sender, EventArgs e)
  405. {
  406. try
  407. {
  408. ProcessStartInfo start = new ProcessStartInfo(
  409. Application.StartupPath + @"\CommonAIServer.Connector.exe",
  410. string.Format("FactoryClass={0} DataRoot=\"{1}\" ConnectString={2}",
  411. TemplateManager.Factory.GetType().FullName,
  412. dataDir.FullName,
  413. server.Server.ClientConnectString
  414. ));
  415. start.WorkingDirectory = Application.StartupPath;
  416. Process.Start(start);
  417. }
  418. catch (Exception err)
  419. {
  420. MessageBox.Show(err.Message);
  421. }
  422. }
  423. private void btn_ShowMsgBytes_Click(object sender, EventArgs e)
  424. {
  425. var msg_bytes_rec = new FormMsgBytes(server.Codec);
  426. msg_bytes_rec.Show();
  427. msg_bytes_rec.BringToFront();
  428. }
  429. private void btn_GC_Click(object sender, EventArgs e)
  430. {
  431. System.GC.Collect();
  432. }
  433. private void btn_disconnectAll_Click(object sender, EventArgs e)
  434. {
  435. foreach (var session in server.Server.GetSessions())
  436. {
  437. session.Disconnect(true);
  438. }
  439. }
  440. private void chk_EnableAOI_Click(object sender, EventArgs e)
  441. {
  442. server.Node.EnableAOI = chk_EnableAOI.Checked;
  443. }
  444. }
  445. }