Jump to content

reading & writing files with the sanitization module


Recommended Posts

Posted

Simple question, for (maybe) hard answer:

 

How can I launch MIST and subsequently another script by me that read & writes in external files information and data whitout having to comment/disable the sanitization module?

 

I may not require to launch it inside the mission (like now), but the script will need to access the Scripting engine which rely on. Also, the best option would be to be able to schedule the launch of those files after the mission goes online...

 

Any help will be extremely appreciated :D

ChromiumDis.png

Author of DSMC, mod to enable scenario persistency and save updated miz file

Stable version & site: https://dsmcfordcs.wordpress.com/

Openbeta: https://github.com/Chromium18/DSMC

 

The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.

Posted (edited)

You can modify the sanitation module to expose additional functions to mission scripts instead of disabling it entirely. For example, you could expose a function that allows reading and writing to a specific file.

 

Take a look at how it is done in DCS Witchcraft and the Mission Planner. Note that everything that can be used for privileged actions is kept inside local variables and only the functions that should be accessible to the mission scripts are exposed via global variables or functions.

 

Also, the best option would be to be able to schedule the launch of those files after the mission goes online...

Add a trigger to your mission which calls your function.

If you have to do it without modifying the mission file, you'll have to look at modding the server Lua environment (like SlMod does). I think there you could define a callback for when a new mission has loaded that could call your script by executing a piece of Lua code in the mission environment.

Edited by [FSF]Ian
Posted

I already use a do script file that is nothing else than this to launch the script:

 

dofile(lfs.writedir() .. 'DAWS/mist.lua')
dofile(lfs.writedir() .. 'DAWS/DAWS_main.lua')

 

And I believe that this is the very best solution to be able to use/not use my script function in a single fast step. So, I would prefer to have this script to be launched as a triggered action as I do now and as you suggested, instead of making it running by default.

 

But I was concerned that launching this inside the SSE wouldn't grant me any possible bypass to the sanitization module. I'll check this now!

 

I will look into your script to try to undestand how you bypass the sanitization module. My best would be to be able to read & write freely inside the "DAWS" folder that is inside the "saved games" DCS folder.

ChromiumDis.png

Author of DSMC, mod to enable scenario persistency and save updated miz file

Stable version & site: https://dsmcfordcs.wordpress.com/

Openbeta: https://github.com/Chromium18/DSMC

 

The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.

Posted

Let's illustrate how this works using the "io.open()" function as an example.

To call any function such as io.open() from a Lua script, you need to have a reference to this function. By default, Lua will provide the global variable "io" that is a table containing, among others, the "open" function.

 

You can see for yourself using the interactive Lua interpreter:

% lua
Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> print(tostring(io))
table: 0xdedfb0
> print(tostring(io.open))
function: 0x41bd90
>

 

The sanitation module works by "throwing those references away" (setting them to nil).

> io = nil
> print(tostring(io))
nil
> print(tostring(io.open))
stdin:1: attempt to index global 'io' (a nil value)
stack traceback:
       stdin:1: in main chunk
       [C]: in ?
>

 

What you want to do then is to (a) grab a copy of that reference before it is deleted and (b) store that copy in a local variable so no one else can access it.

Posted

I'm trying to understand what you mean... should I create a local copy of io.open, write etc etc before calling the sanitization module. But How should I recall those locals outside, in a script ran in SSE?

 

