コード例 #1
0
        public void Load(byte[] bytes, Action done)
        {
            if (m_isBusy)
            {
                throw new InvalidOperationException("m_isBusy");
            }

            m_isBusy = true;
            m_progressIndicator.IsVisible = true;
            m_progressIndicator.SetText("LOADING...");

            for (int i = 0; i < m_mapCameras.Count; ++i)
            {
                m_mapCameras[i].IsOn = false;
            }

            m_job.Submit(
                () =>
            {
                ProtobufSerializer serializer = null;
                try
                {
                    var pool = Dependencies.Serializer;
                    if (pool != null)
                    {
                        serializer = pool.Acquire();
                        m_map      = serializer.Deserialize <MapRoot>(bytes);
                    }
                    CalculateBounds();

                    //how to make sure than no one accessing cameras during background thread job ?
                    for (int i = 0; i < m_mapCameras.Count; ++i)
                    {
                        m_mapCameras[i].Map = m_map;
                    }
                }
                catch (Exception e)
                {
                    return(e);
                }
                finally
                {
                    if (serializer != null)
                    {
                        var pool = Dependencies.Serializer;
                        if (pool != null)
                        {
                            pool.Release(serializer);
                        }
                    }
                }

                return(null);
            },
                result =>
            {
                if (result is Exception)
                {
                    Debug.LogError(result.ToString());
                }

                m_isBusy = false;
                m_progressIndicator.IsVisible = false;

                if (Loaded != null)
                {
                    Loaded(this, EventArgs.Empty);
                }

                if (done != null)
                {
                    done();
                }

                for (int i = 0; i < m_mapCameras.Count; ++i)
                {
                    m_mapCameras[i].IsOn = m_isOn;
                }
            });
        }
コード例 #2
0
        private void DownloadMapDataById(Guid mapId, ServerEventHandler <MapData> callback)
        {
            MapData mapData = null;
            Error   error   = new Error();

            string dataPath = m_persistentDataPath + "/Maps/";
            string filePath = dataPath + mapId + ".data";

            if (!File.Exists(filePath))
            {
                error.Code = StatusCode.NotFound;

                if (m_lag == 0)
                {
                    callback(error, mapData);
                }
                else
                {
                    m_job.Submit(() => { Thread.Sleep(m_lag); return(null); }, result => callback(error, mapData));
                }
            }
            else
            {
                m_job.Submit(() =>
                {
                    error.Code = StatusCode.OK;
                    ProtobufSerializer serializer = null;
                    try
                    {
                        var pool = Dependencies.Serializer;
                        if (pool != null)
                        {
                            serializer          = pool.Acquire();
                            byte[] mapDataBytes = File.ReadAllBytes(filePath);
                            mapData             = serializer.Deserialize <MapData>(mapDataBytes);
                        }
                    }
                    catch (Exception e)
                    {
                        error.Code    = StatusCode.UnhandledException;
                        error.Message = e.Message;
                    }
                    finally
                    {
                        if (serializer != null)
                        {
                            var pool = Dependencies.Serializer;
                            if (pool != null)
                            {
                                pool.Release(serializer);
                            }
                        }
                    }

                    return(null);
                },
                             result =>
                {
                    callback(error, mapData);
                });
            }
        }