1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2017 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
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
;
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
59 * If the received timestamp and the file timestamp differ, the file cache
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
);
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
);
129 static const char *getSquadLocalizedName(NLMISC::CSheetId id
);
131 static const char *getSquadLocalizedDescription(NLMISC::CSheetId id
);
135 CStringManagerClient();
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
);
157 std::string::size_type ReplacementPoint
;
168 struct TDynStringInfo
177 NLMISC::CBitMemStream Message
;
179 std::vector
<TParamValue
> Params
;
186 EmptyDynStringId
= 0xffffffff
191 /// Pointer to the utf-8 string to fill
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
;
233 //\name Cache management
234 /// Flag for cache management initialisation done.
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.
242 /// Fullpath name of the current cache file.
243 std::string _CacheFilename
;
244 /// Flag to incate that the cache is loaded.
246 /// Waiting string to be saved in cache
252 std::vector
<CCacheString
> _CacheStringToSave
;
259 // The Name of the item
261 // The Women Name of the item
262 std::string WomenName
;
263 // Description of the item
265 // Optional Second description (For SBrick composition for example)
268 void serial(NLMISC::IStream
&f
)
270 sint ver
= f
.serialVersion(2);
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
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
;
311 const char *WomenName
;
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.
333 uint32 ReferenceDate
;
342 bool operator==(const CFileCheck
&o
) const
344 return ReferenceDate
==o
.ReferenceDate
&& AdditionDate
==o
.AdditionDate
;
347 void serial(NLMISC::IStream
&f
)
350 f
.serial(ReferenceDate
);
351 f
.serial(AdditionDate
);
355 // The Header for the Packed String Client
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"));
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
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
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