Skip to content

Commit

Permalink
restart a game (#604)
Browse files Browse the repository at this point in the history
Closes #575
  • Loading branch information
andrew-codes authored Oct 29, 2024
2 parents 7d824ad + 562ec00 commit 79a3fc1
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 72 deletions.
26 changes: 26 additions & 0 deletions apps/PlayniteWebPlugin/src/Models/Release.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

namespace PlayniteWeb.Models
{
public enum RunState
{
Running,
Installed,
Uninstalled
}

public class Release : IIdentifiable
{
private readonly Playnite.SDK.Models.Game game;
Expand All @@ -23,6 +30,25 @@ public Release(Playnite.SDK.Models.Game game, Platform platform)
this.platform = platform;
}

[DontSerialize]
public RunState RunState
{
get
{
if (game.IsRunning)
{
return RunState.Running;
}
else if (game.IsInstalled)
{
return RunState.Installed;
}
else
{
return RunState.Uninstalled;
}
}
}
public int? ProcessId { get; set; }
public Guid Id => game.Id;
public string Name => game.Name;
Expand Down
19 changes: 18 additions & 1 deletion apps/PlayniteWebPlugin/src/PlayniteWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ public override void OnApplicationStopped(OnApplicationStoppedEventArgs args)
subscriber.OnStartRelease -= Subscriber_OnStartRelease;
subscriber.OnInstallRelease -= Subscriber_OnInstallRelease;
subscriber.OnUninstallRelease -= Subscriber_OnUninstallRelease;
subscriber.OnStopRelease -= Subscriber_OnStopRelease;
subscriber.OnStopRelease -= Subscriber_OnStopRelease;
subscriber.OnRestartRelease -= Subscriber_OnRestartRelease;

gameUpdates.Dispose();
platformUpdates.Dispose();
Expand All @@ -281,13 +282,24 @@ public override void OnApplicationStopped(OnApplicationStoppedEventArgs args)

}

private void Subscriber_OnRestartRelease(object sender, Release e)
{
this.Subscriber_OnStopRelease(sender, e);
Task.Delay(3000).ContinueWith(t => this.Subscriber_OnStartRelease(sender, e));
}

private void Subscriber_OnStopRelease(object sender, Release e)
{
if (!isPcPlatform(e.Platform))
{
return;
}

if (e.RunState != RunState.Running)
{
return;
}

try
{
var gameProcess = Process.GetProcessById(e.ProcessId.Value);
Expand Down Expand Up @@ -328,6 +340,7 @@ public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
subscriber.OnInstallRelease += Subscriber_OnInstallRelease;
subscriber.OnUninstallRelease += Subscriber_OnUninstallRelease;
subscriber.OnStopRelease += Subscriber_OnStopRelease;
subscriber.OnRestartRelease += Subscriber_OnRestartRelease;

gameUpdates.Subscribe(e => HandleGameUpdated(this, e));
platformUpdates.Subscribe(e => HandlePlatformUpdated(this, e));
Expand Down Expand Up @@ -384,7 +397,11 @@ private void Subscriber_OnStartRelease(object sender, Release release)
if (!isPcPlatform(release.Platform))
{
return;
}

if (release.RunState == RunState.Running)
{
return;
}

if (!release.IsInstalled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ internal interface ISubscribeToPlayniteWeb
event EventHandler<Release> OnInstallRelease;
event EventHandler<Release> OnUninstallRelease;
event EventHandler<Release> OnStopRelease;
event EventHandler<Release> OnRestartRelease;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private Task Client_ConnectedAsync(MqttClientConnectedEventArgs args)
public event EventHandler<Release> OnInstallRelease;
public event EventHandler<Release> OnUninstallRelease;
public event EventHandler<Release> OnStopRelease;
public event EventHandler<Release> OnRestartRelease;

private Task MesssageReceived(MqttApplicationMessageReceivedEventArgs args)
{
Expand Down Expand Up @@ -69,6 +70,10 @@ private Task MesssageReceived(MqttApplicationMessageReceivedEventArgs args)
{
eventHandler = OnStopRelease;
}
else if (args.ApplicationMessage.Topic == topicBuilder.GetSubscribeTopic(SubscribeTopics.RequestRestartRelease) && OnRestartRelease != null)
{
eventHandler = OnRestartRelease;
}

if (eventHandler == null)
{
Expand Down
1 change: 1 addition & 0 deletions apps/PlayniteWebPlugin/src/TopicManager/SubscribeTopics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public static class SubscribeTopics
public const string RequestInstallRelease = "game/install";
public const string RequestUninstallRelease = "game/uninstall";
public const string RequestStopRelease = "game/stop";
public const string RequestRestartRelease = "game/restart";
}
}
4 changes: 2 additions & 2 deletions apps/playnite-web/src/queryHooks/gameById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const useGameById = (opts: QueryHookOptions) => {
useEffect(() => {
q.refetch()
}, [
sub.data?.releaseActivationStateChanged?.id,
sub.data?.releaseActivationStateChanged?.state,
sub.data?.releaseRunStateChanged?.id,
sub.data?.releaseRunStateChanged?.state,
])

return q
Expand Down
4 changes: 2 additions & 2 deletions apps/playnite-web/src/queryHooks/playlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const usePlaylists = () => {
useEffect(() => {
q.refetch()
}, [
sub.data?.releaseActivationStateChanged?.id,
sub.data?.releaseActivationStateChanged?.state,
sub.data?.releaseRunStateChanged?.id,
sub.data?.releaseRunStateChanged?.state,
])

return q
Expand Down
3 changes: 0 additions & 3 deletions apps/playnite-web/src/queryHooks/restartRelease.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { gql } from '@apollo/client/core/core.cjs'
import { useMutation } from '@apollo/client/react/hooks/hooks.cjs'
import { GameRelease } from 'apps/playnite-web/.generated/types.generated'
import _ from 'lodash'

const { merge } = _

const Restart_Game_Release_Mutation = gql`
mutation restartGameRelease($releaseId: String!) {
Expand Down
30 changes: 1 addition & 29 deletions apps/playnite-web/src/queryHooks/startRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { gql } from '@apollo/client/core/core.cjs'
import { useMutation } from '@apollo/client/react/hooks/hooks.cjs'
import { GameRelease } from 'apps/playnite-web/.generated/types.generated'
import _ from 'lodash'
import { PlaylistEntity } from '../server/graphql/resolverTypes'
import { AllPlaylists } from './playlists'

const { merge } = _

Expand All @@ -16,32 +14,6 @@ const Activate_Mutation = gql`
`

const useStartRelease = () => {
return useMutation<{ startGameRelease: GameRelease }>(Activate_Mutation, {
update: (cache, mutationResult) => {
let data = cache.readQuery<{ playlists: Array<PlaylistEntity> }>({
query: AllPlaylists,
})
if (data) {
cache.writeQuery({
query: AllPlaylists,
data: merge({}, data, {
playlists: data.playlists.map((playlist) =>
merge({}, playlist, {
games: playlist.games.map((game) =>
merge({}, game, {
releases: game.releases.map((release) =>
merge({}, release, {
runState: 'launching',
}),
),
}),
),
}),
),
}),
})
}
},
})
return useMutation<{ startGameRelease: GameRelease }>(Activate_Mutation)
}
export { Activate_Mutation, useStartRelease }
29 changes: 0 additions & 29 deletions apps/playnite-web/src/queryHooks/stopRelease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { gql } from '@apollo/client/core/core.cjs'
import { useMutation } from '@apollo/client/react/hooks/hooks.cjs'
import { GameRelease } from 'apps/playnite-web/.generated/types.generated'
import _ from 'lodash'
import { PlaylistEntity } from '../server/graphql/resolverTypes'
import { AllPlaylists } from './playlists'

const { merge } = _

Expand All @@ -18,33 +16,6 @@ const Stop_Game_Release_Mutation = gql`
const useStopRelease = () => {
return useMutation<{ stopGameRelease: GameRelease }>(
Stop_Game_Release_Mutation,
{
update: (cache, mutationResult) => {
let data = cache.readQuery<{ playlists: Array<PlaylistEntity> }>({
query: AllPlaylists,
})
if (data) {
cache.writeQuery({
query: AllPlaylists,
data: merge({}, data, {
playlists: data.playlists.map((playlist) =>
merge({}, playlist, {
games: playlist.games.map((game) =>
merge({}, game, {
releases: game.releases.map((release) =>
merge({}, release, {
runState: 'stopping',
}),
),
}),
),
}),
),
}),
})
}
},
},
)
}
export { Stop_Game_Release_Mutation, useStopRelease }
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { GameReleaseStateSubscriptionPayloadResolvers } from './../../../../../../.generated/types.generated'
export const GameReleaseStateSubscriptionPayload: GameReleaseStateSubscriptionPayloadResolvers =
{
runState: async (parent, _args, _ctx) => {
return (parent.runState ?? 'installed') as unknown as string
},
/* Implement GameReleaseStateSubscriptionPayload resolver logic here */
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { GraphQLError } from 'graphql'
import { Platform, Release } from '../../../../../data/types.entities'
import { fromString } from '../../../../../oid'
import type { MutationResolvers } from './../../../../../../../.generated/types.generated'
Expand All @@ -17,10 +16,6 @@ export const restartGameRelease: NonNullable<
value: releaseId,
})) as Array<Release>

if (release.runState.id === 'running') {
throw new GraphQLError('Game is already running')
}

await _ctx.updateQueryApi.executeUpdate<Release>(
{
entityType: 'Release',
Expand Down Expand Up @@ -52,6 +47,7 @@ export const restartGameRelease: NonNullable<
id: release.id,
gameId: release.gameId,
name: release.name,
processId: release.processId,
platform: {
id: platform.id,
name: platform.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import createDebugger from 'debug'
import _ from 'lodash'
import { HandlerOptions } from '..'
import type { IHandlePublishedTopics } from '../IHandlePublishedTopics'

const debug = createDebugger(
'playnite-web/game-db-updater/handler/persistGameReleaseState',
)
const { merge } = _

const topicMatch = /^playnite\/.*\/response\/game\/state$/

Expand Down Expand Up @@ -87,7 +89,12 @@ const create =
},
)

options.pubsub.publish('releaseRunStateChanged', release)
options.pubsub.publish('releaseRunStateChanged', {
id: release.id,
gameId: release.gameId,
processId: release.processId ?? null,
runState: newState,
})
} catch (e) {
console.error(e)
}
Expand Down

0 comments on commit 79a3fc1

Please sign in to comment.