Repository navigation
Expand file tree
/
Copy pathQuickAddPlugin.cs
More file actions
169 lines (149 loc) · 6.95 KB
/
Copy pathQuickAddPlugin.cs
File metadata and controls
169 lines (149 loc) · 6.95 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
160
161
162
163
164
165
166
167
168
169
using FullscreenKit;
using Playnite.SDK;
using Playnite.SDK.Events;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
namespace QuickAdd
{
public class QuickAddPlugin : GenericPlugin
{
static readonly string[] Exts = { ".exe", ".lnk", ".bat", ".cmd" };
public override Guid Id { get; } = new Guid("c1a5e0d2-7b3f-4e6a-9d18-5f2b8a4c7e91");
public QuickAddPlugin(IPlayniteAPI api) : base(api) { FileJobManager.Init(api); }
public override IEnumerable<MainMenuItem> GetMainMenuItems(GetMainMenuItemsArgs args)
{
yield return new MainMenuItem
{
Description = "Add a game or app...",
MenuSection = "Quick Add",
Action = a => AddLoop()
};
}
// Fullscreen controller input goes straight to the picker (no virtual cursor).
public override void OnControllerButtonStateChanged(OnControllerButtonStateChangedArgs args)
{
PickerHost.RouteController(args.Button, args.State == ControllerInputState.Pressed);
}
public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
{
PickerHost.Configure(PlayniteApi, GetPluginUserDataPath());
SafeIconExtractor.Cleanup(GetPluginUserDataPath()); // only removes this plugin's own leftovers
}
void AddLoop()
{
PickerHost.Configure(PlayniteApi, GetPluginUserDataPath()); // idempotent safety net
LibraryIndex.Refresh(PlayniteApi);
var paths = PickerHost.PickFiles(PlayniteApi, "Quick Add", Exts, null, true, true, true, true, true, true);
if (paths == null || paths.Count == 0) return;
var settings = QuickSettings.Current;
var added = new List<Game>();
for (int i = 0; i < paths.Count; i++)
{
var game = AddFromPath(paths[i], i + 1, paths.Count, settings.AskNamesAdd);
if (game != null) added.Add(game);
}
if (added.Count == 0) return;
Notify((added.Count == 1 ? "Quick Add: 1 game added." : "Quick Add: " + added.Count + " games added.") +
" Update your Playnite library to download their metadata.");
if (settings.SelectAdded)
{
// so they are easy to find when you update the library / download metadata in Playnite
try { PlayniteApi.MainView.SelectGames(added.Select(g => g.Id)); } catch { }
}
}
void Notify(string text)
{
PlayniteApi.Notifications.Add(new NotificationMessage(Guid.NewGuid().ToString(), text, NotificationType.Info));
}
Game AddFromPath(string path, int index, int total, bool askName)
{
try
{
string target = path, args = null, workDir = null;
if (string.Equals(Path.GetExtension(path), ".lnk", StringComparison.OrdinalIgnoreCase))
{
if (ResolveShortcut(path, out var t, out var a, out var w) && !string.IsNullOrEmpty(t) && File.Exists(t))
{
target = t; args = a; workDir = w;
}
}
// the same executable can't be in the library twice
if (LibraryIndex.Contains(target) || LibraryIndex.Contains(path))
{
PlayniteApi.Dialogs.ShowMessage(
"This executable is already present in the library and can't be added again:\n" + Path.GetFileName(target),
"Quick Add");
return null;
}
var dir = string.IsNullOrEmpty(workDir) ? Path.GetDirectoryName(target) : workDir;
var name = SuggestName(path, target);
if (askName)
{
var message = total > 1 ? "Name of the game / app (" + index + " of " + total + "):" : "Name of the game / app:";
var input = PlayniteApi.Dialogs.SelectString(message, "Add game", name);
if (!input.Result || string.IsNullOrWhiteSpace(input.SelectedString)) return null; // Cancel skips this one
name = input.SelectedString.Trim();
}
var game = new Game(name)
{
IsInstalled = true,
InstallDirectory = dir,
Added = DateTime.Now,
GameActions = new ObservableCollection<GameAction>
{
new GameAction
{
Type = GameActionType.File, Name = name, Path = target,
Arguments = args, WorkingDir = dir, IsPlayAction = true
}
}
};
var pc = PlayniteApi.Database.Platforms.FirstOrDefault(p => p.SpecificationId == "pc_windows");
if (pc != null) game.PlatformIds = new List<Guid> { pc.Id };
var icon = SafeIconExtractor.ImportIcon(PlayniteApi, GetPluginUserDataPath(), target, game.Id);
if (!string.IsNullOrEmpty(icon)) game.Icon = icon;
PlayniteApi.Database.Games.Add(game);
LibraryIndex.Add(target);
return game;
}
catch (Exception ex)
{
PlayniteApi.Dialogs.ShowErrorMessage(ex.Message, "Quick Add");
return null;
}
}
static string SuggestName(string path, string target)
{
try
{
if (string.Equals(Path.GetExtension(path), ".lnk", StringComparison.OrdinalIgnoreCase))
return Path.GetFileNameWithoutExtension(path);
var product = FileVersionInfo.GetVersionInfo(target).ProductName;
if (!string.IsNullOrWhiteSpace(product)) return product.Trim();
}
catch { }
return Path.GetFileNameWithoutExtension(target);
}
static bool ResolveShortcut(string lnk, out string target, out string args, out string workDir)
{
target = args = workDir = null;
try
{
var shell = Activator.CreateInstance(Type.GetTypeFromProgID("WScript.Shell"));
var sc = shell.GetType().InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, shell, new object[] { lnk });
string Get(string p) => sc.GetType().InvokeMember(p, BindingFlags.GetProperty, null, sc, null) as string;
target = Get("TargetPath"); args = Get("Arguments"); workDir = Get("WorkingDirectory");
return true;
}
catch { return false; }
}
}
}