Linux multi-monitor fullscreen support
[ryzomcore.git] / ryzom / common / src / game_share / string_manager_sender.cpp
blob83336ecf3747836d1654c859df304bc1e86d1330
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdpch.h"
21 #include "string_manager_sender.h"
22 #include "nel/net/unified_network.h"
23 #include "nel/net/message.h"
24 #include "nel/net/service.h"
25 #include "nel/net/service.h"
26 #include "nel/misc/string_conversion.h"
27 #include "nel/misc/variable.h"
28 #include "utils.h"
29 #include "synchronised_message.h"
31 using namespace std;
32 using namespace NLMISC;
34 NLMISC::CVariable<bool> DebugPhraseSender("debug", "DebugPhraseSender", "Send debug phrase message to the IOS", false, 0, true);
36 namespace STRING_MANAGER
39 void TParam::serial(NLMISC::IStream &f)
41 serialParam(true, f, invalid_value);
44 bool TParam::serialParam(bool debug, NLMISC::IStream & f,TParamType type)
46 bool ret = true;
47 if (debug)
49 TParamType checkType = NB_PARAM_TYPES;
52 if (!f.isReading() && type != invalid_value)
54 nlassert(Type == type);
55 checkType = Type;
58 f.serialEnum(checkType);
60 if (f.isReading() && type != invalid_value)
62 if (type != checkType)
64 nlwarning("Serializing a string TParam : need a '%s', receive a '%s' !",
65 paramTypeToString(type).c_str(),
66 paramTypeToString(checkType).c_str());
67 ret = false;
69 Type = checkType;
72 else
74 if ( f.isReading() )
75 Type = type;
78 switch (Type)
80 case item:
81 case creature_model:
82 case sphrase:
83 case sbrick:
84 case outpost:
85 // sheetId
86 f.serial(const_cast<NLMISC::CSheetId&>(SheetId));
87 break;
88 case creature:
89 case guild:
90 case player:
91 case bot:
92 case entity:
93 // entity
94 f.serial(const_cast<NLMISC::CEntityId&>(EId));
95 f.serial(_AIAlias);
96 break;
98 case place:
99 case bot_name:
100 case title:
101 case event_faction:
102 // identifier (string)
103 f.serial(const_cast<std::string &>(Identifier));
104 break;
106 case skill:
107 case role:
108 // case career:
109 // case job:
110 case ecosystem:
111 case race:
112 case faction:
113 case compass:
114 case body_part:
115 case score:
116 case characteristic:
117 case damage_type:
118 case power_type:
119 case classification_type:
120 case clan:
121 // enum
122 f.serial(const_cast<uint32&>(Enum));
123 break;
124 case integer:
125 // integer.
126 f.serial(const_cast<sint32&>(Int));
127 break;
128 case time:
129 // time
130 f.serial(const_cast<uint32&>(Time));
131 break;
132 case money:
133 f.serial(const_cast<uint64&>(Money));
134 break;
135 case string_id:
136 case dyn_string_id:
137 f.serial(const_cast<uint32&>(StringId));
138 break;
139 case literal:
140 f.serial(Literal);
141 break;
142 case self:
143 nlwarning("Invalid parameter type : can't be self");
144 return false;
145 default:
146 nlwarning("Invalid parameter type : unknown (%d)", Type);
147 return false;
149 return ret;
152 static uint32 SequenceCounter = 0;
154 uint32 pickStringSerialNumber()
156 SequenceCounter = (SequenceCounter+1) & 0xffffff ;
157 if (SequenceCounter == 0)
158 SequenceCounter++;
160 uint32 stringSeq = SequenceCounter++;
162 stringSeq = (NLNET::IService::getInstance()->getServiceId().get() << 24) | (stringSeq & 0xffffff);
164 return stringSeq;
169 * Generic method used to send localised texts to clients ( whether they are ingame or in the character creation process )
170 * this method is inline and declared there so that it cannot be accessed from elsewhere.
171 * Use sendStringToClient or sendStringToUser
173 inline uint32 sendString(NLNET::CMessage & msg, const std::string &stringName, const std::vector<TParam> &params, ISender *sender)
175 uint32 stringSeq = pickStringSerialNumber();
177 msg.serial(stringSeq);
178 msg.serial(const_cast<std::string&>(stringName));
180 for (uint i=0; i<params.size(); ++i)
182 const TParam &param = params[i];
183 if (!((TParam&)param).serialParam(DebugPhraseSender, msg, param.Type) )
184 return 0xFFFFFFFF;
187 if (sender == NULL)
189 // send the message through network.
190 // NLNET::CUnifiedNetwork::getInstance()->send("IOS", msg);
191 sendMessageViaMirror("IOS", msg);
193 else
195 // send message with sender interface
196 sender->send(msg, DebugPhraseSender);
199 return stringSeq;
202 //-----------------------------------------------
203 // setPhrase
204 //-----------------------------------------------
205 void setPhrase(const std::string &phraseName, const ucstring &phraseContent)
207 NLNET::CMessage msg("SET_PHRASE");
208 std::string mutablePhraseName = phraseName;
209 ucstring mutablePhraseContent = phraseContent;
210 msg.serial(mutablePhraseName);
211 msg.serial(mutablePhraseContent);
212 sendMessageViaMirror("IOS", msg);
215 //-----------------------------------------------
216 // sendStringToClient
217 //-----------------------------------------------
218 uint32 sendStringToClient(const TDataSetRow &clientRowId, const std::string &stringName, const std::vector<TParam> &params, ISender *sender)
220 BOMB_IF(stringName.empty(), "Trying to send a phrase to IOS with an empty phraseName", return 0;);
221 if (DebugPhraseSender)
223 NLNET::CMessage msg("PHRASE_DEBUG");
224 msg.serial(const_cast<TDataSetRow &>(clientRowId));
225 return sendString( msg,stringName,params,sender );
227 else
229 NLNET::CMessage msg("PHRASE");
230 msg.serial(const_cast<TDataSetRow &>(clientRowId));
231 return sendString( msg,stringName,params,sender );
235 void sendSystemStringToClientAudience(const TDataSetRow &clientRowId, const std::vector<NLMISC::CEntityId> &excluded, CChatGroup::TGroupType audience, const char * stringName, const std::vector<TParam> &params, ISender *sender)
237 BOMB_IF(strlen(stringName) == 0, "Trying to send a phrase to IOS with an empty stringName", return;);
238 if (DebugPhraseSender)
240 NLNET::CMessage msg("BROADCAST_SYSTEM_PHRASE_DEBUG");
241 msg.serial(const_cast<TDataSetRow &>(clientRowId));
242 msg.serialCont(const_cast<vector<CEntityId> &>(excluded));
243 msg.serialEnum(audience);
244 sendString( msg,stringName,params,sender );
246 else
248 NLNET::CMessage msg("BROADCAST_SYSTEM_PHRASE");
249 msg.serial(const_cast<TDataSetRow &>(clientRowId));
250 msg.serialCont(const_cast<vector<CEntityId> &>(excluded));
251 msg.serialEnum(audience);
252 sendString( msg,stringName,params,sender );
256 void sendCustomEmoteTextToClientAudience(const TDataSetRow &clientRowId, const std::vector<NLMISC::CEntityId> &excluded, const char * stringName, const std::vector<STRING_MANAGER::TParam> &params, ISender *sender)
258 BOMB_IF(strlen(stringName) == 0, "Trying to send a custom emot phrase to IOS with an empty stringName", return;);
260 // send crowd emote message to IOS
261 NLNET::CMessage msgout2("EMOTE_CROWD");
262 std::string stringNameStr(stringName);
263 msgout2.serial( const_cast<TDataSetRow&>(clientRowId) );
264 msgout2.serial(const_cast<string&>(stringNameStr));
266 uint32 size = (uint32)params.size();
267 msgout2.serial(size);
268 for ( uint i = 0; i < size; i++ )
270 uint8 type8 = (uint8)params[i].Type;
271 msgout2.serial(type8);
272 const_cast<STRING_MANAGER::TParam&>(params[i]).serialParam( false, msgout2, params[i].Type );
274 msgout2.serialCont(const_cast<std::vector<NLMISC::CEntityId> &>(excluded));
275 sendMessageViaMirror("IOS", msgout2);
280 //-----------------------------------------------
281 // sendStringToUser
282 //-----------------------------------------------
283 uint32 sendStringToUser(uint32 userId, const std::string &stringName, const std::vector<TParam> &params, ISender *sender)
285 BOMB_IF(stringName.empty(), "Trying to send a phrase to IOS with an empty phraseName", return 0;);
286 if (DebugPhraseSender)
288 NLNET::CMessage msg("PHRASE_USER_DEBUG");
289 msg.serial(userId);
290 return sendString( msg,stringName,params,sender );
292 else
294 NLNET::CMessage msg("PHRASE_USER");
295 msg.serial(userId);
296 return sendString( msg,stringName,params,sender );
300 NL_BEGIN_STRING_CONVERSION_TABLE (TParamType)
301 NL_STRING_CONVERSION_TABLE_ENTRY( item )
302 NL_STRING_CONVERSION_TABLE_ENTRY( place )
303 NL_STRING_CONVERSION_TABLE_ENTRY( creature )
304 NL_STRING_CONVERSION_TABLE_ENTRY( skill )
305 NL_STRING_CONVERSION_TABLE_ENTRY( role )
306 // NL_STRING_CONVERSION_TABLE_ENTRY( career )
307 // NL_STRING_CONVERSION_TABLE_ENTRY( job )
308 NL_STRING_CONVERSION_TABLE_ENTRY( ecosystem )
309 NL_STRING_CONVERSION_TABLE_ENTRY( race )
310 NL_STRING_CONVERSION_TABLE_ENTRY( sbrick )
311 NL_STRING_CONVERSION_TABLE_ENTRY( faction )
312 NL_STRING_CONVERSION_TABLE_ENTRY( guild )
313 NL_STRING_CONVERSION_TABLE_ENTRY( player )
314 NL_STRING_CONVERSION_TABLE_ENTRY( bot )
315 NL_STRING_CONVERSION_TABLE_ENTRY( integer )
316 NL_STRING_CONVERSION_TABLE_ENTRY( time )
317 NL_STRING_CONVERSION_TABLE_ENTRY( money )
318 NL_STRING_CONVERSION_TABLE_ENTRY( compass )
319 NL_STRING_CONVERSION_TABLE_ENTRY( string_id )
320 NL_STRING_CONVERSION_TABLE_ENTRY( dyn_string_id )
321 NL_STRING_CONVERSION_TABLE_ENTRY( self )
322 NL_STRING_CONVERSION_TABLE_ENTRY( creature_model )
323 NL_STRING_CONVERSION_TABLE_ENTRY( entity )
324 NL_STRING_CONVERSION_TABLE_ENTRY( body_part )
325 NL_STRING_CONVERSION_TABLE_ENTRY( score )
326 NL_STRING_CONVERSION_TABLE_ENTRY( sphrase )
327 NL_STRING_CONVERSION_TABLE_ENTRY( characteristic )
328 NL_STRING_CONVERSION_TABLE_ENTRY( damage_type )
329 NL_STRING_CONVERSION_TABLE_ENTRY( bot_name)
330 NL_STRING_CONVERSION_TABLE_ENTRY( power_type )
331 NL_STRING_CONVERSION_TABLE_ENTRY( literal )
332 NL_STRING_CONVERSION_TABLE_ENTRY( title )
333 NL_STRING_CONVERSION_TABLE_ENTRY( event_faction )
334 NL_STRING_CONVERSION_TABLE_ENTRY( classification_type )
335 NL_STRING_CONVERSION_TABLE_ENTRY( outpost )
336 NL_STRING_CONVERSION_TABLE_ENTRY( clan )
337 NL_END_STRING_CONVERSION_TABLE(TParamType, ParamTypeConversion, NB_PARAM_TYPES)
339 //-----------------------------------------------
340 // stringToParamType
341 //-----------------------------------------------
342 TParamType stringToParamType( const std::string & str )
344 return ParamTypeConversion.fromString( str );
347 //-----------------------------------------------
348 // stringToParamType
349 //-----------------------------------------------
350 const std::string & paramTypeToString( TParamType type )
352 return ParamTypeConversion.toString( type );
356 void TParam::setEId(const NLMISC::CEntityId& eId)
358 EId = eId;
359 _AIAlias = 0;
362 NLMISC::CEntityId TParam::getEId() const
364 return EId;
367 TAIAlias TParam::getAIAlias() const
369 return _AIAlias;
372 void TParam::setAIAlias(TAIAlias aiAlias)
374 _AIAlias = aiAlias;
375 EId = CEntityId::Unknown;
378 void TParam::setEIdAIAlias(const NLMISC::CEntityId& eId, TAIAlias aiAlias)
380 _AIAlias = aiAlias;
381 EId = eId;