Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ read_globals = {
"net",
"coord",
"DCS",
"Sim",
"env",
"Group",
"land",
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 32 additions & 6 deletions lua/DCS-gRPC/grpc-hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion lua/DCS-gRPC/grpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 62 additions & 12 deletions lua/DCS-gRPC/methods/hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand Down
Loading