~hristoast/openmw-validator

ref: 16f8ae329052dc7b12b3a3c2296f3e00002fce77 openmw-validator/files.go -rw-r--r-- 2.4 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
package main

import (
	"log"
	"path/filepath"
	"strings"
)

// Represent a plugin file defined in the openmw.cfg file
type contentFile struct {
	file  *dataFile
	order int
}

// Represent a data file and the associated data path, "local path"
// (e.g. meshes/x/foo.nif), and the type of file it is (directory, file).
type dataFile struct {
	path      string
	localPath string
}

// Is the given `dataFile` a valid plugin file?
func (d *dataFile) isPlugin() bool {
	f := strings.ToLower(d.localPath)
	return strings.HasSuffix(f, ".bsa") || strings.HasSuffix(f, ".esm") || strings.HasSuffix(f, ".esp") || strings.HasSuffix(f, ".omwaddon")
}

// Strings representing data paths (e.g. `/meshes/x/foobar.nif`) that have been
// replaced, and the data dirs of the replaced and replacing files.
type replacedFile struct {
	dir           string
	path          string
	replacedByDir string
}

func checkContentFile(cf string, order int, gc *gameConfig) bool {
	// Format the file...
	c := strings.Replace(cf, "content=", "", -1)
	c = strings.TrimPrefix(c, "'")
	c = strings.TrimPrefix(c, "\"")
	c = strings.TrimSuffix(c, "\r")
	c = strings.TrimSuffix(c, "'")
	c = strings.TrimSuffix(c, "\"")

	if !gc.runCfg.quiet {
		log.Printf("Checking: %v", c)
	}

	// Verify that the content file exists...
	var exists bool
	for _, content := range gc.foundPlugins {
		if strings.HasSuffix(content, c) {
			exists = true
			//TODO: Check if it already exists, register a replacement if so
			break
		}
	}

	if !exists {
		if !gc.runCfg.quiet {
			log.Printf("The content file \"%s\" doesn't exist in any configured data path!", c)
		}
		gc.bunkContent = append(gc.bunkContent, c)
		return false
	} else {
		gc.contentFiles = append(gc.contentFiles, &contentFile{
			file:  &dataFile{},
			order: 0,
		})
	}
	return true
}

// Check a file path against an internal map of files that should be ignored and
// return a bool indicating if it is or is not.
func ignoredFile(fullPath string) bool {
	ok := false

	// First check for an ignored extension.
	ignoredExtensions := []string{
		".7z",
		".txt",
		".zip",
	}
	for _, ext := range ignoredExtensions {
		if strings.HasSuffix(fullPath, ext) {
			return true
		}
	}

	// Then check for a specific file name.
	ignoredFiles := map[string]bool{
		".ds_store": true,
		"thumbs.db": true,
	}
	if _, ok = ignoredFiles[strings.ToLower(filepath.Base(fullPath))]; ok {
		return ok // true
	}

	return ok // false
}