s-d-a / DCS-ExportScripts

DCS World Export Scripts
GNU Lesser General Public License v3.0
114 stars 52 forks source link

Incompatibility with WinWing Hardware #14

Open CrashEd1664 opened 3 years ago

CrashEd1664 commented 3 years ago

Hi,

I'm having issues when running the DSC-ExportScript alongside WinWing hardware, such as the Taurus throttle and the combat ready/takeoff panels. Here is an example of the Export.lua file:

_local wwtlfs=require('lfs')
dofile(wwtlfs.writedir()..'Scripts/wwt/wwtExport.lua')

-- load the DCS ExportScript for DAC and Ikarus
dofile(lfs.writedir()..[[Scripts\DCS-ExportScript\ExportScript.lua]])_ 

When both chunks of code are present in the lua file then the WinWing hardware fails to extract data from DCS.

Here is the WinWing wwtNetwork.lua file:

_w_net={}
package.path  = package.path..";.\\LuaSocket\\?.lua"
package.cpath = package.cpath..";.\\LuaSocket\\?.dll"
--全局变量
w_net.socket={}
w_net.net = {}
w_net.json=loadfile("Scripts\\JSON.lua")()
w_net.addr={{ip="127.0.0.1",port=16536},{ip="127.0.0.1",port=16537}}
--module开始
function w_net.start()
    w_net.socket=require("socket")
    w_net.net = w_net.socket.udp()
    w_net.net:setsockname("*", 0)
    w_net.net:setoption('broadcast', true)
    w_net.net:settimeout(.001)
end

--module发送
function w_net.send(msg)
    for i,_addr in pairs(w_net.addr) do
        w_net.socket.try(w_net.net:sendto(w_net.json:encode(msg),_addr.ip,_addr.port))
    end
end

--module接收
function w_net.get()
    local _msg=w_net.net:receive()
    local _table={}
    if type(_msg)=="string" and #_msg>0 then
        _table=w_net.json:decode(_msg)
        if type(_table)=="table" then
            return _table
        else
            return {}
        end
    else
        return {}
    end
end

--module结束
function w_net.stop()
    --关闭socket
    w_net.net:close()
end

return w_net_

And wwExport.lua file:

