dae.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright 2006 Sony Computer Entertainment Inc.
  3. *
  4. * Licensed under the MIT Open Source License, for details please see license.txt or the website
  5. * http://www.opensource.org/licenses/mit-license.php
  6. *
  7. */
  8. #ifndef __DAE__
  9. #define __DAE__
  10. // We use the boost filesystem library for cross-platform file system support. You'll need
  11. // to have boost on your machine for this to work. For the Windows build boost is provided
  12. // in the external-libs folder, but for Linux it's expected that you'll install a boost
  13. // obtained via your distro's package manager. For example on Debian/Ubuntu, you can run
  14. // apt-get install libboost-filesystem-dev
  15. // to install the boost filesystem library on your machine.
  16. //
  17. // Disable the warnings we get from Boost
  18. // warning C4180: qualifier applied to function type has no meaning; ignored
  19. // warning C4245: 'argument' : conversion from 'int' to 'boost::filesystem::system_error_type',
  20. // signed/unsigned mismatch
  21. #ifdef _MSC_VER
  22. #pragma warning(push)
  23. #pragma warning(disable: 4180 4245)
  24. #include <wchar.h>
  25. #endif
  26. #ifndef NO_BOOST
  27. #include <boost/filesystem.hpp>
  28. #endif
  29. #ifdef _MSC_VER
  30. #pragma warning(pop)
  31. #endif
  32. #include <dae/daeTypes.h>
  33. #include <dae/daeError.h>
  34. #include <dae/daeDatabase.h>
  35. #include <dae/daeIOPlugin.h>
  36. #include <dae/daeAtomicType.h>
  37. #include <dae/daeMetaElement.h>
  38. #include <dae/daeIDRef.h>
  39. #include <dae/daeURI.h>
  40. #include <dae/daeUtils.h>
  41. #include <dae/daeRawResolver.h>
  42. #include <dae/daeSIDResolver.h>
  43. class domCOLLADA;
  44. typedef daeSmartRef<domCOLLADA> domCOLLADARef;
  45. class daeDatabase;
  46. // The DAE class is the core interface via which you interact with the DOM. It
  47. // has methods to load/save documents, get the root element of each document,
  48. // etc. Although internally the DOM works exclusively with URIs, the methods of
  49. // the DAE class that take document paths can take URIs or OS-specific file
  50. // paths.
  51. class DLLSPEC DAE
  52. {
  53. public:
  54. // Constructor. If no database or IO plugin are provided, a default database and
  55. // IO plugin will be used.
  56. DAE(daeDatabase* database = NULL, daeIOPlugin* ioPlugin = NULL)
  57. : atomicTypes(*this),
  58. baseUri(*this, cdom::getCurrentDirAsUri().c_str())
  59. {
  60. // See the end of the thread linked below for an explanation of why we have the DAE
  61. // constructor set up this way. Basically, I'm going to be changing the build output
  62. // location, and when this happens people sometimes continue to link against the old
  63. // libraries by accident (e.g. if they just do an svn update). By introducing a new
  64. // function that gets called from a function in a header file, I'm ensuring that someone
  65. // who tries linking against old libraries will get a link error. This may not sound
  66. // very nice, but it's certainly better than getting bizarre runtime crashes.
  67. // https://collada.org/public_forum/viewtopic.php?t=771&sid=f13c34f2d17ca720c5021bccbe5128b7
  68. init(database, ioPlugin);
  69. dummyFunction1();
  70. }
  71. virtual ~DAE();
  72. // Release all memory used by the DOM. You never need to call this explicitly. It's
  73. // called automatically when all DAE objects go out of scope.
  74. // Deletes directory returned by cdom::getSafeTmpDir().
  75. static void cleanup();
  76. public:
  77. // Database setup
  78. virtual daeDatabase* getDatabase();
  79. virtual daeInt setDatabase(daeDatabase* database);
  80. // IO Plugin setup
  81. virtual daeIOPlugin* getIOPlugin();
  82. virtual daeInt setIOPlugin(daeIOPlugin* plugin);
  83. // Creates a new document, returning null on failure.
  84. virtual domCOLLADA* add(const std::string& path);
  85. // Opens an existing document, returning null on failure.
  86. virtual domCOLLADA* open(const std::string& path);
  87. // Opens a document from memory, returning null on failure.
  88. virtual domCOLLADA* openFromMemory(const std::string& path, daeString buffer);
  89. // Write a document to the path specified by the document's URI, returning false on failure.
  90. virtual bool write(const std::string& path);
  91. // Write a document to the path specified in the second parameter, returning false on failure.
  92. virtual bool writeTo(const std::string& docPath, const std::string& pathToWriteTo);
  93. // Writes all documents, returning false if any document failed to write.
  94. virtual bool writeAll();
  95. // Close a specific document, unloading all memory used by the document. Returns false on failure.
  96. virtual void close(const std::string& path);
  97. // Remove all loaded documents. Always returns DAE_OK.
  98. virtual daeInt clear();
  99. // Returns the total number of documents.
  100. virtual int getDocCount();
  101. // Returns the i'th document .
  102. virtual daeDocument* getDoc(int i);
  103. // Returns a document matching the path.
  104. virtual daeDocument* getDoc(const std::string& path);
  105. // Get the root domCOLLADA object corresponding to a particular document.
  106. virtual domCOLLADA* getRoot(const std::string& path);
  107. // Set the root domCOLLADA object corresponding to a particular document, returning false on failure.
  108. virtual bool setRoot(const std::string& path, domCOLLADA* root);
  109. // Returns the Collada version, i.e. 1.4, 1.5, etc. Note that this _isn't_ the
  110. // same as the DOM version (1.3, 2.0, ...).
  111. virtual daeString getDomVersion();
  112. // Returns the (modifiable) list of atomic type objects.
  113. daeAtomicTypeList& getAtomicTypes();
  114. // Get/set a daeMetaElement object given the meta object's type ID.
  115. daeMetaElement* getMeta(daeInt typeID);
  116. void setMeta(daeInt typeID, daeMetaElement& meta);
  117. // Get all daeMetaElement objects.
  118. daeMetaElementRefArray& getAllMetas();
  119. // Returns the list of URI resolvers. You can modify the list to add new resolvers.
  120. daeURIResolverList& getURIResolvers();
  121. // The base URI used for resolving relative URI references.
  122. daeURI& getBaseURI();
  123. void setBaseURI(const daeURI& uri);
  124. void setBaseURI(const std::string& uri);
  125. // Returns the list of ID reference resolvers. You can modify the list to add new
  126. // resolvers.
  127. daeIDRefResolverList& getIDRefResolvers();
  128. // Meant for internal DOM use only.
  129. daeRawRefCache& getRawRefCache();
  130. daeSidRefCache& getSidRefCache();
  131. // These functions specify the client's character encoding for the DOM. The
  132. // default is Utf8, but if you specify Latin1 then the DOM will use libxml's
  133. // character conversion functions to convert to Utf8 when writing data and
  134. // convert to Latin1 when reading data. This can help with the handling of
  135. // non-ASCII characters on Windows. Only when using libxml for xml I/O does
  136. // any character conversion occur.
  137. //
  138. // Most people can probably just ignore this completely. If you have trouble
  139. // with non-ASCII characters on Windows, try setting the char encoding to
  140. // Latin1 to see if that helps.
  141. //
  142. // Frankly this certainly isn't the best way of handling non-ASCII character
  143. // support on Windows, so this interface is a likely target for significant
  144. // changes in the future.
  145. //
  146. // See this Sourceforge thread for more info:
  147. // http://sourceforge.net/tracker/index.php?func=detail&aid=1818473&group_id=157838&atid=805426
  148. //
  149. enum charEncoding {
  150. Utf8,
  151. Latin1
  152. };
  153. // Global encoding setting. Defaults to Utf8. Set this if you want to make a
  154. // char encoding change and apply it to all DAE objects.
  155. static charEncoding getGlobalCharEncoding();
  156. static void setGlobalCharEncoding(charEncoding encoding);
  157. // Local encoding setting. If set, overrides the global setting. Useful for setting
  158. // a specific char encoding for a single DAE object but not for all DAE objects.
  159. charEncoding getCharEncoding();
  160. void setCharEncoding(charEncoding encoding);
  161. // Deprecated. Alternative methods are given.
  162. virtual daeInt load(daeString uri, daeString docBuffer = NULL); // Use open
  163. virtual daeInt save(daeString uri, daeBool replace=true); // Use write
  164. virtual daeInt save(daeUInt documentIndex, daeBool replace=true); // Use write
  165. virtual daeInt saveAs(daeString uriToSaveTo, daeString docUri, daeBool replace=true); // Use writeTo
  166. virtual daeInt saveAs(daeString uriToSaveTo, daeUInt documentIndex=0, daeBool replace=true); // Use writeTo
  167. virtual daeInt unload(daeString uri); // Use close
  168. virtual domCOLLADA* getDom(daeString uri); // use getRoot
  169. virtual daeInt setDom(daeString uri, domCOLLADA* dom); // use setRoot
  170. private:
  171. void init(daeDatabase* database, daeIOPlugin* ioPlugin);
  172. void dummyFunction1();
  173. std::string makeFullUri(const std::string& path);
  174. domCOLLADA* openCommon(const std::string& path, daeString buffer);
  175. bool writeCommon(const std::string& docPath, const std::string& pathToWriteTo, bool replace);
  176. daeDatabase *database;
  177. daeIOPlugin *plugin;
  178. bool defaultDatabase;
  179. bool defaultPlugin;
  180. daeAtomicTypeList atomicTypes;
  181. daeMetaElementRefArray metas;
  182. daeURI baseUri;
  183. daeURIResolverList uriResolvers;
  184. daeIDRefResolverList idRefResolvers;
  185. daeRawRefCache rawRefCache;
  186. daeSidRefCache sidRefCache;
  187. std::unique_ptr<charEncoding> localCharEncoding;
  188. static charEncoding globalCharEncoding;
  189. };
  190. template <typename T>
  191. inline T *daeSafeCast(daeElement *element)
  192. {
  193. if (element && element->typeID() == T::ID())
  194. return (T*)element;
  195. return NULL;
  196. }
  197. #endif // __DAE_INTERFACE__