RemoteTools.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package hrt.impl;
  2. /**
  3. A helper class to use a RemoteConsole in game.
  4. */
  5. class RemoteTools {
  6. public static var RETRY_DELAY : Float = 2;
  7. static var rc : hrt.impl.RemoteConsole;
  8. static var mainEvent : haxe.MainLoop.MainEvent;
  9. static var lastUpdate : Float;
  10. static var onConnected : Bool -> Void;
  11. static var menuActions : Array<{ action : RemoteConsole.RemoteMenuAction, f : (id:String) -> Int}> = [];
  12. public static function autoConnect( ?onConnected : Bool -> Void ) {
  13. RemoteTools.onConnected = onConnected;
  14. if( rc != null )
  15. return;
  16. var configdyn : Dynamic = null;
  17. if( hxd.res.Loader.currentInstance != null ) {
  18. var config = hxd.res.Loader.currentInstance.fs.get("props.json");
  19. configdyn = try haxe.Json.parse(config.getText()).remoteconsole catch( e : Dynamic ) null;
  20. } else {
  21. var config = try sys.io.File.getContent("res/props.json") catch( e : Dynamic ) null;
  22. configdyn = try haxe.Json.parse(config).remoteconsole catch( e : Dynamic ) null;
  23. }
  24. rc = new hrt.impl.RemoteConsole(configdyn?.port, configdyn?.host);
  25. mainEvent = haxe.MainLoop.add(update);
  26. }
  27. public static function stop() {
  28. if( mainEvent != null ) {
  29. mainEvent.stop();
  30. }
  31. mainEvent = null;
  32. if( rc != null ) {
  33. rc.close();
  34. }
  35. rc = null;
  36. }
  37. public static function isConnected() {
  38. return rc != null && rc.isConnected();
  39. }
  40. public static dynamic function onConsoleCommand( cmd : String ) : Int {
  41. logError('onConsoleCommand not implemented, received $cmd');
  42. return -1;
  43. }
  44. public static function registerCdbMenu( sheet : String, name : String, f : (id:String) -> Int ) {
  45. menuActions.push({ action : { name : name, cdbSheet: sheet }, f : f});
  46. // Send menuActions if already connected
  47. if( rc != null && rc.isConnected() ) {
  48. rc.sendCommand("registerMenuActions", { actions : [for( ma in menuActions ) ma.action] });
  49. }
  50. }
  51. static function onMenuAction( action : RemoteConsole.RemoteMenuAction, id : String ) : Int {
  52. if( action == null || id == null )
  53. return -1;
  54. for( ma in menuActions ) {
  55. if( ma.action.name == action.name && ma.action.cdbSheet == action.cdbSheet ) {
  56. return ma.f(id);
  57. }
  58. }
  59. return -1;
  60. }
  61. static function update() {
  62. if( rc == null || rc.isConnected() )
  63. return;
  64. var current = haxe.Timer.stamp();
  65. if( current - lastUpdate < RETRY_DELAY )
  66. return;
  67. lastUpdate = current;
  68. rc.connect(function(b) {
  69. if( onConnected != null )
  70. onConnected(b);
  71. if( b ) {
  72. var c = rc.connections[0];
  73. if( c != null ) {
  74. c.onConsoleCommand = (cmd) -> onConsoleCommand(cmd);
  75. c.onMenuAction = (action,id) -> onMenuAction(action,id);
  76. }
  77. // Send menuActions
  78. rc.sendCommand("registerMenuActions", { actions : [for( ma in menuActions ) ma.action] });
  79. }
  80. });
  81. }
  82. // ----- Commands -----
  83. public static function log( msg : String ) {
  84. rc?.sendCommand("log", msg);
  85. }
  86. public static function logError( msg : String ) {
  87. rc?.sendCommand("logError", msg);
  88. }
  89. /**
  90. @param selectExpr hscript expression that are used for select a line in the given sheet.
  91. Example: `$.id == Some_Unique_Id`, `$.name == SomeName`
  92. (`"` can be omitted in String literal when no ambiguity).
  93. */
  94. public static function openCdb( sheet : String, ?line : Int, ?column : Int, ?selectExpr : String ) {
  95. rc?.sendCommand("open", { cdbsheet : sheet, line : line, column : column, selectExpr : selectExpr });
  96. }
  97. public static function openRes( file : String ) {
  98. rc?.sendCommand("open", { file : file });
  99. }
  100. public static function openDomkit( file : String, ?line : Int, ?column : Int ) {
  101. rc?.sendCommand("open", { file : file, line : line, column : column });
  102. }
  103. /**
  104. @param selectExpr hscript expression that are used for select some elements in the view.
  105. Example: `$.name == SomeName`, `$.props.id == Some_Unique_Id`
  106. (`"` can be omitted in String literal when no ambiguity).
  107. */
  108. public static function openPrefab( file : String, ?selectExpr : String ) {
  109. rc?.sendCommand("open", { file : file, selectExpr : selectExpr });
  110. }
  111. }