当前位置: 首页 > news >正文

【DCS开源项目】—— Lua 如何调用 DLL、DLL 与 DCS World 的交互

文章目录

    • 1. Lua 调用 C++ DLL 的机制
      • 入口与注册
        • entry.lua
      • DLL 导出接口
        • `onSimulationStart`代码
        • `onSimulationFrame`代码
        • `setUnitsData`代码
      • 生命周期与回调
        • `onSimulationStart`代码
        • `onSimulationFrame`代码
        • `onSimulationStop`代码
        • `coreInit`代码
        • `coreFrame`代码
        • `coreDeinit`代码
    • 2. DLL 与 DCS World 的交互
      • Lua State 共享
        • 示例
      • 数据交互方式
        • dcstools.cpp
      • 线程与同步
    • 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, -- if false that will be place holder , or advertisingdirName		 = current_mod_path,binaries	 ={
--		'Olympus',},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 函数(如 onSimulationStartonSimulationFramesetUnitsData 等),这些函数会被 Lua 脚本直接调用。
onSimulationStart代码
//olympus.cppstatic 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
http://www.lryc.cn/news/573323.html

相关文章:

  • day44-硬件学习之arm启动代码
  • 【Datawhale组队学习202506】零基础学爬虫 02 数据解析与提取
  • 【simulink】IEEE5节点系统潮流仿真模型(2机5节点全功能基础模型)
  • 【智能体】dify部署本地步骤
  • LeetCode第279题_完全平方数
  • 湖北理元理律师事务所企业债务纾困路径:司法重整中的再生之道
  • 蓝桥杯备赛篇(上) - 参加蓝桥杯所需要的基础能力 1(C++)
  • 华为OD机试_2025 B卷_判断一组不等式是否满足约束并输出最大差(Python,100分)(附详细解题思路)
  • 车载电子电器架构 --- 电子电气架构设计方案
  • QC -io 服务器排查报错方式/报错: Failed to convert string to integer of varId variable!“
  • 2.7 Python方法调用机制解析:从描述符到字节码执行
  • 学习C++、QT---03(C++的输入输出、C++的基本数据类型介绍)
  • 【无标题】使用 Chocolatey 安装 WSL 管理工具 LxRunOffline
  • 贪心算法思路详解
  • Mac电脑-Markdown编辑器-Typora
  • 利用nRF54L15-DK的DEBUG OUT接口调试用户设计的ARM处理器系统
  • springboot口腔管理平台
  • 【分布式理论】读确认数与写确认数:分布式一致性的核心概念
  • WPF Style样式 全局样式资源字典
  • 获取 DOM 与 nextTick:Vue 中的 DOM 操作
  • CTF--PhP Web解题(走入CTF)
  • 增量学习ASAP的源码剖析:如何实现人形的运动追踪和全身控制(核心涉及HumanoidVerse中的agents模块)
  • Redis集群部署终极指南:架构选型、生产部署与深度优化
  • 人形机器人_双足行走动力学:本田机械腿的倒立摆模型
  • rt-thread中使用usb官方自带的驱动问题记录
  • 【全开源】填表问卷统计预约打卡表单系统+uniapp前端
  • 基于FPGA的白噪声信号发生器verilog实现,包含testbench和开发板硬件测试
  • 基于物联网的智能饮水机系统设计
  • API网关Apisix管理接口速查
  • STM32 CAN简介及帧格式