bool ObjectRecord::saveToStream(std::ofstream& outStream) const { if (!outStream.good()) { DuskLog() << "ObjectRecord::saveToStream: ERROR: Stream contains errors!\n"; return false; } unsigned int len; //write header "ObjS" outStream.write((const char*) &cHeaderObjS, sizeof(uint32_t)); //Object, Static //write ID len = ID.length(); outStream.write((const char*) &len, sizeof(unsigned int)); outStream.write(ID.c_str(), len); //write mesh len = Mesh.length(); outStream.write((const char*) &len, sizeof(unsigned int)); outStream.write(Mesh.c_str(), len); //write collision flag outStream.write((const char*) &collide, sizeof(bool)); //check if (!outStream.good()) { DuskLog() << "ObjectRecord::saveToStream: Error while writing data to " << "stream. Current object is \""<<ID<<"\".\n"; return false; } return outStream.good(); }
bool dusk::Scripting::RegisterFunction(const string& funcName, LuaCallback callback) { if (funcName.empty()) { DuskLog("error", "Cannot register a function with no name"); return false; } if (callback == nullptr) { DuskLog("error", "Cannot register a fucntion with no callback"); } if (s_Functions.contains_key(funcName)) { DuskLog("error", "Cannot register a function twice"); return false; } s_Functions.add(funcName, callback); for (auto it : s_ScriptHosts) it->RegisterFunction(funcName, callback); return true; }
bool LightRecord::saveToStream(std::ofstream& outStream) const { if (!outStream.good()) { DuskLog() << "LightRecord::saveToStream: ERROR: bad stream.\n"; return false; } //write header Light outStream.write((const char*) &cHeaderLight, sizeof(unsigned int)); //write ID const unsigned int len = ID.length(); outStream.write((const char*) &len, sizeof(unsigned int)); outStream.write(ID.c_str(), len); //write colour data outStream.write((const char*) &red, sizeof(float)); outStream.write((const char*) &green, sizeof(float)); outStream.write((const char*) &blue, sizeof(float)); outStream.write((const char*) &radius, sizeof(float)); //write light type outStream.write((const char*) &type, sizeof(Ogre::Light::LightTypes)); //check if (!outStream.good()) { DuskLog() << "LightRecord::saveToStream: ERROR: while writing light " << "data. Current ID is \""<<ID<<"\".\n"; return false; } return outStream.good(); }
bool ContainerRecord::saveToStream(std::ofstream& outStream) const { if (!outStream.good()) { DuskLog() << "ContainerRecord::saveToStream: ERROR: stream contains errors!\n"; return false; } uint32_t len = 0; //header "Cont" outStream.write((const char*) &cHeaderCont, sizeof(uint32_t)); //ID len = ID.length(); outStream.write((const char*) &len, sizeof(uint32_t)); outStream.write(ID.c_str(), len); //Mesh len = Mesh.length(); outStream.write((const char*) &len, sizeof(uint32_t)); outStream.write(Mesh.c_str(), len); //Inventory if (!ContainerInventory.saveToStream(outStream)) { DuskLog() << "ContainerRecord::saveStream: ERROR while writing " << "container's inventory.\n"; return false; }//if return outStream.good(); }
bool SoundRecord::saveToStream(std::ofstream& outStream) const { if (!outStream.good()) { DuskLog() << "SoundRecord::saveToStream: ERROR: Stream contains errors!\n"; return false; } //write header "Soun" outStream.write((const char*) &cHeaderSoun, sizeof(uint32_t)); //Sound //write ID uint32_t len = ID.length(); outStream.write((const char*) &len, sizeof(uint32_t)); outStream.write(ID.c_str(), len); //write path len = filePath.length(); outStream.write((const char*) &len, sizeof(uint32_t)); outStream.write(filePath.c_str(), len); //check if (!outStream.good()) { DuskLog() << "SoundRecord::saveToStream: Error while writing data to " << "stream. Current object is \""<<ID<<"\".\n"; return false; } return outStream.good(); }
bool ObjectRecord::loadFromStream(std::ifstream& inStream) { unsigned int len; uint32_t Header = 0; char ID_Buffer[256], Mesh_Buffer[256]; //read header "ObjS" (Object, Static) inStream.read((char*) &Header, sizeof(uint32_t)); if (Header!=cHeaderObjS) { DuskLog() << "ObjectRecord::loadFromStream: ERROR: Stream contains invalid " << "record header.\n"; return false; }//if //read length of ID inStream.read((char*) &len, sizeof(unsigned int)); if (len>255) { DuskLog() << "ObjectRecord::loadFromStream: ERROR: ID cannot be longer than " << "255 characters.\n"; return false; } //read ID memset(ID_Buffer, 0, 256); inStream.read(ID_Buffer, len); ID_Buffer[len] = '\0'; //add terminating null character if (!inStream.good()) { DuskLog() << "ObjectRecord::loadFromStream: ERROR while reading data.\n"; return false; } //read length of mesh name inStream.read((char*) &len, sizeof(unsigned int)); if (len>255) { DuskLog() << "ObjectRecord::loadFromStream: ERROR: Name of Mesh cannot be " << "longer than 255 characters.\n"; return false; } //read mesh memset(Mesh_Buffer, 0, 256); inStream.read(Mesh_Buffer, len); Mesh_Buffer[len] = '\0'; //add terminating null character //read collision flag bool collision_flag = true; inStream.read((char*) &collision_flag, sizeof(bool)); if (!inStream.good()) { DuskLog() << "ObjectRecord::loadFromStream: ERROR while reading data.\n"; return false; } //now set the data ID = std::string(ID_Buffer); Mesh = std::string(Mesh_Buffer); collide = collision_flag; return true; }
bool LightRecord::loadFromStream(std::ifstream& inStream) { if (!inStream.good()) { DuskLog() << "LightRecord::loadFromStream: ERROR: stream contains errors.\n"; return false; } char ID_Buffer[256]; uint32_t Header = 0; inStream.read((char*) &Header, sizeof(uint32_t)); if (Header != cHeaderLight) { DuskLog() << "LightRecord::loadFromStream: ERROR: invalid record header.\n"; return false; } //read ID length Header = 0; inStream.read((char*) &Header, sizeof(uint32_t)); if (Header>255) { DuskLog() << "LightRecord::loadFromStream: ERROR: ID is longer than 255" << "characters.\n"; return false; } //read ID memset(ID_Buffer, 0, Header+1); inStream.read(ID_Buffer, Header); if (!inStream.good()) { DuskLog() << "LightRecord::loadFromStream: ERROR while reading light's" << " ID from stream.\n"; return false; } ID_Buffer[Header] = '\0'; //make sure it's NUL-terminated ID = std::string(ID_Buffer); //read RGB and radius inStream.read((char*) &red, sizeof(float)); inStream.read((char*) &green, sizeof(float)); inStream.read((char*) &blue, sizeof(float)); inStream.read((char*) &radius, sizeof(float)); //read light type inStream.read((char*) &type, sizeof(Ogre::Light::LightTypes)); if (!inStream.good()) { DuskLog() << "LightRecord::loadFromStream: ERROR while reading light's" << " colour values from stream.\n"; return false; } //normalise values, just to be safe normalise(); return true; }
bool SoundRecord::loadFromStream(std::ifstream& inStream) { uint32_t len = 0; //read header "Soun" (Sound) inStream.read((char*) &len, sizeof(uint32_t)); if (len!=cHeaderSoun) { DuskLog() << "SoundRecord::loadFromStream: ERROR: Stream contains invalid " << "record header.\n"; return false; }//if //read length of ID inStream.read((char*) &len, sizeof(uint32_t)); if (len>255) { DuskLog() << "SoundRecord::loadFromStream: ERROR: ID cannot be longer than " << "255 characters.\n"; return false; } //read ID char buffer[256]; memset(buffer, 0, 256); inStream.read(buffer, len); if (!inStream.good()) { DuskLog() << "SoundRecord::loadFromStream: ERROR while reading ID.\n"; return false; } ID = std::string(buffer); //read length of path inStream.read((char*) &len, sizeof(uint32_t)); if (len>255) { DuskLog() << "SoundRecord::loadFromStream: ERROR: File path cannot " << "be longer than 255 characters.\n"; return false; } //read mesh memset(buffer, 0, 256); inStream.read(buffer, len); if (!inStream.good()) { DuskLog() << "SoundRecord::loadFromStream: ERROR while reading path data.\n"; return false; } filePath = std::string(buffer); return (!filePath.empty() and !ID.empty()); }
bool CommandPlaySound::execute(Dusk::Scene* scene, int count) { Source & src = Sound::get().getSource(m_Noise); switch(m_operation) { case sopPlay: return src.play(); case sopPause: return src.pause(); case sopUnPause: return src.unPause(); case sopStop: return src.stop(); case sopReplay: //New version of Sound has no Replay method yet. Instead we are using //a combination of Stop and Play to simulate the former Replay method. if (src.stop()) { return src.play(); } return false; }//switch DuskLog() << "CommandPlaySound::execute: ERROR: unknown op code parameter.\n"; return false; }
void Inventory::addItem(const std::string& ItemID, const unsigned int count) { if (ItemID.empty() or count==0) { DuskLog() << "Inventory::addItem: ERROR: ItemID is emtpy string or amount " << "is zero!\n"; return; } std::map<std::string, unsigned int>::iterator iter = m_Items.find(ItemID); if (iter!=m_Items.end()) { //there are already items of given ID iter->second = iter->second + count; } else { m_Items[ItemID] = count; } //adjust weight of inventory according to added items if (Database::getSingleton().hasTypedRecord<ItemRecord>(ItemID)) { m_TotalWeight = m_TotalWeight +count*Database::getSingleton().getTypedRecord<ItemRecord>(ItemID).weight; } else { //if there's no such item in item base, it must have been a weapon m_TotalWeight = m_TotalWeight +count*Database::getSingleton().getTypedRecord<WeaponRecord>(ItemID).weight; } }
bool Inventory::saveToStream(std::ofstream& OutStream) const { if (!OutStream.good()) { DuskLog() << "Inventory::saveToStream: ERROR: stream contains errors!\n"; return false; } OutStream.write((char*) &cHeaderInve, sizeof(unsigned int)); //write number of items unsigned int len = m_Items.size(); OutStream.write((char*) &len, sizeof(unsigned int)); //write items (ID and amount) std::map<std::string, unsigned int>::const_iterator traverse; traverse = m_Items.begin(); while (traverse != m_Items.end()) { //ID len = traverse->first.length(); OutStream.write((char*) &len, sizeof(unsigned int)); OutStream.write(traverse->first.c_str(), len); //amount OutStream.write((char*) &(traverse->second), sizeof(unsigned int)); ++traverse; }//while return OutStream.good(); }
bool Inventory::loadFromStream(std::ifstream& InStream) { if (!InStream.good()) { DuskLog() << "Inventory::loadFromStream: ERROR: stream contains errors!\n"; return false; } unsigned int len=0, i, count=0; InStream.read((char*) &len, sizeof(unsigned int)); if (len!=cHeaderInve) { DuskLog() << "Inventory::loadFromStream: ERROR: stream contains unexpected header!\n"; return false; } //read number of items InStream.read((char*) &count, sizeof(unsigned int)); makeEmpty(); char ID_Buffer[256]; ID_Buffer[0] = ID_Buffer[255] = '\0'; for (i=0; i<count; i++) { //read loop //ID len=0; InStream.read((char*) &len, sizeof(unsigned int)); if (len>255) { DuskLog() << "Inventory::loadFromStream: ERROR: ID is longer than 255 characters!\n"; return false; } InStream.read(ID_Buffer, len); if (!InStream.good()) { DuskLog() << "Inventory::loadFromStream: ERROR while reading item ID!\n"; return false; } ID_Buffer[len] = '\0'; //amount InStream.read((char*) &len, sizeof(unsigned int)); if (!InStream.good()) { DuskLog() << "Inventory::loadFromStream: ERROR while reading item amount!\n"; return false; } addItem(std::string(ID_Buffer), len); }//for return InStream.good(); }
bool ContainerRecord::loadFromStream(std::ifstream& inStream) { if (!inStream.good()) { DuskLog() << "ContainerRecord::loadFromStream: ERROR: stream contains errors!\n"; return false; } uint32_t len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len != cHeaderCont) { DuskLog() << "ContainerRecord::loadFromStream: ERROR: stream contains unexpected header!\n"; return false; } char ID_Buffer[256]; memset(ID_Buffer, 0, 256); //read ID len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len>255) { DuskLog() << "ContainerRecord::loadFromStream: ERROR: ID is " << "longer than 255 characters!\n"; return false; } inStream.read(ID_Buffer, len); if (!inStream.good()) { DuskLog() << "ContainerRecord::loadFromStream: ERROR while " << "reading ID from stream!\n"; return false; } //read Mesh char Mesh_Buffer[256]; memset(Mesh_Buffer, 0, 256); len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len>255) { DuskLog() << "ContainerRecord::loadFromStream: ERROR: mesh path " << "is longer than 255 characters!\n"; return false; } inStream.read(Mesh_Buffer, len); if (!inStream.good()) { DuskLog() << "ContainerRecord::loadFromStream: ERROR while " << "reading mesh path from stream!\n"; return false; } Inventory temp; if (!temp.loadFromStream(inStream)) { DuskLog() << "ContainerRecord::loadFromStream: ERROR while " << "reading inventory contens from stream!\n"; return false; } //all right so far return inStream.good(); }
bool VehicleRecord::saveToStream(std::ofstream& outStream) const { if (!outStream.good()) { DuskLog() << "VehicleRecord::saveToStream: ERROR: stream contains errors!\n"; return false; } uint32_t len = 0; float temp; //header "Vehi" outStream.write((const char*) &cHeaderVehi, sizeof(uint32_t)); //ID len = ID.length(); outStream.write((const char*) &len, sizeof(uint32_t)); outStream.write(ID.c_str(), len); //Mesh len = Mesh.length(); outStream.write((const char*) &len, sizeof(uint32_t)); outStream.write(Mesh.c_str(), len); //Name len = Name.length(); outStream.write((const char*) &len, sizeof(uint32_t)); outStream.write(Name.c_str(), len); //MaxSpeed outStream.write((const char*) &MaxSpeed, sizeof(MaxSpeed)); //number of Mountpoints len = Mountpoints.size(); outStream.write((const char*) &len, sizeof(len)); //write mountpoint data for (len=0; len<Mountpoints.size(); ++len) { // -- offset temp = Mountpoints.at(len).offset.x; outStream.write((const char*) &temp, sizeof(float)); temp = Mountpoints.at(len).offset.y; outStream.write((const char*) &temp, sizeof(float)); temp = Mountpoints.at(len).offset.z; outStream.write((const char*) &temp, sizeof(float)); // -- rotation temp = Mountpoints.at(len).rotation.w; outStream.write((const char*) &temp, sizeof(float)); temp = Mountpoints.at(len).rotation.x; outStream.write((const char*) &temp, sizeof(float)); temp = Mountpoints.at(len).rotation.y; outStream.write((const char*) &temp, sizeof(float)); temp = Mountpoints.at(len).rotation.z; outStream.write((const char*) &temp, sizeof(float)); }//for return outStream.good(); }
bool ProjectileRecord::saveToStream(std::ofstream& outStream) const { if (!outStream.good()) { DuskLog() << "ProjectileRecord::saveToStream: ERROR: stream contains errors!\n"; return false; } uint32_t len = 0; //header "Proj" outStream.write((const char*) &cHeaderProj, sizeof(uint32_t)); //ID len = ID.length(); outStream.write((const char*) &len, sizeof(uint32_t)); outStream.write(ID.c_str(), len); //Mesh len = Mesh.length(); outStream.write((const char*) &len, sizeof(uint32_t)); outStream.write(Mesh.c_str(), len); //TTL outStream.write((const char*) &DefaultTTL, sizeof(float)); //velocity outStream.write((const char*) &DefaultVelocity, sizeof(float)); //times outStream.write((const char*) ×, sizeof(uint8_t)); //dice outStream.write((const char*) &dice, sizeof(uint8_t)); //check if (!outStream.good()) { DuskLog() << "ProjectileRecord::saveToStream: Error while writing data to " << "stream. Current projectile is \""<<ID<<"\".\n"; return false; } return outStream.good(); }
bool ProjectileRecord::loadFromStream(std::ifstream& inStream) { if (!inStream.good()) { DuskLog() << "ProjectileRecord::loadFromStream: ERROR: stream contains errors!\n"; return false; } uint32_t len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len != cHeaderProj) { DuskLog() << "ProjectileRecord::loadFromStream: ERROR: stream " << "contains unexpected header!\n"; return false; } char Buffer[256]; memset(Buffer, 0, 256); //read ID len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len>255) { DuskLog() << "ProjectileRecord::loadFromStream: ERROR: ID is " << "longer than 255 characters!\n"; return false; } inStream.read(Buffer, len); if (!inStream.good()) { DuskLog() << "ProjectileRecord::loadFromStream: ERROR while " << "reading ID from stream!\n"; return false; } Buffer[len] = '\0'; ID = std::string(Buffer); //read Mesh len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len>255) { DuskLog() << "ProjectileRecord::loadFromStream: ERROR: mesh " << "path is longer than 255 characters!\n"; return false; } inStream.read(Buffer, len); if (!inStream.good()) { DuskLog() << "ProjectileRecord::loadFromStream: ERROR while " << "reading mesh path from stream!\n"; return false; } Buffer[len] = '\0'; Mesh = std::string(Buffer); //TTL inStream.read((char*) &DefaultTTL, sizeof(float)); //velocity inStream.read((char*) &DefaultVelocity, sizeof(float)); //times inStream.read((char*) ×, sizeof(uint8_t)); //dice inStream.read((char*) &dice, sizeof(uint8_t)); if (!inStream.good()) { DuskLog() << "ProjectileRecord::loadFromStream: ERROR while " << "reading projectile data from stream!\n"; return false; } return true; }
bool VehicleRecord::loadFromStream(std::ifstream& inStream) { if (!inStream.good()) { DuskLog() << "VehicleRecord::loadFromStream: ERROR: stream " << "contains errors!\n"; return false; } uint32_t len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len != cHeaderVehi) { DuskLog() << "VehicleRecord::loadFromStream: ERROR: stream " << "contains unexpected header!\n"; return false; } char Buffer[256]; memset(Buffer, 0, 256); //read ID len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len>255) { DuskLog() << "VehicleRecord::loadFromStream: ERROR: ID is longer " << "than 255 characters!\n"; return false; } inStream.read(Buffer, len); if (!inStream.good()) { DuskLog() << "VehicleRecord::loadFromStream: ERROR while reading " << "ID from stream!\n"; return false; } Buffer[len] = '\0'; ID = std::string(Buffer); //read mesh len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len>255) { DuskLog() << "VehicleRecord::loadFromStream: ERROR: mesh path of " << "vehicle \""<<ID<<"\" is longer than 255 characters!\n"; return false; } memset(Buffer, 0, 256); inStream.read(Buffer, len); if (!inStream.good()) { DuskLog() << "VehicleRecord::loadFromStream: ERROR while reading " << "mesh path from stream!\n"; return false; } Mesh = std::string(Buffer); //read name len = 0; inStream.read((char*) &len, sizeof(uint32_t)); if (len>255) { DuskLog() << "VehicleRecord::loadFromStream: ERROR: name of vehicle \"" << ID <<"\" is longer than 255 characters!\n"; return false; } memset(Buffer, 0, 256); inStream.read(Buffer, len); if (!inStream.good()) { DuskLog() << "VehicleRecord::loadFromStream: ERROR while reading " << "name from stream!\n"; return false; } Buffer[len] = '\0'; Name = std::string(Buffer); //MaxSpeed inStream.read((char*) &MaxSpeed, sizeof(MaxSpeed)); if (MaxSpeed<0.0f) { DuskLog() << "VehicleRecord::loadFromStream: ERROR while reading " << "data from stream. Vehicle has invalid maximum speed.\n"; return false; } //number of Mountpoints len = 0; inStream.read((char*) &len, sizeof(len)); if (!inStream.good()) { DuskLog() << "VehicleRecord::loadFromStream: ERROR while reading " << "data from stream!\n"; return false; } if (len>cMaxMountpointCount) { DuskLog() << "VehicleRecord::loadFromStream: ERROR while reading " << "data from stream! Number of mountpoints is too big. Current " << "value is "<<len<<", but only up to " << cMaxMountpointCount << " are allowed.\n"; return false; } //read the mountpoint data float w, x, y, z; MountpointData mpd; Mountpoints.clear(); unsigned int i; for (i=0; i<len; ++i) { // -- offset first inStream.read((char*) &x, sizeof(float)); inStream.read((char*) &y, sizeof(float)); inStream.read((char*) &z, sizeof(float)); mpd.offset = Ogre::Vector3(x,y,z); // -- rotation inStream.read((char*) &w, sizeof(float)); inStream.read((char*) &x, sizeof(float)); inStream.read((char*) &y, sizeof(float)); inStream.read((char*) &z, sizeof(float)); mpd.rotation = Ogre::Quaternion(w,x,y,z); if (!inStream.good()) { DuskLog() << "VehicleRecord::loadFromStream: ERROR while reading" << " mountpoint data from stream!\n"; return false; }//if Mountpoints.push_back(mpd); }//for //Did all go well so far? return inStream.good(); }