Commit 72ddde14 authored by hex's avatar hex

add supre cards download

parent ffb49cf9
fileFormatVersion: 2 fileFormatVersion: 2
guid: e02149331e6358e40a85089823f54be3 guid: e02149331e6358e40a85089823f54be3
timeCreated: 1547774603
licenseType: Free
PluginImporter: PluginImporter:
externalObjects: {}
serializedVersion: 2 serializedVersion: 2
iconMap: {} iconMap: {}
executionOrder: {} executionOrder: {}
defineConstraints: []
isPreloaded: 0 isPreloaded: 0
isOverridable: 0 isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData: platformData:
data: - first:
first: Any:
Any: second:
second: enabled: 1
enabled: 1 settings: {}
settings: {} - first:
data: Editor: Editor
first: second:
Editor: Editor enabled: 0
second: settings:
enabled: 0 DefaultValueInitialized: true
settings: - first:
DefaultValueInitialized: true Windows Store Apps: WindowsStoreApps
data: second:
first: enabled: 0
Windows Store Apps: WindowsStoreApps settings:
second: CPU: AnyCPU
enabled: 0
settings:
CPU: AnyCPU
userData: userData:
assetBundleName: assetBundleName:
assetBundleVariant: assetBundleVariant:
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
...@@ -9,6 +10,9 @@ using UnityEngine; ...@@ -9,6 +10,9 @@ using UnityEngine;
public class Menu : WindowServantSP public class Menu : WindowServantSP
{ {
// [新增] 用于防止重复点击下载的标志位
private bool isDownloadingSuperPre = false;
//GameObject screen; //GameObject screen;
public override void initialize() public override void initialize()
{ {
...@@ -23,7 +27,8 @@ public class Menu : WindowServantSP ...@@ -23,7 +27,8 @@ public class Menu : WindowServantSP
//UIHelper.registEvent(gameObject, "ai_", onClickAI); //UIHelper.registEvent(gameObject, "ai_", onClickAI);
UIHelper.registEvent(gameObject, "exit_", onClickExit); UIHelper.registEvent(gameObject, "exit_", onClickExit);
UIHelper.registEvent(gameObject, "joinQQ_", onClickJoinQQ); UIHelper.registEvent(gameObject, "joinQQ_", onClickJoinQQ);
UIHelper.registEvent(gameObject, "download_", onClickDownload); // 将 onClickDownload 的事件注册指向我们新的实现
UIHelper.registEvent(gameObject, "supreCards_", onClickDownloadSuperPre);
//(new Thread(up)).Start(); //(new Thread(up)).Start();
} }
...@@ -150,39 +155,108 @@ public class Menu : WindowServantSP ...@@ -150,39 +155,108 @@ public class Menu : WindowServantSP
#endif #endif
} }
void onClickDownload() /// <summary>
/// 当用户点击下载超先行卡按钮时触发
/// </summary>
void onClickDownloadSuperPre()
{ {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN //编译器、Windows if (isDownloadingSuperPre)
Application.OpenURL("https://github.com/Unicorn369/closeup_mobile/releases/tag/0.1"); {
#elif UNITY_ANDROID //Android Program.PrintToChat("正在处理中,请稍候...");
AndroidJavaObject jo = new AndroidJavaObject("cn.unicorn369.library.API"); return;
if (!File.Exists("updates/closeup_0.1.txt")) }
{ //用于检查更新 // 启动包装器协程
if (File.Exists("closeup_0.1.zip")) Program.I().StartCoroutine(DownloadAndApplySuperPrePackCoroutine());
{ //如果有则直接解压 }
jo.Call("doExtractZipFile", "closeup_0.1.zip", Program.ANDROID_GAME_PATH);
/// <summary>
/// [修改] 这是包装器协程,负责错误捕获和状态管理。
/// </summary>
private IEnumerator DownloadAndApplySuperPrePackCoroutine()
{
isDownloadingSuperPre = true;
// 定义常量和路径
const string downloadUrl = "https://cdn02.moecube.com:444/ygopro-super-pre/archive/ygopro-super-pre.ypk";
string expansionsDir = "expansions";
string ypkFileName = "ygopro-super-pre.ypk";
string zipFileName = "ygopro-super-pre.zip";
string ypkFilePath = Path.Combine(expansionsDir, ypkFileName);
string zipFilePath = Path.Combine(expansionsDir, zipFileName);
// 1. 确保 expansions 目录存在
if (!Directory.Exists(expansionsDir))
{
Directory.CreateDirectory(expansionsDir);
}
Program.PrintToChat("开始检查超先行卡更新...");
// 2. 使用 DownloadFileWithHeadCheck 下载文件
bool downloadCompleted = false;
bool fileExistedBefore = File.Exists(ypkFilePath);
int lastReportedProgress = 0; // 用于控制进度报告的频率
yield return Program.I().StartCoroutine(UnityFileDownloader.DownloadFileWithHeadCheck(
downloadUrl,
ypkFilePath,
(success) =>
{
downloadCompleted = success;
},
(progress) =>
{
int currentProgress = Mathf.FloorToInt(progress * 100);
if (currentProgress >= lastReportedProgress + 10)
{
Program.PrintToChat($"下载进度: {currentProgress}%");
lastReportedProgress = currentProgress; // 更新已报告的进度
}
} }
else if (File.Exists("updates/closeup_0.1.txt")) ));
{ //如果有则下载更新包
jo.Call( // 3. 处理下载结果
"doDownloadZipFile", if (!downloadCompleted)
"https://github.com/Unicorn369/closeup_mobile/releases/download/0.1/closeup_0.1.zip" {
); Program.PrintToChat("超先行卡更新失败,请检查网络连接或稍后再试。");
yield break; // 提前退出协程
}
bool fileExistsAfter = File.Exists(ypkFilePath);
if (!fileExistedBefore && fileExistsAfter)
{
Program.PrintToChat("发现新版本,下载完成,开始解压。");
yield return null;
try
{
// 我们将 .ypk 文件复制为 .zip 来进行解压,而不是重命名它。
File.Copy(ypkFilePath, zipFilePath, true); // `true` 表示如果目标文件已存在则覆盖
// 解压 .zip 文件
byte[] zipData = File.ReadAllBytes(zipFilePath);
Program.I().ExtractZipFile(zipData, expansionsDir);
Program.PrintToChat("解压完成!");
// 清理临时的 .zip 文件并提示用户
File.Delete(zipFilePath);
Program.PrintToChat("超先行卡包已成功应用,请【重启游戏】以使改动完全生效。");
} }
else catch (Exception e)
{ //否则下载并解压,锁定目录:ANDROID_GAME_PATH {
jo.Call( Program.PrintToChat($"应用更新时发生错误: {e.Message}");
"doDownloadZipFile", // 如果zip文件还存在,尝试删除
"https://github.com/Unicorn369/closeup_mobile/releases/download/0.1/closeup_0.1.zip" if (File.Exists(zipFilePath))
); {
File.Delete(zipFilePath);
}
} }
} }
else else
{ {
jo.Call("showToast", "已是最新,无需再次下载!"); Program.PrintToChat("文件已是最新版本,无需下载。");
Program.PrintToChat(InterString.Get("已是最新,无需再次下载!"));
} }
#endif
isDownloadingSuperPre = false;
} }
public static void deleteShell() public static void deleteShell()
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment