[2938] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
[7490] | 23 | * Oliver Scheuss <scheusso [at] orxonox.net>, (C) 2010 |
---|
[2938] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[7490] | 29 | #include "FunctionCall.h" |
---|
[2938] | 30 | |
---|
| 31 | #include <cassert> |
---|
[3214] | 32 | #include "util/MultiType.h" |
---|
[7490] | 33 | #include "NetworkFunction.h" |
---|
[2938] | 34 | |
---|
| 35 | namespace orxonox { |
---|
[6417] | 36 | |
---|
[7490] | 37 | FunctionCall::FunctionCall() |
---|
| 38 | : nrOfArguments_(-1), objectID_(OBJECTID_UNKNOWN), size_(0) |
---|
[2938] | 39 | { |
---|
| 40 | } |
---|
| 41 | |
---|
[7490] | 42 | FunctionCall::~FunctionCall() |
---|
[2938] | 43 | { |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
[7490] | 47 | bool FunctionCall::execute(){ |
---|
| 48 | if( this->bIsStatic_ ) |
---|
[2938] | 49 | { |
---|
[7490] | 50 | NetworkFunctionStatic *fct = NetworkFunctionStatic::getFunction( this->functionID_ ); |
---|
| 51 | assert( this->nrOfArguments_==this->arguments_.size() ); |
---|
| 52 | switch(this->nrOfArguments_) |
---|
[2938] | 53 | { |
---|
[7490] | 54 | case 0: |
---|
| 55 | fct->call(); |
---|
| 56 | break; |
---|
| 57 | case 1: |
---|
| 58 | fct->call(this->arguments_[0]); |
---|
| 59 | break; |
---|
| 60 | case 2: |
---|
| 61 | fct->call(this->arguments_[0], this->arguments_[1]); |
---|
| 62 | break; |
---|
| 63 | case 3: |
---|
| 64 | fct->call(this->arguments_[0], this->arguments_[1], this->arguments_[2]); |
---|
| 65 | break; |
---|
| 66 | case 4: |
---|
| 67 | fct->call(this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3]); |
---|
| 68 | break; |
---|
| 69 | case 5: |
---|
| 70 | fct->call(this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3], this->arguments_[4]); |
---|
| 71 | break; |
---|
| 72 | default: |
---|
| 73 | assert(0); |
---|
[2938] | 74 | } |
---|
[7490] | 75 | } |
---|
| 76 | else // not a static function, so also handle with the objectID |
---|
| 77 | { |
---|
| 78 | NetworkMemberFunctionBase *fct = NetworkMemberFunctionBase::getFunction( this->functionID_ ); |
---|
| 79 | switch(this->nrOfArguments_) |
---|
[2938] | 80 | { |
---|
[7490] | 81 | case 0: |
---|
[7495] | 82 | if( !fct->call(this->objectID_) ) |
---|
| 83 | return false; |
---|
[7490] | 84 | break; |
---|
| 85 | case 1: |
---|
[7495] | 86 | if( !fct->call(this->objectID_, this->arguments_[0]) ) |
---|
| 87 | return false; |
---|
[7490] | 88 | break; |
---|
| 89 | case 2: |
---|
[7495] | 90 | if( !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1]) ) |
---|
| 91 | return false; |
---|
[7490] | 92 | break; |
---|
| 93 | case 3: |
---|
[7495] | 94 | if( !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1], this->arguments_[2]) ) |
---|
| 95 | return false; |
---|
[7490] | 96 | break; |
---|
| 97 | case 4: |
---|
[7495] | 98 | if( !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3]) ) |
---|
| 99 | return false; |
---|
[7490] | 100 | break; |
---|
| 101 | case 5: |
---|
[7495] | 102 | if( !fct->call(this->objectID_, this->arguments_[0], this->arguments_[1], this->arguments_[2], this->arguments_[3], this->arguments_[4]) ) |
---|
| 103 | return false; |
---|
[7490] | 104 | break; |
---|
| 105 | default: |
---|
| 106 | assert(0); |
---|
[2938] | 107 | } |
---|
| 108 | } |
---|
| 109 | return true; |
---|
| 110 | } |
---|
| 111 | |
---|
[7490] | 112 | void FunctionCall::setCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){ |
---|
[6417] | 113 | |
---|
[2938] | 114 | // first determine the size that has to be reserved for this call |
---|
[2944] | 115 | uint32_t callsize = 2*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and for bool isStatic |
---|
[2938] | 116 | uint32_t nrOfArguments = 0; |
---|
| 117 | if(mt1) |
---|
| 118 | { |
---|
| 119 | nrOfArguments++; |
---|
| 120 | callsize += mt1->getNetworkSize(); |
---|
[7490] | 121 | this->arguments_.push_back(*mt1); |
---|
[2938] | 122 | if(mt2) |
---|
| 123 | { |
---|
| 124 | nrOfArguments++; |
---|
| 125 | callsize += mt2->getNetworkSize(); |
---|
[7490] | 126 | this->arguments_.push_back(*mt2); |
---|
[2938] | 127 | if(mt3) |
---|
| 128 | { |
---|
| 129 | nrOfArguments++; |
---|
| 130 | callsize += mt3->getNetworkSize(); |
---|
[7490] | 131 | this->arguments_.push_back(*mt3); |
---|
[2938] | 132 | if(mt4) |
---|
| 133 | { |
---|
| 134 | nrOfArguments++; |
---|
| 135 | callsize += mt4->getNetworkSize(); |
---|
[7490] | 136 | this->arguments_.push_back(*mt4); |
---|
[2938] | 137 | if(mt5) |
---|
| 138 | { |
---|
| 139 | nrOfArguments++; |
---|
| 140 | callsize += mt5->getNetworkSize(); |
---|
[7490] | 141 | this->arguments_.push_back(*mt5); |
---|
[2938] | 142 | } |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|
[7490] | 147 | this->nrOfArguments_ = nrOfArguments; |
---|
| 148 | this->size_ = callsize; |
---|
| 149 | this->bIsStatic_ = true; |
---|
| 150 | this->functionID_ = networkID; |
---|
[2938] | 151 | } |
---|
| 152 | |
---|
[7490] | 153 | void FunctionCall::setCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){ |
---|
[6417] | 154 | |
---|
[2938] | 155 | // first determine the size that has to be reserved for this call |
---|
[7490] | 156 | uint32_t callsize = 3*sizeof(uint32_t)+sizeof(uint8_t); //size for network-function-id and nrOfArguments and the objectID and bIsStatic |
---|
[2938] | 157 | uint32_t nrOfArguments = 0; |
---|
| 158 | if(mt1) |
---|
| 159 | { |
---|
| 160 | nrOfArguments++; |
---|
| 161 | callsize += mt1->getNetworkSize(); |
---|
[7490] | 162 | this->arguments_.push_back(*mt1); |
---|
[2938] | 163 | if(mt2) |
---|
| 164 | { |
---|
| 165 | nrOfArguments++; |
---|
| 166 | callsize += mt2->getNetworkSize(); |
---|
[7490] | 167 | this->arguments_.push_back(*mt2); |
---|
[2938] | 168 | if(mt3) |
---|
| 169 | { |
---|
| 170 | nrOfArguments++; |
---|
| 171 | callsize += mt3->getNetworkSize(); |
---|
[7490] | 172 | this->arguments_.push_back(*mt3); |
---|
[2938] | 173 | if(mt4) |
---|
| 174 | { |
---|
| 175 | nrOfArguments++; |
---|
| 176 | callsize += mt4->getNetworkSize(); |
---|
[7490] | 177 | this->arguments_.push_back(*mt4); |
---|
[2938] | 178 | if(mt5) |
---|
| 179 | { |
---|
| 180 | nrOfArguments++; |
---|
| 181 | callsize += mt5->getNetworkSize(); |
---|
[7490] | 182 | this->arguments_.push_back(*mt5); |
---|
[2938] | 183 | } |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | } |
---|
[7490] | 188 | this->nrOfArguments_ = nrOfArguments; |
---|
| 189 | this->bIsStatic_ = false; |
---|
| 190 | this->functionID_ = networkID; |
---|
| 191 | this->size_ = callsize; |
---|
| 192 | this->objectID_ = objectID; |
---|
| 193 | } |
---|
[6417] | 194 | |
---|
[7490] | 195 | void FunctionCall::loadData(uint8_t*& mem) |
---|
| 196 | { |
---|
| 197 | this->functionID_ = *(uint32_t*)mem; |
---|
| 198 | this->bIsStatic_ = *(uint8_t*)(mem+sizeof(uint32_t)); |
---|
| 199 | this->nrOfArguments_ = *(uint32_t*)(mem+sizeof(uint32_t)+sizeof(uint8_t)); |
---|
| 200 | if( this->bIsStatic_ ) |
---|
[2938] | 201 | { |
---|
[7490] | 202 | mem += 2*sizeof(uint32_t)+sizeof(uint8_t); |
---|
[2938] | 203 | } |
---|
[7490] | 204 | else |
---|
| 205 | { |
---|
| 206 | this->objectID_ = *(uint32_t*)(mem+2*sizeof(uint32_t)+sizeof(uint8_t)); |
---|
| 207 | mem += 3*sizeof(uint32_t)+sizeof(uint8_t); |
---|
| 208 | } |
---|
| 209 | for( unsigned int i=0; i<this->nrOfArguments_; ++i ) |
---|
| 210 | { |
---|
| 211 | this->arguments_.push_back(MultiType()); |
---|
| 212 | this->arguments_.back().importData(mem); |
---|
| 213 | } |
---|
| 214 | } |
---|
[6417] | 215 | |
---|
[7490] | 216 | void FunctionCall::saveData(uint8_t*& mem) |
---|
| 217 | { |
---|
| 218 | // now serialise the mt values and copy the function id and isStatic |
---|
| 219 | *(uint32_t*)mem = this->functionID_; |
---|
| 220 | *(uint8_t*)(mem+sizeof(uint32_t)) = this->bIsStatic_; |
---|
| 221 | *(uint32_t*)(mem+sizeof(uint32_t)+sizeof(uint8_t)) = this->nrOfArguments_; |
---|
| 222 | if( this->bIsStatic_ ) |
---|
[2938] | 223 | { |
---|
[7490] | 224 | mem += 2*sizeof(uint32_t)+sizeof(uint8_t); |
---|
[2938] | 225 | } |
---|
[7490] | 226 | { |
---|
| 227 | *(uint32_t*)(mem+2*sizeof(uint32_t)+sizeof(uint8_t)) = this->objectID_; |
---|
| 228 | mem += 3*sizeof(uint32_t)+sizeof(uint8_t); |
---|
| 229 | } |
---|
| 230 | for( std::vector<MultiType>::iterator it = this->arguments_.begin(); it!=this->arguments_.end(); ++it ) |
---|
| 231 | { |
---|
| 232 | it->exportData( mem ); |
---|
| 233 | } |
---|
[2938] | 234 | } |
---|
| 235 | |
---|
| 236 | |
---|
[7490] | 237 | |
---|
[2938] | 238 | } //namespace orxonox |
---|