Пример #1
0
        private HashSet <string> GetMatchingCharacterIds(string textToFind)
        {
            HashSet <string> matchingCharacterIds;

            if (!m_findTextToMatchingCharacterIds.TryGetValue(textToFind, out matchingCharacterIds))
            {
                if (CharacterDetailData.Singleton.GetAll().Count() !=
                    CharacterVerseData.SingletonLocalizedCharacterIdToCharacterIdDictionary.Count)
                {
                    // First time getting UI versions of character IDs (typically when running tests), so we need to force
                    // population of CharacterVerseData.SingletonLocalizedCharacterIdToCharacterIdDictionary.
                    foreach (var characterId in CharacterGroups.SelectMany(characterGroup => characterGroup.CharacterIds))
                    {
                        CharacterVerseData.GetCharacterNameForUi(characterId);
                    }
                }
                matchingCharacterIds = new HashSet <string>();
                m_findTextToMatchingCharacterIds.Add(textToFind, matchingCharacterIds);
                foreach (var kvp in CharacterVerseData.SingletonLocalizedCharacterIdToCharacterIdDictionary)
                {
                    if (kvp.Value.IndexOf(textToFind, StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        matchingCharacterIds.Add(kvp.Key);
                    }
                }
            }
            return(matchingCharacterIds);
        }
Пример #2
0
        /// <summary>
        /// Generates a password with the given length.
        /// </summary>
        /// <param name="length">The length of the password to be generated.</param>
        /// <returns>A randomly generated password.</returns>
        public string GeneratePassword(int length)
        {
            if (length < MinLength)
            {
                throw new ArgumentOutOfRangeException(nameof(length), $"The length of the generated password must be at least {MinLength} characters.");
            }

            // Create a collection of character groups the size of the password to be generated.
            // The number of times a character group appears in this collection is equal to the
            // minimum number of occurrences of that character group. An AllCharacters group is
            // created to fill in the characters not accounted for by the other character groups.
            List <CharacterGroup> characterGroups = CharacterGroups
                                                    .SelectMany(characterGroup => Enumerable.Repeat(characterGroup, characterGroup.MinOccurrence))
                                                    .Concat(Enumerable.Repeat(new CharacterGroup(AllCharacters), length - MinLength))
                                                    .ToList();

            char[] passwordChars = new char[length];

            for (int i = 0; i < length; i++)
            {
                // Pull a random character from the character group into the password
                CharacterGroup characterGroup = characterGroups[i];
                int            index          = Random.Int32Between(0, characterGroup.Characters.Length);
                passwordChars[i] = characterGroup.Characters[index];
            }

            // Scramble the generated set of characters
            passwordChars.Scramble();

            return(new string(passwordChars));
        }
Пример #3
0
        public void Sort(SortedBy by, bool sortAscending)
        {
            Comparison <CharacterGroup> how;
            int direction = sortAscending ? kAscending : kDescending;

            switch (by)
            {
            case SortedBy.Name:
                how = (a, b) => String.Compare(a.Name, b.Name, StringComparison.CurrentCulture) * direction;
                break;

            case SortedBy.Attributes:
                how = (a, b) => String.Compare(a.AttributesDisplay, b.AttributesDisplay, StringComparison.CurrentCulture) * direction;
                break;

            case SortedBy.Actor:
                how = (a, b) =>
                {
                    var actorA = m_project.VoiceActorList.GetVoiceActorById(a.VoiceActorId);
                    var nameA  = actorA == null ? String.Empty : actorA.Name;
                    var actorB = m_project.VoiceActorList.GetVoiceActorById(b.VoiceActorId);
                    var nameB  = actorB == null ? String.Empty : actorB.Name;
                    return(String.Compare(nameA, nameB, StringComparison.CurrentCulture) * direction);
                };
                break;

            case SortedBy.EstimatedTime:
                how = (a, b) => a.EstimatedHours.CompareTo(b.EstimatedHours) * direction;
                break;

            default:
                throw new ArgumentException("Unexpected sorting method", "by");
            }
            CharacterGroups.Sort(how);
        }
Пример #4
0
 void Start()
 {
     if (main == null)
         main = this;
     else if (main == this)
         Destroy(gameObject);
 }
Пример #5
0
 /// <summary>
 /// Note: There should only ever be one such group, but early on, Glyssen would allow the same
 /// actor to be assigned to multiple groups, so for compatibility reasons, we allow for this.
 /// </summary>
 public IEnumerable <CharacterGroup> GetGroupsAssignedToActor(int actorId)
 {
     if (actorId <= CharacterGroup.kNoActorAssigned)
     {
         throw new ArgumentException("GetGroupsAssignedToActor should not be used to get groups assigned to no actor (no good reason; it just shouldn't).");
     }
     return(CharacterGroups.Where(g => g.VoiceActorId == actorId));
 }
Пример #6
0
 public IRandomStringGenerationBuilder AndAllowCharacters(CharacterGroups characters)
 {
     foreach (var c in CharacterGroup.Get(characters))
     {
         this.allowedCharacters.Add(c);
     }
     return(this);
 }
        public VoiceActorAssignmentViewModel(Project project)
        {
            m_project = project;

            CharacterGroupAttribute <CharacterGender> .GetUiStringForValue = GetUiStringForCharacterGender;
            CharacterGroupAttribute <CharacterAge> .GetUiStringForValue    = GetUiStringForCharacterAge;

#if DEBUG
            var p = new Proximity(m_project.IncludedBooks, m_project.DramatizationPreferences);
            foreach (var group in CharacterGroups.OrderBy(g => g.GroupIdForUiDisplay))
            {
                Debug.WriteLine(group.GroupIdForUiDisplay + ": " + p.CalculateMinimumProximity(group.CharacterIds));
            }
#endif
        }
        public VoiceActorAssignmentViewModel(Project project)
        {
            m_project        = project;
            ProjectProximity = new Proximity(m_project, false);

            CharacterGroupAttribute <CharacterGender> .GetUiStringForValue = GetUiStringForCharacterGender;
            CharacterGroupAttribute <CharacterAge> .GetUiStringForValue    = GetUiStringForCharacterAge;

            LogAndOutputToDebugConsole("Group".PadRight(7) + ": " + MinimumProximity.ReportHeader + Environment.NewLine +
                                       "-".PadRight(100, '-'));
            foreach (var group in CharacterGroups.OrderBy(g => g.GroupIdForUiDisplay))
            {
                LogAndOutputToDebugConsole(group.GroupIdForUiDisplay.PadRight(7) + ": " + ProjectProximity.CalculateMinimumProximity(group.CharacterIds));
            }
        }
Пример #9
0
        // Keep this method around for now in case we decide to support templates in some scenarios
        // ReSharper disable once UnusedMember.Local
        private void CreateInitialGroupsFromTemplate()
        {
            CharacterGroupTemplate charGroupTemplate;

            using (var tempFile = new TempFile())
            {
                File.WriteAllBytes(tempFile.Path, Resources.CharacterGroups);
                ICharacterGroupSource charGroupSource = new CharacterGroupTemplateExcelFile(m_project, tempFile.Path);
                charGroupTemplate = charGroupSource.GetTemplate(m_project.VoiceActorList.Actors.Count);
            }

            HashSet <string> includedCharacterIds = new HashSet <string>();

            foreach (var book in m_project.IncludedBooks)
            {
                foreach (var block in book.GetScriptBlocks(true))
                {
                    if (!block.CharacterIsUnclear())
                    {
                        includedCharacterIds.Add(block.CharacterId);
                    }
                }
            }
            ISet <string> matchedCharacterIds = new HashSet <string>();

            foreach (var group in charGroupTemplate.CharacterGroups.Values)
            {
                group.CharacterIds.IntersectWith(includedCharacterIds);

                if (!group.CharacterIds.Any())
                {
                    continue;
                }

                CharacterGroups.Add(group);

                matchedCharacterIds.AddRange(group.CharacterIds);
            }

            // Add an extra group for any characters which weren't in the template
            var unmatchedCharacters     = includedCharacterIds.Except(matchedCharacterIds);
            var unmatchedCharacterGroup = new CharacterGroup(m_project, ByKeyStrokeComparer);

            unmatchedCharacterGroup.GroupNumber = 999;
            unmatchedCharacterGroup.CharacterIds.AddRange(unmatchedCharacters);
            CharacterGroups.Add(unmatchedCharacterGroup);
        }
Пример #10
0
        public VoiceActorAssignmentViewModel(Project project, Dictionary <string, int> keyStrokesByCharacterId = null)
        {
            m_project = project;

            m_keyStrokesByCharacterId = keyStrokesByCharacterId ?? m_project.GetKeyStrokesByCharacterId();

            CharacterGroupAttribute <CharacterGender> .GetUiStringForValue = GetUiStringForCharacterGender;
            CharacterGroupAttribute <CharacterAge> .GetUiStringForValue    = GetUiStringForCharacterAge;
            m_project.CharacterGroupList.PopulateEstimatedHours(m_keyStrokesByCharacterId);

#if DEBUG
            var p = new Proximity(m_project);
            foreach (var group in CharacterGroups.OrderBy(g => g.GroupNumber))
            {
                Debug.WriteLine(group.GroupNumber + ": " + p.CalculateMinimumProximity(group.CharacterIds));
            }
#endif
        }
Пример #11
0
        private CharacterGroup GetSourceGroupForMove(IList <string> characterIds, CharacterGroup destGroup)
        {
            if (characterIds.Count == 0)
            {
                throw new ArgumentException("At least one characterId must be provided", "characterIds");
            }

            // Currently, we assume all characterIds are coming from the same source group
            CharacterGroup sourceGroup = CharacterGroups.FirstOrDefault(t => t.CharacterIds.Contains(characterIds[0]));

            // REVIEW: The second part of this condition used to be done in the following "if" statement, but I can't
            // think of any reason why moving a character to the group it's already in should result in any unused groups.
            if (sourceGroup == null || sourceGroup == destGroup)
            {
                return(null);
            }

            if (destGroup == null && !sourceGroup.IsVoiceActorAssigned && sourceGroup.CharacterIds.SetEquals(characterIds))
            {
                return(null);                // Moving all characetrs from an unassigned group to a new group would accomplish nothing.
            }
            return(sourceGroup);
        }
Пример #12
0
        public static char[] Get(CharacterGroups allowedCharacters)
        {
            if (allowedCharacters == CharacterGroups.None)
            {
                throw new ArgumentOutOfRangeException(nameof(allowedCharacters),
                                                      "At least one character group is needed to generate a random string.");
            }

            var allowedCharactersArrayLength = 0;

            if (allowedCharacters.HasFlag(CharacterGroups.UpperCaseLetters))
            {
                allowedCharactersArrayLength += CharacterGroup.UpperCaseLetters.Length;
            }

            if (allowedCharacters.HasFlag(CharacterGroups.LowerCaseLetters))
            {
                allowedCharactersArrayLength += CharacterGroup.LowerCaseLetters.Length;
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Digits))
            {
                allowedCharactersArrayLength += CharacterGroup.Digits.Length;
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Minus))
            {
                allowedCharactersArrayLength += CharacterGroup.Minus.Length;
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Underscore))
            {
                allowedCharactersArrayLength += CharacterGroup.Underscore.Length;
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Space))
            {
                allowedCharactersArrayLength += CharacterGroup.Space.Length;
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Brackets))
            {
                allowedCharactersArrayLength += CharacterGroup.Bracket.Length;
            }

            if (allowedCharacters.HasFlag(CharacterGroups.SpecialReadableAsciiLetters))
            {
                allowedCharactersArrayLength += CharacterGroup.SpecialReadableAscriiLetters.Length;
            }

            var       finalArray = new char[allowedCharactersArrayLength];
            var       offset     = 0;
            const int itemSize   = sizeof(char);

            if (allowedCharacters.HasFlag(CharacterGroups.UpperCaseLetters))
            {
                Buffer.BlockCopy(CharacterGroup.UpperCaseLetters, 0, finalArray, offset, CharacterGroup.UpperCaseLetters.Length * itemSize);
                offset += (CharacterGroup.UpperCaseLetters.Length * sizeof(char));
            }

            if (allowedCharacters.HasFlag(CharacterGroups.LowerCaseLetters))
            {
                Buffer.BlockCopy(CharacterGroup.LowerCaseLetters, 0, finalArray, offset, CharacterGroup.LowerCaseLetters.Length * itemSize);
                offset += (CharacterGroup.LowerCaseLetters.Length * sizeof(char));
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Digits))
            {
                Buffer.BlockCopy(CharacterGroup.Digits, 0, finalArray, offset, CharacterGroup.Digits.Length * itemSize);
                offset += (CharacterGroup.Digits.Length * sizeof(char));
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Minus))
            {
                Buffer.BlockCopy(CharacterGroup.Minus, 0, finalArray, offset, CharacterGroup.Minus.Length * itemSize);
                offset += (CharacterGroup.Minus.Length * sizeof(char));
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Underscore))
            {
                Buffer.BlockCopy(CharacterGroup.Underscore, 0, finalArray, offset, CharacterGroup.Underscore.Length * itemSize);
                offset += (CharacterGroup.Underscore.Length * sizeof(char));
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Space))
            {
                Buffer.BlockCopy(CharacterGroup.Space, 0, finalArray, offset, CharacterGroup.Space.Length * itemSize);
                offset += (CharacterGroup.Space.Length * sizeof(char));
            }

            if (allowedCharacters.HasFlag(CharacterGroups.Brackets))
            {
                Buffer.BlockCopy(CharacterGroup.Bracket, 0, finalArray, offset, CharacterGroup.Bracket.Length * itemSize);
                offset += (CharacterGroup.Bracket.Length * sizeof(char));
            }

            if (allowedCharacters.HasFlag(CharacterGroups.SpecialReadableAsciiLetters))
            {
                Buffer.BlockCopy(CharacterGroup.SpecialReadableAscriiLetters, 0, finalArray,
                                 offset, CharacterGroup.SpecialReadableAscriiLetters.Length * itemSize);
                // do not forget offset increment when adding new character groups after this!
            }

            return(finalArray);
        }
