5 Commits

Author SHA1 Message Date
35b3d8b099 Reduce some of the reads and writes
It's really not necessary
2025-03-28 23:39:11 +01:00
2e3e958e15 Fix some tests and add some logs 2025-03-28 23:31:44 +01:00
955afc4295 Refactor running commands to separate functions 2025-03-28 16:59:22 +01:00
2c487bc443 Implement "Isolate" commands
Commands that run alone one by one on reading and writing the file
This should be used on commands that will modify a large part of the
file (or generally large parts)
Since that can fuck up the indices of other commands when ran together
2025-03-28 16:56:39 +01:00
b77224176b Add file lua value 2025-03-28 16:47:21 +01:00
6 changed files with 127 additions and 48 deletions

7
.vscode/launch.json vendored
View File

@@ -12,11 +12,10 @@
"program": "${workspaceFolder}", "program": "${workspaceFolder}",
"cwd": "C:/Users/Administrator/Seafile/Games-Barotrauma", "cwd": "C:/Users/Administrator/Seafile/Games-Barotrauma",
"args": [ "args": [
"loglevel", "-loglevel",
"trace", "trace",
"(?-s)LightComponent!anyrange=\"(!num)\"", "-cook",
"*4", "*.yml",
"**/Outpost*.xml"
] ]
}, },
{ {

120
main.go
View File

@@ -80,6 +80,17 @@ func main() {
globs := utils.AggregateGlobs(commands) globs := utils.AggregateGlobs(commands)
logger.Debug("Aggregated %d globs before deduplication", utils.CountGlobsBeforeDedup(commands)) logger.Debug("Aggregated %d globs before deduplication", utils.CountGlobsBeforeDedup(commands))
for _, command := range commands {
logger.Trace("Command: %s", command.Name)
logger.Trace("Regex: %s", command.Regex)
logger.Trace("Files: %v", command.Files)
logger.Trace("Lua: %s", command.Lua)
logger.Trace("Git: %t", command.Git)
logger.Trace("Reset: %t", command.Reset)
logger.Trace("Isolate: %t", command.Isolate)
logger.Trace("LogLevel: %s", command.LogLevel)
}
// Resolve all the files for all the globs // Resolve all the files for all the globs
logger.Info("Found %d unique file patterns", len(globs)) logger.Info("Found %d unique file patterns", len(globs))
files, err := utils.ExpandGLobs(globs) files, err := utils.ExpandGLobs(globs)
@@ -107,7 +118,10 @@ func main() {
startTime := time.Now() startTime := time.Now()
var fileMutex sync.Mutex var fileMutex sync.Mutex
for file, commands := range associations { // This aggregation is great but what if one modification replaces the whole entire file?
// Shit......
// TODO: Add "Isolate" field to modifications which makes them run alone
for file, association := range associations {
workers <- struct{}{} workers <- struct{}{}
wg.Add(1) wg.Add(1)
logger.SafeGoWithArgs(func(args ...interface{}) { logger.SafeGoWithArgs(func(args ...interface{}) {
@@ -122,43 +136,19 @@ func main() {
logger.Error("Failed to read file %q: %v", file, err) logger.Error("Failed to read file %q: %v", file, err)
return return
} }
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
fileDataStr := string(fileData) fileDataStr := string(fileData)
// Aggregate all the modifications and execute them fileDataStr, err = RunIsolateCommands(association, file, fileDataStr, &fileMutex)
modifications := []utils.ReplaceCommand{} if err != nil {
for _, command := range commands { logger.Error("Failed to run isolate commands for file %q: %v", file, err)
logger.Info("Processing file %q with command %q", file, command.Regex)
commands, err := processor.ProcessRegex(fileDataStr, command)
if err != nil {
logger.Error("Failed to process file %q with command %q: %v", file, command.Regex, err)
return
}
modifications = append(modifications, commands...)
// It is not guranteed that all the commands will be executed...
// TODO: Make this better
// We'd have to pass the map to executemodifications or something...
count, ok := stats.ModificationsPerCommand.Load(command.Name)
if !ok {
count = 0
}
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(commands))
}
if len(modifications) == 0 {
logger.Info("No modifications found for file %q", file)
return return
} }
// Sort commands in reverse order for safe replacements fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex)
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr) if err != nil {
logger.Error("Failed to run other commands for file %q: %v", file, err)
fileMutex.Lock() return
stats.ProcessedFiles++ }
stats.TotalModifications += count
fileMutex.Unlock()
logger.Info("Executed %d modifications for file %q", count, file)
err = os.WriteFile(file, []byte(fileDataStr), 0644) err = os.WriteFile(file, []byte(fileDataStr), 0644)
if err != nil { if err != nil {
@@ -239,3 +229,67 @@ func main() {
} }
} }
} }
func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, fileMutex *sync.Mutex) (string, error) {
// Aggregate all the modifications and execute them
modifications := []utils.ReplaceCommand{}
for _, command := range association.Commands {
logger.Info("Processing file %q with command %q", file, command.Regex)
newModifications, err := processor.ProcessRegex(fileDataStr, command, file)
if err != nil {
return fileDataStr, fmt.Errorf("failed to process file %q with command %q: %w", file, command.Regex, err)
}
modifications = append(modifications, newModifications...)
// It is not guranteed that all the commands will be executed...
// TODO: Make this better
// We'd have to pass the map to executemodifications or something...
count, ok := stats.ModificationsPerCommand.Load(command.Name)
if !ok {
count = 0
}
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications))
}
if len(modifications) == 0 {
logger.Info("No modifications found for file %q", file)
return fileDataStr, nil
}
// Sort commands in reverse order for safe replacements
var count int
fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
fileMutex.Lock()
stats.ProcessedFiles++
stats.TotalModifications += count
fileMutex.Unlock()
logger.Info("Executed %d modifications for file %q", count, file)
return fileDataStr, nil
}
func RunIsolateCommands(association utils.FileCommandAssociation, file string, fileDataStr string, fileMutex *sync.Mutex) (string, error) {
for _, isolateCommand := range association.IsolateCommands {
logger.Info("Processing file %q with isolate command %q", file, isolateCommand.Regex)
modifications, err := processor.ProcessRegex(fileDataStr, isolateCommand, file)
if err != nil {
return fileDataStr, fmt.Errorf("failed to process file %q with isolate command %q: %w", file, isolateCommand.Regex, err)
}
if len(modifications) == 0 {
logger.Warning("No modifications found for file %q", file)
return fileDataStr, nil
}
var count int
fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
fileMutex.Lock()
stats.ProcessedFiles++
stats.TotalModifications += count
fileMutex.Unlock()
logger.Info("Executed %d isolate modifications for file %q", count, file)
}
return fileDataStr, nil
}

View File

@@ -21,7 +21,9 @@ type CaptureGroup struct {
} }
// ProcessContent applies regex replacement with Lua processing // ProcessContent applies regex replacement with Lua processing
func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceCommand, error) { // The filename here exists ONLY so we can pass it to the lua environment
// It's not used for anything else
func ProcessRegex(content string, command utils.ModifyCommand, filename string) ([]utils.ReplaceCommand, error) {
var commands []utils.ReplaceCommand var commands []utils.ReplaceCommand
logger.Trace("Processing regex: %q", command.Regex) logger.Trace("Processing regex: %q", command.Regex)
@@ -79,6 +81,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
logger.Error("Error creating Lua state: %v", err) logger.Error("Error creating Lua state: %v", err)
return commands, fmt.Errorf("error creating Lua state: %v", err) return commands, fmt.Errorf("error creating Lua state: %v", err)
} }
L.SetGlobal("file", lua.LString(filename))
// Hmm... Maybe we don't want to defer this.. // Hmm... Maybe we don't want to defer this..
// Maybe we want to close them every iteration // Maybe we want to close them every iteration
// We'll leave it as is for now // We'll leave it as is for now
@@ -205,7 +208,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
for _, capture := range captureGroups { for _, capture := range captureGroups {
if capture.Value == capture.Updated { if capture.Value == capture.Updated {
logger.Info("Capture group unchanged: %s", capture.Value) logger.Info("Capture group unchanged: %s", LimitString(capture.Value, 50))
continue continue
} }

View File

@@ -2,7 +2,6 @@ package processor
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"modify/utils" "modify/utils"
"os" "os"
@@ -38,7 +37,7 @@ func ApiAdaptor(content string, regex string, lua string) (string, int, int, err
LogLevel: "TRACE", LogLevel: "TRACE",
} }
commands, err := ProcessRegex(content, command) commands, err := ProcessRegex(content, command, "test")
if err != nil { if err != nil {
return "", 0, 0, err return "", 0, 0, err
} }

View File

@@ -15,7 +15,7 @@ func ApiAdaptor(content string, regex string, lua string) (string, int, int, err
LogLevel: "TRACE", LogLevel: "TRACE",
} }
commands, err := processor.ProcessRegex(content, command) commands, err := processor.ProcessRegex(content, command, "test")
if err != nil { if err != nil {
return "", 0, 0, err return "", 0, 0, err
} }

