Adopt house golangci-lint config; fix all approved-linter findings
.golangci.yml is the prompts-repo standard verbatim plus one approved
exception (paralleltest, sneak 2026-07-06). Roughly 1,500 findings
fixed:
- autofix sweep (wsl_v5/nlreturn/intrange/modernize formatting), with
the misspell autofix REVERTED where it rewrote authentic C game text
("missle vanishes" stays, with nolint and a comment)
- error handling: errcheck sites get real handling — saveFile now
closes explicitly and removes corrupt saves, scoreboard writes are
documented best-effort, err113 sentinel errors (ErrSaveOutOfDate,
ErrScreenTooSmall); panics allowed for unrecoverable states per
MEMORY.md policy
- gosec: real fixes (ParseInt for SEED, 0600 scorefile) and justified
per-line nolints for provably-bounded conversions (randomMonsterLetter
helper collapses eight rnd(26)+'A' sites)
- API tidying from linters: pointer receivers on flag types
(recvcheck), msg helpers renamed addmsgf/doaddf/Printwf/MvPrintwf
(goprintffuncname), myExit()/findFloor(monst)/mkGameInput(t) drop
always-constant params (unparam), gameEnd carries no status
- gocritic/staticcheck: if-else chains to switches, the pack.c
inventory filter untangled into matchesFilter, main.go split so
defers run before exit (exitAfterDefer, funlen)
- revive doc comments on all exported flag types/consts/methods
Remaining findings are confined to linters pending sneak's exception
decision (mnd, gochecknoglobals, cyclop, nestif, gocognit, exhaustive,
goconst, testpackage); TODO.md records the state. MEMORY.md added at
repo root as the project's agent working notes.
This commit is contained in:
@@ -18,61 +18,45 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
os.Exit(run())
|
||||
}
|
||||
|
||||
// run carries the real main so that deferred terminal restoration runs
|
||||
// before the process exits (os.Exit skips defers).
|
||||
func run() int {
|
||||
scores := flag.Bool("s", false, "print the scoreboard and exit")
|
||||
deathDemo := flag.Bool("d", false, "die a random death (demo)")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
home, _ := os.UserHomeDir()
|
||||
|
||||
// get options from environment (main.c)
|
||||
rogueOpts := os.Getenv("ROGUEOPTS")
|
||||
name := ""
|
||||
if u, err := user.Current(); err == nil {
|
||||
name = u.Username
|
||||
}
|
||||
|
||||
wizard := os.Getenv("ROGUE_WIZARD") != ""
|
||||
// dungeon number: SEED for reproducible dungeons (wizard mode in C),
|
||||
// else time+pid
|
||||
var seed int32
|
||||
if env := os.Getenv("SEED"); env != "" && wizard {
|
||||
n, _ := strconv.Atoi(env)
|
||||
seed = int32(n)
|
||||
} else {
|
||||
seed = int32(time.Now().Unix()) + int32(os.Getpid())
|
||||
}
|
||||
|
||||
cfg := game.Config{
|
||||
Seed: seed,
|
||||
Name: name,
|
||||
RogueOpts: rogueOpts,
|
||||
Home: home,
|
||||
ScorePath: home + "/.rogue.scores",
|
||||
Wizard: wizard,
|
||||
}
|
||||
cfg := loadConfig()
|
||||
|
||||
if *scores {
|
||||
g := game.NewGame(cfg)
|
||||
g.ShowScores()
|
||||
return
|
||||
game.NewGame(cfg).ShowScores()
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
t, err := term.New()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
|
||||
return 1
|
||||
}
|
||||
defer t.Fini()
|
||||
|
||||
cfg.Term = t
|
||||
|
||||
var g *game.RogueGame
|
||||
|
||||
if args := flag.Args(); len(args) == 1 && !*deathDemo {
|
||||
// restore a saved game
|
||||
g, err = game.Restore(args[0], cfg)
|
||||
if err != nil {
|
||||
t.Fini()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
|
||||
return 1
|
||||
}
|
||||
} else {
|
||||
g = game.NewGame(cfg)
|
||||
@@ -80,22 +64,75 @@ func main() {
|
||||
|
||||
if *deathDemo {
|
||||
g.DeathDemo()
|
||||
return
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// SIGHUP/SIGTERM autosave (save.c auto_save)
|
||||
installAutosave(g, t)
|
||||
|
||||
runErr := g.Run()
|
||||
if runErr != nil {
|
||||
t.Fini()
|
||||
fmt.Fprintln(os.Stderr, runErr)
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// loadConfig gathers the game configuration from the environment: home
|
||||
// directory, ROGUEOPTS, user name, wizard mode, and the dungeon seed
|
||||
// (main.c's startup).
|
||||
func loadConfig() game.Config {
|
||||
home, _ := os.UserHomeDir()
|
||||
|
||||
name := ""
|
||||
|
||||
u, userErr := user.Current()
|
||||
if userErr == nil {
|
||||
name = u.Username
|
||||
}
|
||||
|
||||
wizard := os.Getenv("ROGUE_WIZARD") != ""
|
||||
|
||||
return game.Config{
|
||||
Seed: chooseSeed(wizard),
|
||||
Name: name,
|
||||
RogueOpts: os.Getenv("ROGUEOPTS"),
|
||||
Home: home,
|
||||
ScorePath: home + "/.rogue.scores",
|
||||
Wizard: wizard,
|
||||
}
|
||||
}
|
||||
|
||||
// installAutosave saves the game and exits on SIGHUP/SIGTERM (save.c
|
||||
// auto_save).
|
||||
func installAutosave(g *game.RogueGame, t *term.Tcell) {
|
||||
sig := make(chan os.Signal, 1)
|
||||
|
||||
signal.Notify(sig, syscall.SIGHUP, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
<-sig
|
||||
g.AutoSave()
|
||||
t.Fini()
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
if err := g.Run(); err != nil {
|
||||
t.Fini()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// chooseSeed picks the dungeon number: SEED for reproducible dungeons
|
||||
// (wizard mode, as in the C game), else time+pid (main.c).
|
||||
func chooseSeed(wizard bool) int32 {
|
||||
if env := os.Getenv("SEED"); env != "" && wizard {
|
||||
n, err := strconv.ParseInt(env, 10, 32)
|
||||
if err == nil {
|
||||
return int32(n)
|
||||
}
|
||||
}
|
||||
|
||||
// The C game computed `lowtime + getpid()` in int; the truncation to
|
||||
// 32 bits is the same wraparound the C int arithmetic performed.
|
||||
return int32(time.Now().Unix()&0x7fffffff) + //nolint:gosec // G115: deliberate wrap
|
||||
int32(os.Getpid()&0x7fffffff) //nolint:gosec // G115: deliberate wrap
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user