Merge pull request #161 from VEAF/make-mission-portable

Make mission portable
This commit is contained in:
C. Perreau 2020-09-29 23:50:34 +02:00 committed by GitHub
commit f68e6387e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 32 deletions

View File

@ -1,3 +1,6 @@
# 2.x.x
* **[Mission Generator]** Use inline loading of the JSON.lua library, and save to either %LIBERATION_EXPORT_DIR%, to %TEMP%, or to DCS working directory
# 2.1.2 # 2.1.2
## Fixes : ## Fixes :

View File

@ -250,41 +250,80 @@ class Operation:
print(e) print(e)
# Inject Mist Script if not done already in the plugins # Inject Mist Script if not done already in the plugins
if not "mist.lua" in listOfPluginsScripts and not "mist_4_3_74.lua" in listOfPluginsScripts: # don't load mist twice if not "mist.lua" in listOfPluginsScripts and not "mist_4_3_74.lua" in listOfPluginsScripts: # don't load the script twice
trigger = TriggerStart(comment="Load Mist Lua Framework") trigger = TriggerStart(comment="Load Mist Lua framework")
fileref = self.current_mission.map_resource.add_resource_file("./resources/scripts/mist_4_3_74.lua") fileref = self.current_mission.map_resource.add_resource_file("./resources/scripts/mist_4_3_74.lua")
trigger.add_action(DoScriptFile(fileref)) trigger.add_action(DoScriptFile(fileref))
self.current_mission.triggerrules.triggers.append(trigger) self.current_mission.triggerrules.triggers.append(trigger)
# Inject Liberation script # Inject JSON library if not done already in the plugins
load_dcs_libe = TriggerStart(comment="Load DCS Liberation Script") if not "json.lua" in listOfPluginsScripts : # don't load the script twice
with open("./resources/scripts/dcs_liberation.lua") as f: trigger = TriggerStart(comment="Load JSON Lua library")
script = f.read() fileref = self.current_mission.map_resource.add_resource_file("./resources/scripts/json.lua")
json_location = "[["+os.path.abspath("resources\\scripts\\json.lua")+"]]" trigger.add_action(DoScriptFile(fileref))
self.current_mission.triggerrules.triggers.append(trigger)
# set a LUA table with data from Liberation that we want to set
# at the moment it contains Liberation's install path, and an overridable definition for the JTACAutoLase function
# later, we'll add data about the units and points having been generated, in order to facilitate the configuration of the plugin lua scripts
state_location = "[[" + os.path.abspath("state.json") + "]]" state_location = "[[" + os.path.abspath("state.json") + "]]"
script = script.replace("{{json_file_abs_location}}", json_location) lua = """
script = script.replace("{{debriefing_file_location}}", state_location) -- setting configuration table
load_dcs_libe.add_action(DoScript(String(script))) env.info("DCSLiberation|: setting configuration table")
self.current_mission.triggerrules.triggers.append(load_dcs_libe)
# Load Ciribob's JTACAutoLase script if not done already in the plugins -- all data in this table is overridable.
if not "JTACAutoLase.lua" in listOfPluginsScripts: # don't load JTACAutoLase twice dcsLiberation = {}
load_autolase = TriggerStart(comment="Load JTAC script")
with open("./resources/scripts/JTACAutoLase.lua") as f:
script = f.read() -- the base location for state.json; if non-existent, it'll be replaced with LIBERATION_EXPORT_DIR, TEMP, or DCS working directory
script = script + "\n" dcsLiberation.installPath=""" + state_location + """
-- you can override dcsLiberation.JTACAutoLase to make it use your own function ; it will be called with these parameters : ({jtac.unit_name}, {jtac.code}, {smoke}, 'vehicle') for all JTACs
if ctld then
dcsLiberation.JTACAutoLase=ctld.JTACAutoLase
elseif JTACAutoLase then
dcsLiberation.JTACAutoLase=JTACAutoLase
end
-- later, we'll add more data to the table
--dcsLiberation.POIs = {}
--dcsLiberation.BASEs = {}
--dcsLiberation.JTACs = {}
"""
trigger = TriggerStart(comment="Set DCS Liberation data")
trigger.add_action(DoScript(String(lua)))
self.current_mission.triggerrules.triggers.append(trigger)
# Inject DCS-Liberation script if not done already in the plugins
if not "dcs_liberation.lua" in listOfPluginsScripts : # don't load the script twice
trigger = TriggerStart(comment="Load DCS Liberation script")
fileref = self.current_mission.map_resource.add_resource_file("./resources/scripts/dcs_liberation.lua")
trigger.add_action(DoScriptFile(fileref))
self.current_mission.triggerrules.triggers.append(trigger)
# Inject Ciribob's JTACAutoLase if not done already in the plugins
if not "JTACAutoLase.lua" in listOfPluginsScripts : # don't load the script twice
trigger = TriggerStart(comment="Load JTACAutoLase.lua script")
fileref = self.current_mission.map_resource.add_resource_file("./resources/scripts/JTACAutoLase.lua")
trigger.add_action(DoScriptFile(fileref))
self.current_mission.triggerrules.triggers.append(trigger)
# add a configuration for JTACAutoLase and start lasing for all JTACs
smoke = "true" smoke = "true"
if hasattr(self.game.settings, "jtac_smoke_on"): if hasattr(self.game.settings, "jtac_smoke_on"):
if not self.game.settings.jtac_smoke_on: if not self.game.settings.jtac_smoke_on:
smoke = "false" smoke = "false"
lua = """
-- setting and starting JTACs
env.info("DCSLiberation|: setting and starting JTACs")
"""
for jtac in jtacs: for jtac in jtacs:
script += f"\nJTACAutoLase('{jtac.unit_name}', {jtac.code}, {smoke}, 'vehicle')\n" lua += f"if dcsLiberation.JTACAutoLase then dcsLiberation.JTACAutoLase('{jtac.unit_name}', {jtac.code}, {smoke}, 'vehicle') end\n"
load_autolase.add_action(DoScript(String(script))) trigger = TriggerStart(comment="Start JTACs")
self.current_mission.triggerrules.triggers.append(load_autolase) trigger.add_action(DoScript(String(lua)))
self.current_mission.triggerrules.triggers.append(trigger)
self.assign_channels_to_flights() self.assign_channels_to_flights()

