Skip to content

Commit

Permalink
mend
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codes committed Feb 4, 2025
1 parent e1fd8cc commit 16f8cae
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 160 deletions.
7 changes: 7 additions & 0 deletions apps/PlayniteWebPlugin/src/Models/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public Game(IEnumerable<Playnite.SDK.Models.Game> playniteGames, IEnumerable<Pla
}
}

public Game(Guid id)
{
defaultPlatforms = Enumerable.Empty<Platform>();
defaultSource =null;
this.id = id;
}

private IEnumerable<Release> GetReleases(IGrouping<GameSource, Playnite.SDK.Models.Game> groupedBySource)
{

Expand Down
1 change: 1 addition & 0 deletions apps/PlayniteWebPlugin/src/Models/Release.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public RunState RunState
public int? CommunityScore => game.CommunityScore;
public DateTime? LastActivity => game.LastActivity;
public ulong Playtime => game.Playtime;
public string GameId => game.GameId;
public DateTime? Added => game.Added;
public DateTime? Modified => game.Modified;
public ulong PlayCount => game.PlayCount;
Expand Down
8 changes: 5 additions & 3 deletions apps/PlayniteWebPlugin/src/Models/UpdateEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ namespace PlayniteWeb.Models
{
internal class UpdateEntity
{
public string EntityTypeName { get; set; }
public string EntityId { get; set; }
public string EntityType { get; set; }
public Guid EntityId { get; set; }

public IDictionary<string, object> Fields { get; set; }
public string UpdateAction { get; set; }

public IDictionary<string, object> Entity { get; set; }
}
}
80 changes: 39 additions & 41 deletions apps/PlayniteWebPlugin/src/PlayniteWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public PlayniteWeb(IPlayniteAPI api) : base(api)
publisher = new MqttPublisher(mqtt, topicManager, settings.Settings.ClientId);

var deserializer = new ObjectDeserializer();
subscriber = new PlayniteWebSubscriber(publisher, topicManager, deserializer, api);
subscriber = new PlayniteWebSubscriber(publisher, topicManager, deserializer, api, settings.Settings.DeviceId);
Properties = new GenericPluginProperties
{
HasSettings = true
Expand Down Expand Up @@ -430,49 +430,47 @@ private void Subscriber_OnUpdateEntity(object sender, UpdateEntity e)
{
try
{
var id = Guid.Parse(e.EntityId);

if (e.EntityTypeName == "Game") {
if (e.EntityType == "Game") {
var collection = PlayniteApi.Database.Games;
var entity = collection.Get(Guid.Parse(e.EntityId));
entity = entityUpdater.Update(entity, e.Fields);
collection.Update(entity);
}
else if (e.EntityTypeName == "Platform")
{
var collection = PlayniteApi.Database.Platforms;
var entity = collection.Get(Guid.Parse(e.EntityId));
entity = entityUpdater.Update(entity, e.Fields);
collection.Update(entity);
}
else if (e.EntityTypeName == "Genre")
{
var collection = PlayniteApi.Database.Genres;
var entity = collection.Get(Guid.Parse(e.EntityId));
entity = entityUpdater.Update(entity, e.Fields);
collection.Update(entity);
}
else if (e.EntityTypeName == "Tag")
{
var collection = PlayniteApi.Database.Tags;
var entity = collection.Get(Guid.Parse(e.EntityId));
entity = entityUpdater.Update(entity, e.Fields);
collection.Update(entity);
}
else if (e.EntityTypeName == "Feature")
{
var collection = PlayniteApi.Database.Features;
var entity = collection.Get(Guid.Parse(e.EntityId));
entity = entityUpdater.Update(entity, e.Fields);
collection.Update(entity);
}
else if (e.EntityTypeName == "Series")
{
var collection = PlayniteApi.Database.Series;
var entity = collection.Get(Guid.Parse(e.EntityId));
entity = entityUpdater.Update(entity, e.Fields);
var entity = collection.Get(e.EntityId);
entity = entityUpdater.Update(entity, e.Entity);
collection.Update(entity);
}
//else if (e.EntityType == "Platform")
//{
// var collection = PlayniteApi.Database.Platforms;
// var entity = collection.Get(Guid.Parse(e.EntityId));
// entity = entityUpdater.Update(entity, e.Fields);
// collection.Update(entity);
//}
//else if (e.EntityType == "Genre")
//{
// var collection = PlayniteApi.Database.Genres;
// var entity = collection.Get(Guid.Parse(e.EntityId));
// entity = entityUpdater.Update(entity, e.Fields);
// collection.Update(entity);
//}
//else if (e.EntityType == "Tag")
//{
// var collection = PlayniteApi.Database.Tags;
// var entity = collection.Get(Guid.Parse(e.EntityId));
// entity = entityUpdater.Update(entity, e.Fields);
// collection.Update(entity);
//}
//else if (e.EntityType == "Feature")
//{
// var collection = PlayniteApi.Database.Features;
// var entity = collection.Get(Guid.Parse(e.EntityId));
// entity = entityUpdater.Update(entity, e.Fields);
// collection.Update(entity);
//}
//else if (e.EntityType == "Series")
//{
// var collection = PlayniteApi.Database.Series;
// var entity = collection.Get(Guid.Parse(e.EntityId));
// entity = entityUpdater.Update(entity, e.Fields);
// collection.Update(entity);
//}
}
catch (Exception ex)
{
Expand Down
1 change: 1 addition & 0 deletions apps/PlayniteWebPlugin/src/PlayniteWeb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<Compile Include="Services\GameState.cs" />
<Compile Include="Services\IDeserializeObjects.cs" />
<Compile Include="Services\ObjectDeserializer.cs" />
<Compile Include="Services\Publishers\EntityUpdatePayload.cs" />
<Compile Include="Services\Publishers\GameStatePayload.cs" />
<Compile Include="Services\Publishers\IApplyPublisherOptions.cs" />
<Compile Include="Services\Publishers\IPublishToPlayniteWeb.cs" />
Expand Down
4 changes: 2 additions & 2 deletions apps/PlayniteWebPlugin/src/PlayniteWebSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace PlayniteWeb
{
public class PlayniteWebSettings : ObservableObject
{
private string clientId = "Playnite";
private string clientId = Guid.NewGuid().ToString();

private string deviceId = "playnite";
private string deviceId = Guid.NewGuid().ToString();

private string deviceName;

Expand Down
3 changes: 2 additions & 1 deletion apps/PlayniteWebPlugin/src/Services/ObjectDeserializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Playnite.SDK;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -43,7 +44,7 @@ public class ObjectDeserializer : IDeserializeObjects

public T Deserialize<T>(string data)
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
var options = new JsonSerializerOptions(JsonSerializerDefaults.General)
{
Converters = { new TypeConverter(), new ListJsonConverter<string>() }
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using PlayniteWeb.Models;
using System;

namespace PlayniteWeb.Services.Publishers
{
internal class GameStatePayload
{
public Release Release {get; set; }
public int? ProcessId { get; set; }
public string GameId {get; set; }
public string State { get; set; }
}
}
22 changes: 12 additions & 10 deletions apps/PlayniteWebPlugin/src/Services/Publishers/Mqtt/PublishGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ public PublishGame(IMqttClient client, IManageTopics topicBuilder, ISerializeObj

public IEnumerable<Task> Publish(IIdentifiable game)
{

if (!((Models.Game)game).Releases.Any())
if (game is Models.Game g)
{
yield break;
}
if (!g.Releases.Any())
{
yield break;
}

var topic = topicBuilder.GetPublishTopic(PublishTopics.Game(game.Id));
yield return client.PublishStringAsync(topic, serializer.Serialize(game), MqttQualityOfServiceLevel.ExactlyOnce, retain: false, cancellationToken: default);
var releasePublishes = ((Models.Game)game).Releases.SelectMany(release => publishRelease.Publish(release));
foreach (var task in releasePublishes)
{
yield return task;
}

var releasePublishes = ((Models.Game)game).Releases.SelectMany(release => publishRelease.Publish(release));
foreach (var task in releasePublishes)
{
yield return task;
var topic = topicBuilder.GetPublishTopic(PublishTopics.Game(g.Id));
yield return client.PublishStringAsync(topic, serializer.Serialize(new EntityUpdatePayload<Models.Game>(EntityUpdateAction.Update) { Entity = g}), MqttQualityOfServiceLevel.ExactlyOnce, retain: true, cancellationToken: default);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public PublishGameEntity(IMqttClient client, IManageTopics topicBuilder, ISerial
public IEnumerable<Task> Publish(IIdentifiable item)
{
var topic = topicBuilder.GetPublishTopic(PublishTopics.GameEntity(item.GetType().Name, item.Id));
yield return client.PublishStringAsync(topic, serializer.Serialize(item), MqttQualityOfServiceLevel.ExactlyOnce, retain: false, cancellationToken: default);
yield return client.PublishStringAsync(topic, serializer.Serialize(new EntityUpdatePayload<IIdentifiable>(EntityUpdateAction.Update) { Entity = item}), MqttQualityOfServiceLevel.ExactlyOnce, retain: true, cancellationToken: default);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MQTTnet.Protocol;
using Playnite.SDK;
using Playnite.SDK.Models;
using PlayniteWeb.Models;
using PlayniteWeb.TopicManager;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -29,32 +30,27 @@ public PublishGameEntityRemoval(IMqttClient client, IManageTopics topicBuilder,

public IEnumerable<Task> Publish(IIdentifiable item)
{
if (item.GetType().Name == "Game")
if (item is Playnite.SDK.Models.Game g)
{
var removeReleaseTopic = topicBuilder.GetPublishTopic(PublishTopics.GameEntityRemoval("Release", item.Id));
yield return client.PublishStringAsync(removeReleaseTopic, string.Empty, MqttQualityOfServiceLevel.ExactlyOnce, retain: false, cancellationToken: default);

var isGameRemoved = gameDatabase.Games
.Where(g => !g.Id.Equals(Guid.Empty))
.All(g => !g.Name.Equals(((Game)item).Name));
.Where(dbg => !dbg.Id.Equals(Guid.Empty))
.All(dbg => !dbg.Name.Equals(g.Name));

if (isGameRemoved)
{
using (MD5 md5 = MD5.Create())
{
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(((Game)item).Name));
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(g.Name));
var id = new Guid(hash);

var removeGameTopic = topicBuilder.GetPublishTopic(PublishTopics.GameEntityRemoval("Game", id));
yield return client.PublishStringAsync(removeGameTopic, string.Empty, MqttQualityOfServiceLevel.ExactlyOnce, retain: false, cancellationToken: default);
var gameTopic = topicBuilder.GetPublishTopic(PublishTopics.Game(id));
yield return client.PublishStringAsync(gameTopic, serializer.Serialize(new EntityUpdatePayload<Models.Game>(EntityUpdateAction.Delete) { Entity = new Models.Game(id) }), MqttQualityOfServiceLevel.ExactlyOnce, retain: true, cancellationToken: default);
}
}

}
else
{
var topic = topicBuilder.GetPublishTopic(PublishTopics.GameEntityRemoval(item.GetType().Name, item.Id));
yield return client.PublishStringAsync(topic, string.Empty, MqttQualityOfServiceLevel.ExactlyOnce, retain: false, cancellationToken: default);
var releaseTopic = topicBuilder.GetPublishTopic(PublishTopics.Release(g.Id));
yield return client.PublishStringAsync(releaseTopic, serializer.Serialize(new EntityUpdatePayload<Release>(EntityUpdateAction.Delete) { Entity = new Release(g, null) }), MqttQualityOfServiceLevel.ExactlyOnce, retain: true, cancellationToken: default);

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ public PublishGameState(GameState state, IMqttClient client, IManageTopics topic

public IEnumerable<Task> Publish(IIdentifiable release)
{
var r = (Models.Release)release;
if (state != null)
if (release is Models.Release r)
{
if (state == null)
{
yield break;
}

yield return client.PublishStringAsync(topicBuilder.GetPublishTopic(PublishTopics.GameState()), serializer.Serialize(new GameStatePayload()
{
Release = r,
GameId = r.GameId,
ProcessId = r.ProcessId,
State = state.ToString()
}), MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce);
}), MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, retain: false, cancellationToken: default);
yield break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,32 @@ public PublishPlatform(IMqttClient client, IManageTopics topicBuilder, ISerializ

public IEnumerable<Task> Publish(IIdentifiable platform)
{
var topic = topicBuilder.GetPublishTopic(PublishTopics.Platform(platform.Id));
yield return client.PublishStringAsync(topic, serializer.Serialize(platform), MqttQualityOfServiceLevel.ExactlyOnce, retain: false, cancellationToken: default);

var coverImageFilePath = new PlatformCoverFilePath(platform).getFilePath();
var coverPublisher = new PublishAsset(client, gameDatabase, coverImageFilePath, topic, AssetType.cover);
foreach (var task in coverPublisher.Publish(platform))
if (platform is Platform p)
{
yield return task;
}
var topic = topicBuilder.GetPublishTopic(PublishTopics.Platform(p.Id));

var backgroundImageFilePath = new PlatformBackgroundFilePath(platform).getFilePath();
var backgroundPublisher = new PublishAsset(client, gameDatabase, backgroundImageFilePath, topic, AssetType.background);
foreach (var task in backgroundPublisher.Publish(platform))
{
yield return task;
}
var coverImageFilePath = new PlatformCoverFilePath(p).getFilePath();
var coverPublisher = new PublishAsset(client, gameDatabase, coverImageFilePath, topic, AssetType.cover);
foreach (var task in coverPublisher.Publish(p))
{
yield return task;
}

var iconFilePath = new PlatformIconFilePath(platform).getFilePath();
var iconPublisher = new PublishAsset(client, gameDatabase, iconFilePath, topic, AssetType.icon);
foreach (var task in iconPublisher.Publish(platform))
{
yield return task;
var backgroundImageFilePath = new PlatformBackgroundFilePath(p).getFilePath();
var backgroundPublisher = new PublishAsset(client, gameDatabase, backgroundImageFilePath, topic, AssetType.background);
foreach (var task in backgroundPublisher.Publish(p))
{
yield return task;
}

var iconFilePath = new PlatformIconFilePath(p).getFilePath();
var iconPublisher = new PublishAsset(client, gameDatabase, iconFilePath, topic, AssetType.icon);
foreach (var task in iconPublisher.Publish(p))
{
yield return task;
}

yield return client.PublishStringAsync(topic, serializer.Serialize(new EntityUpdatePayload<Platform>(EntityUpdateAction.Update) { Entity = p }), MqttQualityOfServiceLevel.ExactlyOnce, retain: true, cancellationToken: default);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MQTTnet.Protocol;
using Playnite.SDK;
using Playnite.SDK.Models;
using PlayniteWeb.Models;
using PlayniteWeb.TopicManager;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand All @@ -25,8 +26,11 @@ public PublishPlaylist(IMqttClient client, IManageTopics topicBuilder, ISerializ

public IEnumerable<Task> Publish(IIdentifiable item)
{
var topic = topicBuilder.GetPublishTopic(PublishTopics.Playlist(item.Id));
yield return client.PublishStringAsync(topic, serializer.Serialize(item), MqttQualityOfServiceLevel.ExactlyOnce, retain: false, cancellationToken: default);
if (item is Playlist p)
{
var topic = topicBuilder.GetPublishTopic(PublishTopics.Playlist(p.Id));
yield return client.PublishStringAsync(topic, serializer.Serialize(new EntityUpdatePayload<Playlist>(EntityUpdateAction.Update) { Entity = p }), MqttQualityOfServiceLevel.ExactlyOnce, retain: true, cancellationToken: default);
}
}
}
}
Loading

0 comments on commit 16f8cae

Please sign in to comment.