25 # include <curl/curl.h>
26 # include <curl/easy.h>
30 #include "scriptany/scriptany.h"
31 #include "scriptarray/scriptarray.h"
32 #include "scripthelper/scripthelper.h"
33 #include "scriptmath/scriptmath.h"
34 #include "scriptstdstring/scriptstdstring.h"
66 #include <rapidjson/document.h>
67 #include <rapidjson/writer.h>
71 using namespace AngelScript;
85 void GameScript::logFormat(
const char*
format, ...)
87 char buffer[4000] = {};
88 sprintf(buffer,
"[RoR|Script] ");
89 char* buffer_pos = buffer + 13;
93 vsprintf(buffer_pos,
format, args);
99 void GameScript::activateAllVehicles()
104 void GameScript::setTrucksForcedAwake(
bool forceActive)
109 float GameScript::getTime()
114 void GameScript::setPersonPosition(
const Vector3& vec)
116 if (!this->HavePlayerAvatar(__FUNCTION__))
122 void GameScript::loadTerrain(
const String& terrain)
127 Vector3 GameScript::getPersonPosition()
129 Vector3 result(Vector3::ZERO);
135 void GameScript::movePerson(
const Vector3& vec)
137 if (!this->HavePlayerAvatar(__FUNCTION__))
143 void GameScript::setPersonRotation(
const Radian& rot)
145 if (!this->HavePlayerAvatar(__FUNCTION__))
151 Radian GameScript::getPersonRotation()
159 String GameScript::getCaelumTime()
171 void GameScript::setCaelumTime(
float value)
174 if (!this->HaveSimTerrain(__FUNCTION__))
181 bool GameScript::getCaelumAvailable()
191 void GameScript::stopTimer()
196 void GameScript::startTimer(
int id)
201 void GameScript::setTimeDiff(
float diff)
206 void GameScript::setBestLapTime(
float time)
211 void GameScript::setWaterHeight(
float value)
213 if (!this->HaveSimTerrain(__FUNCTION__))
224 float GameScript::getGroundHeight(Vector3& v)
226 float result = -1.0f;
232 float GameScript::getWaterHeight()
245 float GameScript::getGravity()
255 void GameScript::setGravity(
float value)
257 if (!this->HaveSimTerrain(__FUNCTION__))
268 int GameScript::getNumTrucksByFlag(
int flag)
273 if (!flag ||
static_cast<int>(actor->ar_state) == flag)
279 int GameScript::getCurrentTruckNumber()
285 void GameScript::registerForEvent(
int eventValue)
297 void GameScript::unRegisterEvent(
int eventValue)
323 void GameScript::flashMessage(String& txt,
float time,
float charHeight)
328 void GameScript::message(String& txt, String& icon)
333 void GameScript::updateDirectionArrow(String& text, Vector3& vec)
338 int GameScript::getChatFontSize()
343 void GameScript::setChatFontSize(
int size)
348 void GameScript::showChooser(
const String& type,
const String&
instance,
const String& box)
352 if (type ==
"airplane")
360 if (type ==
"extension")
366 if (type ==
"trailer")
372 if (type ==
"vehicle")
382 void GameScript::repairVehicle(
const String&
instance,
const String& box,
bool keepPosition)
387 void GameScript::removeVehicle(
const String& event_source_instance_name,
const String& event_source_box_name)
396 void GameScript::destroyObject(
const String& instanceName)
398 if (!this->HaveSimTerrain(__FUNCTION__))
407 void GameScript::moveObjectVisuals(
const String& instanceName,
const Vector3& pos)
409 if (!this->HaveSimTerrain(__FUNCTION__))
418 void GameScript::spawnObject(
const String& objectName,
const String& instanceName,
const Vector3& pos,
const Vector3& rot,
const String& eventhandler,
bool uniquifyMaterials)
420 if (!this->HaveSimTerrain(__FUNCTION__))
425 this->logFormat(
"spawnObject(): Cannot spawn object, no terrain loaded!");
431 this->logFormat(
"spawnObject(): Cannot spawn object, no terrain script loaded!");
438 if (module ==
nullptr)
440 this->logFormat(
"spawnObject(): Failed to fetch/create script module");
444 int handler_func_id = -1;
445 if (!eventhandler.empty())
450 "spawnObject(): Specifying event handler function in `game.spawnObject()` (or .TOBJ file) is obsolete and only works with terrain scripts;"
451 " Use `eventCallbackEx()` with event `SE_EVENTBOX_ENTER` instead, it does the same job and works with any script."
452 " Just pass an empty string to the `game.spawnObject()` parameter.");
459 if (handler_func !=
nullptr)
461 handler_func_id = handler_func->GetId();
465 const String type =
"";
475 void GameScript::hideDirectionArrow()
480 bool GameScript::getScreenPosFromWorldPos(Ogre::Vector3
const& world_pos, Ogre::Vector2& out_screen)
482 ImVec2 screen_size = ImGui::GetIO().DisplaySize;
485 Ogre::Vector3 pos_xyz = world2screen.
Convert(world_pos);
488 out_screen.x = pos_xyz.x;
489 out_screen.y = pos_xyz.y;
495 Ogre::Vector2 GameScript::getDisplaySize()
497 ImVec2 size = ImGui::GetIO().DisplaySize;
498 return Vector2(size.x, size.y);
501 Ogre::Vector2 GameScript::getMouseScreenPosition()
503 ImVec2 pos = ImGui::GetIO().MousePos;
504 return Vector2(pos.x, pos.y);
507 int GameScript::setMaterialAmbient(
const String& materialName,
float red,
float green,
float blue)
511 MaterialPtr m = MaterialManager::getSingleton().getByName(materialName);
514 m->setAmbient(red, green, blue);
524 int GameScript::setMaterialDiffuse(
const String& materialName,
float red,
float green,
float blue,
float alpha)
528 MaterialPtr m = MaterialManager::getSingleton().getByName(materialName);
531 m->setDiffuse(red, green, blue, alpha);
541 int GameScript::setMaterialSpecular(
const String& materialName,
float red,
float green,
float blue,
float alpha)
545 MaterialPtr m = MaterialManager::getSingleton().getByName(materialName);
548 m->setSpecular(red, green, blue, alpha);
558 int GameScript::setMaterialEmissive(
const String& materialName,
float red,
float green,
float blue)
562 MaterialPtr m = MaterialManager::getSingleton().getByName(materialName);
565 m->setSelfIllumination(red, green, blue);
575 int GameScript::getTextureUnitState(TextureUnitState** tu,
const String materialName,
int techniqueNum,
int passNum,
int textureUnitNum)
580 MaterialPtr m = MaterialManager::getSingleton().getByName(materialName);
585 if (techniqueNum < 0 || techniqueNum > m->getNumTechniques())
587 Technique* t = m->getTechnique(techniqueNum);
592 if (passNum < 0 || passNum > t->getNumPasses())
594 Pass* p = t->getPass(passNum);
599 if (textureUnitNum < 0 || textureUnitNum > p->getNumTextureUnitStates())
601 TextureUnitState* tut = p->getTextureUnitState(textureUnitNum);
609 int GameScript::setMaterialTextureName(
const String& materialName,
int techniqueNum,
int passNum,
int textureUnitNum,
const String& textureName)
613 TextureUnitState* tu = 0;
614 int res = getTextureUnitState(&tu, materialName, techniqueNum, passNum, textureUnitNum);
615 if (res == 0 && tu != 0)
618 tu->setTextureName(textureName);
629 int GameScript::setMaterialTextureRotate(
const String& materialName,
int techniqueNum,
int passNum,
int textureUnitNum,
float rotation)
633 TextureUnitState* tu = 0;
634 int res = getTextureUnitState(&tu, materialName, techniqueNum, passNum, textureUnitNum);
635 if (res == 0 && tu != 0)
637 tu->setTextureRotate(Degree(rotation));
648 int GameScript::setMaterialTextureScroll(
const String& materialName,
int techniqueNum,
int passNum,
int textureUnitNum,
float sx,
float sy)
652 TextureUnitState* tu = 0;
653 int res = getTextureUnitState(&tu, materialName, techniqueNum, passNum, textureUnitNum);
654 if (res == 0 && tu != 0)
656 tu->setTextureScroll(sx, sy);
667 int GameScript::setMaterialTextureScale(
const String& materialName,
int techniqueNum,
int passNum,
int textureUnitNum,
float u,
float v)
671 TextureUnitState* tu = 0;
672 int res = getTextureUnitState(&tu, materialName, techniqueNum, passNum, textureUnitNum);
673 if (res == 0 && tu != 0)
675 tu->setTextureScale(u, v);
686 float GameScript::rangeRandom(
float from,
float to)
688 return Math::RangeRandom(from, to);
691 int GameScript::getLoadedTerrain(String& result)
693 String terrainName =
"";
698 result = terrainName;
701 return !terrainName.empty();
709 void GameScript::clearEventCache()
711 if (!this->HaveSimTerrain(__FUNCTION__))
716 this->logFormat(
"Cannot execute '%s', collisions not ready", __FUNCTION__);
723 void GameScript::setCameraPosition(
const Vector3& pos)
725 if (!this->HaveMainCamera(__FUNCTION__))
731 void GameScript::setCameraDirection(
const Vector3& rot)
733 if (!this->HaveMainCamera(__FUNCTION__))
739 void GameScript::setCameraOrientation(
const Quaternion& q)
741 if (!this->HaveMainCamera(__FUNCTION__))
747 void GameScript::setCameraYaw(
float rotX)
749 if (!this->HaveMainCamera(__FUNCTION__))
755 void GameScript::setCameraPitch(
float rotY)
757 if (!this->HaveMainCamera(__FUNCTION__))
763 void GameScript::setCameraRoll(
float rotZ)
765 if (!this->HaveMainCamera(__FUNCTION__))
771 Vector3 GameScript::getCameraPosition()
773 Vector3 result(Vector3::ZERO);
779 Vector3 GameScript::getCameraDirection()
781 Vector3 result(Vector3::ZERO);
790 Quaternion GameScript::getCameraOrientation()
792 Quaternion result(Quaternion::ZERO);
798 void GameScript::cameraLookAt(
const Vector3& pos)
800 if (!this->HaveMainCamera(__FUNCTION__))
806 int GameScript::useOnlineAPI(
const String& apiquery,
const AngelScript::CScriptDictionary& dict, String& result)
817 if (player_actor ==
nullptr)
823 std::string token = std::string(
"RoR-Api-User-Token: ") + hashtok;
830 rapidjson::Document j_doc;
833 j_doc.AddMember(
"user-name", rapidjson::StringRef(
App::mp_player_name->getStr().c_str()), j_doc.GetAllocator());
834 j_doc.AddMember(
"user-country", rapidjson::StringRef(
App::app_country->getStr().c_str()), j_doc.GetAllocator());
835 j_doc.AddMember(
"user-token", rapidjson::StringRef(hashtok.c_str()), j_doc.GetAllocator());
837 j_doc.AddMember(
"terrain-name", rapidjson::StringRef(terrain_name.c_str()), j_doc.GetAllocator());
838 j_doc.AddMember(
"terrain-filename", rapidjson::StringRef(
App::sim_terrain_name->getStr().c_str()), j_doc.GetAllocator());
840 j_doc.AddMember(
"script-name", rapidjson::StringRef(script_name.c_str()), j_doc.GetAllocator());
841 j_doc.AddMember(
"script-hash", rapidjson::StringRef(script_hash.c_str()), j_doc.GetAllocator());
843 j_doc.AddMember(
"actor-name", rapidjson::StringRef(player_actor->
ar_design_name.c_str()), j_doc.GetAllocator());
844 j_doc.AddMember(
"actor-filename", rapidjson::StringRef(player_actor->
ar_filename.c_str()), j_doc.GetAllocator());
845 j_doc.AddMember(
"actor-hash", rapidjson::StringRef(player_actor->
ar_filehash.c_str()), j_doc.GetAllocator());
847 rapidjson::Value j_linked_actors(rapidjson::kArrayType);
850 rapidjson::Value j_actor(rapidjson::kObjectType);
851 j_actor.AddMember(
"actor-name", rapidjson::StringRef(actor->
ar_design_name.c_str()), j_doc.GetAllocator());
852 j_actor.AddMember(
"actor-filename", rapidjson::StringRef(actor->
ar_filename.c_str()), j_doc.GetAllocator());
853 j_actor.AddMember(
"actor-hash", rapidjson::StringRef(actor->
ar_filehash.c_str()), j_doc.GetAllocator());
854 j_linked_actors.PushBack(j_actor, j_doc.GetAllocator());
856 j_doc.AddMember(
"linked-actors", j_linked_actors, j_doc.GetAllocator());
858 j_doc.AddMember(
"avg-fps", getAvgFPS(), j_doc.GetAllocator());
859 j_doc.AddMember(
"ror-version", rapidjson::StringRef(
ROR_VERSION_STRING), j_doc.GetAllocator());
861 for (
auto item : dict)
863 const std::string& key = item.GetKey();
864 const std::string* value = (std::string *)item.GetAddressOfValue();
865 j_doc.AddMember(rapidjson::StringRef(key.c_str()), rapidjson::StringRef(value->c_str()), j_doc.GetAllocator());
868 rapidjson::StringBuffer buffer;
869 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
870 j_doc.Accept(writer);
871 std::string json = buffer.GetString();
875 _L(
"using Online API..."),
"information.png");
877 LOG(
"[RoR|GameScript] Submitting race results to '" + url +
"'");
879 std::thread([url, user, token, json]()
881 long response_code = 0;
883 struct curl_slist *slist = NULL;
884 slist = curl_slist_append(slist,
"Accept: application/json");
885 slist = curl_slist_append(slist,
"Content-Type: application/json");
886 slist = curl_slist_append(slist, user.c_str());
887 slist = curl_slist_append(slist, token.c_str());
889 CURL *curl = curl_easy_init();
890 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
891 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
892 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
894 CURLcode curl_result = curl_easy_perform(curl);
895 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
897 if (curl_result != CURLE_OK || response_code != 200)
899 Ogre::LogManager::getSingleton().stream()
900 <<
"[RoR|GameScript] `useOnlineAPI()` failed to submit data;"
901 <<
" Error: '" << curl_easy_strerror(curl_result) <<
"'; HTTP status code: " << response_code;
904 curl_easy_cleanup(curl);
906 curl_slist_free_all(slist);
911 _L(
"Cannot use Online API in this build (CURL not available)"));
917 void GameScript::openUrlInDefaultBrowser(
const std::string& url)
922 void GameScript::fetchUrlAsStringAsync(
const std::string& url,
const std::string& display_filename)
924 #if defined(USE_CURL)
934 std::thread(std::move(pktask), task).detach();
935 #endif // defined(USE_CURL)
938 void GameScript::boostCurrentTruck(
float factor)
944 rpm += 2000.0f * factor;
949 int GameScript::addScriptFunction(
const String& arg)
954 int GameScript::scriptFunctionExists(
const String& arg)
959 int GameScript::deleteScriptFunction(
const String& arg)
964 int GameScript::addScriptVariable(
const String& arg)
969 int GameScript::deleteScriptVariable(
const String& arg)
974 int GameScript::sendGameCmd(
const String& message)
982 #endif // USE_SOCKETW
987 AngelScript::CScriptArray* GameScript::getRunningScripts()
989 std::vector<ScriptUnitId_t> ids;
991 ids.push_back(pair.first);
1003 AngelScript::CScriptDictionary* dict = AngelScript::CScriptDictionary::Create(
App::GetScriptEngine()->getEngine());
1007 dict->Set(
"uniqueId", (asINT64)info.
uniqueId);
1008 dict->Set(
"scriptName",
new std::string(info.
scriptName), stringTypeid);
1009 dict->Set(
"scriptCategory", &info.
scriptCategory, scriptCategoryTypeid);
1010 dict->Set(
"eventMask", (asINT64)info.
eventMask);
1011 dict->Set(
"scriptBuffer",
new std::string(info.
scriptBuffer), stringTypeid);
1039 if (actor !=
nullptr)
1046 ActorPtr GameScript::spawnTruck(Ogre::String& truckName, Ogre::Vector3& pos, Ogre::Vector3& rot)
1050 rq.
asr_rotation = Quaternion(Degree(rot.x), Vector3::UNIT_X) * Quaternion(Degree(rot.y), Vector3::UNIT_Y) * Quaternion(Degree(rot.z), Vector3::UNIT_Z);
1055 ActorPtr GameScript::spawnTruckAI(Ogre::String& truckName, Ogre::Vector3& pos, Ogre::String& truckSectionConfig, std::string& truckSkin,
int x)
1063 std::vector<Ogre::Vector3> waypoints;
1070 std::reverse(waypoints.begin(), waypoints.end());
1074 Ogre::Vector3 dir = Ogre::Vector3::ZERO;
1075 if (waypoints.size() >= 2)
1076 dir = waypoints[0] - waypoints[1];
1077 else if (waypoints.size() >= 1)
1080 rq.
asr_rotation = Ogre::Vector3::UNIT_X.getRotationTo(dir, Ogre::Vector3::UNIT_Y);
1095 AngelScript::CScriptArray* GameScript::getWaypoints(
int x)
1097 std::vector<Ogre::Vector3> vec;
1104 std::reverse(vec.begin(), vec.end());
1107 AngelScript::CScriptArray* arr = AngelScript::CScriptArray::Create(AngelScript::asGetActiveContext()->GetEngine()->GetTypeInfoByDecl(
"array<vector3>"), vec.size());
1109 for(AngelScript::asUINT i = 0; i < arr->GetSize(); i++)
1112 arr->SetValue(i, &vec[i]);
1118 AngelScript::CScriptArray* GameScript::getAllTrucks()
1121 AngelScript::CScriptArray* arr = AngelScript::CScriptArray::Create(AngelScript::asGetActiveContext()->GetEngine()->GetTypeInfoByDecl(
"array<BeamClass@>"), actors.size());
1123 for (AngelScript::asUINT i = 0; i < arr->GetSize(); i++)
1126 arr->SetValue(i, &actors[i]);
1132 void GameScript::addWaypoint(
const Ogre::Vector3& pos)
1134 std::vector<Ogre::Vector3> waypoints;
1141 AngelScript::CScriptArray* GameScript::getWaypointsSpeed()
1143 std::vector<int> vec;
1149 AngelScript::CScriptArray* arr = AngelScript::CScriptArray::Create(AngelScript::asGetActiveContext()->GetEngine()->GetTypeInfoByDecl(
"array<int>"), vec.size());
1151 for(AngelScript::asUINT i = 0; i < arr->GetSize(); i++)
1154 arr->SetValue(i, &vec[i]);
1160 int GameScript::getAIVehicleCount()
1166 int GameScript::getAIVehicleDistance()
1172 int GameScript::getAIVehiclePositionScheme()
1178 int GameScript::getAIVehicleSpeed()
1184 Ogre::String GameScript::getAIVehicleName(
int x)
1198 Ogre::String GameScript::getAIVehicleSectionConfig(
int x)
1212 std::string GameScript::getAIVehicleSkin(
int x)
1226 int GameScript::getAIRepeatTimes()
1232 int GameScript::getAIMode()
1240 void GameScript::setAIVehicleCount(
int num)
1245 void GameScript::setAIVehicleDistance(
int dist)
1250 void GameScript::setAIVehiclePositionScheme(
int scheme)
1255 void GameScript::setAIVehicleSpeed(
int speed)
1260 void GameScript::setAIVehicleName(
int x, std::string name)
1272 void GameScript::setAIVehicleSectionConfig(
int x, std::string config)
1283 this->
log(
fmt::format(
"setAIVehicleSectionConfig: ERROR, valid 'x' is 0 or 1, got {}", x));
1288 void GameScript::setAIVehicleSkin(
int x, std::string skin)
1299 this->
log(
fmt::format(
"setAIVehicleSkin: ERROR, valid 'x' is 0 or 1, got {}", x));
1304 void GameScript::setAIRepeatTimes(
int times)
1309 void GameScript::setAIMode(
int mode)
1314 void GameScript::showMessageBox(Ogre::String& title, Ogre::String& text,
bool use_btn1, Ogre::String& btn1_text,
bool allow_close,
bool use_btn2, Ogre::String& btn2_text)
1317 const char* btn1_cstr =
nullptr;
1318 const char* btn2_cstr =
nullptr;
1322 btn1_cstr = (btn1_text.empty() ?
"~1~" : btn1_text.c_str());
1326 btn2_cstr = (btn2_text.empty() ?
"~2~" : btn2_text.c_str());
1332 void GameScript::backToMenu()
1338 void GameScript::quitGame()
1343 float GameScript::getFPS()
1348 float GameScript::getAvgFPS()
1353 bool GameScript::getMousePositionOnTerrain(Ogre::Vector3& out_pos)
1355 if (!HaveSimTerrain(__FUNCTION__))
1363 out_pos = ray_result.position;
1365 return ray_result.hit;
1368 Ogre::SceneManager* GameScript::getSceneManager()
1373 bool GameScript::pushMessage(
MsgType type, AngelScript::CScriptDictionary* dict)
1418 if (!has_filename && !has_buffer)
1420 this->
log(
fmt::format(
"{}: ERROR, either 'filename' or 'buffer' must be set!", log_msg));
1427 int64_t instance_id;
1430 this->
log(
fmt::format(
"{}: WARNING, category 'ACTOR' specified but 'associated_actor' not given.", log_msg, rq->
lsr_filename));
1504 std::string skin_name;
1509 this->
log(
fmt::format(
"{}: WARNING, skin '{}' is not installed.", log_msg, skin_name));
1526 int64_t instance_id = -1;
1547 int64_t instance_id = -1;
1557 this->
log(
fmt::format(
"{}: Actor with instance ID '{}' not found!", log_msg, instance_id));
1571 int64_t instance_id = -1;
1574 && instance_id > -1)
1584 Ogre::Vector3 position;
1587 m.
payload =
new Ogre::Vector3(position);
1609 case (int64_t)FreeForceType::CONSTANT:
1621 case (int64_t)FreeForceType::TOWARDS_COORDS:
1633 case (int64_t)FreeForceType::TOWARDS_NODE:
1723 CScriptArray* GameScript::getAllSoundScriptTemplates()
1733 AngelScript::CScriptArray* GameScript::getAllSoundScriptInstances()
1738 SoundPtr GameScript::createSoundFromResource(
const std::string& filename,
const std::string& resource_group_name)
1743 SoundScriptInstancePtr GameScript::createSoundScriptInstance(
const std::string& template_name,
int actor_instance_id = SoundScriptInstance::ACTOR_ID_UNKNOWN)
1748 bool GameScript::checkResourceExists(
const std::string& filename,
const std::string& resource_group)
1752 std::string resource_name = this->CheckFileAccess(
"checkResourceExists()", filename, resource_group);
1753 if (resource_name ==
"")
1757 return Ogre::ResourceGroupManager::getSingleton().resourceExists(resource_group, resource_name);
1766 bool GameScript::deleteResource(
const std::string& filename,
const std::string& resource_group)
1770 std::string resource_name = this->CheckFileAccess(
"deleteResource()", filename, resource_group);
1771 if (resource_name ==
"")
1775 Ogre::ResourceGroupManager::getSingleton().deleteResource(resource_name, resource_group);
1785 std::string GameScript::loadTextResourceAsString(
const std::string& filename,
const std::string& resource_group)
1789 std::string resource_name = this->CheckFileAccess(
"loadTextResourceAsString()", filename, resource_group);
1790 if (resource_name ==
"")
1793 Ogre::DataStreamPtr stream = Ogre::ResourceGroupManager::getSingleton().openResource(resource_name, resource_group);
1795 if (stream.isNull() || !stream->isReadable())
1798 fmt::format(
"loadTextResourceAsString() could not read resource '{}' in group '{}'",
1799 resource_name, resource_group));
1803 #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
1808 const size_t BUF_LEN = 4000;
1809 char buf[BUF_LEN] = {};
1813 size_t read_len = stream->read(buf, BUF_LEN);
1814 if (read_len < BUF_LEN)
1819 str.append(buf, read_len);
1823 return stream->getAsString();
1833 bool GameScript::createTextResourceFromString(
const std::string& data,
const std::string& filename,
const std::string& resource_group,
bool overwrite)
1837 std::string resource_name = this->CheckFileAccess(
"createTextResourceFromString()", filename, resource_group);
1838 if (resource_name ==
"")
1841 Ogre::DataStreamPtr stream = Ogre::ResourceGroupManager::getSingleton().createResource(resource_name, resource_group, overwrite);
1843 if (stream.isNull() || !stream->isWriteable())
1846 fmt::format(
"createTextResourceFromString() could not create resource '{}' in group '{}'",
1847 resource_name, resource_group));
1851 stream->write(data.data(), data.size());
1861 AngelScript::CScriptArray* GameScript::findResourceFileInfo(
const std::string& resource_group,
const std::string& pattern,
bool dirs )
1866 Ogre::FileInfoListPtr fileInfoList
1867 = Ogre::ResourceGroupManager::getSingleton().findResourceFileInfo(resource_group, pattern, dirs);
1871 AngelScript::CScriptArray* arr = AngelScript::CScriptArray::Create(typeinfo);
1873 for (
const Ogre::FileInfo& fileinfo: *fileInfoList)
1875 AngelScript::CScriptDictionary* dict = AngelScript::CScriptDictionary::Create(
App::GetScriptEngine()->getEngine());
1876 dict->Set(
"filename",
new std::string(fileinfo.filename), stringTypeid);
1877 dict->Set(
"basename",
new std::string(fileinfo.basename), stringTypeid);
1878 dict->Set(
"compressedSize", (asINT64)fileinfo.compressedSize);
1879 dict->Set(
"uncompressedSize", (asINT64)fileinfo.uncompressedSize);
1881 arr->InsertLast(dict);
1892 Ogre::Image GameScript::loadImageResource(
const std::string& filename,
const std::string& resource_group)
1896 std::string resource_name = this->CheckFileAccess(
"loadImageResource()", filename, resource_group);
1897 if (resource_name ==
"")
1898 return Ogre::Image();
1901 return img.load(resource_name, resource_group);
1906 return Ogre::Image();
1910 bool GameScript::serializeMeshResource(
const std::string& filename,
const std::string& resource_group,
const Ogre::MeshPtr& mesh)
1914 std::string resource_name = this->CheckFileAccess(
"serializeMeshResource()", filename, resource_group);
1915 if (resource_name ==
"")
1918 Ogre::MeshSerializer ser;
1919 Ogre::DataStreamPtr stream = Ogre::ResourceGroupManager::getSingleton().createResource(resource_name, resource_group);
1920 ser.exportMesh(mesh.get(), stream);
1933 bool GameScript::HaveSimTerrain(
const char* func_name)
1937 this->logFormat(
"Cannot execute '%s', terrain not ready", func_name);
1943 bool GameScript::HavePlayerAvatar(
const char* func_name)
1947 this->logFormat(
"Cannot execute '%s', player avatar not ready", func_name);
1953 bool GameScript::HaveMainCamera(
const char* func_name)
1957 this->logFormat(
"Cannot execute '%s', main camera not ready", func_name);
1963 std::string GameScript::CheckFileAccess(
const char* func_name,
const std::string& filename,
const std::string& resource_group)
1968 std::string basename, extension, path;
1969 Ogre::StringUtil::splitFullFilename(filename, basename, extension, path);
1973 fmt::format(
"{}: access denied to '{}' with group '{}' - file paths are not allowed",
1974 func_name, filename, resource_group));
1979 return basename +
"." + extension;