-
Notifications
You must be signed in to change notification settings - Fork 2
/
Playlist.cs
124 lines (112 loc) · 4.39 KB
/
Playlist.cs
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
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.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Media;
namespace Playlist
{
public class Playlist : GenericPlugin
{
private static readonly ILogger logger = LogManager.GetLogger();
private PlaylistViewModel PlaylistViewModel { get; set; }
private PlaylistView PlaylistView { get; set; }
public ObservableCollection<Game> PlaylistGames { get; set; }
private const string playlistPath = "playlist.txt";
public override IEnumerable<SidebarItem> GetSidebarItems()
{
yield return new SidebarItem
{
Title = "Playlist",
Type = SiderbarItemType.View,
Icon = new TextBlock
{
Text = "\ueca6", // Circled play button
FontFamily = ResourceProvider.GetResource("FontIcoFont") as FontFamily,
},
Opened = () => {
if (PlaylistViewModel == null)
{
PlaylistViewModel = new PlaylistViewModel(PlaylistGames, PlayniteApi);
PlaylistView = new PlaylistView(PlaylistViewModel);
}
return PlaylistView;
}
};
}
public override IEnumerable<GameMenuItem> GetGameMenuItems(GetGameMenuItemsArgs args)
{
yield return new GameMenuItem
{
Description = "Add to Playlist",
Icon = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "icon.png"),
Action = (itemArgs) =>
{
foreach (Game game in args.Games)
{
PlaylistGames.AddMissing(game);
}
}
};
}
public override Guid Id { get; } = Guid.Parse("b0313f81-2b86-4eba-9f24-1a727dedbd45");
public Playlist(IPlayniteAPI api) : base(api)
{
// Ensure the library loaded now, relative to the extension DLL.
// If the XAML trys to load it later it will incorrectly load it relative to Playnite's executable
Assembly.Load("GongSolutions.WPF.DragDrop");
}
private IEnumerable<Game> LoadPlaylistFile()
{
string path = Path.Combine(GetPluginUserDataPath(), playlistPath);
if (File.Exists(path))
{
foreach (string guid in File.ReadLines(path))
{
Game game = PlayniteApi.Database.Games.Get(Guid.Parse(guid));
if (game != null)
{
yield return game;
}
}
}
}
private void UpdatePlaylistFile()
{
string path = Path.Combine(GetPluginUserDataPath(), playlistPath);
File.WriteAllLines(path, PlaylistGames.Select((g) => g.Id.ToString()));
}
public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
{
try
{
// Initialization is done inside OnApplicationStarted, otherwise
// loadPlaylistFile runs too early in Playnite's startup and
// cannot call PlayniteApi.Database.Games.Get()
PlaylistGames = new ObservableCollection<Game>(LoadPlaylistFile());
PlaylistGames.CollectionChanged += (sender, changedArgs) =>
{
UpdatePlaylistFile();
};
PlayniteApi.Database.Games.ItemCollectionChanged += (sender, changedArgs) =>
{
foreach (Game game in changedArgs.RemovedItems)
{
PlaylistGames.Remove(game);
}
};
}
catch (Exception e)
{
logger.Error(e, "Error loading PlaylistGames in OnApplicationStarted");
PlayniteApi.Notifications.Add($"{Id}-OnApplicationStarted", $"Playlist extension could not load file: {e.Message}", NotificationType.Error);
}
}
}
}