Пример #1
0
            internal static IEnumerable <SearchItem> SearchItems(SearchContext context, SearchProvider provider)
            {
                var webRequest = UnityWebRequest.Get(url + context.searchQuery);

                webRequest.SetRequestHeader("X-Unity-Session", InternalEditorUtility.GetAuthToken());
                webRequest.SendWebRequest(); // Don't yield return this, as it is not a coroutine and will block the UI

                while (!webRequest.isDone)
                {
                    if (webRequest.isHttpError || webRequest.isNetworkError)
                    {
                        yield break;
                    }
                    yield return(null);
                }

                if (string.IsNullOrEmpty(webRequest.downloadHandler.text))
                {
                    yield break;
                }


                var reqJson = Utils.JsonDeserialize(webRequest.downloadHandler.text) as Dictionary <string, object>;

                if (reqJson == null || !reqJson.ContainsKey("status") || reqJson["status"].ToString() != "ok" || !reqJson.ContainsKey("groups"))
                {
                    yield break;
                }

                if (!(reqJson["groups"] is List <object> groups))
                {
                    yield break;
                }

                foreach (var g in groups)
                {
                    var group = g as Dictionary <string, object>;
                    if (group == null || !group.ContainsKey("matches"))
                    {
                        yield return(null);

                        continue;
                    }

                    var matches = group["matches"] as List <object>;
                    if (matches == null)
                    {
                        yield return(null);

                        continue;
                    }

                    foreach (var m in matches.Take(50))
                    {
                        var match = m as Dictionary <string, object>;
                        if (match == null)
                        {
                            yield return(null);

                            continue;
                        }
                        match["groupType"] = group["name"];

                        if (!match.ContainsKey("name") || !match.ContainsKey("id") || !match.ContainsKey("package_id"))
                        {
                            yield return(null);

                            continue;
                        }
                        var id   = match["id"].ToString();
                        var data = new AssetStoreData {
                            packageId = Convert.ToInt32(match["package_id"]), id = Convert.ToInt32(match["id"])
                        };
                        var item = provider.CreateItem(id, match["name"].ToString(), $"Asset Store ({match["groupType"]})", null, data);

                        if (match.ContainsKey("static_preview_url") && !s_CurrentSearchAssetData.ContainsKey(id))
                        {
                            s_CurrentSearchAssetData.Add(id, new AssetPreviewData()
                            {
                                staticPreviewUrl = match["static_preview_url"].ToString()
                            });
                        }

                        yield return(item);
                    }
                }
            }
Пример #2
0
            internal static void SearchCompleted(AsyncOperation op)
            {
                if (s_CurrentSearchRequest.isDone && !s_CurrentSearchRequest.isHttpError && !s_CurrentSearchRequest.isNetworkError && !string.IsNullOrEmpty(s_CurrentSearchRequest.downloadHandler.text))
                {
                    try
                    {
                        var reqJson = Utils.JsonDeserialize(s_CurrentSearchRequest.downloadHandler.text) as Dictionary <string, object>;
                        if (!reqJson.ContainsKey("status") || reqJson["status"].ToString() != "ok" || !reqJson.ContainsKey("groups"))
                        {
                            return;
                        }

                        if (!(reqJson["groups"] is List <object> groups))
                        {
                            return;
                        }

                        var limitedMatches = new List <Dictionary <string, object> >();
                        foreach (var g in groups)
                        {
                            var group = g as Dictionary <string, object>;
                            if (group == null || !group.ContainsKey("matches"))
                            {
                                continue;
                            }

                            var matches = group["matches"] as List <object>;
                            if (matches == null)
                            {
                                continue;
                            }

                            foreach (var m in matches.Take(50))
                            {
                                var item = m as Dictionary <string, object>;
                                if (item == null)
                                {
                                    continue;
                                }
                                item["groupType"] = group["name"];
                                limitedMatches.Add(item);
                            }
                        }

                        var items = new List <SearchItem>();
                        foreach (var match in limitedMatches)
                        {
                            if (!match.ContainsKey("name") || !match.ContainsKey("id") || !match.ContainsKey("package_id"))
                            {
                                continue;
                            }
                            var id   = match["id"].ToString();
                            var item = s_Provider.CreateItem(id, match["name"].ToString(), $"Asset Store ({match["groupType"]})");
                            var data = new AssetStoreData();
                            data.packageId = Convert.ToInt32(match["package_id"]);
                            data.id        = Convert.ToInt32(match["id"]);
                            item.data      = data;

                            items.Add(item);
                            if (match.ContainsKey("static_preview_url") && !s_CurrentSearchAssetData.ContainsKey(id))
                            {
                                s_CurrentSearchAssetData.Add(id, new AssetPreviewData()
                                {
                                    staticPreviewUrl = match["static_preview_url"].ToString()
                                });
                            }
                        }

                        SearchService.SortItemList(items);
                        s_AddAsyncItems(s_SearchId, items.ToArray());
                    }
                    catch
                    {
                        // ignored
                    }
                }

                s_CurrentSearchRequestOp.completed -= SearchCompleted;
                s_CurrentSearchRequestOp            = null;
                s_CurrentSearchRequest              = null;
            }