Coding improvements!

Afr0

Well-Known Member
Working on Project Dollhouse has allowed me to learn a lot about proper error and exception handling.
Examples:

  • Never assume that a file or directory exists.
  • Never assume that a registry key exists.
  • Always log if you know something can go wrong. Always.
Here's some code for the chat client I'm working on that I thought might be cool to share.

Code:
        /// <summary>
        /// Returns this Sim's thumbnail image.
        /// If the HeadOutfitID isn't set, an empty Bitmap instance
        /// will be returned.
        /// </summary>
        public Bitmap Thumbnail
        {
            get
            {
                if (m_HeadOutfitID != 0)
                {
                    if (m_Thumbnail == null)
                    {
                        m_Thumbnail = GetThumbnail();
                        return m_Thumbnail;
                    }
                    else
                        return m_Thumbnail;
                }
                else
                    return new Bitmap(1, 1);
            }
        }      

/// <summary>
        /// Gets a sim's thumbnail image.
        /// </summary>
        /// <returns></returns>
        private Bitmap GetThumbnail()
        {
            Outfit Oft = new Outfit();
            Appearance Apr = new Appearance();
            Bitmap Thumbnail = new Bitmap(1, 1);

            if (!File.Exists(GlobalSettings.Default.ClientPath + "avatardata\\heads\\outfits\\outfits.dat"))
            {
                Debug.WriteLine("WARNING: Couldn't find: " + GlobalSettings.Default.ClientPath +
                "avatardata\\heads\\outfits\\outfits.dat");

                return Thumbnail;
            }

            FAR3Archive Archive = new FAR3Archive(GlobalSettings.Default.ClientPath +
                "avatardata\\heads\\outfits\\outfits.dat");
            Oft.Read(new MemoryStream(Archive.GetItemByID(HeadOutfitID)));

            Archive = new FAR3Archive(GlobalSettings.Default.ClientPath +
                "avatardata\\heads\\appearances\\appearances.dat");
            TSO.Common.content.ContentID ApprID;

            switch (Appearance)
            {
                case AppearanceType.Light:
                    ApprID = Oft.GetAppearance(AppearanceType.Light);
                    Apr.Read(new MemoryStream(Archive.GetItemByID(ApprID.Shift())));

                    Archive = new FAR3Archive(GlobalSettings.Default.ClientPath +
                        "avatardata\\heads\\thumbnails\\thumbnails.dat");
                    Thumbnail = new Bitmap(new MemoryStream(Archive.GetItemByID(Apr.ThumbnailID.Shift())));
                    break;
                case AppearanceType.Medium:
                    ApprID = Oft.GetAppearance(AppearanceType.Medium);
                    Apr.Read(new MemoryStream(Archive.GetItemByID(ApprID.Shift())));

                    Archive = new FAR3Archive(GlobalSettings.Default.ClientPath +
                        "avatardata\\heads\\thumbnails\\thumbnails.dat");
                    Thumbnail = new Bitmap(new MemoryStream(Archive.GetItemByID(Apr.ThumbnailID.Shift())));
                    break;
                case AppearanceType.Dark:
                    ApprID = Oft.GetAppearance(AppearanceType.Dark);
                    Apr.Read(new MemoryStream(Archive.GetItemByID(ApprID.Shift())));

                    Archive = new FAR3Archive(GlobalSettings.Default.ClientPath +
                        "avatardata\\heads\\thumbnails\\thumbnails.dat");
                    Thumbnail = new Bitmap(new MemoryStream(Archive.GetItemByID(Apr.ThumbnailID.Shift())));
                    break;
            }

            return Thumbnail;
        }
 
Seems pretty standard... A few questions though:
  • Why not use the existing content system to retrieve appearances? This system is used by the VM and is initiated during the loading phase I believe. Here you're reading and decoding the FARv3 archive every time the GetThumbnail function is called... Here's a code example:
TSO.Content.Content.Get().AvatarAppearances.Get(ID);​
  • Why create the 1x1 bitmap all the time? It should only be created when the resource is not found.
 
I didn't wanna have to use all the client Libs. If you look at the get accessor, I'm only calling GetThumbnail() once.
 
Back
Top