Example #1
0
        public void Should_parse_mod_file_archive()
        {
            DISetup.SetupContainer();

            var sb = new StringBuilder();

            sb.AppendLine(@"name=""AI Species Limit""");
            sb.AppendLine(@"archive=""path""");
            sb.AppendLine(@"user_dir=""dir""");
            sb.AppendLine(@"replace_path=""replace""");
            sb.AppendLine(@"tags={");
            sb.AppendLine(@"	""Gameplay""");
            sb.AppendLine(@"	""Fixes""");
            sb.AppendLine(@"}");
            sb.AppendLine(@"picture=""thumbnail.png""");
            sb.AppendLine(@"supported_version=""2.5.*""");
            sb.AppendLine(@"remote_file_id=""1830063425""");
            sb.AppendLine(@"version = ""version""");
            sb.AppendLine(@"dependencies = {");
            sb.AppendLine(@"	""fake""");
            sb.AppendLine(@"}");

            var parser = new ModParser(new CodeParser(new Logger()));
            var result = parser.Parse(sb.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries));

            result.FileName.Should().Be("path");
        }
Example #2
0
        public void Should_parse_mod_file()
        {
            DISetup.SetupContainer();

            var sb = new StringBuilder();

            sb.AppendLine(@"name=""AI Species Limit""");
            sb.AppendLine(@"path=""path""");
            sb.AppendLine(@"user_dir=""dir""");
            sb.AppendLine(@"replace_path=""replace""");
            sb.AppendLine(@"tags={");
            sb.AppendLine(@"	""Gameplay""");
            sb.AppendLine(@"	""Fixes""");
            sb.AppendLine(@"}");
            sb.AppendLine(@"picture=""thumbnail.png""");
            sb.AppendLine(@"supported_version=""2.5.*""");
            sb.AppendLine(@"remote_file_id=""1830063425""");
            sb.AppendLine(@"version = ""version""");
            sb.AppendLine(@"dependencies = {");
            sb.AppendLine(@"	""fake""");
            sb.AppendLine(@"}");

            var parser = new ModParser(new CodeParser(new Logger()));
            var result = parser.Parse(sb.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries));

            result.Dependencies.Count().Should().Be(1);
            result.Dependencies.First().Should().Be("fake");
            result.FileName.Should().Be("path");
            result.Name.Should().Be("AI Species Limit");
            result.Picture.Should().Be("thumbnail.png");
            result.RemoteId.Should().Be(1830063425);
            result.Tags.Count().Should().Be(2);
            result.Tags.First().Should().Be("Gameplay");
            result.Tags.Last().Should().Be("Fixes");
            result.Version.Should().Be("2.5.*");
            result.UserDir.Count().Should().Be(1);
            result.UserDir.FirstOrDefault().Should().Be("dir");
            result.ReplacePath.Count().Should().Be(1);
            result.ReplacePath.FirstOrDefault().Should().Be("replace");
        }
Example #3
0
        public static bool Parse(SyntaxContext context, int position)
        {
            var list     = context.list;
            var offset   = 0;
            var index    = position;
            var count    = 0;
            var isMissed = false;

            while (ParenParser.Parse(context, index))
            {
                ;
            }
            while (TableIParser.Parse(context, index))
            {
                ;
            }
            while (TableSParser.Parse(context, index))
            {
                ;
            }
            while (ListParser.Parse(context, index))
            {
                ;
            }
            while (PropertyParser.Parse(context, index))
            {
                ;
            }
            while (IndexParser.Parse(context, index))
            {
                ;
            }
            while (CallParser.Parse(context, index))
            {
                ;
            }
            while (NotParser.Parse(context, index))
            {
                ;
            }
            while (LengthParser.Parse(context, index))
            {
                ;
            }
            while (NegateParser.Parse(context, index))
            {
                ;
            }
            while (PowerParser.Parse(context, index))
            {
                ;
            }
            while (MultiplyParser.Parse(context, index))
            {
                ;
            }
            while (DivisionParser.Parse(context, index))
            {
                ;
            }
            while (ModParser.Parse(context, index))
            {
                ;
            }
            while (AddParser.Parse(context, index))
            {
                ;
            }
            while (SubtractParser.Parse(context, index))
            {
                ;
            }
            if (!list[index].isRightValue)
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            if (!ParserHelper.IsOperator(list[index], "<"))
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            while (ParenParser.Parse(context, index))
            {
                ;
            }
            while (TableIParser.Parse(context, index))
            {
                ;
            }
            while (TableSParser.Parse(context, index))
            {
                ;
            }
            while (ListParser.Parse(context, index))
            {
                ;
            }
            while (PropertyParser.Parse(context, index))
            {
                ;
            }
            while (IndexParser.Parse(context, index))
            {
                ;
            }
            while (CallParser.Parse(context, index))
            {
                ;
            }
            while (NotParser.Parse(context, index))
            {
                ;
            }
            while (LengthParser.Parse(context, index))
            {
                ;
            }
            while (NegateParser.Parse(context, index))
            {
                ;
            }
            while (PowerParser.Parse(context, index))
            {
                ;
            }
            while (MultiplyParser.Parse(context, index))
            {
                ;
            }
            while (DivisionParser.Parse(context, index))
            {
                ;
            }
            while (ModParser.Parse(context, index))
            {
                ;
            }
            while (AddParser.Parse(context, index))
            {
                ;
            }
            while (SubtractParser.Parse(context, index))
            {
                ;
            }
            if (!list[index].isRightValue)
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            context.Insert(position, ExpressionCreator.CreateLess(list, position, offset));
            context.Remove(position + 1, offset);
            return(true);
        }
