xoreos  0.0.5
locstring.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* See BioWare's own specs released for Neverwinter Nights modding
26  * (<https://github.com/xoreos/xoreos-docs/tree/master/specs/bioware>)
27  */
28 
29 #include "src/common/util.h"
30 #include "src/common/scopedptr.h"
32 #include "src/common/encoding.h"
33 
34 #include "src/aurora/locstring.h"
35 #include "src/aurora/aurorafile.h"
36 #include "src/aurora/talkman.h"
37 
38 namespace Aurora {
39 
41 }
42 
44 }
45 
48 
49  _strings.clear();
50 }
51 
52 bool LocString::empty() const {
53  return (_id == kStrRefInvalid) && (_strings.empty() || getString().empty());
54 }
55 
57  SWAP(_id, str._id);
58 
59  _strings.swap(str._strings);
60 }
61 
63  return _strings.size();
64 }
65 
67  return _id;
68 }
69 
71  _id = id;
72 }
73 
74 bool LocString::hasString(Language language, LanguageGender gender) const {
75  return hasString(LangMan.getLanguageID(language, gender));
76 }
77 
78 bool LocString::hasString(uint32 languageID) const {
79  return _strings.find(languageID) != _strings.end();
80 }
81 
83  return getString(LangMan.getLanguageID(language, gender));
84 }
85 
86 static const Common::UString kEmpty;
87 const Common::UString &LocString::getString(uint32 languageID) const {
88  StringMap::const_iterator s = _strings.find(languageID);
89  if (s == _strings.end())
90  return kEmpty;
91 
92  return s->second;
93 }
94 
95 void LocString::setString(Language language, LanguageGender gender, const Common::UString &str) {
96  return setString(LangMan.getLanguageID(language, gender), str);
97 }
98 
99 void LocString::setString(Language language, const Common::UString &str) {
100  setString(language, kLanguageGenderMale , str);
101  setString(language, kLanguageGenderFemale, str);
102 }
103 
104 void LocString::setString(uint32 languageID, const Common::UString &str) {
105  _strings[languageID] = str;
106 }
107 
109  if (_id == kStrRefInvalid)
110  return kEmpty;
111 
112  return TalkMan.getString(_id);
113 }
114 
116  if (_strings.empty())
117  return getStrRefString();
118 
119  return _strings.begin()->second;
120 }
121 
123  uint32 languageID = LangMan.getLanguageID(LangMan.getCurrentLanguageText(), LangMan.getCurrentGender());
124 
125  // Look whether we have an internal localized string
126  if (hasString(languageID))
127  return getString(languageID);
128 
129  // Try the differently gendered internal string
130  if (hasString(LangMan.swapLanguageGender(languageID)))
131  return getString(LangMan.swapLanguageGender(languageID));
132 
133  // Next, try the external localized one
134  const Common::UString &refString = getStrRefString();
135  if (!refString.empty())
136  return refString;
137 
138  // If all else fails, just get the first one available
139  return getFirstString();
140 }
141 
143  uint32 length = stream.readUint32LE();
144 
145  std::pair<StringMap::iterator, bool> s = _strings.insert(std::make_pair(languageID, ""));
146  if (length == 0)
147  return;
148 
149  s.first->second = "[???]";
150 
152  Common::ScopedPtr<Common::MemoryReadStream> parsed(LangMan.preParseColorCodes(*data));
153 
154  Common::Encoding encoding = LangMan.getEncodingLocString(LangMan.getLanguageGendered(languageID));
155  if (encoding != Common::kEncodingInvalid)
156  s.first->second = Common::readString(*parsed, encoding);
157 }
158 
160  uint32 languageID = stream.readUint32LE();
161 
162  readString(languageID, stream);
163 }
164 
166  _id = id;
167 
168  while (count-- > 0)
169  readLocSubString(stream);
170 }
171 
173  uint32 id = stream.readUint32LE();
174  uint32 count = stream.readUint32LE();
175 
176  readLocString(stream, id, count);
177 }
178 
179 uint32 LocString::getWrittenSize(bool withNullTerminate) const {
180  uint32 size = 0;
181  for (StringMap::const_iterator iter = _strings.begin(); iter != _strings.end() ; iter++) {
182  size += (*iter).second.size();
183 
184  if (withNullTerminate)
185  size += 1;
186 
187  size += 8;
188  }
189 
190  return size;
191 }
192 
193 void LocString::writeLocString(Common::WriteStream &stream, bool withNullTerminate) const {
194  for (StringMap::const_iterator iter = _strings.begin(); iter != _strings.end() ; iter++) {
195  stream.writeUint32LE((*iter).first);
196  stream.writeUint32LE((*iter).second.size());
197  stream.write((*iter).second.c_str(), (*iter).second.size());
198  if (withNullTerminate)
199  stream.writeByte(0);
200  }
201 }
202 
203 } // End of namespace Aurora
LanguageGender
Definition: language.h:67
StringMap _strings
Definition: locstring.h:103
uint32 readUint32LE()
Read an unsigned 32-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:133
#define TalkMan
Shortcut for accessing the talk manager.
Definition: talkman.h:111
void setString(Language language, LanguageGender gender, const Common::UString &str)
Set the string of that language.
Definition: locstring.cpp:95
A class holding an UTF-8 string.
Definition: ustring.h:48
A localized string.
Definition: locstring.h:43
MemoryReadStream * readStream(size_t dataSize)
Read the specified amount of data into a new[]&#39;ed buffer which then is wrapped into a MemoryReadStrea...
Definition: readstream.cpp:67
void readLocString(Common::SeekableReadStream &stream, uint32 id, uint32 count)
Read a LocString out of a stream.
Definition: locstring.cpp:165
const Common::UString & getFirstString() const
Get the first available string.
Definition: locstring.cpp:115
uint32 getID() const
Return the string ID / StrRef.
Definition: locstring.cpp:66
Implementing the reading stream interfaces for plain memory blocks.
uint32 getWrittenSize(bool withNullTerminate=false) const
Get the size, the string table will consume after being written.
Definition: locstring.cpp:179
const Common::UString & getStrRefString() const
Get the string the StrRef points to.
Definition: locstring.cpp:108
Language
Definition: language.h:46
A simple scoped smart pointer template.
void setID(uint32 id)
Set the string ID / StrRef.
Definition: locstring.cpp:70
Utility templates and functions.
void swap(LocString &str)
Swap the contents of the LocString with this LocString&#39;s.
Definition: locstring.cpp:56
Base for BioWare&#39;s Aurora engine files.
bool hasString(Language language, LanguageGender gender=kLanguageGenderCurrent) const
Does the LocString have a string of this language?
Definition: locstring.cpp:74
static const uint32 kStrRefInvalid
Definition: types.h:444
Utility functions for working with differing string encodings.
Encoding
Definition: encoding.h:37
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
virtual size_t write(const void *dataPtr, size_t dataSize)=0
Write data into the stream.
void writeByte(byte value)
Definition: writestream.h:88
Handling BioWare&#39;s localized strings.
Generic interface for a writable data stream.
Definition: writestream.h:64
uint32 _id
The string&#39;s ID / StrRef.
Definition: locstring.h:101
uint32 getNumStrings() const
Get the number of strings.
Definition: locstring.cpp:62
const Common::UString & getString() const
Try to get the most appropriate string.
Definition: locstring.cpp:122
#define LangMan
Shortcut for accessing the language manager.
Definition: language.h:275
void readLocSubString(Common::SeekableReadStream &stream)
Read a LocSubString (substring of a LocString in game data) out of a stream.
Definition: locstring.cpp:159
uint32_t uint32
Definition: types.h:204
bool empty() const
Is this localized string empty, without any strings whatsoever?
Definition: locstring.cpp:52
The global talk manager for Aurora strings.
UString readString(SeekableReadStream &stream, Encoding encoding)
Read a string with the given encoding of a stream.
Definition: encoding.cpp:287
void readString(uint32 languageID, Common::SeekableReadStream &stream)
Read a string out of a stream.
Definition: locstring.cpp:142
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void writeUint32LE(uint32 value)
Definition: writestream.h:104
void writeLocString(Common::WriteStream &stream, bool withNullTerminate=false) const
Write the LocString to a write stream.
Definition: locstring.cpp:193
void SWAP(T &a, T &b)
Template method which swaps the values of its two parameters.
Definition: util.h:78
static const Common::UString kEmpty
Definition: 2dafile.cpp:119