-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlaynitePropertyHelper.cs
More file actions
93 lines (77 loc) · 3.13 KB
/
PlaynitePropertyHelper.cs
File metadata and controls
93 lines (77 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using Playnite.SDK.Models;
using Playnite.SDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace F95ZoneMetadataProvider
{
public enum PlayniteProperty
{
Features = 0,
Genres = 1,
Tags = 2
}
public static class PlaynitePropertyHelper
{
public static IEnumerable<DatabaseObject>? GetDatabaseCollection(IPlayniteAPI playniteAPI, PlayniteProperty property)
{
return property switch
{
PlayniteProperty.Features => playniteAPI.Database.Features,
PlayniteProperty.Genres => playniteAPI.Database.Genres,
PlayniteProperty.Tags => playniteAPI.Database.Tags,
_ => null
};
}
public static IEnumerable<MetadataProperty>? ConvertValuesToProperties(IPlayniteAPI playniteAPI, IEnumerable<string> values, PlayniteProperty currentProperty)
{
var collection = GetDatabaseCollection(playniteAPI, currentProperty);
if (collection is null) return null;
var metadataProperties = values
.Select(value => (value, collection.Where(x => x.Name is not null).FirstOrDefault(x => x.Name.Equals(value, StringComparison.OrdinalIgnoreCase))))
.Select(tuple =>
{
var (value, property) = tuple;
if (property is not null) return (MetadataProperty)new MetadataIdProperty(property.Id);
return new MetadataNameProperty(value);
})
.ToList();
return metadataProperties;
}
public static IEnumerable<MetadataProperty>? ConvertValuesIfPossible(IPlayniteAPI playniteAPI,
PlayniteProperty settingsProperty, PlayniteProperty currentProperty,
Func<IEnumerable<string>?> getValues)
{
if (settingsProperty != currentProperty) return null;
var values = getValues();
if (values is null) return null;
var properties = ConvertValuesToProperties(playniteAPI, values, settingsProperty);
return properties ?? null;
}
public static IEnumerable<MetadataProperty>? MultiConcat(params IEnumerable<MetadataProperty>?[] enumerables)
{
/*
* To reduce allocations we look for the first enumerable that is not null and use that as the starting point.
* This is more memory efficient than starting with an empty enumerable and appending everything to that.
*/
var start = -1;
for (var i = 0; i < enumerables.Length; i++)
{
if (enumerables[i] is null) continue;
start = i;
break;
}
if (start == -1) return null;
var res = enumerables[start++]!;
for (var i = start; i < enumerables.Length; i++)
{
var cur = enumerables[i];
if (cur is null) continue;
res = res.Concat(cur);
}
return res;
}
}
}