Example #4
0
        /// <summary>
        /// Gets the installed mods internal.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <param name="ignorePatchMods">if set to <c>true</c> [ignore patch mods].</param>
        /// <returns>IEnumerable&lt;IMod&gt;.</returns>
        /// <exception cref="ArgumentNullException">game</exception>
        protected virtual IEnumerable <IMod> GetInstalledModsInternal(IGame game, bool ignorePatchMods)
        {
            if (game == null)
            {
                throw new ArgumentNullException("game");
            }
            var mods = Cache.Get <IEnumerable <IMod> >(ModsCachePrefix, ConstructModsCacheKey(game, ignorePatchMods));

            if (mods != null)
            {
                return(mods);
            }
            else
            {
                var result        = new List <IMod>();
                var installedMods = Reader.Read(Path.Combine(game.UserDirectory, Shared.Constants.ModDirectory));
                if (installedMods?.Count() > 0)
                {
                    foreach (var installedMod in installedMods.Where(p => p.Content.Count() > 0))
                    {
                        var mod = Mapper.Map <IMod>(ModParser.Parse(installedMod.Content));
                        if (ignorePatchMods && IsPatchModInternal(mod))
                        {
                            continue;
                        }
                        mod.DescriptorFile = $"{Shared.Constants.ModDirectory}/{installedMod.FileName}";
                        mod.Source         = GetModSource(installedMod);
                        if (mod.Source == ModSource.Paradox)
                        {
                            mod.RemoteId = GetPdxModId(installedMod.FileName);
                        }
                        if (string.IsNullOrWhiteSpace(mod.FileName))
                        {
                            mod.FileName = string.Empty;
                            mod.FullPath = string.Empty;
                        }
                        else
                        {
                            if (Path.IsPathFullyQualified(mod.FileName))
                            {
                                mod.FullPath = mod.FileName;
                            }
                            else
                            {
                                // Check user directory and workshop directory.
                                var userDirectoryMod     = Path.Combine(game.UserDirectory, mod.FileName);
                                var workshopDirectoryMod = Path.Combine(game.WorkshopDirectory, mod.FileName);
                                if (File.Exists(userDirectoryMod) || Directory.Exists(userDirectoryMod))
                                {
                                    mod.FullPath = userDirectoryMod;
                                }
                                else if (File.Exists(workshopDirectoryMod) || Directory.Exists(workshopDirectoryMod))
                                {
                                    mod.FullPath = workshopDirectoryMod;
                                }
                            }
                        }
                        // Validate if path exists
                        mod.IsValid = File.Exists(mod.FullPath) || Directory.Exists(mod.FullPath);
                        mod.Game    = game.Type;
                        result.Add(mod);
                    }
                }
                Cache.Set <IEnumerable <IMod> >(ModsCachePrefix, ConstructModsCacheKey(game, ignorePatchMods), result);
                return(result);
            }
        }
