当前位置: 首页 > 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.xdnf.cn/news/1063369.html

相关文章:

  • Vue3 + TypeScript + xlsx 导入excel文件追踪数据流转详细记录(从原文件到目标数据)
  • 领域驱动设计(DDD)【3】之事件风暴
  • EasyExcel导出极致封装 含枚举转换 分页导出
  • GitHub Copilot快捷键
  • 缓存与加速技术实践-Kafka消息队列
  • 腾讯云IM即时通讯:开启实时通信新时代
  • Python中字符串常用的操作方法
  • Linux TCP/IP协议栈中的TCP输入处理:net/ipv4/tcp_input.c解析
  • 学习C++、QT---03(C++的输入输出、C++的基本数据类型介绍)
  • AI与SEO关键词协同进化
  • IEC61850 通信协议测试验证方法详解
  • 解锁K-近邻算法:数据挖掘的秘密武器
  • 华为云Flexus+DeepSeek征文 | 基于Flexus X实例的金融AI Agent开发:智能风控与交易决策系统
  • 【AI论文】扩散二元性
  • 面试题-定义一个函数入参数是any类型,返回值是string类型,如何写出这个函数,代码示例
  • ncu学习笔记01——合并访存
  • 系统化的Node.js服务器搭建攻略
  • 将Python的JSON字符串转换为JSON
  • UE5 游戏模板 —— FirstShootGame
  • Docker简单介绍与使用以及下载对应镜像(项目前置)
  • 【软考高级系统架构论文】论湖仓一体架构及其应用
  • RNN工作原理和架构
  • Python的6万张图像数据集CIFAR-10和CIFAR-100说明
  • Redis哨兵模式的学习(三)
  • STM32F103_LL库+寄存器学习笔记12.3 - 串口DMA高效收发实战3:支持多实例化的版本
  • 【24】二维码数据集(有v5/v8模型)/YOLO二维码检测
  • 项目拓展-Spring实现策略类统一管理
  • 【Elasticsearch】脚本(Script)
  • Duende Identity Server学习之一:认证服务器及一个Oidc/OAuth认证、用于Machine 2 Machine的客户端
  • 零基础学习Redis(12) -- Java连接redis服务器