From 4301294b662134b99b613436fede69a2b3c28d04 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 21 May 2025 12:53:23 +0200 Subject: [PATCH] Implement removing read achievements from source files --- main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 27a9ca3..4c4fa91 100644 --- a/main.go +++ b/main.go @@ -109,18 +109,59 @@ func saveAchievementsToSourceFiles(luaStates *sync.Map, achievements *sync.Map) // State itself should now be updated }() - writeToSource(path, state) + fixSource(path, state) return true }) wg.Wait() } -func writeToSource(path string, state *lua.LState) { - if err := state.DoString(script); err != nil { - logger.Error("Failed to write Lua state: %v", err) +func fixSource(path string, state *lua.LState) { + log := logger.Default.WithPrefix(path) + log.Info("Reading source file") + fileContent, err := os.ReadFile(path) + if err != nil { + logger.Error("Failed to read file: %v", err) return } + strContent := string(fileContent) + log.Info("Read %d bytes", len(strContent)) + + strContent = removeAchievements(strContent) + log.Info("Removed achievements, now %d bytes", len(strContent)) + + log.Info("Writing file") + err = os.WriteFile(path, []byte(strContent), 0644) + if err != nil { + logger.Error("Failed to write file: %v", err) + return + } + log.Info("Done") +} + +func removeAchievements(sourceContent string) string { + lines := strings.Split(sourceContent, "\n") + writeIndex := 0 + isInPlayers := false + for _, line := range lines { + if strings.HasPrefix(line, "\t[\"players\"] = {") { + isInPlayers = true + lines[writeIndex] = line + writeIndex++ + continue + } + if isInPlayers && strings.HasPrefix(line, "\t}") { + isInPlayers = false + lines[writeIndex] = line + writeIndex++ + continue + } + if !isInPlayers { + lines[writeIndex] = line + writeIndex++ + } + } + return strings.Join(lines[:writeIndex], "\n") } func updateSeenTable(state *lua.LState, seenTable lua.LValue, achievements *sync.Map) {