@@ -427,13 +427,16 @@ def do_startup(self):
427427 missions = []
428428 for mission in self .settings ['missionList' ]:
429429 if '.dcssb' in mission :
430- secondary = os .path .join (os .path .dirname (os .path .dirname (mission )), os .path .basename (mission ))
430+ _mission = os .path .join (os .path .dirname (os .path .dirname (mission )), os .path .basename (mission ))
431431 else :
432- secondary = os .path .join (os .path .dirname (mission ), '.dcssb' , os .path .basename (mission ))
433- if os .path .exists (mission ):
432+ _mission = mission
433+ # check if the orig file has been updated
434+ orig = _mission + '.orig'
435+ if os .path .exists (orig ) and os .path .getmtime (orig ) > os .path .getmtime (mission ):
436+ shutil .copy2 (orig , _mission )
437+ missions .append (_mission )
438+ elif os .path .exists (mission ):
434439 missions .append (mission )
435- elif os .path .exists (secondary ):
436- missions .append (secondary )
437440 else :
438441 self .log .warning (f"Removing mission { mission } from serverSettings.lua as it could not be found!" )
439442 if len (missions ) != len (self .settings ['missionList' ]):
@@ -724,20 +727,25 @@ async def keep_alive(self):
724727 WHERE node = %s AND server_name = %s
725728 """ , (self .node .name , self .name ))
726729
727- async def uploadMission (self , filename : str , url : str , force : bool = False , missions_dir : str = None ) -> UploadStatus :
730+ async def uploadMission (self , filename : str , url : str , * , missions_dir : str = None , force : bool = False ,
731+ orig = False ) -> UploadStatus :
728732 if not missions_dir :
729733 missions_dir = self .instance .missions_dir
730734 filename = os .path .normpath (os .path .join (missions_dir , filename ))
731- secondary = os .path .join (os .path .dirname (filename ), '.dcssb' , os .path .basename (filename ))
732- for idx , name in enumerate (self .settings ['missionList' ]):
733- if (os .path .normpath (name ) == filename ) or (os .path .normpath (name ) == secondary ):
734- if self .current_mission and idx == int (self .settings ['listStartIndex' ]) - 1 :
735- if not force :
736- return UploadStatus .FILE_IN_USE
737- add = True
738- break
735+ if orig :
736+ filename += '.orig'
737+ add = False
739738 else :
740- add = self .locals .get ('autoadd' , True )
739+ secondary = os .path .join (os .path .dirname (filename ), '.dcssb' , os .path .basename (filename ))
740+ for idx , name in enumerate (self .settings ['missionList' ]):
741+ if (os .path .normpath (name ) == filename ) or (os .path .normpath (name ) == secondary ):
742+ if self .current_mission and idx == int (self .settings ['listStartIndex' ]) - 1 :
743+ if not force :
744+ return UploadStatus .FILE_IN_USE
745+ add = True
746+ break
747+ else :
748+ add = self .locals .get ('autoadd' , True )
741749 rc = await self .node .write_file (filename , url , force )
742750 if rc != UploadStatus .OK :
743751 return rc
@@ -859,6 +867,24 @@ async def replaceMission(self, mission_id: int, path: str) -> list[str]:
859867 return self .settings ['missionList' ]
860868
861869 async def loadMission (self , mission : Union [int , str ], modify_mission : Optional [bool ] = True ) -> bool :
870+ # check if we re-load the running mission
871+ start_index = int (self .settings ['listStartIndex' ])
872+ if ((isinstance (mission , int ) and mission == start_index ) or
873+ (isinstance (mission , str ) and mission == self ._get_current_mission_file ())):
874+ mission = self .settings ['missionList' ][start_index - 1 ]
875+ # now determine the original mission name
876+ _mission = utils .get_orig_file (mission )
877+ # check if the orig file has been replaced
878+ if os .path .exists (_mission ) and os .path .getmtime (_mission ) > os .path .getmtime (mission ):
879+ new_filename = utils .create_writable_mission (mission )
880+ # we can't write the original one, so use the copy
881+ if new_filename != mission :
882+ shutil .copy2 (_mission , new_filename )
883+ await self .replaceMission (start_index , new_filename )
884+ return await self .loadMission (start_index , modify_mission = modify_mission )
885+ else :
886+ return await self .loadMission (start_index , modify_mission = modify_mission )
887+
862888 if isinstance (mission , int ):
863889 if mission > len (self .settings ['missionList' ]):
864890 mission = 1
@@ -878,7 +904,7 @@ async def loadMission(self, mission: Union[int, str], modify_mission: Optional[b
878904 else :
879905 try :
880906 idx = self .settings ['missionList' ].index (filename ) + 1
881- if idx == int ( self . settings [ 'listStartIndex' ]) :
907+ if idx == start_index :
882908 rc = await self .send_to_dcs_sync ({"command" : "startMission" , "filename" : filename })
883909 else :
884910 rc = await self .send_to_dcs_sync ({"command" : "startMission" , "id" : idx })
@@ -971,3 +997,30 @@ async def uninstall_extension(self, name: str) -> None:
971997 async def cleanup (self ) -> None :
972998 tempdir = os .path .join (tempfile .gettempdir (), self .instance .name )
973999 await asyncio .to_thread (utils .safe_rmtree , tempdir )
1000+
1001+ async def getAllMissionFiles (self ) -> list [tuple [str , str ]]:
1002+ def shorten_filename (file : str ) -> str :
1003+ if file .endswith ('.orig' ):
1004+ return file [:- 5 ]
1005+ if '.dcssb' in file :
1006+ return file .replace (os .path .sep + '.dcssb' , '' )
1007+ return file
1008+
1009+ result = []
1010+ base_dir , all_missions = await self .node .list_directory (self .instance .missions_dir , pattern = "*.miz" ,
1011+ ignore = ['.dcssb' , 'Scripts' , 'Saves' ], traverse = True )
1012+ for mission in all_missions :
1013+ orig = utils .get_orig_file (mission , create_file = False )
1014+ secondary = os .path .join (
1015+ os .path .dirname (mission ), '.dcssb' , os .path .basename (mission )
1016+ )
1017+ if orig and os .path .getmtime (orig ) > os .path .getmtime (mission ):
1018+ file = orig
1019+ else :
1020+ file = mission
1021+ if os .path .exists (secondary ) and os .path .getmtime (secondary ) > os .path .getmtime (file ):
1022+ file = secondary
1023+
1024+ result .append ((shorten_filename (file ), file ))
1025+
1026+ return result
0 commit comments