mirror of
https://github.com/AynaLivePlayer/AynaLivePlayer.git
synced 2025-12-13 05:28:16 +08:00
config panel, kuwo source, playlist operation, bug fix @6, panic handling
This commit is contained in:
100
config/config.go
100
config/config.go
@@ -2,12 +2,11 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/ini.v1"
|
||||
"path"
|
||||
)
|
||||
|
||||
const VERSION = "alpha 0.4"
|
||||
const VERSION = "alpha 0.6"
|
||||
|
||||
const CONFIG_PATH = "./config.ini"
|
||||
const Assests_PATH = "./assets"
|
||||
@@ -16,91 +15,40 @@ func GetAssetPath(name string) string {
|
||||
return path.Join(Assests_PATH, name)
|
||||
}
|
||||
|
||||
type LogConfig struct {
|
||||
Path string
|
||||
Level logrus.Level
|
||||
type Config interface {
|
||||
Name() string
|
||||
}
|
||||
|
||||
var Log = &LogConfig{
|
||||
Path: "./log.txt",
|
||||
Level: logrus.InfoLevel,
|
||||
}
|
||||
var ConfigFile *ini.File
|
||||
var Configs = make([]Config, 0)
|
||||
|
||||
type LiveRoomConfig struct {
|
||||
History []string
|
||||
}
|
||||
|
||||
var LiveRoom = &LiveRoomConfig{History: []string{"9076804", "3819533"}}
|
||||
|
||||
type PlayerConfig struct {
|
||||
Playlists []string
|
||||
PlaylistsProvider []string
|
||||
PlaylistIndex int
|
||||
PlaylistRandom bool
|
||||
}
|
||||
|
||||
var Player = &PlayerConfig{
|
||||
Playlists: []string{"116746576", "646548465"},
|
||||
PlaylistsProvider: []string{"netease", "netease"},
|
||||
PlaylistIndex: 0,
|
||||
PlaylistRandom: true,
|
||||
}
|
||||
|
||||
type ProviderConfig struct {
|
||||
Priority []string
|
||||
LocalDir string
|
||||
}
|
||||
|
||||
var Provider = &ProviderConfig{
|
||||
Priority: []string{"local", "netease", "kuwo", "bilibili"},
|
||||
LocalDir: "./music",
|
||||
func LoadConfig(cfg Config) {
|
||||
sec, err := ConfigFile.GetSection(cfg.Name())
|
||||
if err == nil {
|
||||
_ = sec.MapTo(cfg)
|
||||
}
|
||||
Configs = append(Configs, cfg)
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
cfg, err := ini.Load(CONFIG_PATH)
|
||||
var err error
|
||||
ConfigFile, err = ini.Load(CONFIG_PATH)
|
||||
if err != nil {
|
||||
fmt.Println("config not found")
|
||||
SaveToConfigFile(CONFIG_PATH)
|
||||
return
|
||||
fmt.Println("config not found, using default config")
|
||||
ConfigFile = ini.Empty()
|
||||
}
|
||||
err = cfg.Section("Log").MapTo(Log)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = cfg.Section("LiveRoom").MapTo(LiveRoom)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(cfg.Section("Player").GetKey("Playlists"))
|
||||
err = cfg.Section("Player").MapTo(Player)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
err = cfg.Section("Provider").MapTo(Provider)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
for _, cfg := range []Config{Log, LiveRoom, Player, Provider} {
|
||||
LoadConfig(cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func SaveToConfigFile(filename string) error {
|
||||
cfg := ini.Empty()
|
||||
err := ini.ReflectFrom(cfg, &struct {
|
||||
Log *LogConfig
|
||||
LiveRoom *LiveRoomConfig
|
||||
Player *PlayerConfig
|
||||
Provider *ProviderConfig
|
||||
}{
|
||||
Log: Log,
|
||||
LiveRoom: LiveRoom,
|
||||
Player: Player,
|
||||
Provider: Provider,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
cfgFile := ini.Empty()
|
||||
for _, cfg := range Configs {
|
||||
if err := cfgFile.Section(cfg.Name()).ReflectFrom(cfg); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
return cfg.SaveTo(filename)
|
||||
return cfgFile.SaveTo(filename)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user