1 /*
2 * Copyright (C) 2011-2016 OpenDungeons Team
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "creatureskill/CreatureSkillManager.h"
19
20 #include "creatureskill/CreatureSkill.h"
21 #include "utils/Helper.h"
22 #include "utils/LogManager.h"
23
24 #include <istream>
25 #include <vector>
26
27 namespace
28 {
getFactories()29 static std::vector<const CreatureSkillFactory*>& getFactories()
30 {
31 static std::vector<const CreatureSkillFactory*> factory;
32 return factory;
33 }
34 }
35
registerFactory(const CreatureSkillFactory * factory)36 void CreatureSkillManager::registerFactory(const CreatureSkillFactory* factory)
37 {
38 std::vector<const CreatureSkillFactory*>& factories = getFactories();
39 factories.push_back(factory);
40 }
41
unregisterFactory(const CreatureSkillFactory * factory)42 void CreatureSkillManager::unregisterFactory(const CreatureSkillFactory* factory)
43 {
44 std::vector<const CreatureSkillFactory*>& factories = getFactories();
45 auto it = std::find(factories.begin(), factories.end(), factory);
46 if(it == factories.end())
47 {
48 OD_LOG_ERR("Trying to unregister unknown factory=" + factory->getCreatureSkillName());
49 return;
50 }
51 factories.erase(it);
52 }
53
clone(const CreatureSkill * skill)54 CreatureSkill* CreatureSkillManager::clone(const CreatureSkill* skill)
55 {
56 return skill->clone();
57 }
58
load(std::istream & is)59 CreatureSkill* CreatureSkillManager::load(std::istream& is)
60 {
61 if(!is.good())
62 return nullptr;
63
64 std::vector<const CreatureSkillFactory*>& factories = getFactories();
65 std::string nextParam;
66 OD_ASSERT_TRUE(is >> nextParam);
67 const CreatureSkillFactory* factoryToUse = nullptr;
68 for(const CreatureSkillFactory* factory : factories)
69 {
70 if(factory == nullptr)
71 continue;
72
73 if(factory->getCreatureSkillName().compare(nextParam) != 0)
74 continue;
75
76 factoryToUse = factory;
77 break;
78 }
79
80 if(factoryToUse == nullptr)
81 {
82 OD_LOG_ERR("Unknown Skill=" + nextParam);
83 return nullptr;
84 }
85
86 CreatureSkill* skill = factoryToUse->createCreatureSkill();
87 if(!skill->importFromStream(is))
88 {
89 OD_LOG_ERR("Couldn't load creature Skill=" + nextParam);
90 delete skill;
91 return nullptr;
92 }
93
94 return skill;
95 }
96
dispose(const CreatureSkill * skill)97 void CreatureSkillManager::dispose(const CreatureSkill* skill)
98 {
99 delete skill;
100 }
101
write(const CreatureSkill & skill,std::ostream & os)102 void CreatureSkillManager::write(const CreatureSkill& skill, std::ostream& os)
103 {
104 os << skill.getSkillName();
105 skill.exportToStream(os);
106 }
107
getFormatString(const CreatureSkill & skill,std::string & format)108 void CreatureSkillManager::getFormatString(const CreatureSkill& skill, std::string& format)
109 {
110 format = "# SkillName";
111 skill.getFormatString(format);
112 }
113
areEqual(const CreatureSkill & skill1,const CreatureSkill & skill2)114 bool CreatureSkillManager::areEqual(const CreatureSkill& skill1, const CreatureSkill& skill2)
115 {
116 return skill1.isEqual(skill2);
117 }
118