-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathAssetStoreContext.cs
More file actions
452 lines (381 loc) · 15.2 KB
/
AssetStoreContext.cs
File metadata and controls
452 lines (381 loc) · 15.2 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using UnityEditor.Web;
using UnityEditor.Analytics;
namespace UnityEditor
{
/// <summary>
/// The interface of this class is mirrored into the browser's JavaScript world as
/// "window.context".
/// </summary>
/// <remarks>
/// The WebView will expose the methods of this class as functions on the JavaScript
/// object created by AssetStoreWindow.SetContextObject(). Return values and arguments
/// of these methods can be any primitive type, arrays of any other valid type, and
/// classes with public fields of any other valid type. In the last case, objects of
/// these classes will be translate to and from JavaScript objects with C# fields mapping
/// to JavaScript properties.
/// </remarks>
[InitializeOnLoad]
internal partial class AssetStoreContext
{
static AssetStoreContext()
{
AssetStoreContext.GetInstance();
}
public static AssetStoreContext GetInstance()
{
if (s_Instance == null)
{
s_Instance = new AssetStoreContext();
JSProxyMgr.GetInstance().AddGlobalObject("AssetStoreContext", s_Instance);
}
return s_Instance;
}
public string GetInitialOpenURL()
{
if (initialOpenURL != null)
{
string tmp = initialOpenURL;
initialOpenURL = null;
return tmp;
}
else
{
return "";
}
}
public string GetAuthToken()
{
return UnityEditorInternal.InternalEditorUtility.GetAuthToken();
}
public int[] GetLicenseFlags()
{
return UnityEditorInternal.InternalEditorUtility.GetLicenseFlags();
}
public string GetString(string key)
{
return EditorPrefs.GetString(key);
}
public int GetInt(string key)
{
return EditorPrefs.GetInt(key);
}
public float GetFloat(string key)
{
return EditorPrefs.GetFloat(key);
}
public void SetString(string key, string value)
{
EditorPrefs.SetString(key, value);
}
public void SetInt(string key, int value)
{
EditorPrefs.SetInt(key, value);
}
public void SetFloat(string key, float value)
{
EditorPrefs.SetFloat(key, value);
}
public bool HasKey(string key)
{
return EditorPrefs.HasKey(key);
}
public void DeleteKey(string key)
{
EditorPrefs.DeleteKey(key);
}
public int GetSkinIndex()
{
return EditorGUIUtility.skinIndex;
}
public bool GetDockedStatus()
{
return docked;
}
public bool OpenPackage(string id)
{
return OpenPackage(id, "default");
}
public bool OpenPackage(string id, string action)
{
return OpenPackageInternal(id);
}
public static bool OpenPackageInternal(string id)
{
Match match = s_GeneratedIDRegExp.Match(id);
if (match.Success && File.Exists(match.Groups[1].Value)) // If id looks like a path name, just try to open that
{
AssetDatabase.ImportPackage(match.Groups[1].Value, true);
return true;
}
else
{
foreach (PackageInfo package in PackageInfo.GetPackageList())
{
if (package.jsonInfo != "")
{
JSONValue item = JSONParser.SimpleParse(package.jsonInfo);
string itemID = item.Get("id").IsNull() ? null : item["id"].AsString(true);
if (itemID != null && itemID == id && File.Exists(package.packagePath))
{
AssetDatabase.ImportPackage(package.packagePath, true);
return true;
}
}
}
}
Debug.LogError("Unknown package ID " + id);
return false;
}
public void OpenBrowser(string url)
{
Application.OpenURL(url);
}
[Serializable]
public struct DownloadAssetInfo
{
public string package_id;
public string package_name;
public string publisher_name;
public string category_name;
}
public void Download(Package package, DownloadInfo downloadInfo)
{
Download(
downloadInfo.id,
downloadInfo.url,
downloadInfo.key,
package.title,
package.publisher.label,
package.category.label,
null
);
}
public static void Download(string package_id, string url, string key, string package_name,
string publisher_name, string category_name, AssetStoreUtils.DownloadDoneCallback doneCallback)
{
string[] dest = PackageStorePath(publisher_name, category_name,
package_name, package_id, url);
JSONValue existing = JSONParser.SimpleParse(AssetStoreUtils.CheckDownload(package_id, url, dest, key));
// If the package is actively being downloaded right now just return
if (existing.Get("in_progress").AsBool(true))
{
Debug.Log("Will not download " + package_name + ". Download is already in progress.");
return;
}
// The package is not being downloaded.
// If the package has previously been partially downloaded then
// resume that download.
string existingUrl = existing.Get("download.url").AsString(true);
string existingKey = existing.Get("download.key").AsString(true);
bool resumeOK = (existingUrl == url && existingKey == key);
JSONValue download = new JSONValue();
download["url"] = url;
download["key"] = key;
JSONValue parameters = new JSONValue();
parameters["download"] = download;
AssetStoreUtils.Download(package_id, url, dest, key, parameters.ToString(), resumeOK, doneCallback);
EditorAnalytics.SendAssetDownloadEvent(new DownloadAssetInfo()
{
package_id = package_id,
package_name = package_name,
publisher_name = publisher_name,
category_name = category_name
});
}
/// <summary>
/// Create an array consisting of publisherName, categoryName and packageName
/// This is to be used by AssetStoreUtils.*Download functions
/// </summary>
public static string[] PackageStorePath(string publisher_name,
string category_name,
string package_name,
string package_id,
string url)
{
string[] dest = { publisher_name, category_name, package_name };
for (int i = 0; i < 3; i++)
dest[i] = s_InvalidPathCharsRegExp.Replace(dest[i], "");
// If package name cannot be stored as a valid file name, use the package id
if (dest[2] == "")
dest[2] = s_InvalidPathCharsRegExp.Replace(package_id, "");
// If still no valid chars use a mangled url as the file name
if (dest[2] == "")
dest[2] = s_InvalidPathCharsRegExp.Replace(url, "");
return dest;
}
public PackageList GetPackageList()
{
var packages = new Dictionary<string, Package>();
var packageInfos = PackageInfo.GetPackageList();
foreach (PackageInfo info in packageInfos)
{
Package package = new Package();
if (info.jsonInfo == "")
{
package.title = System.IO.Path.GetFileNameWithoutExtension(info.packagePath);
package.id = info.packagePath;
if (IsBuiltinStandardAsset(info.packagePath))
{
package.publisher = new LabelAndId { label = "Unity Technologies", id = "1" };
package.category = new LabelAndId { label = "Prefab Packages", id = "4" };
package.version = "3.5.0.0";
}
}
else
{
var jsonData = JSONParser.SimpleParse(info.jsonInfo);
if (jsonData.IsNull())
continue;
package.Initialize(jsonData);
if (package.id == null)
{
var linkId = jsonData.Get("link.id");
if (!linkId.IsNull())
package.id = linkId.AsString();
else
package.id = info.packagePath;
}
}
package.local_icon = info.iconURL;
package.local_path = info.packagePath;
// If no package with the same ID is in the dictionary yet or if the current package
// is newer than what we currently have in the dictionary, add the package to the
// dictionary.
if (!packages.ContainsKey(package.id) ||
packages[package.id].version_id == null ||
packages[package.id].version_id == "-1" ||
(package.version_id != null && package.version_id != "-1" && Int32.Parse(packages[package.id].version_id) <= Int32.Parse(package.version_id)))
{
packages[package.id] = package;
}
}
var results = packages.Values.ToArray();
return new PackageList { results = results };
}
private bool IsBuiltinStandardAsset(string path)
{
return s_StandardPackageRegExp.IsMatch(path);
}
private static Regex s_StandardPackageRegExp = new Regex(@"/Standard Packages/(Character\ Controller|Glass\ Refraction\ \(Pro\ Only\)|Image\ Effects\ \(Pro\ Only\)|Light\ Cookies|Light\ Flares|Particles|Physic\ Materials|Projectors|Scripts|Standard\ Assets\ \(Mobile\)|Skyboxes|Terrain\ Assets|Toon\ Shading|Tree\ Creator|Water\ \(Basic\)|Water\ \(Pro\ Only\))\.unitypackage$", RegexOptions.IgnoreCase);
private static Regex s_GeneratedIDRegExp = new Regex(@"^\{(.*)\}$");
private static Regex s_InvalidPathCharsRegExp = new Regex(@"[^a-zA-Z0-9() _-]");
internal bool docked;
internal string initialOpenURL;
private static AssetStoreContext s_Instance;
// Some data is created through reflection in C++ and then
// passed on to us. Shut up warning about fields that are
// never assigned to.
#pragma warning disable 0649
// The following classes are used for data interchange with JavaScript.
// Public fields in them directly translate to respective properties on
// JavaScript objects.
public class DownloadInfo
{
public string url;
public string key;
public string id;
}
public class LabelAndId
{
public string label;
public string id;
public void Initialize(JSONValue json)
{
if (json.ContainsKey("label"))
label = json["label"].AsString();
if (json.ContainsKey("id"))
id = json["id"].AsString();
}
public override string ToString()
{
return string.Format("{{label={0}, id={1}}}", label, id);
}
}
public class Link
{
public string type;
public string id;
public void Initialize(JSONValue json)
{
if (json.ContainsKey("type"))
type = json["type"].AsString();
if (json.ContainsKey("id"))
id = json["id"].AsString();
}
public override string ToString()
{
return string.Format("{{type={0}, id={1}}}", type, id);
}
}
public class Package
{
public string title;
public string id;
public string version;
public string version_id;
public string local_icon;
public string local_path;
public string pubdate;
public string description;
public LabelAndId publisher;
public LabelAndId category;
public Link link;
public void Initialize(JSONValue json)
{
if (json.ContainsKey("title"))
title = json["title"].AsString();
if (json.ContainsKey("id"))
id = json["id"].AsString();
if (json.ContainsKey("version"))
version = json["version"].AsString();
if (json.ContainsKey("version_id"))
version_id = json["version_id"].AsString();
if (json.ContainsKey("local_icon"))
local_icon = json["local_icon"].AsString();
if (json.ContainsKey("local_path"))
local_path = json["local_path"].AsString();
if (json.ContainsKey("pubdate"))
pubdate = json["pubdate"].AsString();
if (json.ContainsKey("description"))
description = json["description"].AsString();
if (json.ContainsKey("publisher"))
{
publisher = new LabelAndId();
publisher.Initialize(json["publisher"]);
}
if (json.ContainsKey("category"))
{
category = new LabelAndId();
category.Initialize(json["category"]);
}
if (json.ContainsKey("link"))
{
link = new Link();
link.Initialize(json["link"]);
}
}
public override string ToString()
{
return string.Format
("{{title={0}, id={1}, publisher={2}, category={3}, pubdate={8}, version={4}, version_id={5}, description={9}, link={10}, local_icon={6}, local_path={7}}}",
title, id, publisher, category, version, version_id, local_icon, local_path, pubdate, description, link);
}
}
public class PackageList
{
public Package[] results;
}
}
}