Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

load custom lua file (windows)

emc

New Coder
Hello, I got some lua script
Code:
package.path = "./Scripts/DCS-BIOS/test/compile/?.lua;" .. package.path
lfs = require("Scripts.DCS-BIOS.test.compile.lfs")
dofile([[Scripts/DCS-BIOS/BIOS.lua]])
But I need rewite it using absolut paths, my take:

Code:
print(package.path)
package.path = "C:\\\\Users\\\\mplichta\\\\Saved Games\\\\DCS-BIOS\\\\test\\\\compile\\\\?.lua;" .. package.path
print(package.path)
lfs = require("Users.mplichta.Saved Games.DCS-BIOS.test.compile.lfs")
...
Looks like package.path is updated correctly, but I dont know how use require to load file lfs.lua from ..\test\compile\ directory, result is like (by the way I'm using lupa python libarary:
Code:
.\?.lua;C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\?.lua;C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\?\init.lua;
C:\Users\mplichta\Saved Games\DCS-BIOS\test\compile\?.lua;.\?.lua;C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\?.lua;C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\?\init.lua;
Traceback (most recent call last):
  File "C:\Users\mplichta\AppData\Roaming\JetBrains\PyCharm2024.2\scratches\scratch_21.py", line 22, in <module>
    lua.execute(lua_script)
    ~~~~~~~~~~~^^^^^^^^^^^^
  File "lupa\\luajit21.pyx", line 463, in lupa.luajit21.LuaRuntime.execute
  File "lupa\\luajit21.pyx", line 1904, in lupa.luajit21.run_lua
  File "lupa\\luajit21.pyx", line 1918, in lupa.luajit21.call_lua
  File "lupa\\luajit21.pyx", line 1945, in lupa.luajit21.execute_lua_call
  File "lupa\\luajit21.pyx", line 1826, in lupa.luajit21.raise_lua_error
lupa.luajit21.LuaError: [string "<python>"]:5: module 'Users.mplichta.Saved Games.DCS-BIOS.test.compile.lfs' not found:
    no field package.preload['Users.mplichta.Saved Games.DCS-BIOS.test.compile.lfs']
    no file 'C:\Users\mplichta\Saved Games\DCS-BIOS\test\compile\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.lua'
    no file '.\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.lua'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.lua'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs\init.lua'
    no file '.\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.dll'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.dll'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\loadall.dll'
    no file '.\Users.dll'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\Users.dll'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\loadall.dll'
stack traceback:
    [string "<python>"]:5: in main chunk
    [C]: in function 'require'
 
Hello, I got some lua script
Code:
package.path = "./Scripts/DCS-BIOS/test/compile/?.lua;" .. package.path
lfs = require("Scripts.DCS-BIOS.test.compile.lfs")
dofile([[Scripts/DCS-BIOS/BIOS.lua]])
But I need rewite it using absolut paths, my take:

Code:
print(package.path)
package.path = "C:\\\\Users\\\\mplichta\\\\Saved Games\\\\DCS-BIOS\\\\test\\\\compile\\\\?.lua;" .. package.path
print(package.path)
lfs = require("Users.mplichta.Saved Games.DCS-BIOS.test.compile.lfs")
...
Looks like package.path is updated correctly, but I dont know how use require to load file lfs.lua from ..\test\compile\ directory, result is like (by the way I'm using lupa python libarary:
Code:
.\?.lua;C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\?.lua;C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\?\init.lua;
C:\Users\mplichta\Saved Games\DCS-BIOS\test\compile\?.lua;.\?.lua;C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\?.lua;C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\?\init.lua;
Traceback (most recent call last):
  File "C:\Users\mplichta\AppData\Roaming\JetBrains\PyCharm2024.2\scratches\scratch_21.py", line 22, in <module>
    lua.execute(lua_script)
    ~~~~~~~~~~~^^^^^^^^^^^^
  File "lupa\\luajit21.pyx", line 463, in lupa.luajit21.LuaRuntime.execute
  File "lupa\\luajit21.pyx", line 1904, in lupa.luajit21.run_lua
  File "lupa\\luajit21.pyx", line 1918, in lupa.luajit21.call_lua
  File "lupa\\luajit21.pyx", line 1945, in lupa.luajit21.execute_lua_call
  File "lupa\\luajit21.pyx", line 1826, in lupa.luajit21.raise_lua_error
lupa.luajit21.LuaError: [string "<python>"]:5: module 'Users.mplichta.Saved Games.DCS-BIOS.test.compile.lfs' not found:
    no field package.preload['Users.mplichta.Saved Games.DCS-BIOS.test.compile.lfs']
    no file 'C:\Users\mplichta\Saved Games\DCS-BIOS\test\compile\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.lua'
    no file '.\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.lua'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.lua'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\lua\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs\init.lua'
    no file '.\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.dll'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\Users\mplichta\Saved Games\DCS-BIOS\test\compile\lfs.dll'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\loadall.dll'
    no file '.\Users.dll'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\Users.dll'
    no file 'C:\Users\mplichta\AppData\Roaming\uv\python\cpython-3.13.0rc2-windows-x86_64-none\loadall.dll'
stack traceback:
    [string "<python>"]:5: in main chunk
    [C]: in function 'require'
Hi there,

Can you provide a bit more regarding the purpose of your script?
 
Hi there,

Can you provide a bit more regarding the purpose of your script?
So LocalCompile.lua orginal contet:
Code:
-- This can be used to run the DCS-BIOS JSON generation locally without
-- starting a DCS mission
-- DCS-BIOS wiki Developer Guide have information on how to install the lua interpreter. No need to install
-- additional lua modules such as socket etc.
-- Execute => lua LocalCompile-lua or run it via VS Code task

package.path = "./Scripts/DCS-BIOS/test/compile/?.lua;" .. package.path

lfs = require("Scripts.DCS-BIOS.test.compile.lfs")

-- Include these that will mock the DCS APIs and the socket.
dofile([[Scripts/DCS-BIOS/BIOS.lua]])
in Scripts/DCS-BIOS/test/compile directory there are other files as well:
Code:
DCS_API_mocks.lua
lfs.lua
LocalCompile.lua
Log.lua
socket.lua
lfs.lua:
Code:
module("lfs", package.seeall)

---@diagnostic disable-next-line: lowercase-global
lfs = {}

---@diagnostic disable-next-line: duplicate-set-field
lfs.writedir = function()
    return "./"
end

return lfs
log.lua:
Code:
module("Log", package.seeall)

--- @class Log
local Log = {}

--- mock
--- @param str string
function Log:log(str) end

return Log
socket.lua:
Code:
module("socket", package.seeall)

local MockSocket = require("Scripts.DCS-BIOS.test.io.MockSocket")

---@diagnostic disable-next-line: lowercase-global
socket = MockSocket:new()

return socket
DCS_API_mocks.lua:
Code:
---@diagnostic disable: duplicate-set-field

---@class CockpitDevice
--- DCS Cockpit Device
CockpitDevice = {}

loadfile = loadfile

--- Sets command for a device
--- @param command_id integer
--- @param value integer
function CockpitDevice:SetCommand(command_id, value) end

--- Sets a device's argument?
--- @param argument_id integer
--- @param value integer
function CockpitDevice:set_argument_value(argument_id, value) end

--- Sets a device's frequency
--- @param frequency number
function CockpitDevice:set_frequency(frequency) end

--- Gets a device's frequency
--- @return number
function CockpitDevice:get_frequency()
    return 0
end

--- Gets a device based on the device ID as specified in devices.lua
--- @param argument_id integer
--- @return number The current value of the CockpitDevice
function CockpitDevice:get_argument_value(argument_id)
    return 0
end

--- Makes the device perform an action
--- @param command_id integer
--- @param argument number
function CockpitDevice:performClickableAction(command_id, argument) end

--- Gets a device based on the device ID as specified in devices.lua
--- @param device_id integer
--- @return CockpitDevice
function GetDevice(device_id)
    return CockpitDevice
end

--- Gets the draw value for a certain animation
--- @param draw_argument_id integer
--- @return number
function LoGetAircraftDrawArgumentValue(draw_argument_id)
    return 0
end

--- Contains Postions and Altitude
--- @class LatLongAlt
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Lat number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Long number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Alt number Altitude in meters
LatLongAlt = { Lat = 0, Long = 0, Alt = 0 }

--- Contains Aircraft Data
--- @class AicraftData
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Name string
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Heading number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field LatLongAlt LatLongAlt
AicraftData = { Name = "Test", Heading = 0, LatLongAlt }

--- Returns SelfData that holds information about the aircraft
--- @return AicraftData
function LoGetSelfData()
    return AicraftData
end

--- Returns the simulation time
--- @return number
function LoGetModelTime()
    return 0
end

--- Returns the mission start time
--- @return number
function LoGetMissionStartTime()
    return 0
end

--- Returns whether own ships/aircraft data can be exported.
--- @return boolean
function LoIsOwnshipExportAllowed()
    return true
end

--- Returns pilot's name
--- @return string
function LoGetPilotName()
    return "Neil Armstrong"
end

--- Returns indicated airspeed
--- @return number
function LoGetIndicatedAirSpeed()
    return 0
end

--- Contains acceleration info for all dimensions
--- @class AccelerationUnit
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field x number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field y number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field z number
AccelerationUnit = { x = 0, y = 0, z = 0 }

--- Returns G Load
--- @return AccelerationUnit
function LoGetAccelerationUnits()
    return AccelerationUnit
end

--- Returns ADI pitch, band, yaw
--- @return number
function LoGetADIPitchBankYaw()
    return unpack({ 0, 0, 0 })
end

--- @class CounterMeasures
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field chaff number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field flare number
CounterMeasures = { chaff = 0, flare = 0 }

--- Returns information about countermeasures
--- @return CounterMeasures
function LoGetSnares()
    return CounterMeasures
end

--- Returns a string for a cockpit indicator (screen)
--- @return string
function list_indication(indicator_id)
    return "A"
end

--- Returns a list of pages (cockpit screens)
--- @return string
function list_cockpit_params()
    return ""
end

--- Maps value to from input_range to output_range
--- @param argument_value number
--- @param input_range table
--- @param output_range table
--- @return number
function ValueConvert(argument_value, input_range, output_range)
    return 0
end

--- Returns  altitude above sea level
--- @return number
function LoGetAltitudeAboveSeaLevel()
    return 0
end

--- Returns  altitude above ground level
--- @return number
function LoGetAltitudeAboveGroundLevel()
    return 0
end

--- Returns vertical velocity
--- @return number
function LoGetVerticalVelocity()
    return 0
end

--- Returns true air speed
--- @return number
function LoGetTrueAirSpeed()
    return 0
end

--- Returns mach number
--- @return number
function LoGetMachNumber()
    return 0
end

--- Returns angle of attack
--- @return number
function LoGetAngleOfAttack()
    return 0
end

--- Returns glide deviation
--- @return number
function LoGetGlideDeviation()
    return 0
end

--- Returns side deviation
--- @return number
function LoGetSideDeviation()
    return 0
end

--- Returns slip ball position
--- @return number
function LoGetSlipBallPosition()
    return 0
end

--- @class EngineSide
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field right number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field left number
EngineSide = { right = 0, left = 0 }

--- @class EngineInformation
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field RPM EngineSide
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Temperature EngineSide
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field HydraulicPressure EngineSide
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field FuelConsumption EngineSide
EngineInformation = { RPM = EngineSide, Temperature = EngineSide, HydraulicPressure = EngineSide, FuelConsumption = EngineSide }

--- Returns engine information
--- @return EngineInformation
function LoGetEngineInfo()
    return EngineInformation
end

--- @class GearValue
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field right number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field left number
GearValue = { right = 0, left = 0 }

--- @class MechanicalInformation
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field gear GearValue
MechanicalInformation = { gear = GearValue }

--- Returns mechanical information
--- @return MechanicalInformation
function LoGetMechInfo()
    return MechanicalInformation
end

So basically there are mocks to call main file BIOS.lua (see LocalCompile.lua):
Code:
package.path = package.path .. ";.\\LuaSocket\\?.lua"
package.cpath = package.cpath .. ";.\\LuaSocket\\?.dll"

package.path = lfs.writedir() .. "?.lua;" .. package.path

-- all requires must come after updates to package.path
local BIOSConfig = require("Scripts.DCS-BIOS.BIOSConfig")
local BIOSStateMachine = require("Scripts.DCS-BIOS.lib.BIOSStateMachine")
local ConnectionManager = require("Scripts.DCS-BIOS.lib.ConnectionManager")
local Log = require("Scripts.DCS-BIOS.lib.common.Log")
local Protocol = require("Scripts.DCS-BIOS.lib.Protocol")
local TCPServer = require("Scripts.DCS-BIOS.lib.io.TCPServer")
local UDPServer = require("Scripts.DCS-BIOS.lib.io.UDPServer")
local socket = require("socket") --[[@as Socket]]

local MetadataEnd = require("Scripts.DCS-BIOS.lib.modules.common_modules.MetadataEnd")
local MetadataStart = require("Scripts.DCS-BIOS.lib.modules.common_modules.MetadataStart")

----------------------------------------------------------------------------Modules Start------------------------------------

local modules = {
    MetadataEnd,
    MetadataStart,
    require("Scripts.DCS-BIOS.lib.modules.common_modules.CommonData"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.A-10C"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.A-29B"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.A-4E-C"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.AH-6J"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.AH-64D"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.AJS37"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Alphajet"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.AV8BNA"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Bf-109K-4"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.C-101"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.CH-47F"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Christen Eagle II"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Edge540"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-14"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-15E"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-16C_50"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-22A"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-4E"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-5E-3"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-86F Sabre"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.FA-18C_hornet"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.FC3"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.FW-190A8"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.FW-190D9"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.I-16"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.JF-17"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Ka-50"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.L-39"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.M-2000C"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MB-339"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MH-60R"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Mi-24P"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Mi-8MT"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MirageF1"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MiG-15bis"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MiG-19P"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MiG-21Bis"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Mosquito"),
    require("Scripts.DCS-BIOS.lib.modules.common_modules.NS430"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.OH-58D"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.P-47D"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.P-51D"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.SA342"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.SpitfireLFMkIX"),
    -- require("Scripts.DCS-BIOS.lib.modules.common_modules.SuperCarrier"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.UH-1H"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.VNAO_Room"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.VNAO_T-45"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Yak-52"),
}

for _, module in ipairs(modules) do
    Protocol.writeNewModule(module)
end

----------------------------------------------------------------------------Modules End--------------------------------------
--Saves aliases for each aircraft for external programs
pcall(Protocol.saveAliases)
-- save constants for arduino devs to a header file
pcall(Protocol.saveAddresses)

-- Prev Export functions.
local PrevExport = {}
PrevExport.LuaExportStart = LuaExportStart
PrevExport.LuaExportStop = LuaExportStop
PrevExport.LuaExportBeforeNextFrame = LuaExportBeforeNextFrame
PrevExport.LuaExportAfterNextFrame = LuaExportAfterNextFrame

local connection_manager = ConnectionManager:new({})

local state_machine = BIOSStateMachine:new(Protocol.aircraft_names_to_modules(), MetadataStart, MetadataEnd, 11000, connection_manager)

local function process_input_line(line)
    state_machine:processInputLine(line)
end

for _, udp in ipairs(BIOSConfig.udp_config) do
    connection_manager:addConnection(UDPServer:new(udp.send_address, udp.send_port, udp.receive_address, udp.receive_port, socket, process_input_line))
end

for _, tcp in ipairs(BIOSConfig.tcp_config) do
    connection_manager:addConnection(TCPServer:new(tcp.address, tcp.port, socket, process_input_line))
end

-- track whether we successfully initialized dcs-bios, so we don't try exporting if something went wrong
local initialized = false

--- @param func function? the nested function to call
local function call_function_safe(func)
    if func then
        local status, result = pcall(func)
        if not status then
            Log:log_error("error calling export function from another script")
            Log:log_error(result)
        end
    end
end

-- Lua Export Functions
LuaExportStart = function()
    local status, result = pcall(state_machine.init, state_machine)

    if not status then
        Log:log_error("error initializing export server")
        Log:log_error(result)
    end

    initialized = true

    -- Chain previously-included export as necessary
    call_function_safe(PrevExport.LuaExportStart)
end

LuaExportStop = function()
    local status, result = pcall(state_machine.shutdown, state_machine)

    if not status then
        Log:log_error("error shutting down export server")
        Log:log_error(result)
    end

    initialized = false

    -- Chain previously-included export as necessary
    call_function_safe(PrevExport.LuaExportStop)
end

function LuaExportBeforeNextFrame()
    if initialized then
        local status, result = pcall(state_machine.receive, state_machine)

        if not status then
            Log:log_error("error receiving data from network connections")
            Log:log_error(result)
        end
    end

    -- Chain previously-included export as necessary
    call_function_safe(PrevExport.LuaExportBeforeNextFrame)
end

function LuaExportAfterNextFrame()
    if initialized then
        local status, result = pcall(state_machine.step, state_machine)

        if not status then
            Log:log_error("error exporting cockpit data")
            Log:log_error(result)
        end
    end

    -- Chain previously-included export as necessary
    call_function_safe(PrevExport.LuaExportAfterNextFrame)
end
So if I'm in directory were Scripts dir is, I can call:
Code:
PS C:\Users\mplichta\Projects\dcspy> dir

    Directory: C:\Users\mplichta\Projects\dcspy

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          17.09.2024    16:36                Scripts

PS C:\Users\mplichta\Projects\dcspy> lua Scripts\DCS-BIOS\test\compile\LocalCompile.lua
and it generate bunch of JSONs in Scripts\DCS-BIOS\doc\json directory
 
So LocalCompile.lua orginal contet:
Code:
-- This can be used to run the DCS-BIOS JSON generation locally without
-- starting a DCS mission
-- DCS-BIOS wiki Developer Guide have information on how to install the lua interpreter. No need to install
-- additional lua modules such as socket etc.
-- Execute => lua LocalCompile-lua or run it via VS Code task

package.path = "./Scripts/DCS-BIOS/test/compile/?.lua;" .. package.path

lfs = require("Scripts.DCS-BIOS.test.compile.lfs")

-- Include these that will mock the DCS APIs and the socket.
dofile([[Scripts/DCS-BIOS/BIOS.lua]])
in Scripts/DCS-BIOS/test/compile directory there are other files as well:
Code:
DCS_API_mocks.lua
lfs.lua
LocalCompile.lua
Log.lua
socket.lua
lfs.lua:
Code:
module("lfs", package.seeall)

---@diagnostic disable-next-line: lowercase-global
lfs = {}

---@diagnostic disable-next-line: duplicate-set-field
lfs.writedir = function()
    return "./"
end

return lfs
log.lua:
Code:
module("Log", package.seeall)

--- @class Log
local Log = {}

--- mock
--- @param str string
function Log:log(str) end

return Log
socket.lua:
Code:
module("socket", package.seeall)

local MockSocket = require("Scripts.DCS-BIOS.test.io.MockSocket")

---@diagnostic disable-next-line: lowercase-global
socket = MockSocket:new()

return socket
DCS_API_mocks.lua:
Code:
---@diagnostic disable: duplicate-set-field

---@class CockpitDevice
--- DCS Cockpit Device
CockpitDevice = {}

loadfile = loadfile

--- Sets command for a device
--- @param command_id integer
--- @param value integer
function CockpitDevice:SetCommand(command_id, value) end

--- Sets a device's argument?
--- @param argument_id integer
--- @param value integer
function CockpitDevice:set_argument_value(argument_id, value) end

--- Sets a device's frequency
--- @param frequency number
function CockpitDevice:set_frequency(frequency) end

--- Gets a device's frequency
--- @return number
function CockpitDevice:get_frequency()
    return 0
end

--- Gets a device based on the device ID as specified in devices.lua
--- @param argument_id integer
--- @return number The current value of the CockpitDevice
function CockpitDevice:get_argument_value(argument_id)
    return 0
end

--- Makes the device perform an action
--- @param command_id integer
--- @param argument number
function CockpitDevice:performClickableAction(command_id, argument) end

--- Gets a device based on the device ID as specified in devices.lua
--- @param device_id integer
--- @return CockpitDevice
function GetDevice(device_id)
    return CockpitDevice
end

--- Gets the draw value for a certain animation
--- @param draw_argument_id integer
--- @return number
function LoGetAircraftDrawArgumentValue(draw_argument_id)
    return 0
end

--- Contains Postions and Altitude
--- @class LatLongAlt
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Lat number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Long number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Alt number Altitude in meters
LatLongAlt = { Lat = 0, Long = 0, Alt = 0 }

--- Contains Aircraft Data
--- @class AicraftData
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Name string
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Heading number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field LatLongAlt LatLongAlt
AicraftData = { Name = "Test", Heading = 0, LatLongAlt }

--- Returns SelfData that holds information about the aircraft
--- @return AicraftData
function LoGetSelfData()
    return AicraftData
end

--- Returns the simulation time
--- @return number
function LoGetModelTime()
    return 0
end

--- Returns the mission start time
--- @return number
function LoGetMissionStartTime()
    return 0
end

--- Returns whether own ships/aircraft data can be exported.
--- @return boolean
function LoIsOwnshipExportAllowed()
    return true
end

--- Returns pilot's name
--- @return string
function LoGetPilotName()
    return "Neil Armstrong"
end

--- Returns indicated airspeed
--- @return number
function LoGetIndicatedAirSpeed()
    return 0
end

--- Contains acceleration info for all dimensions
--- @class AccelerationUnit
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field x number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field y number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field z number
AccelerationUnit = { x = 0, y = 0, z = 0 }

--- Returns G Load
--- @return AccelerationUnit
function LoGetAccelerationUnits()
    return AccelerationUnit
end

--- Returns ADI pitch, band, yaw
--- @return number
function LoGetADIPitchBankYaw()
    return unpack({ 0, 0, 0 })
end

--- @class CounterMeasures
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field chaff number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field flare number
CounterMeasures = { chaff = 0, flare = 0 }

--- Returns information about countermeasures
--- @return CounterMeasures
function LoGetSnares()
    return CounterMeasures
end

--- Returns a string for a cockpit indicator (screen)
--- @return string
function list_indication(indicator_id)
    return "A"
end

--- Returns a list of pages (cockpit screens)
--- @return string
function list_cockpit_params()
    return ""
end

--- Maps value to from input_range to output_range
--- @param argument_value number
--- @param input_range table
--- @param output_range table
--- @return number
function ValueConvert(argument_value, input_range, output_range)
    return 0
end

--- Returns  altitude above sea level
--- @return number
function LoGetAltitudeAboveSeaLevel()
    return 0
end

--- Returns  altitude above ground level
--- @return number
function LoGetAltitudeAboveGroundLevel()
    return 0
end

--- Returns vertical velocity
--- @return number
function LoGetVerticalVelocity()
    return 0
end

--- Returns true air speed
--- @return number
function LoGetTrueAirSpeed()
    return 0
end

--- Returns mach number
--- @return number
function LoGetMachNumber()
    return 0
end

--- Returns angle of attack
--- @return number
function LoGetAngleOfAttack()
    return 0
end

--- Returns glide deviation
--- @return number
function LoGetGlideDeviation()
    return 0
end

--- Returns side deviation
--- @return number
function LoGetSideDeviation()
    return 0
end

--- Returns slip ball position
--- @return number
function LoGetSlipBallPosition()
    return 0
end

--- @class EngineSide
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field right number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field left number
EngineSide = { right = 0, left = 0 }

--- @class EngineInformation
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field RPM EngineSide
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field Temperature EngineSide
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field HydraulicPressure EngineSide
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field FuelConsumption EngineSide
EngineInformation = { RPM = EngineSide, Temperature = EngineSide, HydraulicPressure = EngineSide, FuelConsumption = EngineSide }

--- Returns engine information
--- @return EngineInformation
function LoGetEngineInfo()
    return EngineInformation
end

--- @class GearValue
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field right number
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field left number
GearValue = { right = 0, left = 0 }

--- @class MechanicalInformation
--- @diagnostic disable-next-line: duplicate-doc-field
--- @field gear GearValue
MechanicalInformation = { gear = GearValue }

--- Returns mechanical information
--- @return MechanicalInformation
function LoGetMechInfo()
    return MechanicalInformation
end

So basically there are mocks to call main file BIOS.lua (see LocalCompile.lua):
Code:
package.path = package.path .. ";.\\LuaSocket\\?.lua"
package.cpath = package.cpath .. ";.\\LuaSocket\\?.dll"

package.path = lfs.writedir() .. "?.lua;" .. package.path

-- all requires must come after updates to package.path
local BIOSConfig = require("Scripts.DCS-BIOS.BIOSConfig")
local BIOSStateMachine = require("Scripts.DCS-BIOS.lib.BIOSStateMachine")
local ConnectionManager = require("Scripts.DCS-BIOS.lib.ConnectionManager")
local Log = require("Scripts.DCS-BIOS.lib.common.Log")
local Protocol = require("Scripts.DCS-BIOS.lib.Protocol")
local TCPServer = require("Scripts.DCS-BIOS.lib.io.TCPServer")
local UDPServer = require("Scripts.DCS-BIOS.lib.io.UDPServer")
local socket = require("socket") --[[@as Socket]]

local MetadataEnd = require("Scripts.DCS-BIOS.lib.modules.common_modules.MetadataEnd")
local MetadataStart = require("Scripts.DCS-BIOS.lib.modules.common_modules.MetadataStart")

----------------------------------------------------------------------------Modules Start------------------------------------

local modules = {
    MetadataEnd,
    MetadataStart,
    require("Scripts.DCS-BIOS.lib.modules.common_modules.CommonData"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.A-10C"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.A-29B"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.A-4E-C"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.AH-6J"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.AH-64D"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.AJS37"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Alphajet"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.AV8BNA"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Bf-109K-4"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.C-101"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.CH-47F"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Christen Eagle II"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Edge540"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-14"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-15E"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-16C_50"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-22A"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-4E"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-5E-3"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.F-86F Sabre"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.FA-18C_hornet"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.FC3"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.FW-190A8"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.FW-190D9"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.I-16"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.JF-17"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Ka-50"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.L-39"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.M-2000C"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MB-339"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MH-60R"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Mi-24P"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Mi-8MT"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MirageF1"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MiG-15bis"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MiG-19P"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.MiG-21Bis"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Mosquito"),
    require("Scripts.DCS-BIOS.lib.modules.common_modules.NS430"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.OH-58D"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.P-47D"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.P-51D"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.SA342"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.SpitfireLFMkIX"),
    -- require("Scripts.DCS-BIOS.lib.modules.common_modules.SuperCarrier"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.UH-1H"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.VNAO_Room"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.VNAO_T-45"),
    require("Scripts.DCS-BIOS.lib.modules.aircraft_modules.Yak-52"),
}

for _, module in ipairs(modules) do
    Protocol.writeNewModule(module)
end

----------------------------------------------------------------------------Modules End--------------------------------------
--Saves aliases for each aircraft for external programs
pcall(Protocol.saveAliases)
-- save constants for arduino devs to a header file
pcall(Protocol.saveAddresses)

-- Prev Export functions.
local PrevExport = {}
PrevExport.LuaExportStart = LuaExportStart
PrevExport.LuaExportStop = LuaExportStop
PrevExport.LuaExportBeforeNextFrame = LuaExportBeforeNextFrame
PrevExport.LuaExportAfterNextFrame = LuaExportAfterNextFrame

local connection_manager = ConnectionManager:new({})

local state_machine = BIOSStateMachine:new(Protocol.aircraft_names_to_modules(), MetadataStart, MetadataEnd, 11000, connection_manager)

local function process_input_line(line)
    state_machine:processInputLine(line)
end

for _, udp in ipairs(BIOSConfig.udp_config) do
    connection_manager:addConnection(UDPServer:new(udp.send_address, udp.send_port, udp.receive_address, udp.receive_port, socket, process_input_line))
end

for _, tcp in ipairs(BIOSConfig.tcp_config) do
    connection_manager:addConnection(TCPServer:new(tcp.address, tcp.port, socket, process_input_line))
end

-- track whether we successfully initialized dcs-bios, so we don't try exporting if something went wrong
local initialized = false

--- @param func function? the nested function to call
local function call_function_safe(func)
    if func then
        local status, result = pcall(func)
        if not status then
            Log:log_error("error calling export function from another script")
            Log:log_error(result)
        end
    end
end

-- Lua Export Functions
LuaExportStart = function()
    local status, result = pcall(state_machine.init, state_machine)

    if not status then
        Log:log_error("error initializing export server")
        Log:log_error(result)
    end

    initialized = true

    -- Chain previously-included export as necessary
    call_function_safe(PrevExport.LuaExportStart)
end

LuaExportStop = function()
    local status, result = pcall(state_machine.shutdown, state_machine)

    if not status then
        Log:log_error("error shutting down export server")
        Log:log_error(result)
    end

    initialized = false

    -- Chain previously-included export as necessary
    call_function_safe(PrevExport.LuaExportStop)
end

function LuaExportBeforeNextFrame()
    if initialized then
        local status, result = pcall(state_machine.receive, state_machine)

        if not status then
            Log:log_error("error receiving data from network connections")
            Log:log_error(result)
        end
    end

    -- Chain previously-included export as necessary
    call_function_safe(PrevExport.LuaExportBeforeNextFrame)
end

function LuaExportAfterNextFrame()
    if initialized then
        local status, result = pcall(state_machine.step, state_machine)

        if not status then
            Log:log_error("error exporting cockpit data")
            Log:log_error(result)
        end
    end

    -- Chain previously-included export as necessary
    call_function_safe(PrevExport.LuaExportAfterNextFrame)
end
So if I'm in directory were Scripts dir is, I can call:
Code:
PS C:\Users\mplichta\Projects\dcspy> dir

    Directory: C:\Users\mplichta\Projects\dcspy

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          17.09.2024    16:36                Scripts

PS C:\Users\mplichta\Projects\dcspy> lua Scripts\DCS-BIOS\test\compile\LocalCompile.lua
and it generate bunch of JSONs in Scripts\DCS-BIOS\doc\json directory
Not what I was asking... let me rephrase:
What is the purpose of your script? What are you trying to accomplish?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom