summaryrefslogtreecommitdiff
path: root/listmaps.go
blob: ea438905a925c011356e38fad4fe839bde9bf083 (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
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`, 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())

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

		maps[info.Name()] = path.Join(p, "region")
	}

	return maps
}