Fix classique titles display
[ryzomcore.git] / ryzom / client / src / string_manager_client.h
blobef55b036780f2f17f79734d288d44d9fd582b08a
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2017 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program 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 Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #ifndef STRING_MANAGER_CLIENT_H
24 #define STRING_MANAGER_CLIENT_H
26 #include "nel/misc/static_map.h"
27 #include "nel/misc/ucstring.h"
28 #include "nel/misc/bit_mem_stream.h"
29 #include "nel/misc/i18n.h"
30 #include "nel/misc/sheet_id.h"
31 #include "nel/misc/diff_tool.h"
32 #include "game_share/brick_families.h"
33 #include "game_share/skills.h"
36 namespace STRING_MANAGER
39 class IStringWaiterRemover;
40 class IStringWaitCallback;
42 class CStringManagerClient
44 friend class IStringWaiterRemover;
45 friend class IStringWaitCallback;
46 public:
47 // Singleton pattern implementation
48 static CStringManagerClient *instance();
49 static bool hasInstance() { return _Instance; }
50 static void release(bool mustReleaseStaticArrays);
52 /** Prepare the string manager to use a persistent string cache.
53 * There is one cache file for each language and for each encountered shard.
55 void initCache(const std::string &shardId, const std::string &languageCode);
56 /** Clear the current string table and load the content of the cache file.
57 * This method is called after receiving the impulse RELOAD_CACHE from
58 * IOS.
59 * If the received timestamp and the file timestamp differ, the file cache
60 * is reseted.
62 void loadCache(uint32 timestamp);
63 bool isCacheLoaded() {return _CacheLoaded;};
64 // Force the cache to be saved
65 void flushStringCache();
67 bool getString(uint32 stringId, std::string &result);
68 void waitString(uint32 stringId, const IStringWaiterRemover *premover, std::string *result);
69 void waitString(uint32 stringId, IStringWaitCallback *pcallback);
70 bool getDynString(uint32 dynStringId, std::string &result);
71 void waitDynString(uint32 stringId, const IStringWaiterRemover *premover, std::string *result);
72 void waitDynString(uint32 stringId, IStringWaitCallback *pcallback);
74 void receiveString(uint32 stringId, const std::string &str);
75 void receiveDynString(NLMISC::CBitMemStream &bms);
77 void releaseDynString(uint32 stringId);
79 // Yoyo: Append to the I18N some words (skills, bricks...)
80 static void initI18NSpecialWords(const std::string &languageCode);
81 static void specialWordsMemoryCompress();
82 // Yoyo: Replace the Brick Name with Filled stats (CSBrickManager work). No-Op if not found
83 static void replaceSBrickName(NLMISC::CSheetId id, const std::string &name, const std::string &desc, const std::string &desc2);
84 static void replaceDynString(const std::string &name, const std::string &text);
86 // Get the Localized Name of the Places.
87 static const char *getPlaceLocalizedName(const std::string &placeNameID);
88 // Get the Localized Name of the faction (for the fame)
89 static const char *getFactionLocalizedName(const std::string &factionNameID);
90 // Get the Localized Name of the Skill.
91 static const char *getSkillLocalizedName(SKILLS::ESkills e);
92 // Get the Localized Name of the Item.
93 static const char *getItemLocalizedName(NLMISC::CSheetId id);
94 // Get the Localized Name of the Creature.
95 static const char *getCreatureLocalizedName(NLMISC::CSheetId id);
96 // Get the Localized Name of the SBrick.
97 static const char *getSBrickLocalizedName(NLMISC::CSheetId id);
98 // Get the Localized Name of the SPhrase.
99 static const char *getSPhraseLocalizedName(NLMISC::CSheetId id);
101 // Get the Localized Description of the Skill.
102 static const char *getSkillLocalizedDescription(SKILLS::ESkills e);
103 // Get the Localized Descriptionof the Item.
104 static const char *getItemLocalizedDescription(NLMISC::CSheetId id);
105 // Get the Localized Description of the SBrick.
106 static const char *getSBrickLocalizedDescription(NLMISC::CSheetId id);
107 // Get the Localized Composition Description of the SBrick.
108 static const char *getSBrickLocalizedCompositionDescription(NLMISC::CSheetId id);
109 // Get the Localized Description of the SPhrase.
110 static const char *getSPhraseLocalizedDescription(NLMISC::CSheetId id);
112 // Get the Localized Title name
113 static const char *getTitleLocalizedName(const std::string &titleId, bool women);
114 static const char *getLocalizedName(const std::string &text);
115 static std::vector<std::string> getTitleInfos(const std::string &titleId, bool women);
117 // Get the Localized name of a classification type
118 static const char *getClassificationTypeLocalizedName(EGSPD::CClassificationType::TClassificationType type);
120 // Outpost name
121 static const char *getOutpostLocalizedName(NLMISC::CSheetId id);
122 // Outpost description
123 static const char *getOutpostLocalizedDescription(NLMISC::CSheetId id);
124 // Outpost building name
125 static const char *getOutpostBuildingLocalizedName(NLMISC::CSheetId id);
126 // Outpost building description
127 static const char *getOutpostBuildingLocalizedDescription(NLMISC::CSheetId id);
128 // Squad name
129 static const char *getSquadLocalizedName(NLMISC::CSheetId id);
130 // Squad description
131 static const char *getSquadLocalizedDescription(NLMISC::CSheetId id);
133 private:
134 // constructor.
135 CStringManagerClient();
136 // destructor.
137 ~CStringManagerClient();
139 /// A string waiter is destoyed, remove any waiting string reference from it.
140 void removeStringWaiter(const IStringWaiterRemover *remover);
141 /// A string wait callback is destoyed, remove any waiting string reference from it.
142 void removeStringWaiter(const IStringWaitCallback *callback);
144 enum TParamType
146 string_id,
147 integer,
148 time,
149 money,
150 dyn_string_id,
151 sheet_id
154 struct TParamValue
156 TParamType Type;
157 std::string::size_type ReplacementPoint;
158 union
160 uint32 StringId;
161 sint32 Integer;
162 uint32 Time;
163 uint64 Money;
164 uint32 DynStringId;
168 struct TDynStringInfo
170 enum TStatus
172 received,
173 serialized,
174 complete
176 TStatus Status;
177 NLMISC::CBitMemStream Message;
178 uint32 StringId;
179 std::vector<TParamValue> Params;
180 std::string String;
183 enum
185 EmptyStringId= 0,
186 EmptyDynStringId= 0xffffffff
189 struct TStringWaiter
191 /// Pointer to the utf-8 string to fill
192 std::string *Result;
193 /// Pointer to the remover that contains this string reference
194 const IStringWaiterRemover *Remover;
197 bool buildDynString(TDynStringInfo &dynInfo);
200 /// Container for simple strings
201 typedef CHashMap<uint, std::string> TStringsContainer;
202 /// Container for dyn strings
203 typedef CHashMap<uint, TDynStringInfo> TDynStringsContainer;
204 /// Container of string reference waiting for value.
205 typedef CHashMultiMap<uint, TStringWaiter> TStringWaitersContainer;
206 /// Container of string reference to string callback object.
207 typedef CHashMultiMap<uint, IStringWaitCallback*> TStringCallbacksContainer;
210 TStringsContainer _ReceivedStrings;
211 CHashSet<uint> _WaitingStrings;
213 TDynStringsContainer _ReceivedDynStrings;
214 TDynStringsContainer _WaitingDynStrings;
216 /// String waiting the string value from the server.
217 TStringWaitersContainer _StringsWaiters;
218 // String waiting the dyn string value from the server.
219 TStringWaitersContainer _DynStringsWaiters;
221 // Callback for string value from the server
222 TStringCallbacksContainer _StringsCallbacks;
223 // Callback for dyn string value from the server
224 TStringCallbacksContainer _DynStringsCallbacks;
226 // Return value for waiting string..
227 static std::string _WaitString;
229 // Singleton pattern implementation
230 static CStringManagerClient *_Instance;
232 //@{
233 //\name Cache management
234 /// Flag for cache management initialisation done.
235 bool _CacheInited;
236 /// Shard id is used to identify the cache file to use.
237 std::string _ShardId;
238 /// Language code is used to identify the cache file to use.
239 std::string _LanguageCode;
240 /// Timestamp (unix date) of the corrently loaded cache file.
241 uint32 _Timestamp;
242 /// Fullpath name of the current cache file.
243 std::string _CacheFilename;
244 /// Flag to incate that the cache is loaded.
245 bool _CacheLoaded;
246 /// Waiting string to be saved in cache
247 struct CCacheString
249 uint32 StringId;
250 std::string String;
252 std::vector<CCacheString> _CacheStringToSave;
253 //@}
255 // SpecialItems.
256 class CItem
258 public:
259 // The Name of the item
260 std::string Name;
261 // The Women Name of the item
262 std::string WomenName;
263 // Description of the item
264 std::string Desc;
265 // Optional Second description (For SBrick composition for example)
266 std::string Desc2;
268 void serial(NLMISC::IStream &f)
270 sint ver = f.serialVersion(2);
271 if (ver >= 2)
273 f.serial(Name);
274 f.serial(WomenName);
275 f.serial(Desc);
276 f.serial(Desc2);
278 else
280 nlassert(f.isReading());
281 ucstring name; // Old UTF-16 serial
282 ucstring womenName; // Old UTF-16 serial
283 ucstring desc; // Old UTF-16 serial
284 ucstring desc2; // Old UTF-16 serial
285 f.serial(name);
286 if (ver >= 1)
287 f.serial(womenName);
288 f.serial(desc);
289 f.serial(desc2);
290 Name = name.toUtf8();
291 WomenName = womenName.toUtf8();
292 Desc = desc.toUtf8();
293 Desc2 = desc2.toUtf8();
298 static bool _SpecItem_MemoryCompressed;
300 static std::map<std::string, CItem> _SpecItem_TempMap;
301 static std::vector<std::string> _TitleWords;
302 static std::map<std::string, std::string> _DynStrings;
305 static char *_SpecItem_Labels;
306 static char *_SpecItem_NameDesc;
307 struct CItemLight
309 const char *Label;
310 const char *Name;
311 const char *WomenName;
312 const char *Desc;
313 const char *Desc2;
315 struct CItemLightComp
317 bool operator()(const CItemLight &x, const CItemLight &y) const
319 return strcmp(x.Label, y.Label) < 0;
323 static std::vector<CItemLight> _SpecItems;
325 static const char *getSpecialWord(const std::string &label, bool women = false);
326 static const char *getSpecialDesc(const std::string &label);
327 static const char *getSpecialDesc2(const std::string &label);
329 // Check Files for the Packed string.
330 class CFileCheck
332 public:
333 uint32 ReferenceDate;
334 uint32 AdditionDate;
336 CFileCheck()
338 ReferenceDate= 0;
339 AdditionDate= 0;
342 bool operator==(const CFileCheck &o) const
344 return ReferenceDate==o.ReferenceDate && AdditionDate==o.AdditionDate;
347 void serial(NLMISC::IStream &f)
349 f.serialVersion(0);
350 f.serial(ReferenceDate);
351 f.serial(AdditionDate);
355 // The Header for the Packed String Client
356 class CPackHeader
358 public:
359 // The language code used
360 std::string LanguageCode;
361 // The Modification Dates of each word files
362 std::vector<CFileCheck> FileChecks;
363 // If must rebuild the packed version for any reason.
364 uint32 PackedVersion;
366 void serial(NLMISC::IStream &f)
368 f.serialCheck(NELID("_RTS"));
369 f.serialCheck(NELID("KCAP"));
370 f.serialVersion(0);
371 f.serial(PackedVersion);
372 f.serial(LanguageCode);
373 f.serialCont(FileChecks);
377 // return true if OK (same).
378 static bool checkWordFileDates(std::vector<CFileCheck> &fileChecks, const std::vector<std::string> &fileNames, const std::string &languageCode);
383 /** Class that want to register to wait for string
384 * need to derive from this class.
386 class IStringWaiterRemover
388 public:
389 virtual ~IStringWaiterRemover()
391 // signal the string manager that this waiter is destroyed
392 if (CStringManagerClient::hasInstance())
393 CStringManagerClient::instance()->removeStringWaiter(this);
397 /** Implement this class if you want to wait for
398 * string to be delivered.
400 class IStringWaitCallback
402 public:
403 /// Overide this method to receive callback for string.
404 virtual void onStringAvailable(uint /* stringId */, const std::string &/* value */) {}
405 /// Overide this method to receive callback for dynamic string.
406 virtual void onDynStringAvailable(uint /* stringId */, const std::string &/* value */) {}
408 virtual ~IStringWaitCallback()
410 // signal the string manager that this waiter is destroyed
411 if (CStringManagerClient::hasInstance())
412 CStringManagerClient::instance()->removeStringWaiter(this);
418 /** A proxy file loader for CI18N
419 * The implementation will check if there is any
420 * new content in the translation/work directory of ryzom
421 * then overwrite the value found in data/gamedev/language
423 class CLoadProxy : public NLMISC::CI18N::ILoadProxy, public TStringDiff::IDiffCallback
425 void loadStringFile(const std::string &filename, ucstring &text); // TODO: UTF-8 (serial)
427 void onEquivalent(uint addIndex, uint refIndex, TStringDiffContext &context);
428 void onAdd(uint addIndex, uint refIndex, TStringDiffContext &context);
429 void onRemove(uint addIndex, uint refIndex, TStringDiffContext &context);
430 void onChanged(uint addIndex, uint refIndex, TStringDiffContext &context);
431 void onSwap(uint newIndex, uint refIndex, TStringDiffContext &context);
435 } // namespace STRING_MANAGER
437 #endif //STRING_MANAGER_CLIENT_H