Implement removing read achievements from source files
This commit is contained in:
49
main.go
49
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) {
|
||||
|
Reference in New Issue
Block a user