Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Lua constants (#3022) #3039

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
22 changes: 22 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ SString CLuaMain::ms_strExpectedUndumpHash;
#include "luascripts/exports.lua.h"
#include "luascripts/inspect.lua.h"

#include "luascripts/constants_main.lua.h"
#include "luascripts/constants_elements.lua.h"
#include "luascripts/constants_vehicles.lua.h"
#include "luascripts/constants_weapons.lua.h"
#include "luascripts/constants_peds.lua.h"
#include "luascripts/constants_ui.lua.h"
#include "luascripts/constants_other.lua.h"

CLuaMain::CLuaMain(CLuaManager* pLuaManager, CResource* pResourceOwner, bool bEnableOOP)
{
// Initialise everything to be setup in the Start function
Expand Down Expand Up @@ -187,6 +195,20 @@ void CLuaMain::LoadEmbeddedScripts()
LoadScript(EmbeddedLuaCode::exports);
LoadScript(EmbeddedLuaCode::coroutine_debug);
LoadScript(EmbeddedLuaCode::inspect);

LoadScript(EmbeddedLuaCode::constantsMain);
LoadScript(EmbeddedLuaCode::constantsElements);
LoadScript(EmbeddedLuaCode::constantsVehicles);
LoadScript(EmbeddedLuaCode::constantsWeapons);
LoadScript(EmbeddedLuaCode::constantsPeds);
LoadScript(EmbeddedLuaCode::constantsUI);
LoadScript(EmbeddedLuaCode::constantsOther);

// To make `constants` table read-only
LoadScript(R"~LUA~(
setmetatable(constants, __readonly_table_meta__)
__readonly_table_meta__ = nil
)~LUA~");
DECLARE_PROFILER_SECTION(OnPostLoadScript)
}

Expand Down
22 changes: 22 additions & 0 deletions Server/mods/deathmatch/logic/lua/CLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ extern CNetServer* g_pRealNetServer;
#include "luascripts/exports.lua.h"
#include "luascripts/inspect.lua.h"

#include "luascripts/constants_main.lua.h"
#include "luascripts/constants_elements.lua.h"
#include "luascripts/constants_vehicles.lua.h"
#include "luascripts/constants_weapons.lua.h"
#include "luascripts/constants_peds.lua.h"
#include "luascripts/constants_ui.lua.h"
#include "luascripts/constants_other.lua.h"

CLuaMain::CLuaMain(CLuaManager* pLuaManager, CObjectManager* pObjectManager, CPlayerManager* pPlayerManager, CVehicleManager* pVehicleManager,
CBlipManager* pBlipManager, CRadarAreaManager* pRadarAreaManager, CMapManager* pMapManager, CResource* pResourceOwner, bool bEnableOOP)
{
Expand Down Expand Up @@ -244,6 +252,20 @@ void CLuaMain::LoadEmbeddedScripts()
LoadScript(EmbeddedLuaCode::exports);
LoadScript(EmbeddedLuaCode::coroutine_debug);
LoadScript(EmbeddedLuaCode::inspect);

LoadScript(EmbeddedLuaCode::constantsMain);
LoadScript(EmbeddedLuaCode::constantsElements);
LoadScript(EmbeddedLuaCode::constantsVehicles);
LoadScript(EmbeddedLuaCode::constantsWeapons);
LoadScript(EmbeddedLuaCode::constantsPeds);
LoadScript(EmbeddedLuaCode::constantsUI);
LoadScript(EmbeddedLuaCode::constantsOther);

// To make `constants` table read-only;
LoadScript(R"~LUA~(
setmetatable(constants, __readonly_table_meta__)
__readonly_table_meta__ = nil
)~LUA~");
}

void CLuaMain::RegisterModuleFunctions()
Expand Down
33 changes: 33 additions & 0 deletions Shared/mods/deathmatch/logic/luascripts/constants_elements.lua.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace EmbeddedLuaCode
{
const char* const constantsElements = R"~LUA~(
--[[
SERVER AND CLIENT.
Defines a constant variables available for server and client.
--]]

constants.ElementType = setmetatable({
Player = 'player',
Ped = 'ped',
Water = 'water',
Sound = 'sound',
Vehicle = 'vehicle',
Object = 'object',
Pickup = 'pickup',
Marker = 'marker',
Colshape = 'colshape',
Blip = 'blip',
RadarArea = 'radararea',
Team = 'team',
SpawnPoint = 'spawnpoint',
Console = 'console',
Projectile = 'projectile',
Effect = 'effect',
Light = 'light',
Searchlight = 'searchlight',
Shader = 'shader',
Texture = 'texture',
}, __readonly_table_meta__)

)~LUA~";
}
37 changes: 37 additions & 0 deletions Shared/mods/deathmatch/logic/luascripts/constants_main.lua.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace EmbeddedLuaCode
{
const char* const constantsMain = R"~LUA~(
--[[
SERVER AND CLIENT.
Defines a constant variables available for server and client.
--]]

__readonly_table_meta__ = {
__index = function(tbl, key)
local val = rawget(tbl, key)
if val then
return val
end
for k,v in pairs(tbl) do
if tostring(k):lower() == tostring(key):lower() then
return v
end
end
end,
__newindex = function(tbl, key)
return error('Table is read only!')
end,
__call = function(tbl, key)
if not key then return end
for k,v in pairs(tbl) do
if tostring(v):lower() == tostring(key):lower() then
return k
end
end
end
}

constants = {}

)~LUA~";
}
Loading
Loading