Translated using Weblate (Italian)
[oweals/minetest.git] / src / texture_override.cpp
1 /*
2 Minetest
3 Copyright (C) 2020 Hugues Ross <hugues.ross@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "texture_override.h"
21
22 #include "log.h"
23 #include "util/string.h"
24 #include <algorithm>
25 #include <fstream>
26
27 TextureOverrideSource::TextureOverrideSource(std::string filepath)
28 {
29         std::ifstream infile(filepath.c_str());
30         std::string line;
31         int line_index = 0;
32         while (std::getline(infile, line)) {
33                 line_index++;
34
35                 // Also trim '\r' on DOS-style files
36                 line = trim(line);
37
38                 // Ignore empty lines and comments
39                 if (line.empty() || line[0] == '#')
40                         continue;
41
42                 std::vector<std::string> splitted = str_split(line, ' ');
43                 if (splitted.size() != 3) {
44                         warningstream << filepath << ":" << line_index
45                                         << " Syntax error in texture override \"" << line
46                                         << "\": Expected 3 arguments, got " << splitted.size()
47                                         << std::endl;
48                         continue;
49                 }
50
51                 TextureOverride texture_override = {};
52                 texture_override.id = splitted[0];
53                 texture_override.texture = splitted[2];
54
55                 // Parse the target mask
56                 std::vector<std::string> targets = str_split(splitted[1], ',');
57                 for (const std::string &target : targets) {
58                         if (target == "top")
59                                 texture_override.target |= static_cast<u8>(OverrideTarget::TOP);
60                         else if (target == "bottom")
61                                 texture_override.target |= static_cast<u8>(OverrideTarget::BOTTOM);
62                         else if (target == "left")
63                                 texture_override.target |= static_cast<u8>(OverrideTarget::LEFT);
64                         else if (target == "right")
65                                 texture_override.target |= static_cast<u8>(OverrideTarget::RIGHT);
66                         else if (target == "front")
67                                 texture_override.target |= static_cast<u8>(OverrideTarget::FRONT);
68                         else if (target == "back")
69                                 texture_override.target |= static_cast<u8>(OverrideTarget::BACK);
70                         else if (target == "inventory")
71                                 texture_override.target |= static_cast<u8>(OverrideTarget::INVENTORY);
72                         else if (target == "wield")
73                                 texture_override.target |= static_cast<u8>(OverrideTarget::WIELD);
74                         else if (target == "sides")
75                                 texture_override.target |= static_cast<u8>(OverrideTarget::SIDES);
76                         else if (target == "all" || target == "*")
77                                 texture_override.target |= static_cast<u8>(OverrideTarget::ALL_FACES);
78                         else {
79                                 // Report invalid target
80                                 warningstream << filepath << ":" << line_index
81                                                 << " Syntax error in texture override \"" << line
82                                                 << "\": Unknown target \"" << target << "\""
83                                                 << std::endl;
84                         }
85                 }
86
87                 // If there are no valid targets, skip adding this override
88                 if (texture_override.target == static_cast<u8>(OverrideTarget::INVALID)) {
89                         continue;
90                 }
91
92                 m_overrides.push_back(texture_override);
93         }
94 }
95
96 //! Get all overrides that apply to item definitions
97 std::vector<TextureOverride> TextureOverrideSource::getItemTextureOverrides()
98 {
99         std::vector<TextureOverride> found_overrides;
100
101         for (const TextureOverride &texture_override : m_overrides) {
102                 if (texture_override.hasTarget(OverrideTarget::ITEM_TARGETS))
103                         found_overrides.push_back(texture_override);
104         }
105
106         return found_overrides;
107 }
108
109 //! Get all overrides that apply to node definitions
110 std::vector<TextureOverride> TextureOverrideSource::getNodeTileOverrides()
111 {
112         std::vector<TextureOverride> found_overrides;
113
114         for (const TextureOverride &texture_override : m_overrides) {
115                 if (texture_override.hasTarget(OverrideTarget::ALL_FACES))
116                         found_overrides.push_back(texture_override);
117         }
118
119         return found_overrides;
120 }