From 7d1faf4b46af6045bc821d3046a32cc4a1b48d9a Mon Sep 17 00:00:00 2001 From: pidgy Date: Thu, 30 May 2024 12:24:15 -0400 Subject: [PATCH] v3.4.0 --- core/notify/notify.go | 2 +- global/global.go | 2 +- gui/ui/configure.go | 5 +- gui/ui/gui.go | 28 ++----- gui/ui/loading.go | 8 +- gui/ui/main.go | 5 +- system/process/process.go | 80 +++++++++++-------- system/tray/tray.go | 75 +++++++++++++----- system/wapi/helpers.go | 9 ++- system/wapi/wapi.go | 16 ++-- www/UniteHUD Client.html | 16 ++-- www/assets/css/style.css | 25 ++++-- www/assets/js/main.js | 162 +++++++++++++++++++++++++------------- 13 files changed, 279 insertions(+), 154 deletions(-) diff --git a/core/notify/notify.go b/core/notify/notify.go index d38047d..aa23378 100644 --- a/core/notify/notify.go +++ b/core/notify/notify.go @@ -124,7 +124,7 @@ func LastNStrings(n int) (s []string) { if feed.logs[i].count > 1 { continue } - s = append([]string{feed.logs[i].msg}, s...) + s = append(s, feed.logs[i].msg) } return } diff --git a/global/global.go b/global/global.go index c6cbd93..45efa51 100644 --- a/global/global.go +++ b/global/global.go @@ -10,7 +10,7 @@ import ( const ( Title = "UniteHUD" Version = "v" + VersionNoV - VersionNoV = "3.3.0" + VersionNoV = "3.4.0" TitleVersion = Title + " " + Version AssetDirectory = `assets` ) diff --git a/gui/ui/configure.go b/gui/ui/configure.go index c3d392e..28cfcae 100644 --- a/gui/ui/configure.go +++ b/gui/ui/configure.go @@ -34,6 +34,7 @@ import ( "github.com/pidgy/unitehud/gui/ux/button" "github.com/pidgy/unitehud/gui/ux/decorate" "github.com/pidgy/unitehud/gui/ux/title" + "github.com/pidgy/unitehud/system/process" ) type footer struct { @@ -153,8 +154,8 @@ func (g *GUI) configure() { decorate.Background(gtx) decorate.Label(&ui.footer.api, "API: %s", device.APIHumanName(device.API(config.Current.Video.Capture.Device.API))) - decorate.Label(&ui.footer.cpu, g.performance.cpu) - decorate.Label(&ui.footer.ram, g.performance.ram) + decorate.Label(&ui.footer.cpu, process.CPU.String()) + decorate.Label(&ui.footer.ram, process.RAM.String()) decorate.Label(&ui.footer.hz, "%s Hz", g.hz) decorate.Label(&ui.footer.fps, "%.0f FPS", fps) decorate.LabelColor(&ui.footer.fps, nrgba.Percent(p).Color()) diff --git a/gui/ui/gui.go b/gui/ui/gui.go index 2b74ae3..2aeee29 100644 --- a/gui/ui/gui.go +++ b/gui/ui/gui.go @@ -53,8 +53,8 @@ type GUI struct { } performance struct { - cpu, ram, uptime string - eco bool + uptime string + eco bool } previous struct { @@ -119,14 +119,10 @@ func New() *GUI { hz: fps.NewHz(), performance: struct { - cpu, - ram, uptime string eco bool }{ - cpu: "0%", - ram: "0MB", uptime: "00:00", eco: true, @@ -277,25 +273,17 @@ func (g *GUI) proc() { for ; is.Now != is.Closing; time.Sleep(time.Second) { g.performance.uptime = process.Uptime() - ram := process.RAM() - if ram > peak.ram+100 { - peak.ram = ram + if process.RAM.Float64() > peak.ram+100 { + peak.ram = process.RAM.Float64() notify.Replace("[UI] RAM", notify.Warn, "[UI] RAM Usage: %.0fMB", peak.ram) } - g.performance.ram = fmt.Sprintf("RAM %.0f%s", ram, "MB") - go stats.RAM(ram) + go stats.RAM(process.RAM.Float64()) - cpu, err := process.CPU() - if err != nil { - notify.Error("[Process] Failed to monitor CPU/RAM (%v)", err) - continue - } - if cpu > peak.cpu+10 { - peak.cpu = cpu + if process.CPU.Float64() > peak.cpu+10 { + peak.cpu = process.CPU.Float64() notify.Replace("[UI] CPU Usage", notify.Warn, "[UI] CPU Usage: %.1f%s", peak.cpu, "%") } - g.performance.cpu = fmt.Sprintf("CPU %.1f%s", cpu, "%") - go stats.CPU(cpu) + go stats.CPU(process.CPU.Float64()) } } diff --git a/gui/ui/loading.go b/gui/ui/loading.go index 8b44903..2bba9b8 100644 --- a/gui/ui/loading.go +++ b/gui/ui/loading.go @@ -22,7 +22,6 @@ import ( "github.com/pidgy/unitehud/gui/is" "github.com/pidgy/unitehud/gui/ux/decorate" "github.com/pidgy/unitehud/system/tray" - "github.com/pidgy/unitehud/system/wapi" ) type loading struct { @@ -67,9 +66,10 @@ func (g *GUI) loading() { for is.Now == is.Loading { switch event := g.window.NextEvent().(type) { case app.ViewEvent: - g.HWND = event.HWND - tray.SetHWND(g.HWND) - wapi.SetWindowDarkMode(g.HWND) + if event.HWND != 0 { + g.HWND = event.HWND + tray.SetHWND(g.HWND) + } case system.DestroyEvent: g.next(is.Closing) return diff --git a/gui/ui/main.go b/gui/ui/main.go index 6392ad3..925b782 100644 --- a/gui/ui/main.go +++ b/gui/ui/main.go @@ -46,6 +46,7 @@ import ( "github.com/pidgy/unitehud/system/desktop" "github.com/pidgy/unitehud/system/desktop/clicked" "github.com/pidgy/unitehud/system/discord" + "github.com/pidgy/unitehud/system/process" "github.com/pidgy/unitehud/system/save" "github.com/pidgy/unitehud/system/tray" ) @@ -213,9 +214,9 @@ func (g *GUI) main() { g.dimensions.size = event.Size decorate.Background(gtx) - decorate.Label(&ui.labels.cpu, g.performance.cpu) + decorate.Label(&ui.labels.cpu, process.CPU.String()) decorate.Label(&ui.labels.cpuGraph, stats.CPUGraph()) - decorate.Label(&ui.labels.ram, g.performance.ram) + decorate.Label(&ui.labels.ram, process.RAM.String()) decorate.Label(&ui.labels.ramGraph, stats.RAMGraph()) decorate.Label(&ui.labels.holding, ui.labels.holding.Text) decorate.ForegroundAlt(&ui.labels.cpuGraph.Color) diff --git a/system/process/process.go b/system/process/process.go index b95b6b4..de1548b 100644 --- a/system/process/process.go +++ b/system/process/process.go @@ -12,6 +12,7 @@ import ( "golang.org/x/sys/windows" + "github.com/pidgy/unitehud/core/notify" "github.com/pidgy/unitehud/global" ) @@ -23,42 +24,18 @@ type Process struct { Exe string } +type Stat struct { + value float64 + label string +} + var ( handle syscall.Handle - memory runtime.MemStats - ctime, etime, ktime, utime syscall.Filetime - prev, usage = ctime.Nanoseconds(), ktime.Nanoseconds() + utime.Nanoseconds() - cpus = float64(runtime.NumCPU()) - 2 + CPU, RAM = Stat{0, "CPU 0%"}, Stat{0, "RAM 0MB"} ) -func CPU() (float64, error) { - err := syscall.GetProcessTimes(handle, &ctime, &etime, &ktime, &utime) - if err != nil { - return 0.0, err - } - - now := time.Now().UnixNano() - diff := now - prev - - current := ktime.Nanoseconds() + utime.Nanoseconds() - diff2 := current - usage - - prev = now - usage = current - - return (100 * float64(diff2) / float64(diff)) / cpus, nil -} - -func Memory() runtime.MemStats { - return memory -} - -func RAM() float64 { - memory = runtime.MemStats{} - runtime.ReadMemStats(&memory) - return float64(memory.Sys) / 1024 / 1024 -} +func init() { go poll() } func Start() error { err := replace() @@ -74,6 +51,14 @@ func Start() error { return nil } +func (s *Stat) String() string { + return s.label +} + +func (s *Stat) Float64() float64 { + return s.value +} + func Uptime() string { u := time.Time{}.Add(time.Since(global.Uptime)) return fmt.Sprintf("%02d:%02d:%02d", u.Hour(), u.Minute(), u.Second()) @@ -148,6 +133,39 @@ func kill(exe string) error { return nil } +func poll() { + cpus := float64(runtime.NumCPU()) - 2 + prev, usage := int64(0), int64(0) + + t := time.NewTicker(time.Second * 5) + for range t.C { + var ctime, etime, ktime, utime syscall.Filetime + err := syscall.GetProcessTimes(handle, &ctime, &etime, &ktime, &utime) + if err != nil { + notify.Error("[Process] Failed to poll process statistics (%v)", err) + return + } + + now := time.Now().UnixNano() + diff := now - prev + + current := ktime.Nanoseconds() + utime.Nanoseconds() + diff2 := current - usage + + prev = now + usage = current + + CPU.value = ((100 * float64(diff2) / float64(diff)) / cpus) + CPU.label = fmt.Sprintf("CPU %.1f%s", CPU.value, "%") + + memory := runtime.MemStats{} + runtime.ReadMemStats(&memory) + + RAM.value = float64(memory.Sys) / 1024 / 1024 + RAM.label = fmt.Sprintf("RAM %.1fMB", RAM.value) + } +} + func replace() error { for _, exe := range []string{"UniteHUD.exe", "UniteHUD_Debug.exe"} { err := kill(path.Base(exe)) diff --git a/system/tray/tray.go b/system/tray/tray.go index 88d6ce2..d600c17 100644 --- a/system/tray/tray.go +++ b/system/tray/tray.go @@ -2,12 +2,14 @@ package tray import ( "os" + "time" "github.com/rupor-github/win-gpg-agent/systray" "github.com/skratchdot/open-golang/open" "github.com/pidgy/unitehud/avi/img" "github.com/pidgy/unitehud/core/notify" + "github.com/pidgy/unitehud/system/process" "github.com/pidgy/unitehud/system/wapi" ) @@ -23,10 +25,11 @@ var ( var menu = struct { visible bool - header, - website, - startstop, - quit toggle + header toggle + website toggle + startstop toggle + cpu, ram toggle + quit toggle startstopq chan bool errorq chan error @@ -51,10 +54,20 @@ func Open(title, version string, exit func()) error { go systray.Run(func() { menu.header = header(title, version) + menu.cpu = cpu() + menu.ram = ram() menu.website = website() menu.startstop = startstop() + menu.quit = quit() + go func() { + for ; ; time.Sleep(time.Second) { + menu.cpu.event() + menu.ram.event() + } + }() + menu.errorq <- nil menu.visible = true @@ -96,25 +109,16 @@ func StartStopEvent() bool { } } -func quit() toggle { - notify.Debug("[Tray] Adding Quit") +func cpu() toggle { + notify.Debug("[Tray] Adding CPU") systray.AddSeparator() return toggle{ - MenuItem: systray.AddMenuItem("Quit UniteHUD", "Close UniteHUD"), - event: func() { os.Exit(0) }, - } -} - -func startstop() toggle { - notify.Debug("[Tray] Adding Start/Stop") - - systray.AddSeparator() - - return toggle{ - MenuItem: systray.AddMenuItem("Start", "Start capturing events"), - event: func() { menu.startstopq <- true }, + MenuItem: systray.AddMenuItem("CPU 0%", "CPU"), + event: func() { + menu.cpu.SetTitle(process.CPU.String()) + }, } } @@ -137,6 +141,39 @@ func header(title, version string) toggle { } } +func quit() toggle { + notify.Debug("[Tray] Adding Quit") + + systray.AddSeparator() + + return toggle{ + MenuItem: systray.AddMenuItem("Quit UniteHUD", "Close UniteHUD"), + event: func() { os.Exit(0) }, + } +} + +func ram() toggle { + notify.Debug("[Tray] Adding RAM") + + return toggle{ + MenuItem: systray.AddMenuItem("RAM 0MB", "RAM"), + event: func() { + menu.ram.SetTitle(process.RAM.String()) + }, + } +} + +func startstop() toggle { + notify.Debug("[Tray] Adding Start/Stop") + + systray.AddSeparator() + + return toggle{ + MenuItem: systray.AddMenuItem("Start", "Start capturing events"), + event: func() { menu.startstopq <- true }, + } +} + func website() toggle { notify.Debug("[Tray] Adding Website") diff --git a/system/wapi/helpers.go b/system/wapi/helpers.go index 64ee7dd..5259582 100644 --- a/system/wapi/helpers.go +++ b/system/wapi/helpers.go @@ -136,7 +136,10 @@ func ObjectSelect(hwnd1, hwnd2 uintptr) { } func RaiseWindow(hwnd uintptr) { - go BringWindowToTop.Call(hwnd) + ShowWindow.Call(hwnd, ShowWindowFlags.ShowMinimized) + ShowWindow.Call(hwnd, ShowWindowFlags.Restore) + + // go BringWindowToTop.Call(hwnd) } func SetWindowDarkMode(hwnd uintptr) { @@ -153,6 +156,10 @@ func SetWindowPosNoSize(hwnd uintptr, pt image.Point) { helpSetWindowPos(hwnd, pt, image.Pt(0, 0), SetWindowPosFlags.NoSize) } +func SetWindowPosNoSizeNoMoveShowWindow(hwnd uintptr) { + helpSetWindowPos(hwnd, image.Pt(0, 0), image.Pt(0, 0), SetWindowPosFlags.NoSize|SetWindowPosFlags.NoMove|SetWindowPosFlags.ShowWindow) +} + func SetWindowPosHide(hwnd uintptr, pt image.Point, size image.Point) { helpSetWindowPos(hwnd, pt, size, SetWindowPosFlags.Hide) } diff --git a/system/wapi/wapi.go b/system/wapi/wapi.go index e89367b..d6e601e 100644 --- a/system/wapi/wapi.go +++ b/system/wapi/wapi.go @@ -174,12 +174,16 @@ var ( None, NoSize, Hide, - Show uintptr + Show, + NoMove, + ShowWindow uintptr }{ - None: 0x0000, - NoSize: 0x0001, - Hide: 0x0080, - Show: 0x0040, + None: 0x0000, + NoSize: 0x0001, + Hide: 0x0080, + Show: 0x0040, + NoMove: 0x0002, + ShowWindow: 0x0040, } ShowWindowFlags = struct { Hide, @@ -195,7 +199,7 @@ var ( ShowNA, Restore, ShowDefault, - ForceMinimize uint + ForceMinimize uintptr }{ Hide: 0, Normal: 1, diff --git a/www/UniteHUD Client.html b/www/UniteHUD Client.html index d3b0b05..480beaa 100644 --- a/www/UniteHUD Client.html +++ b/www/UniteHUD Client.html @@ -102,13 +102,15 @@ - -