@@ -7,20 +7,29 @@ use std::path::{Path, PathBuf};
7
7
8
8
use serde:: { Deserialize , Serialize } ;
9
9
10
- pub use update_protocol :: UpdateResponse ;
10
+ use skyline_update :: UpdateResponse ;
11
11
12
- pub fn get_helios_path ( ) -> PathBuf {
12
+ fn get_helios_path ( ) -> PathBuf {
13
13
PathBuf :: from ( format ! ( "sd:/helios/{:016X}" , skyline:: info:: get_program_id( ) ) )
14
14
}
15
15
16
16
#[ derive( Serialize , Deserialize , Debug , Clone ) ]
17
- pub struct Config {
17
+ struct Config {
18
18
pub name : String ,
19
19
pub version : String ,
20
20
pub server_ip : IpAddr ,
21
21
}
22
22
23
- pub fn file_discovery ( ) -> Result < Vec < ( UpdateResponse , PathBuf ) > , io:: Error > {
23
+ struct Update {
24
+ response : UpdateResponse ,
25
+ config_path : PathBuf ,
26
+ ip : IpAddr ,
27
+ config : Config ,
28
+ }
29
+
30
+ type Updates = Vec < Update > ;
31
+
32
+ fn update_discovery ( ) -> Result < Updates , io:: Error > {
24
33
// Get Helios path for current Application
25
34
let helios_path = get_helios_path ( ) ;
26
35
println ! ( "[helios] Path to discover: {:?}" , helios_path) ;
@@ -31,7 +40,7 @@ pub fn file_discovery() -> Result<Vec<(UpdateResponse, PathBuf)>, io::Error> {
31
40
}
32
41
33
42
// Iterate through all the DirEntries
34
- fs:: read_dir ( helios_path) ?
43
+ fs:: read_dir ( & helios_path) ?
35
44
. map ( |entry| {
36
45
let entry = entry?;
37
46
@@ -41,9 +50,12 @@ pub fn file_discovery() -> Result<Vec<(UpdateResponse, PathBuf)>, io::Error> {
41
50
}
42
51
43
52
println ! ( "About to read configuration" ) ;
53
+
54
+ let config_path = helios_path. join ( entry. path ( ) ) ;
44
55
45
56
// Read the configuration
46
- let config: Config = open_config_toml ( & entry. path ( ) ) . unwrap ( ) ;
57
+ let config: Config = open_config_toml ( & config_path) . unwrap ( ) ;
58
+ let ip = config. server_ip ;
47
59
48
60
println ! ( "Configuration read successfully" ) ;
49
61
@@ -54,14 +66,15 @@ pub fn file_discovery() -> Result<Vec<(UpdateResponse, PathBuf)>, io::Error> {
54
66
55
67
println ! ( "Finished asking server" ) ;
56
68
57
- Ok ( update. map ( |x| ( x, entry. path ( ) ) ) )
69
+
70
+ Ok ( update. map ( |response| Update { response, config_path, ip, config } ) )
58
71
} )
59
72
. filter_map ( |x| x. transpose ( ) )
60
73
. collect ( )
61
74
}
62
75
63
76
64
- pub fn open_config_toml < P : AsRef < Path > + std:: fmt:: Debug > ( path : P ) -> Option < Config > {
77
+ fn open_config_toml < P : AsRef < Path > + std:: fmt:: Debug > ( path : P ) -> Option < Config > {
65
78
println ! ( "About to open configuration: {:#?}" , path) ;
66
79
67
80
match fs:: read_to_string ( path) {
@@ -85,7 +98,7 @@ pub fn open_config_toml<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Option<Con
85
98
}
86
99
}
87
100
88
- pub fn update_config_toml < P : AsRef < Path > > ( path : P , config : & Config ) {
101
+ fn update_config_toml < P : AsRef < Path > > ( path : P , config : & Config ) {
89
102
println ! ( "About to update configuration" ) ;
90
103
91
104
// Convert the confix to a string
@@ -97,15 +110,49 @@ pub fn update_config_toml<P: AsRef<Path>>(path: P, config: &Config) {
97
110
98
111
}
99
112
113
+ fn install ( updates : & Updates ) -> Result < ( ) , ( ) > {
114
+ for Update { response, ip, .. } in updates {
115
+ skyline_update:: install_update ( * ip, response) ;
116
+ }
117
+
118
+ Ok ( ( ) )
119
+ }
120
+
121
+ fn update_versions ( updates : & Updates ) -> Result < ( ) , ( ) > {
122
+ for Update { response, config_path, config, .. } in updates {
123
+ let new_config = Config {
124
+ version : response. new_plugin_version . clone ( ) ,
125
+ ..config. clone ( )
126
+ } ;
127
+ update_config_toml ( config_path, & new_config)
128
+ }
129
+
130
+ Ok ( ( ) )
131
+ }
132
+
100
133
#[ skyline:: main( name = "helios" ) ]
101
134
pub fn main ( ) {
102
- match file_discovery ( ) {
103
- Ok ( _ ) => { } ,
104
- Err ( error ) => {
105
- println ! ( "{}" , error )
106
- } ,
135
+ let updates = update_discovery ( ) . unwrap ( ) ;
136
+
137
+ if updates . is_empty ( ) {
138
+ println ! ( "[helios] No updates found" ) ;
139
+ return ;
107
140
}
108
141
109
- // Should probably restart if mods were updated?
110
- //skyline::nn::oe::RestartProgramNoArgs();
142
+ let update_names = updates. iter ( ) . map ( |Update { response, .. } | response. plugin_name . clone ( ) ) ;
143
+
144
+ let lines: Vec < String > = update_names. map ( |name| format ! ( "<li>{}</li>" , name) ) . collect ( ) ;
145
+
146
+ let text = format ! ( "Download the following updates?\n \n <ul style=\" max-height: 250px; overflow: hidden; overflow-y: scroll; text-align: left; display: inline-block;\" >{}</ul>" , lines. join( "\n \n " ) ) ;
147
+
148
+ if skyline_web:: Dialog :: yes_no ( & text) {
149
+ // Install updates
150
+ install ( & updates) . unwrap ( ) ;
151
+
152
+ // Update versions
153
+ update_versions ( & updates) . unwrap ( ) ;
154
+
155
+ // Restart
156
+ skyline:: nn:: oe:: RestartProgramNoArgs ( ) ;
157
+ }
111
158
}
0 commit comments