Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
AnimationTypeGenerator.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <fstream>
5 #include <iostream>
6 #include <rapidjson/document.h>
7 #include <vector>
8 
9 #include "RehtiReader.hpp"
10 
15 {
16  std::vector<std::string> animationNames = readAnimations();
17 
18  std::ofstream hppFile(Config.GENERATED_ANIMATIONS_HPP_PATH);
19 
20  if (hppFile.is_open())
21  {
22  hppFile << "/**\n";
23  hppFile << " * WARNING: This file is auto-generated by a script. Do not modify manually.\n";
24  hppFile << " */\n\n";
25 
26  hppFile << "#pragma once\n\n";
27 
28  hppFile << "#include <cstdint>\n";
29  hppFile << "#include <algorithm>\n";
30  hppFile << "#include <string>\n\n";
31 
32  hppFile << "constexpr size_t ANIMATION_TYPE_COUNT = " << animationNames.size() << ";\n\n";
33  hppFile << "enum class AnimationType : uint32_t\n";
34  hppFile << "{\n";
35 
36  for (size_t i = 0; i < animationNames.size(); ++i)
37  {
38  std::string enumName = animationNames[i];
39  std::transform(enumName.begin(), enumName.end(), enumName.begin(), ::toupper);
40 
41  hppFile << " " << enumName;
42  if (i < animationNames.size())
43  {
44  hppFile << ",";
45  }
46  hppFile << "\n";
47  }
48  hppFile << " UNDEFINED\n";
49 
50  hppFile << "};\n\n";
51  hppFile << "inline const char* AnimationTypeStrings[] = {\n";
52 
53  for (size_t i = 0; i < animationNames.size(); ++i)
54  {
55  std::string lowercaseName = animationNames[i];
56  std::transform(lowercaseName.begin(), lowercaseName.end(), lowercaseName.begin(), ::tolower);
57 
58  hppFile << " \"" << lowercaseName << "\"";
59  if (i < animationNames.size() - 1)
60  {
61  hppFile << ",";
62  }
63  hppFile << "\n";
64  }
65 
66  hppFile << "};\n\n";
67 
68  // Generate getAnimationType function
69  hppFile << "inline AnimationType getAnimationType(const std::string animationName) {\n";
70  hppFile << " std::string lowercaseName(animationName);\n";
71  hppFile << " std::transform(lowercaseName.begin(), lowercaseName.end(), lowercaseName.begin(), ::tolower);\n\n";
72  hppFile << " for (size_t i = 0; i < ANIMATION_TYPE_COUNT; ++i) {\n";
73  hppFile << " if (lowercaseName.find(AnimationTypeStrings[i]) != std::string::npos) {\n";
74  hppFile << " return static_cast<AnimationType>(i);\n";
75  hppFile << " }\n";
76  hppFile << " }\n\n";
77  hppFile << " return AnimationType::UNDEFINED;\n";
78  hppFile << "}\n";
79 
80  hppFile.close();
81  std::cout << "GeneratedAnimations.hpp generated successfully.\n";
82  }
83  else
84  {
85  std::cerr << "Unable to open GeneratedAnimations.hpp for writing.\n";
86  }
87 }
std::vector< std::string > readAnimations()
Reads animations described in JSON file.
Definition: AnimationReader.cpp:10
void generateAnimationHppFile()
Generate GeneratedAnimations.hpp file.
Definition: AnimationTypeGenerator.hpp:14