database.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "database.hpp"
  4. #include <iostream>
  5. #include <sstream>
  6. #include "malloc.hpp"
  7. #include "showmsg.hpp"
  8. #include "utilities.hpp"
  9. using namespace rathena;
  10. bool YamlDatabase::nodeExists( const ryml::NodeRef& node, const std::string& name ){
  11. return (node.num_children() > 0 && node.has_child(c4::to_csubstr(name)));
  12. }
  13. bool YamlDatabase::nodesExist( const ryml::NodeRef& node, std::initializer_list<const std::string> names ){
  14. bool missing = false;
  15. for( const std::string& name : names ){
  16. if( !this->nodeExists( node, name ) ){
  17. ShowError( "Missing mandatory node \"%s\".\n", name.c_str() );
  18. missing = true;
  19. }
  20. }
  21. if( missing ){
  22. this->invalidWarning( node, "At least one mandatory node did not exist.\n" );
  23. return false;
  24. }
  25. return true;
  26. }
  27. bool YamlDatabase::verifyCompatibility( const ryml::Tree& tree ){
  28. if( !this->nodeExists( tree.rootref(), "Header" ) ){
  29. ShowError( "No database \"Header\" was found.\n" );
  30. return false;
  31. }
  32. const ryml::NodeRef& headerNode = tree["Header"];
  33. std::string tmpType;
  34. if( !this->asString( headerNode, "Type", tmpType ) ){
  35. return false;
  36. }
  37. if( this->type != tmpType ){
  38. ShowError( "Database type mismatch: %s != %s.\n", this->type.c_str(), tmpType.c_str() );
  39. return false;
  40. }
  41. uint16 tmpVersion;
  42. if( !this->asUInt16( headerNode, "Version", tmpVersion ) ){
  43. return false;
  44. }
  45. if( tmpVersion != this->version ){
  46. if( tmpVersion > this->version ){
  47. ShowError( "Database version %hu is not supported. Maximum version is: %hu\n", tmpVersion, this->version );
  48. return false;
  49. }else if( tmpVersion >= this->minimumVersion ){
  50. ShowWarning( "Database version %hu is outdated and should be updated. Current version is: %hu\n", tmpVersion, this->version );
  51. ShowWarning( "Reduced compatibility with %s database file from '" CL_WHITE "%s" CL_RESET "'.\n", this->type.c_str(), this->currentFile.c_str() );
  52. }else{
  53. ShowError( "Database version %hu is not supported anymore. Minimum version is: %hu\n", tmpVersion, this->minimumVersion );
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. bool YamlDatabase::load(){
  60. bool ret = this->load( this->getDefaultLocation() );
  61. this->loadingFinished();
  62. return ret;
  63. }
  64. bool YamlDatabase::reload(){
  65. this->clear();
  66. return this->load();
  67. }
  68. bool YamlDatabase::load(const std::string& path) {
  69. ShowStatus("Loading '" CL_WHITE "%s" CL_RESET "'..." CL_CLL "\r", path.c_str());
  70. FILE* f = fopen(path.c_str(), "r");
  71. if (f == nullptr) {
  72. ShowError("Failed to open %s database file from '" CL_WHITE "%s" CL_RESET "'.\n", this->type.c_str(), path.c_str());
  73. return false;
  74. }
  75. fseek(f, 0, SEEK_END);
  76. size_t size = ftell(f);
  77. char* buf = (char *)aMalloc(size+1);
  78. rewind(f);
  79. size_t real_size = fread(buf, sizeof(char), size, f);
  80. // Zero terminate
  81. buf[real_size] = '\0';
  82. fclose(f);
  83. parser = {};
  84. ryml::Tree tree;
  85. try{
  86. tree = parser.parse_in_arena(c4::to_csubstr(path), c4::to_csubstr(buf));
  87. }catch( const std::runtime_error& e ){
  88. ShowError( "Failed to load %s database file from '" CL_WHITE "%s" CL_RESET "'.\n", this->type.c_str(), path.c_str() );
  89. ShowError( "There is likely a syntax error in the file.\n" );
  90. ShowError( "Error message: %s\n", e.what() );
  91. return false;
  92. }
  93. // Required here already for header error reporting
  94. this->currentFile = path;
  95. if (!this->verifyCompatibility(tree)){
  96. ShowError("Failed to verify compatibility with %s database file from '" CL_WHITE "%s" CL_RESET "'.\n", this->type.c_str(), this->currentFile.c_str());
  97. aFree(buf);
  98. return false;
  99. }
  100. const ryml::NodeRef& header = tree["Header"];
  101. if( this->nodeExists( header, "Clear" ) ){
  102. bool clear;
  103. if( this->asBool( header, "Clear", clear ) && clear ){
  104. this->clear();
  105. }
  106. }
  107. this->parse( tree );
  108. this->parseImports( tree );
  109. aFree(buf);
  110. return true;
  111. }
  112. void YamlDatabase::loadingFinished(){
  113. // Does nothing by default, just for hooking
  114. }
  115. void YamlDatabase::parse( const ryml::Tree& tree ){
  116. uint64 count = 0;
  117. if( this->nodeExists( tree.rootref(), "Body" ) ){
  118. const ryml::NodeRef& bodyNode = tree["Body"];
  119. size_t childNodesCount = bodyNode.num_children();
  120. size_t childNodesProgressed = 0;
  121. const char* fileName = this->currentFile.c_str();
  122. for( const ryml::NodeRef &node : bodyNode ){
  123. count += this->parseBodyNode( node );
  124. ShowStatus( "Loading [%" PRIdPTR "/%" PRIdPTR "] entries from '" CL_WHITE "%s" CL_RESET "'" CL_CLL "\r", ++childNodesProgressed, childNodesCount, fileName );
  125. }
  126. ShowStatus( "Done reading '" CL_WHITE "%" PRIu64 CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'" CL_CLL "\n", count, fileName );
  127. }
  128. }
  129. void YamlDatabase::parseImports( const ryml::Tree& rootNode ){
  130. if( this->nodeExists( rootNode.rootref(), "Footer" ) ){
  131. const ryml::NodeRef& footerNode = rootNode["Footer"];
  132. if( this->nodeExists( footerNode, "Imports") ){
  133. const ryml::NodeRef& importsNode = footerNode["Imports"];
  134. for( const ryml::NodeRef &node : importsNode ){
  135. std::string importFile;
  136. if( !this->asString( node, "Path", importFile ) ){
  137. continue;
  138. }
  139. if( this->nodeExists( node, "Mode" ) ){
  140. std::string mode;
  141. if( !this->asString( node, "Mode", mode ) ){
  142. continue;
  143. }
  144. #ifdef RENEWAL
  145. std::string compiledMode = "Renewal";
  146. #else
  147. std::string compiledMode = "Prerenewal";
  148. #endif
  149. if( compiledMode != mode ){
  150. // Skip this import
  151. continue;
  152. }
  153. }
  154. if (this->nodeExists(node, "Generator")) {
  155. bool isGenerator;
  156. if (!this->asBool(node, "Generator", isGenerator)) {
  157. continue;
  158. }
  159. if (!(shouldLoadGenerator && isGenerator))
  160. continue; // skip import
  161. }
  162. this->load( importFile );
  163. }
  164. }
  165. }
  166. }
  167. template <typename R> bool YamlDatabase::asType( const ryml::NodeRef& node, const std::string& name, R& out ){
  168. if( this->nodeExists( node, name ) ){
  169. const ryml::NodeRef& dataNode = node[c4::to_csubstr(name)];
  170. if (dataNode.val_is_null()) {
  171. this->invalidWarning(node, "Node \"%s\" is missing a value.\n", name.c_str());
  172. return false;
  173. }
  174. try{
  175. dataNode >> out;
  176. }catch( std::runtime_error const& ){
  177. this->invalidWarning( node, "Node \"%s\" cannot be parsed as %s.\n", name.c_str(), typeid( R ).name() );
  178. return false;
  179. }
  180. return true;
  181. }else{
  182. this->invalidWarning( node, "Missing node \"%s\".\n", name.c_str() );
  183. return false;
  184. }
  185. }
  186. bool YamlDatabase::asBool(const ryml::NodeRef& node, const std::string &name, bool &out) {
  187. const ryml::NodeRef& targetNode = node[c4::to_csubstr(name)];
  188. if (targetNode.val_is_null()) {
  189. this->invalidWarning(node, "Node \"%s\" is missing a value.\n", name.c_str());
  190. return false;
  191. }
  192. std::string str;
  193. targetNode >> str;
  194. util::tolower( str );
  195. if( str == "true" ){
  196. out = true;
  197. return true;
  198. }else if( str == "false" ){
  199. out = false;
  200. return true;
  201. }else{
  202. this->invalidWarning( targetNode, "Unknown boolean value: \"%s\".\n", str.c_str() );
  203. return false;
  204. }
  205. }
  206. bool YamlDatabase::asInt16( const ryml::NodeRef& node, const std::string& name, int16& out ){
  207. return asType<int16>( node, name, out);
  208. }
  209. bool YamlDatabase::asUInt16(const ryml::NodeRef& node, const std::string& name, uint16& out) {
  210. return asType<uint16>(node, name, out);
  211. }
  212. bool YamlDatabase::asInt32(const ryml::NodeRef& node, const std::string &name, int32 &out) {
  213. return asType<int32>(node, name, out);
  214. }
  215. bool YamlDatabase::asUInt32(const ryml::NodeRef& node, const std::string &name, uint32 &out) {
  216. return asType<uint32>(node, name, out);
  217. }
  218. bool YamlDatabase::asInt64(const ryml::NodeRef& node, const std::string &name, int64 &out) {
  219. return asType<int64>(node, name, out);
  220. }
  221. bool YamlDatabase::asUInt64(const ryml::NodeRef& node, const std::string &name, uint64 &out) {
  222. return asType<uint64>(node, name, out);
  223. }
  224. bool YamlDatabase::asFloat(const ryml::NodeRef& node, const std::string &name, float &out) {
  225. return asType<float>(node, name, out);
  226. }
  227. bool YamlDatabase::asDouble(const ryml::NodeRef& node, const std::string &name, double &out) {
  228. return asType<double>(node, name, out);
  229. }
  230. bool YamlDatabase::asString(const ryml::NodeRef& node, const std::string &name, std::string &out) {
  231. return asType<std::string>(node, name, out);
  232. }
  233. bool YamlDatabase::asUInt16Rate( const ryml::NodeRef& node, const std::string& name, uint16& out, uint16 maximum ){
  234. if( this->asUInt16( node, name, out ) ){
  235. if( out > maximum ){
  236. this->invalidWarning( node[c4::to_csubstr(name)], "Node \"%s\" with value %" PRIu16 " exceeds maximum of %" PRIu16 ".\n", name.c_str(), out, maximum );
  237. return false;
  238. }else if( out == 0 ){
  239. this->invalidWarning( node[c4::to_csubstr(name)], "Node \"%s\" needs to be at least 1.\n", name.c_str() );
  240. return false;
  241. }else{
  242. return true;
  243. }
  244. }else{
  245. return false;
  246. }
  247. }
  248. bool YamlDatabase::asUInt32Rate( const ryml::NodeRef& node, const std::string& name, uint32& out, uint32 maximum ){
  249. if( this->asUInt32( node, name, out ) ){
  250. if( out > maximum ){
  251. this->invalidWarning( node[c4::to_csubstr(name)], "Node \"%s\" with value %" PRIu32 " exceeds maximum of %" PRIu32 ".\n", name.c_str(), out, maximum );
  252. return false;
  253. }else if( out == 0 ){
  254. this->invalidWarning( node[c4::to_csubstr(name)], "Node \"%s\" needs to be at least 1.\n", name.c_str() );
  255. return false;
  256. }else{
  257. return true;
  258. }
  259. }else{
  260. return false;
  261. }
  262. }
  263. int32 YamlDatabase::getLineNumber(const ryml::NodeRef& node) {
  264. return parser.source().has_str() ? (int32)parser.location(node).line : 0;
  265. }
  266. int32 YamlDatabase::getColumnNumber(const ryml::NodeRef& node) {
  267. return parser.source().has_str() ? (int32)parser.location(node).col : 0;
  268. }
  269. void YamlDatabase::invalidWarning( const ryml::NodeRef& node, const char* fmt, ... ){
  270. va_list ap;
  271. va_start(ap, fmt);
  272. // Remove any remaining garbage of a previous loading line
  273. ShowMessage( CL_CLL );
  274. // Print the actual error
  275. _vShowMessage( MSG_ERROR, fmt, ap );
  276. va_end(ap);
  277. ShowError( "Occurred in file '" CL_WHITE "%s" CL_RESET "' on line %d and column %d.\n", this->currentFile.c_str(), this->getLineNumber(node), this->getColumnNumber(node));
  278. #ifdef DEBUG
  279. std::cout << node;
  280. #endif
  281. }
  282. std::string YamlDatabase::getCurrentFile(){
  283. return this->currentFile;
  284. }
  285. void YamlDatabase::setGenerator(bool shouldLoad) {
  286. shouldLoadGenerator = shouldLoad;
  287. }
  288. void on_yaml_error( const char* msg, size_t len, ryml::Location loc, void *user_data ){
  289. throw std::runtime_error( msg );
  290. }
  291. void do_init_database(){
  292. ryml::set_callbacks( ryml::Callbacks( nullptr, nullptr, nullptr, on_yaml_error ) );
  293. }