MainWindow.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. using System;
  2. using Gtk;
  3. using Gdk;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.Text;
  8. using Newtonsoft.Json.Linq;
  9. public partial class MainWindow: Gtk.Window
  10. {
  11. private System.Net.Sockets.Socket m_sock = null;
  12. private TextTag tagInfo;
  13. private TextTag tagWarning;
  14. private TextTag tagError;
  15. private TextTag tagDebug;
  16. // Console history
  17. private const uint MAX_HISTORY_ITEMS = 256;
  18. private uint history_size = 0;
  19. private uint history_current = 0;
  20. private string[] history = new string[MAX_HISTORY_ITEMS];
  21. // Socket recv buffer
  22. private byte [] m_byBuff = new byte[4096]; // Recieved data buffer
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="MainWindow"/> class.
  25. /// </summary>
  26. public MainWindow (): base (Gtk.WindowType.Toplevel)
  27. {
  28. Build ();
  29. Connect ();
  30. // Create tags for color-formatted text
  31. tagInfo = new Gtk.TextTag ("info");
  32. tagInfo.BackgroundGdk = new Gdk.Color (255, 255, 255);
  33. tagWarning = new Gtk.TextTag ("warning");
  34. tagWarning.BackgroundGdk = new Gdk.Color (255, 255, 153);
  35. tagError = new Gtk.TextTag ("error");
  36. tagError.BackgroundGdk = new Gdk.Color (255, 153, 153);
  37. tagDebug = new Gtk.TextTag ("debug");
  38. tagDebug.BackgroundGdk = new Gdk.Color (224, 224, 224);
  39. TextBuffer textbuffer1 = textview1.Buffer;
  40. textbuffer1.TagTable.Add (tagInfo);
  41. textbuffer1.TagTable.Add (tagWarning);
  42. textbuffer1.TagTable.Add (tagError);
  43. textbuffer1.TagTable.Add (tagDebug);
  44. // Create completion dictionary
  45. ListStore lua_api = new ListStore (typeof (string));
  46. lua_api.AppendValues ("Device.frame_count");
  47. lua_api.AppendValues ("Device.last_delta_time");
  48. lua_api.AppendValues ("Device.start");
  49. lua_api.AppendValues ("Device.stop");
  50. lua_api.AppendValues ("Device.create_resource_package");
  51. lua_api.AppendValues ("Device.destroy_resource_package");
  52. lua_api.AppendValues ("Window.show");
  53. lua_api.AppendValues ("Window.hide");
  54. lua_api.AppendValues ("Window.get_size");
  55. lua_api.AppendValues ("Window.get_position");
  56. lua_api.AppendValues ("Window.resize");
  57. lua_api.AppendValues ("Window.move");
  58. lua_api.AppendValues ("Window.minimize");
  59. lua_api.AppendValues ("Window.restore");
  60. lua_api.AppendValues ("Window.is_resizable");
  61. lua_api.AppendValues ("Window.set_resizable");
  62. lua_api.AppendValues ("Window.show_cursor");
  63. lua_api.AppendValues ("Window.get_cursor_xy");
  64. lua_api.AppendValues ("Window.set_cursor_xy");
  65. lua_api.AppendValues ("Window.title");
  66. lua_api.AppendValues ("Window.set_title");
  67. lua_api.AppendValues ("Math.deg_to_rad");
  68. lua_api.AppendValues ("Math.rad_to_deg");
  69. lua_api.AppendValues ("Math.next_pow_2");
  70. lua_api.AppendValues ("Math.is_pow_2");
  71. lua_api.AppendValues ("Math.ceil");
  72. lua_api.AppendValues ("Math.floor");
  73. lua_api.AppendValues ("Math.sqrt");
  74. lua_api.AppendValues ("Math.inv_sqrt");
  75. lua_api.AppendValues ("Math.sin");
  76. lua_api.AppendValues ("Math.cos");
  77. lua_api.AppendValues ("Math.asin");
  78. lua_api.AppendValues ("Math.acos");
  79. lua_api.AppendValues ("Math.tan");
  80. lua_api.AppendValues ("Math.atan2");
  81. lua_api.AppendValues ("Math.abs");
  82. lua_api.AppendValues ("Math.fmod");
  83. lua_api.AppendValues ("Vec2.new");
  84. lua_api.AppendValues ("Vec2.val");
  85. lua_api.AppendValues ("Vec2.add");
  86. lua_api.AppendValues ("Vec2.sub");
  87. lua_api.AppendValues ("Vec2.mul");
  88. lua_api.AppendValues ("Vec2.div");
  89. lua_api.AppendValues ("Vec2.dot");
  90. lua_api.AppendValues ("Vec2.equals");
  91. lua_api.AppendValues ("Vec2.lower");
  92. lua_api.AppendValues ("Vec2.greater");
  93. lua_api.AppendValues ("Vec2.length");
  94. lua_api.AppendValues ("Vec2.squared_length");
  95. lua_api.AppendValues ("Vec2.set_length");
  96. lua_api.AppendValues ("Vec2.normalize");
  97. lua_api.AppendValues ("Vec2.negate");
  98. lua_api.AppendValues ("Vec2.get_distance_to");
  99. lua_api.AppendValues ("Vec2.get_angle_between");
  100. lua_api.AppendValues ("Vec2.zero");
  101. lua_api.AppendValues ("Vec3.new");
  102. lua_api.AppendValues ("Vec3.val");
  103. lua_api.AppendValues ("Vec3.add");
  104. lua_api.AppendValues ("Vec3.sub");
  105. lua_api.AppendValues ("Vec3.mul");
  106. lua_api.AppendValues ("Vec3.div");
  107. lua_api.AppendValues ("Vec3.dot");
  108. lua_api.AppendValues ("Vec3.cross");
  109. lua_api.AppendValues ("Vec3.equals");
  110. lua_api.AppendValues ("Vec3.lower");
  111. lua_api.AppendValues ("Vec3.greater");
  112. lua_api.AppendValues ("Vec3.length");
  113. lua_api.AppendValues ("Vec3.squared_length");
  114. lua_api.AppendValues ("Vec3.set_length");
  115. lua_api.AppendValues ("Vec3.normalize");
  116. lua_api.AppendValues ("Vec3.negate");
  117. lua_api.AppendValues ("Vec3.get_distance_to");
  118. lua_api.AppendValues ("Vec3.get_angle_between");
  119. lua_api.AppendValues ("Vec3.zero");
  120. lua_api.AppendValues ("Quat.new");
  121. lua_api.AppendValues ("Quat.negate");
  122. lua_api.AppendValues ("Quat.load_identity");
  123. lua_api.AppendValues ("Quat.length");
  124. lua_api.AppendValues ("Quat.conjugate");
  125. lua_api.AppendValues ("Quat.inverse");
  126. lua_api.AppendValues ("Quat.cross");
  127. lua_api.AppendValues ("Quat.mul");
  128. lua_api.AppendValues ("Quat.pow");
  129. lua_api.AppendValues ("StringSetting.value");
  130. lua_api.AppendValues ("StringSetting.synopsis");
  131. lua_api.AppendValues ("StringSetting.update");
  132. lua_api.AppendValues ("IntSetting.value");
  133. lua_api.AppendValues ("IntSetting.synopsis");
  134. lua_api.AppendValues ("IntSetting.min");
  135. lua_api.AppendValues ("IntSetting.max");
  136. lua_api.AppendValues ("IntSetting.update");
  137. lua_api.AppendValues ("FloatSetting.value");
  138. lua_api.AppendValues ("FloatSetting.synopsis");
  139. lua_api.AppendValues ("FloatSetting.min");
  140. lua_api.AppendValues ("FloatSetting.max");
  141. lua_api.AppendValues ("FloatSetting.update");
  142. lua_api.AppendValues ("Mouse.button_pressed");
  143. lua_api.AppendValues ("Mouse.button_released");
  144. lua_api.AppendValues ("Mouse.any_pressed");
  145. lua_api.AppendValues ("Mouse.any_released");
  146. lua_api.AppendValues ("Mouse.cursor_xy");
  147. lua_api.AppendValues ("Mouse.set_cursor_xy");
  148. lua_api.AppendValues ("Mouse.cursor_relative_xy");
  149. lua_api.AppendValues ("Mouse.set_cursor_relative_xy");
  150. lua_api.AppendValues ("Mouse.MB_LEFT");
  151. lua_api.AppendValues ("Mouse.KB_MIDDLE");
  152. lua_api.AppendValues ("Mouse.MB_RIGHT");
  153. lua_api.AppendValues ("Keyboard.modifier_pressed");
  154. lua_api.AppendValues ("Keyboard.button_pressed");
  155. lua_api.AppendValues ("Keyboard.button_released");
  156. lua_api.AppendValues ("Keyboard.any_pressed");
  157. lua_api.AppendValues ("Keyboard.any_released");
  158. lua_api.AppendValues ("Keyboard.TAB");
  159. lua_api.AppendValues ("Keyboard.ENTER");
  160. lua_api.AppendValues ("Keyboard.ESCAPE");
  161. lua_api.AppendValues ("Keyboard.SPACE");
  162. lua_api.AppendValues ("Keyboard.BACKSPACE");
  163. lua_api.AppendValues ("Keyboard.KP_0");
  164. lua_api.AppendValues ("Keyboard.KP_1");
  165. lua_api.AppendValues ("Keyboard.KP_2");
  166. lua_api.AppendValues ("Keyboard.KP_3");
  167. lua_api.AppendValues ("Keyboard.KP_4");
  168. lua_api.AppendValues ("Keyboard.KP_5");
  169. lua_api.AppendValues ("Keyboard.KP_6");
  170. lua_api.AppendValues ("Keyboard.KP_7");
  171. lua_api.AppendValues ("Keyboard.KP_8");
  172. lua_api.AppendValues ("Keyboard.KP_9");
  173. lua_api.AppendValues ("Keyboard.F1");
  174. lua_api.AppendValues ("Keyboard.F2");
  175. lua_api.AppendValues ("Keyboard.F3");
  176. lua_api.AppendValues ("Keyboard.F4");
  177. lua_api.AppendValues ("Keyboard.F5");
  178. lua_api.AppendValues ("Keyboard.F6");
  179. lua_api.AppendValues ("Keyboard.F7");
  180. lua_api.AppendValues ("Keyboard.F8");
  181. lua_api.AppendValues ("Keyboard.F9");
  182. lua_api.AppendValues ("Keyboard.F10");
  183. lua_api.AppendValues ("Keyboard.F11");
  184. lua_api.AppendValues ("Keyboard.F12");
  185. lua_api.AppendValues ("Keyboard.HOME");
  186. lua_api.AppendValues ("Keyboard.LEFT");
  187. lua_api.AppendValues ("Keyboard.UP");
  188. lua_api.AppendValues ("Keyboard.RIGHT");
  189. lua_api.AppendValues ("Keyboard.DOWN");
  190. lua_api.AppendValues ("Keyboard.PAGE_UP");
  191. lua_api.AppendValues ("Keyboard.PAGE_DOWN");
  192. lua_api.AppendValues ("Keyboard.LCONTROL");
  193. lua_api.AppendValues ("Keyboard.RCONTROL");
  194. lua_api.AppendValues ("Keyboard.LSHIFT");
  195. lua_api.AppendValues ("Keyboard.RSHIFT");
  196. lua_api.AppendValues ("Keyboard.CAPS_LOCK");
  197. lua_api.AppendValues ("Keyboard.LALT");
  198. lua_api.AppendValues ("Keyboard.RALT");
  199. lua_api.AppendValues ("Keyboard.LSUPER");
  200. lua_api.AppendValues ("Keyboard.RSUPER");
  201. lua_api.AppendValues ("Keyboard.NUM_0");
  202. lua_api.AppendValues ("Keyboard.NUM_1");
  203. lua_api.AppendValues ("Keyboard.NUM_2");
  204. lua_api.AppendValues ("Keyboard.NUM_3");
  205. lua_api.AppendValues ("Keyboard.NUM_4");
  206. lua_api.AppendValues ("Keyboard.NUM_5");
  207. lua_api.AppendValues ("Keyboard.NUM_6");
  208. lua_api.AppendValues ("Keyboard.NUM_7");
  209. lua_api.AppendValues ("Keyboard.NUM_8");
  210. lua_api.AppendValues ("Keyboard.NUM_9");
  211. lua_api.AppendValues ("Keyboard.A");
  212. lua_api.AppendValues ("Keyboard.B");
  213. lua_api.AppendValues ("Keyboard.C");
  214. lua_api.AppendValues ("Keyboard.D");
  215. lua_api.AppendValues ("Keyboard.E");
  216. lua_api.AppendValues ("Keyboard.F");
  217. lua_api.AppendValues ("Keyboard.G");
  218. lua_api.AppendValues ("Keyboard.H");
  219. lua_api.AppendValues ("Keyboard.I");
  220. lua_api.AppendValues ("Keyboard.J");
  221. lua_api.AppendValues ("Keyboard.K");
  222. lua_api.AppendValues ("Keyboard.L");
  223. lua_api.AppendValues ("Keyboard.M");
  224. lua_api.AppendValues ("Keyboard.N");
  225. lua_api.AppendValues ("Keyboard.O");
  226. lua_api.AppendValues ("Keyboard.P");
  227. lua_api.AppendValues ("Keyboard.Q");
  228. lua_api.AppendValues ("Keyboard.R");
  229. lua_api.AppendValues ("Keyboard.S");
  230. lua_api.AppendValues ("Keyboard.T");
  231. lua_api.AppendValues ("Keyboard.U");
  232. lua_api.AppendValues ("Keyboard.V");
  233. lua_api.AppendValues ("Keyboard.W");
  234. lua_api.AppendValues ("Keyboard.X");
  235. lua_api.AppendValues ("Keyboard.Y");
  236. lua_api.AppendValues ("Keyboard.Z");
  237. lua_api.AppendValues ("Keyboard.a");
  238. lua_api.AppendValues ("Keyboard.b");
  239. lua_api.AppendValues ("Keyboard.c");
  240. lua_api.AppendValues ("Keyboard.d");
  241. lua_api.AppendValues ("Keyboard.e");
  242. lua_api.AppendValues ("Keyboard.f");
  243. lua_api.AppendValues ("Keyboard.g");
  244. lua_api.AppendValues ("Keyboard.h");
  245. lua_api.AppendValues ("Keyboard.i");
  246. lua_api.AppendValues ("Keyboard.j");
  247. lua_api.AppendValues ("Keyboard.k");
  248. lua_api.AppendValues ("Keyboard.l");
  249. lua_api.AppendValues ("Keyboard.m");
  250. lua_api.AppendValues ("Keyboard.n");
  251. lua_api.AppendValues ("Keyboard.o");
  252. lua_api.AppendValues ("Keyboard.p");
  253. lua_api.AppendValues ("Keyboard.q");
  254. lua_api.AppendValues ("Keyboard.r");
  255. lua_api.AppendValues ("Keyboard.s");
  256. lua_api.AppendValues ("Keyboard.t");
  257. lua_api.AppendValues ("Keyboard.u");
  258. lua_api.AppendValues ("Keyboard.v");
  259. lua_api.AppendValues ("Keyboard.w");
  260. lua_api.AppendValues ("Keyboard.x");
  261. lua_api.AppendValues ("Keyboard.y");
  262. lua_api.AppendValues ("Keyboard.z");
  263. lua_api.AppendValues ("ResourcePackage.load");
  264. lua_api.AppendValues ("ResourcePackage.unload");
  265. lua_api.AppendValues ("ResourcePackage.flush");
  266. lua_api.AppendValues ("ResourcePackage.has_loaded");
  267. entry1.Completion = new EntryCompletion ();
  268. entry1.Completion.Model = lua_api;
  269. entry1.Completion.TextColumn = 0;
  270. }
  271. /// <summary>
  272. /// Connect this instance.
  273. /// </summary>
  274. public void Connect()
  275. {
  276. // Close the socket if it is still open
  277. if( m_sock != null && m_sock.Connected )
  278. {
  279. m_sock.Shutdown( SocketShutdown.Both );
  280. System.Threading.Thread.Sleep( 10 );
  281. m_sock.Close();
  282. }
  283. // Create the socket object
  284. m_sock = new System.Net.Sockets.Socket( AddressFamily.InterNetwork,
  285. SocketType.Stream, ProtocolType.Tcp );
  286. // Define the Server address and port
  287. IPEndPoint epServer = new IPEndPoint( IPAddress.Parse("127.0.0.1"), 10001 );
  288. // Connect to the server blocking method
  289. // and setup callback for recieved data
  290. // m_sock.Connect( epServer );
  291. // SetupRecieveCallback( m_sock );
  292. // Connect to server non-Blocking method
  293. m_sock.Blocking = false;
  294. AsyncCallback onconnect = new AsyncCallback( OnConnect );
  295. m_sock.BeginConnect( epServer, onconnect, m_sock );
  296. }
  297. /// <summary>
  298. /// Raises the connect event.
  299. /// </summary>
  300. /// <param name="ar">Ar.</param>
  301. public void OnConnect( IAsyncResult ar )
  302. {
  303. // Socket was the passed in object
  304. System.Net.Sockets.Socket sock = (System.Net.Sockets.Socket)ar.AsyncState;
  305. // Check if we were sucessfull
  306. try
  307. {
  308. // sock.EndConnect( ar );
  309. if( sock.Connected )
  310. SetupRecieveCallback( sock );
  311. else
  312. Console.Write("Unable to connect to remote machine");
  313. }
  314. catch( Exception ex )
  315. {
  316. Console.Write("Unable to connect to remote machine");
  317. }
  318. }
  319. /// <summary>
  320. /// Setups the recieve callback.
  321. /// </summary>
  322. /// <param name="sock">Sock.</param>
  323. public void SetupRecieveCallback(System.Net.Sockets.Socket sock)
  324. {
  325. try
  326. {
  327. AsyncCallback recieveData = new AsyncCallback( OnRecievedData );
  328. sock.BeginReceive( m_byBuff, 0, m_byBuff.Length,
  329. SocketFlags.None, recieveData, sock );
  330. }
  331. catch( Exception ex )
  332. {
  333. Console.Write("Setup Recieve Callback failed!");
  334. }
  335. }
  336. /// <summary>
  337. /// Raises the recieved data event.
  338. /// </summary>
  339. /// <param name="ar">Ar.</param>
  340. public void OnRecievedData( IAsyncResult ar )
  341. {
  342. // Socket was the passed in object
  343. System.Net.Sockets.Socket sock = (System.Net.Sockets.Socket)ar.AsyncState;
  344. // Check if we got any data
  345. try
  346. {
  347. int nBytesRec = sock.EndReceive( ar );
  348. if( nBytesRec > 0 )
  349. {
  350. // Wrote the data to the List
  351. string sRecieved = Encoding.ASCII.GetString( m_byBuff, 0, nBytesRec );
  352. Console.Write(sRecieved);
  353. JObject obj = JObject.Parse(sRecieved);
  354. if (obj["type"].ToString() == "message")
  355. {
  356. string severity = obj["severity"].ToString();
  357. string message = obj["message"].ToString();
  358. if (severity == "info")
  359. {
  360. WriteLog(message, tagInfo);
  361. }
  362. else if (severity == "warning")
  363. {
  364. WriteLog(message, tagWarning);
  365. }
  366. else if (severity == "error")
  367. {
  368. WriteLog(message, tagError);
  369. }
  370. else if (severity == "debug")
  371. {
  372. WriteLog(message, tagDebug);
  373. }
  374. }
  375. else
  376. {
  377. WriteLog("Unknown response from server", tagInfo);
  378. }
  379. // If the connection is still usable restablish the callback
  380. SetupRecieveCallback( sock );
  381. }
  382. else
  383. {
  384. // If no data was recieved then the connection is probably dead
  385. Console.WriteLine( "Client {0}, disconnected",
  386. sock.RemoteEndPoint );
  387. sock.Shutdown( SocketShutdown.Both );
  388. sock.Close();
  389. }
  390. }
  391. catch( Exception ex )
  392. {
  393. Console.Write("Unusual error druing Recieve!");
  394. }
  395. }
  396. protected void WriteLog(string text, TextTag tag)
  397. {
  398. Gtk.Application.Invoke (delegate
  399. {
  400. TextIter endIter = textview1.Buffer.EndIter;
  401. textview1.Buffer.Insert(ref endIter, text);
  402. endIter.BackwardChars(text.Length);
  403. textview1.Buffer.ApplyTag(tag, endIter, textview1.Buffer.EndIter);
  404. textview1.ScrollToMark(textview1.Buffer.CreateMark("bottom", textview1.Buffer.EndIter, false), 0, true, 0.0, 1.0);
  405. });
  406. }
  407. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  408. {
  409. Application.Quit ();
  410. a.RetVal = true;
  411. }
  412. protected void SendScript(String script)
  413. {
  414. String json = "{\"type\":\"script\",\"script\":\"";
  415. json += script;
  416. json += "\"}";
  417. Console.Write (json);
  418. m_sock.Send (Encoding.ASCII.GetBytes (json));
  419. }
  420. protected void SendCommand(String command)
  421. {
  422. char[] delimiterChars = { ' ', '\t' };
  423. string[] words = command.Split(delimiterChars);
  424. string cmd = words[0];
  425. string resource_type = words[1];
  426. string resource_name = words[2];
  427. string json = "{\"type\":\"command\",\"command\":\"";
  428. json += cmd;
  429. json += "\",";
  430. json += "\"resource_type\":";
  431. json += "\"";
  432. json += resource_type;
  433. json += "\",";
  434. json += "\"resource_name\":";
  435. json +="\"";
  436. json += resource_name;
  437. json += "\"}";
  438. Console.Write (json);
  439. m_sock.Send (Encoding.ASCII.GetBytes (json));
  440. }
  441. protected void OnConnectActivated (object sender, EventArgs e)
  442. {
  443. Connect ();
  444. }
  445. protected void OnEntryActivated (object sender, EventArgs e)
  446. {
  447. string text = entry1.Text;
  448. text = text.Trim ();
  449. // Do processing only if we have text
  450. if (text.Length > 0)
  451. {
  452. history [history_size % MAX_HISTORY_ITEMS] = text;
  453. history_size++;
  454. history_current = history_size;
  455. // Log entered text
  456. WriteLog ("> " + text + "\n", tagInfo);
  457. // Sanitize text entry
  458. text = text.Replace("\"", "\\\"");
  459. if (combobox1.Active == 0) {
  460. SendScript (text);
  461. } else {
  462. SendCommand (text);
  463. }
  464. }
  465. entry1.Text = "";
  466. }
  467. protected void OnEntryKeyPressed (object o, KeyPressEventArgs args)
  468. {
  469. switch (args.Event.Key)
  470. {
  471. case Gdk.Key.Down:
  472. {
  473. if (history_current < history_size)
  474. {
  475. history_current++;
  476. }
  477. break;
  478. }
  479. case Gdk.Key.Up:
  480. {
  481. if (history_current > 0)
  482. {
  483. history_current--;
  484. }
  485. break;
  486. }
  487. default:
  488. {
  489. return;
  490. }
  491. }
  492. if (history_size == history_current)
  493. {
  494. entry1.Text = "";
  495. }
  496. else
  497. {
  498. entry1.Text = history [history_current % MAX_HISTORY_ITEMS];
  499. entry1.Position = entry1.Text.Length;
  500. }
  501. args.RetVal = true;
  502. }
  503. }