Files
ngzz-mc/internal/config/config.go

114 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package config 解析 client.yaml / server.yaml 配置(设计.md §14
package config
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
// ClientConfig 客户端配置(设计.md §14.2)。
type ClientConfig struct {
Client struct {
WindowWidth int `yaml:"window_width"`
WindowHeight int `yaml:"window_height"`
WindowTitle string `yaml:"window_title"`
VSync bool `yaml:"vsync"`
FOV float64 `yaml:"fov"`
RenderDistance int `yaml:"render_distance"`
Language string `yaml:"language"`
Graphics struct {
SmoothLighting bool `yaml:"smooth_lighting"`
Mipmap bool `yaml:"mipmap"`
Anisotropic int `yaml:"anisotropic"`
} `yaml:"graphics"`
Audio struct {
Volume float64 `yaml:"volume"`
Music bool `yaml:"music"`
} `yaml:"audio"`
} `yaml:"client"`
}
// DefaultClient 客户端默认配置。
func DefaultClient() *ClientConfig {
c := &ClientConfig{}
c.Client.WindowWidth = 1280
c.Client.WindowHeight = 720
c.Client.WindowTitle = "年糕历险记"
c.Client.VSync = true
c.Client.FOV = 70
c.Client.RenderDistance = 12
c.Client.Language = "zh_cn"
c.Client.Graphics.SmoothLighting = true
c.Client.Graphics.Mipmap = true
c.Client.Graphics.Anisotropic = 4
c.Client.Audio.Volume = 1.0
c.Client.Audio.Music = true
return c
}
// ServerConfig 服务器配置(设计.md §14.1)。
type ServerConfig struct {
Server struct {
Port int `yaml:"port"`
MaxPlayers int `yaml:"max_players"`
ViewDistance int `yaml:"view_distance"`
TickRate int `yaml:"tick_rate"`
} `yaml:"server"`
World struct {
Seed int64 `yaml:"seed"`
Name string `yaml:"name"`
Generator struct {
BiomeScale float64 `yaml:"biome_scale"`
HeightScale float64 `yaml:"height_scale"`
CaveDensity float64 `yaml:"cave_density"`
} `yaml:"generator"`
} `yaml:"world"`
Mods struct {
Enabled bool `yaml:"enabled"`
Directory string `yaml:"directory"`
AllowScripts bool `yaml:"allow_scripts"`
ScriptTimeoutMS int `yaml:"script_timeout_ms"`
HotReload bool `yaml:"hot_reload"`
} `yaml:"mods"`
}
// DefaultServer 服务器默认配置。
func DefaultServer() *ServerConfig {
c := &ServerConfig{}
c.Server.Port = 25565
c.Server.MaxPlayers = 20
c.Server.ViewDistance = 10
c.Server.TickRate = 20
c.World.Seed = 123456789
c.World.Name = "NewWorld"
c.World.Generator.BiomeScale = 0.005
c.World.Generator.HeightScale = 0.01
c.World.Generator.CaveDensity = 0.1
c.Mods.Enabled = true
c.Mods.Directory = "mods"
c.Mods.AllowScripts = true
c.Mods.ScriptTimeoutMS = 5
return c
}
// Load 读取 YAML 配置;文件不存在时返回 defaults零拷贝不可行则回退默认构造
func Load[T any](path string, defaults *T) (*T, error) {
b, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return defaults, nil
}
return nil, fmt.Errorf("config.Load %s: %w", path, err)
}
out := new(T)
if defaults != nil {
*out = *defaults
}
if err := yaml.Unmarshal(b, out); err != nil {
return nil, fmt.Errorf("config.Load %s: %w", path, err)
}
return out, nil
}