Ejemplo n.º 1
0
        public IEnumerator ParseJson(string data)
        {
            job        = new ParseJob();
            job.InData = data;
            job.Start();

            yield return(StartCoroutine(job.WaitFor()));
        }
Ejemplo n.º 2
0
        public IEnumerator LoadPlaces(string url)            //Request the API
        {
            Debug.Log("GO PLACES URL: " + url);

            var www = new WWW(url);

            yield return(www);

            ParseJob job = new ParseJob();

            job.InData = www.text;
            job.Start();

            yield return(StartCoroutine(job.WaitFor()));

            IDictionary response = (IDictionary)job.OutData;

            IList results = (IList)response ["results"];

//			foreach (Transform child in transform) {
//				GameObject.Destroy (child.gameObject);
//			}


            foreach (IDictionary result in results)               //This example only takes GPS location and the id of the object. There's lot more, take a look at the places API documentation

            {
                IDictionary location = (IDictionary)((IDictionary)result ["geometry"])["location"];
                double      lat      = (double)location ["lat"];
                double      lng      = (double)location ["lng"];

                //			GameObject go = GameObject.Instantiate (prefab);
                //			go.name = (string)result["place_id"];
                //			goMap.dropPin (lat, lng, go);

                Coordinates coordinates = new Coordinates(lat, lng, 0);
                GameObject  go          = GameObject.Instantiate(prefab);
                Vector3     pos         = coordinates.convertCoordinateToVector(0);

                                #if GOLINK
                pos.y = GoTerrain.GOTerrain.RaycastAltitudeForVector(pos);
                                #endif

                go.transform.localPosition = pos;

                go.transform.parent = transform;
                go.name             = (string)result["place_id"];
            }
        }
        /// <summary>
        /// force the job to be processed in the main thread for debuging user defined parser.
        /// </summary>
        /// <param name="index"></param>
        public unsafe void LoadFileInMainThread(int file_index)
        {
            var p_state = _state[file_index];

            if (p_state.Target->RefCount == 0)
            {
                var p_tmp = new ParseJob <T>(_alloc, _enableBurst);
                p_tmp.BlockSize = _blockSize;
                p_tmp.ReadFileInMainThread(_pathList[file_index], _encoding, _data[file_index], p_state);
                p_tmp.Dispose();
                p_state.Target->JobState = ReadJobState.Completed;
            }

            p_state.Target->RefCount++;
        }
        private unsafe void Init(string path, Allocator alloc, Encoding encoding)
        {
            Data = new T();
            Data.Init();

            Path       = path;
            _blockSize = Define.DefaultDecodeBlock;
            Encoding   = encoding;

            _parser = new ParseJob <T>(alloc);
            _state  = new PtrHandle <ReadStateImpl>(alloc);
            _state.Target->Clear();

            _allocated = true;
        }
Ejemplo n.º 5
0
        public IEnumerator LoadPlaces(string url)            //Request the API
        {
            Debug.Log("GO4Square URL: " + url);

            var www = new WWW(url);

            yield return(www);

            ParseJob job = new ParseJob();

            job.InData = www.text;
            job.Start();

            yield return(StartCoroutine(job.WaitFor()));

            IDictionary response = (IDictionary)((IDictionary)job.OutData)["response"];
            IList       results  = (IList)response ["venues"];

            foreach (Transform child in transform)
            {
                GameObject.Destroy(child.gameObject);
            }

            foreach (IDictionary result in results)              //This example only takes GPS location and the name of the object. There's lot more, take a look at the Foursquare API documentation

            {
                IDictionary location = ((IDictionary)result ["location"]);
                double      lat      = (double)location ["lat"];
                double      lng      = (double)location ["lng"];


                Coordinates coordinates = new Coordinates(lat, lng, 0);
                GameObject  go          = GameObject.Instantiate(prefab);
                Vector3     pos         = coordinates.convertCoordinateToVector(0);

                if (goMap.useElevation)
                {
                    pos = GOMap.AltitudeToPoint(pos);
                }

                go.transform.localPosition = pos;

                go.transform.parent = transform;
                go.name             = (string)result["name"];
            }
        }
        private void GenerateParser()
        {
            for (int i = 0; i < Define.NumParserLimit; i++)
            {
                _gen++;
                if (_gen >= Define.NumParserLimit)
                {
                    _gen = 0;
                }

                if (!_parserPool.ContainsKey(_gen))
                {
                    var p_tmp = new ParseJob <T>(_alloc, _enableBurst);
                    _parserPool.Add(_gen, p_tmp);
                    _parserAvail.Enqueue(_gen);
                    return;
                }
            }

            throw new InvalidOperationException("Internal error: key '_gen' was spent.");
        }
        /// <summary>
        /// Sends multiple RealmEye requests.
        /// </summary>
        /// <param name="names">The names to get data for.</param>
        /// <returns>The job result.</returns>
        private async Task <ParseJob> SendConcurrentRealmEyeRequestsAsync(string[] names)
        {
            var jobId = _concurrJobId++;

            // To make it clearer that there's a new method being called.
            Console.WriteLine();
            _logger.LogInformation(
                $"[ConcurRealmEyeReq] [ID {jobId}] Started Job.\n"
                + $"\t- Name Count: {names.Length}"
                );

            var stopwatch = Stopwatch.StartNew();

            var defaults = names.Intersect(DefaultNames, StringComparer.OrdinalIgnoreCase).ToList();

            names = names.Except(defaults).ToArray();

            var job = new ParseJob
            {
                TotalElapsedSec    = 0,
                ParseWhoElapsedSec = 0,
                ConcurrElapsedSec  = 0,
                CompletedCount     = 0,
                FailedCount        = 0,
                Output             = new List <PlayerData>(),
                Completed          = new List <string>(),
                Failed             = new List <string>(),
                DefaultNames       = defaults,
                Input = names.ToList()
            };

#if USE_ACTION_BLOCK
            // Essentially sending these requests concurrently with a limit.
            var bag   = new ConcurrentBag <PlayerData>();
            var block = new ActionBlock <string>(
                async name => bag.Add(await PlayerScraper.ScrapePlayerProfileAsync(name)),
                new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = 40
            }
                );

            foreach (var name in names)
            {
                await block.SendAsync(name);
            }

            block.Complete();
            await block.Completion;

            var profiles = bag.ToList();