View File

@ -1,9 +1,5 @@
local jsonlib = {{json_file_abs_location}}
json = loadfile(jsonlib)()
logger = mist.Logger:new("DCSLiberation", "info") logger = mist.Logger:new("DCSLiberation", "info")
logger:info("Check that json.lua is loaded : json = "..tostring(json))
debriefing_file_location = {{debriefing_file_location}}
killed_aircrafts = {} killed_aircrafts = {}
killed_ground_units = {} killed_ground_units = {}
@ -32,12 +28,56 @@ write_state = function()
["mission_ended"] = mission_ended, ["mission_ended"] = mission_ended,
["destroyed_objects_positions"] = destroyed_objects_positions, ["destroyed_objects_positions"] = destroyed_objects_positions,
} }
if not json then
local message = string.format("Unable to save DCS Liberation state to %s, JSON library is not loaded !",debriefing_file_location)
logger:error(message)
messageAll(message)
end
fp:write(json:encode(game_state)) fp:write(json:encode(game_state))
fp:close() fp:close()
-- logger.info("Done writing DCS Liberation state") -- logger.info("Done writing DCS Liberation state")
-- messageAll("Done writing DCS Liberation state.") -- messageAll("Done writing DCS Liberation state.")
end end
debriefing_file_location = nil
if dcsLiberation then
debriefing_file_location = dcsLiberation.installPath
end
if debriefing_file_location then
logger:info("Using DCS Liberation install folder for state.json")
else
if os then
debriefing_file_location = os.getenv("LIBERATION_EXPORT_DIR")
if debriefing_file_location then debriefing_file_location = debriefing_file_location .. "\\" end
end
if debriefing_file_location then
logger:info("Using LIBERATION_EXPORT_DIR environment variable for state.json")
else
if os then
debriefing_file_location = os.getenv("TEMP")
if debriefing_file_location then debriefing_file_location = debriefing_file_location .. "\\" end
end
if debriefing_file_location then
logger:info("Using TEMP environment variable for state.json")
else
if lfs then
debriefing_file_location = lfs.writedir()
end
if debriefing_file_location then
logger:info("Using DCS working directory for state.json")
end
end
end
end
if debriefing_file_location then
local filename = "state.json"
if not debriefing_file_location:sub(-#filename) == filename then
debriefing_file_location = debriefing_file_location .. filename
end
logger:info(string.format("DCS Liberation state will be written as json to [[%s]]",debriefing_file_location))
else
logger:error("No usable storage path for state.json")
end
write_state_error_handling = function() write_state_error_handling = function()
if pcall(write_state) then if pcall(write_state) then

View File

@ -968,7 +968,7 @@ function OBJDEF:new(args)
return setmetatable(new, OBJDEF) return setmetatable(new, OBJDEF)
end end
return OBJDEF:new() json = OBJDEF:new()
-- --
-- Version history: -- Version history: