-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMainUpdater.cs
158 lines (142 loc) · 5.49 KB
/
MainUpdater.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
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;
using System.IO;
using System.Runtime.Versioning;
using Meow.AssetUpdater.Core;
using LitJson;
using UnityEngine;
namespace Meow.AssetUpdater
{
public class MainUpdater : MonoBehaviour
{
private VersionInfo _remoteVersionInfo;
[SerializeField] private VersionInfo _persistentVersionInfo;
private VersionInfo _streamingVersionInfo;
public string RemoteUrl;
public string ProjectName;
public string VersionFileName;
#if UNITY_EDITOR
private static int _isSimulationMode = -1;
public static bool IsSimulationMode
{
get
{
if (_isSimulationMode == -1)
_isSimulationMode = UnityEditor.EditorPrefs.GetBool("AssetUpdaterSimulationMode", true) ? 1 : 0;
return _isSimulationMode != 0;
}
set
{
int newValue = value ? 1 : 0;
if (newValue != _isSimulationMode)
{
_isSimulationMode = newValue;
UnityEditor.EditorPrefs.SetBool("AssetUpdaterSimulationMode", value);
}
}
}
#endif
private void Awake()
{
RemoteUrl = Settings.RemoteUrl;
ProjectName = Settings.RelativePath;
VersionFileName = Settings.VersionFileName;
}
/// <summary>
/// Initialize global settings of AssetUpdater
/// </summary>
/// <param name="remoteUrl">the assetbundle server url you want to download from</param>
/// <param name="projectName">the relative directory of your assetbundle root path</param>
/// <param name="versionFileName">name of version file, default by "versionfile.bytes"</param>
public void Initialize(string remoteUrl, string projectName, string versionFileName)
{
RemoteUrl = remoteUrl;
ProjectName = projectName;
VersionFileName = versionFileName;
}
/// <summary>
/// download all version files which in remote url, persistentDataPath and streamingAssetPath. perpare for downloading
/// </summary>
/// <returns></returns>
public IEnumerator LoadAllVersionFiles()
{
#if UNITY_EDITOR
if (!IsSimulationMode)
#endif
{
var vFRoot = Path.Combine(ProjectName, Utils.GetBuildPlatform(Application.platform).ToString());
var vFPath = Path.Combine(vFRoot, VersionFileName);
var op = new DownloadOperation(this, SourceType.RemotePath, vFPath);
yield return op;
if (!string.IsNullOrEmpty(op.Error))
{
Debug.LogError("Can not download remote version file, error = " + op.Error);
yield break;
}
_remoteVersionInfo = JsonMapper.ToObject<VersionInfo>(op.Text);
op = new DownloadOperation(this, SourceType.PersistentPath, vFPath);
yield return op;
_persistentVersionInfo = JsonMapper.ToObject<VersionInfo>(op.Text);
if (_persistentVersionInfo == null)
{
_persistentVersionInfo = new VersionInfo();
}
op = new DownloadOperation(this, SourceType.StreamingPath, vFPath);
yield return op;
_streamingVersionInfo = JsonMapper.ToObject<VersionInfo>(op.Text);
if (_streamingVersionInfo == null)
{
_streamingVersionInfo = new VersionInfo();
}
}
}
/// <summary>
/// download files from streamingAssetPath to persistentDataPath, then update versionfile in persistentDataPath
/// </summary>
/// <returns>updateOperation object contains current downloading information</returns>
public UpdateOperation UpdateFromStreamingAsset()
{
return new UpdateOperation(this, _persistentVersionInfo, _streamingVersionInfo, SourceType.StreamingPath);
}
public UpdateOperation UpdateFromRemoteAsset()
{
return new UpdateOperation(this, _persistentVersionInfo, _remoteVersionInfo, SourceType.RemotePath);
}
/// <summary>
/// get assetbundle name by input asset path
/// </summary>
/// <param name="path">asset path</param>
/// <returns>assetbundle name</returns>
public string GetAssetbundlePathByAssetPath(string path)
{
string result = "";
#if UNITY_EDITOR
if (!IsSimulationMode)
#endif
{
if (_persistentVersionInfo.BundlePath.ContainsKey(path.ToLower()))
{
result = _persistentVersionInfo.BundlePath[path.ToLower()];
}
else
{
Debug.LogErrorFormat("Given asset [{0}] path is not exist in any downloaded assetbundles", path);
}
}
return result;
}
public string GetAssetbundleRootPath(bool forWWW)
{
var path = Path.Combine(Path.Combine(Application.persistentDataPath, ProjectName), Utils.GetBuildPlatform().ToString());
if (forWWW)
{
path = "file://" + path;
}
return path;
}
public string GetManifestName()
{
return Utils.GetBuildPlatform().ToString();
}
}
}