Example #5
0
        public static bool Parse(SyntaxContext context, int position)
        {
            var list     = context.list;
            var offset   = 0;
            var index    = position;
            var count    = 0;
            var isMissed = false;

            if (!ParserHelper.IsKeyword(list[index], "for"))
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            if (list[index].type != Expression.Type.Word)
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            if (!ParserHelper.IsOperator(list[index], "="))
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            while (FunctionAParser.Parse(context, index))
            {
                ;
            }
            while (ParenParser.Parse(context, index))
            {
                ;
            }
            while (TableIParser.Parse(context, index))
            {
                ;
            }
            while (TableSParser.Parse(context, index))
            {
                ;
            }
            while (ListParser.Parse(context, index))
            {
                ;
            }
            while (PropertyParser.Parse(context, index))
            {
                ;
            }
            while (IndexParser.Parse(context, index))
            {
                ;
            }
            while (CallParser.Parse(context, index))
            {
                ;
            }
            while (NotParser.Parse(context, index))
            {
                ;
            }
            while (LengthParser.Parse(context, index))
            {
                ;
            }
            while (NegateParser.Parse(context, index))
            {
                ;
            }
            while (PowerParser.Parse(context, index))
            {
                ;
            }
            while (MultiplyParser.Parse(context, index))
            {
                ;
            }
            while (DivisionParser.Parse(context, index))
            {
                ;
            }
            while (ModParser.Parse(context, index))
            {
                ;
            }
            while (AddParser.Parse(context, index))
            {
                ;
            }
            while (SubtractParser.Parse(context, index))
            {
                ;
            }
            while (ConcatParser.Parse(context, index))
            {
                ;
            }
            while (LessParser.Parse(context, index))
            {
                ;
            }
            while (GreaterParser.Parse(context, index))
            {
                ;
            }
            while (LessEqualParser.Parse(context, index))
            {
                ;
            }
            while (GreaterEqualParser.Parse(context, index))
            {
                ;
            }
            while (EqualParser.Parse(context, index))
            {
                ;
            }
            while (NotEqualParser.Parse(context, index))
            {
                ;
            }
            while (AndParser.Parse(context, index))
            {
                ;
            }
            while (OrParser.Parse(context, index))
            {
                ;
            }
            if (!list[index].isRightValue)
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            if (!ParserHelper.IsOperator(list[index], ","))
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            while (FunctionAParser.Parse(context, index))
            {
                ;
            }
            while (ParenParser.Parse(context, index))
            {
                ;
            }
            while (TableIParser.Parse(context, index))
            {
                ;
            }
            while (TableSParser.Parse(context, index))
            {
                ;
            }
            while (ListParser.Parse(context, index))
            {
                ;
            }
            while (PropertyParser.Parse(context, index))
            {
                ;
            }
            while (IndexParser.Parse(context, index))
            {
                ;
            }
            while (CallParser.Parse(context, index))
            {
                ;
            }
            while (NotParser.Parse(context, index))
            {
                ;
            }
            while (LengthParser.Parse(context, index))
            {
                ;
            }
            while (NegateParser.Parse(context, index))
            {
                ;
            }
            while (PowerParser.Parse(context, index))
            {
                ;
            }
            while (MultiplyParser.Parse(context, index))
            {
                ;
            }
            while (DivisionParser.Parse(context, index))
            {
                ;
            }
            while (ModParser.Parse(context, index))
            {
                ;
            }
            while (AddParser.Parse(context, index))
            {
                ;
            }
            while (SubtractParser.Parse(context, index))
            {
                ;
            }
            while (ConcatParser.Parse(context, index))
            {
                ;
            }
            while (LessParser.Parse(context, index))
            {
                ;
            }
            while (GreaterParser.Parse(context, index))
            {
                ;
            }
            while (LessEqualParser.Parse(context, index))
            {
                ;
            }
            while (GreaterEqualParser.Parse(context, index))
            {
                ;
            }
            while (EqualParser.Parse(context, index))
            {
                ;
            }
            while (NotEqualParser.Parse(context, index))
            {
                ;
            }
            while (AndParser.Parse(context, index))
            {
                ;
            }
            while (OrParser.Parse(context, index))
            {
                ;
            }
            if (!list[index].isRightValue)
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            if (!ParserHelper.IsKeyword(list[index], "do"))
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            while (ModuleParser.Parse(context, index))
            {
                ;
            }
            if (list[index].type != Expression.Type.Module)
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            if (!ParserHelper.IsKeyword(list[index], "end"))
            {
                return(false);
            }
            else
            {
                // ignored
            }
            offset += 1;
            index   = position + offset;
            context.Insert(position, ExpressionCreator.CreateFor(list, position, offset));
            context.Remove(position + 1, offset);
            return(true);
        }
        /// <summary>
        /// Gets the installed mods internal.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <param name="ignorePatchMods">if set to <c>true</c> [ignore patch mods].</param>
        /// <returns>IEnumerable&lt;IMod&gt;.</returns>
        /// <exception cref="ArgumentNullException">game</exception>
        protected virtual IEnumerable <IMod> GetInstalledModsInternal(IGame game, bool ignorePatchMods)
        {
            if (game == null)
            {
                throw new ArgumentNullException(nameof(game));
            }
            var mods = Cache.Get <IEnumerable <IMod> >(new CacheGetParameters()
            {
                Region = ModsCacheRegion, Prefix = game.Type, Key = GetModsCacheKey(ignorePatchMods)
            });

            if (mods != null)
            {
                return(mods);
            }
            else
            {
                var result        = new List <IMod>();
                var installedMods = Reader.Read(Path.Combine(game.UserDirectory, Shared.Constants.ModDirectory));
                if (installedMods?.Count() > 0)
                {
                    foreach (var installedMod in installedMods.Where(p => p.Content.Any()))
                    {
                        var mod = Mapper.Map <IMod>(ModParser.Parse(installedMod.Content));
                        if (ignorePatchMods && IsPatchModInternal(mod))
                        {
                            continue;
                        }
                        mod.Name           = string.IsNullOrWhiteSpace(mod.Name) ? string.Empty : mod.Name;
                        mod.IsLocked       = installedMod.IsReadOnly;
                        mod.DescriptorFile = $"{Shared.Constants.ModDirectory}/{installedMod.FileName}";
                        mod.Source         = GetModSource(installedMod);
                        if (mod.Source == ModSource.Paradox)
                        {
                            mod.RemoteId = GetPdxModId(installedMod.FileName);
                        }
                        if (string.IsNullOrWhiteSpace(mod.FileName))
                        {
                            mod.FileName = string.Empty;
                            mod.FullPath = string.Empty;
                        }
                        else
                        {
                            if (Path.IsPathFullyQualified(mod.FileName))
                            {
                                mod.FullPath = mod.FileName.StandardizeDirectorySeparator();
                            }
                            else
                            {
                                // Check user directory and workshop directory.
                                var userDirectoryMod = new List <string>()
                                {
                                    Path.Combine(game.CustomModDirectory, mod.FileName), Path.Combine(game.UserDirectory, mod.FileName)
                                }.GroupBy(p => p).Select(p => p.First());
                                var workshopDirectoryMod = game.WorkshopDirectory.Select(p => Path.Combine(p, mod.FileName)).GroupBy(p => p).Select(p => p.First());
                                if (userDirectoryMod.Any(p => File.Exists(p) || Directory.Exists(p)))
                                {
                                    mod.FullPath = userDirectoryMod.FirstOrDefault(p => File.Exists(p) || Directory.Exists(p)).StandardizeDirectorySeparator();
                                }
                                else if (workshopDirectoryMod.Any(p => File.Exists(p) || Directory.Exists(p)))
                                {
                                    mod.FullPath = workshopDirectoryMod.FirstOrDefault(p => File.Exists(p) || Directory.Exists(p)).StandardizeDirectorySeparator();
                                }
                            }
                        }
                        // Validate if path exists
                        mod.IsValid = File.Exists(mod.FullPath) || Directory.Exists(mod.FullPath);
                        mod.Game    = game.Type;
                        result.Add(mod);
                    }
                }
                Cache.Set(new CacheAddParameters <IEnumerable <IMod> >()
                {
                    Region = ModsCacheRegion, Prefix = game.Type, Key = GetModsCacheKey(ignorePatchMods), Value = result
                });
                return(result);
            }
        }
