Configuration hardening
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"gitea.westgate.pw/ShadowsOverWestgate/sow-tools/internal/erf"
|
||||
"gitea.westgate.pw/ShadowsOverWestgate/sow-tools/internal/gff"
|
||||
"gitea.westgate.pw/ShadowsOverWestgate/sow-tools/internal/music"
|
||||
"gitea.westgate.pw/ShadowsOverWestgate/sow-tools/internal/project"
|
||||
)
|
||||
|
||||
@@ -49,24 +50,6 @@ type assetGroupResolver struct {
|
||||
defaultGroup string
|
||||
}
|
||||
|
||||
var requiredFields = map[string][]string{
|
||||
"ifo": {"Mod_Name"},
|
||||
}
|
||||
|
||||
var builtinScriptPrefixes = []string{
|
||||
"nw_",
|
||||
"x0_",
|
||||
"x1_",
|
||||
"x2_",
|
||||
"x3_",
|
||||
"ga_",
|
||||
"gc_",
|
||||
"gen_",
|
||||
"gui_",
|
||||
"nwg_",
|
||||
"ta_",
|
||||
}
|
||||
|
||||
func ValidateProject(p *project.Project) Report {
|
||||
report := Report{}
|
||||
resourceIndex := map[string]string{}
|
||||
@@ -75,6 +58,7 @@ func ValidateProject(p *project.Project) Report {
|
||||
assetOccurrences := map[string][]assetOccurrence{}
|
||||
documents := make([]loadedDocument, 0, len(p.Inventory.SourceFiles))
|
||||
resolver := newAssetGroupResolver(p)
|
||||
effective := p.EffectiveConfig()
|
||||
|
||||
for _, rel := range p.Inventory.SourceFiles {
|
||||
if hasUppercaseResourceName(rel) {
|
||||
@@ -101,7 +85,7 @@ func ValidateProject(p *project.Project) Report {
|
||||
resourceIndex[key] = rel
|
||||
}
|
||||
|
||||
validateDocumentStructure(&report, rel, extension, document)
|
||||
validateDocumentStructure(&report, rel, extension, document, effective.Validation.RequiredFields)
|
||||
}
|
||||
|
||||
for _, rel := range p.Inventory.ScriptFiles {
|
||||
@@ -117,7 +101,8 @@ func ValidateProject(p *project.Project) Report {
|
||||
}
|
||||
|
||||
for _, rel := range p.Inventory.AssetFiles {
|
||||
if hasUppercaseResourceName(rel) && !isMusicSourceAsset(rel) {
|
||||
musicSource := isMusicSourceAsset(p, rel)
|
||||
if hasUppercaseResourceName(rel) && !musicSource {
|
||||
report.add(rel, "resource filenames should be lowercase", SeverityWarning)
|
||||
}
|
||||
base := strings.ToLower(strings.TrimSuffix(filepath.Base(rel), filepath.Ext(rel)))
|
||||
@@ -131,6 +116,9 @@ func ValidateProject(p *project.Project) Report {
|
||||
if _, exists := assetIndex[key]; !exists {
|
||||
assetIndex[key] = rel
|
||||
}
|
||||
if musicSource {
|
||||
continue
|
||||
}
|
||||
|
||||
group, err := resolver.groupFor(rel)
|
||||
if err != nil {
|
||||
@@ -147,7 +135,7 @@ func ValidateProject(p *project.Project) Report {
|
||||
classifyAssetDuplicates(&report, assetOccurrences)
|
||||
|
||||
for _, document := range documents {
|
||||
validateReferences(&report, document, resourceIndex, scriptIndex, assetIndex)
|
||||
validateReferences(&report, document, resourceIndex, scriptIndex, assetIndex, effective.Validation.BuiltinScriptPrefixes)
|
||||
}
|
||||
|
||||
slices.SortFunc(report.Diagnostics, func(a, b Diagnostic) int {
|
||||
@@ -296,7 +284,7 @@ func loadDocument(path string) (gff.Document, string, string, error) {
|
||||
return document, resref, extension, nil
|
||||
}
|
||||
|
||||
func validateDocumentStructure(report *Report, path, extension string, document gff.Document) {
|
||||
func validateDocumentStructure(report *Report, path, extension string, document gff.Document, requiredFields map[string][]string) {
|
||||
expectedType := strings.ToUpper(extension)
|
||||
if strings.TrimSpace(document.FileType) != "" && strings.TrimSpace(document.FileType) != expectedType {
|
||||
report.add(path, fmt.Sprintf("file_type %q does not match expected %q", document.FileType, expectedType), SeverityError)
|
||||
@@ -338,7 +326,7 @@ func validateUniqueLabels(report *Report, path, scope string, s gff.Struct) {
|
||||
}
|
||||
}
|
||||
|
||||
func validateReferences(report *Report, document loadedDocument, resources, scripts, assets map[string]string) {
|
||||
func validateReferences(report *Report, document loadedDocument, resources, scripts, assets map[string]string, builtinScriptPrefixes []string) {
|
||||
walkFields(document.Document.Root, func(field gff.Field) {
|
||||
value, ok := fieldStringValue(field.Value)
|
||||
if !ok || value == "" {
|
||||
@@ -347,7 +335,7 @@ func validateReferences(report *Report, document loadedDocument, resources, scri
|
||||
|
||||
switch {
|
||||
case isScriptField(field.Label):
|
||||
if isBuiltinScript(value) {
|
||||
if isBuiltinScript(value, builtinScriptPrefixes) {
|
||||
return
|
||||
}
|
||||
if _, exists := scripts[strings.ToLower(value)]; !exists {
|
||||
@@ -519,20 +507,31 @@ func hasAsset(name string, extensions []string, assets map[string]string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func isMusicSourceAsset(rel string) bool {
|
||||
func isMusicSourceAsset(p *project.Project, rel string) bool {
|
||||
rel = filepath.ToSlash(strings.TrimSpace(rel))
|
||||
if rel != "envi/music" && !strings.HasPrefix(rel, "envi/music/") {
|
||||
return false
|
||||
ext := strings.ToLower(filepath.Ext(rel))
|
||||
effective := p.EffectiveConfig()
|
||||
for _, dataset := range effective.Music.Datasets {
|
||||
if !music.PathIsUnder(dataset.Source, rel) {
|
||||
continue
|
||||
}
|
||||
for _, candidate := range dataset.ConvertExtensions {
|
||||
if ext == strings.ToLower(strings.TrimSpace(candidate)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
switch strings.ToLower(filepath.Ext(rel)) {
|
||||
case ".mp3", ".ogg":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
if music.PathIsUnder("envi/music", rel) {
|
||||
for _, candidate := range effective.Music.ConvertExtensions {
|
||||
if ext == strings.ToLower(strings.TrimSpace(candidate)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isBuiltinScript(name string) bool {
|
||||
func isBuiltinScript(name string, builtinScriptPrefixes []string) bool {
|
||||
lower := strings.ToLower(name)
|
||||
for _, prefix := range builtinScriptPrefixes {
|
||||
if strings.HasPrefix(lower, prefix) {
|
||||
|
||||
Reference in New Issue
Block a user