diff --git a/.vscode/launch.json b/.vscode/launch.json index 5364d53..ded79ea 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -19,6 +19,20 @@ "**/Outpost*.xml" ] }, + { + "name": "Launch Package (Barotrauma cookfile)", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}", + "cwd": "C:/Users/Administrator/Seafile/Games-Barotrauma", + "args": [ + "-loglevel", + "trace", + "-cook", + "cookassistant.yml", + ] + }, { "name": "Launch Package (Workspace)", "type": "go", diff --git a/main.go b/main.go index 1e7a451..dc0919a 100644 --- a/main.go +++ b/main.go @@ -16,16 +16,19 @@ import ( ) type GlobalStats struct { - TotalMatches int - TotalModifications int - ProcessedFiles int - FailedFiles int + TotalMatches int + TotalModifications int + ProcessedFiles int + FailedFiles int + ModificationsPerCommand map[string]int } var ( repo *git.Repository worktree *git.Worktree - stats GlobalStats = GlobalStats{} + stats GlobalStats = GlobalStats{ + ModificationsPerCommand: make(map[string]int), + } ) func main() { @@ -57,7 +60,7 @@ func main() { logger.Info("Initializing with log level: %s", level.String()) // The plan is: - // Load all commands + // Load all commands commands, err := utils.LoadCommands(args) if err != nil { logger.Error("Failed to load commands: %v", err) @@ -95,8 +98,6 @@ func main() { // Add performance tracking startTime := time.Now() - fileCount := 0 - modCount := 0 var fileMutex sync.Mutex for file, commands := range associations { @@ -127,6 +128,10 @@ func main() { 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... + stats.ModificationsPerCommand[command.Name] += len(commands) } if len(modifications) == 0 { @@ -138,8 +143,8 @@ func main() { fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr) fileMutex.Lock() - fileCount++ - modCount += count + stats.ProcessedFiles++ + stats.TotalModifications += count fileMutex.Unlock() logger.Info("Executed %d modifications for file %q", count, file) @@ -157,8 +162,8 @@ func main() { processingTime := time.Since(startTime) logger.Info("Processing completed in %v", processingTime) - if fileCount > 0 { - logger.Info("Average time per file: %v", processingTime/time.Duration(fileCount)) + if stats.ProcessedFiles > 0 { + logger.Info("Average time per file: %v", processingTime/time.Duration(stats.ProcessedFiles)) } // TODO: Also give each command its own logger, maybe prefix it with something... Maybe give commands a name? diff --git a/utils/flags.go b/utils/flags.go index 5790ca4..7c38f97 100644 --- a/utils/flags.go +++ b/utils/flags.go @@ -5,7 +5,9 @@ import ( ) var ( + // Deprecated GitFlag = flag.Bool("git", false, "Use git to manage files") + // Deprecated ResetFlag = flag.Bool("reset", false, "Reset files to their original state") LogLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE") Cookfile = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed") diff --git a/utils/modifycommand.go b/utils/modifycommand.go index 4ac4a5f..35cfa05 100644 --- a/utils/modifycommand.go +++ b/utils/modifycommand.go @@ -124,7 +124,10 @@ func LoadCommands(args []string) ([]ModifyCommand, error) { logger.Info("Loading commands from arguments: %v", args) newcommands, err = LoadCommandFromArgs(args) if err != nil { - return nil, fmt.Errorf("failed to load commands from args: %w", err) + if len(commands) == 0 { + return nil, fmt.Errorf("failed to load commands from args: %w", err) + } + logger.Warning("Failed to load commands from args: %v", err) } logger.Info("Successfully loaded %d commands from args", len(newcommands)) commands = append(commands, newcommands...) @@ -166,7 +169,7 @@ func LoadCommandsFromCookFiles(s string) ([]ModifyCommand, error) { } commands := []ModifyCommand{} - cookFiles, err := doublestar.Glob(os.DirFS(cwd), "*.yaml") + cookFiles, err := doublestar.Glob(os.DirFS(cwd), *Cookfile) if err != nil { return nil, fmt.Errorf("failed to glob cook files: %w", err) } diff --git a/utils/modifycommand_test.go b/utils/modifycommand_test.go index ac043e7..9274026 100644 --- a/utils/modifycommand_test.go +++ b/utils/modifycommand_test.go @@ -523,6 +523,36 @@ func TestLoadCommandsFromCookFileWithMultipleEntries(t *testing.T) { assert.Equal(t, "append", commands[2].Lua) } +func TestLoadCommandsFromCookFileLegitExample(t *testing.T) { + // Arrange + yamlData := []byte(` +- name: crewlayabout + pattern: '!anyvalue="(?!num)"!anyvalue="(?!num)"!anyvalue="(?!num)"!anyvalue="(?!num)"!anydistance="(?!num)"!anySkillBonus!anyvalue="(?!num)"!anyvalue="(?!num)"!anyvalue="(?!num)"!anyvalue="(?!num)"!anyvalue="(?!num)"!anyvalue="(?!num)' + lua: | + repairspeedpenalty=round(repairspeedpenalty/2, 2) + skillpenalty=round(skillpenalty/2, 0) + repairspeedbonus=round(repairspeedbonus*2, 2) + skillbonus=round(skillbonus*2, 0) + distance=round(distance*2, 0) + skillpenaltyv=skillpenalty + skillpenaltyv1=skillpenalty + skillpenaltyv2=skillpenalty + skillpenaltyv3=skillpenalty + skillpenaltyv4=skillpenalty + repairspeedpenaltyv=round(-repairspeedpenalty/100, 2) + files: + - '**/TalentsAssistant.xml' +`) + + // Act + commands, err := LoadCommandsFromCookFile(yamlData) + + // Assert + assert.NoError(t, err) + assert.Len(t, commands, 1) + assert.Equal(t, "crewlayabout", commands[0].Name) +} + // Valid command with minimum 3 arguments returns a ModifyCommand slice with correct values func TestLoadCommandFromArgsWithValidArguments(t *testing.T) { // Setup