예제 #1
0
        public static void SetUserRole(Guid profileId, int userRole)
        {
            CProfile profile = CProfiles.GetProfile(profileId);

            if (profile == null)
            {
                throw new ArgumentException("Invalid profileId");
            }

            var option = (EUserRole)userRole;

            //Only allow the change of all options exept TR_USERROLE_GUEST and TR_USERROLE_NORMAL
            const EUserRole mask = (EUserRole.TR_USERROLE_GUEST | EUserRole.TR_USERROLE_NORMAL);

            option &= ~mask;

            profile.UserRole = (profile.UserRole & mask) | option;

            CProfiles.EditProfile(profile);
            CProfiles.Update();
            CProfiles.SaveProfiles();
        }
예제 #2
0
        public static bool SendProfileData(SProfileData profile)
        {
            CProfile newProfile;
            CProfile existingProfile = CProfiles.GetProfile(profile.ProfileId);

            if (existingProfile != null)
            {
                newProfile = new CProfile
                {
                    ID         = existingProfile.ID,
                    FilePath   = existingProfile.FilePath,
                    Active     = existingProfile.Active,
                    Avatar     = existingProfile.Avatar,
                    Difficulty = existingProfile.Difficulty,
                    UserRole   = existingProfile.UserRole,
                    PlayerName = existingProfile.PlayerName
                };
            }
            else
            {
                newProfile = new CProfile
                {
                    Active   = EOffOn.TR_CONFIG_ON,
                    UserRole = EUserRole.TR_USERROLE_NORMAL
                };
            }

            if (profile.Avatar != null)
            {
                newProfile.Avatar = _AddAvatar(profile.Avatar);
            }
            else if (newProfile.Avatar == null || newProfile.Avatar.ID == -1)
            {
                newProfile.Avatar = CProfiles.GetAvatars().First();

                /*CAvatar avatar = new CAvatar(-1);
                 * avatar.LoadFromFile("Profiles\\Avatar_f.png");
                 * CProfiles.AddAvatar(avatar);
                 * newProfile.Avatar = avatar;*/
            }

            if (!string.IsNullOrEmpty(profile.PlayerName))
            {
                newProfile.PlayerName = profile.PlayerName;
            }
            else if (!string.IsNullOrEmpty(newProfile.PlayerName))
            {
                newProfile.PlayerName = "DummyName";
            }

            if (profile.Difficulty >= 0 && profile.Difficulty <= 2)
            {
                newProfile.Difficulty = (EGameDifficulty)profile.Difficulty;
            }

            if (profile.Type >= 0 && profile.Type <= 1)
            {
                EUserRole option = profile.Type == 0 ? EUserRole.TR_USERROLE_GUEST : EUserRole.TR_USERROLE_NORMAL;
                //Only allow the change of TR_USERROLE_GUEST and TR_USERROLE_NORMAL
                const EUserRole mask = EUserRole.TR_USERROLE_NORMAL;
                newProfile.UserRole = (newProfile.UserRole & mask) | option;
            }

            if (!string.IsNullOrEmpty(profile.Password))
            {
                if (profile.Password == "***__CLEAR_PASSWORD__***")
                {
                    newProfile.PasswordSalt = null;
                    newProfile.PasswordHash = null;
                }
                else
                {
                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                    byte[] buffer = new byte[32];
                    rng.GetNonZeroBytes(buffer);
                    byte[] salt           = buffer;
                    byte[] hashedPassword = _Hash((new UTF8Encoding()).GetBytes(profile.Password), salt);

                    newProfile.PasswordSalt = salt;
                    newProfile.PasswordHash = hashedPassword;
                }
            }

            if (existingProfile != null)
            {
                CProfiles.EditProfile(newProfile);
                CProfiles.Update();
                CProfiles.SaveProfiles();
            }
            else
            {
                CProfiles.AddProfile(newProfile);
            }

            return(true);
        }