Example #1
0
        private bool LoadLayerChunkIndex()
        {
            RBTilemapTMX.TMXMapDef map = null;

            map = layerState.map.internalState.mapDef;

            if (map == null || map.realPathName == null || map.realPathName.Length == 0 || map.layers == null)
            {
                Debug.LogError("Can't load TMX layer, invalid map, or map not loaded yet!");
                layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.BadParam);
                return(false);
            }

            if (!map.infinite)
            {
                Debug.LogError("TMX map is not infinite, use LoadTMXLayer() instead");
                layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.BadParam);
                return(false);
            }

            if (!map.layers.ContainsKey(mTmxSourceLayer))
            {
                Debug.LogError("Layer " + mTmxSourceLayer + " not found");
                layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotFound);
                return(false);
            }

            var tmxLayer      = (RBTilemapTMX.TMXLayerDef)map.layers[mTmxSourceLayer];
            int layerNameHash = mWorkStr.Set(mTmxSourceLayer).ToLowerInvariant().GetHashCode();

            mIndexPath = map.realPathName + "layer_" + layerNameHash.ToString("x") + "_index";
            if (mSource == RB.AssetSource.WWW)
            {
                mIndexPath += ".bytes";
            }

            var cached = map.layerIndexLRU.Get(mIndexPath);

            if (cached != null)
            {
                // Already have the chunk index in cache, no need to do anything else
                mChunkIndex = cached;
                return(true);
            }

            // Not in cache, will have to load async

            // Check if this is a web request
            if (mSource == RB.AssetSource.WWW)
            {
                mWebRequest = UnityWebRequest.Get(mIndexPath);
                if (mWebRequest == null)
                {
                    layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NoResources);
                    return(false);
                }

                if (mWebRequest.SendWebRequest() == null)
                {
                    layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NoResources);
                    return(false);
                }

                layerState.InternalSetErrorStatus(RB.AssetStatus.Loading, RB.Result.Pending);

                return(true);
            }
            else if (mSource == RB.AssetSource.ResourcesAsync)
            {
                mResourceRequest = Resources.LoadAsync <TextAsset>(mIndexPath);

                if (mResourceRequest == null)
                {
                    layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NoResources);

                    return(false);
                }

                layerState.InternalSetErrorStatus(RB.AssetStatus.Loading, RB.Result.Pending);

                return(true);
            }
#if ADDRESSABLES_PACKAGE_AVAILABLE
            else if (mSource == RB.AssetSource.AddressableAssets)
            {
                // Exceptions on LoadAssetAsync can't actually be caught... this might work in the future so leaving it here
                try
                {
                    mAddressableRequest = Addressables.LoadAssetAsync <TextAsset>(mIndexPath);
                }
#pragma warning disable 0414 // Unused warning
                catch (UnityEngine.AddressableAssets.InvalidKeyException e)
                {
                    RBUtil.Unused(e);
                    layerState.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotFound);
                    return(false);
                }
                catch (Exception e)
                {
                    RBUtil.Unused(e);
                    layerState.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined);
                    return(false);
                }
#pragma warning restore 0414

                // Check for an immediate failure
                if (mAddressableRequest.Status == AsyncOperationStatus.Failed)
                {
                    Addressables.Release(mAddressableRequest);
                    layerState.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined);
                    return(false);
                }

                layerState.SetErrorStatus(RB.AssetStatus.Loading, RB.Result.Pending);
                layerState.progress = 0;

                return(true);
            }
#endif
            // This should never happen
            layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined);

            return(false);
        }
        private bool LoadLayerInfo()
        {
            RBTilemapTMX.TMXMapDef map = null;

            map = layerState.map.internalState.mapDef;

            if (map == null || map.realPathName == null || map.realPathName.Length == 0 || map.layers == null)
            {
                Debug.LogError("Can't load TMX layer, invalid map, or map not loaded yet!");
                layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.BadParam);
                return(false);
            }

            if (map.infinite)
            {
                Debug.LogError("TMX map is infinite, use MapLoadTMXLayerChunk() instead");
                layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.BadParam);
                return(false);
            }

            if (!map.layers.ContainsKey(mTmxSourceLayer))
            {
                Debug.LogError("Layer " + mTmxSourceLayer + " not found");
                layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotFound);
                return(false);
            }

            var tmxLayer      = (RBTilemapTMX.TMXLayerDef)map.layers[mTmxSourceLayer];
            var layerNameHash = mWorkStr.Set(mTmxSourceLayer).ToLowerInvariant().GetHashCode().ToString("x");

            mPath = map.realPathName + "layer_" + layerNameHash;

            if (mSource == RB.AssetSource.WWW || mSource == RB.AssetSource.AddressableAssets)
            {
                mPath += ".bytes";
            }

            // Check if this is a web request
            if (mSource == RB.AssetSource.WWW)
            {
                mWebRequest = UnityWebRequest.Get(mPath);
                if (mWebRequest == null)
                {
                    layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NoResources);
                    return(false);
                }

                if (mWebRequest.SendWebRequest() == null)
                {
                    layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NoResources);
                    return(false);
                }

                layerState.InternalSetErrorStatus(RB.AssetStatus.Loading, RB.Result.Pending);

                return(true);
            }
            else if (mSource == RB.AssetSource.ResourcesAsync)
            {
                mResourceRequest = Resources.LoadAsync <TextAsset>(mPath);

                if (mResourceRequest == null)
                {
                    layerState.InternalSetErrorStatus(RB.AssetStatus.Failed, RB.Result.NoResources);

                    return(false);
                }
            }
#if ADDRESSABLES_PACKAGE_AVAILABLE
            else if (mSource == RB.AssetSource.AddressableAssets)
            {
                // Exceptions on LoadAssetAsync can't actually be caught... this might work in the future so leaving it here
                try
                {
                    mAddressableRequest = Addressables.LoadAssetAsync <TextAsset>(mPath);
                }
                catch (UnityEngine.AddressableAssets.InvalidKeyException e)
                {
                    RBUtil.Unused(e);
                    layerState.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.NotFound);
                    return(false);
                }
                catch (Exception e)
                {
                    RBUtil.Unused(e);
                    layerState.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined);
                    return(false);
                }

                // Check for an immediate failure
                if (mAddressableRequest.Status == AsyncOperationStatus.Failed)
                {
                    Addressables.Release(mAddressableRequest);
                    layerState.SetErrorStatus(RB.AssetStatus.Failed, RB.Result.Undefined);
                    return(false);
                }

                layerState.SetErrorStatus(RB.AssetStatus.Loading, RB.Result.Pending);
                layerState.progress = 0;

                return(true);
            }
#endif

            layerState.InternalSetErrorStatus(RB.AssetStatus.Loading, RB.Result.Pending);

            return(true);
        }