Пример #13
0
 /// <summary>
 /// Generates a random string based on specified options.
 /// </summary>
 /// <param name="length">The desired length of the generated string.</param>
 /// <param name="allowedCharacters">The set of allowed characters used for string generation.</param>
 /// <param name="eachCharacterMustOccurAtLeastOnce">Specifies whether each character in the <paramref name="allowedCharacters"/>
 /// array must occur at least once in the generated random string.</param>
 /// <returns>The generated string.</returns>
 protected virtual string CoreCreate(int length, CharacterGroups allowedCharacters, bool eachCharacterMustOccurAtLeastOnce) =>
 this.CoreCreate(length, CharacterGroup.Get(allowedCharacters), eachCharacterMustOccurAtLeastOnce);
Пример #14
0
 /// <summary>
 /// Generates a random string based on specified options.
 /// </summary>
 /// <param name="length">The desired length of the generated string.</param>
 /// <param name="allowedCharacters">The set of allowed characters used for string generation.</param>
 /// <param name="eachCharacterMustOccurAtLeastOnce">Specifies whether each character in the <paramref name="allowedCharacters"/>
 /// array must occur at least once in the generated random string.</param>
 /// <returns>The generated string.</returns>
 public virtual string Generate(int length, CharacterGroups allowedCharacters, bool eachCharacterMustOccurAtLeastOnce = false) =>
 this.CoreCreate(length, allowedCharacters, eachCharacterMustOccurAtLeastOnce);
 /// <summary>
 /// Converts the selected <see cref="CharacterGroups"/> to a char array.
 /// </summary>
 /// <param name="groups">The selected character groups.</param>
 /// <returns>A char array representing the selected character groups.</returns>
 public static char[] ToCharArray(this CharacterGroups groups)
 {
     // we use an extension method so that we do not have to make the whole class public.
     return(CharacterGroup.Get(groups));
 }
Пример #16
0
 public IRandomStringGenerationBuilder AllowCharacters(CharacterGroups characters)
 {
     this.allowedCharacters = new List <char>(CharacterGroup.Get(characters));
     return(this);
 }