1- use std:: path:: Path ;
1+ use std:: { path:: Path , sync :: Arc } ;
22
3- use bridge:: { import:: { ImportFromOtherLauncher , ImportFromOtherLaunchers , OtherLauncher } , modal_action:: ModalAction } ;
3+ use bridge:: { import:: { ImportFromOtherLauncherJob , OtherLauncher } , modal_action:: ModalAction } ;
44use schema:: instance:: InstanceConfiguration ;
55use crate :: { BackendState , launcher_import:: {
66 modrinth:: { import_instances_from_modrinth, read_profiles_from_modrinth_db} ,
@@ -13,39 +13,56 @@ mod multimc;
1313mod modrinth;
1414mod atlauncher;
1515
16- pub fn discover_instances_from_other_launchers ( ) -> ImportFromOtherLaunchers {
17- let mut imports = ImportFromOtherLaunchers :: default ( ) ;
18-
19- let Some ( base_dirs) = directories:: BaseDirs :: new ( ) else {
20- return imports;
21- } ;
22- let data_dir = base_dirs. data_dir ( ) ;
23-
24- let prism_instances = data_dir. join ( "PrismLauncher" ) . join ( "instances" ) ;
25- imports. imports [ OtherLauncher :: Prism ] = from_subfolders ( & prism_instances, & |path| {
26- path. join ( "instance.cfg" ) . exists ( ) && path. join ( "mmc-pack.json" ) . exists ( )
27- } ) ;
28-
29- let multimc_instances = data_dir. join ( "multimc" ) . join ( "instances" ) ;
30- imports. imports [ OtherLauncher :: MultiMC ] = from_subfolders ( & multimc_instances, & |path| {
31- path. join ( "instance.cfg" ) . exists ( ) && path. join ( "mmc-pack.json" ) . exists ( )
32- } ) ;
33-
34- if let Ok ( import) = read_profiles_from_modrinth_db ( data_dir) {
35- imports. imports [ OtherLauncher :: Modrinth ] = import;
16+ pub fn get_import_from_other_launcher_job ( other_launcher : OtherLauncher , path : Arc < Path > ) -> Option < ImportFromOtherLauncherJob > {
17+ if !path. is_dir ( ) {
18+ return None ;
3619 }
20+ match other_launcher {
21+ OtherLauncher :: Prism | OtherLauncher :: MultiMC => {
22+ if !path. join ( "prismlauncher.cfg" ) . is_file ( ) && !path. join ( "multimc.cfg" ) . is_file ( ) {
23+ return None ;
24+ }
25+ Some ( ImportFromOtherLauncherJob {
26+ import_accounts : path. join ( "accounts.json" ) . is_file ( ) ,
27+ paths : collect_subfolders_matching ( & path. join ( "instances" ) , & |path| {
28+ path. join ( "instance.cfg" ) . exists ( ) && path. join ( "mmc-pack.json" ) . exists ( )
29+ } ) ,
30+ root : path,
31+ } )
32+ } ,
33+ OtherLauncher :: Modrinth => {
34+ let paths = match read_profiles_from_modrinth_db ( & path) {
35+ Ok ( paths) => paths?,
36+ Err ( err) => {
37+ log:: error!( "Unable to read modrinth profile database: {err}" ) ;
38+ return None ;
39+ } ,
40+ } ;
3741
38- let atlauncher_instances = data_dir. join ( "atlauncher" ) . join ( "instances" ) ;
39- imports. imports [ OtherLauncher :: ATLauncher ] = from_subfolders ( & atlauncher_instances, & |path| {
40- path. join ( "instance.json" ) . exists ( )
41- } ) ;
42-
43- imports
42+ Some ( ImportFromOtherLauncherJob {
43+ import_accounts : false ,
44+ paths,
45+ root : path,
46+ } )
47+ } ,
48+ OtherLauncher :: ATLauncher => {
49+ if !path. join ( "configs/ATLauncher.json" ) . is_file ( ) {
50+ return None ;
51+ }
52+ Some ( ImportFromOtherLauncherJob {
53+ import_accounts : path. join ( "configs/accounts.json" ) . is_file ( ) ,
54+ paths : collect_subfolders_matching ( & path. join ( "instances" ) , & |path| {
55+ path. join ( "instance.json" ) . exists ( )
56+ } ) ,
57+ root : path,
58+ } )
59+ } ,
60+ }
4461}
4562
46- fn from_subfolders ( folder : & Path , check : & dyn Fn ( & Path ) -> bool ) -> Option < ImportFromOtherLauncher > {
63+ fn collect_subfolders_matching ( folder : & Path , check : & dyn Fn ( & Path ) -> bool ) -> Vec < Arc < Path > > {
4764 let Ok ( read_dir) = std:: fs:: read_dir ( folder) else {
48- return None ;
65+ return Vec :: new ( ) ;
4966 } ;
5067 let mut paths = Vec :: new ( ) ;
5168 for entry in read_dir {
@@ -59,12 +76,9 @@ fn from_subfolders(folder: &Path, check: &dyn Fn(&Path) -> bool) -> Option<Impor
5976 if !( check) ( & path) {
6077 continue ;
6178 }
62- paths. push ( path) ;
79+ paths. push ( path. into ( ) ) ;
6380 }
64- Some ( ImportFromOtherLauncher {
65- can_import_accounts : true ,
66- paths,
67- } )
81+ paths
6882}
6983
7084pub fn try_load_from_other_launcher_formats ( folder : & Path ) -> Option < InstanceConfiguration > {
@@ -77,33 +91,20 @@ pub fn try_load_from_other_launcher_formats(folder: &Path) -> Option<InstanceCon
7791 None
7892}
7993
80- pub async fn import_from_other_launcher ( backend : & BackendState , launcher : OtherLauncher , import_accounts : bool , import_instances : bool , modal_action : ModalAction ) {
81- let Some ( base_dirs) = directories:: BaseDirs :: new ( ) else {
82- return ;
83- } ;
84- let data_dir = base_dirs. data_dir ( ) ;
94+ pub async fn import_from_other_launcher ( backend : & BackendState , launcher : OtherLauncher , import_job : ImportFromOtherLauncherJob , modal_action : ModalAction ) {
8595
8696 match launcher {
87- OtherLauncher :: Prism => {
88- let prism = data_dir. join ( "PrismLauncher" ) ;
89- import_from_multimc ( backend, & prism, import_accounts, import_instances, modal_action) . await ;
97+ OtherLauncher :: Prism | OtherLauncher :: MultiMC => {
98+ import_from_multimc ( backend, import_job, modal_action) . await ;
9099 } ,
91100 OtherLauncher :: Modrinth => {
92- if import_instances {
93- let modrinth = data_dir. join ( "ModrinthApp" ) ;
94- if let Err ( err) = import_instances_from_modrinth ( backend, & modrinth, & modal_action) {
95- log:: error!( "Sqlite error while importing from modrinth: {err}" ) ;
96- modal_action. set_error_message ( "Sqlite error while importing from modrinth, see logs for more info" . into ( ) ) ;
97- }
101+ if let Err ( err) = import_instances_from_modrinth ( backend, import_job, & modal_action) {
102+ log:: error!( "Sqlite error while importing from modrinth: {err}" ) ;
103+ modal_action. set_error_message ( "Sqlite error while importing from modrinth, see logs for more info" . into ( ) ) ;
98104 }
99105 } ,
100- OtherLauncher :: MultiMC => {
101- let multimc = data_dir. join ( "multimc" ) ;
102- import_from_multimc ( backend, & multimc, import_accounts, import_instances, modal_action) . await ;
103- } ,
104106 OtherLauncher :: ATLauncher => {
105- let atlauncher = data_dir. join ( "atlauncher" ) ;
106- import_from_atlauncher ( backend, & atlauncher, import_accounts, import_instances, modal_action) . await ;
107+ import_from_atlauncher ( backend, import_job, modal_action) . await ;
107108 }
108109 }
109110}
0 commit comments