-- 新增外部库地址
g_lfs=require('lfs')
package.path  = package.path..";"..g_lfs.writedir().."Scripts\\wwt\\?.lua"
package.cpath = package.cpath..";"..g_lfs.writedir().."Scripts\\wwt\\?.dll"
-- 定义dcs export函数
do
    --初始化
    if g_winwingInit==nil then
        g_winwingInit=true
        -- 声明
        local _winwing={}
        _winwing.selfData={}
        _winwing.mission=nil
        _winwing.mod=nil
        _winwing.output={}
        local _dev=nil
        local _devVal=nil
        local _key=nil
        local _val=nil
        --加载网络库
        _winwing.net=require("wwtNetwork")
        --启动网络
        _winwing.net.start()
        --网络就绪
        local _send={}
        _send["func"]="net"
        _send["msg"]="ready"
        _winwing.net.send(_send)
        do
            --定义函数
            _winwing.LuaExportStart=function()
                --记录日志
                log.write("WWT",log.INFO,"Export start!")
                --任务就绪
                local _send={}
                _send["func"]="mission"
                _send["msg"]="ready"
                _winwing.net.send(_send)
            end

            _winwing.LuaExportBeforeNextFrame=function()
                --任务开始(仅一次)
                if _winwing.mission==nil then
                    _winwing.mission=true
                    local _send={}
                    _send["func"]="mission"
                    _send["msg"]="start"
                    _winwing.net.send(_send)
                end
                --获取机型
                local _self=LoGetSelfData()
                if _self~=nil then
                    if _winwing.mod~=_self.Name then
                        _winwing.mod=_self.Name
                        local _send={}
                        _send["func"]="mod"
                        _send["msg"]=_self.Name
                        _winwing.net.send(_send)
                    end
                end
                --接受网络数据并处理
                local _get=_winwing.net.get()
                if type(_get)=="table" then
                    if _get["func"]=="addOutput" then--添加输出(变化输出)
                        --遍历数据并添加
                        for _dev,_devVal in pairs(_get["args"]) do
                            for _key,_valOld in pairs(_devVal) do
                                if type(_winwing.output[_dev])~="table" then
                                    _winwing.output[_dev]={}
                                end
                                _winwing.output[_dev][_key]=_valOld
                            end
                        end
                    elseif _get["func"]=="getOutput" then--获取输出(仅一次)
                        --遍历数据并回应
                        local _send={}
                        _send["func"]=_get["func"]
                        for _dev,_devVal in pairs(_get["args"]) do
                            GetDevice(_dev):update_arguments()
                            for _key,_valOld in pairs(_devVal) do
                                local _valNew=GetDevice(_dev):get_argument_value(_key)
                                if type(_send["args"])~='table' then
                                    _send["args"]={}
                                end
                                if type(_send["args"][_dev])~='table' then
                                    _send["args"][_dev]={}
                                end
                                _send["args"][_dev][_key]=_valNew
                            end
                        end
                        _winwing.net.send(_send)
                    elseif _get["func"]=="clearOutput" then--清空输出
                        _winwing.output={}
                    elseif _get["func"]=="original" then--原始接口
                        local _func,_err=loadstring(_get["msg"])
                        if _func then
                            local _status,_result = pcall(_func)
                            local _send={}
                            _send["func"]=_get["func"]
                            _send["status"]=_status
                            _send["msg"]=_result
                            _winwing.net.send(_send)
                        else
                            local _send={}
                            _send["func"]=_get["func"]
                            _send["status"]=false
                            _send["msg"]=tostring(_err)
                            _winwing.net.send(_send)
                        end
                    elseif _get["func"]=="setInput" then--设置输入
                        --遍历数据并触发
                        for _dev,_devVal in pairs(_get["args"]) do
                            for _key,_val in pairs(_devVal) do
                                GetDevice(_dev):performClickableAction(_key,_val)
                            end
                        end
                    end
                end
            end

            _winwing.LuaExportActivityNextEvent=function(t)
                t=t+0.01
                --发送之前添加的输出数据(变化发送)
                local _send={}
                _send["func"]="addOutput"
                for _dev,_devVal in pairs(_winwing.output) do
                    if type(GetDevice(_dev))~='table' then
                        break
                    end
                    GetDevice(_dev):update_arguments()
                    for _key,_valOld in pairs(_devVal) do
                        local _valNew=GetDevice(_dev):get_argument_value(_key)
                        if math.abs(_valNew-_valOld)>0.01 then
                            _winwing.output[_dev][_key]=_valNew
                            if type(_send["args"])~='table' then
                                _send["args"]={}
                            end
                            if type(_send["args"][_dev])~='table' then
                                _send["args"][_dev]={}
                            end
                            _send["args"][_dev][_key]=_valNew
                        end
                    end
                end
                if _send["args"]~=nil then
                    _winwing.net.send(_send)
                end
                return t
            end

            _winwing.LuaExportStop=function()
                --任务结束,关闭网络
                local _send={}
                _send["func"]="mission"
                _send["msg"]="stop"
                _winwing.net.send(_send)
                --记录日志
                log.write("WWT",log.INFO,"Export stop!")
            end
            --记录其他的(第三方)export函数,方便之后在执行我们的函数后,执行第三方函数。
            _winwing.OtherLuaExportStart=LuaExportStart                             --开始函数
            _winwing.OtherLuaExportBeforeNextFrame=LuaExportBeforeNextFrame         --输入数据到DCS内部,比如控制飞机的横滚
            _winwing.OtherLuaExportActivityNextEvent=LuaExportActivityNextEvent     --输出数据到DCS外部,比如获取飞机的高度
            _winwing.OtherLuaExportStop=LuaExportStop                               --结束函数

            --定义dcs export函数
            LuaExportStart=function()
                _winwing.LuaExportStart()
                if _winwing.OtherLuaExportStart then
                    _winwing.OtherLuaExportStart()
                end
            end

            LuaExportBeforeNextFrame=function()
                _winwing.LuaExportBeforeNextFrame()
                if _winwing.OtherLuaExportBeforeNextFrame then
                    _winwing.OtherLuaExportBeforeNextFrame()
                end
            end

            LuaExportActivityNextEvent=function(t)
                t=_winwing.LuaExportActivityNextEvent(t)
                if _winwing.OtherLuaExportActivityNextEvent then
                    t=_winwing.OtherLuaExportActivityNextEvent(t)
                end
                return t
            end

            LuaExportStop=function()
                _winwing.LuaExportStop()
                if _winwing.OtherLuaExportStop then
                    _winwing.OtherLuaExportStop()
                end
            end
            log.write("WWT",log.INFO,"Winwing export installed!")
        end
    end
end

Is there a possible port clash somewhere?

Any help would be massively appreciated!

CrashEd1664 commented 3 years ago

Any news on this please? Happy to test anything as I have both sets of hardware...

Andytr1 commented 3 years ago

I had to put winwing lines below DCS-Export in export.lua to make them both work.

CrashEd1664 commented 3 years ago

I’m pretty sure I’ve already tried that but I’ll give it another go. Good to hear you have them both working together.

Andytr1 commented 3 years ago

Simapp pro rewrites your export.lua everytime you start it, forcing ww lines to be at the top. You need to start simapp pro, then open export.lua and put the winwing line after dcs-export.