mirror of
https://github.com/AynaLivePlayer/AynaLivePlayer.git
synced 2025-12-08 19:28:13 +08:00
fix gui freeze bug (work round)
This commit is contained in:
101
common/config/config.go
Normal file
101
common/config/config.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gopkg.in/ini.v1"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
const (
|
||||
ProgramName = "卡西米尔唱片机"
|
||||
Version = "beta 0.9.7"
|
||||
)
|
||||
|
||||
const (
|
||||
ConfigPath = "./config.ini"
|
||||
AssetsPath = "./assets"
|
||||
)
|
||||
|
||||
func GetAssetPath(name string) string {
|
||||
return path.Join(AssetsPath, name)
|
||||
}
|
||||
|
||||
var DEFAULT_CONFIGS = []Config{General}
|
||||
|
||||
type Config interface {
|
||||
Name() string
|
||||
OnLoad()
|
||||
OnSave()
|
||||
}
|
||||
|
||||
type BaseConfig struct {
|
||||
}
|
||||
|
||||
func (c *BaseConfig) OnLoad() {
|
||||
}
|
||||
|
||||
func (c *BaseConfig) OnSave() {
|
||||
}
|
||||
|
||||
var ConfigFile *ini.File
|
||||
var Configs = make([]Config, 0)
|
||||
var Environment = CurrentEnvironment()
|
||||
|
||||
func LoadFromFile(filename string) {
|
||||
var err error
|
||||
ConfigFile, err = ini.Load(filename)
|
||||
if err != nil {
|
||||
fmt.Println("config not found, using default config")
|
||||
ConfigFile = ini.Empty()
|
||||
}
|
||||
for _, cfg := range DEFAULT_CONFIGS {
|
||||
LoadConfig(cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
ConfigFile = ini.Empty()
|
||||
for _, cfg := range DEFAULT_CONFIGS {
|
||||
LoadConfig(cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func LoadConfig(cfg Config) {
|
||||
sec, err := ConfigFile.GetSection(cfg.Name())
|
||||
if err == nil {
|
||||
_ = sec.MapTo(cfg)
|
||||
}
|
||||
cfg.OnLoad()
|
||||
Configs = append(Configs, cfg)
|
||||
return
|
||||
}
|
||||
|
||||
func SaveToConfigFile(filename string) error {
|
||||
cfgFile := ini.Empty()
|
||||
for _, cfg := range Configs {
|
||||
cfg.OnSave()
|
||||
if err := cfgFile.Section(cfg.Name()).ReflectFrom(cfg); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
return cfgFile.SaveTo(filename)
|
||||
}
|
||||
|
||||
func LoadJson(path string, dst any) error {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(data, dst)
|
||||
}
|
||||
|
||||
func SaveJson(path string, dst any) error {
|
||||
data, err := json.MarshalIndent(dst, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(path, data, 0666)
|
||||
}
|
||||
14
common/config/config_general.go
Normal file
14
common/config/config_general.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package config
|
||||
|
||||
type _GeneralConfig struct {
|
||||
BaseConfig
|
||||
Language string
|
||||
}
|
||||
|
||||
func (c *_GeneralConfig) Name() string {
|
||||
return "General"
|
||||
}
|
||||
|
||||
var General = &_GeneralConfig{
|
||||
Language: "en",
|
||||
}
|
||||
20
common/config/environment.go
Normal file
20
common/config/environment.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
type EnvType string
|
||||
|
||||
const (
|
||||
Development EnvType = "development"
|
||||
Production EnvType = "production"
|
||||
)
|
||||
|
||||
func CurrentEnvironment() EnvType {
|
||||
t := EnvType(os.Getenv("ENV"))
|
||||
if t == Production {
|
||||
return Production
|
||||
}
|
||||
return Development
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"AynaLivePlayer/common/config"
|
||||
"AynaLivePlayer/common/util"
|
||||
"AynaLivePlayer/config"
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -1,35 +1,26 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"AynaLivePlayer/config"
|
||||
nested "github.com/antonfisher/nested-logrus-formatter"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/virtuald/go-paniclog"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var Logger *logrus.Logger
|
||||
|
||||
func init() {
|
||||
Logger = logrus.New()
|
||||
Logger.SetLevel(config.Log.Level)
|
||||
Logger.SetFormatter(&nested.Formatter{
|
||||
FieldsOrder: []string{"Module"},
|
||||
HideKeys: true,
|
||||
NoColors: true,
|
||||
})
|
||||
//_ = os.Truncate(config.Log.Path, 0)
|
||||
file, err := os.OpenFile(config.Log.Path, os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err == nil {
|
||||
Logger.Out = io.MultiWriter(file, os.Stdout)
|
||||
} else {
|
||||
Logger.Info("Failed to log to file, using default stdout")
|
||||
}
|
||||
if config.Log.RedirectStderr {
|
||||
Logger.Info("panic/stderr redirect to log file")
|
||||
if _, err = paniclog.RedirectStderr(file); err != nil {
|
||||
Logger.Infof("Failed to redirect stderr to to file: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
//var Logger *logrus.Logger
|
||||
//
|
||||
//func init() {
|
||||
// Logger = logrus.New()
|
||||
// Logger.SetLevel(config.Log.Level)
|
||||
// Logger.SetFormatter(&nested.Formatter{
|
||||
// FieldsOrder: []string{"Module"},
|
||||
// HideKeys: true,
|
||||
// NoColors: true,
|
||||
// })
|
||||
// //_ = os.Truncate(config.Log.Path, 0)
|
||||
// file, err := os.OpenFile(config.Log.Path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
// if err == nil {
|
||||
// Logger.Out = io.MultiWriter(file, os.Stdout)
|
||||
// } else {
|
||||
// Logger.Info("Failed to log to file, using default stdout")
|
||||
// }
|
||||
// if config.Log.RedirectStderr {
|
||||
// Logger.Info("panic/stderr redirect to log file")
|
||||
// if _, err = paniclog.RedirectStderr(file); err != nil {
|
||||
// Logger.Infof("Failed to redirect stderr to to file: %s", err)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
5
common/util/window.go
Normal file
5
common/util/window.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package util
|
||||
|
||||
func GetWindowHandle(title string) uintptr {
|
||||
return getWindowHandle(title)
|
||||
}
|
||||
33
common/util/window_windows.go
Normal file
33
common/util/window_windows.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"golang.org/x/sys/windows"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var user32dll = windows.MustLoadDLL("user32.dll")
|
||||
|
||||
func getWindowHandle(title string) uintptr {
|
||||
var the_handle uintptr
|
||||
window_byte_name := []byte(title)
|
||||
|
||||
// Windows will loop over this function for each window.
|
||||
wndenumproc_function := syscall.NewCallback(func(hwnd uintptr, lparam uintptr) uintptr {
|
||||
// Allocate 100 characters so that it has something to write to.
|
||||
var filename_data [100]uint16
|
||||
user32dll.MustFindProc("GetWindowTextW").Call(hwnd, uintptr(unsafe.Pointer(&filename_data)), uintptr(100))
|
||||
|
||||
// If there's a match, save the value and return 0 to stop the iteration.
|
||||
if strings.Contains(windows.UTF16ToString(filename_data[:]), string(window_byte_name)) {
|
||||
the_handle = hwnd
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
})
|
||||
// Call the above looping function.
|
||||
user32dll.MustFindProc("EnumWindows").Call(wndenumproc_function, uintptr(0))
|
||||
|
||||
return the_handle
|
||||
}
|
||||
Reference in New Issue
Block a user