From f72db8bf2eeac8a3b34521962e899437a60a5ecf Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 29 Jul 2024 15:41:05 +0900 Subject: [PATCH] Revert "Some works for #124" This reverts commit f73b18032938377969d9375166a60e9f6591c412. --- .../WelsonJS.Service/ServiceMain.cs | 60 ++++------ .../{ScreenTimer.cs => TemplateMatching.cs} | 110 ++++++------------ .../WelsonJS.Service/WelsonJS.Service.csproj | 4 +- app.js | 10 +- 4 files changed, 60 insertions(+), 124 deletions(-) rename WelsonJS.Toolkit/WelsonJS.Service/{ScreenTimer.cs => TemplateMatching.cs} (55%) diff --git a/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs b/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs index 02e6ea3..3841d3a 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs @@ -35,7 +35,6 @@ namespace WelsonJS.Service private static List timers; private string workingDirectory; private string scriptName; - private bool disabledScreenTimer = false; private string scriptFilePath; private string scriptText; private ScriptControl scriptControl; @@ -53,23 +52,12 @@ namespace WelsonJS.Service timers = new List(); // set default timer - Timer defaultTimer = new Timer + Timer timer = new Timer { Interval = 60000 // 1 minute }; - defaultTimer.Elapsed += OnElapsedTime; - timers.Add(defaultTimer); - - // set screen timer - if (!disabledScreenTimer) - { - timers.Add(new ScreenTimer - { - Parent = this, - WorkingDirectory = workingDirectory, - Interval = 1000 // 1 second - }); - } + timer.Elapsed += OnElapsedTime; + timers.Add(timer); } internal void TestStartupAndStop(string[] args) @@ -94,10 +82,6 @@ namespace WelsonJS.Service case "script-name": scriptName = entry.Value; break; - - case "--disable-screen-timer": - disabledScreenTimer = true; - break; } } @@ -143,7 +127,7 @@ namespace WelsonJS.Service scriptControl.AddCode(scriptText); // initialize - Log(DispatchServiceEvent("start")); + Log(DispatchServiceEvent(scriptName, "start")); } catch (Exception ex) { @@ -168,7 +152,7 @@ namespace WelsonJS.Service try { - Log(DispatchServiceEvent("stop")); + Log(DispatchServiceEvent(scriptName, "stop")); scriptControl?.Reset(); } catch (Exception ex) @@ -184,7 +168,7 @@ namespace WelsonJS.Service { try { - Log(DispatchServiceEvent("elapsedTime")); + Log(DispatchServiceEvent(scriptName, "elapsedTime")); } catch (Exception ex) { @@ -192,6 +176,11 @@ namespace WelsonJS.Service } } + private string DispatchServiceEvent(string name, string eventType) + { + return InvokeScriptMethod("dispatchServiceEvent", name, eventType); + } + private string InvokeScriptMethod(string methodName, params object[] parameters) { if (scriptControl != null) @@ -206,6 +195,15 @@ namespace WelsonJS.Service return "void"; } + + private void Log(string message) + { + using (StreamWriter writer = new StreamWriter(logFilePath, true)) + { + writer.WriteLine($"{DateTime.Now}: {message}"); + } + } + private Dictionary ParseArguments(string[] args) { var arguments = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -221,30 +219,12 @@ namespace WelsonJS.Service var value = arg.Substring(index + 1); arguments[key] = value; } - else - { - var key = arg.Substring(2); - arguments[key] = ""; - } } } return arguments; } - public void Log(string message) - { - using (StreamWriter writer = new StreamWriter(logFilePath, true)) - { - writer.WriteLine($"{DateTime.Now}: {message}"); - } - } - - public string DispatchServiceEvent(string eventType, object[] args = null) - { - return InvokeScriptMethod("dispatchServiceEvent", scriptName, eventType, args); - } - public string GetWorkingDirectory() { return workingDirectory; diff --git a/WelsonJS.Toolkit/WelsonJS.Service/ScreenTimer.cs b/WelsonJS.Toolkit/WelsonJS.Service/TemplateMatching.cs similarity index 55% rename from WelsonJS.Toolkit/WelsonJS.Service/ScreenTimer.cs rename to WelsonJS.Toolkit/WelsonJS.Service/TemplateMatching.cs index e37e77c..c7ff1f9 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/ScreenTimer.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/TemplateMatching.cs @@ -1,85 +1,51 @@ -/* - * WelsonJS.Service - * - * filename: - * ScreenTimer.cs - * - * description: - * WelsonJS - Build a Windows app on the Windows built-in JavaScript engine - * - * website: - * - https://github.com/gnh1201/welsonjs - * - https://catswords.social/@catswords_oss - * - https://teams.live.com/l/community/FEACHncAhq8ldnojAI - * - * author: - * Namhyeon Go - * - * license: - * GPLv3 or MS-RL(Microsoft Reciprocal License) - */ +// this is prototype code +// https://github.com/gnh1201/welsonjs using System; using System.Collections.Generic; using System.Drawing; using System.IO; -using System.ServiceProcess; -using System.Timers; using System.Windows.Forms; using WelsonJS.Service; -public class ScreenTimer : System.Timers.Timer +public class TemplateMatching { - private static List templateImages = new List(); - private string templateDirectory; - - public string WorkingDirectory { get; set; } - public ServiceBase Parent { get; set; } - - public class MatchedResult + public void Test(string workingDirectory) { - public string FileName; - public int ScreenNumber; - public Point Location; - public double MaxCorrelation; - } + string templateFolderPath = Path.Combine(workingDirectory, "assets/img/templates"); - public ScreenTimer() - { - Elapsed += OnTimeElapsed; + // Load all template images + List templateImages = LoadTemplateImages(templateFolderPath); - templateDirectory = Path.Combine(WorkingDirectory, "assets/img/templates"); - LoadTemplateImages(); - } + // try template matching + List<(string FileName, int ScreenNumber, Point Location, double MaxCorrelation)> results = + CaptureAndMatchAllScreens(templateImages); - private void OnTimeElapsed(object sender, ElapsedEventArgs e) - { - List matchedResults = CaptureAndMatchAllScreens(); - - ServiceMain svc = (ServiceMain)Parent; - matchedResults.ForEach(result => + // print results + foreach (var result in results) { - svc.Log(svc.DispatchServiceEvent("screenTime", new object[] { - result.FileName, - result.ScreenNumber, - result.Location.X, - result.Location.Y, - result.MaxCorrelation - })); - }); - } - - public void LoadTemplateImages() - { - string[] imageFiles = Directory.GetFiles(templateDirectory, "*.png"); - foreach (string file in imageFiles) - { - templateImages.Add(new Bitmap(file)); + Console.WriteLine($"Template: {result.FileName}, Screen: {result.ScreenNumber}, " + + $"Location: (x: {result.Location.X}, y: {result.Location.Y}), " + + $"Max Correlation: {result.MaxCorrelation}"); } } - public List CaptureAndMatchAllScreens() + public static List LoadTemplateImages(string folderPath) { - var results = new List(); + var templates = new List(); + var files = Directory.GetFiles(folderPath, "*.png"); + + foreach (var file in files) + { + templates.Add(new Bitmap(file)); + } + + return templates; + } + + public static List<(string FileName, int ScreenNumber, Point Location, double MaxCorrelation)> + CaptureAndMatchAllScreens(List templateImages) + { + var results = new List<(string FileName, int ScreenNumber, Point Location, double MaxCorrelation)>(); for (int i = 0; i < Screen.AllScreens.Length; i++) { @@ -91,20 +57,14 @@ public class ScreenTimer : System.Timers.Timer Point matchLocation = FindTemplate(mainImage, templateImage, out double maxCorrelation); string templateFileName = templateImage.Tag as string; - results.Add(new MatchedResult - { - FileName = templateFileName, - ScreenNumber = i, - Location = matchLocation, - MaxCorrelation = maxCorrelation - }); + results.Add((templateFileName, i, matchLocation, maxCorrelation)); } } return results; } - public Bitmap CaptureScreen(Screen screen) + public static Bitmap CaptureScreen(Screen screen) { Rectangle screenSize = screen.Bounds; Bitmap bitmap = new Bitmap(screenSize.Width, screenSize.Height); @@ -117,7 +77,7 @@ public class ScreenTimer : System.Timers.Timer return bitmap; } - public Point FindTemplate(Bitmap mainImage, Bitmap templateImage, out double maxCorrelation) + public static Point FindTemplate(Bitmap mainImage, Bitmap templateImage, out double maxCorrelation) { int mainWidth = mainImage.Width; int mainHeight = mainImage.Height; @@ -143,7 +103,7 @@ public class ScreenTimer : System.Timers.Timer return bestMatch; } - private double CalculateCorrelation(Bitmap mainImage, Bitmap templateImage, int offsetX, int offsetY) + private static double CalculateCorrelation(Bitmap mainImage, Bitmap templateImage, int offsetX, int offsetY) { int templateWidth = templateImage.Width; int templateHeight = templateImage.Height; diff --git a/WelsonJS.Toolkit/WelsonJS.Service/WelsonJS.Service.csproj b/WelsonJS.Toolkit/WelsonJS.Service/WelsonJS.Service.csproj index e1534fb..4965e9c 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/WelsonJS.Service.csproj +++ b/WelsonJS.Toolkit/WelsonJS.Service/WelsonJS.Service.csproj @@ -90,9 +90,6 @@ - - Component - Component @@ -107,6 +104,7 @@ ProjectInstaller.cs + diff --git a/app.js b/app.js index 1ea8cba..c943b2a 100644 --- a/app.js +++ b/app.js @@ -589,7 +589,7 @@ function initializeWindow(name, args, w, h) { } } -function dispatchServiceEvent(name, eventType, args) { +function dispatchServiceEvent(name, eventType) { var app = require(name); // load the service @@ -597,9 +597,8 @@ function dispatchServiceEvent(name, eventType, args) { return (function(action) { if (eventType in action) { try { - return (function(f) { - return (typeof f !== "function" ? null : f(args)); - })(action[eventType]); + var f = action[eventType]; + if (typeof f === "function") return f(); } catch (e) { console.error("Exception:", e.message); } @@ -607,8 +606,7 @@ function dispatchServiceEvent(name, eventType, args) { })({ start: app.onServiceStart, stop: app.onServiceStop, - elapsedTime: app.onServiceElapsedTime, - screenTime: app.onServiceScreenTime + elapsedTime: app.onServiceElapsedTime }); } else { console.error("Could not find", name + ".js");