/// <summary>
        /// Creates an unscaled <see cref="Bitmap"/> of a <see cref="StationaryGrhData"/>.
        /// </summary>
        /// <param name="gd">The <see cref="StationaryGrhData"/>.</param>
        /// <returns>The unscaled <see cref="Bitmap"/>, or null if an error occured.</returns>
        static Bitmap CreateUnscaledBitmap(StationaryGrhData gd)
        {
            Bitmap img;

            // The image was null, so we are going to be the ones to create it
            try
            {
                // Try to create the image
                var tex = gd.GetOriginalTexture();
                if (tex == null)
                {
                    img = null;
                }
                else
                {
                    img = tex.ToBitmap(gd.OriginalSourceRect);
                }
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to create GrhImageList image for `{0}`. Exception: {1}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, gd, ex);
                }
                if (!(ex is LoadingFailedException))
                {
                    Debug.Fail(string.Format(errmsg, gd, ex));
                }
                img = null;
            }

            return(img);
        }
Exemple #2
0
        static string TryGetAbsoluteFilePath(StationaryGrhData gd, ContentPaths contentPath)
        {
            string ret     = null;
            var    isValid = true;

            try
            {
                ret = gd.TextureName.GetAbsoluteFilePath(contentPath);
                if (!File.Exists(ret))
                {
                    isValid = false;
                }
            }
            catch
            {
                isValid = false;
            }

            if (!isValid)
            {
                const string errmsg =
                    "Could not update the size of GrhData `{0}` since the file for the texture named `{1}`" +
                    " could not be found in the ContentPaths.Dev. Expected file: {2}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, gd, gd.TextureName, ret ?? "[NULL]");
                }
                return(null);
            }
            else
            {
                return(ret);
            }
        }
        /// <summary>
        /// Gets the image key for a <see cref="GrhData"/>.
        /// </summary>
        /// <param name="grhData">The <see cref="GrhData"/> to get the image key for.</param>
        /// <returns>The image key for the <paramref name="grhData"/>.</returns>
        protected virtual string GetImageKey(StationaryGrhData grhData)
        {
            if (grhData == null)
            {
                return(string.Empty);
            }

            if (!grhData.GrhIndex.IsInvalid)
            {
                // For normal GrhDatas, we return the unique GrhIndex
                var grhIndex = grhData.GrhIndex;

                if (grhIndex.IsInvalid)
                {
                    return(string.Empty);
                }

                return(grhIndex.ToString());
            }
            else
            {
                // When we have a frame for a GrhData with an invalid GrhIndex, we prefix a "_" and the use the texture name
                var textureName = grhData.TextureName != null?grhData.TextureName.ToString() : null;

                if (string.IsNullOrEmpty(textureName))
                {
                    return(string.Empty);
                }
                else
                {
                    return("_" + textureName);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ThreadPoolAsyncCallbackState"/> class.
 /// </summary>
 /// <param name="grhData">The <see cref="StationaryGrhData"/>.</param>
 /// <param name="callback">The callback to invoke when complete.</param>
 /// <param name="userState">The user state object.</param>
 /// <param name="wait">If true, performs a spin-wait. If false, generates the <see cref="Image"/> on the thread.</param>
 /// <param name="bitmap">The unscaled <see cref="Bitmap"/>. Only needed when <paramref name="wait"/> is false.</param>
 public ThreadPoolAsyncCallbackState(StationaryGrhData grhData, GrhImageListAsyncCallback callback, object userState,
                                     bool wait, Bitmap bitmap)
 {
     _grhData   = grhData;
     _callback  = callback;
     _userState = userState;
     _wait      = wait;
     _bitmap    = bitmap;
 }
Exemple #5
0
        void ShowGrhInfoForStationary(StationaryGrhData grhData)
        {
            // Stationary
            chkAutoSize.Checked     = grhData.AutomaticSize;
            radioStationary.Checked = true;
            radioAnimated.Checked   = false;
            var r = grhData.OriginalSourceRect;

            txtX.Text = r.X.ToString();
            txtY.Text = r.Y.ToString();
            txtW.Text = r.Width.ToString();
            txtH.Text = r.Height.ToString();
            txtTexture.ChangeTextToDefault(grhData.TextureName.ToString(), true);
        }
Exemple #6
0
        /// <summary>
        /// Gets the tool tip text for a <see cref="StationaryGrhData"/>.
        /// </summary>
        /// <param name="grhData">The <see cref="StationaryGrhData"/>.</param>
        /// <returns>The tool tip text for a <see cref="StationaryGrhData"/>.</returns>
        static string GetToolTipTextStationary(StationaryGrhData grhData)
        {
            // Stationary
            var sb = new StringBuilder();

            sb.AppendLine("Grh: " + grhData.GrhIndex);
            sb.AppendLine("Texture: " + grhData.TextureName);

            var sourceRect = grhData.SourceRect;

            sb.AppendLine("Pos: (" + sourceRect.X + "," + sourceRect.Y + ")");
            sb.Append("Size: " + sourceRect.Width + "x" + sourceRect.Height);

            return(sb.ToString());
        }
Exemple #7
0
        /// <summary>
        /// Callback for getting an <see cref="Image"/> asynchronously.
        /// We make this static and pass the <see cref="GrhTreeViewNode"/> as the <paramref name="userState"/> so we can
        /// just create a single delegate instance and pass that around, greatly reducing garbage and overhead of async operations.
        /// </summary>
        /// <param name="sender">The <see cref="GrhImageList"/> the callback came from.</param>
        /// <param name="gd">The <see cref="StationaryGrhData"/> that the <paramref name="image"/> is for. May be null if the
        /// <paramref name="image"/> is equal to <see cref="GrhImageList.ErrorImage"/> or null.</param>
        /// <param name="image">The <see cref="Image"/> that was created.</param>
        /// <param name="userState">The optional user state object that was passed to the method.</param>
        static void GrhImageListAsyncCallback(GrhImageList sender, StationaryGrhData gd, Image image, object userState)
        {
            var node = (GrhTreeViewNode)userState;

            // If the async callback was run on another thread, we will have to use Control.Invoke() to get it to the correct thread.
            // Unfortunately, this will result in a bit of overhead and create some garbage due to the parameter list, but
            // its the best we can do (as far as I can see) and GrhImageList avoids running a new thread when possible anyways so
            // it only really happens while loading.
            if (!node.TreeView.InvokeRequired)
            {
                SetNodeImage(node, image);
            }
            else
            {
                node.TreeView.Invoke(_setNodeImage, node, image);
            }
        }
        /// <summary>
        /// Updates all of the automaticly added GrhDatas.
        /// </summary>
        /// <param name="cm"><see cref="IContentManager"/> to use for new GrhDatas.</param>
        /// <param name="rootGrhDir">Root Grh texture directory.</param>
        /// <param name="added">The GrhDatas that were added (empty if none were added).</param>
        /// <param name="deleted">The GrhDatas that were deleted (empty if none were added).</param>
        /// <param name="grhDataFileTags">The file tags for the corresponding GrhDatas.</param>
        /// <returns>
        /// IEnumerable of all of the new GrhDatas created.
        /// </returns>
        public static void Update(IContentManager cm, string rootGrhDir, out GrhData[] added, out GrhData[] deleted, out Dictionary <GrhData, GrhData.FileTags> grhDataFileTags)
        {
            if (!rootGrhDir.EndsWith("\\") && !rootGrhDir.EndsWith("/"))
            {
                rootGrhDir += "/";
            }

            // Clear the temporary content to make sure we have plenty of working memory
            cm.Unload(ContentLevel.Temporary, true);

            // Get the relative file path for all files up-front (only do this once since it doesn't scale well)
            var relFilePaths = Directory.GetFiles(rootGrhDir, "*", SearchOption.AllDirectories)
                               .Select(x => x.Replace('\\', '/').Substring(rootGrhDir.Length))
                               .ToArray();

            // Also grab the existing GrhDatas
            var existingGrhDatas = GrhInfo.GrhDatas.ToDictionary(x => x.Categorization.ToString(), x => x);

            // Go through each file and do the adds
            grhDataFileTags = new Dictionary <GrhData, GrhData.FileTags>();
            HashSet <GrhData> addedGrhDatas           = new HashSet <GrhData>();
            HashSet <GrhData> deletedGrhDatas         = new HashSet <GrhData>();
            HashSet <GrhData> grhDatasToDelete        = new HashSet <GrhData>(existingGrhDatas.Values);
            HashSet <string>  checkedAnimationRelDirs = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (var relFilePath in relFilePaths)
            {
                // Before doing anything else, ensure it is a valid file type to handle
                string fileExtension = Path.GetExtension(relFilePath);
                if (!_graphicFileSuffixes.Contains(fileExtension, StringComparer.OrdinalIgnoreCase))
                {
                    continue;
                }

                string absFilePath = rootGrhDir + relFilePath;

                // Grab some stuff based on the file path
                string absDir = Path.GetDirectoryName(absFilePath);
                if (rootGrhDir.Length >= absDir.Length)
                {
                    continue;
                }

                string relDir                   = absDir.Substring(rootGrhDir.Length);
                string parentDirName            = absDir.Substring(Path.GetDirectoryName(absDir).Length + 1);
                string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(relFilePath);

                bool isAnimationFrame = parentDirName.StartsWith("_");

                if (!isAnimationFrame)
                {
                    // ** Stationary **

                    var fileTags = GrhData.FileTags.Create(fileNameWithoutExtension);

                    // Build the categorization info
                    string category          = relDir.Replace("/", SpriteCategorization.Delimiter).Replace("\\", SpriteCategorization.Delimiter);
                    SpriteCategorization cat = new SpriteCategorization(category, fileTags.Title);

                    // Get existing
                    GrhIndex?grhIndex = null;
                    GrhData  grhData;
                    if (existingGrhDatas.TryGetValue(cat.ToString(), out grhData))
                    {
                        grhDatasToDelete.Remove(grhData);
                    }

                    // If already exists as animated, delete first
                    if (grhData != null && (grhData is AnimatedGrhData || grhData is AutomaticAnimatedGrhData))
                    {
                        grhIndex = grhData.GrhIndex; // We will re-use this GrhIndex
                        GrhInfo.Delete(grhData);
                        deletedGrhDatas.Add(grhData);
                        grhData = null;
                    }

                    // Add new
                    string texturePath = "/" + relFilePath.Substring(0, relFilePath.Length - Path.GetExtension(absFilePath).Length);
                    if (grhData == null)
                    {
                        grhData = GrhInfo.CreateGrhData(cm, cat, texturePath, grhIndex);
                        addedGrhDatas.Add(grhData);
                    }
                    else
                    {
                        // Make sure the texture is correct
                        string currTextureName = "/" + ((StationaryGrhData)grhData).TextureName.ToString().TrimStart('/', '\\');
                        if (currTextureName != texturePath)
                        {
                            ((StationaryGrhData)grhData).ChangeTexture(texturePath);
                        }
                    }

                    // Ensure set to auto-size
                    StationaryGrhData stationaryGrhData = (StationaryGrhData)grhData;
                    if (!stationaryGrhData.AutomaticSize)
                    {
                        stationaryGrhData.AutomaticSize = true;
                    }

                    grhDataFileTags.Add(grhData, fileTags);
                }
                else
                {
                    // ** Animated **

                    // Make sure we only handle each animation once (since this will get called for each frame since we're looping over the files)
                    if (!checkedAnimationRelDirs.Add(relDir))
                    {
                        continue;
                    }

                    var fileTags = GrhData.FileTags.Create(parentDirName.Substring(1)); // Remove the _ prefix from directory name

                    // Build the categorization
                    string category = Path.GetDirectoryName(absDir).Substring(rootGrhDir.Length)
                                      .Replace("/", SpriteCategorization.Delimiter).Replace("\\", SpriteCategorization.Delimiter);
                    SpriteCategorization cat = new SpriteCategorization(category, fileTags.Title);

                    // Get existing
                    GrhIndex?grhIndex = null;
                    GrhData  grhData;
                    if (existingGrhDatas.TryGetValue(cat.ToString(), out grhData))
                    {
                        grhDatasToDelete.Remove(grhData);

                        // If already exists as stationary, delete first
                        if (grhData is StationaryGrhData)
                        {
                            grhIndex = grhData.GrhIndex; // We will re-use this GrhIndex
                            GrhInfo.Delete(grhData);
                            deletedGrhDatas.Add(grhData);
                            grhData = null;
                        }
                    }

                    // Add new
                    if (grhData == null)
                    {
                        grhData = GrhInfo.CreateAutomaticAnimatedGrhData(cm, cat, grhIndex);
                        addedGrhDatas.Add(grhData);
                    }

                    grhDataFileTags.Add(grhData, fileTags);
                }
            }

            // Now check if there are any GrhDatas to be deleted by taking existing GrhDatas, getting their relative path, and
            // see if that exists in our relative path list we built earlier against the file system
            foreach (var toDelete in grhDatasToDelete)
            {
                GrhInfo.Delete(toDelete);
                deletedGrhDatas.Add(toDelete);
            }

            if (log.IsInfoEnabled)
            {
                log.WarnFormat("Automatic GrhData creation update resulted in `{0}` new GrhData(s) and `{1}` deleted GrhData(s).", addedGrhDatas.Count, deletedGrhDatas.Count);
            }

            added   = addedGrhDatas.ToArray();
            deleted = deletedGrhDatas.ToArray();
        }
Exemple #9
0
 /// <summary>
 /// Sets the icon for a stationary <see cref="GrhData"/>.
 /// </summary>
 /// <param name="grhData">The <see cref="StationaryGrhData"/>.</param>
 void SetIconImageStationary(StationaryGrhData grhData)
 {
     _grhImageList.GetImageAsync(grhData, _asyncCallback, this);
 }
Exemple #10
0
 /// <summary>
 /// Asynchronously gets the <see cref="Image"/> for the given argument.
 /// </summary>
 /// <param name="gd">The <see cref="StationaryGrhData"/> to get the <see cref="Image"/> for.</param>
 /// <param name="callback">The <see cref="GrhImageListAsyncCallback"/> to invoke when the operation has finished.</param>
 /// <param name="userState">The optional user state object to pass to the <paramref name="callback"/>.</param>
 public void GetImageAsync(StationaryGrhData gd, GrhImageListAsyncCallback callback, object userState)
 {
     GetImage(gd, true, callback, userState);
 }
Exemple #11
0
        /// <summary>
        /// Gets the <see cref="Image"/> for the given argument.
        /// </summary>
        /// <param name="gd">The <see cref="StationaryGrhData"/> to get the <see cref="Image"/> for.</param>
        /// <param name="async">If true, asynchronous mode will be used. This will return null immediately if the desired
        /// <see cref="Image"/> has not yet been created.</param>
        /// <param name="callback">When <see cref="async"/> is false, contains the callback method to invoke when the <see cref="Image"/>
        /// has been created.</param>
        /// <param name="userState">The optional user state object to pass to the <paramref name="callback"/>.</param>
        /// <returns>
        /// The <see cref="Image"/> for the <paramref name="gd"/>, or null if <paramref name="async"/> is set.
        /// </returns>
        Image GetImage(StationaryGrhData gd, bool async, GrhImageListAsyncCallback callback, object userState)
        {
            if (gd == null)
            {
                if (!async)
                {
                    // Return the ErrorImage directly
                    return(ErrorImage);
                }
                else
                {
                    // Raise the callback and pass the ErrorImage
                    if (callback != null)
                    {
                        callback(this, gd, ErrorImage, userState);
                    }
                    return(null);
                }
            }

            // Get the key
            var key = GetImageKey(gd);

            // Get the image from the cache
            Image img;

            lock (_imagesSync)
            {
                // Check if the image already exists
                if (!_images.TryGetValue(key, out img))
                {
                    // Image does not exist, so add the placeholder since we are about to create it. Placing the placeholder
                    // in there will make sure that no other threads try to create it at the same time.
                    img = null;
                    _images.Add(key, _placeholder);
                }
            }

            if (!async)
            {
                if (img != null)
                {
                    if (img == _placeholder)
                    {
                        // If we got the placeholder image, do a spin-wait until we get the actual image, then return the image. This will
                        // happen when another thread is creating the image.
                        return(SpinWaitForImage(key));
                    }
                    else
                    {
                        // Any other non-null image means that the image was already created, so we can just return it immediately
                        return(img);
                    }
                }
                else
                {
                    // Create it on this thread and return it when its done
                    return(CreateAndInsertImage(key, gd));
                }
            }
            else
            {
                if (img != null)
                {
                    // When we get the placeholder image while in async mode, this is slightly more annoying since we have
                    // to create another thread to spin-wait on
                    if (img == _placeholder)
                    {
                        // Create the thread to spin-wait on... though only if we were given a callback method. Obviously does no
                        // good to wait for the image when there is no callback method.
                        if (callback != null)
                        {
                            var tpacs = new ThreadPoolAsyncCallbackState(gd, callback, userState, true, null);
                            ExecuteOnThreadPool(tpacs);
                        }
                    }
                    else
                    {
                        // But when we get the actual image, we can just invoke the callback directly from this thread. This is the
                        // once scenario where no threads are created in async mode.
                        if (callback != null)
                        {
                            callback(this, gd, img, userState);
                        }
                    }
                }
                else
                {
                    // NOTE: The asynchronous aspect is less than optimal due to this.
                    // When originally designing this, I was working under the assumption that SFML would be able to deal with the threading
                    // better. Turns out I was wrong. There are probably some other threading issues that would have to be taken into
                    // account, too, like that content can be disposed on the main thread. I could offload more onto the worker thread
                    // than just the rescaling, such as the generation of the original unscaled bitmap, but the biggest gains come from
                    // the ability to offload the actual image loading. I guess its helpful to have at least a little work offloaded
                    // than to be completely synchronous since that does mean multi-core CPUs can load a bit faster.
                    var bmp = CreateUnscaledBitmap(gd);
                    if (bmp == null)
                    {
                        // If the bitmap failed to be created for whatever reason, use the ErrorImage
                        if (callback != null)
                        {
                            callback(this, gd, ErrorImage, userState);
                        }

                        lock (_imagesSync)
                        {
                            Debug.Assert(_images[key] == _placeholder);
                            _images[key] = ErrorImage;
                        }
                    }
                    else
                    {
                        // Add the Image creation job to the thread pool
                        var tpacs = new ThreadPoolAsyncCallbackState(gd, callback, userState, false, bmp);
                        ExecuteOnThreadPool(tpacs);
                    }
                }

                // Async always returns null
                return(null);
            }
        }
Exemple #12
0
 /// <summary>
 /// Gets the <see cref="Image"/> for the given argument.
 /// </summary>
 /// <param name="gd">The <see cref="StationaryGrhData"/> to get the <see cref="Image"/> for.</param>
 /// <returns>
 /// The <see cref="Image"/> for the <paramref name="gd"/>.
 /// </returns>
 public Image GetImage(StationaryGrhData gd)
 {
     return(GetImage(gd, false, null, null));
 }
Exemple #13
0
        /// <summary>
        /// Creates an <see cref="Image"/> for a <see cref="StationaryGrhData"/>, adds it to the cache, and returns it.
        /// Only call this if the image actually needs to be created, and never call this while in the <see cref="_imagesSync"/>
        /// lock!
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="gd">The <see cref="StationaryGrhData"/>.</param>
        /// <returns>The <see cref="Image"/>.</returns>
        Image CreateAndInsertImage(string key, StationaryGrhData gd)
        {
            Image img;

            // The image was null, so we are going to be the ones to create it
            try
            {
                // Try to create the image
                var tex = gd.GetOriginalTexture();
                if (tex == null)
                {
                    img = ErrorImage;
                }
                else
                {
                    img = tex.ToBitmap(gd.OriginalSourceRect, ImageWidth, ImageHeight);
                }
            }
            catch (Exception ex)
            {
                const string errmsg = "Failed to create GrhImageList image for `{0}`. Exception: {1}";
                if (log.IsErrorEnabled)
                {
                    log.ErrorFormat(errmsg, gd, ex);
                }
                if (!(ex is LoadingFailedException))
                {
                    Debug.Fail(string.Format(errmsg, gd, ex));
                }
                img = _errorImage;
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Created GrhImageList image for `{0}`.", img);
            }

            // If we for some reason have the _placeholder or null image by this point, there is an error in the logic above. But to
            // avoid a deadlock (even though this should never happen), we will just set it to the ErrorImage if it somehow does.
            if (img == null || img == _placeholder)
            {
                const string errmsg = "Created image was either null or the placeholder image, which should never happen.";
                if (log.IsErrorEnabled)
                {
                    log.Error(errmsg);
                }
                Debug.Fail(errmsg);
                img = _errorImage;
            }

            // Add the image to the cache (it will either be the correct image, or the ErrorImage if it failed). Remember that we
            // have already inserted the placeholder image at the key. So we're just replacing the placeholder with the actual image.
            lock (_imagesSync)
            {
                Debug.Assert(_images[key] == _placeholder);
                _images[key] = img;
            }

            // Return the generated image
            return(img);
        }