Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / rive-cpp / rivinfo / main.cpp
1 /*
2  * Copyright 2022 Rive
3  */
4
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"
11
12 class JSoner {
13     std::vector<bool> m_IsArray;
14
15     void tab() {
16         for (int i = 0; i < m_IsArray.size(); ++i) {
17             printf("\t");
18         }
19     }
20     void add_c(const char key[], char c) {
21         this->tab();
22         if (key) {
23             printf("\"%s\": %c\n", key, c);
24         } else {
25             printf("%c\n", c);
26         }
27     }
28
29 public:
30     JSoner() {}
31     ~JSoner() {
32         while (!m_IsArray.empty()) {
33             this->pop();
34         }
35     }
36
37     void add(const char key[], const char value[]) {
38         this->tab();
39         printf("\"%s\": \"%s\"\n", key, value);
40     }
41     void pushArray(const char key[] = nullptr) {
42         this->add_c(key, '[');
43         m_IsArray.push_back(true);
44     }
45     void pushStruct(const char key[] = nullptr) {
46         this->add_c(key, '{');
47         m_IsArray.push_back(false);
48     }
49     void pop() {
50         assert(!m_IsArray.empty());
51         char c = m_IsArray.front() ? ']' : '}';
52         m_IsArray.pop_back();
53
54         this->tab();
55         printf("%c\n", c);
56     }
57
58     void add(const char key[], int value) {
59         this->add(key, std::to_string(value).c_str());
60     }
61 };
62
63 //////////////////////////////////////////////////
64
65 static void dump(JSoner& js, rive::LinearAnimationInstance* anim) {
66     js.pushStruct();
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());
70     js.pop();
71 }
72
73 static void dump(JSoner& js, rive::StateMachineInstance* smi) {
74     js.pushStruct();
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());
81         }
82         js.pop();
83     }
84     js.pop();
85 }
86
87 static void dump(JSoner& js, rive::ArtboardInstance* abi) {
88     js.pushStruct();
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());
94         }
95         js.pop();
96     }
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());
101         }
102         js.pop();
103     }
104     js.pop();
105 }
106
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());
112     }
113     js.pop();
114 }
115
116 static std::unique_ptr<rive::File> open_file(const char name[]) {
117     FILE* f = fopen(name, "rb");
118     if (!f) {
119         return nullptr;
120     }
121
122     fseek(f, 0, SEEK_END);
123     auto length = ftell(f);
124     fseek(f, 0, SEEK_SET);
125
126     std::vector<uint8_t> bytes(length);
127
128     if (fread(bytes.data(), 1, length, f) != length) {
129         printf("Failed to read file into bytes array\n");
130         return nullptr;
131     }
132
133     static rive::NoOpFactory gFactory;
134     return rive::File::import(rive::toSpan(bytes), &gFactory);
135 }
136
137 static bool is_arg(const char arg[], const char target[], const char alt[] = nullptr) {
138     return !strcmp(arg, target) || (arg && !strcmp(arg, alt));
139 }
140
141 int main(int argc, const char* argv[]) {
142     const char* filename = nullptr;
143
144     for (int i = 1; i < argc; ++i) {
145         if (is_arg(argv[i], "--file", "-f")) {
146             filename = argv[++i];
147             continue;
148         }
149         printf("Unrecognized argument %s\n", argv[i]);
150         return 1;
151     }
152
153     if (!filename) {
154         printf("Need --file filename\n");
155         return 1;
156     }
157
158     auto file = open_file(filename);
159     if (!file) {
160         printf("Can't open %s\n", filename);
161         return 1;
162     }
163
164     JSoner js;
165     js.pushStruct();
166     dump(js, file.get());
167     return 0;
168 }