diff --git a/.luacheckrc b/.luacheckrc index 5c58e2e..3abf1e0 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -10,6 +10,7 @@ read_globals = { "net", "coord", "DCS", + "Sim", "env", "Group", "land", diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b6d22a..43d3b91 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 419a7f4..e799e34 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 + 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 + 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 + 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 + 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 b6ee79b..ec7542e 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 + 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 39f28f0..eed969f 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 + 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()