文章目录
-
- 1. Lua 调用 C++ DLL 的机制
-
- 入口与注册
-
- DLL 导出接口
-
- `onSimulationStart`代码
- `onSimulationFrame`代码
- `setUnitsData`代码
- 生命周期与回调
-
- `onSimulationStart`代码
- `onSimulationFrame`代码
- `onSimulationStop`代码
- `coreInit`代码
- `coreFrame`代码
- `coreDeinit`代码
- 2. DLL 与 DCS World 的交互
-
- Lua State 共享
-
- 数据交互方式
-
- 线程与同步
- 3. 典型流程举例
- 4. 总结
1. Lua 调用 C++ DLL 的机制
入口与注册
- 在 DCS World 的 Mods 目录下,Olympus 以插件形式加载,Lua 脚本(如 entry.lua)声明插件并初始化。
- 主要 Lua 脚本(如 OlympusCommand.lua)负责加载 DLL,并通过
require("olympus")
或 package.loadlib
方式调用 C++ 导出的 Lua C API。
entry.lua
local self_ID = "DCS-Olympus"
declare_plugin(self_ID,
{
image = "Olympus.png",
installed = true,
dirName = current_mod_path,
binaries =
{
},
load_immediately = true,
displayName = "Olympus",
shortName = "Olympus",
fileMenuName = "Olympus",
version = "{
{OLYMPUS_VERSION_NUMBER}}",
state = "installed",
developerName= "DCS Refugees 767 squadron",
info = _("DCS Olympus is a mod for DCS World. It allows users to spawn, control, task, group, and remove units from a DCS World server using a real-time map interface, similarly to Real Time Strategy games. The user interface also provides useful informations units, like loadouts, fuel, tasking, and so on. In the future, more features for DCS World GCI and JTAC will be available."),
Skins =
{
{
name = "Olympus",
dir = "Theme"
},
},
Options =
{
{
name = "Olympus",
nameId = "Olympus",
dir = "Options",
CLSID = "{Olympus-options}"
},
},
})
plugin_done()
DLL 导出接口
- C++ 侧通过
extern "C" DllExport int luaopen_olympus(lua_State *L)
导出模块初始化函数,供 Lua 加载。
- 在 olympus.cpp 中,注册了一系列 Lua 可调用的 C 函数(如
onSimulationStart
、onSimulationFrame
、setUnitsData
等),这些函数会被 Lua 脚本直接调用。
onSimulationStart
代码
static int onSimulationStart(lua_State* L)
{
LogInfo(L, "Trying to load core.dll from " + modPath);
SetDllDirectoryA(modPath.c_str());
setLogDirectory(modPath);
log("onSimulationStart callback called successfully");
string dllLocation = modPath + "\\core.dll";
log("Loading core.dll");
hGetProcIDDLL = LoadLibrary(to_wstring(dllLocation).c_str());
if (!hGetProcIDDLL) {
LogError(L, "Error loading core DLL");
goto error;
}
log("Core DLL loaded successfully");
coreInit = (f_coreInit)GetProcAddress(hGetProcIDDLL, "coreInit");
if (!coreInit)
{
LogError(L, "Error getting coreInit ProcAddress from DLL");
goto error;
}
coreDeinit = (f_coreDeinit)GetProcAddress(hGetProcIDDLL, "coreDeinit");
if (!coreDeinit)
{
LogError(L, "Error getting coreDeinit ProcAddress from DLL");
goto error;
}
coreFrame = (f_coreFrame)GetProcAddress(hGetProcIDDLL, "coreFrame");
if (!coreFrame)
{
LogError(L, "Error getting coreFrame ProcAddress from DLL");
goto error;
}
coreUnitsData = (f_coreUnitsData)GetProcAddress(hGetProcIDDLL, "coreUnitsData");
if (!coreUnitsData)
{
LogError(L, "Error getting coreUnitsData ProcAddress from DLL");
goto error;
}
coreWeaponsData = (f_coreWeaponsData)GetProcAddress(hGetProcIDDLL, "coreWeaponsData");
if (!coreWeaponsData)
{
LogError(L, "Error getting coreWeaponsData ProcAddress from DLL");
goto error;
}
coreMissionData = (f_coreMissionData)GetProcAddress(hGetProcIDDLL, "coreMissionData");
if (!coreMissionData)
{
LogError(L, "Error getting coreMissionData ProcAddress from DLL");
goto error;
}
coreDrawingsData = (f_coreDrawingsData)GetProcAddress(hGetProcIDDLL, "coreDrawingsData");
if (!coreDrawingsData)
{
LogError