Commit 72ddde14 authored by hex's avatar hex

add supre cards download

parent ffb49cf9
fileFormatVersion: 2
guid: e02149331e6358e40a85089823f54be3
timeCreated: 1547774603
licenseType: Free
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
data:
first:
Any:
second:
enabled: 1
settings: {}
data:
first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
data:
first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
......@@ -9,6 +10,9 @@ using UnityEngine;
public class Menu : WindowServantSP
{
// [新增] 用于防止重复点击下载的标志位
private bool isDownloadingSuperPre = false;
//GameObject screen;
public override void initialize()
{
......@@ -23,7 +27,8 @@ public class Menu : WindowServantSP
//UIHelper.registEvent(gameObject, "ai_", onClickAI);
UIHelper.registEvent(gameObject, "exit_", onClickExit);
UIHelper.registEvent(gameObject, "joinQQ_", onClickJoinQQ);
UIHelper.registEvent(gameObject, "download_", onClickDownload);
// 将 onClickDownload 的事件注册指向我们新的实现
UIHelper.registEvent(gameObject, "supreCards_", onClickDownloadSuperPre);
//(new Thread(up)).Start();
}
......@@ -150,39 +155,108 @@ public class Menu : WindowServantSP
#endif
}
void onClickDownload()
/// <summary>
/// 当用户点击下载超先行卡按钮时触发
/// </summary>
void onClickDownloadSuperPre()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN //编译器、Windows
Application.OpenURL("https://github.com/Unicorn369/closeup_mobile/releases/tag/0.1");
#elif UNITY_ANDROID //Android
AndroidJavaObject jo = new AndroidJavaObject("cn.unicorn369.library.API");
if (!File.Exists("updates/closeup_0.1.txt"))
{ //用于检查更新
if (File.Exists("closeup_0.1.zip"))
{ //如果有则直接解压
jo.Call("doExtractZipFile", "closeup_0.1.zip", Program.ANDROID_GAME_PATH);
if (isDownloadingSuperPre)
{
Program.PrintToChat("正在处理中,请稍候...");
return;
}
// 启动包装器协程
Program.I().StartCoroutine(DownloadAndApplySuperPrePackCoroutine());
}
/// <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(
"doDownloadZipFile",
"https://github.com/Unicorn369/closeup_mobile/releases/download/0.1/closeup_0.1.zip"
);
));
// 3. 处理下载结果
if (!downloadCompleted)
{
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
{ //否则下载并解压,锁定目录:ANDROID_GAME_PATH
jo.Call(
"doDownloadZipFile",
"https://github.com/Unicorn369/closeup_mobile/releases/download/0.1/closeup_0.1.zip"
);
catch (Exception e)
{
Program.PrintToChat($"应用更新时发生错误: {e.Message}");
// 如果zip文件还存在,尝试删除
if (File.Exists(zipFilePath))
{
File.Delete(zipFilePath);
}
}
}
else
{
jo.Call("showToast", "已是最新,无需再次下载!");
Program.PrintToChat(InterString.Get("已是最新,无需再次下载!"));
Program.PrintToChat("文件已是最新版本,无需下载。");
}
#endif
isDownloadingSuperPre = false;
}
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