/indra/newview/llwearable.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 1271 lines · 981 code · 138 blank · 152 comment · 227 complexity · 9daeeaf7ffee67adcb72c1c25507eb4d MD5 · raw file

  1. /**
  2. * @file llwearable.cpp
  3. * @brief LLWearable class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "llagent.h"
  28. #include "llagentcamera.h"
  29. #include "llagentwearables.h"
  30. #include "lldictionary.h"
  31. #include "llfloatersidepanelcontainer.h"
  32. #include "lllocaltextureobject.h"
  33. #include "llnotificationsutil.h"
  34. #include "llviewertexturelist.h"
  35. #include "llinventorymodel.h"
  36. #include "llinventoryobserver.h"
  37. #include "llsidepanelappearance.h"
  38. #include "lltexlayer.h"
  39. #include "lltexglobalcolor.h"
  40. #include "lltrans.h"
  41. #include "llviewerregion.h"
  42. #include "llvisualparam.h"
  43. #include "llvoavatar.h"
  44. #include "llvoavatarself.h"
  45. #include "llvoavatardefines.h"
  46. #include "llwearable.h"
  47. #include "llviewercontrol.h"
  48. using namespace LLVOAvatarDefines;
  49. // static
  50. S32 LLWearable::sCurrentDefinitionVersion = 1;
  51. // support class - remove for 2.1 (hackity hack hack)
  52. class LLOverrideBakedTextureUpdate
  53. {
  54. public:
  55. LLOverrideBakedTextureUpdate(bool temp_state)
  56. {
  57. U32 num_bakes = (U32) LLVOAvatarDefines::BAKED_NUM_INDICES;
  58. for( U32 index = 0; index < num_bakes; ++index )
  59. {
  60. composite_enabled[index] = gAgentAvatarp->isCompositeUpdateEnabled(index);
  61. }
  62. gAgentAvatarp->setCompositeUpdatesEnabled(temp_state);
  63. }
  64. ~LLOverrideBakedTextureUpdate()
  65. {
  66. U32 num_bakes = (U32)LLVOAvatarDefines::BAKED_NUM_INDICES;
  67. for( U32 index = 0; index < num_bakes; ++index )
  68. {
  69. gAgentAvatarp->setCompositeUpdatesEnabled(index, composite_enabled[index]);
  70. }
  71. }
  72. private:
  73. bool composite_enabled[LLVOAvatarDefines::BAKED_NUM_INDICES];
  74. };
  75. // Private local functions
  76. static std::string terse_F32_to_string(F32 f);
  77. static std::string asset_id_to_filename(const LLUUID &asset_id);
  78. LLWearable::LLWearable(const LLTransactionID& transaction_id) :
  79. mDefinitionVersion(LLWearable::sCurrentDefinitionVersion),
  80. mType(LLWearableType::WT_INVALID)
  81. {
  82. mTransactionID = transaction_id;
  83. mAssetID = mTransactionID.makeAssetID(gAgent.getSecureSessionID());
  84. }
  85. LLWearable::LLWearable(const LLAssetID& asset_id) :
  86. mDefinitionVersion( LLWearable::sCurrentDefinitionVersion ),
  87. mType(LLWearableType::WT_INVALID)
  88. {
  89. mAssetID = asset_id;
  90. mTransactionID.setNull();
  91. }
  92. LLWearable::~LLWearable()
  93. {
  94. }
  95. const std::string& LLWearable::getTypeLabel() const
  96. {
  97. return LLWearableType::getTypeLabel(mType);
  98. }
  99. const std::string& LLWearable::getTypeName() const
  100. {
  101. return LLWearableType::getTypeName(mType);
  102. }
  103. LLAssetType::EType LLWearable::getAssetType() const
  104. {
  105. return LLWearableType::getAssetType(mType);
  106. }
  107. BOOL LLWearable::exportFile(LLFILE* file) const
  108. {
  109. // header and version
  110. if( fprintf( file, "LLWearable version %d\n", mDefinitionVersion ) < 0 )
  111. {
  112. return FALSE;
  113. }
  114. // name
  115. if( fprintf( file, "%s\n", mName.c_str() ) < 0 )
  116. {
  117. return FALSE;
  118. }
  119. // description
  120. if( fprintf( file, "%s\n", mDescription.c_str() ) < 0 )
  121. {
  122. return FALSE;
  123. }
  124. // permissions
  125. if( !mPermissions.exportFile( file ) )
  126. {
  127. return FALSE;
  128. }
  129. // sale info
  130. if( !mSaleInfo.exportFile( file ) )
  131. {
  132. return FALSE;
  133. }
  134. // wearable type
  135. S32 type = (S32)mType;
  136. if( fprintf( file, "type %d\n", type ) < 0 )
  137. {
  138. return FALSE;
  139. }
  140. // parameters
  141. S32 num_parameters = mVisualParamIndexMap.size();
  142. if( fprintf( file, "parameters %d\n", num_parameters ) < 0 )
  143. {
  144. return FALSE;
  145. }
  146. for (visual_param_index_map_t::const_iterator iter = mVisualParamIndexMap.begin();
  147. iter != mVisualParamIndexMap.end();
  148. ++iter)
  149. {
  150. S32 param_id = iter->first;
  151. const LLVisualParam* param = iter->second;
  152. F32 param_weight = param->getWeight();
  153. if( fprintf( file, "%d %s\n", param_id, terse_F32_to_string( param_weight ).c_str() ) < 0 )
  154. {
  155. return FALSE;
  156. }
  157. }
  158. // texture entries
  159. S32 num_textures = mTEMap.size();
  160. if( fprintf( file, "textures %d\n", num_textures ) < 0 )
  161. {
  162. return FALSE;
  163. }
  164. for (te_map_t::const_iterator iter = mTEMap.begin(); iter != mTEMap.end(); ++iter)
  165. {
  166. S32 te = iter->first;
  167. const LLUUID& image_id = iter->second->getID();
  168. if( fprintf( file, "%d %s\n", te, image_id.asString().c_str()) < 0 )
  169. {
  170. return FALSE;
  171. }
  172. }
  173. return TRUE;
  174. }
  175. void LLWearable::createVisualParams()
  176. {
  177. for (LLViewerVisualParam* param = (LLViewerVisualParam*) gAgentAvatarp->getFirstVisualParam();
  178. param;
  179. param = (LLViewerVisualParam*) gAgentAvatarp->getNextVisualParam())
  180. {
  181. if (param->getWearableType() == mType)
  182. {
  183. addVisualParam(param->cloneParam(this));
  184. }
  185. }
  186. // resync driver parameters to point to the newly cloned driven parameters
  187. for (visual_param_index_map_t::iterator param_iter = mVisualParamIndexMap.begin();
  188. param_iter != mVisualParamIndexMap.end();
  189. ++param_iter)
  190. {
  191. LLVisualParam* param = param_iter->second;
  192. LLVisualParam*(LLWearable::*wearable_function)(S32)const = &LLWearable::getVisualParam;
  193. // need this line to disambiguate between versions of LLCharacter::getVisualParam()
  194. LLVisualParam*(LLVOAvatarSelf::*avatar_function)(S32)const = &LLVOAvatarSelf::getVisualParam;
  195. param->resetDrivenParams();
  196. if(!param->linkDrivenParams(boost::bind(wearable_function,(LLWearable*)this, _1), false))
  197. {
  198. if( !param->linkDrivenParams(boost::bind(avatar_function,gAgentAvatarp,_1 ), true))
  199. {
  200. llwarns << "could not link driven params for wearable " << getName() << " id: " << param->getID() << llendl;
  201. continue;
  202. }
  203. }
  204. }
  205. }
  206. BOOL LLWearable::importFile( LLFILE* file )
  207. {
  208. // *NOTE: changing the type or size of this buffer will require
  209. // changes in the fscanf() code below. You would be better off
  210. // rewriting this to use streams and not require an open FILE.
  211. char text_buffer[2048]; /* Flawfinder: ignore */
  212. S32 fields_read = 0;
  213. // suppress texlayerset updates while wearables are being imported. Layersets will be updated
  214. // when the wearables are "worn", not loaded. Note state will be restored when this object is destroyed.
  215. LLOverrideBakedTextureUpdate stop_bakes(false);
  216. // read header and version
  217. fields_read = fscanf( file, "LLWearable version %d\n", &mDefinitionVersion );
  218. if( fields_read != 1 )
  219. {
  220. // Shouldn't really log the asset id for security reasons, but
  221. // we need it in this case.
  222. llwarns << "Bad Wearable asset header: " << mAssetID << llendl;
  223. //gVFS->dumpMap();
  224. return FALSE;
  225. }
  226. // Temoprary hack to allow wearables with definition version 24 to still load.
  227. // This should only affect lindens and NDA'd testers who have saved wearables in 2.0
  228. // the extra check for version == 24 can be removed before release, once internal testers
  229. // have loaded these wearables again. See hack pt 2 at bottom of function to ensure that
  230. // these wearables get re-saved with version definition 22.
  231. if( mDefinitionVersion > LLWearable::sCurrentDefinitionVersion && mDefinitionVersion != 24 )
  232. {
  233. llwarns << "Wearable asset has newer version (" << mDefinitionVersion << ") than XML (" << LLWearable::sCurrentDefinitionVersion << ")" << llendl;
  234. return FALSE;
  235. }
  236. // name
  237. int next_char = fgetc( file ); /* Flawfinder: ignore */
  238. if( '\n' == next_char )
  239. {
  240. // no name
  241. mName = "";
  242. }
  243. else
  244. {
  245. ungetc( next_char, file );
  246. fields_read = fscanf( /* Flawfinder: ignore */
  247. file,
  248. "%2047[^\n]",
  249. text_buffer);
  250. if( (1 != fields_read) || (fgetc( file ) != '\n') ) /* Flawfinder: ignore */
  251. {
  252. llwarns << "Bad Wearable asset: early end of file" << llendl;
  253. return FALSE;
  254. }
  255. mName = text_buffer;
  256. LLStringUtil::truncate(mName, DB_INV_ITEM_NAME_STR_LEN );
  257. }
  258. // description
  259. next_char = fgetc( file ); /* Flawfinder: ignore */
  260. if( '\n' == next_char )
  261. {
  262. // no description
  263. mDescription = "";
  264. }
  265. else
  266. {
  267. ungetc( next_char, file );
  268. fields_read = fscanf( /* Flawfinder: ignore */
  269. file,
  270. "%2047[^\n]",
  271. text_buffer );
  272. if( (1 != fields_read) || (fgetc( file ) != '\n') ) /* Flawfinder: ignore */
  273. {
  274. llwarns << "Bad Wearable asset: early end of file" << llendl;
  275. return FALSE;
  276. }
  277. mDescription = text_buffer;
  278. LLStringUtil::truncate(mDescription, DB_INV_ITEM_DESC_STR_LEN );
  279. }
  280. // permissions
  281. S32 perm_version;
  282. fields_read = fscanf( file, " permissions %d\n", &perm_version );
  283. if( (fields_read != 1) || (perm_version != 0) )
  284. {
  285. llwarns << "Bad Wearable asset: missing permissions" << llendl;
  286. return FALSE;
  287. }
  288. if( !mPermissions.importFile( file ) )
  289. {
  290. return FALSE;
  291. }
  292. // sale info
  293. S32 sale_info_version;
  294. fields_read = fscanf( file, " sale_info %d\n", &sale_info_version );
  295. if( (fields_read != 1) || (sale_info_version != 0) )
  296. {
  297. llwarns << "Bad Wearable asset: missing sale_info" << llendl;
  298. return FALSE;
  299. }
  300. // Sale info used to contain next owner perm. It is now in the
  301. // permissions. Thus, we read that out, and fix legacy
  302. // objects. It's possible this op would fail, but it should pick
  303. // up the vast majority of the tasks.
  304. BOOL has_perm_mask = FALSE;
  305. U32 perm_mask = 0;
  306. if( !mSaleInfo.importFile(file, has_perm_mask, perm_mask) )
  307. {
  308. return FALSE;
  309. }
  310. if(has_perm_mask)
  311. {
  312. // fair use fix.
  313. if(!(perm_mask & PERM_COPY))
  314. {
  315. perm_mask |= PERM_TRANSFER;
  316. }
  317. mPermissions.setMaskNext(perm_mask);
  318. }
  319. // wearable type
  320. S32 type = -1;
  321. fields_read = fscanf( file, "type %d\n", &type );
  322. if( fields_read != 1 )
  323. {
  324. llwarns << "Bad Wearable asset: bad type" << llendl;
  325. return FALSE;
  326. }
  327. if( 0 <= type && type < LLWearableType::WT_COUNT )
  328. {
  329. setType((LLWearableType::EType)type);
  330. }
  331. else
  332. {
  333. mType = LLWearableType::WT_COUNT;
  334. llwarns << "Bad Wearable asset: bad type #" << type << llendl;
  335. return FALSE;
  336. }
  337. // parameters header
  338. S32 num_parameters = 0;
  339. fields_read = fscanf( file, "parameters %d\n", &num_parameters );
  340. if( fields_read != 1 )
  341. {
  342. llwarns << "Bad Wearable asset: missing parameters block" << llendl;
  343. return FALSE;
  344. }
  345. if( num_parameters != mVisualParamIndexMap.size() )
  346. {
  347. llwarns << "Wearable parameter mismatch. Reading in " << num_parameters << " from file, but created " << mVisualParamIndexMap.size() << " from avatar parameters. type: " << mType << llendl;
  348. }
  349. // parameters
  350. S32 i;
  351. for( i = 0; i < num_parameters; i++ )
  352. {
  353. S32 param_id = 0;
  354. F32 param_weight = 0.f;
  355. fields_read = fscanf( file, "%d %f\n", &param_id, &param_weight );
  356. if( fields_read != 2 )
  357. {
  358. llwarns << "Bad Wearable asset: bad parameter, #" << i << llendl;
  359. return FALSE;
  360. }
  361. mSavedVisualParamMap[param_id] = param_weight;
  362. }
  363. // textures header
  364. S32 num_textures = 0;
  365. fields_read = fscanf( file, "textures %d\n", &num_textures);
  366. if( fields_read != 1 )
  367. {
  368. llwarns << "Bad Wearable asset: missing textures block" << llendl;
  369. return FALSE;
  370. }
  371. // textures
  372. for( i = 0; i < num_textures; i++ )
  373. {
  374. S32 te = 0;
  375. fields_read = fscanf( /* Flawfinder: ignore */
  376. file,
  377. "%d %2047s\n",
  378. &te, text_buffer);
  379. if( fields_read != 2 )
  380. {
  381. llwarns << "Bad Wearable asset: bad texture, #" << i << llendl;
  382. return FALSE;
  383. }
  384. if( !LLUUID::validate( text_buffer ) )
  385. {
  386. llwarns << "Bad Wearable asset: bad texture uuid: " << text_buffer << llendl;
  387. return FALSE;
  388. }
  389. LLUUID id = LLUUID(text_buffer);
  390. LLViewerFetchedTexture* image = LLViewerTextureManager::getFetchedTexture( id );
  391. if( mTEMap.find(te) != mTEMap.end() )
  392. {
  393. delete mTEMap[te];
  394. }
  395. if( mSavedTEMap.find(te) != mSavedTEMap.end() )
  396. {
  397. delete mSavedTEMap[te];
  398. }
  399. if(gSavedSettings.getBOOL("DebugAvatarLocalTexLoadedTime"))
  400. {
  401. image->setLoadedCallback(LLVOAvatarSelf::debugOnTimingLocalTexLoaded,0,TRUE,FALSE, new LLVOAvatarSelf::LLAvatarTexData(id, (LLVOAvatarDefines::ETextureIndex)te), NULL);
  402. }
  403. LLUUID textureid(text_buffer);
  404. mTEMap[te] = new LLLocalTextureObject(image, textureid);
  405. mSavedTEMap[te] = new LLLocalTextureObject(image, textureid);
  406. createLayers(te);
  407. }
  408. // copy all saved param values to working params
  409. revertValues();
  410. return TRUE;
  411. }
  412. // Avatar parameter and texture definitions can change over time.
  413. // This function returns true if parameters or textures have been added or removed
  414. // since this wearable was created.
  415. BOOL LLWearable::isOldVersion() const
  416. {
  417. if (!isAgentAvatarValid()) return FALSE;
  418. if( LLWearable::sCurrentDefinitionVersion < mDefinitionVersion )
  419. {
  420. llwarns << "Wearable asset has newer version (" << mDefinitionVersion << ") than XML (" << LLWearable::sCurrentDefinitionVersion << ")" << llendl;
  421. llassert(0);
  422. }
  423. if( LLWearable::sCurrentDefinitionVersion != mDefinitionVersion )
  424. {
  425. return TRUE;
  426. }
  427. S32 param_count = 0;
  428. for( LLViewerVisualParam* param = (LLViewerVisualParam*) gAgentAvatarp->getFirstVisualParam();
  429. param;
  430. param = (LLViewerVisualParam*) gAgentAvatarp->getNextVisualParam() )
  431. {
  432. if( (param->getWearableType() == mType) && (param->isTweakable() ) )
  433. {
  434. param_count++;
  435. if( !is_in_map(mVisualParamIndexMap, param->getID() ) )
  436. {
  437. return TRUE;
  438. }
  439. }
  440. }
  441. if( param_count != mVisualParamIndexMap.size() )
  442. {
  443. return TRUE;
  444. }
  445. S32 te_count = 0;
  446. for( S32 te = 0; te < TEX_NUM_INDICES; te++ )
  447. {
  448. if (LLVOAvatarDictionary::getTEWearableType((ETextureIndex) te) == mType)
  449. {
  450. te_count++;
  451. if( !is_in_map(mTEMap, te ) )
  452. {
  453. return TRUE;
  454. }
  455. }
  456. }
  457. if( te_count != mTEMap.size() )
  458. {
  459. return TRUE;
  460. }
  461. return FALSE;
  462. }
  463. // Avatar parameter and texture definitions can change over time.
  464. // * If parameters or textures have been REMOVED since the wearable was created,
  465. // they're just ignored, so we consider the wearable clean even though isOldVersion()
  466. // will return true.
  467. // * If parameters or textures have been ADDED since the wearable was created,
  468. // they are taken to have default values, so we consider the wearable clean
  469. // only if those values are the same as the defaults.
  470. BOOL LLWearable::isDirty() const
  471. {
  472. if (!isAgentAvatarValid()) return FALSE;
  473. for( LLViewerVisualParam* param = (LLViewerVisualParam*) gAgentAvatarp->getFirstVisualParam();
  474. param;
  475. param = (LLViewerVisualParam*) gAgentAvatarp->getNextVisualParam() )
  476. {
  477. if( (param->getWearableType() == mType)
  478. && (param->isTweakable() )
  479. && !param->getCrossWearable())
  480. {
  481. F32 current_weight = getVisualParamWeight(param->getID());
  482. current_weight = llclamp( current_weight, param->getMinWeight(), param->getMaxWeight() );
  483. F32 saved_weight = get_if_there(mSavedVisualParamMap, param->getID(), param->getDefaultWeight());
  484. saved_weight = llclamp( saved_weight, param->getMinWeight(), param->getMaxWeight() );
  485. U8 a = F32_to_U8( saved_weight, param->getMinWeight(), param->getMaxWeight() );
  486. U8 b = F32_to_U8( current_weight, param->getMinWeight(), param->getMaxWeight() );
  487. if( a != b )
  488. {
  489. return TRUE;
  490. }
  491. }
  492. }
  493. for( S32 te = 0; te < TEX_NUM_INDICES; te++ )
  494. {
  495. if (LLVOAvatarDictionary::getTEWearableType((ETextureIndex) te) == mType)
  496. {
  497. te_map_t::const_iterator current_iter = mTEMap.find(te);
  498. if(current_iter != mTEMap.end())
  499. {
  500. const LLUUID& current_image_id = current_iter->second->getID();
  501. te_map_t::const_iterator saved_iter = mSavedTEMap.find(te);
  502. if(saved_iter != mSavedTEMap.end())
  503. {
  504. const LLUUID& saved_image_id = saved_iter->second->getID();
  505. if (saved_image_id != current_image_id)
  506. {
  507. // saved vs current images are different, wearable is dirty
  508. return TRUE;
  509. }
  510. }
  511. else
  512. {
  513. // image found in current image list but not saved image list
  514. return TRUE;
  515. }
  516. }
  517. }
  518. }
  519. return FALSE;
  520. }
  521. void LLWearable::setParamsToDefaults()
  522. {
  523. if (!isAgentAvatarValid()) return;
  524. for( LLVisualParam* param = gAgentAvatarp->getFirstVisualParam(); param; param = gAgentAvatarp->getNextVisualParam() )
  525. {
  526. if( (((LLViewerVisualParam*)param)->getWearableType() == mType ) && (param->isTweakable() ) )
  527. {
  528. setVisualParamWeight(param->getID(),param->getDefaultWeight(), FALSE);
  529. }
  530. }
  531. }
  532. void LLWearable::setTexturesToDefaults()
  533. {
  534. for( S32 te = 0; te < TEX_NUM_INDICES; te++ )
  535. {
  536. if (LLVOAvatarDictionary::getTEWearableType((ETextureIndex) te) == mType)
  537. {
  538. LLUUID id = LLVOAvatarDictionary::getDefaultTextureImageID((ETextureIndex) te);
  539. LLViewerFetchedTexture * image = LLViewerTextureManager::getFetchedTexture( id );
  540. if( mTEMap.find(te) == mTEMap.end() )
  541. {
  542. mTEMap[te] = new LLLocalTextureObject(image, id);
  543. createLayers(te);
  544. }
  545. else
  546. {
  547. // Local Texture Object already created, just set image and UUID
  548. LLLocalTextureObject *lto = mTEMap[te];
  549. lto->setID(id);
  550. lto->setImage(image);
  551. }
  552. }
  553. }
  554. }
  555. // Updates the user's avatar's appearance
  556. void LLWearable::writeToAvatar()
  557. {
  558. if (!isAgentAvatarValid()) return;
  559. ESex old_sex = gAgentAvatarp->getSex();
  560. // Pull params
  561. for( LLVisualParam* param = gAgentAvatarp->getFirstVisualParam(); param; param = gAgentAvatarp->getNextVisualParam() )
  562. {
  563. // cross-wearable parameters are not authoritative, as they are driven by a different wearable. So don't copy the values to the
  564. // avatar object if cross wearable. Cross wearable params get their values from the avatar, they shouldn't write the other way.
  565. if( (((LLViewerVisualParam*)param)->getWearableType() == mType) && (!((LLViewerVisualParam*)param)->getCrossWearable()) )
  566. {
  567. S32 param_id = param->getID();
  568. F32 weight = getVisualParamWeight(param_id);
  569. gAgentAvatarp->setVisualParamWeight( param_id, weight, FALSE );
  570. }
  571. }
  572. // Pull texture entries
  573. for( S32 te = 0; te < TEX_NUM_INDICES; te++ )
  574. {
  575. if (LLVOAvatarDictionary::getTEWearableType((ETextureIndex) te) == mType)
  576. {
  577. te_map_t::const_iterator iter = mTEMap.find(te);
  578. LLUUID image_id;
  579. if(iter != mTEMap.end())
  580. {
  581. image_id = iter->second->getID();
  582. }
  583. else
  584. {
  585. image_id = LLVOAvatarDictionary::getDefaultTextureImageID((ETextureIndex) te);
  586. }
  587. LLViewerTexture* image = LLViewerTextureManager::getFetchedTexture( image_id, TRUE, LLViewerTexture::BOOST_NONE, LLViewerTexture::LOD_TEXTURE );
  588. // MULTI-WEARABLE: assume index 0 will be used when writing to avatar. TODO: eliminate the need for this.
  589. gAgentAvatarp->setLocalTextureTE(te, image, 0);
  590. }
  591. }
  592. ESex new_sex = gAgentAvatarp->getSex();
  593. if( old_sex != new_sex )
  594. {
  595. gAgentAvatarp->updateSexDependentLayerSets( FALSE );
  596. }
  597. // if( upload_bake )
  598. // {
  599. // gAgent.sendAgentSetAppearance();
  600. // }
  601. }
  602. // Updates the user's avatar's appearance, replacing this wearables' parameters and textures with default values.
  603. // static
  604. void LLWearable::removeFromAvatar( LLWearableType::EType type, BOOL upload_bake )
  605. {
  606. if (!isAgentAvatarValid()) return;
  607. // You can't just remove body parts.
  608. if( (type == LLWearableType::WT_SHAPE) ||
  609. (type == LLWearableType::WT_SKIN) ||
  610. (type == LLWearableType::WT_HAIR) ||
  611. (type == LLWearableType::WT_EYES) )
  612. {
  613. return;
  614. }
  615. // Pull params
  616. for( LLVisualParam* param = gAgentAvatarp->getFirstVisualParam(); param; param = gAgentAvatarp->getNextVisualParam() )
  617. {
  618. if( (((LLViewerVisualParam*)param)->getWearableType() == type) && (param->isTweakable() ) )
  619. {
  620. S32 param_id = param->getID();
  621. gAgentAvatarp->setVisualParamWeight( param_id, param->getDefaultWeight(), upload_bake );
  622. }
  623. }
  624. if(gAgentCamera.cameraCustomizeAvatar())
  625. {
  626. LLFloaterSidePanelContainer::showPanel("appearance", LLSD().with("type", "edit_outfit"));
  627. }
  628. gAgentAvatarp->updateVisualParams();
  629. gAgentAvatarp->wearableUpdated(type, FALSE);
  630. // if( upload_bake )
  631. // {
  632. // gAgent.sendAgentSetAppearance();
  633. // }
  634. }
  635. // Does not copy mAssetID.
  636. // Definition version is current: removes obsolete enties and creates default values for new ones.
  637. void LLWearable::copyDataFrom(const LLWearable* src)
  638. {
  639. if (!isAgentAvatarValid()) return;
  640. mDefinitionVersion = LLWearable::sCurrentDefinitionVersion;
  641. mName = src->mName;
  642. mDescription = src->mDescription;
  643. mPermissions = src->mPermissions;
  644. mSaleInfo = src->mSaleInfo;
  645. setType(src->mType);
  646. mSavedVisualParamMap.clear();
  647. // Deep copy of mVisualParamMap (copies only those params that are current, filling in defaults where needed)
  648. for (LLViewerVisualParam* param = (LLViewerVisualParam*) gAgentAvatarp->getFirstVisualParam();
  649. param;
  650. param = (LLViewerVisualParam*) gAgentAvatarp->getNextVisualParam() )
  651. {
  652. if( (param->getWearableType() == mType) )
  653. {
  654. S32 id = param->getID();
  655. F32 weight = src->getVisualParamWeight(id);
  656. mSavedVisualParamMap[id] = weight;
  657. }
  658. }
  659. destroyTextures();
  660. // Deep copy of mTEMap (copies only those tes that are current, filling in defaults where needed)
  661. for (S32 te = 0; te < TEX_NUM_INDICES; te++)
  662. {
  663. if (LLVOAvatarDictionary::getTEWearableType((ETextureIndex) te) == mType)
  664. {
  665. te_map_t::const_iterator iter = src->mTEMap.find(te);
  666. LLUUID image_id;
  667. LLViewerFetchedTexture *image = NULL;
  668. if(iter != src->mTEMap.end())
  669. {
  670. image = src->getLocalTextureObject(te)->getImage();
  671. image_id = src->getLocalTextureObject(te)->getID();
  672. mTEMap[te] = new LLLocalTextureObject(image, image_id);
  673. mSavedTEMap[te] = new LLLocalTextureObject(image, image_id);
  674. mTEMap[te]->setBakedReady(src->getLocalTextureObject(te)->getBakedReady());
  675. mTEMap[te]->setDiscard(src->getLocalTextureObject(te)->getDiscard());
  676. }
  677. else
  678. {
  679. image_id = LLVOAvatarDictionary::getDefaultTextureImageID((ETextureIndex) te);
  680. image = LLViewerTextureManager::getFetchedTexture( image_id );
  681. mTEMap[te] = new LLLocalTextureObject(image, image_id);
  682. mSavedTEMap[te] = new LLLocalTextureObject(image, image_id);
  683. }
  684. createLayers(te);
  685. }
  686. }
  687. // Probably reduntant, but ensure that the newly created wearable is not dirty by setting current value of params in new wearable
  688. // to be the same as the saved values (which were loaded from src at param->cloneParam(this))
  689. revertValues();
  690. }
  691. void LLWearable::setItemID(const LLUUID& item_id)
  692. {
  693. mItemID = item_id;
  694. }
  695. const LLUUID& LLWearable::getItemID() const
  696. {
  697. return mItemID;
  698. }
  699. void LLWearable::setType(LLWearableType::EType type)
  700. {
  701. mType = type;
  702. createVisualParams();
  703. }
  704. LLLocalTextureObject* LLWearable::getLocalTextureObject(S32 index)
  705. {
  706. te_map_t::iterator iter = mTEMap.find(index);
  707. if( iter != mTEMap.end() )
  708. {
  709. LLLocalTextureObject* lto = iter->second;
  710. return lto;
  711. }
  712. return NULL;
  713. }
  714. const LLLocalTextureObject* LLWearable::getLocalTextureObject(S32 index) const
  715. {
  716. te_map_t::const_iterator iter = mTEMap.find(index);
  717. if( iter != mTEMap.end() )
  718. {
  719. const LLLocalTextureObject* lto = iter->second;
  720. return lto;
  721. }
  722. return NULL;
  723. }
  724. void LLWearable::setLocalTextureObject(S32 index, LLLocalTextureObject &lto)
  725. {
  726. if( mTEMap.find(index) != mTEMap.end() )
  727. {
  728. mTEMap.erase(index);
  729. }
  730. mTEMap[index] = new LLLocalTextureObject(lto);
  731. }
  732. void LLWearable::addVisualParam(LLVisualParam *param)
  733. {
  734. if( mVisualParamIndexMap[param->getID()] )
  735. {
  736. delete mVisualParamIndexMap[param->getID()];
  737. }
  738. param->setIsDummy(FALSE);
  739. mVisualParamIndexMap[param->getID()] = param;
  740. mSavedVisualParamMap[param->getID()] = param->getDefaultWeight();
  741. }
  742. void LLWearable::setVisualParams()
  743. {
  744. for (visual_param_index_map_t::const_iterator iter = mVisualParamIndexMap.begin(); iter != mVisualParamIndexMap.end(); iter++)
  745. {
  746. S32 id = iter->first;
  747. LLVisualParam *wearable_param = iter->second;
  748. F32 value = wearable_param->getWeight();
  749. gAgentAvatarp->setVisualParamWeight(id, value, FALSE);
  750. }
  751. }
  752. void LLWearable::setVisualParamWeight(S32 param_index, F32 value, BOOL upload_bake)
  753. {
  754. if( is_in_map(mVisualParamIndexMap, param_index ) )
  755. {
  756. LLVisualParam *wearable_param = mVisualParamIndexMap[param_index];
  757. wearable_param->setWeight(value, upload_bake);
  758. }
  759. else
  760. {
  761. llerrs << "LLWearable::setVisualParam passed invalid parameter index: " << param_index << " for wearable type: " << this->getName() << llendl;
  762. }
  763. }
  764. F32 LLWearable::getVisualParamWeight(S32 param_index) const
  765. {
  766. if( is_in_map(mVisualParamIndexMap, param_index ) )
  767. {
  768. const LLVisualParam *wearable_param = mVisualParamIndexMap.find(param_index)->second;
  769. return wearable_param->getWeight();
  770. }
  771. else
  772. {
  773. llwarns << "LLWerable::getVisualParam passed invalid parameter index: " << param_index << " for wearable type: " << this->getName() << llendl;
  774. }
  775. return (F32)-1.0;
  776. }
  777. LLVisualParam* LLWearable::getVisualParam(S32 index) const
  778. {
  779. visual_param_index_map_t::const_iterator iter = mVisualParamIndexMap.find(index);
  780. return (iter == mVisualParamIndexMap.end()) ? NULL : iter->second;
  781. }
  782. void LLWearable::getVisualParams(visual_param_vec_t &list)
  783. {
  784. visual_param_index_map_t::iterator iter = mVisualParamIndexMap.begin();
  785. visual_param_index_map_t::iterator end = mVisualParamIndexMap.end();
  786. // add all visual params to the passed-in vector
  787. for( ; iter != end; ++iter )
  788. {
  789. list.push_back(iter->second);
  790. }
  791. }
  792. void LLWearable::animateParams(F32 delta, BOOL upload_bake)
  793. {
  794. for(visual_param_index_map_t::iterator iter = mVisualParamIndexMap.begin();
  795. iter != mVisualParamIndexMap.end();
  796. ++iter)
  797. {
  798. LLVisualParam *param = (LLVisualParam*) iter->second;
  799. param->animate(delta, upload_bake);
  800. }
  801. }
  802. LLColor4 LLWearable::getClothesColor(S32 te) const
  803. {
  804. LLColor4 color;
  805. U32 param_name[3];
  806. if( LLVOAvatar::teToColorParams( (LLVOAvatarDefines::ETextureIndex)te, param_name ) )
  807. {
  808. for( U8 index = 0; index < 3; index++ )
  809. {
  810. color.mV[index] = getVisualParamWeight(param_name[index]);
  811. }
  812. }
  813. return color;
  814. }
  815. void LLWearable::setClothesColor( S32 te, const LLColor4& new_color, BOOL upload_bake )
  816. {
  817. U32 param_name[3];
  818. if( LLVOAvatar::teToColorParams( (LLVOAvatarDefines::ETextureIndex)te, param_name ) )
  819. {
  820. for( U8 index = 0; index < 3; index++ )
  821. {
  822. setVisualParamWeight(param_name[index], new_color.mV[index], upload_bake);
  823. }
  824. }
  825. }
  826. void LLWearable::revertValues()
  827. {
  828. //update saved settings so wearable is no longer dirty
  829. // non-driver params first
  830. for (param_map_t::const_iterator iter = mSavedVisualParamMap.begin(); iter != mSavedVisualParamMap.end(); iter++)
  831. {
  832. S32 id = iter->first;
  833. F32 value = iter->second;
  834. LLVisualParam *param = getVisualParam(id);
  835. if(param && !dynamic_cast<LLDriverParam*>(param) )
  836. {
  837. setVisualParamWeight(id, value, TRUE);
  838. }
  839. }
  840. //then driver params
  841. for (param_map_t::const_iterator iter = mSavedVisualParamMap.begin(); iter != mSavedVisualParamMap.end(); iter++)
  842. {
  843. S32 id = iter->first;
  844. F32 value = iter->second;
  845. LLVisualParam *param = getVisualParam(id);
  846. if(param && dynamic_cast<LLDriverParam*>(param) )
  847. {
  848. setVisualParamWeight(id, value, TRUE);
  849. }
  850. }
  851. // make sure that saved values are sane
  852. for (param_map_t::const_iterator iter = mSavedVisualParamMap.begin(); iter != mSavedVisualParamMap.end(); iter++)
  853. {
  854. S32 id = iter->first;
  855. LLVisualParam *param = getVisualParam(id);
  856. if( param )
  857. {
  858. mSavedVisualParamMap[id] = param->getWeight();
  859. }
  860. }
  861. syncImages(mSavedTEMap, mTEMap);
  862. LLSidepanelAppearance *panel = dynamic_cast<LLSidepanelAppearance*>(LLFloaterSidePanelContainer::getPanel("appearance"));
  863. if( panel )
  864. {
  865. panel->updateScrollingPanelList();
  866. }
  867. }
  868. BOOL LLWearable::isOnTop() const
  869. {
  870. return (this == gAgentWearables.getTopWearable(mType));
  871. }
  872. void LLWearable::createLayers(S32 te)
  873. {
  874. LLTexLayerSet *layer_set = gAgentAvatarp->getLayerSet((ETextureIndex)te);
  875. if (layer_set)
  876. {
  877. layer_set->cloneTemplates(mTEMap[te], (ETextureIndex)te, this);
  878. }
  879. else
  880. {
  881. llerrs << "could not find layerset for LTO in wearable!" << llendl;
  882. }
  883. }
  884. void LLWearable::saveValues()
  885. {
  886. //update saved settings so wearable is no longer dirty
  887. mSavedVisualParamMap.clear();
  888. for (visual_param_index_map_t::const_iterator iter = mVisualParamIndexMap.begin(); iter != mVisualParamIndexMap.end(); ++iter)
  889. {
  890. S32 id = iter->first;
  891. LLVisualParam *wearable_param = iter->second;
  892. F32 value = wearable_param->getWeight();
  893. mSavedVisualParamMap[id] = value;
  894. }
  895. // Deep copy of mTEMap (copies only those tes that are current, filling in defaults where needed)
  896. syncImages(mTEMap, mSavedTEMap);
  897. LLSidepanelAppearance *panel = dynamic_cast<LLSidepanelAppearance*>(LLFloaterSidePanelContainer::getPanel("appearance"));
  898. if( panel )
  899. {
  900. panel->updateScrollingPanelList();
  901. }
  902. }
  903. void LLWearable::syncImages(te_map_t &src, te_map_t &dst)
  904. {
  905. // Deep copy of src (copies only those tes that are current, filling in defaults where needed)
  906. for( S32 te = 0; te < TEX_NUM_INDICES; te++ )
  907. {
  908. if (LLVOAvatarDictionary::getTEWearableType((ETextureIndex) te) == mType)
  909. {
  910. te_map_t::const_iterator iter = src.find(te);
  911. LLUUID image_id;
  912. LLViewerFetchedTexture *image = NULL;
  913. LLLocalTextureObject *lto = NULL;
  914. if(iter != src.end())
  915. {
  916. // there's a Local Texture Object in the source image map. Use this to populate the values to store in the destination image map.
  917. lto = iter->second;
  918. image = lto->getImage();
  919. image_id = lto->getID();
  920. }
  921. else
  922. {
  923. // there is no Local Texture Object in the source image map. Get defaults values for populating the destination image map.
  924. image_id = LLVOAvatarDictionary::getDefaultTextureImageID((ETextureIndex) te);
  925. image = LLViewerTextureManager::getFetchedTexture( image_id );
  926. }
  927. if( dst.find(te) != dst.end() )
  928. {
  929. // there's already an entry in the destination map for the texture. Just update its values.
  930. dst[te]->setImage(image);
  931. dst[te]->setID(image_id);
  932. }
  933. else
  934. {
  935. // no entry found in the destination map, we need to create a new Local Texture Object
  936. dst[te] = new LLLocalTextureObject(image, image_id);
  937. }
  938. if( lto )
  939. {
  940. // If we pulled values from a Local Texture Object in the source map, make sure the proper flags are set in the new (or updated) entry in the destination map.
  941. dst[te]->setBakedReady(lto->getBakedReady());
  942. dst[te]->setDiscard(lto->getDiscard());
  943. }
  944. }
  945. }
  946. }
  947. void LLWearable::destroyTextures()
  948. {
  949. for( te_map_t::iterator iter = mTEMap.begin(); iter != mTEMap.end(); ++iter )
  950. {
  951. LLLocalTextureObject *lto = iter->second;
  952. delete lto;
  953. }
  954. mTEMap.clear();
  955. for( te_map_t::iterator iter = mSavedTEMap.begin(); iter != mSavedTEMap.end(); ++iter )
  956. {
  957. LLLocalTextureObject *lto = iter->second;
  958. delete lto;
  959. }
  960. mSavedTEMap.clear();
  961. }
  962. void LLWearable::pullCrossWearableValues()
  963. {
  964. // scan through all of the avatar's visual parameters
  965. for (LLViewerVisualParam* param = (LLViewerVisualParam*) gAgentAvatarp->getFirstVisualParam();
  966. param;
  967. param = (LLViewerVisualParam*) gAgentAvatarp->getNextVisualParam())
  968. {
  969. if( param )
  970. {
  971. LLDriverParam *driver_param = dynamic_cast<LLDriverParam*>(param);
  972. if(driver_param)
  973. {
  974. // parameter is a driver parameter, have it update its
  975. driver_param->updateCrossDrivenParams(getType());
  976. }
  977. }
  978. }
  979. }
  980. void LLWearable::setLabelUpdated() const
  981. {
  982. gInventory.addChangedMask(LLInventoryObserver::LABEL, getItemID());
  983. }
  984. void LLWearable::refreshName()
  985. {
  986. LLUUID item_id = getItemID();
  987. LLInventoryItem* item = gInventory.getItem(item_id);
  988. if( item )
  989. {
  990. mName = item->getName();
  991. }
  992. }
  993. struct LLWearableSaveData
  994. {
  995. LLWearableType::EType mType;
  996. };
  997. void LLWearable::saveNewAsset() const
  998. {
  999. // llinfos << "LLWearable::saveNewAsset() type: " << getTypeName() << llendl;
  1000. //llinfos << *this << llendl;
  1001. const std::string filename = asset_id_to_filename(mAssetID);
  1002. LLFILE* fp = LLFile::fopen(filename, "wb"); /* Flawfinder: ignore */
  1003. BOOL successful_save = FALSE;
  1004. if(fp && exportFile(fp))
  1005. {
  1006. successful_save = TRUE;
  1007. }
  1008. if(fp)
  1009. {
  1010. fclose(fp);
  1011. fp = NULL;
  1012. }
  1013. if(!successful_save)
  1014. {
  1015. std::string buffer = llformat("Unable to save '%s' to wearable file.", mName.c_str());
  1016. llwarns << buffer << llendl;
  1017. LLSD args;
  1018. args["NAME"] = mName;
  1019. LLNotificationsUtil::add("CannotSaveWearableOutOfSpace", args);
  1020. return;
  1021. }
  1022. // save it out to database
  1023. if( gAssetStorage )
  1024. {
  1025. /*
  1026. std::string url = gAgent.getRegion()->getCapability("NewAgentInventory");
  1027. if (!url.empty())
  1028. {
  1029. llinfos << "Update Agent Inventory via capability" << llendl;
  1030. LLSD body;
  1031. body["folder_id"] = gInventory.findCategoryUUIDForType(LLFolderType::assetToFolderType(getAssetType()));
  1032. body["asset_type"] = LLAssetType::lookup(getAssetType());
  1033. body["inventory_type"] = LLInventoryType::lookup(LLInventoryType::IT_WEARABLE);
  1034. body["name"] = getName();
  1035. body["description"] = getDescription();
  1036. LLHTTPClient::post(url, body, new LLNewAgentInventoryResponder(body, filename));
  1037. }
  1038. else
  1039. {
  1040. }
  1041. */
  1042. LLWearableSaveData* data = new LLWearableSaveData;
  1043. data->mType = mType;
  1044. gAssetStorage->storeAssetData(filename, mTransactionID, getAssetType(),
  1045. &LLWearable::onSaveNewAssetComplete,
  1046. (void*)data);
  1047. }
  1048. }
  1049. // static
  1050. void LLWearable::onSaveNewAssetComplete(const LLUUID& new_asset_id, void* userdata, S32 status, LLExtStat ext_status) // StoreAssetData callback (fixed)
  1051. {
  1052. LLWearableSaveData* data = (LLWearableSaveData*)userdata;
  1053. const std::string& type_name = LLWearableType::getTypeName(data->mType);
  1054. if(0 == status)
  1055. {
  1056. // Success
  1057. llinfos << "Saved wearable " << type_name << llendl;
  1058. }
  1059. else
  1060. {
  1061. std::string buffer = llformat("Unable to save %s to central asset store.", type_name.c_str());
  1062. llwarns << buffer << " Status: " << status << llendl;
  1063. LLSD args;
  1064. args["NAME"] = type_name;
  1065. LLNotificationsUtil::add("CannotSaveToAssetStore", args);
  1066. }
  1067. // Delete temp file
  1068. const std::string src_filename = asset_id_to_filename(new_asset_id);
  1069. LLFile::remove(src_filename);
  1070. // delete the context data
  1071. delete data;
  1072. }
  1073. std::ostream& operator<<(std::ostream &s, const LLWearable &w)
  1074. {
  1075. s << "wearable " << LLWearableType::getTypeName(w.mType) << "\n";
  1076. s << " Name: " << w.mName << "\n";
  1077. s << " Desc: " << w.mDescription << "\n";
  1078. //w.mPermissions
  1079. //w.mSaleInfo
  1080. s << " Params:" << "\n";
  1081. for (LLWearable::visual_param_index_map_t::const_iterator iter = w.mVisualParamIndexMap.begin();
  1082. iter != w.mVisualParamIndexMap.end(); ++iter)
  1083. {
  1084. S32 param_id = iter->first;
  1085. LLVisualParam *wearable_param = iter->second;
  1086. F32 param_weight = wearable_param->getWeight();
  1087. s << " " << param_id << " " << param_weight << "\n";
  1088. }
  1089. s << " Textures:" << "\n";
  1090. for (LLWearable::te_map_t::const_iterator iter = w.mTEMap.begin();
  1091. iter != w.mTEMap.end(); ++iter)
  1092. {
  1093. S32 te = iter->first;
  1094. const LLUUID& image_id = iter->second->getID();
  1095. s << " " << te << " " << image_id << "\n";
  1096. }
  1097. return s;
  1098. }
  1099. std::string terse_F32_to_string(F32 f)
  1100. {
  1101. std::string r = llformat("%.2f", f);
  1102. S32 len = r.length();
  1103. // "1.20" -> "1.2"
  1104. // "24.00" -> "24."
  1105. while (len > 0 && ('0' == r[len - 1]))
  1106. {
  1107. r.erase(len-1, 1);
  1108. len--;
  1109. }
  1110. if ('.' == r[len - 1])
  1111. {
  1112. // "24." -> "24"
  1113. r.erase(len-1, 1);
  1114. }
  1115. else if (('-' == r[0]) && ('0' == r[1]))
  1116. {
  1117. // "-0.59" -> "-.59"
  1118. r.erase(1, 1);
  1119. }
  1120. else if ('0' == r[0])
  1121. {
  1122. // "0.59" -> ".59"
  1123. r.erase(0, 1);
  1124. }
  1125. return r;
  1126. }
  1127. std::string asset_id_to_filename(const LLUUID &asset_id)
  1128. {
  1129. std::string asset_id_string;
  1130. asset_id.toString(asset_id_string);
  1131. std::string filename = gDirUtilp->getExpandedFilename(LL_PATH_CACHE,asset_id_string) + ".wbl";
  1132. return filename;
  1133. }