main.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package main
  2. import (
  3. "./etdd"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "sync"
  9. )
  10. type result struct {
  11. handler *etdd.Logger
  12. file string
  13. }
  14. func main() {
  15. input := flag.String("i", "**.dm_84", "demo filename mask")
  16. output := flag.String("o", "etdd.log", "output file name (use -- for standard output)")
  17. colors := flag.Bool("c", false, "don't strip color codes")
  18. flag.Parse()
  19. files, err := filepath.Glob(*input)
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Printf("> ETDD v1.1 - Enemy Territory demo decoder\n")
  24. fmt.Printf("> a domestic cat (c) 2019\n\n")
  25. if len(files) == 0 {
  26. _, _ = fmt.Fprintf(os.Stderr, "No input files. Use --help.\n")
  27. }
  28. writeTo := os.Stdout
  29. writeErr := os.Stderr
  30. if *output != "--" {
  31. writeTo, err = os.Create(*output)
  32. if err != nil {
  33. panic(err)
  34. }
  35. writeErr = writeTo
  36. }
  37. // RC vulnerable.
  38. etdd.HuffInit()
  39. mutex := &sync.Mutex{}
  40. var wait sync.WaitGroup
  41. limiter := make(chan int, 16)
  42. n := 0
  43. var results []*result
  44. for _, file := range files {
  45. limiter <- 1
  46. wait.Add(1)
  47. mutex.Lock()
  48. handler := etdd.NewLogger(*colors)
  49. results = append(results, &result{&handler, file})
  50. mutex.Unlock()
  51. go func(file string, handler *etdd.Logger) {
  52. defer wait.Done()
  53. handler.Write("Demo: " + file + "\n")
  54. demo, err := etdd.Open(file)
  55. if err == nil {
  56. demo.Read(handler)
  57. demo.Close()
  58. } else {
  59. handler.Error(err)
  60. handler.End()
  61. }
  62. <-limiter
  63. mutex.Lock()
  64. completed := 0
  65. for _, done := range results {
  66. if !done.handler.Complete {
  67. break
  68. }
  69. completed++
  70. n++
  71. done.handler.WriteTo(writeTo, writeErr)
  72. if *output != "--" {
  73. fmt.Printf("[%05d/%05d]: %s\n", n, len(files), done.file)
  74. }
  75. }
  76. if completed > 0 {
  77. results = results[completed:]
  78. }
  79. mutex.Unlock()
  80. }(file, &handler)
  81. }
  82. wait.Wait()
  83. }