| 21 | namespace OpenRCT2::CommandLine::Sprite |
| 22 | { |
| 23 | ExitCode build(const char** argv, int32_t argc, ImportMode spriteMode) |
| 24 | { |
| 25 | if (argc < 3) |
| 26 | { |
| 27 | fprintf(stdout, "usage: sprite build <spritefile> <sprite description file> [silent]\n"); |
| 28 | return ExitCode::fail; |
| 29 | } |
| 30 | |
| 31 | const utf8* spriteFilePath = argv[1]; |
| 32 | const utf8* spriteDescriptionPath = argv[2]; |
| 33 | const auto directoryPath = Path::GetDirectory(spriteDescriptionPath); |
| 34 | |
| 35 | json_t jsonSprites = Json::ReadFromFile(spriteDescriptionPath); |
| 36 | if (jsonSprites.is_null()) |
| 37 | { |
| 38 | fprintf(stderr, "Unable to read sprite description file: %s\n", spriteDescriptionPath); |
| 39 | return ExitCode::fail; |
| 40 | } |
| 41 | |
| 42 | if (jsonSprites.is_object() && !jsonSprites["images"].is_null()) |
| 43 | jsonSprites = jsonSprites["images"]; |
| 44 | |
| 45 | if (!jsonSprites.is_array()) |
| 46 | { |
| 47 | fprintf(stderr, "Error: expected array\n"); |
| 48 | return ExitCode::fail; |
| 49 | } |
| 50 | |
| 51 | bool silent = (argc >= 4 && strcmp(argv[3], "silent") == 0); |
| 52 | |
| 53 | // keep sprite file entirely in memory until ready to write out a complete, |
| 54 | // correct file |
| 55 | SpriteFile spriteFile; |
| 56 | spriteFile.Header.numEntries = 0; |
| 57 | spriteFile.Header.totalSize = 0; |
| 58 | |
| 59 | fprintf(stdout, "Building: %s\n", spriteFilePath); |
| 60 | |
| 61 | json_t sprite_description; |
| 62 | |
| 63 | uint32_t numSuccessful = 0; |
| 64 | |
| 65 | std::unordered_map<u8string, Image> images{}; |
| 66 | |
| 67 | ImageImporter importer; |
| 68 | |
| 69 | // Note: jsonSprite is deliberately left non-const: json_t behaviour changes when const |
| 70 | for (auto& [jsonKey, jsonSprite] : jsonSprites.items()) |
| 71 | { |
| 72 | if (!jsonSprite.is_object()) |
| 73 | { |
| 74 | fprintf(stderr, "Error: expected object for sprite %s\n", jsonKey.c_str()); |
| 75 | return ExitCode::fail; |
| 76 | } |
| 77 | |
| 78 | json_t colours = jsonSprite["colours"]; |
| 79 | if (colours.is_array()) |
| 80 | { |