summaryrefslogtreecommitdiff
path: root/listmaps.go
blob: 8a1c0882b13de4cf577fcbcea37b97ecece930f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main

import (
	"fmt"
	"os"
	"path"
	"runtime"
)

func allMaps() map[string]string {
	savesDir := ""
	switch runtime.GOOS {
	case "linux":
		savesDir = fmt.Sprintf("%s/.minecraft/saves", os.Getenv("HOME"))
	case "darwin":
		savesDir = fmt.Sprintf("%s/Library/Application Support/minecraft/saves", os.Getenv("HOME"))
	case "windows":
		savesDir = fmt.Sprintf(`%s\.minecraft\saves`, os.Getenv("appdata"))
	default:
		return nil
	}

	f, err := os.Open(savesDir)
	if err != nil {
		return nil
	}
	defer f.Close()
	fi, err := f.Stat()
	if (err != nil) || (!fi.IsDir()) {
		return nil
	}

	infos, err := f.Readdir(-1)
	if err != nil {
		return nil
	}

	maps := make(map[string]string)
	for _, info := range infos {
		if !info.IsDir() {
			continue
		}
		p := path.Join(savesDir, info.Name())

		leveldat := path.Join(p, "level.dat")
		fi, err := os.Stat(leveldat)
		if (err != nil) || (!fi.Mode().IsRegular()) {
			continue
		}

		maps[info.Name()] = leveldat
	}

	return maps
}