| 1 | /** |
| 2 | Copyright (c) テクニカル諏訪子 |
| 3 | |
| 4 | Permission is hereby granted to any person obtaining a copy of the software |
| 5 | Tetris (the "Software") to use, modify, merge, copy, publish, distribute, |
| 6 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 7 | |
| 8 | 1. **Origin Attribution**: |
| 9 | - You must not misrepresent the origin of the Software; you must not claim |
| 10 | you created the original Software. |
| 11 | - If the Software is used in a product, you must either: |
| 12 | a. Provide clear attribution in the product's documentation, user interface, |
| 13 | or other visible areas, **OR** |
| 14 | b. Pay the original developers a fee they specify in writing. |
| 15 | 2. **Usage Restriction**: |
| 16 | - The Software, or any derivative works, dependencies, or libraries |
| 17 | incorporating it, must not be used for censorship or to suppress freedom of |
| 18 | speech, expression, or creativity. Prohibited uses include, but are not |
| 19 | limited to: |
| 20 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 21 | ideas, or objective reality. |
| 22 | - Tools or systems designed to restrict access to information or |
| 23 | artistic works. |
| 24 | 3. **Notice Preservation**: |
| 25 | - This license and the above copyright notice must remain intact in all copies |
| 26 | of the source code. |
| 27 | |
| 28 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 29 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 30 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 31 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 32 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 33 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 34 | */ |
| 35 | #include <tetris/sys/save.hh> |
| 36 | #include <util/file.hh> |
| 37 | #include <sstream> |
| 38 | #include <iostream> |
| 39 | |
| 40 | namespace tetris::sys { |
| 41 | SaveData GetScore() { |
| 42 | SaveData data; |
| 43 | vector<u8> fromFile = read_file("tetris.sav", true); |
| 44 | if (fromFile.empty()) return data; |
| 45 | |
| 46 | string content(fromFile.begin(), fromFile.end()); |
| 47 | vector<string> lines; |
| 48 | std::stringstream ss(content); |
| 49 | string line; |
| 50 | while (getline(ss, line)) { |
| 51 | if (!line.empty() && line.back() == '\r') line.pop_back(); |
| 52 | if (!line.empty()) lines.push_back(line); |
| 53 | } |
| 54 | |
| 55 | if (lines.size() < 4) return data; |
| 56 | if (lines[0] != string(data.magic)) return data; |
| 57 | data.highScore = stoul(lines[2]); |
| 58 | data.mostLines = static_cast<u16>(stoul(lines[3])); |
| 59 | data.bestLevel = static_cast<u16>(stoul(lines[4])); |
| 60 | |
| 61 | return data; |
| 62 | } |
| 63 | |
| 64 | bool SaveScore(u32 score, u16 lines, u16 level) { |
| 65 | SaveData data = GetScore(); |
| 66 | std::cout << score << " | " << data.highScore << std::endl; |
| 67 | std::cout << lines << " | " << data.mostLines << std::endl; |
| 68 | std::cout << level << " | " << data.bestLevel << std::endl; |
| 69 | if (score > data.highScore) data.highScore = score; |
| 70 | if (lines > data.mostLines) data.mostLines = lines; |
| 71 | if (level > data.bestLevel) data.bestLevel = level; |
| 72 | |
| 73 | vector<string> dat{ |
| 74 | string(data.magic), |
| 75 | std::to_string(data.Version), |
| 76 | std::to_string(data.highScore), |
| 77 | std::to_string(data.mostLines), |
| 78 | std::to_string(data.bestLevel), |
| 79 | }; |
| 80 | |
| 81 | return write_file("tetris.sav", dat, true); |
| 82 | } |
| 83 | } // namespace tetris::sys |