#else
            var profiles = await Task.WhenAll(names.Select(PlayerScraper.ScrapePlayerProfileAsync));
#endif

            foreach (var profile in profiles)
            {
                if (profile.ResultCode is not ResultCode.Success)
                {
                    job.Failed.Add(profile.Name);
                    continue;
                }

                job.Completed.Add(profile.Name);
                job.Output.Add(profile);
            }

            stopwatch.Stop();
            job.TotalElapsedSec   = stopwatch.Elapsed.TotalSeconds;
            job.ConcurrElapsedSec = stopwatch.Elapsed.TotalSeconds;
            job.CompletedCount    = job.Completed.Count;
            job.FailedCount       = job.Failed.Count;

            _logger.LogInformation(
                $"[ConcurRealmEyeReq] [ID {jobId}] Finished Job.\n"
                + $"\t- Time: {job.TotalElapsedSec} Seconds.\n"
                + $"\t- Completed: {job.CompletedCount}\n"
                + $"\t- Failed: {job.FailedCount} ({string.Join(", ", job.Failed)})"
                );
            return(job);
        }
        public static IEnumerator jsonRequest(MonoBehaviour host, string url, bool useCache, string filename, Action <Dictionary <string, object>, string> response)
        {
            ParseJob job = new ParseJob();

            if (Application.isPlaying)               //Runtime build

            {
                if (useCache && FileHandler.Exist(filename))
                {
                    job.InData = FileHandler.LoadText(filename);
                    job.Start();
                    yield return(host.StartCoroutine(job.WaitFor()));

                    response((Dictionary <string, object>)job.OutData, null);
                }
                else
                {
                    var www = new WWW(url);
                    yield return(www);

                    if (string.IsNullOrEmpty(www.error) && www.bytes.Length > 0)
                    {
                        Debug.Log("[GOUrlRequest]  " + url);
                        if (useCache)
                        {
                            FileHandler.Save(filename, www.bytes);
                        }
                    }
                    else if (www.error != null && (www.error.Contains("429") || www.error.Contains("timed out")))
                    {
                        Debug.LogWarning("[GOUrlRequest] data reload " + www.error);
                        yield return(new WaitForSeconds(1));

                        yield return(host.StartCoroutine(jsonRequest(host, url, useCache, filename, response)));

                        yield break;
                    }
                    else
                    {
                        Debug.LogWarning("[GOUrlRequest] data missing " + www.error + " " + url);
                        response(null, www.error);
                        yield break;
                    }


                    job.InData = www.text;                     //FileHandler.LoadText (filename);
                    job.Start();
                    yield return(host.StartCoroutine(job.WaitFor()));

                    response((Dictionary <string, object>)job.OutData, null);
                }
            }
            else               //Editor build

            {
                if (useCache && FileHandler.Exist(filename))
                {
                    response((Dictionary <string, object>)Json.Deserialize(FileHandler.LoadText(filename)), null);
                }
                else
                {
                        #if UNITY_EDITOR
                    var www = new WWW(url);

                    ContinuationManager.Add(() => www.isDone, () => {
                        if (String.IsNullOrEmpty(www.error) && www.bytes.Length > 0)
                        {
                            Debug.Log("[GOUrlRequest]  " + url);
                            if (useCache)
                            {
                                FileHandler.Save(filename, www.bytes);
                            }
                            response((Dictionary <string, object>)Json.Deserialize(
                                         FileHandler.LoadText(filename)), null);
                        }
                        else if (!String.IsNullOrEmpty(www.error) && (www.error.Contains("429") || www.error.Contains("timed out")))
                        {
                            Debug.LogWarning("[GOUrlRequest] data reload " + www.error);
                            System.Threading.Thread.Sleep(1000);
                            GORoutine.start(jsonRequest(host, url, useCache, filename, response), host);
                        }
                        else
                        {
                            Debug.LogWarning("[GOUrlRequest] Tile data missing " + www.error);
                            response(null, www.error);
                        }
                    });
                        #endif
                    yield break;
                }
            }
            yield return(null);
        }