Exemple #1
0
        public void TestParseRequirements(string input, string expected)
        {
            var builder = new AchievementBuilder();

            builder.ParseRequirements(Tokenizer.CreateTokenizer(input));
            Assert.That(builder.RequirementsDebugString, Is.EqualTo(expected));
        }
        public void TestDiff(string leftSerialized, string rightSerialized, string expected)
        {
            var notes = new Dictionary <int, string>();

            var builder = new AchievementBuilder();

            builder.ParseRequirements(Tokenizer.CreateTokenizer(leftSerialized));
            var leftRequirements = builder.ToAchievement().CoreRequirements;

            builder = new AchievementBuilder();
            builder.ParseRequirements(Tokenizer.CreateTokenizer(rightSerialized));
            var rightRequirements = builder.ToAchievement().CoreRequirements;

            var vmRequirementGroup = new RequirementGroupViewModel("Group", leftRequirements, rightRequirements, NumberFormat.Decimal, notes);

            var strBuilder = new StringBuilder();

            foreach (var vmRequirement in vmRequirementGroup.Requirements)
            {
                var vmComparison = vmRequirement as RequirementComparisonViewModel;
                strBuilder.Append(vmComparison.Definition);
                strBuilder.Append('|');
                strBuilder.Append(vmComparison.OtherDefinition);
                strBuilder.Append('\n');
            }
            strBuilder.Length--;

            Assert.That(strBuilder.ToString(), Is.EqualTo(expected));
        }
        public void TestDefinition(string serialized, string expected)
        {
            var builder = new AchievementBuilder();

            builder.ParseRequirements(Tokenizer.CreateTokenizer(serialized));

            var requirement   = builder.ToAchievement().CoreRequirements.First();
            var notes         = new Dictionary <int, string>();
            var vmRequirement = new RequirementViewModel(requirement, NumberFormat.Decimal, notes);

            Assert.That(vmRequirement.Definition, Is.EqualTo(expected));
        }
Exemple #4
0
        public void TestParseRequirementsAddSourceUninitializedRight()
        {
            // ensures requirement.Right is uninitialized for AddSource
            var builder = new AchievementBuilder();

            builder.ParseRequirements(Tokenizer.CreateTokenizer("A:0xN20770f=0"));
            var requirement = builder.CoreRequirements.First();

            Assert.That(requirement.Right.Size, Is.EqualTo(FieldSize.None));
            Assert.That(requirement.Right.Type, Is.EqualTo(FieldType.None));
            Assert.That(requirement.Right.Value, Is.EqualTo(0));
        }
Exemple #5
0
 private static Achievement CreateValue(string definition)
 {
     var achievementBuilder = new AchievementBuilder();
     if (definition.Length > 2)
     {
         if (definition[1] == ':')
             achievementBuilder.ParseRequirements(Tokenizer.CreateTokenizer(definition));
         else
             achievementBuilder.ParseValue(Tokenizer.CreateTokenizer(definition));
     }
     return achievementBuilder.ToAchievement();
 }
        public void TestRemovedRequirement()
        {
            var notes = new Dictionary <int, string>();

            var builder = new AchievementBuilder();

            builder.ParseRequirements(Tokenizer.CreateTokenizer("0xH1234=7"));
            var requirement = builder.ToAchievement().CoreRequirements.First();

            var vmRequirement = new RequirementComparisonViewModel(null, requirement, NumberFormat.Decimal, notes);

            Assert.That(vmRequirement.Definition, Is.EqualTo(""));
            Assert.That(vmRequirement.OtherDefinition, Is.EqualTo("byte(0x001234) == 7"));
            Assert.That(vmRequirement.IsModified, Is.True);
        }
        public void TestNotes(string serialized, string expected)
        {
            var notes = new Dictionary <int, string>();

            notes[0x1234] = "Addr1";
            notes[0x2345] = "Addr2";
            notes[0x3456] = "This note is long enough that it will need to be wrapped.";
            notes[0x4567] = "This note\nis multiple\nlines.";

            var builder = new AchievementBuilder();

            builder.ParseRequirements(Tokenizer.CreateTokenizer(serialized));
            var requirement   = builder.ToAchievement().CoreRequirements.First();
            var vmRequirement = new RequirementViewModel(requirement, NumberFormat.Decimal, notes);

            Assert.That(vmRequirement.Notes, Is.EqualTo(expected));
        }
Exemple #8
0
        private static bool Matches(string memAddr, SearchCriteria criteria)
        {
            AchievementBuilder builder = new AchievementBuilder();

            builder.ParseRequirements(Tokenizer.CreateTokenizer(memAddr));

            if (criteria.NumConditionsComparison != RequirementOperator.None)
            {
                var numConditions = builder.CoreRequirements.Count;
                foreach (var altGroup in builder.AlternateRequirements)
                {
                    numConditions += altGroup.Count;
                }
                if (!ValueMatches(numConditions, criteria.NumConditions, criteria.NumConditionsComparison))
                {
                    return(false);
                }
            }

            if (!ValueMatches(builder.AlternateRequirements.Count, criteria.NumAltGroups, criteria.NumAltGroupsComparison))
            {
                return(false);
            }

            foreach (var condition in builder.CoreRequirements)
            {
                if (ConditionMatches(condition, criteria))
                {
                    return(true);
                }
            }

            foreach (var altGroup in builder.AlternateRequirements)
            {
                foreach (var condition in altGroup)
                {
                    if (ConditionMatches(condition, criteria))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemple #9
0
        public LeaderboardViewModel(GameViewModel owner, Leaderboard leaderboard)
        {
            _clipboard = ServiceRepository.Instance.FindService <IClipboardService>();

            _leaderboard = leaderboard;
            Title        = leaderboard.Title;
            Description  = leaderboard.Description;

            var groups = new List <LeaderboardGroupViewModel>();

            var achievement = new AchievementBuilder();

            achievement.ParseRequirements(Tokenizer.CreateTokenizer(_leaderboard.Start));
            groups.Add(new LeaderboardGroupViewModel("Start Conditions", achievement.ToAchievement().CoreRequirements, owner.Notes)
            {
                CopyToClipboardCommand = new DelegateCommand(() => _clipboard.SetData(_leaderboard.Start))
            });

            achievement = new AchievementBuilder();
            achievement.ParseRequirements(Tokenizer.CreateTokenizer(_leaderboard.Cancel));
            groups.Add(new LeaderboardGroupViewModel("Cancel Condition", achievement.ToAchievement().CoreRequirements, owner.Notes)
            {
                CopyToClipboardCommand = new DelegateCommand(() => _clipboard.SetData(_leaderboard.Cancel))
            });

            achievement = new AchievementBuilder();
            achievement.ParseRequirements(Tokenizer.CreateTokenizer(_leaderboard.Submit));
            groups.Add(new LeaderboardGroupViewModel("Submit Condition", achievement.ToAchievement().CoreRequirements, owner.Notes)
            {
                CopyToClipboardCommand = new DelegateCommand(() => _clipboard.SetData(_leaderboard.Submit))
            });

            groups.Add(new LeaderboardGroupViewModel("Value", _leaderboard.Value, owner.Notes)
            {
                CopyToClipboardCommand = new DelegateCommand(() => _clipboard.SetData(_leaderboard.Value))
            });

            Groups = groups;

            UpdateLocalCommand = null;
            CanUpdate          = false;
        }
        public void TestDefinitions(string leftSerialized, string rightSerialized,
                                    string expectedDefinition, string expectedOtherDefinition, bool expectedModified)
        {
            var notes = new Dictionary <int, string>();

            var builder = new AchievementBuilder();

            builder.ParseRequirements(Tokenizer.CreateTokenizer(leftSerialized));
            var requirement = builder.ToAchievement().CoreRequirements.First();

            builder = new AchievementBuilder();
            builder.ParseRequirements(Tokenizer.CreateTokenizer(rightSerialized));
            var compareRequirement = builder.ToAchievement().CoreRequirements.First();

            var vmRequirement = new RequirementComparisonViewModel(requirement, compareRequirement, NumberFormat.Decimal, notes);

            Assert.That(vmRequirement.Definition, Is.EqualTo(expectedDefinition));
            Assert.That(vmRequirement.OtherDefinition, Is.EqualTo(expectedOtherDefinition));
            Assert.That(vmRequirement.IsModified, Is.EqualTo(expectedModified));
        }
Exemple #11
0
        public void TestCombine(string input, string expected)
        {
            var achievement = new AchievementBuilder();

            achievement.ParseRequirements(Tokenizer.CreateTokenizer(input));
            var groups = RequirementEx.Combine(achievement.CoreRequirements);

            var builder = new StringBuilder();

            foreach (var group in groups)
            {
                if (builder.Length > 0)
                {
                    builder.Append('|');
                }

                group.AppendString(builder, NumberFormat.Decimal);
            }

            Assert.That(builder.ToString(), Is.EqualTo(expected));

            // make sure we didn't modify the source requirements
            Assert.That(achievement.SerializeRequirements(), Is.EqualTo(input));
        }
Exemple #12
0
        private void ReadAchievement(Tokenizer tokenizer)
        {
            var achievement = new AchievementBuilder();

            var part = tokenizer.ReadTo(':'); // id

            int num;

            if (Int32.TryParse(part.ToString(), out num))
            {
                achievement.Id = num;
            }
            tokenizer.Advance();

            if (tokenizer.NextChar == '"')
            {
                var requirements = tokenizer.ReadQuotedString();
                achievement.ParseRequirements(Tokenizer.CreateTokenizer(requirements));
            }
            else
            {
                achievement.ParseRequirements(tokenizer);
            }
            tokenizer.Advance();

            if (tokenizer.NextChar == '"')
            {
                achievement.Title = tokenizer.ReadQuotedString().ToString();
            }
            else
            {
                achievement.Title = tokenizer.ReadTo(':').ToString();
            }
            tokenizer.Advance();

            if (tokenizer.NextChar == '"')
            {
                achievement.Description = tokenizer.ReadQuotedString().ToString();
            }
            else
            {
                achievement.Description = tokenizer.ReadTo(':').ToString();
            }
            tokenizer.Advance();

            tokenizer.ReadTo(':'); // deprecated
            tokenizer.Advance();

            tokenizer.ReadTo(':'); // deprecated
            tokenizer.Advance();

            tokenizer.ReadTo(':'); // deprecated
            tokenizer.Advance();

            tokenizer.ReadTo(':'); // author
            tokenizer.Advance();

            part = tokenizer.ReadTo(':'); // points
            if (Int32.TryParse(part.ToString(), out num))
            {
                achievement.Points = num;
            }
            tokenizer.Advance();

            var published = tokenizer.ReadTo(':'); // created timestamp

            tokenizer.Advance();

            var updated = tokenizer.ReadTo(':'); // updated timestamp

            tokenizer.Advance();

            tokenizer.ReadTo(':'); // upvotes
            tokenizer.Advance();

            tokenizer.ReadTo(':'); // downvotes
            tokenizer.Advance();

            if (tokenizer.NextChar == '"')
            {
                achievement.BadgeName = tokenizer.ReadQuotedString().ToString();
            }
            else
            {
                achievement.BadgeName = tokenizer.ReadTo(':').ToString();
            }
            if (achievement.BadgeName.EndsWith("_lock"))
            {
                achievement.BadgeName.Remove(achievement.BadgeName.Length - 5);
            }

            var builtAchievement = achievement.ToAchievement();

            if (published != "0" && Int32.TryParse(published.ToString(), out num))
            {
                builtAchievement.Published = _unixEpoch.AddSeconds(num);
            }
            if (updated != "0" && Int32.TryParse(updated.ToString(), out num))
            {
                builtAchievement.LastModified = _unixEpoch.AddSeconds(num);
            }

            _achievements.Add(builtAchievement);
        }
Exemple #13
0
        private void Read()
        {
            if (!_fileSystemService.FileExists(_filename))
            {
                return;
            }

            var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

            using (var reader = new StreamReader(_fileSystemService.OpenFile(_filename, OpenFileMode.Read)))
            {
                _version = reader.ReadLine();
                Title    = reader.ReadLine();

                while (!reader.EndOfStream)
                {
                    var line        = reader.ReadLine();
                    var tokenizer   = Tokenizer.CreateTokenizer(line);
                    var achievement = new AchievementBuilder();

                    var part = tokenizer.ReadTo(':'); // id

                    if (part.StartsWith("L"))         // ignore leaderboards in N64 file
                    {
                        continue;
                    }

                    int num;
                    if (Int32.TryParse(part.ToString(), out num))
                    {
                        achievement.Id = num;
                    }
                    tokenizer.Advance();

                    if (tokenizer.NextChar == '"')
                    {
                        var requirements = tokenizer.ReadQuotedString();
                        achievement.ParseRequirements(Tokenizer.CreateTokenizer(requirements));
                    }
                    else
                    {
                        achievement.ParseRequirements(tokenizer);
                    }
                    tokenizer.Advance();

                    if (tokenizer.NextChar == '"')
                    {
                        achievement.Title = tokenizer.ReadQuotedString().ToString();
                    }
                    else
                    {
                        achievement.Title = tokenizer.ReadTo(':').ToString();
                    }
                    tokenizer.Advance();

                    if (tokenizer.NextChar == '"')
                    {
                        achievement.Description = tokenizer.ReadQuotedString().ToString();
                    }
                    else
                    {
                        achievement.Description = tokenizer.ReadTo(':').ToString();
                    }
                    tokenizer.Advance();

                    tokenizer.ReadTo(':'); // deprecated
                    tokenizer.Advance();

                    tokenizer.ReadTo(':'); // deprecated
                    tokenizer.Advance();

                    tokenizer.ReadTo(':'); // deprecated
                    tokenizer.Advance();

                    tokenizer.ReadTo(':'); // author
                    tokenizer.Advance();

                    part = tokenizer.ReadTo(':'); // points
                    if (Int32.TryParse(part.ToString(), out num))
                    {
                        achievement.Points = num;
                    }
                    tokenizer.Advance();

                    var published = tokenizer.ReadTo(':'); // created timestamp
                    tokenizer.Advance();

                    var updated = tokenizer.ReadTo(':'); // updated timestamp
                    tokenizer.Advance();

                    tokenizer.ReadTo(':'); // upvotes
                    tokenizer.Advance();

                    tokenizer.ReadTo(':'); // downvotes
                    tokenizer.Advance();

                    achievement.BadgeName = tokenizer.ReadTo(':').ToString();
                    if (achievement.BadgeName.EndsWith("_lock"))
                    {
                        achievement.BadgeName.Remove(achievement.BadgeName.Length - 5);
                    }

                    var builtAchievement = achievement.ToAchievement();
                    if (published != "0" && Int32.TryParse(published.ToString(), out num))
                    {
                        builtAchievement.Published = unixEpoch.AddSeconds(num);
                    }
                    if (updated != "0" && Int32.TryParse(updated.ToString(), out num))
                    {
                        builtAchievement.LastModified = unixEpoch.AddSeconds(num);
                    }

                    _achievements.Add(builtAchievement);
                }
            }
        }
Exemple #14
0
        private void ReadPublished()
        {
            _publishedAchievements.Clear();
            _publishedLeaderboards.Clear();

            var fileName = Path.Combine(RACacheDirectory, GameId + ".json");

            using (var stream = _fileSystemService.OpenFile(fileName, OpenFileMode.Read))
            {
                if (stream == null)
                {
                    return;
                }

                var publishedData = new JsonObject(stream);
                Title = publishedData.GetField("Title").StringValue;

                var publishedAchievements = publishedData.GetField("Achievements");
                var coreCount             = 0;
                var corePoints            = 0;
                var unofficialCount       = 0;
                var unofficialPoints      = 0;
                if (publishedAchievements.Type == JsonFieldType.ObjectArray)
                {
                    foreach (var publishedAchievement in publishedAchievements.ObjectArrayValue)
                    {
                        var builder = new AchievementBuilder();
                        builder.Id          = publishedAchievement.GetField("ID").IntegerValue.GetValueOrDefault();
                        builder.Title       = publishedAchievement.GetField("Title").StringValue;
                        builder.Description = publishedAchievement.GetField("Description").StringValue;
                        builder.Points      = publishedAchievement.GetField("Points").IntegerValue.GetValueOrDefault();
                        builder.BadgeName   = publishedAchievement.GetField("BadgeName").StringValue;
                        builder.ParseRequirements(Tokenizer.CreateTokenizer(publishedAchievement.GetField("MemAddr").StringValue));

                        var builtAchievement = builder.ToAchievement();
                        builtAchievement.Published    = UnixEpoch.AddSeconds(publishedAchievement.GetField("Created").IntegerValue.GetValueOrDefault());
                        builtAchievement.LastModified = UnixEpoch.AddSeconds(publishedAchievement.GetField("Modified").IntegerValue.GetValueOrDefault());

                        builtAchievement.Category = publishedAchievement.GetField("Flags").IntegerValue.GetValueOrDefault();
                        if (builtAchievement.Category == 5)
                        {
                            _publishedAchievements.Add(builtAchievement);
                            unofficialCount++;
                            unofficialPoints += builtAchievement.Points;
                        }
                        else if (builtAchievement.Category == 3)
                        {
                            _publishedAchievements.Add(builtAchievement);
                            coreCount++;
                            corePoints += builtAchievement.Points;
                        }
                    }
                }

                var publishedLeaderboards = publishedData.GetField("Leaderboards");
                if (publishedLeaderboards.Type == JsonFieldType.ObjectArray)
                {
                    foreach (var publishedLeaderboard in publishedLeaderboards.ObjectArrayValue)
                    {
                        var leaderboard = new Leaderboard();
                        leaderboard.Id            = publishedLeaderboard.GetField("ID").IntegerValue.GetValueOrDefault();
                        leaderboard.Title         = publishedLeaderboard.GetField("Title").StringValue;
                        leaderboard.Description   = publishedLeaderboard.GetField("Description").StringValue;
                        leaderboard.Format        = Leaderboard.ParseFormat(publishedLeaderboard.GetField("Format").StringValue);
                        leaderboard.LowerIsBetter = publishedLeaderboard.GetField("LowerIsBetter").BooleanValue;

                        var mem       = publishedLeaderboard.GetField("Mem").StringValue;
                        var tokenizer = Tokenizer.CreateTokenizer(mem);
                        while (tokenizer.NextChar != '\0')
                        {
                            var part = tokenizer.ReadTo("::");
                            if (part.StartsWith("STA:"))
                            {
                                leaderboard.Start = part.Substring(4);
                            }
                            else if (part.StartsWith("CAN:"))
                            {
                                leaderboard.Cancel = part.Substring(4);
                            }
                            else if (part.StartsWith("SUB:"))
                            {
                                leaderboard.Submit = part.Substring(4);
                            }
                            else if (part.StartsWith("VAL:"))
                            {
                                leaderboard.Value = part.Substring(4);
                            }

                            tokenizer.Advance(2);
                        }

                        _publishedLeaderboards.Add(leaderboard);
                    }
                }

                CoreAchievementCount        = coreCount;
                CoreAchievementPoints       = corePoints;
                UnofficialAchievementCount  = unofficialCount;
                UnofficialAchievementPoints = unofficialPoints;

                _logger.WriteVerbose(String.Format("Identified {0} core achievements ({1} points)", coreCount, corePoints));
                _logger.WriteVerbose(String.Format("Identified {0} unofficial achievements ({1} points)", unofficialCount, unofficialPoints));
            }
        }
Exemple #15
0
 private static Achievement CreateAchievement(string definition)
 {
     var achievementBuilder = new AchievementBuilder();
     achievementBuilder.ParseRequirements(Tokenizer.CreateTokenizer(definition));
     return achievementBuilder.ToAchievement();
 }
Exemple #16
0
        private void MergePublished(int gameId, List <GeneratedItemViewModelBase> achievements)
        {
            var fileName = Path.Combine(RACacheDirectory, gameId + ".json");

            if (!File.Exists(fileName))
            {
                fileName = Path.Combine(RACacheDirectory, gameId + ".txt");
                if (!File.Exists(fileName))
                {
                    return;
                }
            }

            using (var stream = File.OpenRead(fileName))
            {
                var publishedData = new JsonObject(stream);
                Title = publishedData.GetField("Title").StringValue;

                var publishedAchievements = publishedData.GetField("Achievements");
                var coreCount             = 0;
                var corePoints            = 0;
                var unofficialCount       = 0;
                var unofficialPoints      = 0;
                foreach (var publishedAchievement in publishedAchievements.ObjectArrayValue)
                {
                    var points = publishedAchievement.GetField("Points").IntegerValue.GetValueOrDefault();
                    var title  = publishedAchievement.GetField("Title").StringValue;

                    var id          = publishedAchievement.GetField("ID").IntegerValue.GetValueOrDefault();
                    var achievement = achievements.OfType <GeneratedAchievementViewModel>().FirstOrDefault(a => a.Generated.Id == id);
                    if (achievement == null)
                    {
                        achievement = achievements.OfType <GeneratedAchievementViewModel>().FirstOrDefault(a => String.Compare(a.Generated.Title.Text, title, StringComparison.CurrentCultureIgnoreCase) == 0);
                        if (achievement == null)
                        {
                            achievement = new GeneratedAchievementViewModel(this, null);
                            achievements.Add(achievement);
                        }
                    }

                    var builder = new AchievementBuilder();
                    builder.Id          = id;
                    builder.Title       = title;
                    builder.Description = publishedAchievement.GetField("Description").StringValue;
                    builder.Points      = publishedAchievement.GetField("Points").IntegerValue.GetValueOrDefault();
                    builder.BadgeName   = publishedAchievement.GetField("BadgeName").StringValue;
                    builder.ParseRequirements(Tokenizer.CreateTokenizer(publishedAchievement.GetField("MemAddr").StringValue));

                    var builtAchievement = builder.ToAchievement();
                    builtAchievement.Published    = UnixEpoch.AddSeconds(publishedAchievement.GetField("Created").IntegerValue.GetValueOrDefault());
                    builtAchievement.LastModified = UnixEpoch.AddSeconds(publishedAchievement.GetField("Modified").IntegerValue.GetValueOrDefault());

                    if (publishedAchievement.GetField("Flags").IntegerValue == 5)
                    {
                        achievement.Unofficial.LoadAchievement(builtAchievement);
                        unofficialCount++;
                        unofficialPoints += points;
                    }
                    else
                    {
                        achievement.Core.LoadAchievement(builtAchievement);
                        coreCount++;
                        corePoints += points;
                    }
                }

                CoreAchievementCount        = coreCount;
                CoreAchievementPoints       = corePoints;
                UnofficialAchievementCount  = unofficialCount;
                UnofficialAchievementPoints = unofficialPoints;

                _logger.WriteVerbose(String.Format("Merged {0} core achievements ({1} points)", coreCount, corePoints));
                _logger.WriteVerbose(String.Format("Merged {0} unofficial achievements ({1} points)", unofficialCount, unofficialPoints));
            }
        }