llassettype.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @file llassettype.cpp
  3. * @brief Implementatino of LLAssetType functionality.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include <time.h>
  34. #include "llassettype.h"
  35. #include "lldictionary.h"
  36. struct AssetEntry : public LLDictionaryEntry
  37. {
  38. AssetEntry(const char* desc_name,
  39. const char* type_name, // 8 characters limit !
  40. // For decoding to human readable form; put any and as many
  41. // printable characters you want in each one
  42. const char* human_name,
  43. EDragAndDropType dad_type,
  44. bool can_link, // Can you create a link to this type ?
  45. bool can_fetch, // Can you fetch this asset by Id ?
  46. bool can_know) // Can you see this asset Id ?
  47. :
  48. LLDictionaryEntry(desc_name),
  49. mTypeName(type_name),
  50. mHumanName(human_name),
  51. mDadType(dad_type),
  52. mCanLink(can_link),
  53. mCanFetch(can_fetch),
  54. mCanKnow(can_know)
  55. {
  56. llassert_always(strlen(mTypeName) <= 8);
  57. }
  58. const char* mTypeName;
  59. const char* mHumanName;
  60. EDragAndDropType mDadType;
  61. bool mCanLink;
  62. bool mCanFetch;
  63. bool mCanKnow;
  64. };
  65. class LLAssetDictionary : public LLDictionary<LLAssetType::EType, AssetEntry>
  66. {
  67. public:
  68. LLAssetDictionary();
  69. };
  70. // Since it is a small structure, let's initialize it unconditionally (i.e.
  71. // even if we do not log in) at global scope. This saves having to bother with
  72. // a costly LLSingleton (slow, lot's of CPU cycles and cache lines wasted) or
  73. // to find the right place where to construct the class on login... HB
  74. LLAssetDictionary gAssetDictionary;
  75. LLAssetDictionary::LLAssetDictionary()
  76. {
  77. // DESCRIPTION TYPE NAME HUMAN NAME DRAG&DROP TYPE CAN LINK ? CAN FETCH ? CAN KNOW ?
  78. // |----------------|------------|-------------------|-------------------|-----------|-----------|---------|
  79. addEntry(LLAssetType::AT_TEXTURE, new AssetEntry("TEXTURE", "texture", "texture", DAD_TEXTURE, false, false, true));
  80. addEntry(LLAssetType::AT_SOUND, new AssetEntry("SOUND", "sound", "sound", DAD_SOUND, false, true, true));
  81. addEntry(LLAssetType::AT_CALLINGCARD, new AssetEntry("CALLINGCARD", "callcard", "calling card", DAD_CALLINGCARD, false, false, false));
  82. addEntry(LLAssetType::AT_LANDMARK, new AssetEntry("LANDMARK", "landmark", "landmark", DAD_LANDMARK, false, true, true));
  83. addEntry(LLAssetType::AT_SCRIPT, new AssetEntry("SCRIPT", "script", "legacy script", DAD_NONE, false, false, false));
  84. addEntry(LLAssetType::AT_CLOTHING, new AssetEntry("CLOTHING", "clothing", "clothing", DAD_CLOTHING, true, true, true));
  85. addEntry(LLAssetType::AT_OBJECT, new AssetEntry("OBJECT", "object", "object", DAD_OBJECT, true, false, false));
  86. addEntry(LLAssetType::AT_NOTECARD, new AssetEntry("NOTECARD", "notecard", "note card", DAD_NOTECARD, false, false, true));
  87. addEntry(LLAssetType::AT_CATEGORY, new AssetEntry("CATEGORY", "category", "folder", DAD_CATEGORY, true, false, false));
  88. addEntry(LLAssetType::AT_LSL_TEXT, new AssetEntry("LSL_TEXT", "lsltext", "lsl2 script", DAD_SCRIPT, false, false, false));
  89. addEntry(LLAssetType::AT_LSL_BYTECODE, new AssetEntry("LSL_BYTECODE", "lslbyte", "lsl bytecode", DAD_NONE, false, false, false));
  90. addEntry(LLAssetType::AT_TEXTURE_TGA, new AssetEntry("TEXTURE_TGA", "txtr_tga", "tga texture", DAD_NONE, false, false, false));
  91. addEntry(LLAssetType::AT_BODYPART, new AssetEntry("BODYPART", "bodypart", "body part", DAD_BODYPART, true, true, true));
  92. addEntry(LLAssetType::AT_SOUND_WAV, new AssetEntry("SOUND_WAV", "snd_wav", "sound", DAD_NONE, false, false, false));
  93. addEntry(LLAssetType::AT_IMAGE_TGA, new AssetEntry("IMAGE_TGA", "img_tga", "targa image", DAD_NONE, false, false, false));
  94. addEntry(LLAssetType::AT_IMAGE_JPEG, new AssetEntry("IMAGE_JPEG", "jpeg", "jpeg image", DAD_NONE, false, false, false));
  95. addEntry(LLAssetType::AT_ANIMATION, new AssetEntry("ANIMATION", "animatn", "animation", DAD_ANIMATION, false, true, true));
  96. addEntry(LLAssetType::AT_GESTURE, new AssetEntry("GESTURE", "gesture", "gesture", DAD_GESTURE, true, true, true));
  97. addEntry(LLAssetType::AT_SIMSTATE, new AssetEntry("SIMSTATE", "simstate", "simstate", DAD_NONE, false, false, false));
  98. addEntry(LLAssetType::AT_LINK, new AssetEntry("LINK", "link", "sym link", DAD_LINK, false, false, true));
  99. addEntry(LLAssetType::AT_LINK_FOLDER, new AssetEntry("FOLDER_LINK", "link_f", "sym folder link", DAD_LINK, false, false, true));
  100. addEntry(LLAssetType::AT_MARKETPLACE_FOLDER,new AssetEntry("MARKETPLACE", "market", "marketplace", DAD_NONE, false, false, false));
  101. #if LL_MESH_ASSET_SUPPORT
  102. addEntry(LLAssetType::AT_MESH, new AssetEntry("MESH", "mesh", "mesh", DAD_MESH, false, true, true));
  103. #endif
  104. addEntry(LLAssetType::AT_SETTINGS, new AssetEntry("SETTINGS", "settings", "settings", DAD_SETTINGS, true, true, true));
  105. addEntry(LLAssetType::AT_MATERIAL, new AssetEntry("MATERIAL", "material", "render material", DAD_MATERIAL, true, true, true));
  106. addEntry(LLAssetType::AT_NONE, new AssetEntry("NONE", "-1", NULL, DAD_NONE, false, false, false));
  107. };
  108. //-----------------------------------------------------------------------------
  109. // LLAssetType static class
  110. //-----------------------------------------------------------------------------
  111. //static
  112. LLAssetType::EType LLAssetType::getType(const std::string& desc_name)
  113. {
  114. std::string s = desc_name;
  115. LLStringUtil::toUpper(s);
  116. return gAssetDictionary.lookup(s);
  117. }
  118. //static
  119. const std::string& LLAssetType::getDesc(LLAssetType::EType asset_type)
  120. {
  121. const AssetEntry* entry = gAssetDictionary.lookup(asset_type);
  122. if (entry)
  123. {
  124. return entry->mName;
  125. }
  126. return badLookup();
  127. }
  128. //static
  129. const char* LLAssetType::lookup(LLAssetType::EType asset_type)
  130. {
  131. const AssetEntry* entry = gAssetDictionary.lookup(asset_type);
  132. if (entry)
  133. {
  134. return entry->mTypeName;
  135. }
  136. return badLookup().c_str();
  137. }
  138. //static
  139. LLAssetType::EType LLAssetType::lookup(const char* name)
  140. {
  141. return lookup(ll_safe_string(name));
  142. }
  143. //static
  144. LLAssetType::EType LLAssetType::lookup(const std::string& type_name)
  145. {
  146. for (LLAssetDictionary::const_iterator iter = gAssetDictionary.begin(),
  147. end = gAssetDictionary.end();
  148. iter != end; ++iter)
  149. {
  150. const AssetEntry* entry = iter->second;
  151. if (type_name == entry->mTypeName)
  152. {
  153. return iter->first;
  154. }
  155. }
  156. return AT_NONE;
  157. }
  158. //static
  159. const char* LLAssetType::lookupHumanReadable(LLAssetType::EType asset_type)
  160. {
  161. const AssetEntry* entry = gAssetDictionary.lookup(asset_type);
  162. if (entry)
  163. {
  164. return entry->mHumanName;
  165. }
  166. return badLookup().c_str();
  167. }
  168. //static
  169. LLAssetType::EType LLAssetType::lookupHumanReadable(const char* name)
  170. {
  171. return lookupHumanReadable(ll_safe_string(name));
  172. }
  173. //static
  174. LLAssetType::EType LLAssetType::lookupHumanReadable(const std::string& readable_name)
  175. {
  176. for (LLAssetDictionary::const_iterator iter = gAssetDictionary.begin(),
  177. end = gAssetDictionary.end();
  178. iter != end; ++iter)
  179. {
  180. const AssetEntry* entry = iter->second;
  181. if (entry->mHumanName && (readable_name == entry->mHumanName))
  182. {
  183. return iter->first;
  184. }
  185. }
  186. return AT_NONE;
  187. }
  188. //static
  189. bool LLAssetType::lookupCanLink(EType asset_type)
  190. {
  191. const AssetEntry* entry = gAssetDictionary.lookup(asset_type);
  192. return entry && entry->mCanLink;
  193. }
  194. // Not adding this to dictionary since we probably will only have these two types
  195. //static
  196. bool LLAssetType::lookupIsLinkType(EType asset_type)
  197. {
  198. return asset_type == AT_LINK || asset_type == AT_LINK_FOLDER;
  199. }
  200. //static
  201. const std::string& LLAssetType::badLookup()
  202. {
  203. static const std::string sBadLookup = "llassettype_bad_lookup";
  204. return sBadLookup;
  205. }
  206. //static
  207. bool LLAssetType::lookupIsAssetFetchByIDAllowed(EType asset_type)
  208. {
  209. const AssetEntry* entry = gAssetDictionary.lookup(asset_type);
  210. return entry && entry->mCanFetch;
  211. }
  212. //static
  213. bool LLAssetType::lookupIsAssetIDKnowable(EType asset_type)
  214. {
  215. const AssetEntry* entry = gAssetDictionary.lookup(asset_type);
  216. return entry && entry->mCanKnow;
  217. }
  218. //static
  219. EDragAndDropType LLAssetType::lookupDragAndDropType(EType asset_type)
  220. {
  221. const AssetEntry* entry = gAssetDictionary.lookup(asset_type);
  222. return entry ? entry->mDadType : DAD_NONE;
  223. }
  224. // Generate a good default description
  225. //static
  226. void LLAssetType::generateDescriptionFor(EType asset_type, std::string& desc)
  227. {
  228. constexpr S32 BUF_SIZE = 30;
  229. char time_str[BUF_SIZE];
  230. time_t now;
  231. time(&now);
  232. memset(time_str, '\0', BUF_SIZE);
  233. strftime(time_str, BUF_SIZE - 1, "%Y-%m-%d %H:%M:%S ", localtime(&now));
  234. desc.assign(time_str);
  235. desc.append(lookupHumanReadable(asset_type));
  236. }