* * Reads a byte from the buffer and decompress if needed * */
| 84 | * |
| 85 | */ |
| 86 | uint8_t ReadByte(LoadgameState &ls) |
| 87 | { |
| 88 | /* Old savegames have a nice compression algorithm (RLE) |
| 89 | which means that we have a chunk, which starts with a length |
| 90 | byte. If that byte is negative, we have to repeat the next byte |
| 91 | that many times ( + 1). Else, we need to read that amount of bytes. |
| 92 | Works pretty well if you have many zeros behind each other */ |
| 93 | |
| 94 | if (ls.chunk_size == 0) { |
| 95 | /* Read new chunk */ |
| 96 | int8_t new_byte = ReadByteFromFile(ls); |
| 97 | |
| 98 | if (new_byte < 0) { |
| 99 | /* Repeat next char for new_byte times */ |
| 100 | ls.decoding = true; |
| 101 | ls.decode_char = ReadByteFromFile(ls); |
| 102 | ls.chunk_size = -new_byte + 1; |
| 103 | } else { |
| 104 | ls.decoding = false; |
| 105 | ls.chunk_size = new_byte + 1; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | ls.total_read++; |
| 110 | ls.chunk_size--; |
| 111 | |
| 112 | return ls.decoding ? ls.decode_char : ReadByteFromFile(ls); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * |
no test coverage detected