-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeBundleVersionChecker.cs
75 lines (62 loc) · 2.51 KB
/
CodeBundleVersionChecker.cs
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
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Networking;
namespace CodeBundle
{
public class CodeBundleVersionChecker : MonoBehaviour
{
public float CheckIntervalInSeconds = 100;
public int RetryCount;
public CodeBundleState State;
public UnityEvent OnRemoteDetectChanges;
public UnityEvent OnRemoteRequestSingleError;
public UnityEvent OnRemoteRequestRetrySeriesError;
public UnityEvent OnError;
private IEnumerator Start()
{
if ((State = CodeBundleState.CheckAndLoad(State)) == null)
{
OnError.Invoke();
yield break;
}
if (Application.isEditor)
{
yield break;
}
var waiter = (object)new WaitForSeconds(CheckIntervalInSeconds);
var countOfRetries = 0;
while (true)
{
var url = new Uri(new Uri(State.RemoteUrl), "/version.json");
using (var request = UnityWebRequest.Get(url.ToString()))
{
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError(
$"Asset Bundle version request failed. Code:{request.responseCode}, error: {request.error}");
if (countOfRetries >= RetryCount)
{
Debug.LogError("Too much request retries for version check!");
OnRemoteRequestRetrySeriesError.Invoke();
}
OnRemoteRequestSingleError.Invoke();
countOfRetries++;
continue;
}
countOfRetries = 0;
var versionState = JsonUtility.FromJson<AssetBundleVersionState>(request.downloadHandler.text);
if (!versionState.Equals(State.CurrentRemoteState))
{
State.CurrentRemoteState = versionState;
Debug.Log("Found new version");
OnRemoteDetectChanges.Invoke();
}
}
yield return waiter;
}
}
}
}