@@ -39,6 +39,13 @@ def version(self) -> Optional[str]:
3939 return utils .get_windows_version (os .path .join (os .path .expandvars (self .config ['installation' ]),
4040 'realweather.exe' ))
4141
42+ def load_config (self ) -> Optional [dict ]:
43+ try :
44+ with open (self .config_path , mode = 'rb' ) as infile :
45+ return tomli .load (infile )
46+ except tomli .TOMLDecodeError as ex :
47+ raise RealWeatherException (f"Error while reading { self .config_path } : { ex } " )
48+
4249 def get_config (self , filename : str ) -> dict :
4350 if 'terrains' in self .config :
4451 miz = MizFile (filename )
@@ -62,7 +69,7 @@ def get_icao_code(filename: str) -> Optional[str]:
6269 else :
6370 return None
6471
65- async def generate_config_1_0 (self , input_mission : str , output_mission : str , cwd : str ):
72+ async def generate_config_1_0 (self , input_mission : str , output_mission : str , override : Optional [ dict ] = None ):
6673 try :
6774 with open (self .config_path , mode = 'r' , encoding = 'utf-8' ) as infile :
6875 cfg = json .load (infile )
@@ -88,10 +95,10 @@ async def generate_config_1_0(self, input_mission: str, output_mission: str, cwd
8895 }
8996 }
9097 self .config ['metar' ] = {"icao" : icao }
91- with open ( os . path . join ( cwd , 'config.json' ), mode = 'w' , encoding = 'utf-8' ) as outfile :
92- json . dump ( cfg , outfile , indent = 2 )
98+ self . locals = utils . deep_merge ( cfg , override or {})
99+ await self . write_config ( )
93100
94- async def generate_config_2_0 (self , input_mission : str , output_mission : str , cwd : str ):
101+ async def generate_config_2_0 (self , input_mission : str , output_mission : str , override : Optional [ dict ] = None ):
95102 tmpfd , tmpname = tempfile .mkstemp ()
96103 os .close (tmpfd )
97104 try :
@@ -129,18 +136,26 @@ async def generate_config_2_0(self, input_mission: str, output_mission: str, cwd
129136 "icao" : icao
130137 }
131138 }
132- with open ( os . path . join ( cwd , 'config.toml' ), mode = 'wb' ) as outfile :
133- tomli_w . dump ( cfg , outfile )
139+ self . locals = utils . deep_merge ( cfg , override or {})
140+ await self . write_config ( )
134141
135- async def beforeMissionLoad (self , filename : str ) -> tuple [str , bool ]:
136- tmpfd , tmpname = tempfile .mkstemp ()
137- os .close (tmpfd )
142+ async def write_config (self ):
138143 cwd = await self .server .get_missions_dir ()
144+ if self .version .split ('.' )[0 ] == '1' :
145+ with open (os .path .join (cwd , 'config.json' ), mode = 'w' , encoding = 'utf-8' ) as outfile :
146+ json .dump (self .locals , outfile , indent = 2 )
147+ else :
148+ with open (os .path .join (cwd , 'config.toml' ), mode = 'wb' ) as outfile :
149+ tomli_w .dump (self .locals , outfile )
139150
151+ async def generate_config (self , filename : str , tmpname : str , config : Optional [dict ] = None ):
140152 if self .version .split ('.' )[0 ] == '1' :
141- await self .generate_config_1_0 (filename , tmpname , cwd )
153+ await self .generate_config_1_0 (filename , tmpname , config )
142154 else :
143- await self .generate_config_2_0 (filename , tmpname , cwd )
155+ await self .generate_config_2_0 (filename , tmpname , config )
156+
157+ async def run_realweather (self , filename : str , tmpname : str ) -> tuple [str , bool ]:
158+ cwd = await self .server .get_missions_dir ()
144159 rw_home = os .path .expandvars (self .config ['installation' ])
145160
146161 def run_subprocess ():
@@ -151,7 +166,7 @@ def run_subprocess():
151166 self .log .error (stderr .decode ('utf-8' ))
152167 output = stdout .decode ('utf-8' )
153168 metar = next ((x for x in output .split ('\n ' ) if 'METAR:' in x ), "" )
154- remarks = self .config .get ('realweather' , {}).get ('mission' , {}).get ('brief' , {}).get ('remarks' , 'RMK Generated by DCS Real Weather ' )
169+ remarks = self .locals .get ('realweather' , {}).get ('mission' , {}).get ('brief' , {}).get ('remarks' , '' )
155170 matches = re .search (rf"(?<=METAR: )(.*)(?= { remarks } )" , metar )
156171 if matches :
157172 self .metar = matches .group (0 )
@@ -171,6 +186,18 @@ def run_subprocess():
171186 os .remove (tmpname )
172187 return new_filename , True
173188
189+ async def beforeMissionLoad (self , filename : str ) -> tuple [str , bool ]:
190+ tmpfd , tmpname = tempfile .mkstemp ()
191+ os .close (tmpfd )
192+ await self .generate_config (filename , tmpname )
193+ return await self .run_realweather (filename , tmpname )
194+
195+ async def apply_realweather (self , filename : str , config : dict ) -> str :
196+ tmpfd , tmpname = tempfile .mkstemp ()
197+ os .close (tmpfd )
198+ await self .generate_config (utils .get_orig_file (filename ), tmpname , config )
199+ return (await self .run_realweather (filename , tmpname ))[0 ]
200+
174201 async def render (self , param : Optional [dict ] = None ) -> dict :
175202 if self .version .split ('.' )[0 ] == '1' :
176203 icao = self .config .get ('metar' , {}).get ('icao' )
0 commit comments