llappearancemgr.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013
  1. /**
  2. * @file llappearancemgr.cpp
  3. * @brief Manager for initiating appearance changes on the viewer
  4. *
  5. * $LicenseInfo:firstyear=2004&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, 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 "llviewerprecompiledheaders.h"
  33. #include "boost/unordered_set.hpp"
  34. #include "llappearancemgr.h"
  35. #include "llcallbacklist.h"
  36. #include "llcorehttputil.h"
  37. #include "lldir.h"
  38. #include "llfoldertype.h"
  39. #include "llnotifications.h"
  40. #include "llsdserialize.h"
  41. #include "llmessage.h"
  42. #include "llagent.h"
  43. #include "llagentwearables.h"
  44. #include "llaisapi.h"
  45. #include "llappviewer.h" // For gFrameTimeSeconds
  46. #include "llcommandhandler.h"
  47. #include "llfloatercustomize.h"
  48. #include "hbfloatermakenewoutfit.h"
  49. #include "llgesturemgr.h"
  50. #include "llgridmanager.h" // For gIsInProductionGrid
  51. #include "llinventorybridge.h"
  52. #include "llinventorymodelfetch.h"
  53. //MK
  54. #include "mkrlinterface.h"
  55. //mk
  56. #include "hbviewerautomation.h"
  57. #include "llviewercontrol.h"
  58. #include "llviewerinventory.h"
  59. #include "llviewerjointattachment.h"
  60. #include "llvoavatarself.h"
  61. #include "llwearablelist.h"
  62. // Global
  63. LLAppearanceMgr gAppearanceMgr;
  64. char ORDER_NUMBER_SEPARATOR('@');
  65. // Forward declarations of local function
  66. bool confirm_replace_attachment_rez(const LLSD& notification,
  67. const LLSD& response);
  68. //-----------------------------------------------------------------------------
  69. // Command handlers
  70. //-----------------------------------------------------------------------------
  71. // Support for secondlife:///app/appearance SLapps
  72. class LLAgentAppearanceHandler final : public LLCommandHandler
  73. {
  74. public:
  75. // Requests are throttled from a non-trusted browser
  76. LLAgentAppearanceHandler()
  77. : LLCommandHandler("appearance", UNTRUSTED_BLOCK)
  78. {
  79. }
  80. bool handle(const LLSD& params, const LLSD&, LLMediaCtrl*) override
  81. {
  82. // Support secondlife:///app/appearance/show, but for now we just
  83. // make all secondlife:///app/appearance SLapps behave this way
  84. if (gAgentWearables.areWearablesLoaded())
  85. {
  86. gAgent.changeCameraToCustomizeAvatar();
  87. }
  88. return true;
  89. }
  90. };
  91. LLAgentAppearanceHandler gAgentAppearanceHandler;
  92. // SLapp for easy-wearing of a stock (library) avatar
  93. class LLWearFolderHandler final : public LLCommandHandler
  94. {
  95. public:
  96. // Not allowed from outside the app
  97. LLWearFolderHandler()
  98. : LLCommandHandler("wear_folder", UNTRUSTED_BLOCK)
  99. {
  100. }
  101. bool handle(const LLSD& tokens, const LLSD& query_map,
  102. LLMediaCtrl*) override
  103. {
  104. LLSD::UUID folder_uuid;
  105. if (query_map.has("folder_id"))
  106. {
  107. folder_uuid = query_map["folder_id"].asUUID();
  108. }
  109. if (folder_uuid.isNull() && query_map.has("folder_name"))
  110. {
  111. std::string folder_name = query_map["folder_name"];
  112. LLInventoryModel::cat_array_t cat_array;
  113. LLInventoryModel::item_array_t item_array;
  114. LLNameCategoryCollector has_name(folder_name);
  115. gInventory.collectDescendentsIf(gInventory.getLibraryRootFolderID(),
  116. cat_array, item_array,
  117. LLInventoryModel::EXCLUDE_TRASH,
  118. has_name);
  119. if (cat_array.size())
  120. {
  121. LLViewerInventoryCategory* cat = cat_array[0];
  122. if (cat)
  123. {
  124. folder_uuid = cat->getUUID();
  125. }
  126. }
  127. }
  128. if (folder_uuid.notNull())
  129. {
  130. LLPointer<LLInventoryCategory> category =
  131. new LLInventoryCategory(folder_uuid, LLUUID::null,
  132. LLFolderType::FT_CLOTHING,
  133. "Quick Appearance");
  134. if (gInventory.getCategory(folder_uuid) != NULL)
  135. {
  136. gAppearanceMgr.wearInventoryCategory(category, true, false);
  137. }
  138. }
  139. #if 0
  140. // release avatar picker keyboard focus
  141. gFocusMgr.setKeyboardFocus(NULL);
  142. #endif
  143. return true;
  144. }
  145. };
  146. LLWearFolderHandler gWearFolderHandler;
  147. struct LLWearInfo
  148. {
  149. LLWearInfo(LLUUID cat_id, bool append, bool replace)
  150. : mCategoryID(cat_id),
  151. mAppend(append),
  152. mReplace(replace)
  153. {
  154. }
  155. LLUUID mCategoryID;
  156. bool mAppend;
  157. bool mReplace;
  158. };
  159. struct LLFoundData
  160. {
  161. LLFoundData(const LLUUID& item_id, const LLUUID& linked_item_id,
  162. const LLUUID& asset_id, const std::string& name,
  163. LLAssetType::EType asset_type)
  164. : mItemID(item_id),
  165. mLinkedItemID(linked_item_id),
  166. mAssetID(asset_id),
  167. mName(name),
  168. mAssetType(asset_type),
  169. mWearable(NULL)
  170. {
  171. }
  172. LLUUID mItemID;
  173. LLUUID mLinkedItemID;
  174. LLUUID mAssetID;
  175. std::string mName;
  176. LLAssetType::EType mAssetType;
  177. LLViewerWearable* mWearable;
  178. };
  179. class LLWearableHoldingPattern
  180. {
  181. public:
  182. LLWearableHoldingPattern(bool append, bool replace)
  183. : mResolved(0),
  184. mAppend(append),
  185. mReplace(replace)
  186. {
  187. }
  188. ~LLWearableHoldingPattern()
  189. {
  190. for_each(mFoundList.begin(), mFoundList.end(), DeletePointer());
  191. mFoundList.clear();
  192. }
  193. typedef std::list<LLFoundData*> found_list_t;
  194. found_list_t mFoundList;
  195. S32 mResolved;
  196. bool mAppend;
  197. bool mReplace;
  198. };
  199. class LLOutfitObserver final : public LLInventoryFetchObserver
  200. {
  201. public:
  202. LLOutfitObserver(const LLUUID& cat_id, bool copy_items, bool append)
  203. : mCatID(cat_id),
  204. mCopyItems(copy_items),
  205. mAppend(append)
  206. {
  207. }
  208. void done() override;
  209. private:
  210. void createCategoryCB(const LLUUID& cat_id);
  211. protected:
  212. LLUUID mCatID;
  213. bool mCopyItems;
  214. bool mAppend;
  215. };
  216. class LLWearInventoryCategoryCallback final : public LLInventoryCallback
  217. {
  218. protected:
  219. LOG_CLASS(LLWearInventoryCategoryCallback);
  220. public:
  221. LLWearInventoryCategoryCallback(const LLUUID& cat_id, bool append)
  222. {
  223. mCatID = cat_id;
  224. mAppend = append;
  225. }
  226. void fire(const LLUUID& item_id) override
  227. {
  228. // Do nothing. We only care about the destructor. The reason for this
  229. // is that this callback is used in a hack where the same callback is
  230. // given to dozens of items, and the destructor is called after the
  231. // last item has fired the event and dereferenced it, if all the events
  232. // actually fire !
  233. }
  234. protected:
  235. ~LLWearInventoryCategoryCallback() override
  236. {
  237. // Is the destructor called by ordinary dereference, or because the
  238. // app's shutting down ? If the inventory callback manager goes away,
  239. // we are shutting down, no longer want the callback.
  240. if (LLInventoryCallbackManager::instanceExists())
  241. {
  242. gAppearanceMgr.wearInventoryCategoryOnAvatar(gInventory.getCategory(mCatID),
  243. mAppend);
  244. }
  245. else
  246. {
  247. llwarns << "Dropping unhandled LLWearInventoryCategoryCallback"
  248. << llendl;
  249. }
  250. }
  251. private:
  252. LLUUID mCatID;
  253. bool mAppend;
  254. };
  255. class LLWearCategoryAfterCopy final : public LLInventoryCallback
  256. {
  257. public:
  258. LLWearCategoryAfterCopy(bool append)
  259. : mAppend(append)
  260. {
  261. }
  262. void fire(const LLUUID& id) override
  263. {
  264. // Wear the inventory category.
  265. LLInventoryCategory* cat = gInventory.getCategory(id);
  266. gAppearanceMgr.wearInventoryCategoryOnAvatar(cat, mAppend);
  267. }
  268. private:
  269. bool mAppend;
  270. };
  271. void LLOutfitObserver::createCategoryCB(const LLUUID& cat_id)
  272. {
  273. if (cat_id.isNull())
  274. {
  275. gNotifications.add("CantCreateRequestedInvFolder");
  276. return;
  277. }
  278. // This is our new category Id.
  279. mCatID = cat_id;
  280. // Copy the items into that new category.
  281. LLPointer<LLInventoryCallback> cb =
  282. new LLWearInventoryCategoryCallback(mCatID, mAppend);
  283. for (U32 i = 0, count = mComplete.size(); i < count; ++i)
  284. {
  285. LLViewerInventoryItem* itemp =
  286. (LLViewerInventoryItem*)gInventory.getItem(mComplete[i]);
  287. if (itemp)
  288. {
  289. copy_inventory_item(itemp->getPermissions().getOwner(),
  290. itemp->getUUID(), mCatID, LLStringUtil::null,
  291. cb);
  292. }
  293. }
  294. gInventory.notifyObservers();
  295. }
  296. // We now have an outfit ready to be copied to agent inventory. Do it, and wear
  297. // that outfit normally.
  298. void LLOutfitObserver::done()
  299. {
  300. if (!mCopyItems)
  301. {
  302. // Nothing to do but wear the inventory category as it is.
  303. gAppearanceMgr.wearInventoryCategoryOnAvatar(gInventory.getCategory(mCatID),
  304. mAppend);
  305. return;
  306. }
  307. // We must copy the items to the agent inventory first; the wearing will
  308. // happen once done, via LLWearInventoryCategoryCallback.
  309. LLInventoryCategory* catp = gInventory.getCategory(mCatID);
  310. std::string name;
  311. if (catp)
  312. {
  313. name = catp->getName();
  314. }
  315. else // This should never happen...
  316. {
  317. name = "New outfit";
  318. }
  319. // We will make a folder in the user-preferred folder, or the Clothing
  320. // folder by default (FT_MY_OUTFITS becomes FT_CLOTHING on purpose in
  321. // findChoosenCategoryUUIDForType() when no user preferred folder is set).
  322. const LLUUID pid =
  323. gInventory.findChoosenCategoryUUIDForType(LLFolderType::FT_MY_OUTFITS);
  324. // Create the category and, on completion, call back our method to copy the
  325. // items into it.
  326. inventory_func_t func = boost::bind(&LLOutfitObserver::createCategoryCB,
  327. this, _1);
  328. gInventory.createNewCategory(pid, LLFolderType::FT_NONE, name, func);
  329. }
  330. class LLOutfitFetch final : public LLInventoryFetchDescendentsObserver
  331. {
  332. protected:
  333. LOG_CLASS(LLOutfitFetch);
  334. public:
  335. LLOutfitFetch(bool copy_items, bool append)
  336. : mCopyItems(copy_items),
  337. mAppend(append)
  338. {
  339. }
  340. void done() override;
  341. protected:
  342. bool mCopyItems;
  343. bool mAppend;
  344. };
  345. void LLOutfitFetch::done()
  346. {
  347. if (mCompleteFolders.empty())
  348. {
  349. llwarns << "Failed to load data. Removing observer." << llendl;
  350. gInventory.removeObserver(this);
  351. delete this;
  352. return;
  353. }
  354. // What we do here is get the complete information on the items in the
  355. // library, and set up an observer that will wait for that to happen.
  356. LLInventoryModel::cat_array_t cat_array;
  357. LLInventoryModel::item_array_t item_array;
  358. gInventory.collectDescendents(mCompleteFolders.front(), cat_array,
  359. item_array, LLInventoryModel::EXCLUDE_TRASH);
  360. S32 count = item_array.size();
  361. if (!count)
  362. {
  363. llwarns << "Nothing fetched in category " << mCompleteFolders.front()
  364. << llendl;
  365. gInventory.removeObserver(this);
  366. delete this;
  367. return;
  368. }
  369. LLOutfitObserver* observerp =
  370. new LLOutfitObserver(mCompleteFolders.front(), mCopyItems, mAppend);
  371. uuid_vec_t ids;
  372. for (S32 i = 0; i < count; ++i)
  373. {
  374. ids.emplace_back(item_array[i]->getUUID());
  375. }
  376. // Clean up, and remove 'this' as an observer now, since the call to the
  377. // LLOutfitObserver::done() will notify observers and would throw us into
  378. // an infinite recursion.
  379. gInventory.removeObserver(this);
  380. delete this;
  381. // Do the fetch
  382. observerp->fetchItems(ids);
  383. if (observerp->isFinished())
  384. {
  385. // Everything is already here: call done.
  386. observerp->done();
  387. }
  388. else
  389. {
  390. // It is all on its way: add an observer, and the inventory will call
  391. // done for us when everything is here.
  392. gInventory.addObserver(observerp);
  393. }
  394. }
  395. //-----------------------------------------------------------------------------
  396. // COF link creation callback
  397. //-----------------------------------------------------------------------------
  398. class LLCreateLinkInCOFCallback final : public LLInventoryCallback
  399. {
  400. protected:
  401. LOG_CLASS(LLCreateLinkInCOFCallback);
  402. public:
  403. LLCreateLinkInCOFCallback(const LLUUID& linked_item_id, bool is_wearable)
  404. : mLinkedItemID(linked_item_id),
  405. mIsWearable(is_wearable)
  406. {
  407. sLinkedItemUUIDs.emplace(linked_item_id);
  408. }
  409. void fire(const LLUUID& item_id) override
  410. {
  411. uuid_list_t::iterator it = sLinkedItemUUIDs.find(mLinkedItemID);
  412. if (it != sLinkedItemUUIDs.end())
  413. {
  414. sLinkedItemUUIDs.erase(it);
  415. LL_DEBUGS("COF") << (sLinkedItemUUIDs.empty() ? "Links creation finished in COF"
  416. : "One more link created in COF")
  417. << LL_ENDL;
  418. }
  419. else
  420. {
  421. // Stale/late link created in COF: force a resync
  422. llwarns << "Stale/late link creation in COF, flagging the latter for resync..."
  423. << llendl;
  424. sLinkedItemUUIDs.clear();
  425. if (mIsWearable)
  426. {
  427. gAppearanceMgr.mNeedsSyncWearables = true;
  428. }
  429. else
  430. {
  431. gAppearanceMgr.mNeedsSyncAttachments = true;
  432. }
  433. }
  434. gAppearanceMgr.resetCOFUpdateTimer();
  435. }
  436. static void clearLinksList() { sLinkedItemUUIDs.clear(); }
  437. static bool isLinksListEmpty() { return sLinkedItemUUIDs.empty(); }
  438. private:
  439. LLUUID mLinkedItemID;
  440. bool mIsWearable;
  441. static uuid_list_t sLinkedItemUUIDs;
  442. };
  443. uuid_list_t LLCreateLinkInCOFCallback::sLinkedItemUUIDs;
  444. //-----------------------------------------------------------------------------
  445. // COF slamming callback
  446. //-----------------------------------------------------------------------------
  447. class LLSlamCOFCallback final : public LLInventoryCallback
  448. {
  449. protected:
  450. LOG_CLASS(LLSlamCOFCallback);
  451. public:
  452. LLSlamCOFCallback()
  453. {
  454. ++sPendingCallbacks;
  455. }
  456. ~LLSlamCOFCallback() override
  457. {
  458. --sPendingCallbacks;
  459. }
  460. void fire(const LLUUID&) override
  461. {
  462. }
  463. static bool pendingCallbacks() { return sPendingCallbacks > 0; }
  464. static void resetPendingCallbacks() { sPendingCallbacks = 0; }
  465. private:
  466. static S32 sPendingCallbacks;
  467. };
  468. S32 LLSlamCOFCallback::sPendingCallbacks = 0;
  469. //-----------------------------------------------------------------------------
  470. // Misc callbacks
  471. //-----------------------------------------------------------------------------
  472. //static
  473. U32 LLWearOnAvatarCallback::sCurrentCounterGeneration = 0;
  474. U32 LLWearOnAvatarCallback::sPendingCallbackCount = 0;
  475. void LLWearOnAvatarCallback::fire(const LLUUID& inv_item)
  476. {
  477. if (inv_item.notNull())
  478. {
  479. LLViewerInventoryItem* item = gInventory.getItem(inv_item);
  480. if (item)
  481. {
  482. gAppearanceMgr.wearItemOnAvatar(item->getLinkedUUID(), mReplace);
  483. }
  484. // *TODO: track callbacks by (original) item UUID
  485. if (mCounterGeneration == sCurrentCounterGeneration)
  486. {
  487. if (sPendingCallbackCount > 0)
  488. {
  489. --sPendingCallbackCount;
  490. gAppearanceMgr.resetCOFUpdateTimer();
  491. }
  492. else
  493. {
  494. llwarns << "Spurious callback firing detected !" << llendl;
  495. }
  496. }
  497. else if (item)
  498. {
  499. bool is_object = item->getType() == LLAssetType::AT_OBJECT;
  500. llwarns << "Stale callback triggered for "
  501. << (is_object ? "attachment: " : "wearable: ") << inv_item
  502. << ". Flagging COF for resync." << llendl;
  503. if (is_object)
  504. {
  505. gAppearanceMgr.mNeedsSyncAttachments = true;
  506. }
  507. else
  508. {
  509. gAppearanceMgr.mNeedsSyncWearables = true;
  510. }
  511. gAppearanceMgr.resetCOFUpdateTimer();
  512. }
  513. }
  514. }
  515. //static
  516. U32 LLRezAttachmentCallback::sCurrentCounterGeneration = 0;
  517. U32 LLRezAttachmentCallback::sPendingCallbackCount = 0;
  518. void LLRezAttachmentCallback::fire(const LLUUID& inv_item)
  519. {
  520. if (inv_item.notNull())
  521. {
  522. LLViewerInventoryItem* item = gInventory.getItem(inv_item);
  523. if (item)
  524. {
  525. gAppearanceMgr.rezAttachment(item, mAttach, mReplace);
  526. }
  527. // *TODO: track callbacks by (original) item UUID
  528. if (mCounterGeneration == sCurrentCounterGeneration)
  529. {
  530. if (sPendingCallbackCount > 0)
  531. {
  532. --sPendingCallbackCount;
  533. gAppearanceMgr.resetCOFUpdateTimer();
  534. }
  535. else
  536. {
  537. llwarns << "Spurious callback firing detected !" << llendl;
  538. }
  539. }
  540. else if (item)
  541. {
  542. llwarns << "Stale callback triggered for attachment " << inv_item
  543. << ". Flagging COF for resync." << llendl;
  544. gAppearanceMgr.mNeedsSyncAttachments = true;
  545. gAppearanceMgr.resetCOFUpdateTimer();
  546. }
  547. }
  548. }
  549. //-----------------------------------------------------------------------------
  550. //static
  551. bool LLAppearanceMgr::onSetWearableDialog(const LLSD& notification,
  552. const LLSD& response,
  553. LLViewerWearable* old_wearable)
  554. {
  555. if (!old_wearable)
  556. {
  557. llwarns << "Callback called for a NULL old wearable !" << llendl;
  558. return false;
  559. }
  560. const LLUUID& item_id = notification["payload"]["item_id"].asUUID();
  561. LLViewerInventoryItem* item_to_wear = gInventory.getItem(item_id);
  562. if (!item_to_wear)
  563. {
  564. llwarns << "Callback called for a NULL new item !" << llendl;
  565. return false;
  566. }
  567. U32 index;
  568. if (!gAgentWearables.getWearableIndex(old_wearable, index))
  569. {
  570. llwarns << "Wearable not found" << llendl;
  571. return false;
  572. }
  573. S32 option = LLNotification::getSelectedOption(notification, response);
  574. switch (option)
  575. {
  576. case 0: // "Save"
  577. gAgentWearables.saveWearable(old_wearable->getType(), index);
  578. case 1: // "Don't Save"
  579. gAppearanceMgr.wearInventoryItemOnAvatar(item_to_wear, true);
  580. case 2: // "Cancel"
  581. break;
  582. default:
  583. llassert(false);
  584. break;
  585. }
  586. return false;
  587. }
  588. // *NOTE: hack to get from avatar inventory to avatar
  589. void LLAppearanceMgr::wearInventoryItemOnAvatar(LLInventoryItem* item,
  590. bool replace)
  591. {
  592. if (item && isAgentAvatarValid())
  593. {
  594. LL_DEBUGS("Appearance") << "wearInventoryItemOnAvatar("
  595. << item->getName() << ")" << LL_ENDL;
  596. LLWearableList::getInstance()->getAsset(item->getAssetUUID(),
  597. item->getName(),
  598. gAgentAvatarp,
  599. item->getType(),
  600. LLWearableBridge::onWearOnAvatarArrived,
  601. new OnWearStruct(item->getLinkedUUID(),
  602. replace));
  603. }
  604. }
  605. // User-requested action
  606. bool LLAppearanceMgr::wearItemOnAvatar(const LLUUID& item_id_to_wear,
  607. bool replace)
  608. {
  609. if (item_id_to_wear.isNull()) return false;
  610. LLViewerInventoryItem* item_to_wear = gInventory.getItem(item_id_to_wear);
  611. if (!item_to_wear) return false;
  612. if (gInventory.isObjectDescendentOf(item_to_wear->getUUID(),
  613. gInventory.getLibraryRootFolderID()))
  614. {
  615. LLPointer<LLInventoryCallback> cb = new LLWearOnAvatarCallback(replace);
  616. copy_inventory_item(item_to_wear->getPermissions().getOwner(),
  617. item_to_wear->getUUID(), LLUUID::null,
  618. std::string(), cb);
  619. return false;
  620. }
  621. else if (!gInventory.isObjectDescendentOf(item_to_wear->getUUID(),
  622. gInventory.getRootFolderID()))
  623. {
  624. return false; // not in library and not in agent's inventory
  625. }
  626. else if (gInventory.isInTrash(item_to_wear->getUUID()))
  627. {
  628. gNotifications.add("CannotWearTrash");
  629. return false;
  630. }
  631. const LLAssetType::EType asset_type = item_to_wear->getType();
  632. //MK
  633. if (gRLenabled &&
  634. // Deal with wearables only, here, since attachments are taken care of
  635. // in rezAttachment()
  636. (asset_type == LLAssetType::AT_CLOTHING ||
  637. asset_type == LLAssetType::AT_BODYPART))
  638. {
  639. const LLWearableType::EType type = item_to_wear->getWearableType();
  640. if (!gRLInterface.canWear(item_to_wear) ||
  641. gRLInterface.contains("addoutfit") ||
  642. gRLInterface.contains("addoutfit:" +
  643. gRLInterface.getOutfitLayerAsString(type)))
  644. {
  645. return false;
  646. }
  647. if (replace)
  648. {
  649. // Check to see if we are already wearing a wearable of this type
  650. // and if yes, if we can remove it...
  651. if (gAgentWearables.getViewerWearable(type, 0) &&
  652. !gRLInterface.canUnwear(type))
  653. {
  654. // Cannot remove this wearable type, so cannot replace it
  655. // either
  656. return false;
  657. }
  658. }
  659. }
  660. //mk
  661. switch (asset_type)
  662. {
  663. case LLAssetType::AT_CLOTHING:
  664. {
  665. if (gAgentWearables.areWearablesLoaded())
  666. {
  667. const LLWearableType::EType type = item_to_wear->getWearableType();
  668. // See if we want to avoid wearing multiple wearables that
  669. // don't really make any sense or for which the resulting
  670. // combination is hard for the user to predict and/or notice.
  671. // E.g. for Physics, only the last worn item is taken into
  672. // account, so there's no use wearing more than one...
  673. if ((type == LLWearableType::WT_PHYSICS &&
  674. gSavedSettings.getBool("NoMultiplePhysics")) ||
  675. (type == LLWearableType::WT_SHOES &&
  676. gSavedSettings.getBool("NoMultipleShoes")) ||
  677. (type == LLWearableType::WT_SKIRT &&
  678. gSavedSettings.getBool("NoMultipleSkirts")))
  679. {
  680. replace = true;
  681. }
  682. if (replace && gAgentWearables.getWearableCount(type))
  683. {
  684. gAgentWearables.userRemoveWearablesOfType(type);
  685. }
  686. if (!gAgentWearables.canAddWearable(type))
  687. {
  688. return false;
  689. }
  690. if (replace)
  691. {
  692. LLViewerWearable* old_wearable;
  693. // MULTI_WEARABLE: hardwired to 0
  694. old_wearable = gAgentWearables.getViewerWearable(type, 0);
  695. if (old_wearable && old_wearable->isDirty())
  696. {
  697. // Bring up modal dialog: Save changes ? Yes/No/Cancel
  698. LLSD payload;
  699. payload["item_id"] = item_id_to_wear;
  700. gNotifications.add("WearableSave", LLSD(), payload,
  701. boost::bind(onSetWearableDialog,
  702. _1, _2, old_wearable));
  703. return false;
  704. }
  705. }
  706. wearInventoryItemOnAvatar(item_to_wear, replace);
  707. }
  708. break;
  709. }
  710. case LLAssetType::AT_BODYPART:
  711. {
  712. if (gAgentWearables.areWearablesLoaded())
  713. {
  714. const LLWearableType::EType type = item_to_wear->getWearableType();
  715. LLViewerWearable* old_wearable;
  716. old_wearable = gAgentWearables.getViewerWearable(type, 0);
  717. if (old_wearable && old_wearable->isDirty())
  718. {
  719. // Bring up modal dialog: Save changes ? Yes/No/Cancel
  720. LLSD payload;
  721. payload["item_id"] = item_id_to_wear;
  722. gNotifications.add("WearableSave", LLSD(), payload,
  723. boost::bind(onSetWearableDialog, _1, _2,
  724. old_wearable));
  725. return false;
  726. }
  727. wearInventoryItemOnAvatar(item_to_wear, true);
  728. }
  729. break;
  730. }
  731. case LLAssetType::AT_OBJECT:
  732. {
  733. rezAttachment(item_to_wear, NULL, replace);
  734. break;
  735. }
  736. default:
  737. {
  738. // Nothing to do...
  739. }
  740. }
  741. return false;
  742. }
  743. #if 0 // Not used for now
  744. // *TODO: investigate wearables may not be loaded at this point EXT-8231
  745. bool LLAppearanceMgr::canAddWearables(const uuid_vec_t& item_ids)
  746. {
  747. if (!isAgentAvatarValid())
  748. {
  749. return false;
  750. }
  751. U32 n_objects = 0;
  752. U32 n_clothes = 0;
  753. // Count given clothes (by wearable type) and objects.
  754. for (S32 i = 0, count = item_ids.size(); i < count; ++i)
  755. {
  756. LLViewerInventoryItem* item = gInventory.getItem(item_ids[i]);
  757. if (!item)
  758. {
  759. return false;
  760. }
  761. LLAssetType::EType type = item->getType();
  762. if (type == LLAssetType::AT_OBJECT)
  763. {
  764. ++n_objects;
  765. }
  766. else if (type == LLAssetType::AT_CLOTHING)
  767. {
  768. ++n_clothes;
  769. }
  770. else
  771. {
  772. llwarns << "Unexpected wearable type: " << (S32)type << llendl;
  773. return false;
  774. }
  775. }
  776. // Check whether we can add all the objects.
  777. if (!gAgentAvatarp->canAttachMoreObjects(n_objects))
  778. {
  779. return false;
  780. }
  781. // Check whether we can add all the clothes.
  782. U32 sum_clothes = n_clothes + gAgentWearables.getClothingLayerCount();
  783. return sum_clothes <= LLAgentWearables::MAX_CLOTHING_LAYERS;
  784. }
  785. #endif
  786. void LLAppearanceMgr::getDescendentsOfAssetType(const LLUUID& category,
  787. LLInventoryModel::item_array_t& items,
  788. LLAssetType::EType type)
  789. {
  790. LLInventoryModel::cat_array_t cats;
  791. LLIsType is_of_type(type);
  792. gInventory.collectDescendentsIf(category, cats, items,
  793. LLInventoryModel::EXCLUDE_TRASH,
  794. is_of_type);
  795. }
  796. void LLAppearanceMgr::getDescendentsOfWearableTypes(const LLUUID& category,
  797. LLInventoryModel::item_array_t& items)
  798. {
  799. LLInventoryModel::cat_array_t cats;
  800. LLFindWearables is_wearable;
  801. gInventory.collectDescendentsIf(category, cats, items,
  802. LLInventoryModel::EXCLUDE_TRASH,
  803. is_wearable);
  804. }
  805. void LLAppearanceMgr::getUserDescendents(const LLUUID& category,
  806. LLInventoryModel::item_array_t& wear_items,
  807. LLInventoryModel::item_array_t& obj_items,
  808. LLInventoryModel::item_array_t& gest_items)
  809. {
  810. LLInventoryModel::cat_array_t wear_cats;
  811. LLFindWearables is_wearable;
  812. gInventory.collectDescendentsIf(category, wear_cats, wear_items,
  813. LLInventoryModel::EXCLUDE_TRASH,
  814. is_wearable);
  815. LLInventoryModel::cat_array_t obj_cats;
  816. LLIsType is_object(LLAssetType::AT_OBJECT);
  817. gInventory.collectDescendentsIf(category, obj_cats, obj_items,
  818. LLInventoryModel::EXCLUDE_TRASH,
  819. is_object);
  820. // Find all gestures in this folder
  821. LLInventoryModel::cat_array_t gest_cats;
  822. LLIsType is_gesture(LLAssetType::AT_GESTURE);
  823. gInventory.collectDescendentsIf(category, gest_cats, gest_items,
  824. LLInventoryModel::EXCLUDE_TRASH,
  825. is_gesture);
  826. }
  827. void LLAppearanceMgr::wearInventoryCategory(LLInventoryCategory* category,
  828. bool copy, bool append)
  829. {
  830. if (!category) return;
  831. LL_DEBUGS("Appearance") << "wearInventoryCategory(" << category->getName()
  832. << ")" << LL_ENDL;
  833. // If we are copying from library, use AIS to copy the category.
  834. if (copy && AISAPI::isAvailable())
  835. {
  836. // We will make a folder in the user-preferred folder, or the Clothing
  837. // folder by default (FT_MY_OUTFITS becomes FT_CLOTHING on purpose in
  838. // findChoosenCategoryUUIDForType() when no user preferred folder is
  839. // set).
  840. LLUUID parent_id =
  841. gInventory.findChoosenCategoryUUIDForType(LLFolderType::FT_MY_OUTFITS);
  842. if (parent_id.isNull())
  843. {
  844. parent_id = gInventory.getRootFolderID();
  845. }
  846. LLPointer<LLInventoryCallback> copy_cb =
  847. new LLWearCategoryAfterCopy(append);
  848. AISAPI::completion_t cr = boost::bind(&doInventoryCb, copy_cb, _1);
  849. AISAPI::copyLibraryCategory(category->getUUID(), parent_id, false, cr);
  850. }
  851. else
  852. {
  853. // What we do here is get the complete information on the items in
  854. // the inventory, and set up an observer that will wait for that to
  855. // happen.
  856. LLOutfitFetch* outfit = new LLOutfitFetch(copy, append);
  857. uuid_vec_t folders;
  858. folders.emplace_back(category->getUUID());
  859. outfit->fetchDescendents(folders);
  860. if (outfit->isFinished())
  861. {
  862. // Everything is already here; call done.
  863. outfit->done();
  864. }
  865. else
  866. {
  867. // It is all on its way: add an observer, and the inventory will
  868. // call done for us when everything is here.
  869. gInventory.addObserver(outfit);
  870. }
  871. }
  872. }
  873. void LLAppearanceMgr::wearInventoryCategoryOnAvatar(LLInventoryCategory* category,
  874. bool append, bool replace)
  875. {
  876. // Avoid unintentionally overwriting old wearables. We have to do this up
  877. // front to avoid having to deal with the case of multiple wearables being
  878. // dirty.
  879. if (!category) return;
  880. LL_DEBUGS("Appearance") << "wear_inventory_category_on_avatar("
  881. << category->getName() << ")" << LL_ENDL;
  882. LLWearInfo* info = new LLWearInfo(category->getUUID(), append, replace);
  883. if (gFloaterCustomizep)
  884. {
  885. gFloaterCustomizep->askToSaveIfDirty(wearInventoryCategoryOnAvatarStep2,
  886. info);
  887. }
  888. else
  889. {
  890. wearInventoryCategoryOnAvatarStep2(true, info);
  891. }
  892. }
  893. //static
  894. void LLAppearanceMgr::wearInventoryCategoryOnAvatarStep2(bool proceed,
  895. void* userdata)
  896. {
  897. if (!gAgent.getRegion() || !isAgentAvatarValid()) return;
  898. LLWearInfo* wear_info = (LLWearInfo*)userdata;
  899. if (!wear_info) return;
  900. // Find all the wearables that are in the category's subtree.
  901. LL_DEBUGS("Appearance") << "wearInventoryCategoryOnAvatarStep2()"
  902. << LL_ENDL;
  903. if (proceed)
  904. {
  905. //MK
  906. bool old_restore = gRLInterface.mRestoringOutfit;
  907. gRLInterface.mRestoringOutfit =
  908. gAppearanceMgr.isRestoringInitialOutfit();
  909. //mk
  910. LLUUID cat_id = wear_info->mCategoryID;
  911. LLViewerInventoryItem* item;
  912. S32 i;
  913. #if 0
  914. // Checking and updating links' descriptions of wearables
  915. gAppearanceMgr.updateClothingOrderingInfo(cat_id);
  916. #endif
  917. // Find all the wearables that are in the category's subtree.
  918. LLInventoryModel::item_array_t wear_items;
  919. LLInventoryModel::item_array_t obj_items;
  920. LLInventoryModel::item_array_t gest_items;
  921. gAppearanceMgr.getUserDescendents(cat_id, wear_items, obj_items,
  922. gest_items);
  923. S32 wearable_count = wear_items.size();
  924. S32 obj_count = obj_items.size();
  925. S32 gest_count = gest_items.size();
  926. if (!wearable_count && !obj_count && !gest_count)
  927. {
  928. gNotifications.add("CouldNotPutOnOutfit");
  929. delete wear_info;
  930. return;
  931. }
  932. // Activate all gestures in this folder
  933. if (gest_count > 0)
  934. {
  935. llinfos << "Activating " << gest_count << " gestures" << llendl;
  936. gGestureManager.activateGestures(gest_items);
  937. // Update the inventory item labels to reflect the fact
  938. // they are active.
  939. LLViewerInventoryCategory* catp =
  940. gInventory.getCategory(wear_info->mCategoryID);
  941. if (catp)
  942. {
  943. gInventory.updateCategory(catp);
  944. gInventory.notifyObservers();
  945. }
  946. }
  947. if (wearable_count > 0)
  948. {
  949. // Preparing the list of wearables in the correct order for
  950. // LLAgentWearables
  951. sortItemsByActualDescription(wear_items);
  952. // Note: cannot do normal iteration, because if all the wearables
  953. // can be resolved immediately, then the callback will be called
  954. // (and this object deleted) before the final getNextData().
  955. LLWearableHoldingPattern* holder =
  956. new LLWearableHoldingPattern(wear_info->mAppend,
  957. wear_info->mReplace);
  958. LLFoundData* found;
  959. std::vector<LLFoundData*> found_container;
  960. for (i = 0; i < wearable_count; ++i)
  961. {
  962. item = wear_items[i];
  963. found = new LLFoundData(item->getUUID(),
  964. item->getLinkedUUID(),
  965. item->getAssetUUID(),
  966. item->getName(),
  967. item->getType());
  968. // Pushing back, not front, to preserve order of wearables for
  969. // LLAgentWearables
  970. holder->mFoundList.push_back(found);
  971. found_container.push_back(found);
  972. }
  973. LLWearableList* wl = LLWearableList::getInstance();
  974. for (i = 0; i < wearable_count; ++i)
  975. {
  976. found = found_container[i];
  977. wl->getAsset(found->mAssetID, found->mName, gAgentAvatarp,
  978. found->mAssetType, onWearableAssetFetch,
  979. (void*)holder);
  980. }
  981. }
  982. // If not appending and the folder does not contain only gestures, take
  983. // off attachments that we do not need to keep.
  984. if (!wear_info->mAppend &&
  985. !(wearable_count == 0 && obj_count == 0 && gest_count > 0))
  986. {
  987. if (obj_count > 0)
  988. {
  989. // Build a list of the attachments we want to wear
  990. uuid_list_t keep_these;
  991. for (i = 0; i < obj_count; ++i)
  992. {
  993. keep_these.emplace(obj_items[i]->getLinkedUUID());
  994. }
  995. // Remove all worn attachments not in our keep_these list, and
  996. // remove from the latter the UUIDs of already worn attachments
  997. detachExtraAttachments(keep_these, true);
  998. // Check that all attachments we want to wear are in keep_these
  999. // and remove them if they are not (since already worn).
  1000. LLInventoryModel::item_array_t::iterator it = obj_items.begin();
  1001. while (it != obj_items.end())
  1002. {
  1003. if (!keep_these.count((*it)->getLinkedUUID()))
  1004. {
  1005. it = obj_items.erase(it);
  1006. --obj_count;
  1007. }
  1008. else
  1009. {
  1010. ++it;
  1011. }
  1012. }
  1013. }
  1014. else
  1015. {
  1016. // Take off all worn attachments
  1017. LLAgentWearables::userRemoveAllAttachments();
  1018. }
  1019. }
  1020. if (obj_count > 0 && isAgentAvatarValid())
  1021. {
  1022. // We have found some attachements. Add these and build a compound
  1023. // message to send all the objects that need to be rezzed.
  1024. // Limit number of packets to send
  1025. constexpr S32 MAX_PACKETS_TO_SEND = 10;
  1026. constexpr S32 OBJECTS_PER_PACKET = 4;
  1027. constexpr S32 MAX_OBJECTS_TO_SEND = MAX_PACKETS_TO_SEND *
  1028. OBJECTS_PER_PACKET;
  1029. if (obj_count > MAX_OBJECTS_TO_SEND)
  1030. {
  1031. obj_count = MAX_OBJECTS_TO_SEND;
  1032. }
  1033. // Create an Id to keep the parts of the compound message together
  1034. LLUUID compound_msg_id;
  1035. compound_msg_id.generate();
  1036. LLMessageSystem* msg = gMessageSystemp;
  1037. U8 add_flag = wear_info->mReplace ? 0 : ATTACHMENT_ADD;
  1038. for (i = 0; i < obj_count; ++i)
  1039. {
  1040. if (i % OBJECTS_PER_PACKET == 0)
  1041. {
  1042. // Start a new message chunk
  1043. msg->newMessageFast(_PREHASH_RezMultipleAttachmentsFromInv);
  1044. msg->nextBlockFast(_PREHASH_AgentData);
  1045. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  1046. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  1047. msg->nextBlockFast(_PREHASH_HeaderData);
  1048. msg->addUUIDFast(_PREHASH_CompoundMsgID, compound_msg_id);
  1049. msg->addU8Fast(_PREHASH_TotalObjects, obj_count);
  1050. msg->addBoolFast(_PREHASH_FirstDetachAll, false);
  1051. }
  1052. item = obj_items[i];
  1053. msg->nextBlockFast(_PREHASH_ObjectData);
  1054. msg->addUUIDFast(_PREHASH_ItemID, item->getLinkedUUID());
  1055. msg->addUUIDFast(_PREHASH_OwnerID,
  1056. item->getPermissions().getOwner());
  1057. // Wear at the previous or default attachment point
  1058. msg->addU8Fast(_PREHASH_AttachmentPt, add_flag);
  1059. pack_permissions_slam(msg, item->getFlags(),
  1060. item->getPermissions());
  1061. msg->addStringFast(_PREHASH_Name, item->getName());
  1062. msg->addStringFast(_PREHASH_Description,
  1063. item->getDescription());
  1064. if (obj_count == i + 1 ||
  1065. OBJECTS_PER_PACKET - 1 == i % OBJECTS_PER_PACKET)
  1066. {
  1067. // End of message chunk
  1068. msg->sendReliable(gAgent.getRegionHost());
  1069. }
  1070. }
  1071. }
  1072. //MK
  1073. gRLInterface.mRestoringOutfit = old_restore;
  1074. //mk
  1075. }
  1076. delete wear_info;
  1077. wear_info = NULL;
  1078. }
  1079. //static
  1080. void LLAppearanceMgr::onWearableAssetFetch(LLViewerWearable* wearable,
  1081. void* userdata)
  1082. {
  1083. LLWearableHoldingPattern* holder = (LLWearableHoldingPattern*)userdata;
  1084. if (wearable)
  1085. {
  1086. for (LLWearableHoldingPattern::found_list_t::iterator
  1087. iter = holder->mFoundList.begin(),
  1088. end = holder->mFoundList.end();
  1089. iter != end; ++iter)
  1090. {
  1091. LLFoundData* data = *iter;
  1092. if (wearable->getAssetID() == data->mAssetID)
  1093. {
  1094. data->mWearable = wearable;
  1095. break;
  1096. }
  1097. }
  1098. }
  1099. if (++holder->mResolved >= (S32)holder->mFoundList.size())
  1100. {
  1101. wearInventoryCategoryOnAvatarStep3(holder);
  1102. }
  1103. }
  1104. //static
  1105. void LLAppearanceMgr::wearInventoryCategoryOnAvatarStep3(LLWearableHoldingPattern* holder)
  1106. {
  1107. LL_DEBUGS("Appearance") << "wearInventoryCategoryOnAvatarStep3()"
  1108. << LL_ENDL;
  1109. //MK
  1110. bool old_restore = gRLInterface.mRestoringOutfit;
  1111. gRLInterface.mRestoringOutfit = gAppearanceMgr.isRestoringInitialOutfit();
  1112. //mk
  1113. LLInventoryItem::item_array_t items;
  1114. std::vector<LLViewerWearable*> wearables;
  1115. for (S32 i = 0; i < LLWearableType::WT_COUNT; ++i)
  1116. {
  1117. bool remove_old = false;
  1118. for (LLWearableHoldingPattern::found_list_t::iterator
  1119. iter = holder->mFoundList.begin(),
  1120. end = holder->mFoundList.end();
  1121. iter != end; ++iter)
  1122. {
  1123. LLFoundData* data = *iter;
  1124. LLViewerWearable* wearable = data->mWearable;
  1125. if (wearable && (S32)wearable->getType() == i)
  1126. {
  1127. LLViewerInventoryItem* item =
  1128. (LLViewerInventoryItem*)gInventory.getItem(data->mLinkedItemID);
  1129. if (item && item->getAssetUUID() == wearable->getAssetID())
  1130. {
  1131. items.push_back(item);
  1132. wearables.push_back(wearable);
  1133. if (holder->mReplace &&
  1134. wearable->getAssetType() == LLAssetType::AT_CLOTHING)
  1135. {
  1136. remove_old = true;
  1137. }
  1138. }
  1139. }
  1140. }
  1141. if (remove_old)
  1142. {
  1143. gAgentWearables.removeWearable((LLWearableType::EType)i, true, 0);
  1144. }
  1145. }
  1146. if (wearables.size() > 0)
  1147. {
  1148. gAgentWearables.setWearableOutfit(items, wearables, !holder->mAppend);
  1149. //gInventory.notifyObservers();
  1150. }
  1151. //MK
  1152. gRLInterface.mRestoringOutfit = old_restore;
  1153. //mk
  1154. delete holder;
  1155. }
  1156. void LLAppearanceMgr::wearOutfitByName(const std::string& name)
  1157. {
  1158. llinfos << "Wearing category " << name << llendl;
  1159. LLInventoryModel::cat_array_t cat_array;
  1160. LLInventoryModel::item_array_t item_array;
  1161. LLNameCategoryCollector has_name(name);
  1162. gInventory.collectDescendentsIf(gInventory.getRootFolderID(),
  1163. cat_array,
  1164. item_array,
  1165. LLInventoryModel::EXCLUDE_TRASH,
  1166. has_name);
  1167. bool copy_items = false;
  1168. LLInventoryCategory* cat = NULL;
  1169. if (cat_array.size() > 0)
  1170. {
  1171. // Just wear the first one that matches
  1172. cat = cat_array[0];
  1173. }
  1174. else
  1175. {
  1176. gInventory.collectDescendentsIf(LLUUID::null,
  1177. cat_array,
  1178. item_array,
  1179. LLInventoryModel::EXCLUDE_TRASH,
  1180. has_name);
  1181. if (cat_array.size() > 0)
  1182. {
  1183. cat = cat_array[0];
  1184. copy_items = true;
  1185. }
  1186. }
  1187. if (cat)
  1188. {
  1189. wearInventoryCategory(cat, copy_items, false);
  1190. }
  1191. else
  1192. {
  1193. llwarns << "Couldn't find outfit " << name
  1194. << " in wearOutfitByName()" << llendl;
  1195. }
  1196. }
  1197. std::string build_order_string(LLWearableType::EType type, U32 i)
  1198. {
  1199. std::ostringstream order_num;
  1200. order_num << ORDER_NUMBER_SEPARATOR << type * 100 + i;
  1201. return order_num.str();
  1202. }
  1203. // NOTE: despite the name, this is not the same function as in v2/3 viewers:
  1204. // this function is used to update the description of the inventory links
  1205. // corresponding to a worn clothing item in a folder (category), according
  1206. // to their current layer index.
  1207. void LLAppearanceMgr::updateClothingOrderingInfo(LLUUID cat_id)
  1208. {
  1209. if (cat_id.isNull())
  1210. {
  1211. return;
  1212. }
  1213. LLInventoryModel::item_array_t wear_items;
  1214. getDescendentsOfAssetType(cat_id, wear_items, LLAssetType::AT_CLOTHING);
  1215. if (!wear_items.size())
  1216. {
  1217. return;
  1218. }
  1219. bool inventory_changed = false;
  1220. LLViewerWearable* wearable;
  1221. for (S32 i = 0, count = wear_items.size(); i < count; ++i)
  1222. {
  1223. LLViewerInventoryItem* item = wear_items[i];
  1224. if (!item)
  1225. {
  1226. llwarns << "NULL item found" << llendl;
  1227. continue;
  1228. }
  1229. // Ignore non-links and non-worn link wearables.
  1230. if (!item->getIsLinkType() ||
  1231. !gAgentWearables.isWearingItem(item->getUUID()))
  1232. {
  1233. continue;
  1234. }
  1235. LLWearableType::EType type = item->getWearableType();
  1236. if (type < 0 || type >= LLWearableType::WT_COUNT)
  1237. {
  1238. llwarns << "Invalid wearable type. Inventory type does not match wearable flag bitfield."
  1239. << llendl;
  1240. continue;
  1241. }
  1242. wearable = gAgentWearables.getWearableFromItemID(item->getUUID());
  1243. U32 index;
  1244. if (!gAgentWearables.getWearableIndex(wearable, index))
  1245. {
  1246. llwarns << "Cannot find wearable index for item: "
  1247. << item->getName() << llendl;
  1248. continue;
  1249. }
  1250. std::string new_order_str = build_order_string(type, index);
  1251. std::string old_desc = item->getActualDescription();
  1252. if (new_order_str == old_desc) continue;
  1253. LL_DEBUGS("Appearance") << "Changing the description for link item '"
  1254. << item->getName() << "' from '" << old_desc
  1255. << "' to '" << new_order_str << "'" << LL_ENDL;
  1256. item->setDescription(new_order_str);
  1257. item->setComplete(true);
  1258. LLSD updates;
  1259. updates["desc"] = new_order_str;
  1260. update_inventory_item(item->getUUID(), updates);
  1261. inventory_changed = true;
  1262. }
  1263. // *TODO: do we really need to notify observers ?
  1264. if (inventory_changed)
  1265. {
  1266. gInventory.notifyObservers();
  1267. }
  1268. }
  1269. // A predicate for sorting inventory items by actual descriptions
  1270. bool sort_by_description(const LLInventoryItem* item1,
  1271. const LLInventoryItem* item2)
  1272. {
  1273. if (!item1 || !item2)
  1274. {
  1275. llwarns << "sort_by_description(): Either item1 or item2 is NULL"
  1276. << llendl;
  1277. return true;
  1278. }
  1279. return item1->getActualDescription() < item2->getActualDescription();
  1280. }
  1281. //static
  1282. void LLAppearanceMgr::sortItemsByActualDescription(LLInventoryModel::item_array_t& items)
  1283. {
  1284. if (items.size() < 2) return;
  1285. std::sort(items.begin(), items.end(), sort_by_description);
  1286. }
  1287. void LLAppearanceMgr::removeInventoryCategoryFromAvatar(LLInventoryCategory* category)
  1288. {
  1289. if (!category) return;
  1290. LL_DEBUGS("Appearance") << "removeInventoryCategoryFromAvatar("
  1291. << category->getName() << ")" << LL_ENDL;
  1292. LLUUID* uuid = new LLUUID(category->getUUID());
  1293. if (gFloaterCustomizep)
  1294. {
  1295. gFloaterCustomizep->askToSaveIfDirty(removeInventoryCategoryFromAvatarStep2,
  1296. uuid);
  1297. }
  1298. else
  1299. {
  1300. removeInventoryCategoryFromAvatarStep2(true, uuid);
  1301. }
  1302. }
  1303. //static
  1304. void LLAppearanceMgr::removeInventoryCategoryFromAvatarStep2(bool proceed,
  1305. void* userdata)
  1306. {
  1307. LLUUID* category_id = (LLUUID*)userdata;
  1308. LLViewerInventoryItem* item;
  1309. S32 i;
  1310. LL_DEBUGS("Appearance") << "removeInventoryCategoryFromAvatarStep2()"
  1311. << LL_ENDL;
  1312. if (proceed && isAgentAvatarValid())
  1313. {
  1314. // Find all the wearables that are in the category's subtree.
  1315. LLInventoryModel::item_array_t wear_items;
  1316. LLInventoryModel::item_array_t obj_items;
  1317. LLInventoryModel::item_array_t gest_items;
  1318. gAppearanceMgr.getUserDescendents(*category_id, wear_items, obj_items,
  1319. gest_items);
  1320. S32 wearable_count = wear_items.size();
  1321. S32 obj_count = obj_items.size();
  1322. S32 gest_count = gest_items.size();
  1323. if (wearable_count > 0)
  1324. {
  1325. // Loop through wearables. If worn, remove.
  1326. LLWearableList* wl = LLWearableList::getInstance();
  1327. for (i = 0; i < wearable_count; ++i)
  1328. {
  1329. item = wear_items[i];
  1330. if (gAgentWearables.isWearingItem(item->getUUID()))
  1331. {
  1332. wl->getAsset(item->getAssetUUID(), item->getName(),
  1333. gAgentAvatarp, item->getType(),
  1334. LLWearableBridge::onRemoveFromAvatarArrived,
  1335. new OnRemoveStruct(item->getLinkedUUID()));
  1336. }
  1337. }
  1338. }
  1339. if (obj_count > 0)
  1340. {
  1341. for (i = 0; i < obj_count; ++i)
  1342. {
  1343. item = obj_items[i];
  1344. //MK
  1345. if (!gRLenabled || gRLInterface.canDetach(item))
  1346. {
  1347. //mk
  1348. LLVOAvatarSelf::detachAttachmentIntoInventory(item->getLinkedUUID());
  1349. //MK
  1350. }
  1351. //mk
  1352. }
  1353. }
  1354. if (gest_count > 0)
  1355. {
  1356. for (i = 0; i < gest_count; ++i)
  1357. {
  1358. item = gest_items[i];
  1359. if (gGestureManager.isGestureActive(item->getUUID()))
  1360. {
  1361. gGestureManager.deactivateGesture(item->getUUID());
  1362. gInventory.updateItem(item);
  1363. gInventory.notifyObservers();
  1364. }
  1365. }
  1366. }
  1367. }
  1368. delete category_id;
  1369. category_id = NULL;
  1370. }
  1371. void LLAppearanceMgr::rezAttachment(LLViewerInventoryItem* item,
  1372. LLViewerJointAttachment* attachment,
  1373. bool replace)
  1374. {
  1375. if (!isAgentAvatarValid() || !item) return;
  1376. LLSD payload;
  1377. // Wear the base object in case this is a link.
  1378. payload["item_id"] = item->getLinkedUUID();
  1379. S32 attach_pt = 0;
  1380. if (attachment)
  1381. {
  1382. for (LLVOAvatar::attachment_map_t::iterator
  1383. iter = gAgentAvatarp->mAttachmentPoints.begin(),
  1384. end = gAgentAvatarp->mAttachmentPoints.end();
  1385. iter != end; ++iter)
  1386. {
  1387. if (iter->second == attachment)
  1388. {
  1389. attach_pt = iter->first;
  1390. break;
  1391. }
  1392. }
  1393. }
  1394. if (!replace)
  1395. {
  1396. attach_pt |= ATTACHMENT_ADD;
  1397. }
  1398. payload["attachment_point"] = attach_pt;
  1399. if (attachment)
  1400. {
  1401. payload["attachment_name"] = attachment->getName();
  1402. }
  1403. //MK
  1404. bool old_restore = gRLInterface.mRestoringOutfit;
  1405. gRLInterface.mRestoringOutfit = gAppearanceMgr.isRestoringInitialOutfit();
  1406. //mk
  1407. if (replace && attachment && attachment->getNumObjects() > 0)
  1408. {
  1409. //MK
  1410. if (!gRLenabled ||
  1411. (gRLInterface.canAttach(item) &&
  1412. gRLInterface.canDetach(attachment->getName())))
  1413. //mk
  1414. {
  1415. gNotifications.add("ReplaceAttachment", LLSD(), payload,
  1416. confirm_replace_attachment_rez);
  1417. }
  1418. }
  1419. else
  1420. {
  1421. //MK
  1422. if (!gRLenabled || gRLInterface.canAttach(item))
  1423. //mk
  1424. {
  1425. gNotifications.forceResponse(LLNotification::Params("ReplaceAttachment").payload(payload),
  1426. 0/*YES*/);
  1427. }
  1428. }
  1429. //MK
  1430. gRLInterface.mRestoringOutfit = old_restore;
  1431. //mk
  1432. }
  1433. bool confirm_replace_attachment_rez(const LLSD& notification,
  1434. const LLSD& response)
  1435. {
  1436. if (!gAgent.getRegion()) return false;
  1437. if (!gAgentAvatarp->canAttachMoreObjects())
  1438. {
  1439. // Avoid piling such notifications... Especially since they are modal
  1440. // ones !
  1441. static LLUUID max_attx_notif_id;
  1442. if (max_attx_notif_id.notNull())
  1443. {
  1444. if (gNotifications.find(max_attx_notif_id))
  1445. {
  1446. return false;
  1447. }
  1448. max_attx_notif_id.setNull();
  1449. }
  1450. LLSD args;
  1451. args["MAX_ATTACHMENTS"] = llformat("%d", gMaxSelfAttachments);
  1452. LLNotificationPtr n = gNotifications.add("MaxAttachmentsOnOutfit",
  1453. args);
  1454. if (n)
  1455. {
  1456. max_attx_notif_id = n->getID();
  1457. }
  1458. return false;
  1459. }
  1460. if (LLNotification::getSelectedOption(notification, response) == 0) // YES
  1461. {
  1462. S32 attach_pt = notification["payload"]["attachment_point"].asInteger();
  1463. LLViewerInventoryItem* itemp =
  1464. gInventory.getItem(notification["payload"]["item_id"].asUUID());
  1465. if (itemp && itemp->getLinkedItem())
  1466. {
  1467. itemp = itemp->getLinkedItem();
  1468. }
  1469. if (itemp)
  1470. {
  1471. LLMessageSystem* msg = gMessageSystemp;
  1472. msg->newMessageFast(_PREHASH_RezSingleAttachmentFromInv);
  1473. msg->nextBlockFast(_PREHASH_AgentData);
  1474. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  1475. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  1476. msg->nextBlockFast(_PREHASH_ObjectData);
  1477. msg->addUUIDFast(_PREHASH_ItemID, itemp->getUUID());
  1478. msg->addUUIDFast(_PREHASH_OwnerID,
  1479. itemp->getPermissions().getOwner());
  1480. msg->addU8Fast(_PREHASH_AttachmentPt, attach_pt);
  1481. pack_permissions_slam(msg, itemp->getFlags(),
  1482. itemp->getPermissions());
  1483. msg->addStringFast(_PREHASH_Name, itemp->getName());
  1484. msg->addStringFast(_PREHASH_Description, itemp->getDescription());
  1485. msg->sendReliable(gAgent.getRegionHost());
  1486. }
  1487. }
  1488. return false;
  1489. }
  1490. static LLNotificationFunctorRegistration confirm_replace_attachment_rez_reg("ReplaceAttachment",
  1491. confirm_replace_attachment_rez);
  1492. std::string get_outfit_filename()
  1493. {
  1494. std::string filename = gIsInProductionGrid ? "outfit.xml"
  1495. : "outfit_beta.xml";
  1496. return gDirUtil.getFullPath(LL_PATH_PER_ACCOUNT, filename);
  1497. }
  1498. void LLAppearanceMgr::checkOutfit()
  1499. {
  1500. if (!gAgent.regionCapabilitiesReceived())
  1501. {
  1502. // Wait until the capababilities have been received before dealing with
  1503. // COF resyncs and rebakes...
  1504. return;
  1505. }
  1506. static LLCachedControl<bool> restore_outfit_from_cof(gSavedSettings,
  1507. "RestoreOutfitFromCOF");
  1508. bool restore_from_cof = restore_outfit_from_cof;
  1509. bool must_sync_cof = true;
  1510. if (!gIsInSecondLife)
  1511. {
  1512. // In OpenSim grids, do not sync with the COF unless we chose to do so
  1513. // (OSUseCOF setting is true), or the grid supports SSB (unlikely, but
  1514. // let's be future-proof...).
  1515. static LLCachedControl<bool> os_use_cof(gSavedSettings, "OSUseCOF");
  1516. if (!os_use_cof)
  1517. {
  1518. restore_from_cof = false;
  1519. }
  1520. bool can_do_ssb = LLVOAvatarSelf::canUseServerBaking();
  1521. if (!can_do_ssb)
  1522. {
  1523. setRebaking(false);
  1524. }
  1525. must_sync_cof = os_use_cof || can_do_ssb;
  1526. }
  1527. static LLCachedControl<U32> outfit_restore_min_delay(gSavedSettings,
  1528. "OutfitRestorationMinDelay");
  1529. static LLCachedControl<U32> outfit_restore_max_delay(gSavedSettings,
  1530. "OutfitRestorationMaxDelay");
  1531. if (gAgentWearables.areWearablesLoaded() &&
  1532. gAgentAvatarp->mPendingAttachment.size() == 0)
  1533. {
  1534. if (mIsRestoringInitialOutfit)
  1535. {
  1536. F32 min_delay = (F32)outfit_restore_min_delay;
  1537. if (min_delay < 5.f)
  1538. {
  1539. min_delay = 5.f;
  1540. }
  1541. F32 max_delay = (F32)outfit_restore_max_delay;
  1542. if (max_delay < min_delay + 5.f)
  1543. {
  1544. max_delay = min_delay + 5.f;
  1545. }
  1546. bool loading =
  1547. !LLInventoryModelFetch::getInstance()->isEverythingFetched();
  1548. if (loading)
  1549. {
  1550. // Let's at least wait till the inventory is fully loaded...
  1551. max_delay += gAttachmentsTimer.getElapsedTimeF32();
  1552. }
  1553. if (gAttachmentsTimer.getElapsedTimeF32() < min_delay)
  1554. {
  1555. // Must be reset each time the timer is reset
  1556. mRestorationRetryDelayDelta = 0.f;
  1557. }
  1558. if (gAttachmentsTimer.getElapsedTimeF32() >
  1559. min_delay + mRestorationRetryDelayDelta)
  1560. {
  1561. //MK
  1562. gRLInterface.mRestoringOutfit = true;
  1563. //mk
  1564. bool can_retry =
  1565. gAttachmentsTimer.getElapsedTimeF32() < max_delay;
  1566. ERestoreOutfitStatus status;
  1567. if (restore_from_cof)
  1568. {
  1569. status = restoreOutfitFromCOF(can_retry);
  1570. }
  1571. else
  1572. {
  1573. status = restoreOutfit(can_retry);
  1574. }
  1575. //MK
  1576. gRLInterface.mRestoringOutfit = false;
  1577. //mk
  1578. mIsRestoringInitialOutfit = status == RETRY;
  1579. if (mIsRestoringInitialOutfit)
  1580. {
  1581. mRestorationRetryDelayDelta =
  1582. gAttachmentsTimer.getElapsedTimeF32() - min_delay + 5.f;
  1583. LL_DEBUGS("InitialOutfit") << "Will retry outfit restoration in 5 seconds..."
  1584. << LL_ENDL;
  1585. if (loading && !mOutfitRestorationRetried)
  1586. {
  1587. mOutfitRestorationRetried = true;
  1588. LLNotificationPtr n =
  1589. gNotifications.add("OutfitRestorationDelayed");
  1590. if (n)
  1591. {
  1592. mLoadingNotificationID = n->getID();
  1593. }
  1594. }
  1595. }
  1596. else if (status == DONE || status == INCOMPLETE)
  1597. {
  1598. if (!restore_from_cof)
  1599. {
  1600. // Remove any worn item that is not part of the saved
  1601. // outfit (may happen if coming from a v2/3 viewer for
  1602. // example).
  1603. //MK
  1604. gRLInterface.mRestoringOutfit = true;
  1605. //mk
  1606. removeNonMatchingItems();
  1607. //MK
  1608. gRLInterface.mRestoringOutfit = false;
  1609. //mk
  1610. }
  1611. // Force a saving of the outfit on next run
  1612. gAgentWearables.setWearablesLoaded();
  1613. gAttachmentsListDirty = gWearablesListDirty = true;
  1614. // Dirty attachments spatial groups to avoid missing prims.
  1615. gAgentAvatarp->refreshAttachments();
  1616. // This will force a server-side rebake on next run
  1617. gAgentAvatarp->mLastUpdateRequestCOFVersion =
  1618. LLViewerInventoryCategory::VERSION_UNKNOWN;
  1619. // Make sure we take our Z offset into account
  1620. gAgentAvatarp->scheduleHoverUpdate();
  1621. // Notify the Make New Outfit floater, if opened
  1622. HBFloaterMakeNewOutfit::setDirty();
  1623. // Cancel the "OutfitRestorationDelayed" notification, if
  1624. // any exists and is still active.
  1625. if (mLoadingNotificationID.notNull())
  1626. {
  1627. LLNotificationPtr n =
  1628. gNotifications.find(mLoadingNotificationID);
  1629. if (n)
  1630. {
  1631. gNotifications.cancel(n);
  1632. }
  1633. mLoadingNotificationID.setNull();
  1634. }
  1635. llinfos << "Outfit restoration completed." << llendl;
  1636. std::string msg =
  1637. status == DONE ? "OutfitRestorationCompleted"
  1638. : "OutfitRestorationPartial";
  1639. gNotifications.add(msg);
  1640. }
  1641. else
  1642. {
  1643. // Dirty attachments spatial groups to avoid missing prims.
  1644. gAgentAvatarp->refreshAttachments();
  1645. // Cancel the "OutfitRestorationDelayed" notification, if
  1646. // any and still active.
  1647. if (mLoadingNotificationID.notNull())
  1648. {
  1649. LLNotificationPtr n =
  1650. gNotifications.find(mLoadingNotificationID);
  1651. if (n)
  1652. {
  1653. gNotifications.cancel(n);
  1654. }
  1655. mLoadingNotificationID.setNull();
  1656. }
  1657. llwarns << "Outfit restoration failed !" << llendl;
  1658. gNotifications.add("OutfitRestorationFailed");
  1659. }
  1660. }
  1661. }
  1662. else if (gAttachmentsListDirty || gWearablesListDirty)
  1663. {
  1664. saveOutfit();
  1665. }
  1666. }
  1667. else
  1668. {
  1669. gAttachmentsListDirty = gWearablesListDirty = true;
  1670. gAttachmentsTimer.reset();
  1671. }
  1672. if (!must_sync_cof)
  1673. {
  1674. // OpenSim: No link support, or user chose not to use COF.
  1675. LLFolderType::setCanDeleteCOF(true);
  1676. return;
  1677. }
  1678. else
  1679. {
  1680. LLFolderType::setCanDeleteCOF(false);
  1681. }
  1682. static LLCachedControl<F32> cof_delay(gSavedSettings,
  1683. "SyncCOFUpdateDelay");
  1684. if (mIsRestoringInitialOutfit ||
  1685. mUpdateCOFTimer.getElapsedTimeF32() <=
  1686. llclamp((F32)cof_delay, 1.f, 3.f))
  1687. {
  1688. return;
  1689. }
  1690. if (mNeedsSyncAttachments || mNeedsSyncWearables)
  1691. {
  1692. bool cof_complete = gInventory.isCategoryComplete(getCOF(true)) &&
  1693. LLCreateLinkInCOFCallback::isLinksListEmpty() &&
  1694. !LLSlamCOFCallback::pendingCallbacks() &&
  1695. !LLWearOnAvatarCallback::pendingCallbacks() &&
  1696. !LLRezAttachmentCallback::pendingCallbacks() &&
  1697. !LLWearableSaveData::pendingSavedWearables();
  1698. static LLCachedControl<U32> sync_cof_timeout(gSavedSettings,
  1699. "SyncCOFTimeout");
  1700. if (!cof_complete &&
  1701. mUpdateCOFTimer.getElapsedTimeF32() > llmax((F32)sync_cof_timeout,
  1702. 5.f * (F32)cof_delay))
  1703. {
  1704. llwarns << "Timeout waiting for COF update, forcing an update."
  1705. << llendl;
  1706. LLCreateLinkInCOFCallback::clearLinksList();
  1707. LLSlamCOFCallback::resetPendingCallbacks();
  1708. LLWearOnAvatarCallback::resetPendingCallbacks();
  1709. LLRezAttachmentCallback::resetPendingCallbacks();
  1710. LLWearableSaveData::resetSavedWearableCount();
  1711. cof_complete = true;
  1712. }
  1713. if (cof_complete)
  1714. {
  1715. if (AISAPI::isAvailable() && !mBakeRequestSent &&
  1716. !mForceServerSideRebake)
  1717. {
  1718. LL_DEBUGS("COF") << "COF is complete, resyncing using AIS slam..."
  1719. << LL_ENDL;
  1720. slamCOF();
  1721. }
  1722. else
  1723. {
  1724. LL_DEBUGS("COF") << "COF is complete." << LL_ENDL;
  1725. if (mNeedsSyncAttachments)
  1726. {
  1727. LL_DEBUGS("COF") << "Resyncing attachments..." << LL_ENDL;
  1728. syncAttachmentLinksInCOF();
  1729. }
  1730. if (mNeedsSyncWearables)
  1731. {
  1732. LL_DEBUGS("COF") << "Resyncing wearables..." << LL_ENDL;
  1733. syncWearableLinksInCOF();
  1734. }
  1735. }
  1736. }
  1737. else
  1738. {
  1739. LL_DEBUGS("COF") << "COF is not yet complete, delaying resync..."
  1740. << LL_ENDL;
  1741. }
  1742. }
  1743. else if (LLVOAvatarSelf::canUseServerBaking() && !mBakeRequestSent &&
  1744. gAgentAvatarp->mLastUpdateRequestCOFVersion != getCOFVersion())
  1745. {
  1746. LL_DEBUGS("COF") << "COF now updated, requesting a server-side rebake..."
  1747. << LL_ENDL;
  1748. requestServerAppearanceUpdate();
  1749. }
  1750. }
  1751. //static
  1752. void LLAppearanceMgr::detachExtraAttachments(uuid_list_t& keep_these,
  1753. bool erase_worn)
  1754. {
  1755. if (!isAgentAvatarValid()) return;
  1756. LLAgentWearables::llvo_vec_t objects_to_detach;
  1757. for (S32 i = 0, count = gAgentAvatarp->mAttachedObjectsVector.size();
  1758. i < count; ++i)
  1759. {
  1760. LLViewerObject* object =
  1761. gAgentAvatarp->mAttachedObjectsVector[i].first;
  1762. if (object)
  1763. {
  1764. LLUUID id = object->getAttachmentItemID();
  1765. // Check that it is not a temporary attachment: it is not one if it
  1766. // is in our inventory (item != NULL)...
  1767. LLViewerInventoryItem* item = gInventory.getItem(id);
  1768. if (!item) continue;
  1769. if (keep_these.count(id) == 0)
  1770. {
  1771. objects_to_detach.push_back(object);
  1772. LL_DEBUGS("InitialOutfit") << "Attachment: " << id
  1773. << " not in outfit, marking for detach."
  1774. << LL_ENDL;
  1775. }
  1776. else if (erase_worn)
  1777. {
  1778. keep_these.erase(id);
  1779. }
  1780. }
  1781. }
  1782. // Take off the attachments that will no longer be in the outfit.
  1783. if (!objects_to_detach.empty())
  1784. {
  1785. LL_DEBUGS("InitialOutfit") << "Removing extra attachments" << LL_ENDL;
  1786. gAgentWearables.userRemoveMultipleAttachments(objects_to_detach);
  1787. }
  1788. }
  1789. void LLAppearanceMgr::removeNonMatchingItems()
  1790. {
  1791. // Open the outfit.xml file for reading
  1792. std::string filename = get_outfit_filename();
  1793. LLSD list;
  1794. llifstream llsd_xml(filename.c_str(), std::ios::in | std::ios::binary);
  1795. if (!llsd_xml.is_open())
  1796. {
  1797. LL_DEBUGS("InitialOutfit") << "No outfit.xml file found, or file not readable"
  1798. << LL_ENDL;
  1799. return;
  1800. }
  1801. LL_DEBUGS("InitialOutfit") << "Checking currently worn items against saved outfit list..."
  1802. << LL_ENDL;
  1803. // Create the list of expected items for our final outfit
  1804. uuid_list_t outfit;
  1805. LLSDSerialize::fromXML(list, llsd_xml);
  1806. for (LLSD::map_iterator iter = list.beginMap(), end = list.endMap();
  1807. iter != end; ++iter)
  1808. {
  1809. LLSD::String key_name = iter->first;
  1810. LLSD array = iter->second;
  1811. if (key_name == "attachments" && array.isArray())
  1812. {
  1813. for (S32 i = 0, count = array.size(); i < count; ++i)
  1814. {
  1815. LLSD map = array[i];
  1816. if (map.has("inv_item_id"))
  1817. {
  1818. LLUUID item_id = map.get("inv_item_id");
  1819. outfit.emplace(item_id);
  1820. LL_DEBUGS("InitialOutfit") << "Expected attachment: "
  1821. << item_id << LL_ENDL;
  1822. }
  1823. }
  1824. }
  1825. else if (key_name == "wearables" && array.isArray())
  1826. {
  1827. for (S32 i = 0, count = array.size(); i < count; ++i)
  1828. {
  1829. LLSD map = array[i];
  1830. if (map.has("inv_item_id"))
  1831. {
  1832. LLUUID item_id = map.get("inv_item_id");
  1833. outfit.emplace(item_id);
  1834. LL_DEBUGS("InitialOutfit") << "Expected wearable: "
  1835. << item_id << LL_ENDL;
  1836. }
  1837. }
  1838. }
  1839. }
  1840. // Done with outfit.xml
  1841. llsd_xml.close();
  1842. if (outfit.empty())
  1843. {
  1844. LL_DEBUGS("InitialOutfit") << "Empty or invalid outfit.xml file"
  1845. << LL_ENDL;
  1846. return;
  1847. }
  1848. // Check the currently worn attachments against our list and remove the
  1849. // worn objects in excess.
  1850. detachExtraAttachments(outfit);
  1851. // Check the currently worn clothes (*not* the bodyparts !... they will
  1852. // get replaced automatically by the outfit ones) against our list and
  1853. // remove the worn items in excess.
  1854. uuid_list_t worn_clothes;
  1855. for (U32 i = (U32)LLWearableType::WT_SHIRT;
  1856. i < (U32)LLWearableType::WT_COUNT; ++i)
  1857. {
  1858. LLWearableType::EType type = (LLWearableType::EType)i;
  1859. for (U32 index = 0, count = gAgentWearables.getWearableCount(type);
  1860. index < count; )
  1861. {
  1862. LLInventoryItem* item =
  1863. gInventory.getItem(gAgentWearables.getWearableItemID(type,
  1864. index));
  1865. if (item)
  1866. {
  1867. LLUUID id = item->getUUID();
  1868. if (outfit.count(id) == 0)
  1869. {
  1870. LL_DEBUGS("InitialOutfit") << "Wearable: " << id
  1871. << " not in outfit, removing."
  1872. << LL_ENDL;
  1873. gAgentWearables.removeWearable(type, false, index);
  1874. --count;
  1875. }
  1876. else
  1877. {
  1878. ++index;
  1879. }
  1880. }
  1881. else
  1882. {
  1883. llwarns << "Wearable for type " << type << " and layer "
  1884. << index << " not found in inventory !" << llendl;
  1885. ++index;
  1886. }
  1887. }
  1888. }
  1889. LL_DEBUGS("InitialOutfit") << "Worn items should now be matching the saved list."
  1890. << LL_ENDL;
  1891. }
  1892. LLAppearanceMgr::ERestoreOutfitStatus LLAppearanceMgr::restoreOutfit(bool can_retry)
  1893. {
  1894. ERestoreOutfitStatus status = DONE;
  1895. // First, create a list of currently worn inventory items
  1896. uuid_list_t worn;
  1897. // Add the worn attachments inventory items
  1898. for (S32 i = 0, count = gAgentAvatarp->mAttachedObjectsVector.size();
  1899. i < count; ++i)
  1900. {
  1901. LLViewerObject* object =
  1902. gAgentAvatarp->mAttachedObjectsVector[i].first;
  1903. if (object)
  1904. {
  1905. worn.emplace(object->getAttachmentItemID());
  1906. }
  1907. }
  1908. // Add the worn boby parts and clothes inventory items
  1909. for (U32 i = 0; i < (U32)LLWearableType::WT_COUNT; ++i)
  1910. {
  1911. LLWearableType::EType type = (LLWearableType::EType)i;
  1912. for (U32 index = 0, count = gAgentWearables.getWearableCount(type);
  1913. index < count; ++index)
  1914. {
  1915. LLInventoryItem* item =
  1916. gInventory.getItem(gAgentWearables.getWearableItemID(type,
  1917. index));
  1918. if (item)
  1919. {
  1920. worn.emplace(item->getUUID());
  1921. }
  1922. }
  1923. }
  1924. // Now, compare to the saved outfit.xml file and re-wear items that are
  1925. // not yet worn
  1926. std::string filename = get_outfit_filename();
  1927. llinfos << "Reading the saved outfit from: " << filename << llendl;
  1928. LLSD list;
  1929. llifstream llsd_xml(filename.c_str(), std::ios::in | std::ios::binary);
  1930. if (llsd_xml.is_open())
  1931. {
  1932. LLSDSerialize::fromXML(list, llsd_xml);
  1933. LL_DEBUGS("InitialOutfit") << "Got outfit items list:\n";
  1934. std::stringstream str;
  1935. LLSDSerialize::toPrettyXML(list, str);
  1936. LL_CONT << "\n" << str.str() << LL_ENDL;
  1937. for (LLSD::map_iterator iter = list.beginMap(),
  1938. end = list.endMap();
  1939. iter != end; ++iter)
  1940. {
  1941. LLSD::String key_name = iter->first;
  1942. LLSD array = iter->second;
  1943. if (key_name == "attachments" && array.isArray())
  1944. {
  1945. for (S32 i = 0, count = array.size(); i < count; ++i)
  1946. {
  1947. LLSD map = array[i];
  1948. if (map.has("inv_item_id"))
  1949. {
  1950. LLUUID item_id = map.get("inv_item_id");
  1951. if (worn.find(item_id) == worn.end())
  1952. {
  1953. LLViewerInventoryItem* item =
  1954. gInventory.getItem(item_id);
  1955. if (item)
  1956. {
  1957. LL_DEBUGS("InitialOutfit") << "Reattaching: "
  1958. << item_id.asString()
  1959. << LL_ENDL;
  1960. rezAttachment(item, NULL, false);
  1961. }
  1962. else
  1963. {
  1964. if (can_retry)
  1965. {
  1966. status = RETRY;
  1967. LL_DEBUGS("InitialOutfit") << item_id.asString()
  1968. << " not yet found in inventory."
  1969. << LL_ENDL;
  1970. }
  1971. else
  1972. {
  1973. status = INCOMPLETE;
  1974. llwarns << item_id.asString()
  1975. << " not found in inventory, could not reattach."
  1976. << llendl;
  1977. }
  1978. }
  1979. }
  1980. else
  1981. {
  1982. LL_DEBUGS("InitialOutfit") << "Object: " << item_id
  1983. << " already attached: OK."
  1984. << LL_ENDL;
  1985. }
  1986. }
  1987. else
  1988. {
  1989. llwarns << "Malformed attachments list (no \"inv_item_id\" key). Aborting."
  1990. << llendl;
  1991. llsd_xml.close();
  1992. return FAILED;
  1993. }
  1994. }
  1995. }
  1996. else if (key_name == "wearables" && array.isArray())
  1997. {
  1998. for (S32 i = 0, count = array.size(); i < count; ++i)
  1999. {
  2000. LLSD map = array[i];
  2001. if (map.has("inv_item_id"))
  2002. {
  2003. LLUUID item_id = map.get("inv_item_id");
  2004. if (worn.find(item_id) == worn.end())
  2005. {
  2006. LLViewerInventoryItem* item =
  2007. gInventory.getItem(item_id);
  2008. if (item)
  2009. {
  2010. LL_DEBUGS("InitialOutfit") << "Rewearing: "
  2011. << item_id.asString()
  2012. << LL_ENDL;
  2013. wearInventoryItemOnAvatar(item, false);
  2014. }
  2015. else
  2016. {
  2017. if (can_retry)
  2018. {
  2019. status = RETRY;
  2020. LL_DEBUGS("InitialOutfit") << item_id.asString()
  2021. << " not yet found in inventory."
  2022. << LL_ENDL;
  2023. break; // Do not wear the rest: we must preserve the order
  2024. }
  2025. else
  2026. {
  2027. status = INCOMPLETE;
  2028. llwarns << item_id.asString()
  2029. << " not found in inventory, could not rewear."
  2030. << llendl;
  2031. }
  2032. }
  2033. }
  2034. else
  2035. {
  2036. LL_DEBUGS("InitialOutfit") << "Wearable: " << item_id
  2037. << " already worn: OK."
  2038. << LL_ENDL;
  2039. }
  2040. }
  2041. else
  2042. {
  2043. llwarns << "Malformed wearables list (no \"inv_item_id\" key). Aborting."
  2044. << llendl;
  2045. llsd_xml.close();
  2046. return FAILED;
  2047. }
  2048. }
  2049. }
  2050. else
  2051. {
  2052. llwarns << "Malformed outfit list. Aborting." << llendl;
  2053. llsd_xml.close();
  2054. return FAILED;
  2055. }
  2056. }
  2057. llsd_xml.close();
  2058. }
  2059. else
  2060. {
  2061. llwarns << "Cannot open " << filename << " for outfit restoration."
  2062. << llendl;
  2063. return FAILED;
  2064. }
  2065. return status;
  2066. }
  2067. void LLAppearanceMgr::saveOutfit()
  2068. {
  2069. // This list will hold the full outfit list (attachments + wearables)
  2070. LLSD list;
  2071. // Save the worn attachments list
  2072. LLSD array = LLSD::emptyArray();
  2073. for (S32 i = 0, count = gAgentAvatarp->mAttachedObjectsVector.size();
  2074. i < count; ++i)
  2075. {
  2076. LLViewerObject* object =
  2077. gAgentAvatarp->mAttachedObjectsVector[i].first;
  2078. if (object)
  2079. {
  2080. LLUUID item_id = object->getAttachmentItemID();
  2081. LLViewerInventoryItem* item = gInventory.getItem(item_id);
  2082. if (item)
  2083. {
  2084. LLSD entry = LLSD::emptyMap();
  2085. entry.insert("inv_item_id", item_id);
  2086. array.append(entry);
  2087. LL_DEBUGS("InitialOutfit") << "Attachment " << item_id
  2088. << " saved in outfit list."
  2089. << LL_ENDL;
  2090. }
  2091. else
  2092. {
  2093. // This happens with temporary attachments
  2094. LL_DEBUGS("InitialOutfit") << item_id
  2095. << " not found in inventory. Not saving in outfit list."
  2096. << LL_ENDL;
  2097. }
  2098. }
  2099. }
  2100. list.insert("attachments", array);
  2101. // Save the worn body parts and clothes list
  2102. array = LLSD::emptyArray();
  2103. for (U32 i = 0; i < (U32)LLWearableType::WT_COUNT; ++i)
  2104. {
  2105. LLWearableType::EType type = (LLWearableType::EType)i;
  2106. U32 count = gAgentWearables.getWearableCount(type);
  2107. if (count > 1 &&
  2108. LLWearableType::getAssetType(type) == LLAssetType::AT_BODYPART)
  2109. {
  2110. llwarns << "More than one layer found for body part type: " << i
  2111. << llendl;
  2112. count = 1; // Paranoia: only one wearable per body part type.
  2113. }
  2114. for (U32 index = 0; index < count; ++index)
  2115. {
  2116. LLViewerInventoryItem* item =
  2117. gInventory.getItem(gAgentWearables.getWearableItemID(type,
  2118. index));
  2119. if (item)
  2120. {
  2121. LLSD entry = LLSD::emptyMap();
  2122. entry.insert("inv_item_id", item->getUUID());
  2123. array.append(entry);
  2124. LL_DEBUGS("InitialOutfit") << "Wearable " << item->getUUID()
  2125. << " saved in outfit list."
  2126. << LL_ENDL;
  2127. }
  2128. else
  2129. {
  2130. // This should not happen...
  2131. llwarns << "Wearable type " << type << " on layer " << index
  2132. << " not found in inventory. Not saving in outfit list."
  2133. << llendl;
  2134. }
  2135. }
  2136. }
  2137. list.insert("wearables", array);
  2138. // Save the list to the outfit.xml file
  2139. std::string filename = get_outfit_filename();
  2140. llofstream list_file(filename.c_str());
  2141. if (list_file.is_open())
  2142. {
  2143. LLSDSerialize::toPrettyXML(list, list_file);
  2144. list_file.close();
  2145. LL_DEBUGS("InitialOutfit") << "Outfit items list saved to: " << filename;
  2146. std::stringstream str;
  2147. LLSDSerialize::toPrettyXML(list, str);
  2148. LL_CONT << "\n" << str.str() << LL_ENDL;
  2149. }
  2150. else
  2151. {
  2152. llwarns << "Could not open file '" << filename << "' for wirting."
  2153. << llendl;
  2154. }
  2155. // Notify the Make New Outfit floater, if opened
  2156. HBFloaterMakeNewOutfit::setDirty();
  2157. mNeedsSyncAttachments = mNeedsSyncWearables = true;
  2158. gAttachmentsListDirty = gWearablesListDirty = false;
  2159. mUpdateCOFTimer.reset();
  2160. }
  2161. bool LLAppearanceMgr::isAvatarFullyBaked() const
  2162. {
  2163. static LLCachedControl<bool> os_use_cof(gSavedSettings, "OSUseCOF");
  2164. if (gIsInSecondLife || os_use_cof)
  2165. {
  2166. return !mIsRestoringInitialOutfit && !mRebaking &&
  2167. !mNeedsSyncAttachments && !mNeedsSyncWearables &&
  2168. !gAttachmentsListDirty && !gWearablesListDirty;
  2169. }
  2170. else
  2171. {
  2172. return !mIsRestoringInitialOutfit && !mRebaking &&
  2173. !gAttachmentsListDirty && !gWearablesListDirty;
  2174. }
  2175. }
  2176. void LLAppearanceMgr::setRebaking(bool rebaking)
  2177. {
  2178. if (mRebaking != rebaking)
  2179. {
  2180. mRebaking = rebaking;
  2181. if (!rebaking && gAutomationp)
  2182. {
  2183. gAutomationp->onAgentBaked();
  2184. }
  2185. }
  2186. }
  2187. ///////////////////////////////////////////////////////////////////////////////
  2188. // Simple, no-brainer re-implementation of the ugly COF concept... HB
  2189. ///////////////////////////////////////////////////////////////////////////////
  2190. //static
  2191. const LLUUID LLAppearanceMgr::getCOF(bool create)
  2192. {
  2193. // Note: in OpenSim, we allow to remove the COF, and its UUID may therefore
  2194. // change during a viewer session, so we do not cache this UUID. HB
  2195. return gInventory.findCategoryUUIDForType(LLFolderType::FT_CURRENT_OUTFIT,
  2196. create);
  2197. }
  2198. S32 LLAppearanceMgr::getCOFVersion() const
  2199. {
  2200. LLViewerInventoryCategory* cof = gInventory.getCategory(getCOF());
  2201. if (cof)
  2202. {
  2203. return cof->getVersion();
  2204. }
  2205. return LLViewerInventoryCategory::VERSION_UNKNOWN;
  2206. }
  2207. void LLAppearanceMgr::updateCOF() const
  2208. {
  2209. const LLUUID cof_id = getCOF(true);
  2210. gInventory.updateCategory(gInventory.getCategory(cof_id));
  2211. gInventory.notifyObservers();
  2212. }
  2213. LLAppearanceMgr::ERestoreOutfitStatus LLAppearanceMgr::restoreOutfitFromCOF(bool can_retry)
  2214. {
  2215. const LLUUID cof_id = getCOF(true);
  2216. if (!gInventory.isCategoryComplete(cof_id))
  2217. {
  2218. return can_retry ? RETRY : FAILED;
  2219. }
  2220. LLViewerInventoryCategory* cof = gInventory.getCategory(cof_id);
  2221. if (cof)
  2222. {
  2223. wearInventoryCategoryOnAvatar(cof, true, false);
  2224. return DONE;
  2225. }
  2226. return FAILED;
  2227. }
  2228. void LLAppearanceMgr::cleanupCOF(const LLUUID& cof)
  2229. {
  2230. const LLUUID& laf = gInventory.getLostAndFoundID();
  2231. const LLUUID& trash = gInventory.getTrashID();
  2232. LLInventoryModel::cat_array_t cats;
  2233. LLInventoryModel::item_array_t items;
  2234. gInventory.collectDescendents(cof, cats, items,
  2235. LLInventoryModel::EXCLUDE_TRASH);
  2236. for (U32 i = 0, count = cats.size(); i < count; ++i)
  2237. {
  2238. LLViewerInventoryCategory* catp = cats[i];
  2239. if (catp) // Paranoia
  2240. {
  2241. // Move the folder to Lost And Found
  2242. llwarns << "Found (non-link) folder '" << catp->getName()
  2243. << "' in COF: moving it to Lost And Found." << llendl;
  2244. gInventory.changeCategoryParent(catp, laf, false);
  2245. }
  2246. }
  2247. uuid_list_t linked_items_ids;
  2248. for (U32 i = 0, count = items.size(); i < count; ++i)
  2249. {
  2250. LLViewerInventoryItem* itemp = items[i];
  2251. if (!itemp) continue; // Paranoia
  2252. const LLUUID& item_id = itemp->getUUID();
  2253. LLInventoryObject* objectp = gInventory.getObject(item_id);
  2254. if (objectp &&
  2255. objectp->getActualType() == LLAssetType::AT_LINK_FOLDER)
  2256. {
  2257. llinfos << "Trashing useless folder link '" << itemp->getName()
  2258. << "' out of COF." << llendl;
  2259. gInventory.changeItemParent(itemp, trash, false);
  2260. continue;
  2261. }
  2262. if (itemp->getIsBrokenLink())
  2263. {
  2264. remove_inventory_item(item_id);
  2265. }
  2266. else if (itemp->getIsLinkType())
  2267. {
  2268. const LLUUID& linked_id = itemp->getLinkedUUID();
  2269. if (linked_items_ids.count(linked_id))
  2270. {
  2271. remove_inventory_item(item_id);
  2272. }
  2273. else
  2274. {
  2275. linked_items_ids.emplace(linked_id);
  2276. }
  2277. }
  2278. else
  2279. {
  2280. // If it is not a link, move the item to Lost And Found instead
  2281. // of purging it...
  2282. const std::string& name = itemp->getName();
  2283. llwarns << "Found (non-link) object '" << name
  2284. << "' in COF: moving it to Lost And Found." << llendl;
  2285. move_inventory_item(item_id, laf, name);
  2286. }
  2287. }
  2288. }
  2289. void LLAppearanceMgr::syncAttachmentLinksInCOF()
  2290. {
  2291. const LLUUID cof = getCOF(true);
  2292. // Remove folders from COF (folder links are sometimes created by v4
  2293. // viewers, but are totally useless !) as well as duplicate and broken
  2294. // links
  2295. cleanupCOF(cof);
  2296. // Get the list of attached items in inventory:
  2297. uuid_list_t attached_items_ids;
  2298. for (S32 i = 0, count = gAgentAvatarp->mAttachedObjectsVector.size();
  2299. i < count; ++i)
  2300. {
  2301. LLViewerObject* objectp =
  2302. gAgentAvatarp->mAttachedObjectsVector[i].first;
  2303. if (objectp)
  2304. {
  2305. const LLUUID& item_id = objectp->getAttachmentItemID();
  2306. if (item_id.isNull())
  2307. {
  2308. llwarns << "Null inventory item UUID found for attached object "
  2309. << objectp->getID() << llendl;
  2310. continue;
  2311. }
  2312. LLViewerInventoryItem* itemp = gInventory.getItem(item_id);
  2313. if (itemp) // May be NULL for temporary attachments
  2314. {
  2315. attached_items_ids.emplace(item_id);
  2316. }
  2317. }
  2318. }
  2319. // Get the list of object items in COF:
  2320. LLInventoryModel::item_array_t obj_items;
  2321. getDescendentsOfAssetType(cof, obj_items, LLAssetType::AT_OBJECT);
  2322. // Now remove from COF the links to non-attached objects
  2323. uuid_list_t::iterator attach_id_it;
  2324. for (U32 i = 0, count = obj_items.size(); i < count; ++i)
  2325. {
  2326. LLViewerInventoryItem* itemp = obj_items[i];
  2327. if (itemp) // Paranoia
  2328. {
  2329. LLUUID item_id = itemp->getUUID();
  2330. attach_id_it = attached_items_ids.find(itemp->getLinkedUUID());
  2331. if (attach_id_it != attached_items_ids.end())
  2332. {
  2333. // Get this item out of the list so that:
  2334. // 1.- the next links pointing to it will be removed
  2335. // 2.- it is removed from the list of items for which a new
  2336. // link needs to be created
  2337. attached_items_ids.erase(attach_id_it);
  2338. LL_DEBUGS("COF") << "Found a matching link in COF for attachment: "
  2339. << itemp->getName() << LL_ENDL;
  2340. }
  2341. else if (itemp->getIsLinkType())
  2342. {
  2343. LL_DEBUGS("COF") << "Purging link '" << itemp->getName()
  2344. << "' from COF." << LL_ENDL;
  2345. remove_inventory_item(item_id);
  2346. }
  2347. }
  2348. }
  2349. // Link the remaining unlinked attachments
  2350. uuid_list_t::iterator end = attached_items_ids.end();
  2351. for (attach_id_it = attached_items_ids.begin(); attach_id_it != end;
  2352. ++attach_id_it)
  2353. {
  2354. LLViewerInventoryItem* itemp = gInventory.getItem(*attach_id_it);
  2355. if (itemp) // Paranoia
  2356. {
  2357. // Create a new link for this attached object
  2358. LL_DEBUGS("COF") << "Creating a new link for attached item: "
  2359. << itemp->getName() << LL_ENDL;
  2360. LLPointer<LLInventoryCallback> cb =
  2361. new LLCreateLinkInCOFCallback(itemp->getUUID(), false);
  2362. link_inventory_object(cof, LLPointer<LLInventoryObject>(itemp),
  2363. cb);
  2364. }
  2365. }
  2366. updateCOF();
  2367. mNeedsSyncAttachments = false;
  2368. mUpdateCOFTimer.reset();
  2369. }
  2370. void LLAppearanceMgr::syncWearableLinksInCOF()
  2371. {
  2372. const LLUUID cof = getCOF(true);
  2373. // Remove folders from COF (folder links are sometimes created by v4
  2374. // viewers, but are totally useless !) as well as duplicate and broken
  2375. // links
  2376. cleanupCOF(cof);
  2377. // First collect all wearables items present in the COF
  2378. LLInventoryModel::item_array_t wear_items;
  2379. getDescendentsOfWearableTypes(cof, wear_items);
  2380. // Second, remove from the COF all links to non-worn items and all non-link
  2381. // wearable items (doing this first allows to ensure stale links will be
  2382. // removed already when the last created link will fire the server-side
  2383. // rebake callback).
  2384. uuid_list_t linked_items;
  2385. for (S32 i = 0, count = wear_items.size(); i < count; )
  2386. {
  2387. LLViewerInventoryItem* item = wear_items[i];
  2388. if (!item) // Paranoia
  2389. {
  2390. wear_items.erase(wear_items.begin() + i);
  2391. --count;
  2392. continue;
  2393. }
  2394. LLUUID item_id = item->getUUID();
  2395. LLUUID linked_item_id = item->getLinkedUUID();
  2396. bool link_to_worn_item = item->getIsLinkType() &&
  2397. !item->getIsBrokenLink() &&
  2398. gAgentWearables.isWearingItem(linked_item_id);
  2399. if (link_to_worn_item && (mForceServerSideRebake || mBakeRequestSent))
  2400. {
  2401. // Force the removal of the first link to a worn item, so to force
  2402. // a COF version update and thus, a rebake.
  2403. mForceServerSideRebake = false;
  2404. link_to_worn_item = false;
  2405. }
  2406. if (!link_to_worn_item || linked_items.count(linked_item_id))
  2407. {
  2408. if (item->getIsLinkType())
  2409. {
  2410. LL_DEBUGS("COF") << "Purging link to wearable '"
  2411. << item->getName()
  2412. << "' from COF." << LL_ENDL;
  2413. remove_inventory_item(item_id);
  2414. }
  2415. wear_items.erase(wear_items.begin() + i);
  2416. --count;
  2417. }
  2418. else
  2419. {
  2420. linked_items.emplace(item->getLinkedUUID());
  2421. ++i;
  2422. }
  2423. }
  2424. // Third, update or create links to worn items
  2425. LLUUID item_id;
  2426. std::string order_str;
  2427. for (U32 i = 0; i < (U32)LLWearableType::WT_COUNT; ++i)
  2428. {
  2429. LLWearableType::EType type = (LLWearableType::EType)i;
  2430. U32 count = gAgentWearables.getWearableCount(type);
  2431. if (count > 1 &&
  2432. LLWearableType::getAssetType(type) == LLAssetType::AT_BODYPART)
  2433. {
  2434. LL_DEBUGS("COF") << "More that one layer found for body part type "
  2435. << i << LL_ENDL;
  2436. count = 1; // Paranoia: only one wearable per body part type.
  2437. }
  2438. for (U32 index = 0; index < count; ++index)
  2439. {
  2440. LLViewerInventoryItem* item =
  2441. gInventory.getItem(gAgentWearables.getWearableItemID(type,
  2442. index));
  2443. if (!item) continue; // Paranoia
  2444. item_id = item->getUUID();
  2445. order_str = build_order_string(type, index);
  2446. bool link_exists = false;
  2447. for (S32 j = 0, n = wear_items.size(); j < n; ++j)
  2448. {
  2449. LLViewerInventoryItem* link_item = wear_items[j];
  2450. if (link_item && link_item->getIsLinkType() &&
  2451. link_item->getLinkedUUID() == item_id)
  2452. {
  2453. link_exists = true;
  2454. // If needed, update the link description to match the
  2455. // current layer index...
  2456. if (link_item->getActualDescription() != order_str)
  2457. {
  2458. LL_DEBUGS("COF") << "Changing layer info for item: "
  2459. << link_item->getName() << LL_ENDL;
  2460. link_item->setDescription(order_str);
  2461. link_item->setComplete(true);
  2462. link_item->updateServer(false);
  2463. gInventory.updateItem(link_item);
  2464. }
  2465. else
  2466. {
  2467. LL_DEBUGS("COF") << "A link already exists for item: "
  2468. << link_item->getName() << LL_ENDL;
  2469. }
  2470. // Makes next searches faster
  2471. wear_items.erase(wear_items.begin() + j);
  2472. break;
  2473. }
  2474. }
  2475. if (!link_exists)
  2476. {
  2477. // Create a new link for this worn item
  2478. LL_DEBUGS("COF") << "Creating a new link for worn item: "
  2479. << item->getName() << LL_ENDL;
  2480. LLPointer<LLInventoryCallback> cb =
  2481. new LLCreateLinkInCOFCallback(item->getUUID(), true);
  2482. link_inventory_item(item->getUUID(), cof, order_str,
  2483. LLAssetType::AT_LINK, cb);
  2484. }
  2485. }
  2486. }
  2487. updateCOF();
  2488. mNeedsSyncWearables = false;
  2489. mUpdateCOFTimer.reset();
  2490. }
  2491. void LLAppearanceMgr::slamCOF()
  2492. {
  2493. // Create a list of links to worn items
  2494. LLSD contents = LLSD::emptyArray();
  2495. // Start with wearables
  2496. for (U32 i = 0; i < (U32)LLWearableType::WT_COUNT; ++i)
  2497. {
  2498. LLWearableType::EType type = (LLWearableType::EType)i;
  2499. U32 count = gAgentWearables.getWearableCount(type);
  2500. if (count > 1 &&
  2501. LLWearableType::getAssetType(type) == LLAssetType::AT_BODYPART)
  2502. {
  2503. LL_DEBUGS("COF") << "More that one layer found for body part type "
  2504. << i << LL_ENDL;
  2505. count = 1; // Paranoia: only one wearable per body part type.
  2506. }
  2507. for (U32 index = 0; index < count; ++index)
  2508. {
  2509. LLViewerInventoryItem* item =
  2510. gInventory.getItem(gAgentWearables.getWearableItemID(type,
  2511. index));
  2512. if (!item) continue; // Paranoia
  2513. LLSD item_contents;
  2514. item_contents["name"] = item->getName();
  2515. item_contents["desc"] = build_order_string(type, index);
  2516. item_contents["linked_id"] = item->getLinkedUUID();
  2517. item_contents["type"] = LLAssetType::AT_LINK;
  2518. contents.append(item_contents);
  2519. }
  2520. }
  2521. // Now for attachments...
  2522. for (U32 i = 0, count = gAgentAvatarp->mAttachedObjectsVector.size();
  2523. i < count; ++i)
  2524. {
  2525. LLViewerObject* object =
  2526. gAgentAvatarp->mAttachedObjectsVector[i].first;
  2527. if (!object) continue; // Paranoia
  2528. LLViewerInventoryItem* item =
  2529. gInventory.getItem(object->getAttachmentItemID());
  2530. if (!item) // May be NULL for temporary attachments
  2531. {
  2532. continue;
  2533. }
  2534. LLSD item_contents;
  2535. item_contents["name"] = item->getName();
  2536. item_contents["desc"] = item->getDescription();
  2537. item_contents["linked_id"] = item->getLinkedUUID();
  2538. item_contents["type"] = LLAssetType::AT_LINK;
  2539. contents.append(item_contents);
  2540. }
  2541. // Slam the COF with new links listed in contents
  2542. const LLUUID cof = getCOF(true);
  2543. LLPointer<LLInventoryCallback> cb = new LLSlamCOFCallback();
  2544. slam_inventory_folder(cof, contents, cb);
  2545. mNeedsSyncWearables = mNeedsSyncAttachments = false;
  2546. mUpdateCOFTimer.reset();
  2547. }
  2548. void LLAppearanceMgr::incrementCofVersion()
  2549. {
  2550. if (!gAgent.regionCapabilitiesReceived())
  2551. {
  2552. // Mark as needing a rebake after the capabilities for the new agent
  2553. // region are received.
  2554. gAgent.mRebakeNeeded = true;
  2555. return;
  2556. }
  2557. llinfos << "Forcing an update of the COF and a rebake." << llendl;
  2558. if (!AISAPI::isAvailable())
  2559. {
  2560. const LLUUID cof = getCOF(true);
  2561. remove_folder_contents(cof, LLPointer<LLInventoryCallback>(NULL));
  2562. }
  2563. mForceServerSideRebake = true;
  2564. mNeedsSyncWearables = mNeedsSyncAttachments = true;
  2565. }
  2566. ///////////////////////////////////////////////////////////////////////////////
  2567. // Server-side baking stuff.
  2568. ///////////////////////////////////////////////////////////////////////////////
  2569. void LLAppearanceMgr::requestServerAppearanceUpdate()
  2570. {
  2571. if (!isAgentAvatarValid() || gAgentAvatarp->isEditingAppearance())
  2572. {
  2573. // Do not send out appearance updates if in appearance editing mode
  2574. LL_DEBUGS("Appearance") << "Not sending appearance updates during editing."
  2575. << LL_ENDL;
  2576. return;
  2577. }
  2578. if (!LLVOAvatarSelf::canUseServerBaking())
  2579. {
  2580. llwarns << "Server-side baking not enabled. Aborting." << llendl;
  2581. return;
  2582. }
  2583. if (mBakeRequestSent)
  2584. {
  2585. llinfos << "Server-side rebake already requested and will be retried."
  2586. << llendl;
  2587. mUpdateCOFTimer.reset();
  2588. return;
  2589. }
  2590. setRebaking();
  2591. S32 cof_version = getCOFVersion();
  2592. llinfos << "Sending server-side rebake request with COF version: "
  2593. << cof_version << " (last requested version: "
  2594. << gAgentAvatarp->mLastUpdateRequestCOFVersion
  2595. << " - last received update version: "
  2596. << gAgentAvatarp->mLastUpdateReceivedCOFVersion << ")" << llendl;
  2597. gAgentAvatarp->mLastUpdateRequestCOFVersion = cof_version;
  2598. LLSD body;
  2599. body["cof_version"] = cof_version;
  2600. LLAgent::httpCallback_t succ =
  2601. boost::bind(&LLAppearanceMgr::serverAppearanceUpdateSuccess, this, _1);
  2602. LLAgent::httpCallback_t fail =
  2603. boost::bind(&LLAppearanceMgr::serverAppearanceUpdateFailure, this, _1);
  2604. mBakeRequestSent = true;
  2605. if (!gAgent.requestPostCapability("UpdateAvatarAppearance", body,
  2606. succ, fail))
  2607. {
  2608. mBakeRequestSent = false;
  2609. }
  2610. }
  2611. void LLAppearanceMgr::serverAppearanceUpdateSuccess(const LLSD& result)
  2612. {
  2613. mBakeRequestSent = false;
  2614. if (result.isMap() && result.has("success") &&
  2615. result["success"].asBoolean())
  2616. {
  2617. if (mBakeRetryPolicy.notNull())
  2618. {
  2619. mBakeRetryPolicy->onSuccess();
  2620. }
  2621. llinfos << "Request OK." << llendl;
  2622. setRebaking(false);
  2623. }
  2624. else
  2625. {
  2626. const LLSD& http_results =
  2627. result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS];
  2628. serverAppearanceUpdateFailure(http_results);
  2629. }
  2630. }
  2631. void LLAppearanceMgr::serverAppearanceUpdateFailure(const LLSD& http_results)
  2632. {
  2633. if (!isAgentAvatarValid())
  2634. {
  2635. // Oops... Logged off ?
  2636. return;
  2637. }
  2638. mBakeRequestSent = false;
  2639. S32 status = http_results["type"].asInteger();
  2640. llwarns << "Appearance update request failed with status: " << status
  2641. << llendl;
  2642. LL_DEBUGS("COF") << "HTTP results:\n";
  2643. std::stringstream str;
  2644. LLSDSerialize::toPrettyXML(http_results, str);
  2645. LL_CONT << str.str() << LL_ENDL;
  2646. S32 requested_version = gAgentAvatarp->mLastUpdateRequestCOFVersion;
  2647. S32 expected = requested_version;
  2648. if (http_results.has("error_body"))
  2649. {
  2650. const LLSD& error_body = http_results["error_body"];
  2651. if (error_body.has("expected"))
  2652. {
  2653. expected = error_body["expected"].asInteger();
  2654. }
  2655. }
  2656. if (status == 404)
  2657. {
  2658. llwarns << "Aborting after a 404 error." << llendl;
  2659. }
  2660. else if (requested_version != LLViewerInventoryCategory::VERSION_UNKNOWN &&
  2661. requested_version < getCOFVersion())
  2662. {
  2663. llinfos << "COF got updated, aborting this request and scheduling another"
  2664. << llendl;
  2665. }
  2666. #if 0 // This does not work... Use the COF refetching below instead. HB
  2667. else if (expected > requested_version)
  2668. {
  2669. llinfos << "Requested COF version was: " << requested_version
  2670. << " - COF version expected by the server was: " << expected
  2671. << ". Aborting this request and scheduling another."
  2672. << llendl;
  2673. // Force an update texture request for ourself. The message will return
  2674. // via UDP and be handled in LLVOAvatar::processAvatarAppearance();
  2675. // this should ensure that we receive a new canonical COF from the sim
  2676. // host. Hopefully it will return before the next rebake retry.
  2677. gAgentAvatarp->sendAvatarTexturesRequest(true);
  2678. }
  2679. #endif
  2680. else
  2681. {
  2682. if (mBakeRetryPolicy.isNull())
  2683. {
  2684. mBakeRetryPolicy = new LLAdaptiveRetryPolicy(1.f, 16.f, 2.f, 4,
  2685. true);
  2686. }
  2687. mBakeRetryPolicy->onFailure(status, http_results["headers"]);
  2688. F32 seconds_to_wait;
  2689. if (mBakeRetryPolicy->shouldRetry(seconds_to_wait))
  2690. {
  2691. if (expected < requested_version)
  2692. {
  2693. llinfos << "Requested COF version was: " << requested_version
  2694. << " - COF version expected by the server was: "
  2695. << expected << llendl;
  2696. }
  2697. llinfos << "Retrying..." << llendl;
  2698. doAfterInterval(boost::bind(&LLAppearanceMgr::requestServerAppearanceUpdate,
  2699. &gAppearanceMgr),
  2700. seconds_to_wait);
  2701. return;
  2702. }
  2703. else
  2704. {
  2705. llwarns << "Giving up after too many retries." << llendl;
  2706. if (expected != requested_version)
  2707. {
  2708. // We obviously went out of sync between viewer and server,
  2709. // So try and refetch the COF with the proper version as
  2710. // seen from the server...
  2711. llinfos << "Refetching the COF from the server" << llendl;
  2712. gAgentAvatarp->mLastUpdateRequestCOFVersion =
  2713. LLViewerInventoryCategory::VERSION_UNKNOWN;
  2714. LLViewerInventoryCategory* cof =
  2715. gInventory.getCategory(getCOF(true));
  2716. cof->setVersionUnknown();
  2717. cof->fetch();
  2718. }
  2719. }
  2720. }
  2721. if (mBakeRetryPolicy.notNull())
  2722. {
  2723. mBakeRetryPolicy->reset();
  2724. }
  2725. // Fire a new rebake request after incrementing the COF version
  2726. incrementCofVersion();
  2727. }