Exemple #1
0
        private async Task <ContentEntryBase> CheckFileNode(FileNode file, CancellationToken cancellation)
        {
            if (file.Parent.NameLowerCase == "fonts")
            {
                if (file.NameLowerCase.EndsWith(FontObject.FontExtension))
                {
                    var id = file.NameLowerCase.ApartFromLast(FontObject.FontExtension);
                    foreach (var bitmapExtension in FontObject.BitmapExtensions)
                    {
                        var bitmap = file.Parent.GetSubFile(id + bitmapExtension);
                        if (bitmap != null)
                        {
                            var fileData = await file.Info.ReadAsync();

                            cancellation.ThrowIfCancellationRequested();

                            var bitmapData = await bitmap.Info.ReadAsync();

                            cancellation.ThrowIfCancellationRequested();

                            if (fileData == null)
                            {
                                throw new MissingContentException();
                            }

                            var icon = new FontObjectBitmap(bitmapData, fileData).GetIcon();

                            byte[] ToBytes(BitmapSource cropped)
                            {
                                var encoder = new PngBitmapEncoder();

                                using (var stream = new MemoryStream()) {
                                    encoder.Frames.Add(BitmapFrame.Create(cropped));
                                    encoder.Save(stream);
                                    return(stream.ToArray());
                                }
                            }

                            return(new FontContentEntry(bitmap.Key, file.NameLowerCase, id, ToBytes(icon)));
                        }
                    }
                }
                else if (file.NameLowerCase.EndsWith(TrueTypeFontObject.FileExtension))
                {
                    return(new TrueTypeFontContentEntry(file.Key, file.Name,
                                                        file.Name.ApartFromLast(TrueTypeFontObject.FileExtension, StringComparison.OrdinalIgnoreCase)));
                }
            }

            if (file.Parent.NameLowerCase == "ppfilters" && file.NameLowerCase.EndsWith(PpFilterObject.FileExtension))
            {
                return(new PpFilterContentEntry(file.Key, file.Name,
                                                file.Name.ApartFromLast(PpFilterObject.FileExtension, StringComparison.OrdinalIgnoreCase)));
            }

            if (file.Parent.NameLowerCase == "driver" && file.NameLowerCase.EndsWith(DriverModelObject.FileExtension))
            {
                return(new DriverModelContentEntry(file.Key, file.Name,
                                                   file.Name.ApartFromLast(DriverModelObject.FileExtension, StringComparison.OrdinalIgnoreCase)));
            }

            if (file.NameLowerCase.EndsWith(@".xaml"))
            {
                var data = await file.Info.ReadAsync();

                if (data == null)
                {
                    throw new MissingContentException();
                }

                var version = CmThemeEntry.GetVersion(data.ToUtf8String(), out var isTheme);
                return(isTheme ? new CmThemeEntry(file.Key, file.Name, version) : null);
            }

            // A system config file?
            if (file.NameLowerCase.EndsWith(@".ini") && file.Parent.NameLowerCase == "cfg" &&
                file.Parent.Parent?.NameLowerCase == "system")
            {
                switch (file.NameLowerCase)
                {
                case "audio_engine.ini":
                case "damage_displayer.ini":
                case "graphics.ini":
                case "hdr.ini":
                case "pitstop.ini":
                case "skidmarks.ini":
                case "tyre_smoke.ini":
                case "tyre_smoke_grass.ini":
                case "vr.ini":
                    return(new SystemConfigEntry(file.Key, file.Name));
                }
            }

            if (file.NameLowerCase == ReshadeSetupEntry.ReshadeFileName && file.Parent.HasSubFile(ReshadeSetupEntry.ReshadeConfigFileName))
            {
                var reshadeEntry = await GetReshadeEntry();

                if (reshadeEntry != null)
                {
                    return(reshadeEntry);
                }

                async Task <ContentEntryBase> GetReshadeEntry()
                {
                    var root          = file.Parent;
                    var configuration = root.GetSubFile(ReshadeSetupEntry.ReshadeConfigFileName);

                    if (configuration == null)
                    {
                        return(null);
                    }

                    var data = await configuration.Info.ReadAsync();

                    if (data == null)
                    {
                        throw new MissingContentException();
                    }

                    var ini     = IniFile.Parse(data.ToUtf8String())["GENERAL"];
                    var presets = ini.GetNonEmpty("PresetFiles")?.Split(',').Select(ToRelativePath).NonNull().ToList();

                    if (presets == null || presets.Count == 0)
                    {
                        return(null);
                    }

                    var resources = ini.GetNonEmpty("EffectSearchPaths")?.Split(',')
                                    .Concat(ini.GetNonEmpty("TextureSearchPaths")?.Split(',') ?? new string[0])
                                    .Select(ToRelativePath).NonNull().ToList() ?? new List <string>();

                    return(new ReshadeSetupEntry(file.Key, presets.JoinToReadableString(), presets.Concat(resources)));

                    string ToRelativePath(string input)
                    {
                        if (!Path.IsPathRooted(input))
                        {
                            return(input);
                        }

                        while (input != null)
                        {
                            var index = input.IndexOfAny(new[] { '/', '\\' });
                            if (index == -1)
                            {
                                return(null);
                            }

                            input = input.Substring(index + 1);
                            if (root.HasSubFile(input))
                            {
                                return(input);
                            }
                        }

                        return(null);
                    }
                }
            }

            return(null);
        }