summaryrefslogtreecommitdiff
path: root/src/texture_override.cpp
blob: 10d129b875322457588d899b20a43bdc8cff9d0f (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
/*
Minetest
Copyright (C) 2020 Hugues Ross <hugues.ross@gmail.com>

This program 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; either version 2.1 of the License, or
(at your option) 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "texture_override.h"

#include "log.h"
#include "util/string.h"
#include <algorithm>
#include <fstream>

TextureOverrideSource::TextureOverrideSource(std::string filepath)
{
	std::ifstream infile(filepath.c_str());
	std::string line;
	int line_index = 0;
	while (std::getline(infile, line)) {
		line_index++;

		// Also trim '\r' on DOS-style files
		line = trim(line);

		// Ignore empty lines and comments
		if (line.empty() || line[0] == '#')
			continue;

		std::vector<std::string> splitted = str_split(line, ' ');
		if (splitted.size() != 3) {
			warningstream << filepath << ":" << line_index
					<< " Syntax error in texture override \"" << line
					<< "\": Expected 3 arguments, got " << splitted.size()
					<< std::endl;
			continue;
		}

		TextureOverride texture_override = {};
		texture_override.id = splitted[0];
		texture_override.texture = splitted[2];

		// Parse the target mask
		std::vector<std::string> targets = str_split(splitted[1], ',');
		for (const std::string &target : targets) {
			if (target == "top")
				texture_override.target |= static_cast<u8>(OverrideTarget::TOP);
			else if (target == "bottom")
				texture_override.target |= static_cast<u8>(OverrideTarget::BOTTOM);
			else if (target == "left")
				texture_override.target |= static_cast<u8>(OverrideTarget::LEFT);
			else if (target == "right")
				texture_override.target |= static_cast<u8>(OverrideTarget::RIGHT);
			else if (target == "front")
				texture_override.target |= static_cast<u8>(OverrideTarget::FRONT);
			else if (target == "back")
				texture_override.target |= static_cast<u8>(OverrideTarget::BACK);
			else if (target == "inventory")
				texture_override.target |= static_cast<u8>(OverrideTarget::INVENTORY);
			else if (target == "wield")
				texture_override.target |= static_cast<u8>(OverrideTarget::WIELD);
			else if (target == "sides")
				texture_override.target |= static_cast<u8>(OverrideTarget::SIDES);
			else if (target == "all" || target == "*")
				texture_override.target |= static_cast<u8>(OverrideTarget::ALL_FACES);
			else {
				// Report invalid target
				warningstream << filepath << ":" << line_index
						<< " Syntax error in texture override \"" << line
						<< "\": Unknown target \"" << target << "\""
						<< std::endl;
			}
		}

		// If there are no valid targets, skip adding this override
		if (texture_override.target == static_cast<u8>(OverrideTarget::INVALID)) {
			continue;
		}

		m_overrides.push_back(texture_override);
	}
}

//! Get all overrides that apply to item definitions
std::vector<TextureOverride> TextureOverrideSource::getItemTextureOverrides()
{
	std::vector<TextureOverride> found_overrides;

	for (const TextureOverride &texture_override : m_overrides) {
		if (texture_override.hasTarget(OverrideTarget::ITEM_TARGETS))
			found_overrides.push_back(texture_override);
	}

	return found_overrides;
}

//! Get all overrides that apply to node definitions
std::vector<TextureOverride> TextureOverrideSource::getNodeTileOverrides()
{
	std::vector<TextureOverride> found_overrides;

	for (const TextureOverride &texture_override : m_overrides) {
		if (texture_override.hasTarget(OverrideTarget::ALL_FACES))
			found_overrides.push_back(texture_override);
	}

	return found_overrides;
}