~hristoast/openmw-validator

ref: 16f8ae329052dc7b12b3a3c2296f3e00002fce77 openmw-validator/validations.go -rw-r--r-- 5.0 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main

import (
	"log"
)

// Given the raw cfg data and program config, do validating on the cfg file (and
// all related work).
func runValidations(runCfg *runConfig) {
	log.Println("Welcome to the OpenMW validator!")
	log.Printf("Validating file: %s", runCfg.cfg)
	contentFileCount := 1
	dataDirCount := 1
	d := readCfgFile(runCfg.cfg)
	g := &gameConfig{
		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:         runCfg,
	}

	if len(d.contentFiles) == 0 || len(d.dataPaths) == 0 {
		log.Println("No valid data or content was found in the given cfg file!")
		return
	}

	if !runCfg.quiet {
		log.Println()
		log.Println("Checking all data paths...")
		log.Println()
	}
	for _, p := range d.dataPaths {
		dp, err := checkDataPath(p, dataDirCount, g)
		if err != nil {
			// Most likely this path didn't actually exist..
			if !runCfg.quiet {
				log.Println(err)
			}

		} else {
			dataDirCount++
			g.dataDirs = append(g.dataDirs, dp)
		}
	}

	if !runCfg.quiet {
		log.Println()
		log.Println("Reviewing all data paths...")
		log.Println()
	}

	var totalFiles int
	var totalPlugins int

	for _, p := range g.dataDirs {
		var fileWord string
		var pluginWord string

		fileCount := len(p.files)
		pluginCount := len(p.plugins)

		totalFiles += fileCount
		totalPlugins += pluginCount

		if fileCount > 1 || fileCount == 0 {
			fileWord = "files"
		} else {
			fileWord = "file"
		}

		if pluginCount > 1 || pluginCount == 0 {
			pluginWord = "plugins"
		} else {
			pluginWord = "plugin"
		}

		if fileCount == 0 && pluginCount == 0 {
			g.emptyPaths = append(g.emptyPaths, p.path)
		}

		if !runCfg.quiet {
			log.Printf("Data path #%d \"%s\" has %d %s and %d %s", p.order, p.path, fileCount, fileWord, pluginCount, pluginWord)
		}
	}

	if !runCfg.quiet {
		log.Println()
		log.Println("Checking all content files...")
		log.Println()
	}
	for _, c := range d.contentFiles {
		_ = checkContentFile(c, contentFileCount, g)
		contentFileCount++
	}

	log.Println()
	log.Printf("Total files found: %d", totalFiles)
	log.Printf("Total plugins found: %v", len(g.foundPlugins))
	if !g.runCfg.noReplace {
		log.Printf("Replaced file count: %v", len(g.replacedFiles))
	}
	log.Println()

	dupeCount := len(g.duplicatePaths)
	haveBadPaths := len(g.badPaths) > 0
	haveEmptyPaths := len(g.emptyPaths) > 0
	haveDupedPaths := dupeCount > 0
	haveBunkContent := len(g.bunkContent) > 0

	if haveBadPaths {
		log.Printf("Bad data paths count: %v", len(g.badPaths))
		log.Println("The following data paths have been configured but are not readable:")
		log.Println()
		for _, bp := range g.badPaths {
			log.Println(bp)
		}
		log.Println()
	}

	if haveEmptyPaths {
		log.Printf("Empty data paths count: %v", len(g.emptyPaths))
		log.Println("The following data paths have been configured but provide nothing:")
		log.Println()
		for _, ep := range g.emptyPaths {
			log.Println(ep)
		}
		log.Println()
	}

	if haveDupedPaths {
		log.Printf("Duplicated data paths count: %v", dupeCount)
		log.Println("The following data paths have been duplicated in your config:")
		log.Println()
		for _, dup := range g.duplicatePaths {
			log.Println(dup)
		}
		log.Println()
	}

	if haveBunkContent {
		log.Printf("Bad content files count: %v", len(g.bunkContent))
		log.Println("The following content files have been configured but are not readable:")
		log.Println()
		for _, cf := range g.bunkContent {
			log.Println(cf)
		}
		log.Println()
	}

	if !haveBadPaths && !haveEmptyPaths && !haveDupedPaths && !haveBunkContent {
		log.Println("Great Job! No problems detected.")
		log.Println()
	}

	//TODO: Report any paths that have been fully replaced

	if runCfg.checkPath != "" {
		var dd *dataDir
		for _, d := range g.dataDirs {
			if d.path == runCfg.checkPath {
				dd = d
			}
		}

		if dd != nil {
			replaced := g.checkSinglePath(runCfg.checkPath)
			if len(replaced) > 0 {
				if len(replaced) >= len(dd.files) {
					log.Println()
					log.Println("All files in this path are replaced!")
					log.Println()

				} else {
					log.Println()
					log.Printf("The following %d files in this path have been replaced:", len(replaced))
					log.Println()
					for _, r := range replaced {
						log.Printf("\"%s\" replaced by: \"%s\"", r.path, r.replacedByDir)
					}
					log.Println()
				}

			} else {
				log.Println()
				log.Println("Nothing from this data path gets replaced!")
				log.Println()
			}

		} else {
			log.Println()
			log.Println("ERROR: The given path is not a configured data path!")
			log.Println()
		}

	}

	//TODO: Report unused content files (ones found in data paths but not configured as content files)
	//TODO: Very quiet mode, that just exits 0 or 1
	//TODO: Tests for everything
	//TODO: Read .bsa files(?)
	//TODO: do we care about case-sensitivity? Maybe log those matches
}