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

Luau #2815

Closed
wants to merge 11 commits into from
Closed

Luau #2815

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Client/mods/deathmatch/logic/CPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5095,6 +5095,14 @@ void CPacketHandler::Packet_ResourceStart(NetBitStreamInterface& bitStream)
bitStream.ReadBit(bEnableOOP);
}

ELuaVersion luaVersion = ELuaVersion::VLUA_5_1;
if (bitStream.Can(eBitStreamVersion::ResourceLuaVersion))
{
unsigned char ucVersion;
if (bitStream.Read(ucVersion))
luaVersion = static_cast<ELuaVersion>(ucVersion);
}

int iDownloadPriorityGroup = 0;
if (bitStream.Version() >= 0x62)
{
Expand All @@ -5116,7 +5124,7 @@ void CPacketHandler::Packet_ResourceStart(NetBitStreamInterface& bitStream)
bool bFatalError = false;

CResource* pResource = g_pClientGame->m_pResourceManager->Add(usResourceID, szResourceName, pResourceEntity, pResourceDynamicEntity, strMinServerReq,
strMinClientReq, bEnableOOP);
strMinClientReq, bEnableOOP, luaVersion);
if (pResource)
{
pResource->SetRemainingNoClientCacheScripts(usNoClientCacheScriptCount);
Expand Down
5 changes: 3 additions & 2 deletions Client/mods/deathmatch/logic/CResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern CClientGame* g_pClientGame;
int CResource::m_iShowingCursor = 0;

CResource::CResource(unsigned short usNetID, const char* szResourceName, CClientEntity* pResourceEntity, CClientEntity* pResourceDynamicEntity,
const CMtaVersion& strMinServerReq, const CMtaVersion& strMinClientReq, bool bEnableOOP)
const CMtaVersion& strMinServerReq, const CMtaVersion& strMinClientReq, bool bEnableOOP, ELuaVersion luaVersion)
{
m_uiScriptID = CIdArray::PopUniqueId(this, EIdClass::RESOURCE);
m_usNetID = usNetID;
Expand All @@ -33,6 +33,7 @@ CResource::CResource(unsigned short usNetID, const char* szResourceName, CClient
m_bLoadAfterReceivingNoClientCacheScripts = false;
m_strMinServerReq = strMinServerReq;
m_strMinClientReq = strMinClientReq;
m_LuaVersion = luaVersion;

if (szResourceName)
m_strResourceName.AssignLeft(szResourceName, MAX_RESOURCE_NAME_LENGTH);
Expand Down Expand Up @@ -84,7 +85,7 @@ CResource::CResource(unsigned short usNetID, const char* szResourceName, CClient
m_bOOPEnabled = bEnableOOP;
m_iDownloadPriorityGroup = 0;

m_pLuaVM = m_pLuaManager->CreateVirtualMachine(this, bEnableOOP);
m_pLuaVM = m_pLuaManager->CreateVirtualMachine(this, bEnableOOP, m_LuaVersion);
if (m_pLuaVM)
{
m_pLuaVM->SetScriptName(szResourceName);
Expand Down
4 changes: 3 additions & 1 deletion Client/mods/deathmatch/logic/CResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class CResource
{
public:
CResource(unsigned short usNetID, const char* szResourceName, CClientEntity* pResourceEntity, CClientEntity* pResourceDynamicEntity,
const CMtaVersion& strMinServerReq, const CMtaVersion& strMinClientReq, bool bEnableOOP);
const CMtaVersion& strMinServerReq, const CMtaVersion& strMinClientReq, bool bEnableOOP, ELuaVersion luaVersion);
~CResource();

unsigned short GetNetID() { return m_usNetID; };
Expand Down Expand Up @@ -135,6 +135,8 @@ class CResource
bool m_bOOPEnabled;
int m_iDownloadPriorityGroup;

ELuaVersion m_LuaVersion = ELuaVersion::VLUA_5_1;

// To control cursor show/hide
static int m_iShowingCursor;
bool m_bShowingCursor;
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/CResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ CResourceManager::~CResourceManager()
}

CResource* CResourceManager::Add(unsigned short usNetID, const char* szResourceName, CClientEntity* pResourceEntity, CClientEntity* pResourceDynamicEntity,
const CMtaVersion& strMinServerReq, const CMtaVersion& strMinClientReq, bool bEnableOOP)
const CMtaVersion& strMinServerReq, const CMtaVersion& strMinClientReq, bool bEnableOOP, ELuaVersion luaVersion)
{
CResource* pResource = new CResource(usNetID, szResourceName, pResourceEntity, pResourceDynamicEntity, strMinServerReq, strMinClientReq, bEnableOOP);
CResource* pResource = new CResource(usNetID, szResourceName, pResourceEntity, pResourceDynamicEntity, strMinServerReq, strMinClientReq, bEnableOOP, luaVersion);
if (pResource)
{
m_resources.push_back(pResource);
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/CResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CResourceManager
~CResourceManager();

CResource* Add(unsigned short usNetID, const char* szResourceName, CClientEntity* pResourceEntity, CClientEntity* pResourceDynamicEntity,
const CMtaVersion& strMinServerReq, const CMtaVersion& strMinClientReq, bool bEnableOOP);
const CMtaVersion& strMinServerReq, const CMtaVersion& strMinClientReq, bool bEnableOOP, enum ELuaVersion luaVersion);
CResource* GetResource(const char* szResourceName);
CResource* GetResourceFromNetID(unsigned short usNetID);
CResource* GetResourceFromScriptID(uint uiScriptID);
Expand Down
5 changes: 3 additions & 2 deletions Client/mods/deathmatch/logic/lua/CLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ SString CLuaMain::ms_strExpectedUndumpHash;
#include "luascripts/exports.lua.h"
#include "luascripts/inspect.lua.h"

CLuaMain::CLuaMain(CLuaManager* pLuaManager, CResource* pResourceOwner, bool bEnableOOP)
CLuaMain::CLuaMain(CLuaManager* pLuaManager, CResource* pResourceOwner, bool bEnableOOP, ELuaVersion luaVersion) :
m_LuaVersion(luaVersion)
{
// Initialise everything to be setup in the Start function
m_pLuaManager = pLuaManager;
Expand Down Expand Up @@ -137,7 +138,7 @@ void CLuaMain::InitVM()
assert(!m_luaVM);

// Create a new VM
m_luaVM = lua_open(this);
m_luaVM = VluaL_newstate(this, m_LuaVersion);
m_pLuaManager->OnLuaMainOpenVM(this, m_luaVM);

// Set the instruction count hook
Expand Down
3 changes: 2 additions & 1 deletion Client/mods/deathmatch/logic/lua/CLuaMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CLuaMain //: public CClient
{
public:
ZERO_ON_NEW
CLuaMain(class CLuaManager* pLuaManager, CResource* pResourceOwner, bool bEnableOOP);
CLuaMain(class CLuaManager* pLuaManager, CResource* pResourceOwner, bool bEnableOOP, ELuaVersion luaVersion);
~CLuaMain();

bool LoadScriptFromBuffer(const char* cpBuffer, unsigned int uiSize, const char* szFileName);
Expand Down Expand Up @@ -101,6 +101,7 @@ class CLuaMain //: public CClient
static SString ms_strExpectedUndumpHash;

bool m_bEnableOOP;
ELuaVersion m_LuaVersion = ELuaVersion::VLUA_5_1;

public:
CFastHashMap<const void*, CRefInfo> m_CallbackTable;
Expand Down
14 changes: 12 additions & 2 deletions Client/mods/deathmatch/logic/lua/CLuaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ CLuaManager::CLuaManager(CClientGame* pClientGame)
m_pGUIManager = pClientGame->GetGUIManager();
m_pRegisteredCommands = pClientGame->GetRegisteredCommands();

// Vlua libraries are located in deathmatch mod folder
const SString strDeathmatchPath = CalcMTASAPath("mods\\deathmatch");
SetDllDirectory(strDeathmatchPath);

// Init Vlua library
VluaL_init();

// Ensure lua was compiled with apichecks
#ifdef NDEBUG
#error "NDEBUG should not be defined"
Expand All @@ -54,12 +61,15 @@ CLuaManager::~CLuaManager()

// Clear the C functions
CLuaCFunctions::RemoveAllFunctions();

// Close Vlua library
VluaL_close();
}

CLuaMain* CLuaManager::CreateVirtualMachine(CResource* pResourceOwner, bool bEnableOOP)
CLuaMain* CLuaManager::CreateVirtualMachine(CResource* pResourceOwner, bool bEnableOOP, ELuaVersion luaVersion)
{
// Create it and add it to the list over VM's
CLuaMain* pLuaMain = new CLuaMain(this, pResourceOwner, bEnableOOP);
CLuaMain* pLuaMain = new CLuaMain(this, pResourceOwner, bEnableOOP, luaVersion);
m_virtualMachines.push_back(pLuaMain);
pLuaMain->InitVM();
return pLuaMain;
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/lua/CLuaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CLuaManager
CLuaManager(class CClientGame* pClientGame);
~CLuaManager();

CLuaMain* CreateVirtualMachine(CResource* pResourceOwner, bool bEnableOOP);
CLuaMain* CreateVirtualMachine(CResource* pResourceOwner, bool bEnableOOP, ELuaVersion luaVersion);
bool RemoveVirtualMachine(CLuaMain* vm);
CLuaMain* GetVirtualMachine(lua_State* luaVM);
void OnLuaMainOpenVM(CLuaMain* pLuaMain, lua_State* luaVM);
Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ bool CGame::Start(int iArgumentCount, char* szArguments[])
// Check Windows server is using correctly compiled Lua dll
#ifndef MTA_DEBUG
#ifdef WIN32
HMODULE hModule = LoadLibrary("lua5.1.dll");
HMODULE hModule = LoadLibrary("vlua.dll");
// Release server should not have this function
PVOID pFunc = static_cast<PVOID>(GetProcAddress(hModule, "luaX_is_apicheck_enabled"));
FreeLibrary(hModule);
Expand Down
16 changes: 15 additions & 1 deletion Server/mods/deathmatch/logic/CResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ bool CResource::Load()
m_bOOPEnabledInMetaXml = StringToBool(pNodeClientOOP->GetTagContent().c_str());
}

m_LuaVersion = ELuaVersion::VLUA_5_1;
m_ClientLuaVersion = ELuaVersion::VLUA_5_1;
CXMLNode* pNodeLua = pRoot->FindSubNode("lua", 0);

if (pNodeLua)
{
if (CXMLAttribute* pAttr = pNodeLua->GetAttributes().Find("server"))
m_LuaVersion = pAttr->GetValue() == "luau" ? ELuaVersion::VLUA_U : ELuaVersion::VLUA_5_1;
if (CXMLAttribute* pAttr = pNodeLua->GetAttributes().Find("client"))
m_ClientLuaVersion = pAttr->GetValue() == "luau" ? ELuaVersion::VLUA_U : ELuaVersion::VLUA_5_1;
if (CXMLAttribute* pAttr = pNodeLua->GetAttributes().Find("both"))
m_LuaVersion = m_ClientLuaVersion = pAttr->GetValue() == "luau" ? ELuaVersion::VLUA_U : ELuaVersion::VLUA_5_1;
}

m_iDownloadPriorityGroup = 0;
CXMLNode* pNodeDownloadPriorityGroup = pRoot->FindSubNode("download_priority_group", 0);

Expand Down Expand Up @@ -1181,7 +1195,7 @@ bool CResource::CreateVM(bool bEnableOOP)
{
if (!m_pVM)
{
m_pVM = g_pGame->GetLuaManager()->CreateVirtualMachine(this, bEnableOOP);
m_pVM = g_pGame->GetLuaManager()->CreateVirtualMachine(this, bEnableOOP, m_LuaVersion);
m_pResourceManager->NotifyResourceVMOpen(this, m_pVM);
}

Expand Down
6 changes: 6 additions & 0 deletions Server/mods/deathmatch/logic/CResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ class CResource : public EHS
* @return A pointer to CResourceFile on success, null otherwise
*/
CResourceFile* GetResourceFile(const SString& relativePath) const;

ELuaVersion GetLuaVersion() const { return m_LuaVersion; }
ELuaVersion GetClientLuaVersion() const { return m_ClientLuaVersion; }

public:
static std::list<CResource*> m_StartedResources;
Expand Down Expand Up @@ -431,6 +434,9 @@ class CResource : public EHS
bool m_bIsPersistent = false; // if true, the resource will remain even if it has no Dependents, mainly if started by the user or the startup
bool m_bDestroyed = false;

ELuaVersion m_LuaVersion = ELuaVersion::VLUA_5_1;
ELuaVersion m_ClientLuaVersion = ELuaVersion::VLUA_5_1;

CXMLNode* m_pNodeSettings = nullptr; // Settings XML node, read from meta.xml and copied into it's own instance
CXMLNode* m_pNodeStorage = nullptr; // Dummy XML node used for temporary storage of stuff returned by CSettings::Get

Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/CResourceHTMLItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ bool CResourceHTMLItem::Start()
fwrite ( m_szBuffer, 1, strlen(m_szBuffer), debug );
fclose ( debug );*/

m_pVM = g_pGame->GetLuaManager()->CreateVirtualMachine(m_resource, m_bOOPEnabled);
m_pVM = g_pGame->GetLuaManager()->CreateVirtualMachine(m_resource, m_bOOPEnabled, m_resource->GetLuaVersion());
m_pVM->LoadEmbeddedScripts();
m_pVM->RegisterModuleFunctions();
m_pVM->LoadScript(strScript.c_str());
Expand Down
4 changes: 2 additions & 2 deletions Server/mods/deathmatch/logic/lua/CLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ void CLuaMain::InitClasses(lua_State* luaVM)
CLuaShared::AddClasses(luaVM);
}

void CLuaMain::Initialize()
void CLuaMain::Initialize(ELuaVersion version)
{
assert(!m_luaVM);

// Create a new VM
m_luaVM = lua_open(this);
m_luaVM = VluaL_newstate(this, version);
m_pLuaManager->OnLuaMainOpenVM(this, m_luaVM);

// Set the instruction count hook
Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/lua/CLuaMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class CLuaMain //: public CClient

void RegisterHTMLDFunctions();

void Initialize();
void Initialize(ELuaVersion version);
void LoadEmbeddedScripts();
void RegisterModuleFunctions();
const SString& GetFunctionTag(int iFunctionNumber);
Expand Down
10 changes: 8 additions & 2 deletions Server/mods/deathmatch/logic/lua/CLuaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ CLuaManager::CLuaManager(CObjectManager* pObjectManager, CPlayerManager* pPlayer
m_pMapManager = pMapManager;
m_pEvents = pEvents;

// Init Vlua library
VluaL_init();

// Create our lua dynamic module manager
m_pLuaModuleManager = new CLuaModuleManager(this);
m_pLuaModuleManager->SetScriptDebugging(g_pGame->GetScriptDebugging());
Expand Down Expand Up @@ -86,15 +89,18 @@ CLuaManager::~CLuaManager()

// Destroy the module manager
delete m_pLuaModuleManager;

// Close Vlua library
VluaL_close();
}

CLuaMain* CLuaManager::CreateVirtualMachine(CResource* pResourceOwner, bool bEnableOOP)
CLuaMain* CLuaManager::CreateVirtualMachine(CResource* pResourceOwner, bool bEnableOOP, ELuaVersion version)
{
// Create it and add it to the list over VM's
CLuaMain* pLuaMain = new CLuaMain(this, m_pObjectManager, m_pPlayerManager, m_pVehicleManager, m_pBlipManager, m_pRadarAreaManager, m_pMapManager,
pResourceOwner, bEnableOOP);
m_virtualMachines.push_back(pLuaMain);
pLuaMain->Initialize();
pLuaMain->Initialize(version);

return pLuaMain;
}
Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/lua/CLuaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CLuaManager
CRadarAreaManager* pRadarAreaManager, CRegisteredCommands* pRegisteredCommands, CMapManager* pMapManager, CEvents* pEvents);
~CLuaManager();

CLuaMain* CreateVirtualMachine(CResource* pResourceOwner, bool bEnableOOP);
CLuaMain* CreateVirtualMachine(CResource* pResourceOwner, bool bEnableOOP, ELuaVersion version);
bool RemoveVirtualMachine(CLuaMain* vm);
CLuaMain* GetVirtualMachine(lua_State* luaVM);
CResource* GetVirtualMachineResource(lua_State* luaVM);
Expand Down
5 changes: 5 additions & 0 deletions Server/mods/deathmatch/logic/packets/CResourceStartPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ bool CResourceStartPacket::Write(NetBitStreamInterface& BitStream) const
BitStream.WriteBit(m_pResource->IsOOPEnabledInMetaXml());
}

if (BitStream.Can(eBitStreamVersion::ResourceLuaVersion))
{
BitStream.Write(static_cast<unsigned char>(m_pResource->GetClientLuaVersion()));
}

if (BitStream.Version() >= 0x62)
{
BitStream.Write(m_pResource->GetDownloadPriorityGroup());
Expand Down
4 changes: 4 additions & 0 deletions Shared/sdk/net/bitstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,10 @@ enum class eBitStreamVersion : unsigned short
// Add respawnObject and toggleObjectRespawn to serverside
// 2024-09-04
RespawnObject_Serverside,

// Add lua version support
// 2024-10-04
ResourceLuaVersion,

// This allows us to automatically increment the BitStreamVersion when things are added to this enum.
// Make sure you only add things above this comment.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Loading