diff --git a/config/server.go b/config/server.go index c552805..49f01ce 100644 --- a/config/server.go +++ b/config/server.go @@ -41,8 +41,8 @@ type Server struct { Port int `toml:"port,omitempty" zander:"port"` Hostname string `toml:"hostname,omitempty" zander:"sv_hostname,cvar"` Website string `toml:"website,omitempty" zander:"sv_website,cvar"` - IWAD string `toml:"iwad,omitempty" zander:"iwad"` - PWADs []string `toml:"pwads,omitempty" zander:"file"` + IWAD string `toml:"iwad,omitempty" zander:"iwad,find_in_path"` + PWADs []string `toml:"pwads,omitempty" zander:"file,find_in_path"` Skill int `toml:"skill,omitempty" zander:"skill"` MOTD string `toml:"motd,multiline,omitempty" zander:"sv_motd,cvar"` Maplist []string `toml:"maplist,omitempty" zander:"addmap,cvar"` @@ -78,11 +78,11 @@ func LoadServer(path string) (Server, error) { var rawcvarRegexp = regexp.MustCompile(`(\w+)\s+(.+)`) -func (s Server) Parameters() ([]string, error) { - return serverParams(s) +func (s Server) Parameters(wadDirs []string) ([]string, error) { + return serverParams(s, wadDirs) } -func serverParams(s any) ([]string, error) { +func serverParams(s any, wadDirs []string) ([]string, error) { var out []string t := reflect.TypeOf(s) @@ -92,7 +92,7 @@ func serverParams(s any) ([]string, error) { switch field.Kind() { case reflect.Struct: - x, err := serverParams(field.Interface()) + x, err := serverParams(field.Interface(), wadDirs) if err != nil { return nil, err } @@ -126,11 +126,29 @@ func serverParams(s any) ([]string, error) { case []string: for _, x := range v { if x != "" { + if zanderTag.HasOption("find_in_path") && !strings.Contains(x, string(os.PathSeparator)) { + y, err := FindWAD(x, wadDirs...) + if err != nil { + return nil, fmt.Errorf("%w: %s", err, x) + } + + x = y + } + out = append(out, option, x) } } case string: if v != "" { + if zanderTag.HasOption("find_in_path") && !strings.Contains(v, string(os.PathSeparator)) { + x, err := FindWAD(v, wadDirs...) + if err != nil { + return nil, fmt.Errorf("%w: %s", err, v) + } + + v = x + } + out = append(out, option, v) } case int: diff --git a/config/server_test.go b/config/server_test.go index fda8cec..a36fdb1 100644 --- a/config/server_test.go +++ b/config/server_test.go @@ -29,7 +29,7 @@ func Test_serverParams(t *testing.T) { }, } - out, err := serverParams(x) + out, err := serverParams(x, []string{}) assert.NoError(t, err) assert.Equal(t, []string{ diff --git a/doom/testdata/env/a/test3.wad b/config/testdata/env/a/test3.wad similarity index 100% rename from doom/testdata/env/a/test3.wad rename to config/testdata/env/a/test3.wad diff --git a/doom/testdata/env/b/test4.wad b/config/testdata/env/b/test4.wad similarity index 100% rename from doom/testdata/env/b/test4.wad rename to config/testdata/env/b/test4.wad diff --git a/doom/testdata/env/b/test5.wad b/config/testdata/env/b/test5.wad similarity index 100% rename from doom/testdata/env/b/test5.wad rename to config/testdata/env/b/test5.wad diff --git a/doom/testdata/env/c/test5.wad b/config/testdata/env/c/test5.wad similarity index 100% rename from doom/testdata/env/c/test5.wad rename to config/testdata/env/c/test5.wad diff --git a/doom/testdata/env/test2.wad b/config/testdata/env/test2.wad similarity index 100% rename from doom/testdata/env/test2.wad rename to config/testdata/env/test2.wad diff --git a/doom/testdata/r/r.wad b/config/testdata/r/r.wad similarity index 100% rename from doom/testdata/r/r.wad rename to config/testdata/r/r.wad diff --git a/doom/testdata/test.wad b/config/testdata/test.wad similarity index 100% rename from doom/testdata/test.wad rename to config/testdata/test.wad diff --git a/doom/wad.go b/config/wad.go similarity index 98% rename from doom/wad.go rename to config/wad.go index e18d14b..f56b63a 100644 --- a/doom/wad.go +++ b/config/wad.go @@ -1,4 +1,4 @@ -package doom +package config import ( "errors" diff --git a/doom/wad_test.go b/config/wad_test.go similarity index 96% rename from doom/wad_test.go rename to config/wad_test.go index bd5e2ad..117746e 100644 --- a/doom/wad_test.go +++ b/config/wad_test.go @@ -1,4 +1,4 @@ -package doom +package config import ( "os" @@ -6,6 +6,7 @@ import ( "strings" "testing" + "github.com/davecgh/go-spew/spew" "github.com/stretchr/testify/assert" ) @@ -23,6 +24,7 @@ var ( func Test_FindWAD_FoundInSingleDirectory(t *testing.T) { x, err := FindWAD("test.wad", wadDir) + spew.Dump(x) assert.NoError(t, err) assert.True(t, strings.HasSuffix(x, "test.wad")) } diff --git a/doom/odamex.go b/doom/odamex.go index 38cf72a..a5cd36f 100644 --- a/doom/odamex.go +++ b/doom/odamex.go @@ -45,17 +45,7 @@ func (s *OdamexServer) Copy() (Server, error) { } func (s *OdamexServer) newCmd() error { - if _, err := FindWAD(s.cfg.IWAD, s.wadPaths.Expanded()...); err != nil { - return fmt.Errorf("could not find IWAD %s", s.cfg.IWAD) - } - - for _, pwad := range s.cfg.PWADs { - if _, err := FindWAD(pwad, s.wadPaths.Expanded()...); err != nil { - return fmt.Errorf("could not find PWAD %s", pwad) - } - } - - params, err := s.cfg.Parameters() + params, err := s.cfg.Parameters(s.wadPaths.Expanded()) if err != nil { return fmt.Errorf("could not get config parameters: %w", err) } @@ -97,10 +87,6 @@ func (s *OdamexServer) newCmd() error { } s.cmd = exec.Command(s.binary, params...) - if len(s.wadPaths) > 0 { - s.cmd.Env = append(s.cmd.Env, fmt.Sprintf("DOOMWADPATH=%s", s.wadPaths.String())) - s.cmd.Env = append(s.cmd.Env, fmt.Sprintf("DOOMWADDIR=%s", s.wadPaths.Expanded()[0])) - } return nil } diff --git a/doom/zandronum.go b/doom/zandronum.go index 1eebde2..bc23cdf 100644 --- a/doom/zandronum.go +++ b/doom/zandronum.go @@ -45,17 +45,7 @@ func (s *ZandronumServer) Copy() (Server, error) { } func (s *ZandronumServer) newCmd() error { - if _, err := FindWAD(s.cfg.IWAD, s.wadPaths.Expanded()...); err != nil { - return fmt.Errorf("could not find IWAD %s", s.cfg.IWAD) - } - - for _, pwad := range s.cfg.PWADs { - if _, err := FindWAD(pwad, s.wadPaths.Expanded()...); err != nil { - return fmt.Errorf("could not find PWAD %s", pwad) - } - } - - params, err := s.cfg.Parameters() + params, err := s.cfg.Parameters(s.wadPaths.Expanded()) if err != nil { return fmt.Errorf("could not get config parameters: %w", err) } @@ -117,10 +107,6 @@ func (s *ZandronumServer) newCmd() error { } s.cmd = exec.Command(s.binary, params...) - if len(s.wadPaths) > 0 { - s.cmd.Env = append(s.cmd.Env, fmt.Sprintf("DOOMWADPATH=%s", s.wadPaths.String())) - s.cmd.Env = append(s.cmd.Env, fmt.Sprintf("DOOMWADDIR=%s", s.wadPaths.Expanded()[0])) - } return nil }