From 4c948ef70287ab99f5af73115544a12e5a738826 Mon Sep 17 00:00:00 2001 From: dutchie032 Date: Mon, 14 Jul 2025 10:12:09 +0200 Subject: [PATCH 1/3] Made Sim functions backwards compatible when DCS is still in the API --- lua/DCS-gRPC/grpc-hook.lua | 38 +++++++++++++++--- lua/DCS-gRPC/grpc.lua | 9 ++++- lua/DCS-gRPC/methods/hook.lua | 74 +++++++++++++++++++++++++++++------ 3 files changed, 102 insertions(+), 19 deletions(-) diff --git a/lua/DCS-gRPC/grpc-hook.lua b/lua/DCS-gRPC/grpc-hook.lua index 419a7f44..0e61448b 100644 --- a/lua/DCS-gRPC/grpc-hook.lua +++ b/lua/DCS-gRPC/grpc-hook.lua @@ -25,7 +25,7 @@ local handler = {} function handler.onMissionLoadEnd() local ok, err = pcall(load) if not ok then - log.write("[GRPC-Hook]", log.ERROR, "Failed to set up gRPC listener: "..tostring(err)) + log.write("[GRPC-Hook]", log.ERROR, "Failed to set up gRPC listener: " .. tostring(err)) end end @@ -49,20 +49,33 @@ function handler.onPlayerTrySendChat(playerID, msg) -- note: currently `all` (third parameter) will always `=true` regardless if the target is to the coalition/team -- or to everybody. When ED fixes this, implementation should determine the dcs.common.v0.Coalition + local modelTime = 0 + if DCS then --Backwards compatibility with DCS 2.9.17 and before + modelTime = DCS.getModelTime() + else + modelTime = Sim.getModelTime() + end + grpc.event({ - time = DCS.getModelTime(), + time = modelTime, event = { type = "playerSendChat", playerId = playerID, message = msg }, }) - end function handler.onPlayerTryConnect(addr, name, ucid, id) + local modelTime = 0 + if DCS then --Backwards compatibility with DCS 2.9.17 and before + modelTime = DCS.getModelTime() + else + modelTime = Sim.getModelTime() + end + grpc.event({ - time = DCS.getModelTime(), + time = modelTime, event = { type = "connect", addr = addr, @@ -74,8 +87,15 @@ function handler.onPlayerTryConnect(addr, name, ucid, id) end function handler.onPlayerDisconnect(id, reason) + local modelTime = 0 + if DCS then --Backwards compatibility with DCS 2.9.17 and before + modelTime = DCS.getModelTime() + else + modelTime = Sim.getModelTime() + end + grpc.event({ - time = DCS.getModelTime(), + time = modelTime, event = { type = "disconnect", id = id, @@ -86,9 +106,15 @@ end function handler.onPlayerChangeSlot(playerId) local playerInfo = net.get_player_info(playerId) + local modelTime = 0 + if DCS then --Backwards compatibility with DCS 2.9.17 and before + modelTime = DCS.getModelTime() + else + modelTime = Sim.getModelTime() + end grpc.event({ - time = DCS.getModelTime(), + time = modelTime, event = { type = "playerChangeSlot", playerId = playerId, diff --git a/lua/DCS-gRPC/grpc.lua b/lua/DCS-gRPC/grpc.lua index b6ee79b9..187087ea 100644 --- a/lua/DCS-gRPC/grpc.lua +++ b/lua/DCS-gRPC/grpc.lua @@ -265,7 +265,14 @@ else -- hook env local skipFrames = math.ceil(interval / 0.016) -- 0.016 = 16ms = 1 frame at 60fps local frame = 0 function GRPC.onSimulationFrame() - grpc.simulationFrame(DCS.getModelTime()) + local modelTime = 0 + if DCS then --Backwards compatibility with DCS 2.9.17 and before + modelTime = DCS.getModelTime() + else + modelTime = Sim.getModelTime() + end + + grpc.simulationFrame(modelTime) frame = frame + 1 if frame >= skipFrames then diff --git a/lua/DCS-gRPC/methods/hook.lua b/lua/DCS-gRPC/methods/hook.lua index 39f28f02..a72c29bc 100644 --- a/lua/DCS-gRPC/methods/hook.lua +++ b/lua/DCS-gRPC/methods/hook.lua @@ -4,24 +4,41 @@ -- local DCS = DCS +local Sim = Sim local GRPC = GRPC local net = net local Export = Export GRPC.methods.getMissionName = function() - return GRPC.success({name = DCS.getMissionName()}) + if DCS then --Backwards compatibility with DCS 2.9.17 and before + GRPC.success({name = DCS.getMissionName()}) + else + GRPC.success({name = Sim.getMissionName()}) + end end GRPC.methods.getMissionFilename = function() - return GRPC.success({name = DCS.getMissionFilename()}) + if DCS then --Backwards compatibility with DCS 2.9.17 and before + return GRPC.success({name = DCS.getMissionFilename()}) + else + return GRPC.success({name = Sim.getMissionFilename()}) + end end GRPC.methods.getMissionDescription = function() - return GRPC.success({description = DCS.getMissionDescription()}) + if DCS then --Backwards compatibility with DCS 2.9.17 and before + return GRPC.success({name = DCS.getMissionDescription()}) + else + return GRPC.success({name = Sim.getMissionDescription()}) + end end GRPC.methods.reloadCurrentMission = function() - net.load_mission(DCS.getMissionFilename()) + if DCS then --Backwards compatibility with DCS 2.9.17 and before + net.load_mission(DCS.getMissionFilename()) + else + net.load_mission(Sim.getMissionFilename()) + end return GRPC.success({}) end @@ -34,21 +51,37 @@ GRPC.methods.loadMission = function(params) end GRPC.methods.getPaused = function() - return GRPC.success({paused = DCS.getPause()}) + if DCS then --Backwards compatibility with DCS 2.9.17 and before + return GRPC.success({paused = DCS.getPause()}) + else + return GRPC.success({paused = Sim.getPause()}) + end end GRPC.methods.setPaused = function(params) - DCS.setPause(params.paused) + if DCS then --Backwards compatibility with DCS 2.9.17 and before + DCS.setPause(params.paused) + else + Sim.setPause(params.paused) + end return GRPC.success({}) end GRPC.methods.stopMission = function() - DCS.stopMission() + if DCS then --Backwards compatibility with DCS 2.9.17 and before + DCS.stopMission() + else + Sim.stopMission() + end return GRPC.success({}) end GRPC.methods.exitProcess = function() - DCS.exitProcess() + if DCS then --Backwards compatibility with DCS 2.9.17 and before + DCS.exitProcess() + else + Sim.exitProcess() + end return GRPC.success({}) end @@ -67,11 +100,19 @@ GRPC.methods.hookEval = function(params) end GRPC.methods.isMultiplayer = function() - return GRPC.success({multiplayer = DCS.isMultiplayer()}) + if DCS then --Backwards compatibility with DCS 2.9.17 and before + return GRPC.success({multiplayer = DCS.isMultiplayer()}) + else + return GRPC.success({multiplayer = Sim.isMultiplayer()}) + end end GRPC.methods.isServer = function() - return GRPC.success({server = DCS.isServer()}) + if DCS then --Backwards compatibility with DCS 2.9.17 and before + return GRPC.success({server = DCS.isServer()}) + else + return GRPC.success({server = Sim.isServer()}) + end end GRPC.methods.banPlayer = function(params) @@ -111,7 +152,12 @@ end GRPC.methods.getUnitType = function(params) -- https://wiki.hoggitworld.com/view/DCS_func_getUnitType - local unit_type = DCS.getUnitType(params.id) + local unit_type = nil + if DCS then --Backwards compatibility with DCS 2.9.17 and before + unit_type = DCS.getUnitType(params.id) + else + unit_type = Sim.getUnitType(params.id) + end -- getUnitType returns an empty string if the unit doesn't exist, ensure we catch eventual nils too if unit_type == nil or unit_type == "" then return GRPC.errorNotFound("unit `" .. tostring(params.id) .. "` does not exist") @@ -122,7 +168,11 @@ end GRPC.methods.getRealTime = function() -- https://wiki.hoggitworld.com/view/DCS_func_getRealTime - return GRPC.success({time = DCS.getRealTime()}) + if DCS then --Backwards compatibility with DCS 2.9.17 and before + return GRPC.success({time = DCS.getRealTime()}) + else + return GRPC.success({time = Sim.getRealTime()}) + end end GRPC.methods.getBallisticsCount = function() From 1d4a8b07a7e39bbad75d3f786569d93065193c64 Mon Sep 17 00:00:00 2001 From: dutchie032 Date: Tue, 22 Jul 2025 16:07:54 +0200 Subject: [PATCH 2/3] fixed linting errors --- CHANGELOG.md | 5 +++++ lua/DCS-gRPC/grpc-hook.lua | 14 +++++++------- lua/DCS-gRPC/grpc.lua | 2 +- lua/DCS-gRPC/methods/hook.lua | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b6d22ac..43d3b916 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + + - `DCS` singleton to be deprecated for `Sim` by DCS. + All `DCS` calls replaced with conditional calls and fallback to `Sim` once `DCS` gets deprecated for backwards compatibility. + ## [0.8.1] 2024-11-05 ### Added diff --git a/lua/DCS-gRPC/grpc-hook.lua b/lua/DCS-gRPC/grpc-hook.lua index 0e61448b..e799e344 100644 --- a/lua/DCS-gRPC/grpc-hook.lua +++ b/lua/DCS-gRPC/grpc-hook.lua @@ -49,7 +49,7 @@ function handler.onPlayerTrySendChat(playerID, msg) -- note: currently `all` (third parameter) will always `=true` regardless if the target is to the coalition/team -- or to everybody. When ED fixes this, implementation should determine the dcs.common.v0.Coalition - local modelTime = 0 + local modelTime if DCS then --Backwards compatibility with DCS 2.9.17 and before modelTime = DCS.getModelTime() else @@ -67,7 +67,7 @@ function handler.onPlayerTrySendChat(playerID, msg) end function handler.onPlayerTryConnect(addr, name, ucid, id) - local modelTime = 0 + local modelTime if DCS then --Backwards compatibility with DCS 2.9.17 and before modelTime = DCS.getModelTime() else @@ -87,7 +87,7 @@ function handler.onPlayerTryConnect(addr, name, ucid, id) end function handler.onPlayerDisconnect(id, reason) - local modelTime = 0 + local modelTime if DCS then --Backwards compatibility with DCS 2.9.17 and before modelTime = DCS.getModelTime() else @@ -106,11 +106,11 @@ end function handler.onPlayerChangeSlot(playerId) local playerInfo = net.get_player_info(playerId) - local modelTime = 0 + local modelTime if DCS then --Backwards compatibility with DCS 2.9.17 and before - modelTime = DCS.getModelTime() - else - modelTime = Sim.getModelTime() + modelTime = DCS.getModelTime() + else + modelTime = Sim.getModelTime() end grpc.event({ diff --git a/lua/DCS-gRPC/grpc.lua b/lua/DCS-gRPC/grpc.lua index 187087ea..ec7542e3 100644 --- a/lua/DCS-gRPC/grpc.lua +++ b/lua/DCS-gRPC/grpc.lua @@ -265,7 +265,7 @@ else -- hook env local skipFrames = math.ceil(interval / 0.016) -- 0.016 = 16ms = 1 frame at 60fps local frame = 0 function GRPC.onSimulationFrame() - local modelTime = 0 + local modelTime if DCS then --Backwards compatibility with DCS 2.9.17 and before modelTime = DCS.getModelTime() else diff --git a/lua/DCS-gRPC/methods/hook.lua b/lua/DCS-gRPC/methods/hook.lua index a72c29bc..eed969fe 100644 --- a/lua/DCS-gRPC/methods/hook.lua +++ b/lua/DCS-gRPC/methods/hook.lua @@ -152,7 +152,7 @@ end GRPC.methods.getUnitType = function(params) -- https://wiki.hoggitworld.com/view/DCS_func_getUnitType - local unit_type = nil + local unit_type if DCS then --Backwards compatibility with DCS 2.9.17 and before unit_type = DCS.getUnitType(params.id) else From 96c124c3ac54c9ef9d2a1f64243367b5fdb228b4 Mon Sep 17 00:00:00 2001 From: dutchie032 Date: Tue, 22 Jul 2025 16:10:15 +0200 Subject: [PATCH 3/3] Added Sim as a read_globals in luacheckrc --- .luacheckrc | 1 + 1 file changed, 1 insertion(+) diff --git a/.luacheckrc b/.luacheckrc index 5c58e2e4..3abf1e0e 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -10,6 +10,7 @@ read_globals = { "net", "coord", "DCS", + "Sim", "env", "Group", "land",