File: MetaString.h

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (144 lines) | stat: -rw-r--r-- 4,538 bytes parent folder | download
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
143
144
/*
 * MetaString.h, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#pragma once

VCMI_LIB_NAMESPACE_BEGIN

class JsonNode;
class ArtifactID;
class CreatureID;
class CStackBasicDescriptor;
class JsonSerializeFormat;
class MapObjectID;
class MapObjectSubID;
class PlayerColor;
class SecondarySkill;
class SpellID;
class FactionID;
class GameResID;
using TQuantity = si32;

/// Strings classes that can be used as replacement in MetaString
enum class EMetaText : uint8_t
{
	GENERAL_TXT = 1,
	ARRAY_TXT,
	ADVOB_TXT,
	JK_TXT
};

/// Class for string formatting tools that also support transfer over network with localization using language of local install
/// Can be used to compose resulting text from multiple line segments and with placeholder replacement
class DLL_LINKAGE MetaString
{
private:
	enum class EMessage : uint8_t
	{
		APPEND_RAW_STRING,
		APPEND_LOCAL_STRING,
		APPEND_TEXTID_STRING,
		APPEND_NUMBER,
		REPLACE_RAW_STRING,
		REPLACE_LOCAL_STRING,
		REPLACE_TEXTID_STRING,
		REPLACE_NUMBER,
		REPLACE_POSITIVE_NUMBER,
		APPEND_EOL
	};

	std::vector<EMessage> message;

	std::vector<std::pair<EMetaText,ui32> > localStrings;
	std::vector<std::string> exactStrings;
	std::vector<std::string> stringsTextID;
	std::vector<int64_t> numbers;

	std::string getLocalString(const std::pair<EMetaText, ui32> & txt) const;

public:
	/// Creates MetaString and appends provided raw string to it
	static MetaString createFromRawString(const std::string & value);
	/// Creates MetaString and appends provided text ID string to it
	static MetaString createFromTextID(const std::string & value);

	/// Appends local string to resulting string
	void appendLocalString(EMetaText type, ui32 serial);
	/// Appends raw string, without translation to resulting string
	void appendRawString(const std::string & value);
	/// Appends text ID that will be translated in output
	void appendTextID(const std::string & value);
	/// Appends specified number to resulting string
	void appendNumber(int64_t value);

	void appendName(const ArtifactID& id);
	void appendName(const SpellID& id);
	void appendName(const PlayerColor& id);
	void appendName(const CreatureID & id, TQuantity count);
	void appendName(const GameResID& id);
	void appendNameSingular(const CreatureID & id);
	void appendNamePlural(const CreatureID & id);
	void appendEOL();

	/// Replaces first '%s' placeholder in string with specified local string
	void replaceLocalString(EMetaText type, ui32 serial);
	/// Replaces first '%s' placeholder in string with specified fixed, untranslated string
	void replaceRawString(const std::string & txt);
	/// Replaces first '%s' placeholder with string ID that will be translated in output
	void replaceTextID(const std::string & value);
	/// Replaces first '%d' placeholder in string with specified number
	void replaceNumber(int64_t txt);
	/// Replaces first '%+d' placeholder in string with specified number using '+' sign as prefix
	void replacePositiveNumber(int64_t txt);

	void replaceName(const ArtifactID & id);
	void replaceName(const FactionID& id);
	void replaceName(const MapObjectID & id, const MapObjectSubID & subId);
	void replaceName(const PlayerColor& id);
	void replaceName(const SecondarySkill& id);
	void replaceName(const SpellID& id);
	void replaceName(const GameResID& id);

	/// Replaces first '%s' placeholder with singular or plural name depending on creatures count
	void replaceName(const CreatureID & id, TQuantity count);
	void replaceNameSingular(const CreatureID & id);
	void replaceNamePlural(const CreatureID & id);
	/// Replaces first '%s' placeholder with singular or plural name depending on creatures count
	void replaceName(const CStackBasicDescriptor & stack);

	/// erases any existing content in the string
	void clear();

	///used to handle loot from creature bank
	std::string buildList() const;

	/// Convert all stored values into a single, user-readable string
	std::string toString() const;

	/// Returns true if current string is empty
	bool empty() const;

	bool operator == (const MetaString & other) const;

	void jsonSerialize(JsonNode & dest) const;
	void jsonDeserialize(const JsonNode & dest);
	
	void serializeJson(JsonSerializeFormat & handler);

	template <typename Handler> void serialize(Handler & h)
	{
		h & exactStrings;
		h & localStrings;
		h & stringsTextID;
		h & message;
		h & numbers;
	}
};

VCMI_LIB_NAMESPACE_END