Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Created ExtensionUpdater
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Aug 13, 2020
1 parent 2fc4564 commit da1d7f6
Show file tree
Hide file tree
Showing 18 changed files with 571 additions and 15 deletions.
6 changes: 3 additions & 3 deletions DLSiteMetadata/DLSiteMetadata.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="extension.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="extension.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Include="icon.png">
Expand Down
5 changes: 4 additions & 1 deletion DLSiteMetadata/extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ Author: erri120
Version: 1.4.0
Module: DLSiteMetadata.dll
Type: MetadataProvider
Icon: icon.png
Icon: icon.png
UpdaterConfig:
GitHubUser: erri120
GitHubRepo: Playnite.Extensions
41 changes: 41 additions & 0 deletions ExtensionUpdater/DTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// /*
// Copyright (C) 2020 erri120
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// */

using System;

namespace ExtensionUpdater
{
[Serializable]
public class PlayniteExtensionConfig
{
public string Name { get; set; }
public string Author { get; set; }
public string Version { get; set; }
public string Module { get; set; }
public string Type { get; set; }
public string Icon { get; set; }

public UpdaterConfig UpdaterConfig { get; set; }
}

[Serializable]
public class UpdaterConfig
{
public string GitHubUser { get; set; }
public string GitHubRepo { get; set; }
}
}
84 changes: 84 additions & 0 deletions ExtensionUpdater/ExtensionUpdater.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AC48FFD8-57E6-47FF-B728-270E3F0B1F37}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ExtensionUpdater</RootNamespace>
<AssemblyName>ExtensionUpdater</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Playnite.SDK, Version=5.2.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\PlayniteSDK.5.2.0\lib\net462\Playnite.SDK.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="YamlDotNet, Version=8.0.0.0, Culture=neutral, PublicKeyToken=ec19458f3c15af5e">
<HintPath>..\packages\YamlDotNet.8.1.2\lib\net45\YamlDotNet.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="DTO.cs" />
<Compile Include="ExtensionUpdaterPlugin.cs" />
<Compile Include="GitHub.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="YamlUtils.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="extension.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Extensions.Common\Extensions.Common.csproj">
<Project>{5ac34e06-706a-4706-84d4-510aa345d7fc}</Project>
<Name>Extensions.Common</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

</Project>
192 changes: 192 additions & 0 deletions ExtensionUpdater/ExtensionUpdaterPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// /*
// Copyright (C) 2020 erri120
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// */

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Extensions.Common;
using Playnite.SDK;
using Playnite.SDK.Plugins;

namespace ExtensionUpdater
{
public class ExtensionUpdaterPlugin : Plugin
{
private readonly IPlayniteAPI _playniteAPI;
private readonly ILogger _logger;
private readonly string _extensionsDirectory;
private readonly CancellationTokenSource _source;
private readonly CancellationToken _token;

public ExtensionUpdaterPlugin(IPlayniteAPI playniteAPI) : base(playniteAPI)
{
_playniteAPI = playniteAPI;
_logger = playniteAPI.CreateLogger();

var applicationPath = _playniteAPI.Paths.ApplicationPath;
var extensionsPath = Path.Combine(applicationPath, "Extensions");
if (!Directory.Exists(extensionsPath))
{
_logger.Error($"Directory {extensionsPath} does not exist!");
}
else
{
_extensionsDirectory = extensionsPath;
}

_source = new CancellationTokenSource();
_token = _source.Token;
}

public override Guid Id { get; } = Guid.Parse("f4a74d1b-44c0-4ea5-a935-2b994a638236");

public override void Dispose()
{
_source.Dispose();
base.Dispose();
}

public override void OnApplicationStopped()
{
_source.Cancel();
}

public override void OnApplicationStarted()
{
Task.Run(() =>
{
if (_extensionsDirectory == null)
{
return;
}

if (_playniteAPI.ApplicationInfo.InOfflineMode)
{
_logger.Info("Application is in offline mode, skipping extension update check");
return;
}

IEnumerable<IGrouping<string, PlayniteExtensionConfig>> configs = Directory
.EnumerateFiles(_extensionsDirectory, "*.yaml", SearchOption.AllDirectories)
.Select(file =>
{
try
{
var config = YamlUtils.FromYaml<PlayniteExtensionConfig>(file);
return config?.UpdaterConfig == null ? null : config;
}
catch (Exception e)
{
_logger.Error(e, $"Exception while trying to deserialize {file}!\n");
}

return null;
}).NotNull().GroupBy(config =>
{
var repo = $"{config.UpdaterConfig.GitHubUser}/{config.UpdaterConfig.GitHubRepo}";
return repo;
});

configs.Do(async group =>
{
var repo = group.Key;
_logger.Info($"Found: {repo}");

List<GitHubRelease> releases =
await GitHub.GetGitHubReleases(repo);

if (releases == null || releases.Count == 0)
{
_logger.Error($"Found no releases for {repo}");
return;
}

var latest = releases[0];
var sLatestVersion = latest.tag_name;

if (sLatestVersion[0] == 'v')
{
sLatestVersion = sLatestVersion.Substring(1);
}

var canParseLatest = Version.TryParse(sLatestVersion, out var latestVersion);
List<Tuple<PlayniteExtensionConfig, bool>> shouldUpdate = group.Select(config =>
{
var canParseCurrent = Version.TryParse(config.Version, out var currentVersion);

if (canParseCurrent)
{
if (canParseLatest)
{
if (currentVersion < latestVersion)
{
_logger.Info($"There is a new version for {config.Name}: {latestVersion}");
return new Tuple<PlayniteExtensionConfig, bool>(config, true);
}

if (currentVersion != latestVersion)
return new Tuple<PlayniteExtensionConfig, bool>(config, false);

_logger.Info($"{config.Name} is up-to-date");
return new Tuple<PlayniteExtensionConfig, bool>(config, false);
}

if (config.Version.Equals(sLatestVersion, StringComparison.OrdinalIgnoreCase))
{
_logger.Info($"{config.Name} is up-to-date");
return new Tuple<PlayniteExtensionConfig, bool>(config, false);
}

_logger.Info($"There is a new version for {config.Name}: {sLatestVersion}");
return new Tuple<PlayniteExtensionConfig, bool>(config, true);
}

if (config.Version.Equals(sLatestVersion, StringComparison.OrdinalIgnoreCase))
{
_logger.Info($"{config.Name} is up-to-date");
return new Tuple<PlayniteExtensionConfig, bool>(config, false);
}

_logger.Info($"There is a new version for {config.Name}: {sLatestVersion}");
return new Tuple<PlayniteExtensionConfig, bool>(config, true);
}).ToList();

if (shouldUpdate.Any(x => x.Item2))
{
var extensionsNameString = shouldUpdate.Select(x => x.Item1.Name)
.Aggregate((x, y) => $"{x},{y}");
var message = $"The following Extension(s) can be updated: {extensionsNameString}. Click this message to download the release.";

_playniteAPI.Notifications.Add(new NotificationMessage(repo, message, NotificationType.Info,
() =>
{
Process.Start(latest.html_url);
}));
}
else
{
_logger.Info($"No Update available for {repo}");
}
});
}, _token);
}
}
}
Loading

0 comments on commit da1d7f6

Please sign in to comment.