Example #7
0
            void parseModFiles(string path, ModSource source, bool isDirectory)
            {
                var fileInfo = Reader.GetFileInfo(path, Shared.Constants.DescriptorFile);

                if (fileInfo == null)
                {
                    fileInfo = Reader.GetFileInfo(path, $"*{Shared.Constants.ModExtension}");
                    if (fileInfo == null)
                    {
                        return;
                    }
                }
                var mod = Mapper.Map <IMod>(ModParser.Parse(fileInfo.Content));

                mod.FileName = path.Replace("\\", "/");
                mod.FullPath = path.StandardizeDirectorySeparator();
                mod.IsLocked = fileInfo.IsReadOnly;
                mod.Source   = source;
                var cleanedPath = path;

                if (!isDirectory)
                {
                    cleanedPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
                }

                var localPath = $"{Shared.Constants.ModDirectory}/{cleanedPath.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries).LastOrDefault()}{Shared.Constants.ModExtension}";

                switch (mod.Source)
                {
                case ModSource.Local:
                    setDescriptorPath(mod, localPath, localPath);
                    break;

                case ModSource.Steam:
                    if (mod.RemoteId.GetValueOrDefault() == 0)
                    {
                        if (!isDirectory)
                        {
                            var modParentDirectory = Path.GetDirectoryName(path);
                            mod.RemoteId = GetSteamModId(modParentDirectory, isDirectory);
                        }
                        else
                        {
                            mod.RemoteId = GetSteamModId(path, isDirectory);
                        }
                    }
                    setDescriptorPath(mod, $"{Shared.Constants.ModDirectory}/{Constants.Steam_mod_id}{mod.RemoteId}{Shared.Constants.ModExtension}", localPath);
                    break;

                case ModSource.Paradox:
                    if (!isDirectory)
                    {
                        var modParentDirectory = Path.GetDirectoryName(path);
                        mod.RemoteId = GetPdxModId(modParentDirectory, isDirectory);
                    }
                    else
                    {
                        mod.RemoteId = GetPdxModId(path, isDirectory);
                    }
                    setDescriptorPath(mod, $"{Shared.Constants.ModDirectory}/{Constants.Paradox_mod_id}{mod.RemoteId}{Shared.Constants.ModExtension}", localPath);
                    break;

                default:
                    break;
                }
                mods.Add(mod);
            }