(i'm looking now at witchcraft code, i may understand this during next days whitout hassling you so much... :()

ChromiumDis.png

Author of DSMC, mod to enable scenario persistency and save updated miz file

Stable version & site: https://dsmcfordcs.wordpress.com/

Openbeta: https://github.com/Chromium18/DSMC

 

The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.

Posted

Instead, if I load my DAWS functions table (my structure is similar to witchcraft one...) before sanitization module, should I be able to use io etc in any funciton that is stored in the DAWS table?

 

i.e.

 

 

I load this in MissionScripting.lua before sanitizatio module:

 

DAWS = {}

 

then in my script file, loaded in the SSE during the mission, I have:

 

DAWS.squadronTableRead = function()
 squadronTable = {}
 local a = io.open(lfs.writedir() .. DAWS_tabledirectory .. sqnDataFile, "r")
 
 for line in io.lines(a) do
  local rsqnID, rsqnCoa, rsqnName, rsqnBase, rsqnAcf, rsqnCountry, rsqnCallsign, rsqnMisType, rsqnAcfNum = line:match("(%d-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(%d-)$")
   if (rsqnID) then 
   squadronTable[#squadronTable + 1] = { sqnID = rsqnID, sqnCoa = rsqnCoa, sqnName = rsqnName, sqnBase = rsqnBase, sqnAcf = rsqnAcf, sqnCountry = rsqnCountry, sqnCallsign = rsqnCallsign, sqnMisType = rsqnMisType, sqnAcfNum = tonumber(rsqnAcfNum) }
  end
 end
 a:close()
end 

 

Should it work, cause I created DAWS (a void table at the moment) before "sanitize" io functions?

ChromiumDis.png

Author of DSMC, mod to enable scenario persistency and save updated miz file

Stable version & site: https://dsmcfordcs.wordpress.com/

Openbeta: https://github.com/Chromium18/DSMC

 

The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.

Posted (edited)

That does not work. When your function tries to call "io.open", because you do not have a local variable called "io", Lua will try to access the global "io" variable, which has been set to nil by the time your function is called.

 

You have to create your copy of the "io" variable at the time your function is defined, which happens before the sanitation module runs (because you insert your dofile(...) line before it in MissionScripting.lua).

 

You could do this:

DAWS.io = io -- make a copy of the "io" variable
DAWS.squadronTableRead = function()
 squadronTable = {}
 local a = DAWS.io.open(lfs.writedir() .. DAWS_tabledirectory .. sqnDataFile, "r")
 
 for line in DAWS.io.lines(a) do
  local rsqnID, rsqnCoa, rsqnName, rsqnBase, rsqnAcf, rsqnCountry, rsqnCallsign, rsqnMisType, rsqnAcfNum = line:match("(%d-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(%d-)$")
   if (rsqnID) then 
   squadronTable[#squadronTable + 1] = { sqnID = rsqnID, sqnCoa = rsqnCoa, sqnName = rsqnName, sqnBase = rsqnBase, sqnAcf = rsqnAcf, sqnCountry = rsqnCountry, sqnCallsign = rsqnCallsign, sqnMisType = rsqnMisType, sqnAcfNum = tonumber(rsqnAcfNum) }
  end
 end
 a:close()
end

That would work, but you only gained some "security by obscurity" -- any mission script can access the io module through DAWS.io, because DAWS is a global variable.

 

Let's make our copy in a local variable instead:

local daws_tools = {} -- daws_tools will only be accessible from functions defined in the same file
daws_tools.io = io -- make a copy of the global io variable
DAWS.squadronTableRead = function()
 squadronTable = {}
 local a = daws_tools.io.open(lfs.writedir() .. DAWS_tabledirectory .. sqnDataFile, "r")
 
 for line in daws_tools.io.lines(a) do
  local rsqnID, rsqnCoa, rsqnName, rsqnBase, rsqnAcf, rsqnCountry, rsqnCallsign, rsqnMisType, rsqnAcfNum = line:match("(%d-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(.-)"..DAWS_tss.."(%d-)$")
   if (rsqnID) then 
   squadronTable[#squadronTable + 1] = { sqnID = rsqnID, sqnCoa = rsqnCoa, sqnName = rsqnName, sqnBase = rsqnBase, sqnAcf = rsqnAcf, sqnCountry = rsqnCountry, sqnCallsign = rsqnCallsign, sqnMisType = rsqnMisType, sqnAcfNum = tonumber(rsqnAcfNum) }
  end
 end
 a:close()
end

 

EDIT: Of course, you also have to make copies of anything else the sanitation module removes, e.g. "lfs" and "require".

Edited by [FSF]Ian
Posted (edited)

So I could load my script by dofile (es. "dofile(lfs.writedir() .. 'DAWS/DAWS_main.lua')") in the MissionScripting.lua... but I also need to include in the first lines of DAWS_main.lua the io copy you suggested me to do, in a local fancy.

 

(Oh, sorry, I really hope that I understood this... :( )

 

 

EDIT:

 

no, that way I will load DAWS before the mission... or not?

 

 

EDIT2:

 

since I have a LOT of functions that read or write external files, I can't think of putting all of those inside the MissionScripting.lua.

Edited by chromium

ChromiumDis.png

Author of DSMC, mod to enable scenario persistency and save updated miz file

Stable version & site: https://dsmcfordcs.wordpress.com/

Openbeta: https://github.com/Chromium18/DSMC

 

The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.

Posted
So I could load my script by dofile (es. "dofile(lfs.writedir() .. 'DAWS/DAWS_main.lua')") in the MissionScripting.lua... but I also need to include in the first lines of DAWS_main.lua the io copy you suggested me to do, in a local fancy.

 

(Oh, sorry, I really hope that I understood this... :( )

 

Yes, that's exactly what you want to do.

 

EDIT:

 

no, that way I will load DAWS before the mission... or not?

Yes, you will load DAWS (define your functions) before the mission. Doesn't mean you have to run it before the mission has started.

 

EDIT2:

 

since I have a LOT of functions that read or write external files, I can't think of putting all of those inside the MissionScripting.lua.

You don't have to -- they go into daws_main.lua, which is executed with the dofile() line from MissionScripting.lua.

 

BTW, I am on TS right now (teamspeak.combined-ops-group.com) if you want to talk instead of write.

Posted

Can't, at the moment , I'm going home right now ... but I swear that there will be an occasion (I remember that we tried to talk about my project before summer).

 

PS:

understood. I'm lucky: DAWS will run with a small code that schedules one function after another by state cycle. I will only have to trigger the first state when I want after mission start :)

ChromiumDis.png

Author of DSMC, mod to enable scenario persistency and save updated miz file

Stable version & site: https://dsmcfordcs.wordpress.com/

Openbeta: https://github.com/Chromium18/DSMC

 

The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.

Posted

It works as expected, thanks so much :D

ChromiumDis.png

Author of DSMC, mod to enable scenario persistency and save updated miz file

Stable version & site: https://dsmcfordcs.wordpress.com/

Openbeta: https://github.com/Chromium18/DSMC

 

The thing is, helicopters are different from planes. An airplane by it's nature wants to fly, and if not interfered with too strongly by unusual events or by a deliberately incompetent pilot, it will fly. A helicopter does not want to fly. It is maintained in the air by a variety of forces in opposition to each other, and if there is any disturbance in this delicate balance the helicopter stops flying; immediately and disastrously.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...