CCProperties.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Copyright (c) 2015-2017 Chukong Technologies
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. Original file from GamePlay3D: http://gameplay3d.org
  15. This file was modified to fit the cocos2d-x project
  16. */
  17. #ifndef __cocos2d_libs__CCProperties__
  18. #define __cocos2d_libs__CCProperties__
  19. #include <string>
  20. #include <functional>
  21. #include <cstdint>
  22. #include <vector>
  23. #include "renderer/CCTexture2D.h"
  24. #include "platform/CCPlatformMacros.h"
  25. #include "base/CCRef.h"
  26. #include "base/ccTypes.h"
  27. #include "base/CCVector.h"
  28. NS_CC_BEGIN
  29. class Properties;
  30. class Vec2;
  31. class Vec3;
  32. class Vec4;
  33. class Mat4;
  34. class Quaternion;
  35. class Data;
  36. /**
  37. * Defines a properties file for loading text files.
  38. *
  39. * A properties file has very simple syntax and can contain only namespaces and
  40. * name/value pairs (the properties of a namespace).
  41. * The file can have any file extension a user specifies.
  42. *
  43. * Here's an example of a simple
  44. * file that uses all the available features of the markup language:
  45. @verbatim
  46. // This is a comment.
  47. // This property is in the default namespace:
  48. integerProperty = 5
  49. // This line defines a namespace of type "mynamespace" without an ID:
  50. mynamespace
  51. {
  52. // This namespace can be retrieved by searching for its ID, "spriteTexture",
  53. // or by its name "texture":
  54. texture spriteTexture
  55. {
  56. fileName = sprite.png
  57. width = 64
  58. height = 64
  59. }
  60. // This property is in the "space" namespace:
  61. booleanProperty = true
  62. // It's legal to have a name without a value if you leave out the '=' character:
  63. foo
  64. // In fact, the '=' character is optional if you'd rather write:
  65. bar 23
  66. // But don't write this or you'll get an error:
  67. // illegalProperty =
  68. // Or this:
  69. // = 15
  70. // Properties objects let you retrieve values as various types.
  71. floatProperty = 3.333
  72. stringProperty = This is a string.
  73. vector3Property = 1.0, 5.0, 3.55
  74. colorProperty = 1.0, 0.4, 0.0, 1.0
  75. }
  76. @endverbatim
  77. * Retrieving information out of a file like this could be done in two ways. If the
  78. * available namespaces and name/value pairs are known in advance they can be queried by ID or name.
  79. * For example, if the namespace "spriteTexture" and its properties are required then they can
  80. * be retrieved with a call to getNamespace() followed by calls to getString() and getInt().
  81. * A namespace is stored and retrieved as a Properties object.
  82. * Reading the spriteTexture properties out of the file above in this way could be done with the following code:
  83. @verbatim
  84. // Create the top-level Properties object.
  85. Properties* properties = Properties::createNonRefCounted("example.properties");
  86. // Retrieve the "spriteTexture" namespace.
  87. Properties* spriteTexture = properties->getNamespace("spriteTexture");
  88. // Get the values of known texture properties out of the namespace.
  89. const char* fileName = spriteTexture->getString("fileName");
  90. int width = spriteTexture->getInt("width");
  91. int height = spriteTexture->getInt("height");
  92. // Deleting the top-level Properties object will clean up all nested namespaces.
  93. SAFE_DELETE(properties);
  94. @endverbatim
  95. * On the other hand, if the structure of the file is not known in advance its
  96. * namespaces and name/value pairs can be retrieved one by one using the getNextNamespace()
  97. * and getNextProperty() methods. The following method prints the contents of any properties file
  98. * to the console:
  99. @verbatim
  100. void printProperties(Properties* properties)
  101. {
  102. // Print the name and ID of the current namespace.
  103. const char* spacename = properties->getNamespace();
  104. const char* id = properties->getId();
  105. GP_WARN("Namespace: %s ID: %s\n{", spacename, id);
  106. // Print all properties in this namespace.
  107. const char* name = properties->getNextProperty();
  108. const char* value = NULL;
  109. while (name != NULL)
  110. {
  111. value = properties->getString(name);
  112. GP_WARN("%s = %s", name, value);
  113. name = properties->getNextProperty();
  114. }
  115. GP_WARN("}\n");
  116. // Print the properties of every namespace within this one.
  117. Properties* space = properties->getNextNamespace();
  118. while (space != NULL)
  119. {
  120. printProperties(space);
  121. space = properties->getNextNamespace();
  122. }
  123. }
  124. @endverbatim
  125. * Note that this method does not keep track of the namespace hierarchy, but could be
  126. * modified to do so. Also note that nothing in a properties file indicates the type
  127. * of a property. If the type is unknown, its string can be retrieved and interpreted
  128. * as necessary.
  129. */
  130. class CC_DLL Properties
  131. {
  132. public:
  133. /**
  134. * Data types supported by the properties class.
  135. */
  136. enum Type
  137. {
  138. NONE,
  139. STRING,
  140. NUMBER,
  141. VECTOR2,
  142. VECTOR3,
  143. VECTOR4,
  144. MATRIX
  145. };
  146. /**
  147. * Creates a Properties runtime settings from the specified URL, where the URL is of
  148. * the format "<file-path>.<extension>#<namespace-id>/<namespace-id>/.../<namespace-id>"
  149. * (and "#<namespace-id>/<namespace-id>/.../<namespace-id>" is optional).
  150. *
  151. * @param url The URL to create the properties from.
  152. *
  153. * @return The created Properties or NULL if there was an error.
  154. * @script{create}
  155. */
  156. static Properties* createNonRefCounted(const std::string& url);
  157. /**
  158. * Destructor.
  159. */
  160. ~Properties();
  161. /**
  162. * Get the name of the next property.
  163. *
  164. * If a valid next property is returned, the value of the property can be
  165. * retrieved using any of the get methods in this class, passing NULL for
  166. // the property name.
  167. *
  168. * @return The name of the next property, or NULL if there are no properties remaining.
  169. */
  170. const char* getNextProperty();
  171. /**
  172. * Get the next namespace.
  173. */
  174. Properties* getNextNamespace();
  175. /**
  176. * Rewind the getNextProperty() and getNextNamespace() iterators
  177. * to the beginning of the file.
  178. */
  179. void rewind();
  180. /**
  181. * Get a specific namespace by ID or name. This method will optionally
  182. * perform a depth-first search on all namespaces and inner namespaces
  183. * within this Property.
  184. *
  185. * @param id The ID or name of the namespace to find.
  186. * @param searchNames If true, namespace names are used in the search,
  187. * instead of namespace IDs. By default this parameter is false
  188. * and namespace IDs are searched.
  189. * @param recurse If true, perform a depth-first search, otherwise search
  190. * only the immediate child namespaces.
  191. *
  192. * @return A properties object with the given ID or name.
  193. */
  194. Properties* getNamespace(const char* id, bool searchNames = false, bool recurse = true) const;
  195. /**
  196. * Get the name of this Property's namespace.
  197. *
  198. * @return The name of this Property's namespace.
  199. */
  200. const char* getNamespace() const;
  201. /**
  202. * Get the ID of this Property's namespace. The ID should be a unique identifier,
  203. * but its uniqueness is not enforced.
  204. *
  205. * @return The ID of this Property's namespace.
  206. */
  207. const char* getId() const;
  208. /**
  209. * Check if a property with the given name is specified in this Properties object.
  210. *
  211. * @param name The name of the property to query.
  212. *
  213. * @return True if the property exists, false otherwise.
  214. */
  215. bool exists(const char* name) const;
  216. /**
  217. * Returns the type of a property.
  218. *
  219. * @param name The name of the property to interpret, or NULL to return the current property's type.
  220. *
  221. * @return The type of the property.
  222. */
  223. Type getType(const char* name = NULL) const;
  224. /**
  225. * Get the value of the given property as a string. This can always be retrieved,
  226. * whatever the intended type of the property.
  227. *
  228. * @param name The name of the property to interpret, or NULL to return the current property's value.
  229. * @param defaultValue The default value to return if the specified property does not exist.
  230. *
  231. * @return The value of the given property as a string, or the empty string if no property with that name exists.
  232. */
  233. const char* getString(const char* name = NULL, const char* defaultValue = NULL) const;
  234. /**
  235. * Sets the value of the property with the specified name.
  236. *
  237. * If there is no property in this namespace with the current name,
  238. * one is added. Otherwise, the value of the first property with the
  239. * specified name is updated.
  240. *
  241. * If name is NULL, the value current property (see getNextProperty) is
  242. * set, unless there is no current property, in which case false
  243. * is returned.
  244. *
  245. * @param name The name of the property to set.
  246. * @param value The property value.
  247. *
  248. * @return True if the property was set, false otherwise.
  249. */
  250. bool setString(const char* name, const char* value);
  251. /**
  252. * Interpret the value of the given property as a boolean.
  253. *
  254. * @param name The name of the property to interpret, or NULL to return the current property's value.
  255. * @param defaultValue the default value to return if the specified property does not exist within the properties file.
  256. *
  257. * @return true if the property exists and its value is "true", otherwise false.
  258. */
  259. bool getBool(const char* name = NULL, bool defaultValue = false) const;
  260. /**
  261. * Interpret the value of the given property as an integer.
  262. * If the property does not exist, zero will be returned.
  263. * If the property exists but could not be scanned, an error will be logged and zero will be returned.
  264. *
  265. * @param name The name of the property to interpret, or NULL to return the current property's value.
  266. *
  267. * @return The value of the given property interpreted as an integer.
  268. * Zero if the property does not exist or could not be scanned.
  269. */
  270. int getInt(const char* name = NULL) const;
  271. /**
  272. * Interpret the value of the given property as a floating-point number.
  273. * If the property does not exist, zero will be returned.
  274. * If the property exists but could not be scanned, an error will be logged and zero will be returned.
  275. *
  276. * @param name The name of the property to interpret, or NULL to return the current property's value.
  277. *
  278. * @return The value of the given property interpreted as a float.
  279. * Zero if the property does not exist or could not be scanned.
  280. */
  281. float getFloat(const char* name = NULL) const;
  282. /**
  283. * Interpret the value of the given property as a long integer.
  284. * If the property does not exist, zero will be returned.
  285. * If the property exists but could not be scanned, an error will be logged and zero will be returned.
  286. *
  287. * @param name The name of the property to interpret, or NULL to return the current property's value.
  288. *
  289. * @return The value of the given property interpreted as a long.
  290. * Zero if the property does not exist or could not be scanned.
  291. */
  292. long getLong(const char* name = NULL) const;
  293. /**
  294. * Interpret the value of the given property as a Matrix.
  295. * If the property does not exist, out will be set to the identity matrix.
  296. * If the property exists but could not be scanned, an error will be logged and out will be set
  297. * to the identity matrix.
  298. *
  299. * @param name The name of the property to interpret, or NULL to return the current property's value.
  300. * @param out The matrix to set to this property's interpreted value.
  301. *
  302. * @return True on success, false if the property does not exist or could not be scanned.
  303. */
  304. bool getMat4(const char* name, Mat4* out) const;
  305. /**
  306. * Interpret the value of the given property as a Vector2.
  307. * If the property does not exist, out will be set to Vector2(0.0f, 0.0f).
  308. * If the property exists but could not be scanned, an error will be logged and out will be set
  309. * to Vector2(0.0f, 0.0f).
  310. *
  311. * @param name The name of the property to interpret, or NULL to return the current property's value.
  312. * @param out The vector to set to this property's interpreted value.
  313. *
  314. * @return True on success, false if the property does not exist or could not be scanned.
  315. */
  316. bool getVec2(const char* name, Vec2* out) const;
  317. /**
  318. * Interpret the value of the given property as a Vector3.
  319. * If the property does not exist, out will be set to Vector3(0.0f, 0.0f, 0.0f).
  320. * If the property exists but could not be scanned, an error will be logged and out will be set
  321. * to Vector3(0.0f, 0.0f, 0.0f).
  322. *
  323. * @param name The name of the property to interpret, or NULL to return the current property's value.
  324. * @param out The vector to set to this property's interpreted value.
  325. *
  326. * @return True on success, false if the property does not exist or could not be scanned.
  327. */
  328. bool getVec3(const char* name, Vec3* out) const;
  329. /**
  330. * Interpret the value of the given property as a Vector4.
  331. * If the property does not exist, out will be set to Vector4(0.0f, 0.0f, 0.0f, 0.0f).
  332. * If the property exists but could not be scanned, an error will be logged and out will be set
  333. * to Vector4(0.0f, 0.0f, 0.0f, 0.0f).
  334. *
  335. * @param name The name of the property to interpret, or NULL to return the current property's value.
  336. * @param out The vector to set to this property's interpreted value.
  337. *
  338. * @return True on success, false if the property does not exist or could not be scanned.
  339. */
  340. bool getVec4(const char* name, Vec4* out) const;
  341. /**
  342. * Interpret the value of the given property as a Quaternion specified as an axis angle.
  343. * If the property does not exist, out will be set to Quaternion().
  344. * If the property exists but could not be scanned, an error will be logged and out will be set
  345. * to Quaternion().
  346. *
  347. * @param name The name of the property to interpret, or NULL to return the current property's value.
  348. * @param out The quaternion to set to this property's interpreted value.
  349. *
  350. * @return True on success, false if the property does not exist or could not be scanned.
  351. */
  352. bool getQuaternionFromAxisAngle(const char* name, Quaternion* out) const;
  353. /**
  354. * Interpret the value of the given property as an RGB color in hex and write this color to a Vector3.
  355. * E.g. 0xff0000 represents red and sets the vector to (1, 0, 0).
  356. * If the property does not exist, out will be set to Vector3(0.0f, 0.0f, 0.0f).
  357. * If the property exists but could not be scanned, an error will be logged and out will be set
  358. * to Vector3(0.0f, 0.0f, 0.0f).
  359. *
  360. * @param name The name of the property to interpret, or NULL to return the current property's value.
  361. * @param out The vector to set to this property's interpreted value.
  362. *
  363. * @return True on success, false if the property does not exist or could not be scanned.
  364. */
  365. bool getColor(const char* name, Vec3* out) const;
  366. /**
  367. * Interpret the value of the given property as an RGBA color in hex and write this color to a Vector4.
  368. * E.g. 0xff0000ff represents opaque red and sets the vector to (1, 0, 0, 1).
  369. * If the property does not exist, out will be set to Vector4(0.0f, 0.0f, 0.0f, 0.0f).
  370. * If the property exists but could not be scanned, an error will be logged and out will be set
  371. * to Vector4(0.0f, 0.0f, 0.0f, 0.0f).
  372. *
  373. * @param name The name of the property to interpret, or NULL to return the current property's value.
  374. * @param out The vector to set to this property's interpreted value.
  375. *
  376. * @return True on success, false if the property does not exist or could not be scanned.
  377. */
  378. bool getColor(const char* name, Vec4* out) const;
  379. /**
  380. * Gets the file path for the given property if the file exists.
  381. *
  382. * This method will first search for the file relative to the working directory.
  383. * If the file is not found then it will search relative to the directory the bundle file is in.
  384. *
  385. * @param name The name of the property.
  386. * @param path The string to copy the path to if the file exists.
  387. *
  388. * @return True if the property exists and the file exists, false otherwise.
  389. *
  390. * @script{ignore}
  391. */
  392. bool getPath(const char* name, std::string* path) const;
  393. /**
  394. * Returns the value of a variable that is set in this Properties object.
  395. *
  396. * Variables take on the format ${name} and are inherited from parent Property objects.
  397. *
  398. * @param name Name of the variable to get.
  399. * @param defaultValue Value to return if the variable is not found.
  400. *
  401. * @return The value of the specified variable, or defaultValue if not found.
  402. */
  403. const char* getVariable(const char* name, const char* defaultValue = NULL) const;
  404. /**
  405. * Sets the value of the specified variable.
  406. *
  407. * @param name Name of the variable to set.
  408. * @param value The value to set.
  409. */
  410. void setVariable(const char* name, const char* value);
  411. /**
  412. * Attempts to parse the specified string as a Vector2 value.
  413. *
  414. * On error, false is returned and the output is set to all zero values.
  415. *
  416. * @param str The string to parse.
  417. * @param out The value to populate if successful.
  418. *
  419. * @return True if a valid Vector2 was parsed, false otherwise.
  420. */
  421. static bool parseVec2(const char* str, Vec2* out);
  422. /**
  423. * Attempts to parse the specified string as a Vector3 value.
  424. *
  425. * On error, false is returned and the output is set to all zero values.
  426. *
  427. * @param str The string to parse.
  428. * @param out The value to populate if successful.
  429. *
  430. * @return True if a valid Vector3 was parsed, false otherwise.
  431. */
  432. static bool parseVec3(const char* str, Vec3* out);
  433. /**
  434. * Attempts to parse the specified string as a Vector4 value.
  435. *
  436. * On error, false is returned and the output is set to all zero values.
  437. *
  438. * @param str The string to parse.
  439. * @param out The value to populate if successful.
  440. *
  441. * @return True if a valid Vector4 was parsed, false otherwise.
  442. */
  443. static bool parseVec4(const char* str, Vec4* out);
  444. /**
  445. * Attempts to parse the specified string as an axis-angle value.
  446. *
  447. * The specified string is expected to contain four comma-separated
  448. * values, where the first three values represents the axis and the
  449. * fourth value represents the angle, in degrees.
  450. *
  451. * On error, false is returned and the output is set to all zero values.
  452. *
  453. * @param str The string to parse.
  454. * @param out A Quaternion populated with the orientation of the axis-angle, if successful.
  455. *
  456. * @return True if a valid axis-angle was parsed, false otherwise.
  457. */
  458. static bool parseAxisAngle(const char* str, Quaternion* out);
  459. /**
  460. * Attempts to parse the specified string as an RGB color value.
  461. *
  462. * @param str The string to parse.
  463. * @param out The value to populate if successful.
  464. *
  465. * @return True if a valid RGB color was parsed, false otherwise.
  466. */
  467. static bool parseColor(const char* str, Vec3* out);
  468. /**
  469. * Attempts to parse the specified string as an RGBA color value.
  470. *
  471. * @param str The string to parse.
  472. * @param out The value to populate if successful.
  473. *
  474. * @return True if a valid RGBA color was parsed, false otherwise.
  475. */
  476. static bool parseColor(const char* str, Vec4* out);
  477. private:
  478. /**
  479. * Internal structure containing a single property.
  480. */
  481. struct Property
  482. {
  483. std::string name;
  484. std::string value;
  485. Property(const std::string& aname, const std::string& avalue) : name(aname), value(avalue) { }
  486. };
  487. /**
  488. * Constructor.
  489. */
  490. Properties();
  491. /**
  492. * Constructs the Properties class from a file.
  493. *
  494. * @param stream The stream used for reading the properties from file.
  495. */
  496. Properties(Data* data, ssize_t* dataIdx);
  497. Properties(const Properties& copy);
  498. /**
  499. * Constructor. Read from the beginning of namespace specified.
  500. */
  501. Properties(Data* data, ssize_t* dataIdx, const std::string& name, const char* id, const char* parentID, Properties* parent);
  502. // Data manipulation methods
  503. void readProperties();
  504. void skipWhiteSpace();
  505. char* trimWhiteSpace(char* str);
  506. signed char readChar();
  507. char* readLine(char* output, int num);
  508. bool seekFromCurrent(int offset);
  509. bool eof();
  510. // Called after createNonRefCounted(); copies info from parents into derived namespaces.
  511. void resolveInheritance(const char* id = NULL);
  512. // Called by resolveInheritance().
  513. void mergeWith(Properties* overrides);
  514. // Clones the Properties object.
  515. Properties* clone();
  516. void setDirectoryPath(const std::string* path);
  517. void setDirectoryPath(const std::string& path);
  518. /**
  519. * Reads the next character from the Data. Returns EOF if the end of the Data is reached.
  520. */
  521. // XXX: hack in order to simulate GamePlay's Stream with Cocos2d's Data
  522. ssize_t *_dataIdx;
  523. Data *_data;
  524. std::string _namespace;
  525. std::string _id;
  526. std::string _parentID;
  527. std::vector<Property> _properties;
  528. std::vector<Property>::iterator _propertiesItr;
  529. std::vector<Properties*> _namespaces;
  530. std::vector<Properties*>::const_iterator _namespacesItr;
  531. std::vector<Property>* _variables;
  532. std::string* _dirPath;
  533. Properties* _parent;
  534. };
  535. }
  536. #endif // __cocos2d_libs__CCProperties__