Example #1
0
        private async Task <Dictionary <AssetItem, object> > BuildAndReloadAssets(IEnumerable <AssetItem> assetsToRebuild)
        {
            var assetList = assetsToRebuild.ToList();

            if (assetList.Count == 0)
            {
                return(null);
            }

            logger?.Debug($"Starting BuildAndReloadAssets for assets {string.Join(", ", assetList.Select(x => x.Location))}");
            var value = Interlocked.Increment(ref loadingAssetCount);

            AssetLoading?.Invoke(this, new ContentLoadEventArgs(value));
            try
            {
                // Rebuild the assets
                await Task.WhenAll(assetList.Select(x => database.Build(x)));

                logger?.Debug("Assets have been built");
                // Unload the previous versions of assets and (re)load the newly build ones.
                var reloadedObjects = await UnloadAndReloadAssets(assetList);

                Game.TriggerActiveRenderStageReevaluation();
                return(reloadedObjects);
            }
            finally
            {
                value = Interlocked.Decrement(ref loadingAssetCount);
                AssetLoaded?.Invoke(this, new ContentLoadEventArgs(value));
                logger?.Debug($"Completed BuildAndReloadAssets for assets {string.Join(", ", assetList.Select(x => x.Location))}");
            }
        }
        public override void Load(AssetLoaded <T> callback = null)
        {
            try
            {
                if (!waiting)
                {
                    Logger.Log(String.Format("Updating WebXMLFileAsset {0} from {1}", typeof(T).Name, url));
                    waiting = true;

                    webclient.DownloadStringCompleted -= handler;
                    handler = (object sender, System.Net.DownloadStringCompletedEventArgs e) =>
                    {
                        if (e.Error != null)
                        {
                            Logger.Log(String.Format("Error retrieving WebXMLFileAsset {0} from {1}: {2}", typeof(T).Name, url, e.Error.Message));
                        }
                        else
                        {
                            try
                            {
                                using (StringReader reader = new StringReader(e.Result))
                                {
                                    XmlSerializer serializer = new XmlSerializer(typeof(T), attr);
                                    T             result     = (T)serializer.Deserialize(reader);
                                    if (result != null)
                                    {
                                        TaskDispatcher.QueueOnMainThread(() =>
                                        {
                                            instance = result;
                                            Logger.Log(String.Format("Successfully updated WebXMLFileAsset {0} from {1}", typeof(T).Name, url));
                                        });
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Log(String.Format("Error retrieving WebXMLFileAsset {0} from {1}: {2}", typeof(T).Name, url, ex.Message));
                            }
                        }

                        TaskDispatcher.QueueOnMainThread(() =>
                        {
                            callback?.Invoke(this);
                            waiting = false;
                        });
                    };
                    webclient.DownloadStringCompleted += handler;
                    webclient.DownloadStringAsync(url);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(String.Format("Error retrieving WebXMLFileAsset {0} from {1}: {2}", typeof(T).Name, url, ex.Message));
            }
        }
        private void View_ScriptNotify(object sender, NotifyEventArgs e)
        {
            var key   = e.Value.Substring(0, 4);
            var value = e.Value.Substring(4);

            switch (key)
            {
            // Loading assets
            case "LOAD":
            {
                AssetLoading?.Invoke(this, EventArgs.Empty);
                break;
            }

            // Assets loaded
            case "LODD":
            {
                _view.Opacity = 1;
                AssetLoaded?.Invoke(this, EventArgs.Empty);
                break;
            }

            // Camera alpha angle in radians
            case "ALPH":
            {
                AlphaInDegrees = GetDegreeFromRadian(double.Parse(value));
                break;
            }

            // Camera beta angle in radians
            case "BETA":
            {
                BetaInDegrees = GetDegreeFromRadian(double.Parse(value));
                break;
            }

            // Camera radius
            case "RADI":
            {
                RadiusPercentage = double.Parse(value);
                break;
            }

            // Animations are supported
            case "ANIM":
            {
                if (_commandGrid == null)
                {
                    return;
                }

                _commandGrid.Visibility = Visibility.Visible;

                var animationNames = value.Split(',');

                if (_animationList != null)
                {
                    _animationList.Items.Clear();

                    foreach (var animName in animationNames)
                    {
                        _animationList.Items.Add(animName);
                    }

                    _animationList.SelectedIndex = 0;

                    _playSymbol.Visibility  = Visibility.Collapsed;
                    _pauseSymbol.Visibility = Visibility.Visible;
                }

                break;
            }

            // No animation
            case "NANM":
            {
                if (_commandGrid != null)
                {
                    _commandGrid.Visibility = Visibility.Collapsed;
                }

                break;
            }

            // Current animation frame
            case "FRAM":
            {
                if (_animationSlider == null || !_sliderLayoutUpdated)
                {
                    return;
                }

                var position = Math.Min(1, Math.Max(0, double.Parse(value)));

                // Making sure we are not overwhelming the system
                _sliderLayoutUpdated       = false;
                _lastValueSetFromAnimation = position * 99.0;
                _animationSlider.Value     = _lastValueSetFromAnimation;
                break;
            }
            }
        }