Skip to content

Commit

Permalink
Put full path of wads in cmd instead of relying on env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
nadams committed Jan 11, 2023
1 parent cb8a615 commit 6edb340
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 39 deletions.
30 changes: 24 additions & 6 deletions config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion config/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion doom/wad.go → config/wad.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package doom
package config

import (
"errors"
Expand Down
4 changes: 3 additions & 1 deletion doom/wad_test.go → config/wad_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package doom
package config

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)

Expand All @@ -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"))
}
Expand Down
16 changes: 1 addition & 15 deletions doom/odamex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 1 addition & 15 deletions doom/zandronum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 6edb340

Please sign in to comment.