Configuration hardening

This commit is contained in:
2026-05-07 21:33:53 +02:00
parent bba0d2fb45
commit d1b20684f5
14 changed files with 1335 additions and 176 deletions
+32 -33
View File
@@ -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) {
+96
View File
@@ -199,6 +199,102 @@ func TestValidateProjectWarnsForMissingScriptReferences(t *testing.T) {
}
}
func TestValidateProjectUsesConfiguredBuiltinScriptPrefixes(t *testing.T) {
root := t.TempDir()
mustMkdir(t, filepath.Join(root, "src", "dialogs"))
mustMkdir(t, filepath.Join(root, "assets"))
mustMkdir(t, filepath.Join(root, "build"))
mustWriteFile(t, filepath.Join(root, "nwn-tool.yaml"), `
module:
name: Test Module
resref: testmod
paths:
source: src
assets: assets
build: build
validation:
builtin_script_prefixes:
- custom_
`)
mustWriteFile(t, filepath.Join(root, "src", "dialogs", "merchant.dlg.json"), `{
"file_type": "DLG ",
"file_version": "V3.2",
"root": {
"struct_type": 0,
"fields": [
{
"label": "Script",
"type": "ResRef",
"value": "custom_open_store"
}
]
}
}
`)
p, err := project.Load(root)
if err != nil {
t.Fatalf("load project: %v", err)
}
if err := p.ValidateLayout(); err != nil {
t.Fatalf("validate layout: %v", err)
}
if err := p.Scan(); err != nil {
t.Fatalf("scan: %v", err)
}
report := ValidateProject(p)
if report.HasErrors() || report.WarningCount() != 0 {
t.Fatalf("expected configured script prefix to suppress warning, got %#v", report.Diagnostics)
}
}
func TestValidateProjectUsesConfiguredRequiredFields(t *testing.T) {
root := t.TempDir()
mustMkdir(t, filepath.Join(root, "src", "module"))
mustMkdir(t, filepath.Join(root, "assets"))
mustMkdir(t, filepath.Join(root, "build"))
mustWriteFile(t, filepath.Join(root, "nwn-tool.yaml"), `
module:
name: Test Module
resref: testmod
paths:
source: src
assets: assets
build: build
validation:
required_fields:
ifo: []
`)
mustWriteFile(t, filepath.Join(root, "src", "module", "module.ifo.json"), `{
"file_type": "IFO ",
"file_version": "V3.2",
"root": {
"struct_type": 0,
"fields": []
}
}
`)
p, err := project.Load(root)
if err != nil {
t.Fatalf("load project: %v", err)
}
if err := p.ValidateLayout(); err != nil {
t.Fatalf("validate layout: %v", err)
}
if err := p.Scan(); err != nil {
t.Fatalf("scan: %v", err)
}
report := ValidateProject(p)
if report.HasErrors() {
t.Fatalf("expected configured required fields to suppress IFO error, got %#v", report.Diagnostics)
}
}
func TestValidateProjectReportsDecompiledModelsWithoutFailingValidation(t *testing.T) {
root := t.TempDir()
mustMkdir(t, filepath.Join(root, "src"))