5 #include "rive/artboard.hpp"
6 #include "rive/file.hpp"
7 #include "rive/animation/linear_animation_instance.hpp"
8 #include "rive/animation/state_machine_instance.hpp"
9 #include "rive/animation/state_machine_input_instance.hpp"
10 #include "no_op_factory.hpp"
13 std::vector<bool> m_IsArray;
16 for (int i = 0; i < m_IsArray.size(); ++i) {
20 void add_c(const char key[], char c) {
23 printf("\"%s\": %c\n", key, c);
32 while (!m_IsArray.empty()) {
37 void add(const char key[], const char value[]) {
39 printf("\"%s\": \"%s\"\n", key, value);
41 void pushArray(const char key[] = nullptr) {
42 this->add_c(key, '[');
43 m_IsArray.push_back(true);
45 void pushStruct(const char key[] = nullptr) {
46 this->add_c(key, '{');
47 m_IsArray.push_back(false);
50 assert(!m_IsArray.empty());
51 char c = m_IsArray.front() ? ']' : '}';
58 void add(const char key[], int value) {
59 this->add(key, std::to_string(value).c_str());
63 //////////////////////////////////////////////////
65 static void dump(JSoner& js, rive::LinearAnimationInstance* anim) {
67 js.add("name", anim->name().c_str());
68 js.add("duration", std::to_string(anim->durationSeconds()).c_str());
69 js.add("loop", std::to_string(anim->loopValue()).c_str());
73 static void dump(JSoner& js, rive::StateMachineInstance* smi) {
75 js.add("name", smi->name().c_str());
76 if (auto count = smi->inputCount()) {
77 js.pushArray("inputs");
78 for (auto i = 0; i < count; ++i) {
79 auto inp = smi->input(i);
80 js.add("name", inp->name().c_str());
87 static void dump(JSoner& js, rive::ArtboardInstance* abi) {
89 js.add("name", abi->name().c_str());
90 if (auto count = abi->animationCount()) {
91 js.pushArray("animations");
92 for (size_t i = 0; i < count; ++i) {
93 dump(js, abi->animationAt(i).get());
97 if (auto count = abi->stateMachineCount()) {
98 js.pushArray("machines");
99 for (size_t i = 0; i < count; ++i) {
100 dump(js, abi->stateMachineAt(i).get());
107 static void dump(JSoner& js, rive::File* file) {
108 auto count = file->artboardCount();
109 js.pushArray("artboards");
110 for (size_t i = 0; i < count; ++i) {
111 dump(js, file->artboardAt(i).get());
116 static std::unique_ptr<rive::File> open_file(const char name[]) {
117 FILE* f = fopen(name, "rb");
122 fseek(f, 0, SEEK_END);
123 auto length = ftell(f);
124 fseek(f, 0, SEEK_SET);
126 std::vector<uint8_t> bytes(length);
128 if (fread(bytes.data(), 1, length, f) != length) {
129 printf("Failed to read file into bytes array\n");
133 static rive::NoOpFactory gFactory;
134 return rive::File::import(rive::toSpan(bytes), &gFactory);
137 static bool is_arg(const char arg[], const char target[], const char alt[] = nullptr) {
138 return !strcmp(arg, target) || (arg && !strcmp(arg, alt));
141 int main(int argc, const char* argv[]) {
142 const char* filename = nullptr;
144 for (int i = 1; i < argc; ++i) {
145 if (is_arg(argv[i], "--file", "-f")) {
146 filename = argv[++i];
149 printf("Unrecognized argument %s\n", argv[i]);
154 printf("Need --file filename\n");
158 auto file = open_file(filename);
160 printf("Can't open %s\n", filename);
166 dump(js, file.get());