hbobjectbackup.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  1. /**
  2. * @file hbobjectbackup.cpp
  3. *
  4. * $LicenseInfo:firstyear=2008&license=viewergpl$
  5. *
  6. * Original implementation Copyright (c) 2008 Merkat viewer authors.
  7. * Debugged/rewritten/augmented code Copyright (c) 2008-2023 Henri Beauchamp.
  8. *
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. */
  31. #include "llviewerprecompiledheaders.h"
  32. #include "hbobjectbackup.h"
  33. #include "imageids.h"
  34. #include "llalertdialog.h"
  35. #include "llcallbacklist.h"
  36. #include "lldir.h"
  37. #include "lleconomy.h"
  38. #include "llfilesystem.h"
  39. #include "llimagej2c.h"
  40. #include "llnotifications.h"
  41. #include "llsdserialize.h"
  42. #include "llsdutil_math.h"
  43. #include "lltransactiontypes.h"
  44. #include "lluictrlfactory.h"
  45. #include "lluploaddialog.h"
  46. #include "llagent.h"
  47. #include "llappviewer.h"
  48. #include "llfloaterperms.h"
  49. #include "llgridmanager.h" // For gIsInSecondLife
  50. #include "llinventorymodel.h" // For gInventory
  51. #include "llmaterialmgr.h"
  52. #include "lltexturecache.h"
  53. #include "lltoolplacer.h"
  54. #include "llviewerassetupload.h"
  55. #include "llviewercontrol.h"
  56. #include "llviewerinventory.h" // For create_inventory_item()
  57. #include "llviewertexturelist.h"
  58. #include "llviewerobjectlist.h"
  59. #include "llviewermenu.h"
  60. #include "llvovolume.h"
  61. // Note: these default textures are initialized with hard coded values to
  62. // prevent cheating. When not in SL, the user-configurable values are used
  63. // instead (see setDefaultTextures() below).
  64. LLUUID gTexturePlywood = IMG_PLYWOOD;
  65. LLUUID gTextureBlank = IMG_BLANK;
  66. LLUUID gTextureInvisible = LLUUID("38b86f85-2575-52a9-a531-23108d8da837");
  67. LLUUID gTextureTransparent = LLUUID("8dcd4a48-2d37-4909-9f78-f7a9eb4ef903");
  68. LLUUID gTextureMedia = LLUUID("8b5fec65-8d8d-9dc5-cda8-8fdf2716e361");
  69. //static
  70. HBObjectBackup::rebase_map_t HBObjectBackup::sAssetMap;
  71. class BackupCacheReadResponder final : public LLTextureCache::ReadResponder
  72. {
  73. protected:
  74. LOG_CLASS(BackupCacheReadResponder);
  75. public:
  76. BackupCacheReadResponder(const LLUUID& id, LLImageFormatted* image)
  77. : mFormattedImage(image), mID(id)
  78. {
  79. setImage(image);
  80. }
  81. void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat,
  82. bool imagelocal) override
  83. {
  84. HBObjectBackup* self = HBObjectBackup::findInstance();
  85. if (!self) return;
  86. if (imageformat == IMG_CODEC_TGA &&
  87. mFormattedImage->getCodec() == IMG_CODEC_J2C)
  88. {
  89. llwarns << "FAILED: texture " << mID
  90. << " is formatted as TGA. Not saving." << llendl;
  91. self->mNonExportedTextures |= HBObjectBackup::TEXTURE_BAD_ENCODING;
  92. mFormattedImage = NULL;
  93. mImageSize = 0;
  94. return;
  95. }
  96. if (mFormattedImage.notNull())
  97. {
  98. if (mFormattedImage->getCodec() == imageformat)
  99. {
  100. mFormattedImage->appendData(data, datasize);
  101. }
  102. else
  103. {
  104. llwarns << "FAILED: texture " << mID
  105. << " is formatted as " << mFormattedImage->getCodec()
  106. << " while expecting " << imageformat
  107. << ". Not saving." << llendl;
  108. mFormattedImage = NULL;
  109. mImageSize = 0;
  110. return;
  111. }
  112. }
  113. else
  114. {
  115. mFormattedImage = LLImageFormatted::createFromType(imageformat);
  116. mFormattedImage->setData(data, datasize);
  117. }
  118. mImageSize = imagesize;
  119. mImageLocal = imagelocal;
  120. }
  121. void started() override
  122. {
  123. }
  124. void completed(bool success) override
  125. {
  126. HBObjectBackup* self = HBObjectBackup::findInstance();
  127. if (!self)
  128. {
  129. llwarns << "Export aborted, HBObjectBackup instance gone !"
  130. << llendl;
  131. return;
  132. }
  133. if (success && mFormattedImage.notNull() && mImageSize > 0)
  134. {
  135. llinfos << "SUCCESS getting texture " << mID << llendl;
  136. std::string name;
  137. mID.toString(name);
  138. name = self->getFolder() + name;
  139. llinfos << "Saving to " << name << llendl;
  140. if (!mFormattedImage->save(name))
  141. {
  142. llwarns << "FAILED to save texture " << mID << llendl;
  143. self->mNonExportedTextures |= HBObjectBackup::TEXTURE_SAVED_FAILED;
  144. }
  145. }
  146. else
  147. {
  148. if (!success)
  149. {
  150. llwarns << "FAILED to get texture " << mID << llendl;
  151. self->mNonExportedTextures |= HBObjectBackup::TEXTURE_MISSING;
  152. }
  153. if (mFormattedImage.isNull())
  154. {
  155. llwarns << "FAILED: NULL texture " << mID << llendl;
  156. self->mNonExportedTextures |= HBObjectBackup::TEXTURE_IS_NULL;
  157. }
  158. }
  159. self->mCheckNextTexture = true;
  160. }
  161. private:
  162. LLPointer<LLImageFormatted> mFormattedImage;
  163. LLUUID mID;
  164. };
  165. HBObjectBackup::HBObjectBackup(const LLSD&)
  166. : mRunning(false),
  167. mRetexture(false)
  168. {
  169. LLUICtrlFactory::getInstance()->buildFloater(this,
  170. "floater_object_backup.xml",
  171. NULL, false); // Do not open
  172. }
  173. //virtual
  174. HBObjectBackup::~HBObjectBackup()
  175. {
  176. // Just in case we got closed unexpectedly...
  177. gIdleCallbacks.deleteFunction(exportWorker);
  178. // Release the selection handle
  179. mSelectedObjects = NULL;
  180. }
  181. //static
  182. bool HBObjectBackup::confirmCloseCallback(const LLSD& notification,
  183. const LLSD& response)
  184. {
  185. if (LLNotification::getSelectedOption(notification, response) == 0)
  186. {
  187. HBObjectBackup* self = findInstance();
  188. if (self)
  189. {
  190. self->destroy();
  191. }
  192. }
  193. return false;
  194. }
  195. //virtual
  196. void HBObjectBackup::onClose(bool app_quitting)
  197. {
  198. // Do not destroy the floater on user close action to avoid getting things
  199. // messed up during import/export.
  200. if (app_quitting)
  201. {
  202. destroy();
  203. }
  204. else
  205. {
  206. gNotifications.add("ConfirmAbortBackup", LLSD(), LLSD(),
  207. confirmCloseCallback);
  208. }
  209. }
  210. void HBObjectBackup::showFloater(bool exporting)
  211. {
  212. // Set the title
  213. setTitle(getString(exporting ? "export" : "import"));
  214. mCurObject = 1;
  215. mCurPrim = mObjects = mPrims = mRezCount = 0;
  216. // Make the floater pop up
  217. setVisibleAndFrontmost();
  218. }
  219. void HBObjectBackup::updateExportNumbers()
  220. {
  221. std::stringstream sstr;
  222. LLUICtrl* ctrl = getChild<LLUICtrl>("name_label");
  223. sstr << "Export Progress \n";
  224. sstr << "Remaining Textures " << mTexturesList.size() << "\n";
  225. ctrl->setValue(LLSD("Text") = sstr.str());
  226. }
  227. void HBObjectBackup::updateImportNumbers()
  228. {
  229. std::stringstream sstr;
  230. LLUICtrl* ctrl = getChild<LLUICtrl>("name_label");
  231. if (mRetexture)
  232. {
  233. sstr << " Textures uploads remaining : " << mTexturesList.size()
  234. << "\n";
  235. ctrl->setValue(LLSD("Text") = sstr.str());
  236. }
  237. else
  238. {
  239. sstr << " Textures uploads N/A \n";
  240. ctrl->setValue(LLSD("Text") = sstr.str());
  241. }
  242. sstr << " Objects " << mCurObject << "/" << mObjects << "\n";
  243. ctrl->setValue(LLSD("Text") = sstr.str());
  244. sstr << " Rez " << mRezCount << "/" << mPrims;
  245. ctrl->setValue(LLSD("Text") = sstr.str());
  246. sstr << " Build " << mCurPrim << "/" << mPrims;
  247. ctrl->setValue(LLSD("Text") = sstr.str());
  248. }
  249. //static
  250. void HBObjectBackup::exportCallback(HBFileSelector::ESaveFilter type,
  251. std::string& filename, void*)
  252. {
  253. if (!filename.empty())
  254. {
  255. LLViewerObject* objp = gSelectMgr.getSelection()->getPrimaryObject();
  256. if (!objp || objp->isDead())
  257. {
  258. gNotifications.add("ExportAborted");
  259. return;
  260. }
  261. HBObjectBackup* self = getInstance();
  262. if (self)
  263. {
  264. self->doExportObject(filename);
  265. }
  266. }
  267. }
  268. //static
  269. void HBObjectBackup::exportObject()
  270. {
  271. if (findInstance())
  272. {
  273. llwarns << "Backup operation already in progress !" << llendl;
  274. showInstance();
  275. }
  276. else
  277. {
  278. LLObjectSelectionHandle selection = gSelectMgr.getSelection();
  279. if (selection && selection->getFirstRootNode())
  280. {
  281. std::string suggestion;
  282. suggestion = selection->getFirstRootNode()->mName;
  283. suggestion = LLDir::getScrubbedFileName(suggestion) + ".xml";
  284. // Open the file save dialog
  285. HBFileSelector::saveFile(HBFileSelector::FFSAVE_XML, suggestion,
  286. exportCallback);
  287. }
  288. }
  289. }
  290. void HBObjectBackup::doExportObject(std::string filename)
  291. {
  292. mFileName = filename;
  293. mFolder = gDirUtil.getDirName(mFileName) + LL_DIR_DELIM_STR;
  294. mTexturesList.clear();
  295. mBadPermsTexturesList.clear();
  296. mLLSD.clear();
  297. mThisGroup.clear();
  298. setDefaultTextures();
  299. mNonExportedTextures = TEXTURE_OK;
  300. mExportState = EXPORT_INIT;
  301. mSelectedObjects = gSelectMgr.getSelection();
  302. mGotExtraPhysics = gAgent.hasRegionCapability("GetObjectPhysicsData");
  303. gIdleCallbacks.addFunction(exportWorker);
  304. }
  305. //---------------------------------------------------------------------------//
  306. // Permissions checking functions
  307. //---------------------------------------------------------------------------//
  308. //static
  309. void HBObjectBackup::setDefaultTextures()
  310. {
  311. // When in SL or in an OpenSIM grid without export permission support, we
  312. // need to check for texture permissions based on their owner and UUID, and
  313. // we cannot trust settings that the user could have modified to try and
  314. // make a non-exportable texture pass for a default grid textures.
  315. if (gIsInSecondLife || gAgent.regionHasExportPermSupport())
  316. {
  317. return;
  318. }
  319. gTexturePlywood = LLUUID(gSavedSettings.getString("DefaultObjectTexture"));
  320. gTextureBlank = LLUUID(gSavedSettings.getString("UIImgWhiteUUID"));
  321. gTextureInvisible =
  322. LLUUID(gSavedSettings.getString("UIImgInvisibleUUID"));
  323. }
  324. //static
  325. bool HBObjectBackup::validatePerms(const LLPermissions* item_permissions,
  326. bool strict)
  327. {
  328. // Gwyn cheating here (gwyneth 20240919)
  329. /*
  330. // Let's see if we have got export permission bit support
  331. bool has_export_perm = gAgent.regionHasExportPermSupport();
  332. // By default, allow to export if the asset is full perm and owned by you.
  333. ExportPolicy policy = ep_full_perm;
  334. if (gIsInSecondLife || (strict && !has_export_perm))
  335. {
  336. // In Second Life you must be the creator to be permitted to export the
  337. // asset. This is also the case when asking for strict checking (i.e.
  338. // for mesh exporting or wearable textures viewing) in OpenSIM grids
  339. // without support for the export permission bit.
  340. policy = ep_creator_only;
  341. }
  342. else if (has_export_perm)
  343. {
  344. // In OpenSIM grids with support for export permission, rely on that
  345. // bit to allow/disallow the export.
  346. policy = ep_export_bit;
  347. }
  348. return item_permissions->allowExportBy(gAgentID, policy);
  349. */
  350. // all become full perms. Cool, eh? (gwyneth 20240818)
  351. return item_permissions->allowExportBy(gAgentID, ep_creator_only);
  352. }
  353. // So far, only Second Life forces TPVs to verify the creator for textures...
  354. // which sucks, because there is no other way to check for a texture
  355. // permissions or creator than to try and find an inventory item with the
  356. // asset Id corresponding to the texture Id, and check the permissions or
  357. // creator on the said inventory item, meaning that if you created the texture
  358. // and subsequently deleted it from your inventory, you will not be able to
  359. // export it any more !!!
  360. // The "must be creator" stuff also goes against the usage in Linden Lab's own
  361. // official viewers, since those allow you to save full perm textures (such as
  362. // the textures in the Library), whoever is the actual creator... Go figure !
  363. //static
  364. bool HBObjectBackup::validateAssetPerms(const LLUUID& asset_id, bool strict)
  365. {
  366. // couldn't care less, strict or not (gwyneth 20240819)
  367. return true;
  368. /*
  369. if (!strict && !gIsInSecondLife)
  370. {
  371. if (!gAgent.regionHasExportPermSupport())
  372. {
  373. // If we are not in Second Life, and this is not a strict checking
  374. // (used for wearables textures) and we do not have support for the
  375. // export permission, do not bother and unconditionally accept the
  376. // texture export (legacy behaviour).
  377. return true;
  378. }
  379. }
  380. if (asset_id == gTexturePlywood || asset_id == gTextureBlank ||
  381. asset_id == gTextureInvisible || asset_id == gTextureTransparent ||
  382. asset_id == gTextureMedia || asset_id == IMG_DEFAULT)
  383. {
  384. // Allow to export a few default SL textures.
  385. return true;
  386. }
  387. LLViewerInventoryCategory::cat_array_t cats;
  388. LLViewerInventoryItem::item_array_t items;
  389. LLAssetIDMatches asset_id_matches(asset_id);
  390. gInventory.collectDescendentsIf(LLUUID::null, cats, items,
  391. LLInventoryModel::INCLUDE_TRASH,
  392. asset_id_matches);
  393. for (S32 i = 0, count = items.size(); i < count; ++i)
  394. {
  395. const LLPermissions item_permissions = items[i]->getPermissions();
  396. if (validatePerms(&item_permissions, strict))
  397. {
  398. return true;
  399. }
  400. }
  401. return false;
  402. */
  403. }
  404. LLUUID HBObjectBackup::validateTextureID(const LLUUID& asset_id)
  405. {
  406. if (asset_id.isNull())
  407. {
  408. return asset_id;
  409. }
  410. if (mBadPermsTexturesList.count(asset_id))
  411. {
  412. // We already checked it and know it is bad...
  413. return gTexturePlywood;
  414. }
  415. if (validateAssetPerms(asset_id))
  416. {
  417. return asset_id;
  418. }
  419. mBadPermsTexturesList.emplace(asset_id); // Cache bad texture Id
  420. mNonExportedTextures |= TEXTURE_BAD_PERM;
  421. llwarns << "Bad permissions for texture Id: " << asset_id
  422. << " - Texture will not be exported." << llendl;
  423. return gTexturePlywood;
  424. }
  425. //static
  426. bool HBObjectBackup::validateNode(LLSelectNode* nodep)
  427. {
  428. // all nodes are valid, period (gwyneth 20240819)
  429. return true;
  430. /*
  431. LLPermissions* perms = nodep->mPermissions;
  432. if (!perms || !validatePerms(perms))
  433. {
  434. return false;
  435. }
  436. // Additionally check if this is a sculpt or a mesh object and if yes, if
  437. // we have export permission on the sclupt texture or the mesh object.
  438. LLViewerObject* objp = nodep->getObject();
  439. if (!objp) // Paranoia
  440. {
  441. return false;
  442. }
  443. if (objp->isSculpted())
  444. {
  445. if (objp->isMesh())
  446. {
  447. return validatePerms(perms, true);
  448. }
  449. const LLSculptParams* paramsp = objp->getSculptParams();
  450. if (paramsp)
  451. {
  452. const LLUUID& sculpt_id = paramsp->getSculptTexture();
  453. return validateAssetPerms(sculpt_id);
  454. }
  455. }
  456. return true; */
  457. }
  458. //---------------------------------------------------------------------------//
  459. //static
  460. void HBObjectBackup::exportWorker(void*)
  461. {
  462. HBObjectBackup* self = findInstance();
  463. if (!self)
  464. {
  465. gIdleCallbacks.deleteFunction(exportWorker);
  466. llwarns << "Export process aborted. HBObjectBackup instance gone !"
  467. << llendl;
  468. gNotifications.add("ExportAborted");
  469. return;
  470. }
  471. self->updateExportNumbers();
  472. switch (self->mExportState)
  473. {
  474. case EXPORT_INIT:
  475. {
  476. self->showFloater(true);
  477. // Fall through to EXPORT_CHECK_PERMS
  478. }
  479. case EXPORT_CHECK_PERMS:
  480. {
  481. struct ff final : public LLSelectedNodeFunctor
  482. {
  483. bool apply(LLSelectNode* node) override
  484. {
  485. return HBObjectBackup::validateNode(node);
  486. }
  487. } func;
  488. LLViewerObject* object;
  489. object = self->mSelectedObjects->getPrimaryObject();
  490. if (object)
  491. {
  492. if (self->mSelectedObjects->applyToNodes(&func, false))
  493. {
  494. self->mExportState = EXPORT_FETCH_PHYSICS;
  495. }
  496. else
  497. {
  498. struct vv final : public LLSelectedNodeFunctor
  499. {
  500. bool apply(LLSelectNode* node) override
  501. {
  502. return node->mValid;
  503. }
  504. } func2;
  505. if (self->mSelectedObjects->applyToNodes(&func2, false))
  506. {
  507. llwarns << "Incorrect permission to export" << llendl;
  508. self->mExportState = EXPORT_FAILED;
  509. }
  510. else
  511. {
  512. LL_DEBUGS("ObjectBackup") << "Nodes permissions not yet received, delaying..."
  513. << LL_ENDL;
  514. self->mExportState = EXPORT_CHECK_PERMS;
  515. }
  516. }
  517. }
  518. else
  519. {
  520. self->mExportState = EXPORT_ABORTED;
  521. }
  522. break;
  523. }
  524. case EXPORT_FETCH_PHYSICS:
  525. {
  526. // Do not bother to try and fetch the extra physics flags if we
  527. // do not have sim support for them...
  528. if (!self->mGotExtraPhysics)
  529. {
  530. self->mExportState = EXPORT_STRUCTURE;
  531. break;
  532. }
  533. struct ff final : public LLSelectedNodeFunctor
  534. {
  535. bool apply(LLSelectNode* node) override
  536. {
  537. LLViewerObject* object = node->getObject();
  538. return gObjectList.gotObjectPhysicsFlags(object);
  539. }
  540. } func;
  541. LLViewerObject* object = self->mSelectedObjects->getPrimaryObject();
  542. if (object)
  543. {
  544. if (self->mSelectedObjects->applyToNodes(&func, false))
  545. {
  546. self->mExportState = EXPORT_STRUCTURE;
  547. }
  548. else
  549. {
  550. LL_DEBUGS("ObjectBackup") << "Nodes physics not yet received, delaying..."
  551. << LL_ENDL;
  552. }
  553. }
  554. else
  555. {
  556. self->mExportState = EXPORT_ABORTED;
  557. }
  558. break;
  559. }
  560. case EXPORT_STRUCTURE:
  561. {
  562. struct ff final : public LLSelectedObjectFunctor
  563. {
  564. bool apply(LLViewerObject* object) override
  565. {
  566. bool is_attachment = object->isAttachment();
  567. object->boostTexturePriority(true);
  568. LLViewerObject::child_list_t children =
  569. object->getChildren();
  570. children.push_front(object); // Push root onto list
  571. HBObjectBackup* self = findInstance();
  572. LLSD prim_llsd = self->primsToLLSD(children,
  573. is_attachment);
  574. LLSD stuff;
  575. if (is_attachment)
  576. {
  577. stuff["root_position"] =
  578. object->getPositionEdit().getValue();
  579. stuff["root_rotation"] =
  580. ll_sd_from_quaternion(object->getRotationEdit());
  581. }
  582. else
  583. {
  584. stuff["root_position"] =
  585. object->getPosition().getValue();
  586. stuff["root_rotation"] =
  587. ll_sd_from_quaternion(object->getRotation());
  588. }
  589. stuff["group_body"] = prim_llsd;
  590. self->mLLSD["data"].append(stuff);
  591. return true;
  592. }
  593. } func;
  594. LLViewerObject* object;
  595. object = self->mSelectedObjects->getPrimaryObject();
  596. if (object)
  597. {
  598. self->mExportState = EXPORT_LLSD;
  599. self->mSelectedObjects->applyToRootObjects(&func, false);
  600. }
  601. else
  602. {
  603. self->mExportState = EXPORT_ABORTED;
  604. }
  605. break;
  606. }
  607. case EXPORT_TEXTURES:
  608. {
  609. if (!self->mCheckNextTexture)
  610. {
  611. // The texture is being fetched. Wait till next idle callback.
  612. return;
  613. }
  614. if (self->mTexturesList.empty())
  615. {
  616. self->mExportState = EXPORT_DONE;
  617. return;
  618. }
  619. // Ok, we got work to do...
  620. self->mCheckNextTexture = false;
  621. self->exportNextTexture();
  622. break;
  623. }
  624. case EXPORT_LLSD:
  625. {
  626. // Create a file stream and write to it
  627. llofstream export_file(self->mFileName.c_str());
  628. if (export_file.is_open())
  629. {
  630. LLSDSerialize::toPrettyXML(self->mLLSD, export_file);
  631. export_file.close();
  632. self->mCheckNextTexture = true;
  633. self->mExportState = EXPORT_TEXTURES;
  634. }
  635. else
  636. {
  637. llwarns << "Could not open file '" << self->mFileName
  638. << "' for writing." << llendl;
  639. self->mExportState = EXPORT_FAILED;
  640. }
  641. break;
  642. }
  643. case EXPORT_DONE:
  644. {
  645. gIdleCallbacks.deleteFunction(exportWorker);
  646. if (self->mNonExportedTextures == HBObjectBackup::TEXTURE_OK)
  647. {
  648. llinfos << "Export successful and complete." << llendl;
  649. gNotifications.add("ExportSuccessful");
  650. }
  651. else
  652. {
  653. llinfos << "Export successful but incomplete: some texture(s) not saved."
  654. << llendl;
  655. // *TODO: translate
  656. std::string reason;
  657. U32 error_bits_map = self->mNonExportedTextures;
  658. if (error_bits_map & HBObjectBackup::TEXTURE_BAD_PERM)
  659. {
  660. reason += "\nBad permissions/creator.";
  661. }
  662. if (error_bits_map & HBObjectBackup::TEXTURE_MISSING)
  663. {
  664. reason += "\nMissing texture (retrying after full rezzing might work).";
  665. }
  666. if (error_bits_map & HBObjectBackup::TEXTURE_BAD_ENCODING)
  667. {
  668. reason += "\nBad texture encoding.";
  669. }
  670. if (error_bits_map & HBObjectBackup::TEXTURE_IS_NULL)
  671. {
  672. reason += "\nNull texture.";
  673. }
  674. if (error_bits_map & HBObjectBackup::TEXTURE_SAVED_FAILED)
  675. {
  676. reason += "\nCould not write to disk.";
  677. }
  678. LLSD args;
  679. args["REASON"] = reason;
  680. gNotifications.add("ExportPartial", args);
  681. }
  682. self->destroy();
  683. break;
  684. }
  685. case EXPORT_FAILED:
  686. {
  687. gIdleCallbacks.deleteFunction(exportWorker);
  688. llwarns << "Export process failed." << llendl;
  689. gNotifications.add("ExportFailed");
  690. self->destroy();
  691. break;
  692. }
  693. case EXPORT_ABORTED:
  694. {
  695. gIdleCallbacks.deleteFunction(exportWorker);
  696. llwarns << "Export process aborted." << llendl;
  697. gNotifications.add("ExportAborted");
  698. self->destroy();
  699. break;
  700. }
  701. }
  702. }
  703. LLSD HBObjectBackup::primsToLLSD(LLViewerObject::child_list_t child_list,
  704. bool is_attachment)
  705. {
  706. LLSD llsd;
  707. char localid[16];
  708. LLUUID t_id;
  709. for (LLViewerObject::child_list_t::iterator i = child_list.begin();
  710. i != child_list.end(); ++i)
  711. {
  712. LLViewerObject* objp = i->get();
  713. if (!objp || objp->isDead())
  714. {
  715. continue;
  716. }
  717. llinfos << "Exporting prim " << objp->getID() << llendl;
  718. // Create an LLSD object that represents this prim. It will be injected
  719. // in to the overall LLSD tree structure
  720. LLSD prim_llsd;
  721. if (!objp->isRoot())
  722. {
  723. // Parent id
  724. snprintf(localid, sizeof(localid), "%u",
  725. objp->getSubParent()->getLocalID());
  726. prim_llsd["parent"] = localid;
  727. }
  728. // Name and description
  729. LLSelectNode* node = mSelectedObjects->findNode(objp);
  730. if (node)
  731. {
  732. prim_llsd["name"] = node->mName;
  733. prim_llsd["description"] = node->mDescription;
  734. }
  735. // Transforms
  736. if (is_attachment)
  737. {
  738. prim_llsd["position"] = objp->getPositionEdit().getValue();
  739. prim_llsd["rotation"] =
  740. ll_sd_from_quaternion(objp->getRotationEdit());
  741. }
  742. else
  743. {
  744. prim_llsd["position"] = objp->getPosition().getValue();
  745. prim_llsd["rotation"] =
  746. ll_sd_from_quaternion(objp->getRotation());
  747. }
  748. prim_llsd["scale"] = objp->getScale().getValue();
  749. // Flags
  750. prim_llsd["phantom"] = objp->flagPhantom(); // Legacy
  751. prim_llsd["physical"] = objp->flagUsePhysics(); // Legacy
  752. prim_llsd["flags"] = (S32)objp->getFlags(); // New way
  753. // Extra physics flags
  754. if (mGotExtraPhysics)
  755. {
  756. LLSD& physics = prim_llsd["ExtraPhysics"];
  757. physics["PhysicsShapeType"] = objp->getPhysicsShapeType();
  758. physics["Gravity"] = objp->getPhysicsGravity();
  759. physics["Friction"] = objp->getPhysicsFriction();
  760. physics["Density"] = objp->getPhysicsDensity();
  761. physics["Restitution"] = objp->getPhysicsRestitution();
  762. }
  763. // Click action
  764. prim_llsd["clickaction"] = objp->getClickAction();
  765. // Prim "material" type (wood, metal, rubber, etc)
  766. prim_llsd["material"] = objp->getMaterial();
  767. // Volume params
  768. LLVolumeParams params = objp->getVolume()->getParams();
  769. prim_llsd["volume"] = params.asLLSD();
  770. // Extra params
  771. // Flexible
  772. if (objp->isFlexible())
  773. {
  774. const LLFlexibleObjectData* datap = objp->getFlexibleObjectData();
  775. if (datap)
  776. {
  777. prim_llsd["flexible"] = datap->asLLSD();
  778. }
  779. }
  780. // Light
  781. const LLLightParams* light_paramsp = objp->getLightParams();
  782. if (light_paramsp)
  783. {
  784. prim_llsd["light"] = light_paramsp->asLLSD();
  785. }
  786. // Light image
  787. const LLLightImageParams* light_imgp = objp->getLightImageParams();
  788. if (light_imgp)
  789. {
  790. t_id = validateTextureID(light_imgp->getLightTexture());
  791. if (t_id.notNull() && !mTexturesList.count(t_id))
  792. {
  793. llinfos << "Found a light texture, adding to list " << t_id
  794. << llendl;
  795. mTexturesList.emplace(t_id);
  796. }
  797. prim_llsd["light_texture"] = light_imgp->asLLSD();
  798. }
  799. // Sculpt
  800. const LLSculptParams* sculptp = objp->getSculptParams();
  801. if (sculptp)
  802. {
  803. prim_llsd["sculpt"] = sculptp->asLLSD();
  804. if ((sculptp->getSculptType() & LL_SCULPT_TYPE_MASK) !=
  805. LL_SCULPT_TYPE_MESH)
  806. {
  807. t_id = sculptp->getSculptTexture();
  808. if (t_id.notNull() && t_id == validateTextureID(t_id))
  809. {
  810. if (!mTexturesList.count(t_id))
  811. {
  812. llinfos << "Found a sculpt texture, adding to list "
  813. << t_id << llendl;
  814. mTexturesList.emplace(t_id);
  815. }
  816. }
  817. else
  818. {
  819. llwarns << "Incorrect permission to export a sculpt texture."
  820. << llendl;
  821. mExportState = EXPORT_FAILED;
  822. }
  823. }
  824. }
  825. // Reflection probe
  826. LLVOVolume* volp = objp->asVolume();
  827. if (volp && volp->isReflectionProbe())
  828. {
  829. prim_llsd["probe"] = volp->getReflectionProbeIsDynamic() ?
  830. "dynamic" : "static";
  831. prim_llsd["probe_volume"] = volp->getReflectionProbeIsBox() ?
  832. "box" : "sphere";
  833. if (volp->getReflectionProbeIsMirror())
  834. {
  835. prim_llsd["probe_is_mirror"] = true;
  836. }
  837. else
  838. {
  839. prim_llsd["probe_ambiance"] =
  840. volp->getReflectionProbeAmbiance();
  841. prim_llsd["probe_near_clip"] =
  842. volp->getReflectionProbeNearClip();
  843. }
  844. }
  845. // Textures and materials
  846. LLSD te_llsd;
  847. LLSD this_te_llsd;
  848. LLSD te_mat_llsd;
  849. LLSD te_gltf_llsd;
  850. LLSD this_te_mat_llsd;
  851. bool has_materials = false;
  852. bool has_pbr_mats = false;
  853. for (U8 i = 0, count = objp->getNumTEs(); i < count; ++i)
  854. {
  855. LLTextureEntry* tep = objp->getTE(i);
  856. if (!tep) continue; // Paranoia
  857. // Diffuse map
  858. t_id = validateTextureID(tep->getID());
  859. this_te_llsd = tep->asLLSD();
  860. // Note: LL's code adds a "gltf_override" entry to the LLSD
  861. // returned by LLTextureEntry:asLLSD() when a PBR material
  862. // is set for that face, unlike what happens for legacy materials
  863. // (which are not dumped). We, however, store PBR material in their
  864. // own a "gltf_materials" array, to stay in line with what happens
  865. // for legacy materials. So, just remove this "noise"...
  866. this_te_llsd.erase("gltf_override");
  867. this_te_llsd["imageid"] = t_id;
  868. te_llsd.append(this_te_llsd);
  869. // Do not export non-existent default textures
  870. if (t_id.notNull() && t_id != gTextureBlank &&
  871. t_id != gTextureInvisible)
  872. {
  873. if (!mTexturesList.count(t_id))
  874. {
  875. mTexturesList.emplace(t_id);
  876. }
  877. }
  878. // Materials
  879. LLMaterial* matp = tep->getMaterialParams().get();
  880. if (matp)
  881. {
  882. has_materials = true;
  883. this_te_mat_llsd = matp->asLLSD();
  884. // Add the face number this material is used for.
  885. this_te_mat_llsd["face"] = i;
  886. t_id = validateTextureID(matp->getNormalID());
  887. if (t_id.notNull() && !mTexturesList.count(t_id))
  888. {
  889. mTexturesList.emplace(t_id);
  890. }
  891. t_id = validateTextureID(matp->getSpecularID());
  892. if (t_id.notNull() && !mTexturesList.count(t_id))
  893. {
  894. mTexturesList.emplace(t_id);
  895. }
  896. te_mat_llsd.append(this_te_mat_llsd);
  897. }
  898. // PBR materials (GLTF-encoded).
  899. LLGLTFMaterial* gltfp = tep->getGLTFRenderMaterial();
  900. if (gltfp)
  901. {
  902. has_pbr_mats = true;
  903. this_te_mat_llsd.clear();
  904. // Add the face number entry this material is used for.
  905. this_te_mat_llsd["face"] = i;
  906. const LLUUID& mat_id = objp->getRenderMaterialID(i);
  907. if (mat_id.notNull())
  908. {
  909. // Add the material asset id.
  910. this_te_mat_llsd["mat_id"] = mat_id;
  911. }
  912. this_te_mat_llsd["json"] = gltfp->asJSON();
  913. t_id = validateTextureID(gltfp->getBaseColorId());
  914. if (t_id.notNull() && !mTexturesList.count(t_id))
  915. {
  916. mTexturesList.emplace(t_id);
  917. }
  918. t_id = validateTextureID(gltfp->getNormalId());
  919. if (t_id.notNull() && !mTexturesList.count(t_id))
  920. {
  921. mTexturesList.emplace(t_id);
  922. }
  923. t_id = validateTextureID(gltfp->getMetallicRoughnessId());
  924. if (t_id.notNull() && !mTexturesList.count(t_id))
  925. {
  926. mTexturesList.emplace(t_id);
  927. }
  928. t_id = validateTextureID(gltfp->getEmissiveId());
  929. if (t_id.notNull() && !mTexturesList.count(t_id))
  930. {
  931. mTexturesList.emplace(t_id);
  932. }
  933. te_gltf_llsd.append(this_te_mat_llsd);
  934. }
  935. }
  936. prim_llsd["textures"] = te_llsd;
  937. if (has_materials)
  938. {
  939. prim_llsd["materials"] = te_mat_llsd;
  940. }
  941. if (has_pbr_mats)
  942. {
  943. prim_llsd["gltf_materials"] = te_gltf_llsd;
  944. }
  945. // The keys in the primitive maps do not have to be localids, they can
  946. // be any string. We simply use localids because they are a unique
  947. // identifier
  948. snprintf(localid, sizeof(localid), "%u", objp->getLocalID());
  949. llsd[(const char*)localid] = prim_llsd;
  950. }
  951. updateExportNumbers();
  952. return llsd;
  953. }
  954. void HBObjectBackup::exportNextTexture()
  955. {
  956. LLUUID id;
  957. uuid_list_t::iterator iter = mTexturesList.begin();
  958. while (true)
  959. {
  960. if (mTexturesList.empty())
  961. {
  962. mCheckNextTexture = true;
  963. llinfos << "Finished exporting textures." << llendl;
  964. return;
  965. }
  966. if (iter == mTexturesList.end())
  967. {
  968. // Not yet ready, wait and re-check at next idle callback...
  969. mCheckNextTexture = true;
  970. return;
  971. }
  972. id = *iter++;
  973. if (id.isNull())
  974. {
  975. // NULL texture id: just remove and ignore.
  976. mTexturesList.erase(id);
  977. LL_DEBUGS("ObjectBackup") << "Null texture UUID found, ignoring."
  978. << LL_ENDL;
  979. continue;
  980. }
  981. LLViewerTexture* imagep = LLViewerTextureManager::findTexture(id);
  982. if (!imagep)
  983. {
  984. llwarns << "We *DO NOT* have the texture " << id << llendl;
  985. mNonExportedTextures |= TEXTURE_MISSING;
  986. mTexturesList.erase(id);
  987. continue;
  988. }
  989. if (imagep->getDiscardLevel() <= 0)
  990. {
  991. // Texture is ready !
  992. break;
  993. }
  994. // Boost texture loading
  995. imagep->setBoostLevel(LLGLTexture::BOOST_PREVIEW);
  996. LL_DEBUGS("ObjectBackup") << "Boosting texture: " << id << LL_ENDL;
  997. LLViewerFetchedTexture* texp =
  998. LLViewerTextureManager::staticCast(imagep);
  999. if (texp && texp->getDesiredDiscardLevel() > 0)
  1000. {
  1001. // Set min discard level to 0
  1002. texp->setMinDiscardLevel(0);
  1003. LL_DEBUGS("ObjectBackup") << "Min discard level set to 0 for texture: "
  1004. << id << LL_ENDL;
  1005. }
  1006. }
  1007. mTexturesList.erase(id);
  1008. llinfos << "Requesting texture " << id << " from cache." << llendl;
  1009. LLImageJ2C* mFormattedImage = new LLImageJ2C;
  1010. BackupCacheReadResponder* responder;
  1011. responder = new BackupCacheReadResponder(id, mFormattedImage);
  1012. gTextureCachep->readFromCache(id, 0, 999999, responder);
  1013. }
  1014. //static
  1015. void HBObjectBackup::importCallback(HBFileSelector::ELoadFilter type,
  1016. std::string& filename, void* datap)
  1017. {
  1018. if (!filename.empty())
  1019. {
  1020. HBObjectBackup* self = getInstance();
  1021. if (self)
  1022. {
  1023. self->mRetexture = (bool)(intptr_t)datap;
  1024. self->doImportObject(filename);
  1025. }
  1026. }
  1027. }
  1028. //static
  1029. void HBObjectBackup::importObject(bool upload)
  1030. {
  1031. if (findInstance())
  1032. {
  1033. llwarns << "Backup operation already in progress !" << llendl;
  1034. showInstance();
  1035. }
  1036. else
  1037. {
  1038. HBFileSelector::loadFile(HBFileSelector::FFLOAD_XML, importCallback,
  1039. (void*)upload);
  1040. }
  1041. }
  1042. bool HBObjectBackup::uploadNeeded(const LLUUID& id)
  1043. {
  1044. // Is this asset a default texture ?
  1045. if (id.isNull() || id == gTexturePlywood || id == gTextureBlank ||
  1046. id == gTextureInvisible)
  1047. {
  1048. return false;
  1049. }
  1050. // Did we already register it for upload ?
  1051. if (mTexturesList.count(id))
  1052. {
  1053. return false;
  1054. }
  1055. // Did we already upload and remap it during this session ?
  1056. if (sAssetMap.count(id))
  1057. {
  1058. return false;
  1059. }
  1060. // Do we already have a usable inventory item for this asset ?
  1061. return validateAssetPerms(id, true);
  1062. }
  1063. void HBObjectBackup::doImportObject(std::string filename)
  1064. {
  1065. mTexturesList.clear();
  1066. mCurrentAsset.setNull();
  1067. mGotExtraPhysics = gAgent.hasRegionCapability("GetObjectPhysicsData");
  1068. setDefaultTextures();
  1069. mFolder = gDirUtil.getDirName(filename) + LL_DIR_DELIM_STR;
  1070. llifstream import_file(filename.c_str());
  1071. bool success = import_file.is_open();
  1072. if (success)
  1073. {
  1074. LLSDSerialize::fromXML(mLLSD, import_file);
  1075. import_file.close();
  1076. success = mLLSD.has("data");
  1077. }
  1078. else
  1079. {
  1080. llwarns << "Could not open file '" << filename << "' for reading."
  1081. << llendl;
  1082. }
  1083. if (!success)
  1084. {
  1085. gNotifications.add("ImportFailed");
  1086. destroy();
  1087. return;
  1088. }
  1089. showFloater(false);
  1090. mAgentPos = gAgent.getPositionAgent();
  1091. mAgentRot = LLQuaternion(gAgent.getAtAxis(), gAgent.getLeftAxis(),
  1092. gAgent.getUpAxis());
  1093. // Get the texture map
  1094. mCurObject = 1;
  1095. mCurPrim = 1;
  1096. mObjects = mLLSD["data"].size();
  1097. mPrims = 0;
  1098. mRezCount = 0;
  1099. updateImportNumbers();
  1100. if (!mRetexture)
  1101. {
  1102. importFirstObject();
  1103. return;
  1104. }
  1105. std::string errmsg, warnmsg;
  1106. for (LLSD::array_const_iterator prim_arr_it = mLLSD["data"].beginArray(),
  1107. prim_arr_end = mLLSD["data"].endArray();
  1108. prim_arr_it != prim_arr_end; ++prim_arr_it)
  1109. {
  1110. LLSD llsd2 = (*prim_arr_it)["group_body"];
  1111. for (LLSD::map_const_iterator prim_it = llsd2.beginMap(),
  1112. prim_end = llsd2.endMap();
  1113. prim_it != prim_end; ++prim_it)
  1114. {
  1115. LLSD prim_llsd = llsd2[prim_it->first];
  1116. if (prim_llsd.has("sculpt"))
  1117. {
  1118. LLSculptParams sculpt;
  1119. sculpt.fromLLSD(prim_llsd["sculpt"]);
  1120. if ((sculpt.getSculptType() & LL_SCULPT_TYPE_MASK) !=
  1121. LL_SCULPT_TYPE_MESH)
  1122. {
  1123. const LLUUID& s_id = sculpt.getSculptTexture();
  1124. if (uploadNeeded(s_id))
  1125. {
  1126. llinfos << "Found a new sculpt texture to upload "
  1127. << s_id << llendl;
  1128. mTexturesList.emplace(s_id);
  1129. }
  1130. }
  1131. }
  1132. if (prim_llsd.has("light_texture"))
  1133. {
  1134. LLLightImageParams lightimg;
  1135. lightimg.fromLLSD(prim_llsd["light_texture"]);
  1136. const LLUUID& l_id = lightimg.getLightTexture();
  1137. if (uploadNeeded(l_id))
  1138. {
  1139. llinfos << "Found a new light texture to upload: " << l_id
  1140. << llendl;
  1141. mTexturesList.emplace(l_id);
  1142. }
  1143. }
  1144. // Check both for "textures" and "texture" since the second (buggy)
  1145. // case has already been seen in some exported prims XML files...
  1146. LLSD te_llsd = prim_llsd.has("textures") ? prim_llsd["textures"]
  1147. : prim_llsd["texture"];
  1148. for (LLSD::array_iterator it = te_llsd.beginArray();
  1149. it != te_llsd.endArray(); ++it)
  1150. {
  1151. LLSD the_te = *it;
  1152. LLTextureEntry te;
  1153. te.fromLLSD(the_te);
  1154. LLUUID t_id = te.getID();
  1155. if (uploadNeeded(t_id))
  1156. {
  1157. llinfos << "Found a new texture to upload: " << t_id
  1158. << llendl;
  1159. mTexturesList.emplace(t_id);
  1160. }
  1161. }
  1162. if (prim_llsd.has("materials"))
  1163. {
  1164. LLSD mat_llsd = prim_llsd["materials"];
  1165. for (LLSD::array_iterator it = mat_llsd.beginArray();
  1166. it != mat_llsd.endArray(); ++it)
  1167. {
  1168. LLSD the_mat = *it;
  1169. LLMaterial mat;
  1170. mat.fromLLSD(the_mat);
  1171. const LLUUID& n_id = mat.getNormalID();
  1172. if (uploadNeeded(n_id))
  1173. {
  1174. llinfos << "Found a new normal map to upload: "
  1175. << n_id << llendl;
  1176. mTexturesList.emplace(n_id);
  1177. }
  1178. const LLUUID& s_id = mat.getSpecularID();
  1179. if (uploadNeeded(s_id))
  1180. {
  1181. llinfos << "Found a new specular map to upload: "
  1182. << s_id << llendl;
  1183. mTexturesList.emplace(s_id);
  1184. }
  1185. }
  1186. }
  1187. if (!prim_llsd.has("gltf_materials"))
  1188. {
  1189. continue;
  1190. }
  1191. LLSD mat_llsd = prim_llsd["gltf_materials"];
  1192. if (mat_llsd.has("mat_id"))
  1193. {
  1194. LLUUID mat_id = mat_llsd["mat_id"].asUUID();
  1195. if (mat_id.notNull() &&
  1196. (sAssetMap.count(mat_id) ||
  1197. validateAssetPerms(mat_id, true)))
  1198. {
  1199. // We have the corresponding PBR material asset in our
  1200. // inventory, so we do not need to upload the associated
  1201. // textures.
  1202. continue;
  1203. }
  1204. }
  1205. for (LLSD::array_iterator it = mat_llsd.beginArray();
  1206. it != mat_llsd.endArray(); ++it)
  1207. {
  1208. LLSD the_mat = *it;
  1209. LLGLTFMaterial mat;
  1210. if (!mat.fromJSON(the_mat["json"], warnmsg, errmsg))
  1211. {
  1212. llwarns << "Failed GLTF from JSON decoding: "
  1213. << (errmsg.empty() ? warnmsg : errmsg)
  1214. << llendl;
  1215. continue;
  1216. }
  1217. const LLUUID& b_id = mat.getBaseColorId();
  1218. if (uploadNeeded(b_id))
  1219. {
  1220. llinfos << "Found a new base color map to upload: " << b_id
  1221. << llendl;
  1222. mTexturesList.emplace(b_id);
  1223. }
  1224. const LLUUID& n_id = mat.getNormalId();
  1225. if (uploadNeeded(n_id))
  1226. {
  1227. llinfos << "Found a new normal map to upload: " << n_id
  1228. << llendl;
  1229. mTexturesList.emplace(n_id);
  1230. }
  1231. const LLUUID& m_id = mat.getMetallicRoughnessId();
  1232. if (uploadNeeded(m_id))
  1233. {
  1234. llinfos << "Found a new metallic roughness map to upload: "
  1235. << m_id << llendl;
  1236. mTexturesList.emplace(m_id);
  1237. }
  1238. const LLUUID& e_id = mat.getEmissiveId();
  1239. if (uploadNeeded(e_id))
  1240. {
  1241. llinfos << "Found a new emissive map to upload: " << e_id
  1242. << llendl;
  1243. mTexturesList.emplace(e_id);
  1244. }
  1245. }
  1246. }
  1247. }
  1248. uploadNextAsset();
  1249. }
  1250. LLVector3 HBObjectBackup::offsetAgent(LLVector3 offset)
  1251. {
  1252. return offset * mAgentRot + mAgentPos;
  1253. }
  1254. void HBObjectBackup::rezAgentOffset(LLVector3 offset)
  1255. {
  1256. // This will break for a sitting agent
  1257. LLToolPlacer mPlacer;
  1258. mPlacer.setObjectType(LL_PCODE_CUBE);
  1259. mPlacer.placeObject((S32)offset.mV[0], (S32)offset.mV[1], MASK_NONE);
  1260. }
  1261. void HBObjectBackup::importFirstObject()
  1262. {
  1263. mRunning = true;
  1264. showFloater(false);
  1265. mGroupPrimImportIter = mLLSD["data"].beginArray();
  1266. mRootRootPos = LLVector3((*mGroupPrimImportIter)["root_position"]);
  1267. mObjects = mLLSD["data"].size();
  1268. mCurObject = 1;
  1269. importNextObject();
  1270. }
  1271. void HBObjectBackup::importNextObject()
  1272. {
  1273. mToSelect.clear();
  1274. mRezCount = 0;
  1275. mThisGroup = (*mGroupPrimImportIter)["group_body"];
  1276. mPrimImportIter = mThisGroup.beginMap();
  1277. mCurPrim = 0;
  1278. mPrims = mThisGroup.size();
  1279. updateImportNumbers();
  1280. LLVector3 lgpos = LLVector3((*mGroupPrimImportIter)["root_position"]);
  1281. mGroupOffset = lgpos - mRootRootPos;
  1282. mRootPos = offsetAgent(LLVector3(2.f, 0.f, 0.f));
  1283. mRootRot = ll_quaternion_from_sd((*mGroupPrimImportIter)["root_rotation"]);
  1284. rezAgentOffset(LLVector3(0.f, 2.f, 0.f));
  1285. // Now we must wait for the callback when ViewerObjectList gets the new
  1286. // objects and we have the correct number selected
  1287. }
  1288. class HBBackupMatInvCB final : public LLInventoryCallback
  1289. {
  1290. protected:
  1291. LOG_CLASS(HBBackupMatInvCB);
  1292. public:
  1293. HBBackupMatInvCB(const LLUUID& object_id, const LLUUID& mat_id, S32 face,
  1294. const std::string& name, const std::string& buffer)
  1295. : mFace(face),
  1296. mObjectId(object_id),
  1297. mOriginalMatId(mat_id),
  1298. mItemName(name),
  1299. mBuffer(buffer)
  1300. {
  1301. }
  1302. void fire(const LLUUID& inv_item_id) override
  1303. {
  1304. LLViewerInventoryItem* itemp = gInventory.getItem(inv_item_id);
  1305. if (!itemp) return;
  1306. // Hold a pointer on self to avoid getting destroyed on fire() exit.
  1307. mSelf = this;
  1308. // create_inventory_item() does not allow presetting some permissions;
  1309. // fix it now.
  1310. LLPermissions perms;
  1311. perms.init(gAgentID, gAgentID, LLUUID::null, LLUUID::null);
  1312. itemp->setPermissions(perms);
  1313. itemp->updateServer(false);
  1314. gInventory.updateItem(itemp);
  1315. gInventory.notifyObservers();
  1316. if (itemp->getName() != mItemName)
  1317. {
  1318. LLSD updates;
  1319. updates["name"] = mItemName;
  1320. update_inventory_item(inv_item_id, updates, NULL);
  1321. }
  1322. LLResourceUploadInfo::ptr_t infop =
  1323. std::make_shared<LLBufferedAssetUploadInfo>(
  1324. inv_item_id, LLAssetType::AT_MATERIAL, mBuffer,
  1325. boost::bind(&HBBackupMatInvCB::uploadDone, _2, this),
  1326. boost::bind(&HBBackupMatInvCB::uploadFailed, this));
  1327. const std::string& cap_url =
  1328. gAgent.getRegionCapability("UpdateMaterialAgentInventory");
  1329. LLViewerAssetUpload::enqueueInventoryUpload(cap_url, infop);
  1330. }
  1331. // Applies the material asset to the face, once it has been created.
  1332. static void uploadDone(LLUUID asset_id, HBBackupMatInvCB* self)
  1333. {
  1334. // Remember the mapped Id for the original material we recreated.
  1335. HBObjectBackup::sAssetMap.emplace(self->mOriginalMatId, asset_id);
  1336. // Set the recreated material to the object face.
  1337. LLViewerObject* objp = gObjectList.findObject(self->mObjectId);
  1338. if (objp && !objp->isDead())
  1339. {
  1340. objp->setRenderMaterialID(self->mFace, asset_id);
  1341. }
  1342. self->mSelf = NULL; // Commit suicide.
  1343. }
  1344. static void uploadFailed(HBBackupMatInvCB* self)
  1345. {
  1346. self->mSelf = NULL; // Commit suicide.
  1347. }
  1348. private:
  1349. S32 mFace;
  1350. LLPointer<HBBackupMatInvCB> mSelf;
  1351. LLUUID mObjectId;
  1352. LLUUID mOriginalMatId;
  1353. std::string mItemName;
  1354. std::string mBuffer;
  1355. };
  1356. static void create_inventory_mat_item(const LLUUID& obj_id,
  1357. const LLUUID& mat_id,
  1358. S32 te, LLGLTFMaterial& mat)
  1359. {
  1360. if (!gAgent.hasInventoryMaterial())
  1361. {
  1362. return;
  1363. }
  1364. std::string name = "Material ";
  1365. if (mat_id.notNull())
  1366. {
  1367. name += mat_id.asString();
  1368. }
  1369. else
  1370. {
  1371. name += mat.getHash().asString();
  1372. }
  1373. LLSD asset;
  1374. asset["version"] = LLGLTFMaterial::ASSET_VERSION;
  1375. asset["type"] = LLGLTFMaterial::ASSET_TYPE;
  1376. asset["data"] = mat.asJSON();
  1377. std::stringstream buffer;
  1378. LLSDSerialize::serialize(asset, buffer, LLSDSerialize::LLSD_BINARY);
  1379. LLTransactionID tid;
  1380. tid.generate();
  1381. LLUUID parent_id =
  1382. gInventory.findChoosenCategoryUUIDForType(LLFolderType::FT_MATERIAL);
  1383. LLPermissions perms;
  1384. perms.init(gAgentID, gAgentID, LLUUID::null, LLUUID::null);
  1385. LLPointer<LLInventoryCallback> cb = new HBBackupMatInvCB(obj_id, mat_id,
  1386. te, name,
  1387. buffer.str());
  1388. create_inventory_item(parent_id, tid, name, name,
  1389. LLAssetType::AT_MATERIAL,
  1390. LLInventoryType::IT_MATERIAL, NO_INV_SUBTYPE,
  1391. perms.getMaskNextOwner(), cb);
  1392. }
  1393. // This function takes a pointer to a viewer object and applies the prim
  1394. // definition that prim_llsd has
  1395. void HBObjectBackup::xmlToPrim(LLSD prim_llsd, LLViewerObject* objp)
  1396. {
  1397. const LLUUID& id = objp->getID();
  1398. mExpectingUpdate = id;
  1399. gSelectMgr.selectObjectAndFamily(objp);
  1400. if (prim_llsd.has("name"))
  1401. {
  1402. gSelectMgr.selectionSetObjectName(prim_llsd["name"]);
  1403. }
  1404. if (prim_llsd.has("description"))
  1405. {
  1406. gSelectMgr.selectionSetObjectDescription(prim_llsd["description"]);
  1407. }
  1408. if (prim_llsd.has("material"))
  1409. {
  1410. gSelectMgr.selectionSetMaterial(prim_llsd["material"].asInteger());
  1411. }
  1412. if (prim_llsd.has("clickaction"))
  1413. {
  1414. gSelectMgr.selectionSetClickAction(prim_llsd["clickaction"].asInteger());
  1415. }
  1416. if (prim_llsd.has("parent"))
  1417. {
  1418. // We are not the root node.
  1419. LLVector3 pos = LLVector3(prim_llsd["position"]);
  1420. LLQuaternion rot = ll_quaternion_from_sd(prim_llsd["rotation"]);
  1421. objp->setPositionRegion(pos * mRootRot + mRootPos + mGroupOffset);
  1422. objp->setRotation(rot * mRootRot);
  1423. }
  1424. else
  1425. {
  1426. objp->setPositionRegion(mRootPos + mGroupOffset);
  1427. LLQuaternion rot=ll_quaternion_from_sd(prim_llsd["rotation"]);
  1428. objp->setRotation(rot);
  1429. }
  1430. objp->setScale(LLVector3(prim_llsd["scale"]));
  1431. if (prim_llsd.has("flags"))
  1432. {
  1433. U32 flags = (U32)prim_llsd["flags"].asInteger();
  1434. objp->setFlags(flags, true);
  1435. }
  1436. else // Kept for backward compatibility
  1437. {
  1438. if (prim_llsd.has("phantom") && prim_llsd["phantom"].asInteger() == 1)
  1439. {
  1440. objp->setFlags(FLAGS_PHANTOM, true);
  1441. }
  1442. if (prim_llsd.has("physical") &&
  1443. prim_llsd["physical"].asInteger() == 1)
  1444. {
  1445. objp->setFlags(FLAGS_USE_PHYSICS, true);
  1446. }
  1447. }
  1448. if (mGotExtraPhysics && prim_llsd.has("ExtraPhysics"))
  1449. {
  1450. const LLSD& physics = prim_llsd["ExtraPhysics"];
  1451. objp->setPhysicsShapeType(physics["PhysicsShapeType"].asInteger());
  1452. F32 gravity = physics.has("Gravity") ? physics["Gravity"].asReal()
  1453. : physics["GravityMultiplier"].asReal();
  1454. objp->setPhysicsGravity(gravity);
  1455. objp->setPhysicsFriction(physics["Friction"].asReal());
  1456. objp->setPhysicsDensity(physics["Density"].asReal());
  1457. objp->setPhysicsRestitution(physics["Restitution"].asReal());
  1458. objp->updateFlags(true);
  1459. }
  1460. // Volume params
  1461. LLVolumeParams volume_params = objp->getVolume()->getParams();
  1462. volume_params.fromLLSD(prim_llsd["volume"]);
  1463. objp->updateVolume(volume_params);
  1464. if (prim_llsd.has("sculpt"))
  1465. {
  1466. LLSculptParams sculpt;
  1467. sculpt.fromLLSD(prim_llsd["sculpt"]);
  1468. // *TODO: check if map is valid and only set texture if map is valid
  1469. // and changes
  1470. const LLUUID& t_id = sculpt.getSculptTexture();
  1471. rebase_map_t::const_iterator it = sAssetMap.find(t_id);
  1472. if (it != sAssetMap.end())
  1473. {
  1474. sculpt.setSculptTexture(it->second, LL_SCULPT_TYPE_MESH);
  1475. }
  1476. objp->setParameterEntry(LLNetworkData::PARAMS_SCULPT, sculpt, true);
  1477. }
  1478. if (prim_llsd.has("light"))
  1479. {
  1480. LLLightParams light;
  1481. light.fromLLSD(prim_llsd["light"]);
  1482. objp->setParameterEntry(LLNetworkData::PARAMS_LIGHT, light, true);
  1483. }
  1484. if (prim_llsd.has("light_texture"))
  1485. {
  1486. // Light image
  1487. LLLightImageParams lightimg;
  1488. lightimg.fromLLSD(prim_llsd["light_texture"]);
  1489. const LLUUID& t_id = lightimg.getLightTexture();
  1490. rebase_map_t::const_iterator it = sAssetMap.find(t_id);
  1491. if (it != sAssetMap.end())
  1492. {
  1493. lightimg.setLightTexture(it->second);
  1494. }
  1495. objp->setParameterEntry(LLNetworkData::PARAMS_LIGHT_IMAGE, lightimg,
  1496. true);
  1497. }
  1498. if (prim_llsd.has("flexible"))
  1499. {
  1500. LLFlexibleObjectData flex;
  1501. flex.fromLLSD(prim_llsd["flexible"]);
  1502. objp->setParameterEntry(LLNetworkData::PARAMS_FLEXIBLE, flex, true);
  1503. }
  1504. // Reflection probe
  1505. if (prim_llsd.has("probe"))
  1506. {
  1507. LLVOVolume* volp = objp->asVolume();
  1508. if (volp)
  1509. {
  1510. volp->setIsReflectionProbe(true);
  1511. std::string value = prim_llsd["probe"].asString();
  1512. volp->setReflectionProbeIsDynamic(value == "dynamic");
  1513. value = prim_llsd["probe_volume"].asString();
  1514. volp->setReflectionProbeIsBox(value == "box");
  1515. bool mirror = prim_llsd.has("probe_is_mirror") &&
  1516. prim_llsd["probe_is_mirror"].asBoolean();
  1517. if (mirror)
  1518. {
  1519. volp->setReflectionProbeIsMirror(true);
  1520. }
  1521. else
  1522. {
  1523. F32 param = prim_llsd["probe_ambiance"].asReal();
  1524. volp->setReflectionProbeAmbiance(param);
  1525. param = prim_llsd["probe_near_clip"].asReal();
  1526. volp->setReflectionProbeNearClip(param);
  1527. }
  1528. }
  1529. }
  1530. // Textures
  1531. // Check both for "textures" and "texture" since the second (buggy) case
  1532. // has already been seen in some exported prims XML files...
  1533. llinfos << "Processing textures for prim " << id << llendl;
  1534. LLSD te_llsd = prim_llsd.has("textures") ? prim_llsd["textures"]
  1535. : prim_llsd["texture"];
  1536. U8 i = 0;
  1537. for (LLSD::array_iterator it = te_llsd.beginArray();
  1538. it != te_llsd.endArray(); ++it)
  1539. {
  1540. LLSD the_te = *it;
  1541. LLTextureEntry te;
  1542. te.fromLLSD(the_te);
  1543. const LLUUID& t_id = te.getID();
  1544. rebase_map_t::const_iterator tit = sAssetMap.find(t_id);
  1545. if (tit != sAssetMap.end())
  1546. {
  1547. te.setID(tit->second);
  1548. }
  1549. objp->setTE(i++, te);
  1550. }
  1551. llinfos << "Textures done !" << llendl;
  1552. // Legacy materials
  1553. if (prim_llsd.has("materials"))
  1554. {
  1555. llinfos << "Processing legacy materials for prim " << id << llendl;
  1556. te_llsd = prim_llsd["materials"];
  1557. // Note: old export format lacked a texture entry reference and
  1558. // therefore failed to properly export objects with mixed materials and
  1559. // non-materials faces. For these, we just increment the face number
  1560. // (i) for each new material found in the exported data, hoping there
  1561. // will be no "hole"...
  1562. bool missing_te_ref = false;
  1563. i = 0;
  1564. for (LLSD::array_iterator it = te_llsd.beginArray();
  1565. it != te_llsd.endArray(); ++it)
  1566. {
  1567. LLSD the_mat = *it;
  1568. if (the_mat.has("face"))
  1569. {
  1570. S32 te = the_mat["face"].asInteger();
  1571. if (te >= 0 && te < 256) // Paranoia
  1572. {
  1573. i = te;
  1574. }
  1575. else
  1576. {
  1577. llwarns << "Bad face number (" << te
  1578. << "): material skipped." << llendl;
  1579. continue;
  1580. }
  1581. }
  1582. else
  1583. {
  1584. missing_te_ref = true;
  1585. }
  1586. LLMaterialPtr matp = new LLMaterial(the_mat);
  1587. const LLUUID& n_id = matp->getNormalID();
  1588. if (n_id.notNull())
  1589. {
  1590. rebase_map_t::const_iterator tit = sAssetMap.find(n_id);
  1591. if (tit != sAssetMap.end())
  1592. {
  1593. matp->setNormalID(tit->second);
  1594. }
  1595. }
  1596. const LLUUID& s_id = matp->getSpecularID();
  1597. if (s_id.notNull())
  1598. {
  1599. rebase_map_t::const_iterator tit = sAssetMap.find(n_id);
  1600. if (tit != sAssetMap.end())
  1601. {
  1602. matp->setSpecularID(tit->second);
  1603. }
  1604. }
  1605. LLMaterialMgr::getInstance()->put(id, i++, *matp);
  1606. }
  1607. if (missing_te_ref)
  1608. {
  1609. llwarns << "Legacy materials done, but the exported file got missing face number references for them: they have been set in sequence, which only works for objects not mixing materials and non-materials faces."
  1610. << llendl;
  1611. }
  1612. else
  1613. {
  1614. llinfos << "Legacy materials done !" << llendl;
  1615. }
  1616. }
  1617. // PBR (GLTF-encoded) materials
  1618. if (prim_llsd.has("gltf_materials"))
  1619. {
  1620. std::string warnmsg, errmsg;
  1621. llinfos << "Processing PBR materials for prim " << id << llendl;
  1622. te_llsd = prim_llsd["gltf_materials"];
  1623. for (LLSD::array_iterator it = te_llsd.beginArray();
  1624. it != te_llsd.endArray(); ++it)
  1625. {
  1626. LLSD the_mat = *it;
  1627. if (!the_mat.has("face") || !the_mat.has("json"))
  1628. {
  1629. llwarns << "Malformed gltf_materials LLSD entry. Skipping."
  1630. << llendl;
  1631. continue;
  1632. }
  1633. U8 i = the_mat["face"].asInteger();
  1634. LLUUID mat_id;
  1635. if (te_llsd.has("mat_id"))
  1636. {
  1637. mat_id = te_llsd["mat_id"].asUUID();
  1638. if (mat_id.notNull())
  1639. {
  1640. // Check to see if we already created a new material for
  1641. // this saved material Id.
  1642. rebase_map_t::const_iterator tit = sAssetMap.find(mat_id);
  1643. if (tit != sAssetMap.end())
  1644. {
  1645. mat_id = tit->second;
  1646. }
  1647. // Check to see if we have the original material in our
  1648. // inventory. If not, reset Id to null.
  1649. else if (!validateAssetPerms(mat_id, true))
  1650. {
  1651. mat_id.setNull();
  1652. }
  1653. }
  1654. if (mat_id.notNull())
  1655. {
  1656. // We have the corresponding PBR material asset in our
  1657. // inventory already, so simply apply it.
  1658. objp->setRenderMaterialID(i, mat_id);
  1659. continue;
  1660. }
  1661. }
  1662. LLGLTFMaterial mat;
  1663. if (!mat.fromJSON(the_mat["json"], warnmsg, errmsg))
  1664. {
  1665. llwarns << "Failed GLTF from JSON decoding: "
  1666. << (errmsg.empty() ? warnmsg : errmsg) << llendl;
  1667. continue;
  1668. }
  1669. const LLUUID& b_id = mat.getBaseColorId();
  1670. if (b_id.notNull())
  1671. {
  1672. rebase_map_t::const_iterator tit = sAssetMap.find(b_id);
  1673. if (tit != sAssetMap.end())
  1674. {
  1675. mat.setBaseColorId(tit->second);
  1676. }
  1677. }
  1678. const LLUUID& n_id = mat.getNormalId();
  1679. if (n_id.notNull())
  1680. {
  1681. rebase_map_t::const_iterator tit = sAssetMap.find(n_id);
  1682. if (tit != sAssetMap.end())
  1683. {
  1684. mat.setNormalId(tit->second);
  1685. }
  1686. }
  1687. const LLUUID& m_id = mat.getMetallicRoughnessId();
  1688. if (m_id.notNull())
  1689. {
  1690. rebase_map_t::const_iterator tit = sAssetMap.find(m_id);
  1691. if (tit != sAssetMap.end())
  1692. {
  1693. mat.setMetallicRoughnessId(tit->second);
  1694. }
  1695. }
  1696. const LLUUID& e_id = mat.getEmissiveId();
  1697. if (e_id.notNull())
  1698. {
  1699. rebase_map_t::const_iterator tit = sAssetMap.find(e_id);
  1700. if (tit != sAssetMap.end())
  1701. {
  1702. mat.setEmissiveId(tit->second);
  1703. }
  1704. }
  1705. create_inventory_mat_item(id, mat_id, i, mat);
  1706. }
  1707. llinfos << "PBR materials done !" << llendl;
  1708. }
  1709. objp->sendTEUpdate();
  1710. objp->sendShapeUpdate();
  1711. // There is a server bug preventing to update the scale, position and
  1712. // rotation at once...
  1713. static LLCachedControl<bool> multiple_update_bug(gSavedSettings,
  1714. "MultipleUpdateBug");
  1715. if (multiple_update_bug)
  1716. {
  1717. gSelectMgr.sendMultipleUpdate(UPD_SCALE);
  1718. gSelectMgr.sendMultipleUpdate(UPD_POSITION | UPD_ROTATION);
  1719. }
  1720. else
  1721. {
  1722. gSelectMgr.sendMultipleUpdate(UPD_SCALE | UPD_POSITION | UPD_ROTATION);
  1723. }
  1724. gSelectMgr.deselectAll();
  1725. }
  1726. // This is fired when the update packet is processed so we know the prim
  1727. // settings have stuck
  1728. //static
  1729. void HBObjectBackup::primUpdate(LLViewerObject* objp)
  1730. {
  1731. HBObjectBackup* self = findInstance();
  1732. if (!objp || objp->isDead() || !self || !self->mRunning ||
  1733. objp->mID != self->mExpectingUpdate)
  1734. {
  1735. return;
  1736. }
  1737. ++self->mCurPrim;
  1738. self->updateImportNumbers();
  1739. ++self->mPrimImportIter;
  1740. self->mExpectingUpdate.setNull();
  1741. if (self->mPrimImportIter == self->mThisGroup.endMap())
  1742. {
  1743. llinfos << "Trying to link..." << llendl;
  1744. if (self->mToSelect.size() > 1)
  1745. {
  1746. std::reverse(self->mToSelect.begin(), self->mToSelect.end());
  1747. // Now link
  1748. gSelectMgr.deselectAll();
  1749. gSelectMgr.selectObjectAndFamily(self->mToSelect, true);
  1750. gSelectMgr.sendLink();
  1751. LLViewerObject* root = self->mToSelect.back();
  1752. root->setRotation(self->mRootRot);
  1753. }
  1754. ++self->mCurObject;
  1755. ++self->mGroupPrimImportIter;
  1756. if (self->mGroupPrimImportIter != self->mLLSD["data"].endArray())
  1757. {
  1758. self->importNextObject();
  1759. return;
  1760. }
  1761. self->mRunning = false;
  1762. self->destroy();
  1763. return;
  1764. }
  1765. LLSD prim_llsd = self->mThisGroup[self->mPrimImportIter->first];
  1766. if (self->mToSelect.empty())
  1767. {
  1768. llwarns << "error: ran out of objects to mod." << llendl;
  1769. self->mRunning = false;
  1770. self->destroy();
  1771. return;
  1772. }
  1773. if (self->mPrimImportIter != self->mThisGroup.endMap())
  1774. {
  1775. //rezAgentOffset(LLVector3(1.f, 0.f, 0.f));
  1776. LLSD prim_llsd =self-> mThisGroup[self->mPrimImportIter->first];
  1777. ++self->mProcessIter;
  1778. self->xmlToPrim(prim_llsd, *(self->mProcessIter));
  1779. }
  1780. }
  1781. // Callback when we rez a new object when the importer is running.
  1782. //static
  1783. void HBObjectBackup::newPrim(LLViewerObject* objp)
  1784. {
  1785. HBObjectBackup* self = findInstance();
  1786. if (!objp || objp->isDead() || !self || !self->mRunning) return;
  1787. ++self->mRezCount;
  1788. self->mToSelect.push_back(objp);
  1789. self->updateImportNumbers();
  1790. ++self->mPrimImportIter;
  1791. objp->setPositionLocal(self->offsetAgent(LLVector3(0.f, 1.f, 0.f)));
  1792. gSelectMgr.sendMultipleUpdate(UPD_POSITION);
  1793. if (self->mPrimImportIter != self->mThisGroup.endMap())
  1794. {
  1795. self->rezAgentOffset(LLVector3(1.f, 0.f ,0.f));
  1796. }
  1797. else
  1798. {
  1799. llinfos << "All prims rezzed, moving to build stage" << llendl;
  1800. // Deselecting is required to ensure that the first child prim in
  1801. // the link set (which is also the last rezzed prim and thus
  1802. // currently selected) will be properly renamed and desced.
  1803. gSelectMgr.deselectAll();
  1804. self->mPrimImportIter = self->mThisGroup.beginMap();
  1805. LLSD prim_llsd = self->mThisGroup[self->mPrimImportIter->first];
  1806. self->mProcessIter = self->mToSelect.begin();
  1807. self->xmlToPrim(prim_llsd, *(self->mProcessIter));
  1808. }
  1809. }
  1810. void HBObjectBackup::updateMap(const LLUUID& uploaded_asset)
  1811. {
  1812. if (mCurrentAsset.notNull())
  1813. {
  1814. llinfos << "Mapping " << mCurrentAsset << " to " << uploaded_asset
  1815. << llendl;
  1816. sAssetMap.emplace(mCurrentAsset, uploaded_asset);
  1817. }
  1818. }
  1819. void HBObjectBackup::uploadNextAsset()
  1820. {
  1821. if (gAgent.getRegionCapability("NewFileAgentInventory").empty() &&
  1822. !mTexturesList.empty())
  1823. {
  1824. llwarns << "NewAgentInventory capability not found. Cannot upload !"
  1825. << llendl;
  1826. mTexturesList.clear();
  1827. }
  1828. if (mTexturesList.empty())
  1829. {
  1830. llinfos << "Texture list is empty, moving to rez stage." << llendl;
  1831. mCurrentAsset.setNull();
  1832. importFirstObject();
  1833. return;
  1834. }
  1835. updateImportNumbers();
  1836. uuid_list_t::iterator iter = mTexturesList.begin();
  1837. LLUUID id = *iter;
  1838. mTexturesList.erase(iter);
  1839. llinfos << "Got texture ID " << id << ": trying to upload..." << llendl;
  1840. mCurrentAsset = id;
  1841. std::string struid;
  1842. id.toString(struid);
  1843. std::string filename = mFolder + struid;
  1844. LLAssetID uuid;
  1845. LLTransactionID tid;
  1846. // Generate a new transaction ID for this asset
  1847. tid.generate();
  1848. uuid = tid.makeAssetID(gAgent.getSecureSessionID());
  1849. S64 file_size;
  1850. LLFile infile(filename, "rb", &file_size);
  1851. if (!infile)
  1852. {
  1853. llwarns << "Unable to access input file " << filename << llendl;
  1854. uploadNextAsset();
  1855. return;
  1856. }
  1857. constexpr S32 buf_size = 65536;
  1858. U8 copy_buf[buf_size];
  1859. LLFileSystem file(uuid, LLFileSystem::APPEND);
  1860. while ((file_size = infile.read(copy_buf, buf_size)))
  1861. {
  1862. file.write(copy_buf, file_size);
  1863. }
  1864. S32 upload_cost = LLEconomy::getInstance()->getTextureUploadCost();
  1865. LLResourceUploadInfo::ptr_t
  1866. info(new LLResourceUploadInfo(tid, LLAssetType::AT_TEXTURE,
  1867. struid, struid, 0,
  1868. LLFolderType::FT_TEXTURE,
  1869. LLInventoryType::IT_TEXTURE,
  1870. LLFloaterPerms::getNextOwnerPerms(),
  1871. LLFloaterPerms::getGroupPerms(),
  1872. LLFloaterPerms::getEveryonePerms(),
  1873. upload_cost));
  1874. info->setCapCallback(uploadNextAssetCallback, NULL);
  1875. info->setShowInventoryPanel(false);
  1876. upload_new_resource(info);
  1877. }
  1878. // Recursively calls uploadNextAsset()... *TODO: turn the whole import process
  1879. // into an idle callback worker, like for the export one...
  1880. //static
  1881. void HBObjectBackup::uploadNextAssetCallback(const LLSD& result, void*)
  1882. {
  1883. HBObjectBackup* self = HBObjectBackup::findInstance();
  1884. if (self)
  1885. {
  1886. self->updateMap(result["new_asset"].asUUID());
  1887. self->uploadNextAsset();
  1888. }
  1889. else
  1890. {
  1891. llwarns << "Import aborted, HBObjectBackup instance gone !" << llendl;
  1892. }
  1893. }