Skip to content

Commit f2a893d

Browse files
committed
Make everything work, Finish dialog
1 parent 09da7d4 commit f2a893d

File tree

2 files changed

+63
-18
lines changed

2 files changed

+63
-18
lines changed

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ crate-type = ["cdylib"]
1313
[dependencies]
1414
toml = "0.5.6"
1515
serde = { version = "1", features = ["derive"] }
16-
update-protocol = { path = "../skyline-update/update-protocol" }
1716
skyline = { git = "https://github.com/ultimate-research/skyline-rs.git" }
1817
skyline-web = { path = "../skyline-web" }
19-
skyline_smash = { git = "https://github.com/ultimate-research/skyline-smash.git" }
2018
skyline-update = { path = "../skyline-update/skyline-update" }
2119

2220
[profile.dev]

src/lib.rs

+63-16
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,29 @@ use std::path::{Path, PathBuf};
77

88
use serde::{Deserialize, Serialize};
99

10-
pub use update_protocol::UpdateResponse;
10+
use skyline_update::UpdateResponse;
1111

12-
pub fn get_helios_path() -> PathBuf {
12+
fn get_helios_path() -> PathBuf {
1313
PathBuf::from(format!("sd:/helios/{:016X}", skyline::info::get_program_id()))
1414
}
1515

1616
#[derive(Serialize, Deserialize, Debug, Clone)]
17-
pub struct Config {
17+
struct Config {
1818
pub name: String,
1919
pub version: String,
2020
pub server_ip: IpAddr,
2121
}
2222

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> {
2433
// Get Helios path for current Application
2534
let helios_path = get_helios_path();
2635
println!("[helios] Path to discover: {:?}", helios_path);
@@ -31,7 +40,7 @@ pub fn file_discovery() -> Result<Vec<(UpdateResponse, PathBuf)>, io::Error> {
3140
}
3241

3342
// Iterate through all the DirEntries
34-
fs::read_dir(helios_path)?
43+
fs::read_dir(&helios_path)?
3544
.map(|entry| {
3645
let entry = entry?;
3746

@@ -41,9 +50,12 @@ pub fn file_discovery() -> Result<Vec<(UpdateResponse, PathBuf)>, io::Error> {
4150
}
4251

4352
println!("About to read configuration");
53+
54+
let config_path = helios_path.join(entry.path());
4455

4556
// 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;
4759

4860
println!("Configuration read successfully");
4961

@@ -54,14 +66,15 @@ pub fn file_discovery() -> Result<Vec<(UpdateResponse, PathBuf)>, io::Error> {
5466

5567
println!("Finished asking server");
5668

57-
Ok(update.map(|x| (x, entry.path())))
69+
70+
Ok(update.map(|response| Update { response, config_path, ip, config } ))
5871
})
5972
.filter_map(|x| x.transpose())
6073
.collect()
6174
}
6275

6376

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>{
6578
println!("About to open configuration: {:#?}", path);
6679

6780
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
8598
}
8699
}
87100

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) {
89102
println!("About to update configuration");
90103

91104
// Convert the confix to a string
@@ -97,15 +110,49 @@ pub fn update_config_toml<P: AsRef<Path>>(path: P, config: &Config) {
97110

98111
}
99112

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+
100133
#[skyline::main(name = "helios")]
101134
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;
107140
}
108141

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+
}
111158
}

0 commit comments

Comments
 (0)