-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.cs
158 lines (127 loc) · 5.21 KB
/
Plugin.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
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
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace EmbyParty
{
public class Plugin : BasePlugin<PluginConfiguration>
{
private string[] extractResourcesNames = {
"EmbyParty.dashboard_ui"
};
private string _resourcesPath;
private string _pluginDataPath;
private ILogger _logger;
private List<Action<PartyManager>> _partyManagerSetHandlers = new List<Action<PartyManager>>();
private PartyManager _partyManager;
public PartyManager PartyManager {
get => _partyManager;
set {
_partyManager = value;
foreach (Action<PartyManager> action in _partyManagerSetHandlers)
{
action(value);
}
_partyManagerSetHandlers.Clear();
}
}
public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer, ILogManager logManager) : base(applicationPaths, xmlSerializer)
{
_resourcesPath = applicationPaths.ProgramSystemPath;
_logger = logManager.GetLogger("Party");
_pluginDataPath = Path.Combine(applicationPaths.DataPath, "EmbyParty");
}
public override string Name => "Emby Party";
public override string Description => "Easy to use solution for synchronizing user playback experiences";
public override Guid Id => new Guid("A7572F2B-B3E5-4055-945D-640A57D2C09B");
public void OnPartyManagerSet(Action<PartyManager> action)
{
if (PartyManager != null)
{
action(PartyManager);
}
else
{
_partyManagerSetHandlers.Add(action);
}
}
public override void OnUninstalling()
{
base.OnUninstalling();
CleanupResources(true);
}
public void ExtractResources()
{
Assembly assembly = Assembly.GetAssembly(typeof(Plugin));
bool update = ReadResourceVersion() != Version.ToString();
foreach (string resourceName in assembly.GetManifestResourceNames())
{
if (extractResourcesNames.Where(prefix => resourceName.StartsWith(prefix)).Count() == 0) { continue; }
string outputPath = Path.Combine(_resourcesPath, ConvertResourceNameToPath(assembly, resourceName));
if (!update && File.Exists(outputPath)) { continue; }
_logger.Info("Extracting resource " + resourceName);
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName))
using (FileStream fileStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
{
resourceStream?.CopyTo(fileStream);
}
}
if (update) { WriteResourceVersion(); }
}
private void CleanupResources(bool andDirectory)
{
Assembly assembly = Assembly.GetAssembly(typeof(Plugin));
foreach (string resourceName in assembly.GetManifestResourceNames())
{
if (extractResourcesNames.Where(prefix => resourceName.StartsWith(prefix)).Count() == 0) { continue; }
string outputPath = Path.Combine(_resourcesPath, ConvertResourceNameToPath(assembly, resourceName));
_logger.Info("Deleting resource " + resourceName);
File.Delete(outputPath);
string dir = Path.GetDirectoryName(outputPath);
if (andDirectory && Directory.GetFileSystemEntries(dir).Length == 0)
{
Directory.Delete(dir);
}
}
}
private string ConvertResourceNameToPath(Assembly assembly, string resourceName)
{
string assemblyNamespace = assembly.GetName().Name;
string relativePath = resourceName.Replace(assemblyNamespace + ".", "");
int lastDotIndex = relativePath.LastIndexOf('.');
if (lastDotIndex > -1)
{
relativePath = relativePath.Substring(0, lastDotIndex)
.Replace('.', Path.DirectorySeparatorChar)
.Replace('_', '-')
+ relativePath.Substring(lastDotIndex);
}
return relativePath;
}
private void WriteResourceVersion()
{
Directory.CreateDirectory(_pluginDataPath);
File.WriteAllText(Path.Combine(_pluginDataPath, "rversion.txt"), Version.ToString());
}
private string ReadResourceVersion()
{
try
{
return File.ReadAllText(Path.Combine(_pluginDataPath, "rversion.txt"));
}
catch
{
return "";
}
}
}
}