From ea607cf77c1ba95ea2e57d5d12a5f94f8f6ab001 Mon Sep 17 00:00:00 2001 From: drakeee Date: Mon, 16 Nov 2020 00:01:41 +0100 Subject: [PATCH] Updating cpp-sdk to version 44, implementing readers, bunch of stuff. --- CMakeLists.txt | 14 +- src/CArgReader.h | 12 ++ src/CLuaDefs.cpp | 54 ++++- src/CLuaDefs.h | 5 +- src/CLuaResourceImpl.cpp | 2 + src/CLuaScriptRuntime.cpp | 2 +- src/CLuaScriptRuntime.h | 2 +- src/CVehModels.cpp | 62 ------ src/CVehModels.h | 124 ------------ src/CVehMods.cpp | 33 --- src/CVehMods.h | 43 ---- src/Defs/CLuaAltFuncDefs.cpp | 151 ++++++++++++-- src/Defs/CLuaAltFuncDefs.h | 10 + src/Defs/Entity/CLuaBlipDefs.cpp | 170 ++++++++-------- src/Defs/Entity/CLuaBlipDefs.h | 4 +- src/Defs/Entity/CLuaEntityDefs.cpp | 26 ++- src/Defs/Entity/CLuaEntityDefs.h | 4 +- src/Defs/Entity/CLuaPlayerDefs.cpp | 2 +- src/Defs/Entity/CLuaVehicleDefs.cpp | 4 +- src/Main.cpp | 47 ----- src/Main.h | 9 +- src/VehicleModels.hpp | 301 ++++++++++++++++++++++++++++ vendors/cpp-sdk | 2 +- 23 files changed, 653 insertions(+), 430 deletions(-) delete mode 100644 src/CVehModels.cpp delete mode 100644 src/CVehModels.h delete mode 100644 src/CVehMods.cpp delete mode 100644 src/CVehMods.h create mode 100644 src/VehicleModels.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 37f6560..2ccd490 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,13 +34,17 @@ add_library( ${PROJECT_SOURCE_FILES} ) +target_compile_definitions(${PROJECT_MODULE_NAME} PRIVATE + -DALT_SERVER_API +) + if(UNIX) target_link_libraries(${PROJECT_MODULE_NAME} ${PROJECT_SOURCE_DIR}/vendors/luajit/lib/linux64/libluajit.so) - target_compile_options(${PROJECT_MODULE_NAME} PRIVATE - -fuse-ld=lld - -std=c++17 - -lstdc++fs - ) + #target_compile_options(${PROJECT_MODULE_NAME} PRIVATE + # -fuse-ld=lld + # -std=c++17 + # -lstdc++fs + #) else() target_link_libraries(${PROJECT_MODULE_NAME} ${PROJECT_SOURCE_DIR}/vendors/luajit/lib/win64/lua51.lib diff --git a/src/CArgReader.h b/src/CArgReader.h index 37b99bf..7c253d2 100644 --- a/src/CArgReader.h +++ b/src/CArgReader.h @@ -9,6 +9,7 @@ class CArgReader { m_luaVM = L; m_stackIndex = 1; + m_stackNum = lua_gettop(L); m_errorOccured = false; pendingFunctionRefOut = nullptr; pendingFunctionRef = -1; @@ -19,6 +20,16 @@ class CArgReader } + const char* ToString() + { + return luaL_tolstring(m_luaVM, m_stackIndex++, NULL); + } + + int GetArgNum() + { + return m_stackNum; + } + bool IsNextType(int type) { return ((lua_type(m_luaVM, m_stackIndex + 1)) == type); @@ -409,6 +420,7 @@ class CArgReader int pendingFunctionRef; int m_stackIndex; + int m_stackNum; bool m_errorOccured; std::vector m_errorMessages; diff --git a/src/CLuaDefs.cpp b/src/CLuaDefs.cpp index 89c0357..aa1ef25 100644 --- a/src/CLuaDefs.cpp +++ b/src/CLuaDefs.cpp @@ -420,6 +420,14 @@ alt::IBaseObject* lua_tobaseobject(lua_State* L, int idx) return reinterpret_cast(*((void**)lua_touserdata(L, idx))); } + +int lua_isinteger(lua_State* L, int index) +{ + int32_t x = (int32_t)lua_tointeger(L, index); + lua_Number n = lua_tonumber(L, index); + return ((lua_Number)x == n); +} + alt::MValue lua_tomvalue(lua_State* L, int idx) { alt::MValue mValue; @@ -428,7 +436,10 @@ alt::MValue lua_tomvalue(lua_State* L, int idx) switch (argType) { case LUA_TNUMBER: - mValue = Core->CreateMValueDouble(lua_tonumber(L, idx)); + if (lua_isinteger(L, idx)) + mValue = Core->CreateMValueInt(lua_tointeger(L, idx)); + else + mValue = Core->CreateMValueDouble(lua_tonumber(L, idx)); break; case LUA_TBOOLEAN: mValue = Core->CreateMValueBool(lua_toboolean(L, idx)); @@ -441,7 +452,6 @@ alt::MValue lua_tomvalue(lua_State* L, int idx) break; case LUA_TTABLE: { - //Core->LogInfo("Save table"); auto tempDict = Core->CreateMValueDict(); auto tempList = Core->CreateMValueList(); @@ -630,4 +640,44 @@ void lua_getdebuginfo(lua_State* L, lua_Debug& debugInfo) break; } } +} + +const char* luaL_tolstring(lua_State* L, int idx, size_t* len) +{ + if (luaL_callmeta(L, idx, "__tostring")) + { + if (!lua_isstring(L, -1)) + luaL_error(L, "'__tostring' must return a string"); + } + else { + switch (lua_type(L, idx)) { + case LUA_TNUMBER: { + if (lua_isinteger(L, idx)) + lua_pushfstring(L, "%d", lua_tointeger(L, idx)); + else + lua_pushfstring(L, "%f", lua_tonumber(L, idx)); + break; + } + case LUA_TSTRING: + lua_pushvalue(L, idx); + break; + case LUA_TBOOLEAN: + lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); + break; + case LUA_TNIL: + lua_pushliteral(L, "nil"); + break; + default: { + int tt = luaL_getmetafield(L, idx, "__name"); /* try name */ + const char* kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : + luaL_typename(L, idx); + lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx)); + if (tt != LUA_TNIL) + lua_remove(L, -2); /* remove '__name' */ + break; + } + } + } + + return lua_tolstring(L, -1, len); } \ No newline at end of file diff --git a/src/CLuaDefs.h b/src/CLuaDefs.h index 1de7f2a..8fdadd2 100644 --- a/src/CLuaDefs.h +++ b/src/CLuaDefs.h @@ -46,6 +46,7 @@ void lua_pushresource(lua_State* L, alt::IResource* resource, bool refUserData = void lua_pushstringarray(lua_State* L, const alt::Array& array); int lua_functionref(lua_State* L, int idx); +int lua_isinteger(lua_State* L, int idx); void lua_todict(lua_State* L, int idx); alt::MValue lua_tomvalue(lua_State* L, int indx); alt::IBaseObject* lua_tobaseobject(lua_State* L, int idx); @@ -53,4 +54,6 @@ alt::IBaseObject* lua_tobaseobject(lua_State* L, int idx); void lua_stacktrace(lua_State* L, const char* stackName = "Unknown"); void lua_dumptable(lua_State* L, int idx, int level = 0); -void lua_getdebuginfo(lua_State* L, lua_Debug& debugInfo); \ No newline at end of file +void lua_getdebuginfo(lua_State* L, lua_Debug& debugInfo); + +const char* luaL_tolstring(lua_State* L, int idx, size_t* len); \ No newline at end of file diff --git a/src/CLuaResourceImpl.cpp b/src/CLuaResourceImpl.cpp index 8b32dde..a2868f6 100644 --- a/src/CLuaResourceImpl.cpp +++ b/src/CLuaResourceImpl.cpp @@ -225,6 +225,8 @@ bool CLuaResourceImpl::OnEvent(const alt::CEvent* ev) void CLuaResourceImpl::OnTick() { + this->TriggerResourceLocalEvent("tick", {}); + #ifndef NDEBUG //this->_core->LogInfo("CLuaResourceImpl::OnTick"); #endif diff --git a/src/CLuaScriptRuntime.cpp b/src/CLuaScriptRuntime.cpp index 359132f..d8ce2c1 100644 --- a/src/CLuaScriptRuntime.cpp +++ b/src/CLuaScriptRuntime.cpp @@ -2,7 +2,7 @@ CLuaScriptRuntime::CLuaScriptRuntime() { - CVehModels::Instance(); //instance class once for further usage + VehicleModels::Instance(); //instance class once for further usage this->RegisterServerCallback( alt::CEvent::Type::PLAYER_CONNECT, diff --git a/src/CLuaScriptRuntime.h b/src/CLuaScriptRuntime.h index 09f150d..f6a1b01 100644 --- a/src/CLuaScriptRuntime.h +++ b/src/CLuaScriptRuntime.h @@ -50,7 +50,7 @@ class CLuaScriptRuntime : public alt::IScriptRuntime ~CLuaScriptRuntime() {}; private: - const semver::version version{ 0, 3, 22, semver::prerelease::dev }; + const semver::version version{ 0, 4, 0, semver::prerelease::dev }; std::map resources; EventsCallbacks eventsCallbacks; EventsGetter eventsGetter; diff --git a/src/CVehModels.cpp b/src/CVehModels.cpp deleted file mode 100644 index ae7eceb..0000000 --- a/src/CVehModels.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include - -CVehModels::CVehModels(void) : - core(&alt::ICore::Instance()) -{ - //Try to read the file - this->filePtr = fopen(filePath, "rb"); - if (this->filePtr != NULL) - { - //Read file version string and number - fread(&this->fileHeader, sizeof(Header), 1, this->filePtr); - - //Check if first two byte is correct - if (this->fileHeader.versionString[0] != 'V' || this->fileHeader.versionString[1] != 'E') - { - this->core->LogError("Invalid vehmodels.bin data file. Download new from altv.mp"); - return; - } - - //Check if FILE_VERSIOn is correct - if (this->fileHeader.versionNumber != FILE_VERSION) - { - core->LogError("vehmodels.bin structure (v." + std::to_string(this->fileHeader.versionNumber) + ") doesn't fit server structure (v." + std::to_string(FILE_VERSION) + ")"); - return; - } - - //Loop till we can read data - for (;;) - { - //Try to read the vehicle header - VehicleHeader vehicleHeader; - - //If there are no information break the loop - if (!fread(&vehicleHeader, sizeof(VehicleHeader), 1, this->filePtr)) - break; - - //Create a pointer which will hold the datas - VehicleInfo* vehicleInfo = new VehicleInfo(vehicleHeader.vehicleNameLength + (size_t)1); - fread((*vehicleInfo).vehicleName, sizeof(char) * vehicleHeader.vehicleNameLength, 1, this->filePtr); //read vehicle name first because its length changing - vehicleInfo->vehicleHash = vehicleHeader.vehicleHash; - - //Skip some bytes - int skip = ( - sizeof(vehicleInfo->vehicleHash) + - sizeof(vehicleInfo->vehicleName) - ); - - //Read the rest of the struct - fread(&(*vehicleInfo).vehicleType, sizeof(VehicleInfo) - skip, 1, this->filePtr); //skip 4 byte (vehicle hash) and 8 byte (pointer), then start reading from vehicleType address - - vehicleInfos_hashes[vehicleInfo->vehicleHash] = vehicleInfo; - vehicleInfos_strings[vehicleInfo->vehicleName] = vehicleInfo; - - //core->LogInfo("ModKits: " + std::string(vehicleInfo->vehicleName) + " - " + std::to_string(vehicleInfo->vehicleModkits[0]) + " - " + std::to_string(vehicleInfo->vehicleModkits[1])); - } - } -} - -CVehModels::~CVehModels() -{ - -} \ No newline at end of file diff --git a/src/CVehModels.h b/src/CVehModels.h deleted file mode 100644 index 1aad382..0000000 --- a/src/CVehModels.h +++ /dev/null @@ -1,124 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#ifndef FILE_VERSION - #define FILE_VERSION 2 -#endif - -class CVehModels -{ -public: - - enum class VehicleType : uint8_t { - INVALID, - PED, - AUTOMOBILE, - PLANE, - TRAILER, - QUAD_BIKE, - SUBMARINE_CAR, - AMPHIBIOUS_AUTOMOBILE, - AMPHIBIOUS_QUAD_BIKE, - HELI, - BLIMP, - AUTOGYRO, - BIKE, - BMX, - BOAT, - TRAIN, - SUBMARINE, - OBJECT - }; - -#pragma pack(push, 1) - struct Header - { - char versionString[2]; - uint16_t versionNumber; - }; - - struct VehicleHeader - { - uint32_t vehicleHash; - uint8_t vehicleNameLength; - }; - - struct VehicleInfo - { - VehicleInfo(size_t vehicleNameLength) - { - this->vehicleName = new char[vehicleNameLength]; - memset(this->vehicleName, '\0', (sizeof(char) * vehicleNameLength)); - } - - ~VehicleInfo() { delete[] vehicleName; } - - uint32_t vehicleHash; - char* vehicleName = nullptr; - VehicleType vehicleType = VehicleType::INVALID; - uint8_t vehicleWheelsCount = -1; - bool vehicleHasArmoredWindows = false; - uint8_t vehiclePrimaryColor = -1; - uint8_t vehicleSecondaryColor = -1; - uint8_t vehiclePearlColor = -1; - uint8_t vehicleWheelsColor = -1; - uint8_t vehicleInteriorColor = -1; - uint8_t vehicleDashboardColor = -1; - uint16_t vehicleModkits[2] = { 0 }; - uint16_t vehicleExtras = 0; - uint16_t vehicleDefaultExtras = 0; - }; -#pragma pack(pop) - - typedef std::map HashMap; - typedef std::map StringMap; - - static CVehModels& Instance() - { - static CVehModels instance; - return instance; - } - - CVehModels(CVehModels const&) = delete; - void operator=(CVehModels const&) = delete; - - size_t GetVehicleCount(void) { return this->vehicleInfos_hashes.size(); } - - inline VehicleInfo* GetVehicleInfo(const char* name) - { - return this->vehicleInfos_strings[name]; - } - - inline VehicleInfo* GetVehicleInfo(int hash) - { - return this->vehicleInfos_hashes[hash]; - } - - inline VehicleInfo* GetVehicleInfo(alt::IVehicle* vehicle) - { - return this->GetVehicleInfo(vehicle->GetModel()); - } - - inline const HashMap& GetVehicleInfosByHash(void) const { return this->vehicleInfos_hashes; } - inline const StringMap& GetVehicleInfosByString(void) const { return this->vehicleInfos_strings; } - - const VehicleInfo* operator[](int modelHash) { return this->GetVehicleInfo(modelHash); } - const VehicleInfo* operator[](const char* name) { return this->GetVehicleInfo(name); } - -private: - CVehModels(void); - ~CVehModels(); - - alt::ICore* core = nullptr; - - const char* filePath = "./data/vehmodels.bin"; - FILE* filePtr = nullptr; - - Header fileHeader = Header(); - HashMap vehicleInfos_hashes; - StringMap vehicleInfos_strings; -}; \ No newline at end of file diff --git a/src/CVehMods.cpp b/src/CVehMods.cpp deleted file mode 100644 index 9357a6b..0000000 --- a/src/CVehMods.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include - -CVehMods::CVehMods() : - core(&alt::ICore::Instance()) -{ - this->filePtr = fopen(this->filePath, "rb"); - if (this->filePtr) - { - fread(&this->header, sizeof(ModsHeader), 1, this->filePtr); - - if (header.versionString[0] != 'M' || header.versionString[1] != 'O') - { - this->core->LogError("Invalid vehmods.bin data file. Download new from altv.mp"); - return; - } - - if (header.versionNumber != FILE_MODS_VERSION) - { - core->LogError("vehmods.bin structure (v." + std::to_string(this->header.versionNumber) + ") doesn't fit server structure (v." + std::to_string(FILE_MODS_VERSION) + ")"); - return; - } - - ModsInfoHeader test; - fread(&test, sizeof(ModsInfoHeader), 1, this->filePtr); - - core->LogInfo("Name size: " + std::to_string(test.modNameLength)); - } -} - -CVehMods::~CVehMods() -{ - -} \ No newline at end of file diff --git a/src/CVehMods.h b/src/CVehMods.h deleted file mode 100644 index ec00ac8..0000000 --- a/src/CVehMods.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include - -#ifndef FILE_MODS_VERSION - #define FILE_MODS_VERSION 1 -#endif - -class CVehMods -{ -public: -#pragma pack(push, 1) - struct ModsHeader - { - char versionString[2]; - uint32_t versionNumber; - }; - - struct ModsInfoHeader - { - uint16_t modNameLength; - }; -#pragma pack(pop) - - static CVehMods& Instance() - { - static CVehMods instance; - return instance; - } - - CVehMods(CVehMods const&) = delete; - void operator=(CVehMods const&) = delete; - -private: - CVehMods(); - ~CVehMods(); - - const char* filePath = "./data/vehmods.bin"; - FILE* filePtr; - alt::ICore* core; - - ModsHeader header = ModsHeader(); -}; \ No newline at end of file diff --git a/src/Defs/CLuaAltFuncDefs.cpp b/src/Defs/CLuaAltFuncDefs.cpp index 4c56311..4cdd2fd 100644 --- a/src/Defs/CLuaAltFuncDefs.cpp +++ b/src/Defs/CLuaAltFuncDefs.cpp @@ -1,4 +1,5 @@ #include +#include const char* CLuaAltFuncDefs::ClassName = "alt"; void CLuaAltFuncDefs::Init(lua_State* L) @@ -34,6 +35,15 @@ void CLuaAltFuncDefs::Init(lua_State* L) lua_globalfunction(L, "export", Export); lua_globalfunction(L, "hash", Hash); + lua_globalfunction(L, "isDebug", IsDebug); + lua_globalfunction(L, "fileExists", FileExists); + lua_globalfunction(L, "fileRead", FileRead); + lua_globalfunction(L, "getEntityByID", GetEntityByID); + lua_globalfunction(L, "getRootDirectory", GetRootDirectory); + lua_globalfunction(L, "getPlayersByName", GetPlayersByName); + lua_globalfunction(L, "getNetTime", GetNetTime); + lua_globalfunction(L, "getVersion", GetVersion); + lua_globalfunction(L, "getBranch", GetBranch); lua_globalfunction(L, "startResource", StartResource); lua_globalfunction(L, "stopResource", StopResource); @@ -115,6 +125,12 @@ void CLuaAltFuncDefs::Init(lua_State* L) lua_pushcclosure(L, CLuaAltFuncDefs::ipairs, 1); lua_rawset(L, -3); + lua_pushnumber(L, alt::DEFAULT_DIMENSION); + lua_setglobal(L, "DEFAULT_DIMENSION"); + + lua_pushnumber(L, alt::GLOBAL_DIMENSION); + lua_setglobal(L, "GLOBAL_DIMENSION"); + /*lua_pushstring(L, "dofile"); lua_pushcfunction(L, dofile); lua_rawset(L, -3);*/ @@ -334,6 +350,115 @@ int CLuaAltFuncDefs::Export(lua_State* L) return 0; } +int CLuaAltFuncDefs::IsDebug(lua_State* L) +{ + lua_pushboolean(L, Core->IsDebug()); + return 1; +} + +int CLuaAltFuncDefs::FileExists(lua_State* L) +{ + std::string filePath; + + CArgReader argReader(L); + argReader.ReadString(filePath); + + if (argReader.HasAnyError()) + { + argReader.GetErrorMessages(); + return 0; + } + + lua_pushboolean(L, Core->FileExists(filePath)); + + return 1; +} + +int CLuaAltFuncDefs::FileRead(lua_State* L) +{ + std::string filePath; + + CArgReader argReader(L); + argReader.ReadString(filePath); + + if (argReader.HasAnyError()) + { + argReader.GetErrorMessages(); + return 0; + } + + lua_pushstring(L, Core->FileRead(filePath).CStr()); + + return 1; +} + +int CLuaAltFuncDefs::GetEntityByID(lua_State* L) +{ + alt::IEntity* entity; + + CArgReader argReader(L); + argReader.ReadBaseObject(entity); + + if (argReader.HasAnyError()) + { + argReader.GetErrorMessages(); + return 0; + } + + lua_pushbaseobject(L, entity); + + return 1; +} + +int CLuaAltFuncDefs::GetRootDirectory(lua_State* L) +{ + lua_pushstring(L, Core->GetRootDirectory().CStr()); + return 1; +} + +int CLuaAltFuncDefs::GetPlayersByName(lua_State* L) +{ + std::string playersName; + + CArgReader argReader(L); + argReader.ReadString(playersName); + + if (argReader.HasAnyError()) + { + argReader.GetErrorMessages(); + return 0; + } + + lua_newtable(L); + auto playersFound = Core->GetPlayersByName(playersName); + for (size_t i = 0; i < playersFound.GetSize(); i++) + { + lua_pushnumber(L, (int)(i + 1)); + lua_pushbaseobject(L, playersFound[i].Get()); + lua_rawset(L, -3); + } + + return 1; +} + +int CLuaAltFuncDefs::GetNetTime(lua_State* L) +{ + lua_pushnumber(L, Core->GetNetTime()); + return 1; +} + +int CLuaAltFuncDefs::GetVersion(lua_State* L) +{ + lua_pushstring(L, Core->GetVersion().CStr()); + return 1; +} + +int CLuaAltFuncDefs::GetBranch(lua_State* L) +{ + lua_pushstring(L, Core->GetBranch().CStr()); + return 1; +} + int CLuaAltFuncDefs::Hash(lua_State* L) { std::string hashKey; @@ -709,41 +834,37 @@ int CLuaAltFuncDefs::tostringtest(lua_State* L) int CLuaAltFuncDefs::Log(lua_State* L, LogType logType) { - std::string message; - CArgReader argReader(L); - argReader.ReadString(message); - - //lua_getglobal(L, "_print"); + std::ostringstream message; - //lua_stacktrace(L, "Log"); - - if (argReader.HasAnyError()) + for (int i = 0; i < argReader.GetArgNum(); i++) { - argReader.GetErrorMessages(); - return 0; + message << alt::String(argReader.ToString()); + + if (i != (argReader.GetArgNum() - 1)) + message << '\t'; } switch (logType) { case LogType::LOG_COLORED: - Core->LogColored(message); + Core->LogColored(message.str()); break; case LogType::LOG_DEBUG: - Core->LogDebug(message); + Core->LogDebug(message.str()); break; case LogType::LOG_ERROR: - Core->LogError(message); + Core->LogError(message.str()); break; case LogType::LOG_INFO: - Core->LogInfo(message); + Core->LogInfo(message.str()); break; case LogType::LOG_WARNING: - Core->LogWarning(message); + Core->LogWarning(message.str()); break; } diff --git a/src/Defs/CLuaAltFuncDefs.h b/src/Defs/CLuaAltFuncDefs.h index 857d310..bf0edb2 100644 --- a/src/Defs/CLuaAltFuncDefs.h +++ b/src/Defs/CLuaAltFuncDefs.h @@ -60,6 +60,16 @@ class CLuaAltFuncDefs static int Export(lua_State* L); + static int IsDebug(lua_State* L); + static int FileExists(lua_State* L); + static int FileRead(lua_State* L); + static int GetEntityByID(lua_State* L); + static int GetRootDirectory(lua_State* L); + static int GetPlayersByName(lua_State* L); + static int GetNetTime(lua_State* L); + static int GetVersion(lua_State* L); + static int GetBranch(lua_State* L); + static int Hash(lua_State* L); static int tostringtest(lua_State* L); diff --git a/src/Defs/Entity/CLuaBlipDefs.cpp b/src/Defs/Entity/CLuaBlipDefs.cpp index b5843a8..dc5650c 100644 --- a/src/Defs/Entity/CLuaBlipDefs.cpp +++ b/src/Defs/Entity/CLuaBlipDefs.cpp @@ -10,10 +10,10 @@ void CLuaBlipDefs::Init(lua_State* L) lua_globalfunction(L, "isBlipAttached", IsAttached); lua_globalfunction(L, "getBlipAttachedTo", AttachedTo); lua_globalfunction(L, "getBlipType", GetBlipType); - lua_globalfunction(L, "setBlipSprite", SetSprite); + /*lua_globalfunction(L, "setBlipSprite", SetSprite); lua_globalfunction(L, "setBlipColor", SetColor); lua_globalfunction(L, "setBlipRoute", SetRoute); - lua_globalfunction(L, "setBlipRouteColor", SetRouteColor); + lua_globalfunction(L, "setBlipRouteColor", SetRouteColor);*/ lua_beginclass(L, ClassName, CLuaWorldObjectDefs::ClassName); { @@ -23,10 +23,10 @@ void CLuaBlipDefs::Init(lua_State* L) lua_classfunction(L, "isAttached", IsAttached); lua_classfunction(L, "getAttachedTo", AttachedTo); lua_classfunction(L, "getBlipType", GetBlipType); - lua_classfunction(L, "setSprite", SetSprite); + /*lua_classfunction(L, "setSprite", SetSprite); lua_classfunction(L, "setColor", SetColor); lua_classfunction(L, "setRoute", SetRoute); - lua_classfunction(L, "setRouteColor", SetRouteColor); + lua_classfunction(L, "setRouteColor", SetRouteColor);*/ lua_classvariable(L, "global", nullptr, "isGlobal"); @@ -34,10 +34,10 @@ void CLuaBlipDefs::Init(lua_State* L) lua_classvariable(L, "attached", nullptr, "isAttached"); lua_classvariable(L, "attachedTo", nullptr, "getAttachedTo"); lua_classvariable(L, "blipType", nullptr, "getBlipType"); - lua_classvariable(L, "sprite", "setSprite", nullptr); + /*lua_classvariable(L, "sprite", "setSprite", nullptr); lua_classvariable(L, "color", "setColor", nullptr); lua_classvariable(L, "route", "setRoute", nullptr); - lua_classvariable(L, "routeColor", "setRouteColor", nullptr); + lua_classvariable(L, "routeColor", "setRouteColor", nullptr);*/ } lua_endclass(L); @@ -195,82 +195,82 @@ int CLuaBlipDefs::GetBlipType(lua_State* L) return 1; } -int CLuaBlipDefs::SetSprite(lua_State* L) -{ - alt::IBlip* blip; - uint16_t sprite; - - CArgReader argReader(L); - argReader.ReadBaseObject(blip); - argReader.ReadNumber(sprite); - - if (argReader.HasAnyError()) - { - argReader.GetErrorMessages(); - return 0; - } - - blip->SetSprite(sprite); - - return 0; -} - -int CLuaBlipDefs::SetColor(lua_State* L) -{ - alt::IBlip* blip; - uint8_t color; - - CArgReader argReader(L); - argReader.ReadBaseObject(blip); - argReader.ReadNumber(color); - - if (argReader.HasAnyError()) - { - argReader.GetErrorMessages(); - return 0; - } - - blip->SetColor(color); - - return 0; -} - -int CLuaBlipDefs::SetRoute(lua_State* L) -{ - alt::IBlip* blip; - bool state; - - CArgReader argReader(L); - argReader.ReadBaseObject(blip); - argReader.ReadBool(state); - - if (argReader.HasAnyError()) - { - argReader.GetErrorMessages(); - return 0; - } - - blip->SetRoute(state); - - return 0; -} - -int CLuaBlipDefs::SetRouteColor(lua_State* L) -{ - alt::IBlip* blip; - uint8_t color; - - CArgReader argReader(L); - argReader.ReadBaseObject(blip); - argReader.ReadNumber(color); - - if (argReader.HasAnyError()) - { - argReader.GetErrorMessages(); - return 0; - } - - blip->SetRouteColor(color); - - return 0; -} \ No newline at end of file +//int CLuaBlipDefs::SetSprite(lua_State* L) +//{ +// alt::IBlip* blip; +// uint16_t sprite; +// +// CArgReader argReader(L); +// argReader.ReadBaseObject(blip); +// argReader.ReadNumber(sprite); +// +// if (argReader.HasAnyError()) +// { +// argReader.GetErrorMessages(); +// return 0; +// } +// +// blip->SetSprite(sprite); +// +// return 0; +//} +// +//int CLuaBlipDefs::SetColor(lua_State* L) +//{ +// alt::IBlip* blip; +// uint8_t color; +// +// CArgReader argReader(L); +// argReader.ReadBaseObject(blip); +// argReader.ReadNumber(color); +// +// if (argReader.HasAnyError()) +// { +// argReader.GetErrorMessages(); +// return 0; +// } +// +// blip->SetColor(color); +// +// return 0; +//} +// +//int CLuaBlipDefs::SetRoute(lua_State* L) +//{ +// alt::IBlip* blip; +// bool state; +// +// CArgReader argReader(L); +// argReader.ReadBaseObject(blip); +// argReader.ReadBool(state); +// +// if (argReader.HasAnyError()) +// { +// argReader.GetErrorMessages(); +// return 0; +// } +// +// blip->SetRoute(state); +// +// return 0; +//} +// +//int CLuaBlipDefs::SetRouteColor(lua_State* L) +//{ +// alt::IBlip* blip; +// uint8_t color; +// +// CArgReader argReader(L); +// argReader.ReadBaseObject(blip); +// argReader.ReadNumber(color); +// +// if (argReader.HasAnyError()) +// { +// argReader.GetErrorMessages(); +// return 0; +// } +// +// blip->SetRouteColor(color); +// +// return 0; +//} \ No newline at end of file diff --git a/src/Defs/Entity/CLuaBlipDefs.h b/src/Defs/Entity/CLuaBlipDefs.h index 943899b..aca58e8 100644 --- a/src/Defs/Entity/CLuaBlipDefs.h +++ b/src/Defs/Entity/CLuaBlipDefs.h @@ -19,8 +19,8 @@ class CLuaBlipDefs static int AttachedTo(lua_State* L); static int GetBlipType(lua_State* L); - static int SetSprite(lua_State* L); + /*static int SetSprite(lua_State* L); static int SetColor(lua_State* L); static int SetRoute(lua_State* L); - static int SetRouteColor(lua_State* L); + static int SetRouteColor(lua_State* L);*/ }; \ No newline at end of file diff --git a/src/Defs/Entity/CLuaEntityDefs.cpp b/src/Defs/Entity/CLuaEntityDefs.cpp index 04cad7f..8683fb7 100644 --- a/src/Defs/Entity/CLuaEntityDefs.cpp +++ b/src/Defs/Entity/CLuaEntityDefs.cpp @@ -26,6 +26,9 @@ void CLuaEntityDefs::Init(lua_State* L) lua_beginclass(L, ClassName, CLuaWorldObjectDefs::ClassName); { + lua_classmeta(L, "__pairs", pairs); + lua_classmeta(L, "__ipairs", ipairs); + lua_classfunction(L, "getId", GetID); lua_classfunction(L, "getNetworkOwner", GetNetworkOwner); lua_classfunction(L, "getModel", GetModel); @@ -37,6 +40,7 @@ void CLuaEntityDefs::Init(lua_State* L) lua_classfunction(L, "getSyncedMetaData", GetSyncedMetaData); lua_classfunction(L, "hasStreamSyncedMetaData", HasStreamSyncedMetaData); lua_classfunction(L, "getStreamSyncedMetaData", GetStreamSyncedMetaData); + lua_classfunction(L, "getAll", ipairs); #ifdef ALT_SERVER_API lua_classfunction(L, "setSyncedMetaData", SetSyncedMetaData); @@ -49,12 +53,13 @@ void CLuaEntityDefs::Init(lua_State* L) lua_classvariable(L, "networkOwner", nullptr, "getNetworkOwner"); lua_classvariable(L, "model", nullptr, "getModel"); lua_classvariable(L, "rotation", "setRotation", "getRotation"); + lua_classvariable(L, "all", nullptr, "getAll"); } lua_endclass(L); } -int CLuaEntityDefs::ToString(lua_State* L) +int CLuaEntityDefs::tostring(lua_State* L) { alt::IEntity* entity; @@ -72,6 +77,25 @@ int CLuaEntityDefs::ToString(lua_State* L) return 1; } +int CLuaEntityDefs::pairs(lua_State* L) +{ + return 0; +} + +int CLuaEntityDefs::ipairs(lua_State* L) +{ + lua_newtable(L); + auto allEntites = alt::ICore::Instance().GetEntities(); + for (size_t i = 0; i < allEntites.GetSize(); i++) + { + lua_pushnumber(L, (int)(i + 1)); + lua_pushbaseobject(L, allEntites[i].Get()); + lua_rawset(L, -3); + } + + return 1; +} + int CLuaEntityDefs::GetID(lua_State* L) { alt::IEntity *entity; diff --git a/src/Defs/Entity/CLuaEntityDefs.h b/src/Defs/Entity/CLuaEntityDefs.h index be1cc57..d4106ba 100644 --- a/src/Defs/Entity/CLuaEntityDefs.h +++ b/src/Defs/Entity/CLuaEntityDefs.h @@ -9,7 +9,9 @@ class CLuaEntityDefs static void Init(lua_State* L); private: - static int ToString(lua_State* L); + static int tostring(lua_State* L); + static int pairs(lua_State* L); + static int ipairs(lua_State* L); static int GetID(lua_State* L); diff --git a/src/Defs/Entity/CLuaPlayerDefs.cpp b/src/Defs/Entity/CLuaPlayerDefs.cpp index 6d4c1a0..d92b257 100644 --- a/src/Defs/Entity/CLuaPlayerDefs.cpp +++ b/src/Defs/Entity/CLuaPlayerDefs.cpp @@ -173,7 +173,7 @@ int CLuaPlayerDefs::tostring(lua_State* L) } CLuaScriptRuntime* runtime = &CLuaScriptRuntime::Instance(); - auto vehModels = &CVehModels::Instance(); + auto vehModels = &VehicleModels::Instance(); alt::StringView type(alt::String("userdata:") + runtime->GetBaseObjectType(player) + alt::String(":") + player->GetName()); diff --git a/src/Defs/Entity/CLuaVehicleDefs.cpp b/src/Defs/Entity/CLuaVehicleDefs.cpp index 442553f..83f27d8 100644 --- a/src/Defs/Entity/CLuaVehicleDefs.cpp +++ b/src/Defs/Entity/CLuaVehicleDefs.cpp @@ -388,7 +388,7 @@ void CLuaVehicleDefs::Init(lua_State* L) lua_endclass(L); lua_newtable(L); - for (auto vehicle : CVehModels::Instance().GetVehicleInfosByHash()) + for (auto vehicle : VehicleModels::Instance().GetVehicleInfosByHash()) { lua_pushnumber(L, vehicle.first); lua_pushstring(L, vehicle.second->vehicleName); @@ -473,7 +473,7 @@ int CLuaVehicleDefs::tostring(lua_State* L) } CLuaScriptRuntime* runtime = &CLuaScriptRuntime::Instance(); - auto vehModels = &CVehModels::Instance(); + auto vehModels = &VehicleModels::Instance(); alt::StringView type("userdata:" + runtime->GetBaseObjectType(vehicle) + ":" + vehModels->GetVehicleInfo(vehicle->GetModel())->vehicleName); diff --git a/src/Main.cpp b/src/Main.cpp index 23adebf..79ed70b 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,12 +1,6 @@ #include "Main.h" #include -//#include -//#undef min -//#undef max -// -//#include - alt::ICore* Core; EXPORT bool altMain(alt::ICore* _core) @@ -20,47 +14,6 @@ EXPORT bool altMain(alt::ICore* _core) Core = _core; - //qtl::mysql::database db; - //db.open("localhost", "root", "", "riserpg", 0, 3307); - - //auto start = std::chrono::high_resolution_clock::now(); - //for (int i = 0; i < 1000; i++) - //{ - // db.query("SELECT * FROM `accounts`", [](uint32_t id, const std::string& name) - // { - // //printf("ID=\"%d\", Name=\"%s\"\n", id, name.data()); - // }); - //} - //auto stop = std::chrono::high_resolution_clock::now(); - //auto duration = std::chrono::duration_cast(stop - start); - //printf("qtl: Query took %dms\n", duration.count()); - - - /*sql::DatabaseBase *connection = sql::DatabaseManager::Connect("mariadb", "localhost", "root", "", "riserpg", 3307); - start = std::chrono::high_resolution_clock::now(); - for (int i = 0; i < 1000; i++) - { - auto qResult = connection->Query("SELECT * FROM `accounts`"); - //Core->LogInfo("rowsnum: " + std::to_string(qResult->rowsNum)); - /*for (uint32_t i = 0; i < qResult->rowsNum; i++) - { - auto& b = qResult->rows[i]; - for (auto& [key, value] : qResult->rows[i].results2) - { - auto key1 = key; - auto &value1 = value; - //Core->LogInfo("Key: " + key); - //Core->LogInfo("Value: " + alt::String((char*)value->buffer)); - } - } - }*/ - /*stop = std::chrono::high_resolution_clock::now(); - duration = std::chrono::duration_cast(stop - start); - printf("LuaSQL: Query took %dms\n", duration.count());*/ - - //Core->LogInfo("qResult: " + std::to_string(qResult)); - - return true; } diff --git a/src/Main.h b/src/Main.h index c409a4c..91d61dc 100644 --- a/src/Main.h +++ b/src/Main.h @@ -2,7 +2,10 @@ #define MODULE_VERSION "0.1.0" #define ADDITIONAL_MODULE_FOLDER "modules" -#define ALT_SERVER_API + +#ifndef ALT_SERVER_API + #define ALT_SERVER_API +#endif #ifdef _WIN32 static const char* preferred_separator = "\\"; @@ -42,8 +45,8 @@ extern alt::ICore* Core; typedef alt::Vector Vector3fp; -#include "CVehModels.h" -#include "CVehMods.h" +#include "VehicleModels.hpp" +//#include "CVehMods.h" #include "LRGBA.h" #include "CLuaDefs.h" diff --git a/src/VehicleModels.hpp b/src/VehicleModels.hpp new file mode 100644 index 0000000..51acd1f --- /dev/null +++ b/src/VehicleModels.hpp @@ -0,0 +1,301 @@ +#pragma once + +#include +#include +#include +#include + +#define INVALID_MODKIT 0xFFFF +#ifndef FILE_VERSION + #define FILE_VERSION 2 +#endif + +#ifndef FILE_MODS_VERSION + #define FILE_MODS_VERSION 1 +#endif + +class VehicleMods +{ +public: +#pragma pack(push, 1) + struct ModHeader + { + char versionString[2]; + uint16_t versionNumber; + }; + + struct ModKit + { + uint16_t id = INVALID_MODKIT; + std::string name; + std::unordered_map> mods; + }; +#pragma pack(pop) + + inline ModKit* GetVehicleMod(uint16_t index) + { + return this->modKits.at(index); + } + + static VehicleMods& Instance() + { + static VehicleMods instance; + return instance; + } + + VehicleMods(VehicleMods const&) = delete; + void operator=(VehicleMods const&) = delete; + +private: + VehicleMods() : core(&alt::ICore::Instance()) + { + this->filePtr = fopen(this->filePath, "rb"); + if (this->filePtr) + { + fread(&this->header, sizeof(ModHeader), 1, this->filePtr); + + if (header.versionString[0] != 'M' || header.versionString[1] != 'O') + { + this->core->LogError("Invalid vehmods.bin data file. Download new from altv.mp"); + return; + } + + if (header.versionNumber != FILE_MODS_VERSION) + { + core->LogError("vehmods.bin structure (v." + std::to_string(this->header.versionNumber) + ") doesn't fit server structure (v." + std::to_string(FILE_MODS_VERSION) + ")"); + return; + } + + for (;;) + { + ModKit* modKit = new ModKit; + if (!fread(&modKit->id, sizeof(uint16_t), 1, this->filePtr)) + break; + + uint16_t nameLen = 0; + fread(&nameLen, sizeof(uint16_t), 1, this->filePtr); + + modKit->name.resize(nameLen); + fread(modKit->name.data(), sizeof(char), nameLen, this->filePtr); + + uint8_t modsCount = 0; + fread(&modsCount, sizeof(uint8_t), 1, this->filePtr); + + for (uint8_t i = 0; i < modsCount; ++i) + { + uint8_t modCat = 0; + fread(&modCat, sizeof(uint8_t), 1, this->filePtr); + uint8_t count = 0; + fread(&count, sizeof(uint8_t), 1, this->filePtr); + + std::vector& comps = modKit->mods[modCat]; + for (uint8_t j = 0; j < count; ++j) + { + uint16_t compId = 0; + fread(&compId, sizeof(uint16_t), 1, this->filePtr); + comps.push_back(compId); + } + } + + this->modKits[modKit->id] = modKit; + } + +#ifndef NDEBUG + core->LogInfo("Loaded " + std::to_string(this->modKits.size()) + " modkits"); +#endif + + fclose(this->filePtr); + } + } + ~VehicleMods() {} + + const char* filePath = "./data/vehmods.bin"; + FILE* filePtr; + alt::ICore* core; + + ModHeader header = ModHeader(); + std::map modKits; +}; + +class VehicleModels +{ +public: + + enum class VehicleType : uint8_t { + INVALID, + PED, + AUTOMOBILE, + PLANE, + TRAILER, + QUAD_BIKE, + SUBMARINE_CAR, + AMPHIBIOUS_AUTOMOBILE, + AMPHIBIOUS_QUAD_BIKE, + HELI, + BLIMP, + AUTOGYRO, + BIKE, + BMX, + BOAT, + TRAIN, + SUBMARINE, + OBJECT + }; + +#pragma pack(push, 1) + struct Header + { + char versionString[2]; + uint16_t versionNumber; + }; + + struct VehicleHeader + { + uint32_t vehicleHash; + uint8_t vehicleNameLength; + }; + + struct VehicleInfo + { + VehicleInfo(size_t vehicleNameLength) + { + this->vehicleName = new char[vehicleNameLength]; + memset(this->vehicleName, '\0', (sizeof(char) * vehicleNameLength)); + } + + ~VehicleInfo() { delete[] vehicleName; } + + uint32_t vehicleHash; + char* vehicleName = nullptr; + VehicleType vehicleType = VehicleType::INVALID; + uint8_t vehicleWheelsCount = -1; + bool vehicleHasArmoredWindows = false; + uint8_t vehiclePrimaryColor = -1; + uint8_t vehicleSecondaryColor = -1; + uint8_t vehiclePearlColor = -1; + uint8_t vehicleWheelsColor = -1; + uint8_t vehicleInteriorColor = -1; + uint8_t vehicleDashboardColor = -1; + uint16_t vehicleModkitsTemp[2] = { 0 }; + uint16_t vehicleExtras = 0; + uint16_t vehicleDefaultExtras = 0; + VehicleMods::ModKit* vehicleModkits[2] = { nullptr }; + }; +#pragma pack(pop) + + typedef std::map HashMap; + typedef std::map StringMap; + + static VehicleModels& Instance() + { + static VehicleModels instance; + return instance; + } + + VehicleModels(VehicleModels const&) = delete; + void operator=(VehicleModels const&) = delete; + + size_t GetVehicleCount(void) { return this->vehicleInfos_hashes.size(); } + + inline VehicleInfo* GetVehicleInfo(const char* name) + { + return this->vehicleInfos_strings[name]; + } + + inline VehicleInfo* GetVehicleInfo(int hash) + { + return this->vehicleInfos_hashes[hash]; + } + + inline VehicleInfo* GetVehicleInfo(alt::IVehicle* vehicle) + { + return this->GetVehicleInfo(vehicle->GetModel()); + } + + inline const HashMap& GetVehicleInfosByHash(void) const { return this->vehicleInfos_hashes; } + inline const StringMap& GetVehicleInfosByString(void) const { return this->vehicleInfos_strings; } + + const VehicleInfo* operator[](int modelHash) { return this->GetVehicleInfo(modelHash); } + const VehicleInfo* operator[](const char* name) { return this->GetVehicleInfo(name); } + +private: + VehicleModels(void) : core(&alt::ICore::Instance()) + { + //Try to read the file + this->filePtr = fopen(filePath, "rb"); + if (this->filePtr != NULL) + { + //Read file version string and number + fread(&this->fileHeader, sizeof(Header), 1, this->filePtr); + + //Check if first two byte is correct + if (this->fileHeader.versionString[0] != 'V' || this->fileHeader.versionString[1] != 'E') + { + this->core->LogError("Invalid vehmodels.bin data file. Download new from altv.mp"); + return; + } + + //Check if FILE_VERSIOn is correct + if (this->fileHeader.versionNumber != FILE_VERSION) + { + core->LogError("vehmodels.bin structure (v." + std::to_string(this->fileHeader.versionNumber) + ") doesn't fit server structure (v." + std::to_string(FILE_VERSION) + ")"); + return; + } + + //Loop till we can read data + for (;;) + { + //Try to read the vehicle header + VehicleHeader vehicleHeader; + + //If there are no information break the loop + if (!fread(&vehicleHeader, sizeof(VehicleHeader), 1, this->filePtr)) + break; + + //Create a pointer which will hold the datas + VehicleInfo* vehicleInfo = new VehicleInfo(vehicleHeader.vehicleNameLength + (size_t)1); + fread((*vehicleInfo).vehicleName, sizeof(char) * vehicleHeader.vehicleNameLength, 1, this->filePtr); //read vehicle name first because its length changing + vehicleInfo->vehicleHash = vehicleHeader.vehicleHash; + + //Skip some bytes + int skip = ( + sizeof(vehicleInfo->vehicleHash) + + sizeof(vehicleInfo->vehicleName) + + sizeof(vehicleInfo->vehicleModkits) + ); + + //Read the rest of the struct + fread(&(*vehicleInfo).vehicleType, sizeof(VehicleInfo) - skip, 1, this->filePtr); //skip 4 byte (vehicle hash) and 8 byte (pointer), then start reading from vehicleType address + + vehicleInfos_hashes[vehicleInfo->vehicleHash] = vehicleInfo; + vehicleInfos_strings[vehicleInfo->vehicleName] = vehicleInfo; + + for (size_t i = 0; i < 2; i++) + { + if (vehicleInfo->vehicleModkitsTemp[i] == INVALID_MODKIT) + break; + + vehicleInfo->vehicleModkits[i] = this->modKits->GetVehicleMod(vehicleInfo->vehicleModkitsTemp[i]); + } + + } + +#ifndef NDEBUG + core->LogInfo("Loaded " + std::to_string(vehicleInfos_hashes.size()) + " vehicles information."); +#endif + fclose(this->filePtr); + } + } + ~VehicleModels(){} + + alt::ICore* core = nullptr; + + const char* filePath = "./data/vehmodels.bin"; + FILE* filePtr = nullptr; + + VehicleMods* modKits = &VehicleMods::Instance(); + + Header fileHeader = Header(); + HashMap vehicleInfos_hashes; + StringMap vehicleInfos_strings; +}; \ No newline at end of file diff --git a/vendors/cpp-sdk b/vendors/cpp-sdk index 6addd98..0b0b795 160000 --- a/vendors/cpp-sdk +++ b/vendors/cpp-sdk @@ -1 +1 @@ -Subproject commit 6addd980bdea3fb0aa3d3dfef1f400172cd3c708 +Subproject commit 0b0b795869103fd7ca9a50899663f7f5504f8461