Fix parameter types to pass arguments to scripting engine

Since `ArrayList` is highly compatible with COM, you can use it to pass an array to `ScriptControl`.
This commit is contained in:
Namhyeon Go 2024-08-25 12:56:01 +09:00
parent 3a890168df
commit e4a8357715
5 changed files with 34 additions and 21 deletions

View File

@ -71,28 +71,28 @@ public class ScreenMatch
templateImages = new List<Bitmap>(); templateImages = new List<Bitmap>();
// Read values from configration file // Read values from configration file
string _mode; string screen_time_mode;
string _raw_params; string screen_time_params;
try try
{ {
_mode = this.parent.GetSettingsFileHandler().Read("SCREEN_TIME_MODE", "Service"); screen_time_mode = this.parent.GetSettingsFileHandler().Read("SCREEN_TIME_MODE", "Service");
_raw_params = this.parent.GetSettingsFileHandler().Read("SCREEN_TIME_PARAMS", "Service"); screen_time_params = this.parent.GetSettingsFileHandler().Read("SCREEN_TIME_PARAMS", "Service");
} }
catch (Exception ex) catch (Exception ex)
{ {
_mode = null; screen_time_mode = null;
_raw_params = null; screen_time_params = null;
this.parent.Log($"Failed to read from configration file: {ex.Message}"); this.parent.Log($"Failed to read from configration file: {ex.Message}");
} }
if (! String.IsNullOrEmpty(_raw_params)) if (!String.IsNullOrEmpty(screen_time_params))
{ {
string[] ss = _raw_params.Split(','); string[] ss = screen_time_params.Split(',');
foreach (string s in ss) { foreach (string s in ss) {
AddParam(s); AddParam(s);
} }
} }
SetMode(_mode); SetMode(screen_time_mode);
LoadTemplateImages(); LoadTemplateImages();
} }
@ -154,6 +154,7 @@ public class ScreenMatch
return CaptureAndMatchAllWindows(); return CaptureAndMatchAllWindows();
default: default:
parent.Log($"Unknown capture mode: {mode}");
break; break;
} }

View File

@ -32,6 +32,7 @@ using MSScriptControl;
using System.IO; using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using WelsonJS.TinyINIController; using WelsonJS.TinyINIController;
using System.Collections;
namespace WelsonJS.Service namespace WelsonJS.Service
{ {
@ -326,10 +327,17 @@ namespace WelsonJS.Service
} }
} }
private string InvokeScriptMethod(string methodName, params object[] parameters) private string InvokeScriptMethod(string methodName, string scriptName, string eventType, string[] args)
{ {
if (scriptControl != null) if (scriptControl != null)
{ {
object[] parameters = new object[] {
scriptName,
eventType,
new ArrayList(args)
};
//scriptControl.AddObject("extern_arguments", new ArrayList(args), true);
return scriptControl.Run(methodName, parameters)?.ToString() ?? "void"; return scriptControl.Run(methodName, parameters)?.ToString() ?? "void";
} }
else else
@ -370,11 +378,11 @@ namespace WelsonJS.Service
{ {
if (args == null) if (args == null)
{ {
return InvokeScriptMethod("dispatchServiceEvent", scriptName, eventType, ""); return InvokeScriptMethod("dispatchServiceEvent", scriptName, eventType, new string[] { });
} }
else else
{ {
return InvokeScriptMethod("dispatchServiceEvent", scriptName, eventType, String.Join("; ", args)); return InvokeScriptMethod("dispatchServiceEvent", scriptName, eventType, args);
} }
} }

5
app.js
View File

@ -592,9 +592,8 @@ function initializeWindow(name, args, w, h) {
} }
} }
function dispatchServiceEvent(name, eventType, _args) { function dispatchServiceEvent(name, eventType, w_args) {
var app = require(name); var app = require(name);
var args = _args.split('; ');
// load the service // load the service
if (app) { if (app) {
@ -611,7 +610,7 @@ function dispatchServiceEvent(name, eventType, _args) {
if (eventType in action) { if (eventType in action) {
try { try {
return (function(f) { return (function(f) {
return (typeof f !== "function" ? null : f(args)); return (typeof f !== "function" ? null : f(w_args));
})(action[eventType]); })(action[eventType]);
} catch (e) { } catch (e) {
console.error("Exception:", e.message); console.error("Exception:", e.message);

View File

@ -25,7 +25,7 @@ function onMessageReceived() {
} }
function onServiceScreenTime(args) { function onServiceScreenTime(args) {
return "onServiceScreenTime recevied. " + args.join(', '); return "onServiceScreenTime recevied. " + args(0);
} }
function onFileCreated(args) { function onFileCreated(args) {

View File

@ -1,14 +1,19 @@
[Default] [Default]
JSCRIPT_ENV=development ;development or production ; development or production
JSCRIPT_ENV=development
CHATGPT_APIKEY=file:data/chatgpt-apikey.txt CHATGPT_APIKEY=file:data/chatgpt-apikey.txt
WINDIVERT_CONFIG_FILE=data/windivert.config.json ;for network debugging ; for network debugging
NONFREE_STRICT=false ;User consent may be required when using third-party software with an EULA license. WINDIVERT_CONFIG_FILE=data/windivert.config.json
; User consent may be required when using third-party software with an EULA license.
NONFREE_STRICT=false
[Service] [Service]
DISABLE_SCREEN_TIME=false DISABLE_SCREEN_TIME=false
DISABLE_FILE_MONITOR=false DISABLE_FILE_MONITOR=false
SCREEN_TIME_MODE=screen ;screen or window ; screen or window
SCREEN_TIME_PARAMS= ;filename.exe SCREEN_TIME_MODE=screen
;filename.exe
SCREEN_TIME_PARAMS=
GRPC_HOST=http://localhost:50051 GRPC_HOST=http://localhost:50051
ES_HOST=http://localhost:9200 ES_HOST=http://localhost:9200
ES_USER=elastic ES_USER=elastic