Browse Source

RemoteConsole: add RemoteTools, improve status icon and reload

Yuxiao Mao 7 tháng trước cách đây
mục cha
commit
b13648ac52
4 tập tin đã thay đổi với 102 bổ sung17 xóa
  1. 1 0
      hide/Ide.hx
  2. 33 17
      hide/view/RemoteConsoleView.hx
  3. 4 0
      hrt/impl/RemoteConsole.hx
  4. 64 0
      hrt/impl/RemoteTools.hx

+ 1 - 0
hide/Ide.hx

@@ -789,6 +789,7 @@ class Ide extends hide.tools.IdeData {
 	public function reload() {
 		hasReloaded = true;
 		fileWatcher.dispose();
+		hide.view.RemoteConsoleView.onBeforeReload();
 		js.Browser.location.reload();
 	}
 

+ 33 - 17
hide/view/RemoteConsoleView.hx

@@ -15,15 +15,17 @@ package hide.view;
  */
 class RemoteConsoleView extends hide.ui.View<{}> {
 	static var rcmd : hrt.impl.RemoteConsole;
+	static var statusBarIcon : Element;
 	var panels : Array<RemoteConsolePanel>;
 	var panelsView : Element;
 	var logsView : Element;
 	var newPanelBtn : Element;
-	public var statusBarIcons : Element;
 
 	public function new( ?state ) {
 		super(state);
 		panels = [];
+		if( statusBarIcon == null )
+			statusBarIcon = new Element('<div class="ico ico-dot-circle-o" style="color: darkgray; cursor:default;" title="[Remote Console]"></div>');
 	}
 
 	override function onDisplay() {
@@ -41,15 +43,18 @@ class RemoteConsoleView extends hide.ui.View<{}> {
 				</div>
 			</div>
 		</div>').appendTo(element);
+		hide.Ide.inst.addStatusIcon(statusBarIcon);
 		element.find("#startServerBtn").on('click', function(e) {
 			if( rcmd != null )
 				rcmd.close();
 			rcmd = new hrt.impl.RemoteConsole(port, host);
+			rcmd.onClose = () -> refreshStatusIcon();
 			rcmd.log = (msg) -> log(msg);
 			rcmd.logError = (msg) -> log(msg, true);
 			rcmd.startServer(function(c) {
 				addPanel(c);
 			});
+			refreshStatusIcon();
 		});
 		element.find("#stopServerBtn").on('click', function(e) {
 			if( rcmd != null )
@@ -59,8 +64,6 @@ class RemoteConsoleView extends hide.ui.View<{}> {
 
 		panelsView = element.find(".remoteconsole");
 		logsView = element.find(".logs");
-		statusBarIcons = new Element('<div></div>');
-		hide.Ide.inst.addStatusIcon(statusBarIcons);
 		addPanel();
 	}
 
@@ -89,9 +92,6 @@ class RemoteConsoleView extends hide.ui.View<{}> {
 			panelsView.remove();
 		panelsView = null;
 		logsView = null;
-		if( statusBarIcons != null )
-			statusBarIcons.remove();
-		statusBarIcons = null;
 	}
 
 	public function log( msg : String, error : Bool = false ) {
@@ -101,6 +101,25 @@ class RemoteConsoleView extends hide.ui.View<{}> {
 		logsView.scrollTop(logsView.get(0).scrollHeight);
 	}
 
+	public function refreshStatusIcon() {
+		if( statusBarIcon == null || rcmd == null )
+			return;
+		if( rcmd.isConnected() ) {
+			statusBarIcon.css("color", "#009500");
+			statusBarIcon.prop("title", "[Remote Console] Server active");
+		} else {
+			statusBarIcon.css("color", "#c10000");
+			statusBarIcon.prop("title", "[Remote Console] Server stopped");
+		}
+		var active = 0;
+		for( p in panels ) {
+			if( p.isConnected() )
+				active++;
+		}
+		statusBarIcon.empty();
+		statusBarIcon.append(new Element('<span> $active</span>'));
+	}
+
 	function refreshNewPanelButton() {
 		if( newPanelBtn != null )
 			newPanelBtn.remove();
@@ -145,6 +164,11 @@ class RemoteConsoleView extends hide.ui.View<{}> {
 		p.close();
 	}
 
+	public static function onBeforeReload() {
+		if( rcmd != null )
+			rcmd.close();
+	}
+
 	// allow hide-plugin to add/modify game-specific hide command control
 	public static var commandViews = new Map<String, Class<RemoteConsoleCommand>>();
 	public static function registerCommandView( name : String, cl : Class<RemoteConsoleCommand> ) {
@@ -158,7 +182,6 @@ class RemoteConsoleView extends hide.ui.View<{}> {
 class RemoteConsolePanel extends hide.comp.Component {
 	var view : RemoteConsoleView;
 	public var connection(default, set) : hrt.impl.RemoteConsole.RemoteConsoleConnection;
-	var statusBarIcon : Element;
 	var statusIcon : Element;
 	public function new( view : RemoteConsoleView, connection : Null<hrt.impl.RemoteConsole.RemoteConsoleConnection>, commands : Null<Array<String>> ) {
 		super(null, null);
@@ -176,12 +199,8 @@ class RemoteConsolePanel extends hide.comp.Component {
 			</div>
 		</div>
 		');
-		this.statusBarIcon = new Element('
-			<div class="ico ico-dot-circle-o" style="color: darkgray; cursor:default;" title="[Remote Console] Not connected"></div>'
-		).appendTo(view.statusBarIcons);
 		this.statusIcon = element.find("#statusIcon");
 		element.find("#closeBtn").on('click', function(e) {
-			this.statusBarIcon.remove();
 			view.removePanel(this);
 		});
 		var commandsList = commands ?? ["dump", "prof", "custom"];
@@ -208,16 +227,13 @@ class RemoteConsolePanel extends hide.comp.Component {
 		comp.element.appendTo(element.find(".commands"));
 	}
 	function refreshStatusIcon() {
-		if( statusBarIcon == null || statusIcon == null )
+		view.refreshStatusIcon();
+		if( statusIcon == null )
 			return;
 		if( isConnected() ) {
-			statusBarIcon.css("color", "DarkGreen");
-			statusBarIcon.prop("title", "[Remote console] Connected");
-			statusIcon.css("color", "DarkGreen");
+			statusIcon.css("color", "#009500");
 			statusIcon.prop("title", "Connected");
 		} else {
-			statusBarIcon.css("color", "#c10000");
-			statusBarIcon.prop("title", "[Remote console] Disconnected");
 			statusIcon.css("color", "#c10000");
 			statusIcon.prop("title", "Disconnected");
 		}

+ 4 - 0
hrt/impl/RemoteConsole.hx

@@ -87,12 +87,16 @@ class RemoteConsole {
 				s.close();
 		}
 		cSocks = [];
+		onClose();
 	}
 
 	public function isConnected() {
 		return sock != null;
 	}
 
+	public dynamic function onClose() {
+	}
+
 	public dynamic function log( msg : String ) {
 		trace(msg);
 	}

+ 64 - 0
hrt/impl/RemoteTools.hx

@@ -0,0 +1,64 @@
+package hrt.impl;
+
+/**
+	A helper class to use a RemoteConsole in game.
+ */
+class RemoteTools {
+
+	public static var RETRY_DELAY : Float = 2;
+
+	static var rc : hrt.impl.RemoteConsole;
+	static var mainEvent : haxe.MainLoop.MainEvent;
+	static var lastUpdate : Float;
+
+	public static function autoConnect() {
+		if( rc != null )
+			return;
+		var configdyn : Dynamic = null;
+		if( hxd.res.Loader.currentInstance != null ) {
+			var config = hxd.res.Loader.currentInstance.fs.get("props.json");
+			configdyn = try haxe.Json.parse(config.getText()).remoteconsole catch( e : Dynamic ) null;
+		} else {
+			var config = try sys.io.File.getContent("res/props.json") catch( e : Dynamic ) null;
+			configdyn = try haxe.Json.parse(config).remoteconsole catch( e : Dynamic ) null;
+		}
+		rc = new hrt.impl.RemoteConsole(configdyn?.port, configdyn?.host);
+		mainEvent = haxe.MainLoop.add(update);
+	}
+
+	public static function stop() {
+		if( mainEvent != null ) {
+			mainEvent.stop();
+		}
+		mainEvent = null;
+		if( rc != null ) {
+			rc.close();
+		}
+		rc = null;
+	}
+
+	static function update() {
+		if( rc == null || rc.isConnected() )
+			return;
+		var current = haxe.Timer.stamp();
+		if( current - lastUpdate < RETRY_DELAY )
+			return;
+		lastUpdate = current;
+		rc.connect();
+	}
+
+	// ----- Commands -----
+
+	public static function log( msg : String ) {
+		rc?.sendCommand("log", msg);
+	}
+
+	public static function openCdb( sheet : String, ?line : Int, ?column : Int ) {
+		rc?.sendCommand("open", { cdbsheet : sheet, line : line, column : column });
+	}
+
+	public static function openRes( file : String ) {
+		rc?.sendCommand("open", { file : file });
+	}
+
+}