~hristoast/openmw-validator

ref: 16f8ae329052dc7b12b3a3c2296f3e00002fce77 openmw-validator/config.go -rw-r--r-- 3.3 KiB
16f8ae32Hristos N. Triantafillou Better output 3 months ago
                                                                                
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/user"
	"runtime"
	"strings"
	"time"
)

// Represents data extracted from a given openmw.cfg file.
type cfgData struct {
	contentFiles []string
	dataPaths    []string
}

// Processed configuration for a given openmw.cfg file.
type gameConfig struct {
	dataDirs       []*dataDir
	contentFiles   []*contentFile
	replacedFiles  []*replacedFile
	dataFiles      map[string]*dataFile
	foundPlugins   []string
	badPaths       []string
	bunkContent    []string
	duplicatePaths []string
	emptyPaths     []string
	foundPaths     map[string]bool
	runCfg         *runConfig
}

// Check a single path and return all files within it that were replaced.
func (g *gameConfig) checkSinglePath(path string) []*replacedFile {
	log.Printf("Doing a check against a single data path: %s", path)
	var replaced []*replacedFile

	for _, rf := range g.replacedFiles {
		if strings.HasPrefix(rf.dir, path) {
			replaced = append(replaced, rf)
		}
	}

	return replaced
}

// Helper method for storing details about when one file replaces another.
func (g *gameConfig) registerReplacement(new *dataFile, old *dataFile) {
	g.replacedFiles = append(g.replacedFiles, &replacedFile{
		dir:           old.path,
		path:          new.localPath,
		replacedByDir: strings.Replace(new.path, new.localPath, "", -1),
	})
	if !g.runCfg.noReplace {
		log.Printf("Replaced: %s", old.path)
	}
}

// Program run configuration.
type runConfig struct {
	cfg       string
	checkPath string
	help      bool
	log       string
	noReplace bool
	quiet     bool
	stdout    bool
}

// Return a string representing the default location of the `openmw.cfg` file.
// It should do the right thing on several OSes.
func defaultCfgAndLog() (string, string) {
	var cfg, log string
	rightNow := time.Now().Format("2006-01-02-150405")
	thisOS := runtime.GOOS

	switch thisOS {
	case "linux":
		cfg = fmt.Sprintf("%s/.config/openmw/openmw.cfg", os.Getenv("HOME"))
		log = fmt.Sprintf("%s/.config/openmw/validator-%s.log", os.Getenv("HOME"), rightNow)

	case "darwin":
		cfg = fmt.Sprintf("%s/Library/Preferences/openmw/openmw.cfg", os.Getenv("HOME"))
		log = fmt.Sprintf("%s/Library/Preferences/openmw/validator-%s.log", os.Getenv("HOME"), rightNow)

	case "windows":
		u, _ := user.Current()
		cfg = fmt.Sprintf("%s\\Documents\\My Games\\OpenMW\\openmw.cfg", u.HomeDir)
		log = fmt.Sprintf("%s\\Documents\\My Games\\OpenMW\\validator-%s.log", u.HomeDir, rightNow)

	default:
		// What OS is this?!
		cfg = fmt.Sprintf("%s/.config/openmw/openmw.cfg", os.Getenv("HOME"))
		log = fmt.Sprintf("%s/.config/openmw/validator-%s.log", os.Getenv("HOME"), rightNow)
	}
	return cfg, log
}

// Read the given `filePath`, if it's a valid `openmw.cfg` file return the
// related `cfgData`.
func readCfgFile(filePath string) cfgData {
	var d cfgData

	data, err := ioutil.ReadFile(filePath)
	if err != nil {
		log.Printf("[ ERROR ] The given cfg file does not exist: %s", filePath)
	}

	splitData := strings.Split(string(data), "\n")
	contentFiles := make([]string, 0)
	dataPaths := make([]string, 0)

	for _, line := range splitData {
		if strings.HasPrefix(line, "data=") {
			dataPaths = append(dataPaths, string(line))
		}
		if strings.HasPrefix(line, "content=") {
			contentFiles = append(contentFiles, string(line))
		}
	}

	d.contentFiles = contentFiles
	d.dataPaths = dataPaths

	return d
}