|
| 1 | +package main |
| 2 | + |
| 3 | +/* rpgsave-decrypt |
| 4 | +cli tool to decompress RPG Maker MV save files. |
| 5 | +
|
| 6 | +`.rpgsave` files are compressed using lz-string, then encoded in base64. |
| 7 | +@daku10's go-lz-string library is used to decompress the save file. |
| 8 | +
|
| 9 | +usage: |
| 10 | + either: |
| 11 | + 1. drag and drop the save file onto the executable |
| 12 | + 2. rpgsave-decode.exe <save file path> |
| 13 | +*/ |
| 14 | + |
| 15 | +import ( |
| 16 | + "bufio" |
| 17 | + "bytes" |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "strconv" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/charmbracelet/log" |
| 25 | + lzstring "github.com/daku10/go-lz-string" // migrated from @pieroxy/lz-string-go upon issues with decoding (issues with int -> string conversion?) |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + logger := log.NewWithOptions(os.Stderr, log.Options{ |
| 30 | + ReportCaller: true, |
| 31 | + ReportTimestamp: true, |
| 32 | + }) |
| 33 | + // set log level |
| 34 | + logger.SetLevel(log.DebugLevel) |
| 35 | + logger.Info("starting rpgsave-decode") |
| 36 | + // get file path from command line args |
| 37 | + args := os.Args[1:] |
| 38 | + if len(args) == 0 { |
| 39 | + logger.Error("no file path provided") |
| 40 | + logger.Info("press enter to exit") |
| 41 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 42 | + os.Exit(1) |
| 43 | + } |
| 44 | + filePath := args[0] |
| 45 | + logger.Print("file path: " + filePath) |
| 46 | + // open file |
| 47 | + file, err := os.Open(filePath) |
| 48 | + if err != nil { |
| 49 | + logger.Error("error opening file") |
| 50 | + logger.Error(err) |
| 51 | + logger.Info("press enter to exit") |
| 52 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | + // read file |
| 56 | + fileInfo, err := file.Stat() |
| 57 | + if err != nil { |
| 58 | + logger.Error("error reading file") |
| 59 | + logger.Error(err) |
| 60 | + logger.Info("press enter to exit") |
| 61 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 62 | + os.Exit(1) |
| 63 | + } |
| 64 | + fileSize := fileInfo.Size() |
| 65 | + logger.Print("file size: " + fmt.Sprint(fileSize)) |
| 66 | + fileBytes := make([]byte, fileSize) |
| 67 | + _, err = file.Read(fileBytes) |
| 68 | + if err != nil { |
| 69 | + logger.Error("error reading file") |
| 70 | + logger.Error(err) |
| 71 | + logger.Info("press enter to exit") |
| 72 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 73 | + os.Exit(1) |
| 74 | + } |
| 75 | + // // decode base64 |
| 76 | + // log.Info("decoding base64") |
| 77 | + // decodedBytes, err := base64.StdEncoding.DecodeString(string(fileBytes)) |
| 78 | + // if err != nil { |
| 79 | + // log.Error("error decoding base64") |
| 80 | + // log.Error(err) |
| 81 | + // os.Exit(1) |
| 82 | + // } |
| 83 | + // deprecated, lzstring.DecompressFromBase64 does this automatically |
| 84 | + |
| 85 | + |
| 86 | + // decompress |
| 87 | + logger.Info("decompressing") |
| 88 | + decompressedBytes, err := lzstring.DecompressFromBase64(string(fileBytes)) |
| 89 | + // write to file |
| 90 | + logger.Info("writing to file") |
| 91 | + fileName := strings.Split(filePath, ".")[0] |
| 92 | + fileName += ".json" |
| 93 | + logger.Print("file name: " + fileName) |
| 94 | + if _, err := os.Stat(fileName); err == nil { |
| 95 | + logger.Warn("file", fileName, " exits, overwrite? (y/n)") |
| 96 | + overwrite, _ := bufio.NewReader(os.Stdin).ReadString('\n') |
| 97 | + overwrite = strings.TrimSpace(overwrite) |
| 98 | + if overwrite == "y" { |
| 99 | + logger.Info("trunicating file", fileName) |
| 100 | + err := os.Remove(fileName) |
| 101 | + if err != nil { |
| 102 | + logger.Error("error deleting file") |
| 103 | + logger.Error(err) |
| 104 | + logger.Info("press enter to exit") |
| 105 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 106 | + os.Exit(1) |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + } else { |
| 111 | + logger.Info("not overwriting") |
| 112 | + logger.Info("press enter to exit") |
| 113 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 114 | + os.Exit(0) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // pretty print json |
| 119 | + logger.Info("pretty printing json") |
| 120 | + var jsonData []byte |
| 121 | + // json Indent |
| 122 | + jsonData, err = json.MarshalIndent(decompressedBytes, "", " ") |
| 123 | + if err != nil { |
| 124 | + logger.Error("error marshaling JSON") |
| 125 | + logger.Error(err) |
| 126 | + logger.Info("press enter to exit") |
| 127 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 128 | + os.Exit(1) |
| 129 | + } |
| 130 | + // unquote |
| 131 | + jsonDataStr, err := strconv.Unquote(string(jsonData)) |
| 132 | + if err != nil { |
| 133 | + logger.Error("error unquoting JSON") |
| 134 | + logger.Error(err) |
| 135 | + logger.Info("press enter to exit") |
| 136 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 137 | + os.Exit(1) |
| 138 | + } |
| 139 | + jsonData = []byte(jsonDataStr) |
| 140 | + |
| 141 | + // Indent |
| 142 | + var indentedData bytes.Buffer |
| 143 | + err = json.Indent(&indentedData, jsonData, "", " ") |
| 144 | + if err != nil { |
| 145 | + logger.Error("error marshaling JSON") |
| 146 | + logger.Error(err) |
| 147 | + logger.Info("press enter to exit") |
| 148 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 149 | + os.Exit(1) |
| 150 | + } |
| 151 | + jsonData = indentedData.Bytes() |
| 152 | + |
| 153 | + // write to file |
| 154 | + err = os.WriteFile(fileName, jsonData, 0644) |
| 155 | + if err != nil { |
| 156 | + logger.Error("error writing to file") |
| 157 | + logger.Error(err) |
| 158 | + logger.Info("press enter to exit") |
| 159 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 160 | + os.Exit(1) |
| 161 | + } |
| 162 | + logger.Info("done!") |
| 163 | + logger.Info("press enter to exit") |
| 164 | + bufio.NewReader(os.Stdin).ReadBytes('\n') |
| 165 | + |
| 166 | +} |
0 commit comments