summaryrefslogtreecommitdiff
path: root/src/resources/emotedb.cpp
blob: a7130d5883f41e338368b77d86a22a3f96878616 (plain) (blame)
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
132
133
134
135
136
137
138
139
140
141
142
/*
 *  Emote database
 *  Copyright (C) 2009  Aethyra Development Team
 *  Copyright (C) 2009-2013  The Mana Developers
 *
 *  This file is part of The Mana Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "resources/emotedb.h"

#include "log.h"
#include "imagesprite.h"

#include "resources/resourcemanager.h"
#include "resources/imageset.h"

#include <algorithm>
#include <vector>

namespace
{
    std::vector<Emote> mEmotes;
    Emote mUnknown;
    bool mLoaded = false;
}

void EmoteDB::init()
{
    if (mLoaded)
        unload();

    mUnknown.name = "unknown";
    mUnknown.effectId = -1;
    mUnknown.sprite = std::make_unique<ImageSprite>(
        ResourceManager::getInstance()->getImageRef("graphics/sprites/error.png"));
}

void EmoteDB::readEmoteNode(xmlNodePtr node, const std::string &filename)
{
    const int id = XML::getProperty(node, "id", -1);
    if (id == -1)
    {
        logger->log("Emote Database: Emote with missing ID in %s!", filename.c_str());
        return;
    }

    Emote emote;

    emote.id = id;
    emote.name = XML::getProperty(node, "name", "unknown");
    emote.effectId = XML::getProperty(node, "effectid", -1);

    if (emote.effectId == -1)
    {
        logger->log("Emote Database: Warning: Emote %s has no attached effect in %s!",
                    emote.name.c_str(), filename.c_str());
        return;
    }

    const std::string imageName = XML::getProperty(node, "image", "");
    const int width = XML::getProperty(node, "width", 0);
    const int height = XML::getProperty(node, "height", 0);

    if (imageName.empty() || width <= 0 || height <= 0)
    {
        logger->log("Emote Database: Warning: Emote %s has bad imageset values in %s",
                    emote.name.c_str(), filename.c_str());
        return;
    }

    emote.is = ResourceManager::getInstance()->getImageSet(imageName,
                                                           width,
                                                           height);
    emote.is->decRef(); // clear automatic reference

    if (!emote.is || emote.is->size() == 0)
    {
        logger->log("Emote Database: Error loading imageset for emote %s in %s",
                    emote.name.c_str(), filename.c_str());
        return;
    }

    // For now we just use the first image in the animation
    emote.sprite = std::make_unique<ImageSprite>(emote.is->get(0));

    mEmotes.push_back(std::move(emote));
}

void EmoteDB::checkStatus()
{
    mLoaded = true;
}

void EmoteDB::unload()
{
    for (auto &emote : mEmotes)
        emote.sprite->releaseImageRef();

    mEmotes.clear();

    if (mUnknown.sprite)
        mUnknown.sprite->releaseImageRef();

    mLoaded = false;
}

const Emote &EmoteDB::get(int id)
{
    auto i = std::find_if(mEmotes.begin(), mEmotes.end(),
                          [id](const Emote &e) { return e.id == id; });

    if (i == mEmotes.end())
    {
        logger->log("EmoteDB: Warning, unknown emote ID %d requested", id);
        return mUnknown;
    }

    return *i;
}

const Emote &EmoteDB::getByIndex(int index)
{
    return mEmotes.at(index);
}

int EmoteDB::getEmoteCount()
{
    return mEmotes.size();
}