summaryrefslogtreecommitdiff
path: root/indra/llappearanceutility/llbakingwearablesdata.cpp
blob: 18edb8634807da1aeb7d2bac0b279bf46b773b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
 * @file llbakingwearablesdata.cpp
 * @brief Implementation of LLBakingWearablesData class
 *
 * $LicenseInfo:firstyear=2012&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2012, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */


#include "linden_common.h"

#include "llbakingwearablesdata.h"
#include "llbakingwearable.h"

using namespace LLAvatarAppearanceDefines;

LLBakingWearablesData::LLBakingWearablesData()
{
}

// virtual
LLBakingWearablesData::~LLBakingWearablesData()
{
    std::for_each(mWearables.begin(), mWearables.end(), DeletePointer());
    mWearables.clear();
}

void LLBakingWearablesData::setWearableOutfit(LLSD& sd)
{
    LLSD::array_const_iterator type_iter = sd.beginArray();
    LLSD::array_const_iterator type_end = sd.endArray();
    S32 wearable_index = 0;
    for (; type_iter != type_end && wearable_index < (S32) LLWearableType::WT_COUNT; ++type_iter, ++wearable_index)
    {
        LLSD::array_const_iterator wearable_iter = type_iter->beginArray();
        LLSD::array_const_iterator wearable_end = type_iter->endArray();
        for (; wearable_iter != wearable_end; ++wearable_iter)
        {
            if ( wearable_iter->isDefined() )
            {
                LLBakingWearable* wearable = new LLBakingWearable();
                std::istringstream istr( (*wearable_iter)["contents"].asString() );
                wearable->importStream(istr, mAvatarAppearance);

                // Sanity check the wearable type.
                if (wearable->getType() != wearable_index)
                {
                    LL_WARNS() << "Unexpected wearable type!  Expected " << wearable_index
                            << ", processed " << (S32) wearable->getType() << LL_ENDL;
                    // *TODO: Throw exception?
                    delete wearable;
                    continue;
                }
                mWearables.push_back(wearable);

                const LLWearableType::EType type = wearable->getType();
                if (wearable->getAssetType() == LLAssetType::AT_BODYPART)
                {
                    // exactly one wearable per body part
                    setWearable(type,0,wearable);
                }
                else
                {
                    pushWearable(type,wearable);
                }
            }
        }
    }

    wearable_list_t::const_iterator wearable_iter = mWearables.begin();
    wearable_list_t::const_iterator wearable_end  = mWearables.end();
    for (; wearable_iter != wearable_end; ++wearable_iter)
    {
        LLBakingWearable* wearable = (*wearable_iter);
        const bool removed = false;
        wearableUpdated(wearable, removed);
    }

    for(wearable_index = 0; wearable_index < (S32) LLWearableType::WT_COUNT; wearable_index++)
    {
        LLWearable* top_wearable = getTopWearable((LLWearableType::EType)wearable_index);
        if (top_wearable)
        {
            top_wearable->writeToAvatar(mAvatarAppearance);
        }
    }
}


void LLBakingWearablesData::asLLSD(LLSD& sd) const
{
    sd = LLSD::emptyMap();
    wearableentry_map_t::const_iterator type_iter = mWearableDatas.begin();
    wearableentry_map_t::const_iterator type_end  = mWearableDatas.end();
    for (; type_iter != type_end; ++type_iter)
    {
        LLSD wearable_type_sd = LLSD::emptyArray();
        LLWearableType::EType wearable_type = type_iter->first;
        wearableentry_vec_t::const_iterator wearable_iter = type_iter->second.begin();
        wearableentry_vec_t::const_iterator wearable_end  = type_iter->second.end();
        for (; wearable_iter != wearable_end; ++wearable_iter)
        {
            const LLBakingWearable* wearable = dynamic_cast<LLBakingWearable*>(*wearable_iter);
            LLSD wearable_sd;
            wearable->asLLSD(wearable_sd);
            wearable_type_sd.append(wearable_sd);
        }
        sd[LLWearableType::getInstance()->getTypeName(wearable_type)] = wearable_type_sd;
    }
}