ChannelText.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <common/ChannelText.h>
00022 #include <lang/Lang.h>
00023 
00024 ChannelText::ChannelText() : 
00025         srcPlayerId_(0), destPlayerId_(0),
00026         flags_(0)
00027 {
00028 }
00029 
00030 ChannelText::ChannelText(
00031         const std::string &channel, 
00032         const LangString &message) : 
00033         srcPlayerId_(0), destPlayerId_(0),
00034         channel_(channel),
00035         message_(message),
00036         flags_(0)
00037 {
00038 }
00039 
00040 ChannelText::ChannelText(
00041         const std::string &channel, 
00042         const std::string &key,
00043         const std::string &value) :
00044         srcPlayerId_(0), destPlayerId_(0),
00045         channel_(channel),
00046         messageValue_(value),
00047         flags_(0),
00048         messageKey_(key)
00049 {
00050 }
00051 
00052 ChannelText::ChannelText(
00053         const std::string &channel, 
00054         const std::string &key,
00055         const std::string &value,
00056         const LangStringConverter &param1) :
00057         srcPlayerId_(0), destPlayerId_(0),
00058         channel_(channel),
00059         messageValue_(value),
00060         flags_(0),
00061         messageKey_(key)
00062 {
00063         messageParams_.push_back(param1.getValue());
00064 }
00065 
00066 ChannelText::ChannelText(
00067         const std::string &channel, 
00068         const std::string &key,
00069         const std::string &value,
00070         const LangStringConverter &param1,
00071         const LangStringConverter &param2) :
00072         srcPlayerId_(0), destPlayerId_(0),
00073         channel_(channel),
00074         messageValue_(value),
00075         flags_(0),
00076         messageKey_(key)
00077 {
00078         messageParams_.push_back(param1.getValue());
00079         messageParams_.push_back(param2.getValue());
00080 }
00081 
00082 ChannelText::ChannelText(
00083         const std::string &channel, 
00084         const std::string &key,
00085         const std::string &value,
00086         const LangStringConverter &param1,
00087         const LangStringConverter &param2,
00088         const LangStringConverter &param3) :
00089         srcPlayerId_(0), destPlayerId_(0),
00090         channel_(channel),
00091         messageValue_(value),
00092         flags_(0),
00093         messageKey_(key)
00094 {
00095         messageParams_.push_back(param1.getValue());
00096         messageParams_.push_back(param2.getValue());
00097         messageParams_.push_back(param3.getValue());
00098 }
00099 
00100 ChannelText::ChannelText(
00101         const std::string &channel, 
00102         const std::string &key,
00103         const std::string &value,
00104         const LangStringConverter &param1,
00105         const LangStringConverter &param2,
00106         const LangStringConverter &param3,
00107         const LangStringConverter &param4) :
00108         srcPlayerId_(0), destPlayerId_(0),
00109         channel_(channel),
00110         messageValue_(value),
00111         flags_(0),
00112         messageKey_(key)
00113 {
00114         messageParams_.push_back(param1.getValue());
00115         messageParams_.push_back(param2.getValue());
00116         messageParams_.push_back(param3.getValue());
00117         messageParams_.push_back(param4.getValue());
00118 }
00119 
00120 const LangString &ChannelText::getMessage() 
00121 { 
00122         if (message_.empty() && !messageKey_.empty())
00123         {
00124                 ResourceBundleEntry *entry = 
00125                         Lang::instance()->getEntry(messageKey_, messageValue_);
00126                 switch (messageParams_.size())
00127                 {
00128                 case 1:
00129                         message_ = entry->getString(messageParams_[0]);
00130                         break;
00131                 case 2:
00132                         message_ = entry->getString(messageParams_[0], 
00133                                 messageParams_[1]);
00134                         break;
00135                 case 3:
00136                         message_ = entry->getString(messageParams_[0], 
00137                                 messageParams_[1], messageParams_[2]);
00138                         break;
00139                 case 4:
00140                         message_ = entry->getString(messageParams_[0], 
00141                                 messageParams_[1], messageParams_[2], messageParams_[3]);
00142                         break;
00143                 default:
00144                         message_ = entry->getString();
00145                         break;
00146                 }
00147         }
00148 
00149         return message_; 
00150 }
00151 
00152 bool ChannelText::writeMessage(NetBuffer &buffer)
00153 {
00154         buffer.addToBuffer(channel_);
00155         buffer.addToBuffer(messageKey_.empty()?message_:LangString());
00156         buffer.addToBuffer(srcPlayerId_);
00157         buffer.addToBuffer(destPlayerId_);
00158         buffer.addToBuffer(admin_);
00159         buffer.addToBuffer(flags_);
00160         buffer.addToBuffer(messageKey_);
00161         if (!messageKey_.empty())
00162         {
00163                 buffer.addToBuffer(messageValue_);
00164                 buffer.addToBuffer((int) messageParams_.size());
00165                 std::vector<LangString>::iterator itor;
00166                 for (itor = messageParams_.begin();
00167                         itor != messageParams_.end();
00168                         itor++)
00169                 {
00170                         buffer.addToBuffer(*itor);
00171                 }
00172         }
00173 
00174         return true;
00175 }
00176 
00177 bool ChannelText::readMessage(NetBufferReader &reader)
00178 {
00179         if (!reader.getFromBuffer(channel_)) return false;
00180         if (!reader.getFromBuffer(message_)) return false;
00181         if (!reader.getFromBuffer(srcPlayerId_)) return false;
00182         if (!reader.getFromBuffer(destPlayerId_)) return false;
00183         if (!reader.getFromBuffer(admin_)) return false;
00184         if (!reader.getFromBuffer(flags_)) return false;
00185         if (!reader.getFromBuffer(messageKey_)) return false;
00186         if (!messageKey_.empty())
00187         {
00188                 if (!reader.getFromBuffer(messageValue_)) return false;
00189                 int paramSize = 0;
00190                 if (!reader.getFromBuffer(paramSize)) return false;
00191                 for (int p=0; p<paramSize; p++)
00192                 {
00193                         LangString param;
00194                         if (!reader.getFromBuffer(param)) return false;
00195                         messageParams_.push_back(param);
00196                 }
00197         }
00198 
00199         return true;
00200 }
00201 
00202 ChannelDefinition::ChannelDefinition(const char *c, unsigned int t) :
00203         channel_(c), type_(t)
00204 {
00205 }

Generated on Mon Feb 16 15:14:48 2009 for Scorched3D by  doxygen 1.5.3