@@ -148,6 +148,8 @@ public class FolderTreeViewModel : ReactiveObject
148148 public ReactiveCommand < Unit , Unit > ? OpenCurrentFolder { get ; }
149149 public ReactiveCommand < FileSystemItem , Unit > ? OpenFolderFor { get ; }
150150 public ReactiveCommand < FileSystemItem , Unit > ? SelectOnlineBrowseFileSystemItem { get ; }
151+ public ReactiveCommand < FileSystemItem , Unit > ? DownloadOnlineItemCommand { get ; private set ; }
152+ public ReactiveCommand < OnlineItemPackBrowseResult , Unit > ? DownloadOnlinePackCommand { get ; private set ; }
151153
152154 public ObservableCollection < ObjectDisplayMode > DisplayModeItems { get ; } = [ .. Enum . GetValues < ObjectDisplayMode > ( ) ] ;
153155 static OnlineBrowseTargetOption ObjectOnlineBrowseTarget { get ; } = new ( OnlineApiEndpointGroup . Objects , "Objects" , "Objects" , Client . ObjectsEndpointGroup ) ;
@@ -242,6 +244,8 @@ public FolderTreeViewModel(ObjectEditorContext editorContext)
242244 OpenCurrentFolder = ReactiveCommand . Create ( ( ) => PlatformSpecific . FolderOpenInDesktop ( IsLocal ? CurrentLocalDirectory : this . EditorContext . Settings . DownloadFolder , this . EditorContext . Logger ) ) ;
243245 AddFilterCommand = ReactiveCommand . Create ( ( ) => Filters . Add ( new FilterViewModel ( this . EditorContext , availableFilterCategories , RemoveFilter ) ) ) ;
244246 SelectOnlineBrowseFileSystemItem = ReactiveCommand . Create < FileSystemItem > ( item => CurrentlySelectedObject = item ) ;
247+ DownloadOnlineItemCommand = ReactiveCommand . CreateFromTask < FileSystemItem > ( DownloadOnlineItemAsync ) ;
248+ DownloadOnlinePackCommand = ReactiveCommand . CreateFromTask < OnlineItemPackBrowseResult > ( DownloadOnlinePackAsync ) ;
245249 OpenFolderFor = ReactiveCommand . Create ( ( FileSystemItem clickedOn ) =>
246250 {
247251 if ( IsLocal
@@ -771,4 +775,57 @@ static FileSystemItem CreateOnlineScenarioFileSystemItem(DtoScenarioEntry item)
771775 {
772776 OnlineApiEndpointGroup = OnlineApiEndpointGroup . Scenarios ,
773777 } ;
778+
779+ async Task DownloadOnlineItemAsync ( FileSystemItem item )
780+ {
781+ if ( EditorContext . ObjectServiceClient == null || item . Id == null )
782+ {
783+ return ;
784+ }
785+
786+ byte [ ] ? fileBytes = item . OnlineApiEndpointGroup switch
787+ {
788+ OnlineApiEndpointGroup . Objects => await EditorContext . ObjectServiceClient . GetObjectFileAsync ( item . Id . Value ) ,
789+ OnlineApiEndpointGroup . Scenarios => await EditorContext . ObjectServiceClient . GetScenarioFileAsync ( item . Id . Value ) ,
790+ _ => null ,
791+ } ;
792+
793+ if ( fileBytes == null || fileBytes . Length == 0 )
794+ {
795+ EditorContext . Logger . Error ( $ "Failed to download \" { item . DisplayName } \" (Id={ item . Id } )") ;
796+ return ;
797+ }
798+
799+ var extension = item . OnlineApiEndpointGroup == OnlineApiEndpointGroup . Scenarios ? ".SC5" : ".dat" ;
800+ var safeName = Path . GetInvalidFileNameChars ( ) . Aggregate ( item . DisplayName , ( current , c ) => current . Replace ( c , '_' ) ) ;
801+ var filename = Path . Combine ( EditorContext . Settings . DownloadFolder , $ "{ safeName } -{ item . Id } { extension } ") ;
802+ await File . WriteAllBytesAsync ( filename , fileBytes ) ;
803+ EditorContext . Logger . Info ( $ "Downloaded \" { item . DisplayName } \" to \" { filename } \" ") ;
804+ }
805+
806+ async Task DownloadOnlinePackAsync ( OnlineItemPackBrowseResult pack )
807+ {
808+ if ( EditorContext . ObjectServiceClient == null )
809+ {
810+ return ;
811+ }
812+
813+ byte [ ] ? fileBytes = pack . Group switch
814+ {
815+ OnlineApiEndpointGroup . ObjectPacks => await EditorContext . ObjectServiceClient . GetObjectPackFileAsync ( pack . Id ) ,
816+ OnlineApiEndpointGroup . SC5FilePacks => await EditorContext . ObjectServiceClient . GetSC5FilePackFileAsync ( pack . Id ) ,
817+ _ => null ,
818+ } ;
819+
820+ if ( fileBytes == null || fileBytes . Length == 0 )
821+ {
822+ EditorContext . Logger . Error ( $ "Failed to download pack \" { pack . Name } \" (Id={ pack . Id } )") ;
823+ return ;
824+ }
825+
826+ var safePackName = Path . GetInvalidFileNameChars ( ) . Aggregate ( pack . Name , ( current , c ) => current . Replace ( c , '_' ) ) ;
827+ var filename = Path . Combine ( EditorContext . Settings . DownloadFolder , $ "{ safePackName } .zip") ;
828+ await File . WriteAllBytesAsync ( filename , fileBytes ) ;
829+ EditorContext . Logger . Info ( $ "Downloaded pack \" { pack . Name } \" to \" { filename } \" ") ;
830+ }
774831}
0 commit comments