-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathATLauncherMetadataProvider.cs
More file actions
159 lines (149 loc) · 7.01 KB
/
ATLauncherMetadataProvider.cs
File metadata and controls
159 lines (149 loc) · 7.01 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using Playnite.SDK;
using Playnite.SDK.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace ATLauncherInstanceImporter
{
public class ATLauncherMetadataProvider : LibraryMetadataProvider
{
private static readonly ILogger logger = LogManager.GetLogger();
private readonly ATLauncherInstanceImporter _plugin;
private readonly bool _resizeCovers;
public ATLauncherMetadataProvider(ATLauncherInstanceImporter plugin, bool resizeCovers)
{
this._plugin = plugin;
this._resizeCovers = resizeCovers;
}
public override GameMetadata GetMetadata(Game game)
{
Models.Instance instance = ATLauncherInstanceImporter.GetInstance(game.InstallDirectory);
//logger.Debug(Playnite.SDK.Data.Serialization.ToJson(instance));
Tuple<MetadataFile, MetadataFile, MetadataFile> imgs = Models.Instance.GetPackImages(instance, game.InstallDirectory, _resizeCovers, _plugin.GetPluginUserDataPath());
var metaData = new GameMetadata()
{
Description = GenerateInstanceDescription(instance),
IsInstalled = true,
Source = new MetadataNameProperty("ATLauncher"),
Developers = instance.GetPackAuthors(),
Links = instance.GetPackLinks(),
ReleaseDate = instance.GetReleaseDate(),
Publishers = instance.GetInstancePublishers(),
Features = new HashSet<MetadataProperty> { new MetadataNameProperty("Single Player"), new MetadataNameProperty("Multiplayer") },
Genres = new HashSet<MetadataProperty> { new MetadataNameProperty("Sandbox"), new MetadataNameProperty("Survival") },
Platforms = new HashSet<MetadataProperty> { GetOS() },
Name = instance.Launcher.Name ?? instance.Launcher.Pack,
Icon = imgs.Item1,
CoverImage = imgs.Item2,
BackgroundImage = imgs.Item3
};
return metaData;
}
/// <summary>
/// Converts formatting from the description property of an instance to a format usable by Playnite
/// </summary>
/// <param name="desc">Description property from instance.json file</param>
/// <returns>The formatted description</returns>
private static string FormatGivenDescription(string desc)
{
string pattern = @"\n";
string substitution = @"<br>";
Regex reg = new Regex(pattern, RegexOptions.Compiled);
return reg.Replace(desc, substitution);
}
/// <summary>
/// Generates the description text for an instance
/// </summary>
/// <param name="instance"><c>Instance</c> object to generate description for</param>
/// <returns>String containing instance description</returns>
public static string GenerateInstanceDescription(Models.Instance instance)
{
//logger.Info($"Generating description for instance {instance.Launcher.Name}");
string description = string.Empty;
if (instance.Launcher.IsVanilla.HasValue && instance.Launcher.IsVanilla.Value)
{
if ((instance.Launcher.LoaderVersion == null || instance.Launcher.LoaderVersion.Type == null) && instance.Launcher.Mods.Count == 0)
{
return $"<h1>{ResourceProvider.GetString("LOCATLauncherVanillaMinecraft")} {instance.McVersion}</h1>";
}
}
if (instance.Launcher.Description != null)
{
description = $"<h2>{FormatGivenDescription(instance.Launcher.Description)}</h2>";
}
description += $"<h1>{ResourceProvider.GetString("LOCATLauncherMinecraftVersion")}: {instance.McVersion}</h1>";
if (instance.Launcher.LoaderVersion != null && instance.Launcher.LoaderVersion.Type != null)
{
description += $"<h1>{ResourceProvider.GetString("LOCATLauncherModLoader")}: {instance.Launcher.LoaderVersion.Type}</h1>";
}
description += $"<h1>{ResourceProvider.GetString("LOCATLauncherContains")} {instance.Launcher.Mods.Count} {ResourceProvider.GetString("LOCATLauncherMods")}</h1>";
if (instance.Launcher.Mods.Count != 0)
{
description += $"<h1>{ResourceProvider.GetString("LOCATLauncherModList")}</h1><hr>";
}
foreach (var mod in instance.Launcher.Mods)
{
description += "<p><h2>";
if (mod.CurseForgeProject != null)
{
description += mod.CurseForgeProject.Links.WebsiteUrl != string.Empty ? $"<a href={mod.CurseForgeProject.Links.WebsiteUrl}>{mod.Name}</a>" : $"{mod.Name}";
}
else if (mod.ModrinthProject != null)
{
description += $"<a href=https://modrinth.com/mod/{mod.ModrinthProject.Slug}>{mod.Name}</a>";
}
else
{
description += $"{mod.Name}";
}
description += "</h2>";
var modAuths = Models.Instance.GetModAuthors(mod);
string authString = ResourceProvider.GetString("LOCATLauncherBy");
//logger.Debug($"{mod.Authors.Count()}");
for (int i = 0; i < modAuths.Count(); i++)
{
if (i == modAuths.Count() - 1 && i != 0)
{
authString += $" {ResourceProvider.GetString("LOCATLauncherAnd")}";
}
authString += " " + $"<i>{modAuths[i]}</i>";
if (modAuths.Count() > 2 && i != modAuths.Count() - 1)
{
authString += $",";
}
}
if (modAuths.Count != 0)
{
description += $"{authString}";
}
description += $"<h3>{mod.Description}</h3></p><br>";
}
return description;
}
/// <summary>
/// Gets the current operating system as a MetadataNameProperty to pass as Platform metadata
/// </summary>
/// <returns>MetadataNameProperty for current operating system</returns>
public static MetadataNameProperty GetOS()
{
int platform = (int)Environment.OSVersion.Platform;
if (platform == 4 || platform == 128)
{
return new MetadataNameProperty("PC (Linux)");
}
if (platform == 6)
{
return new MetadataNameProperty("Macintosh");
}
if (platform == 2)
{
return new MetadataNameProperty("PC (Windows)");
}
return new MetadataNameProperty("Other");
}
}
}