-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceDownloadTask.cs
More file actions
158 lines (136 loc) · 5.28 KB
/
Copy pathResourceDownloadTask.cs
File metadata and controls
158 lines (136 loc) · 5.28 KB
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
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;
/*
* File : ResourceDownloadTask.cs
* Desc : Addressables resource download task.
* Handles initialization, size checking, and asynchronous downloading.
*/
public class ResourceDownloadTask : ITask
{
private readonly TextMeshProUGUI m_progessText;
private TextMeshProUGUI m_infoText;
private readonly Slider m_Slider;
private UIConfirmBase m_confirm;
public ResourceDownloadTask(TextMeshProUGUI infotext, TextMeshProUGUI progresstext = null, Slider slider = null, UIConfirmBase confirm = null)
{
m_progessText = progresstext;
m_Slider = slider;
m_confirm = confirm;
m_infoText = infotext;
}
public async Awaitable<bool> ExecuteAsync()
{
try
{
m_infoText.text = "리소스 점검 중...";
Debug.Log("1) Starting Addressables initialization");
await Addressables.InitializeAsync().Task;
Debug.Log("2) Checking for catalog updates");
// Check if there are new catalogs (changes) on the server.
var checkHandle = Addressables.CheckForCatalogUpdates(false);
var catalogs = await checkHandle.Task;
if (catalogs != null && catalogs.Count > 0)
{
Debug.Log("3) Updating catalogs...");
var updateHandle = Addressables.UpdateCatalogs(catalogs, false);
await updateHandle.Task;
Addressables.Release(updateHandle);
}
Addressables.Release(checkHandle);
// 4) Check resources based on updated information
return await CheckResources();
}
catch (Exception e)
{
Debug.LogError($"Resource process failed: {e.Message}");
return false;
}
}
private async Awaitable<bool> CheckResources()
{
long totalDownloadSize = 0;
List<string> groupsToDownload = new List<string>();
IEnumerable<IResourceLocator> resourceLocators = Addressables.ResourceLocators;
foreach (var resourceLocator in resourceLocators)
{
Debug.Log($"{GetType()}::Resource Locator: {resourceLocator.LocatorId}");
}
foreach (Define.RemoteResouceGroup group in Enum.GetValues(typeof(Define.RemoteResouceGroup)))
{
var handle = Addressables.GetDownloadSizeAsync(group.ToString());
await handle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
Debug.Log($"{GetType()}::Download size for {group}: {handle.Result} bytes");
if (handle.Result > 0)
{
totalDownloadSize += handle.Result;
groupsToDownload.Add(group.ToString());
}
}
else
{
Debug.LogError($"{GetType()}::Failed to retrieve download size for {group}");
}
Addressables.Release(handle);
}
if (totalDownloadSize > 0)
{
Debug.Log($"{GetType()}::Total download size: {totalDownloadSize / 1_000_000f:F0}MB");
var acs = new AwaitableCompletionSource<bool>();
m_confirm.Setup(
title: "새로운 패치 발견",
description: $"새로운 패치 버전이 있습니다.\n" +
$"{totalDownloadSize / 1_000_000f:F1} MB\n" +
$"(Wi-fi 상태 권장)\n" +
$"다운로드 하시겠습니까?",
onShow: null,
onClose: null,
onConfirm: () => acs.SetResult(true),
onCancel: () => acs.SetResult(false));
bool confirmed = await acs.Awaitable;
if (!confirmed)
return false;
return await DownloadResources(groupsToDownload);
}
else
{
Debug.Log($"{GetType()}::No resource download required.");
return true;
}
}
private async Awaitable<bool> DownloadResources(List<string> groups)
{
m_Slider.gameObject.SetActive(true);
foreach (var group in groups)
{
Debug.Log($"{GetType()}::Starting download -> {group}");
var handle = Addressables.DownloadDependenciesAsync(group);
while (!handle.IsDone)
{
float progress = handle.PercentComplete;
m_Slider.value = progress;
m_progessText.text = $"{progress * 100:F0}%";
await Awaitable.NextFrameAsync();
}
if (handle.Status != AsyncOperationStatus.Succeeded)
{
Debug.LogError($"{GetType()}::Download failed -> {group}");
Addressables.Release(handle);
return false;
}
Debug.Log($"{GetType()}::Download completed -> {group}");
Addressables.Release(handle);
}
await Awaitable.WaitForSecondsAsync(2.5f);
m_Slider.gameObject.SetActive(false);
m_progessText.text = null;
return true;
}
}