1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! Optifine 画质增强/性能优化模组的下载及安装模块
//!
//! 因 Optifine 并不提供一个稳定的下载方式,故此处会使用镜像源的额外 API 来获取版本下载信息

use async_trait::async_trait;
use inner_future::io::AsyncWriteExt;

use super::{structs::OptifineVersionMeta, Downloader};
use crate::{download::DownloadSource, prelude::*};

const OPTIFINE_INSTALL_HELPER: &[u8] = include_bytes!("../../assets/optifine-installer.jar");

#[cfg(target_os = "windows")]
const CLASS_PATH_SPAREATOR: &str = ";";
#[cfg(target_os = "linux")]
const CLASS_PATH_SPAREATOR: &str = ":";
#[cfg(target_os = "macos")]
const CLASS_PATH_SPAREATOR: &str = ":";

/// 一个用于下载 Optifine 模组下载安装的扩展特质,可以使用 [`crate::download::Downloader`] 来安装
#[async_trait]
pub trait OptifineDownloadExt: Sync {
    /// 根据纯净版本号获取当前可用的所有 Optifine 版本
    async fn get_avaliable_installers(
        &self,
        vanilla_version: &str,
    ) -> DynResult<Vec<OptifineVersionMeta>>;

    /// 下载 Optifine 版本
    async fn download_optifine(
        &self,
        vanilla_version: &str,
        optifine_type: &str,
        optifine_patch: &str,
        dest_path: &str,
    ) -> DynResult;

    /// 安装 Optifine 版本
    async fn install_optifine(
        &self,
        version_name: &str,
        vanilla_version: &str,
        optifine_type: &str,
        optifine_patch: &str,
        as_mod: bool,
    ) -> DynResult;
}

#[async_trait]
impl<R: Reporter> OptifineDownloadExt for Downloader<R> {
    async fn get_avaliable_installers(
        &self,
        vanilla_version: &str,
    ) -> DynResult<Vec<OptifineVersionMeta>> {
        let mut res: Vec<OptifineVersionMeta> = crate::http::retry_get_json(&match self.source {
            DownloadSource::MCBBS => {
                format!("https://download.mcbbs.net/optifine/{vanilla_version}")
            }
            _ => format!("https://bmclapi2.bangbang93.com/optifine/{vanilla_version}"),
        })
        .await
        .map_err(|e| anyhow::anyhow!("获取可用Optifine版本列表失败:{:?}", e))?;
        res.reverse();
        Ok(res)
    }

    async fn download_optifine(
        &self,
        vanilla_version: &str,
        optifine_type: &str,
        optifine_patch: &str,
        dest_path: &str,
    ) -> DynResult {
        let r = self.reporter.sub();
        r.set_message(format!(
            "正在下载 Optifine {vanilla_version} {optifine_patch} {optifine_type}"
        ));
        let uris = [
            match self.source {
                DownloadSource::MCBBS => {
                    format!(
                        "https://download.mcbbs.net/optifine/{vanilla_version}/{optifine_type}/{optifine_patch}"
                    )
                }
                _ => format!(
                    "https://bmclapi2.bangbang93.com/optifine/{vanilla_version}/{optifine_type}/{optifine_patch}"
                ),
            },
            format!(
                "https://download.mcbbs.net/optifine/{vanilla_version}/{optifine_type}/{optifine_patch}"
            ),
            format!(
                "https://bmclapi2.bangbang93.com/optifine/{vanilla_version}/{optifine_type}/{optifine_patch}"
            ),
        ];
        crate::http::download(&uris, dest_path, 0).await?;
        Ok(())
    }

    async fn install_optifine(
        &self,
        version_name: &str,
        vanilla_version: &str,
        optifine_type: &str,
        optifine_patch: &str,
        as_mod: bool,
    ) -> DynResult {
        let r = self.reporter.sub();
        r.set_message(format!("正在给 {version_name} 安装 Optifine"));
        if as_mod {
            let mod_file_name =
                format!("Optifine-{vanilla_version}-{optifine_type}-{optifine_patch}.jar");
            let mod_path = if self.game_independent {
                std::path::Path::new(&self.minecraft_version_path)
                    .join(version_name)
                    .join("mods")
                    .join(mod_file_name)
            } else {
                std::path::Path::new(&self.minecraft_path)
                    .join("mods")
                    .join(mod_file_name)
            };
            inner_future::fs::create_dir_all(mod_path.parent().unwrap()).await?;
            self.download_optifine(
                vanilla_version,
                optifine_type,
                optifine_patch,
                mod_path.to_string_lossy().to_string().as_str(),
            )
            .await?;
        } else {
            // 使用安装器安装

            // 下载 Optifine
            let full_path = format!(
                "{root}/net/optifine/{mc}-{optifine_type}-{optifine_patch}/Optifine-{mc}-{optifine_type}-{optifine_patch}.jar",
                root = self.minecraft_library_path.as_str(),
                mc = vanilla_version,
                optifine_type = optifine_type,
                optifine_patch = optifine_patch,
            );
            inner_future::fs::create_dir_all(std::path::Path::new(&full_path).parent().unwrap())
                .await?;
            self.download_optifine(
                vanilla_version,
                optifine_type,
                optifine_patch,
                full_path.as_str(),
            )
            .await?;

            // Install helper
            let installer_path = format!(
                "{}/net/stevexmh/optifine-installer/0.0.0/optifine-installer.jar",
                self.minecraft_library_path.as_str()
            );

            inner_future::fs::create_dir_all(
                std::path::Path::new(&installer_path).parent().unwrap(),
            )
            .await?;
            let mut file = inner_future::fs::OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(&installer_path)
                .await?;
            file.write_all(OPTIFINE_INSTALL_HELPER).await?;
            let _ = file.flush().await;
            let _ = file.sync_all().await;

            #[cfg(not(windows))]
            let mut cmd = inner_future::process::Command::new(&self.java_path);
            #[cfg(windows)]
            let mut cmd = {
                use inner_future::process::windows::CommandExt;
                let mut cmd = inner_future::process::Command::new(&self.java_path);
                cmd.creation_flags(0x08000000);
                cmd
            };

            cmd.arg("-cp");
            cmd.arg(format!(
                "{}{}{}",
                &installer_path, CLASS_PATH_SPAREATOR, &full_path
            ));
            cmd.arg("net.stevexmh.OptifineInstaller");
            cmd.arg(self.minecraft_path.as_str()); // .minecraft
            cmd.arg(version_name); // 版本名称

            if cmd.status().await?.success() {
                return Ok(());
            } else {
                anyhow::bail!("Optifine 安装器执行失败");
            }
        }
        Ok(())
    }
}