Coding improvements!

Discussion in 'Programming' started by Afr0, Nov 23, 2014.

  1. Afr0

    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;
            }
    aidancheddar likes this.
  2. RHY3756547

    RHY3756547 FreeSO Developer Staff Member Moderator

    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.
  3. Afr0

    Afr0 Well-Known Member

    I didn't wanna have to use all the client Libs. If you look at the get accessor, I'm only calling GetThumbnail() once.
  4. ddfczm

    ddfczm Administrator Staff Member Moderator

Share This Page