View File

@@ -18,6 +18,7 @@ type ModifyCommand struct {
Git bool `yaml:"git"` Git bool `yaml:"git"`
Reset bool `yaml:"reset"` Reset bool `yaml:"reset"`
LogLevel string `yaml:"loglevel"` LogLevel string `yaml:"loglevel"`
Isolate bool `yaml:"isolate"`
} }
type CookFile []ModifyCommand type CookFile []ModifyCommand
@@ -39,6 +40,7 @@ func (c *ModifyCommand) Validate() error {
// Ehh.. Not much better... Guess this wasn't the big deal // Ehh.. Not much better... Guess this wasn't the big deal
var matchesMemoTable map[string]bool = make(map[string]bool) var matchesMemoTable map[string]bool = make(map[string]bool)
func Matches(path string, glob string) (bool, error) { func Matches(path string, glob string) (bool, error) {
key := fmt.Sprintf("%s:%s", path, glob) key := fmt.Sprintf("%s:%s", path, glob)
if matches, ok := matchesMemoTable[key]; ok { if matches, ok := matchesMemoTable[key]; ok {
@@ -53,10 +55,22 @@ func Matches(path string, glob string) (bool, error) {
return matches, nil return matches, nil
} }
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) { type FileCommandAssociation struct {
File string
IsolateCommands []ModifyCommand
Commands []ModifyCommand
}
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string]FileCommandAssociation, error) {
associationCount := 0 associationCount := 0
fileCommands := make(map[string][]ModifyCommand) fileCommands := make(map[string]FileCommandAssociation)
for _, file := range files { for _, file := range files {
fileCommands[file] = FileCommandAssociation{
File: file,
IsolateCommands: []ModifyCommand{},
Commands: []ModifyCommand{},
}
for _, command := range commands { for _, command := range commands {
for _, glob := range command.Files { for _, glob := range command.Files {
matches, err := Matches(file, glob) matches, err := Matches(file, glob)
@@ -66,15 +80,25 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s
} }
if matches { if matches {
logger.Debug("Found match for file %q and command %q", file, command.Regex) logger.Debug("Found match for file %q and command %q", file, command.Regex)
fileCommands[file] = append(fileCommands[file], command) association := fileCommands[file]
if command.Isolate {
association.IsolateCommands = append(association.IsolateCommands, command)
} else {
association.Commands = append(association.Commands, command)
}
fileCommands[file] = association
associationCount++ associationCount++
} }
} }
} }
logger.Debug("Found %d commands for file %q", len(fileCommands[file]), file) logger.Debug("Found %d commands for file %q", len(fileCommands[file].Commands), file)
if len(fileCommands[file]) == 0 { if len(fileCommands[file].Commands) == 0 {
logger.Info("No commands found for file %q", file) logger.Info("No commands found for file %q", file)
} }
if len(fileCommands[file].IsolateCommands) > 0 {
logger.Info("Found %d isolate commands for file %q", len(fileCommands[file].IsolateCommands), file)
}
} }
logger.Info("Found %d associations between %d files and %d commands", associationCount, len(files), len(commands)) logger.Info("Found %d associations between %d files and %d commands", associationCount, len(files), len(commands))
return fileCommands, nil return fileCommands, nil