2023-01-14 21:09:45 +00:00
|
|
|
|
using LANCommander.PlaynitePlugin.Extensions;
|
|
|
|
|
using LANCommander.SDK;
|
2023-01-07 18:34:12 +00:00
|
|
|
|
using Playnite.SDK;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
using Playnite.SDK.Models;
|
|
|
|
|
using Playnite.SDK.Plugins;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-01-07 18:34:12 +00:00
|
|
|
|
using System.IO;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Controls;
|
2023-01-07 18:34:12 +00:00
|
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
|
|
|
|
using PN = Playnite;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
|
2023-01-08 18:09:57 +00:00
|
|
|
|
namespace LANCommander.PlaynitePlugin
|
2023-01-05 01:23:32 +00:00
|
|
|
|
{
|
2023-01-14 21:15:31 +00:00
|
|
|
|
public class LANCommanderLibraryPlugin : LibraryPlugin
|
2023-01-05 01:23:32 +00:00
|
|
|
|
{
|
|
|
|
|
public static readonly ILogger Logger = LogManager.GetLogger();
|
2023-01-14 21:16:13 +00:00
|
|
|
|
internal LANCommanderSettingsViewModel Settings { get; set; }
|
2023-01-05 07:07:17 +00:00
|
|
|
|
internal LANCommanderClient LANCommander { get; set; }
|
2023-01-15 10:56:56 +00:00
|
|
|
|
internal PowerShellRuntime PowerShellRuntime { get; set; }
|
2023-01-05 01:23:32 +00:00
|
|
|
|
|
|
|
|
|
public override Guid Id { get; } = Guid.Parse("48e1bac7-e0a0-45d7-ba83-36f5e9e959fc");
|
|
|
|
|
public override string Name => "LANCommander";
|
2023-01-14 21:17:38 +00:00
|
|
|
|
public override LibraryClient Client { get; } = new LANCommanderLibraryClient();
|
2023-01-05 01:23:32 +00:00
|
|
|
|
|
2023-01-14 21:15:31 +00:00
|
|
|
|
public LANCommanderLibraryPlugin(IPlayniteAPI api) : base(api)
|
2023-01-05 01:23:32 +00:00
|
|
|
|
{
|
|
|
|
|
Properties = new LibraryPluginProperties
|
|
|
|
|
{
|
|
|
|
|
HasSettings = true,
|
|
|
|
|
};
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
2023-01-14 21:16:13 +00:00
|
|
|
|
Settings = new LANCommanderSettingsViewModel(this);
|
2023-01-12 02:48:39 +00:00
|
|
|
|
LANCommander = new LANCommanderClient(Settings.ServerAddress);
|
|
|
|
|
LANCommander.Token = new SDK.Models.AuthToken()
|
|
|
|
|
{
|
|
|
|
|
AccessToken = Settings.AccessToken,
|
|
|
|
|
RefreshToken = Settings.RefreshToken,
|
|
|
|
|
};
|
2023-01-15 10:56:56 +00:00
|
|
|
|
|
|
|
|
|
PowerShellRuntime = new PowerShellRuntime();
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<GameMetadata> GetGames(LibraryGetGamesArgs args)
|
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
var gameMetadata = new List<GameMetadata>();
|
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
try
|
2023-01-05 02:31:02 +00:00
|
|
|
|
{
|
2023-01-06 08:39:00 +00:00
|
|
|
|
var games = LANCommander
|
2023-01-14 21:09:45 +00:00
|
|
|
|
.GetGames();
|
2023-01-07 20:32:52 +00:00
|
|
|
|
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
{
|
2023-01-14 21:09:45 +00:00
|
|
|
|
var manifest = LANCommander.GetGameManifest(game.Id);
|
2023-01-07 20:32:52 +00:00
|
|
|
|
var existingGame = PlayniteApi.Database.Games.FirstOrDefault(g => g.GameId == game.Id.ToString() && g.PluginId == Id && g.IsInstalled);
|
|
|
|
|
|
2023-01-08 07:34:38 +00:00
|
|
|
|
var iconUri = new Uri(new Uri(Settings.ServerAddress), $"Games/GetIcon/{game.Id}");
|
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
var metadata = new GameMetadata()
|
2023-01-06 08:39:00 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
IsInstalled = existingGame != null,
|
2023-01-14 21:09:45 +00:00
|
|
|
|
Name = manifest.Title,
|
|
|
|
|
SortingName = manifest.SortTitle,
|
|
|
|
|
Description = manifest.Description,
|
2023-01-07 20:32:52 +00:00
|
|
|
|
GameId = game.Id.ToString(),
|
2023-01-14 21:09:45 +00:00
|
|
|
|
ReleaseDate = new ReleaseDate(manifest.ReleasedOn),
|
|
|
|
|
//Version = game.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault().Version,
|
|
|
|
|
Icon = new MetadataFile(iconUri.ToString()),
|
|
|
|
|
Genres = new HashSet<MetadataProperty>()
|
2023-01-07 20:32:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-14 21:09:45 +00:00
|
|
|
|
if (manifest.Genre != null && manifest.Genre.Count() > 0)
|
|
|
|
|
metadata.Genres = new HashSet<MetadataProperty>(manifest.Genre.Select(g => new MetadataNameProperty(g)));
|
|
|
|
|
|
|
|
|
|
if (manifest.Developers != null && manifest.Developers.Count() > 0)
|
|
|
|
|
metadata.Developers = new HashSet<MetadataProperty>(manifest.Developers.Select(d => new MetadataNameProperty(d)));
|
|
|
|
|
|
|
|
|
|
if (manifest.Publishers != null && manifest.Publishers.Count() > 0)
|
|
|
|
|
metadata.Publishers = new HashSet<MetadataProperty>(manifest.Publishers.Select(p => new MetadataNameProperty(p)));
|
|
|
|
|
|
|
|
|
|
if (manifest.Tags != null && manifest.Tags.Count() > 0)
|
|
|
|
|
metadata.Tags = new HashSet<MetadataProperty>(manifest.Tags.Select(t => new MetadataNameProperty(t)));
|
|
|
|
|
|
|
|
|
|
metadata.Features = new HashSet<MetadataProperty>();
|
|
|
|
|
|
|
|
|
|
if (manifest.Singleplayer)
|
|
|
|
|
metadata.Features.Add(new MetadataNameProperty("Singleplayer"));
|
|
|
|
|
|
|
|
|
|
if (manifest.LocalMultiplayer != null)
|
|
|
|
|
metadata.Features.Add(new MetadataNameProperty($"Local Multiplayer {manifest.LocalMultiplayer.GetPlayerCount()}".Trim()));
|
|
|
|
|
|
|
|
|
|
if (manifest.LanMultiplayer != null)
|
|
|
|
|
metadata.Features.Add(new MetadataNameProperty($"LAN Multiplayer {manifest.LanMultiplayer.GetPlayerCount()}".Trim()));
|
|
|
|
|
|
|
|
|
|
if (manifest.OnlineMultiplayer != null)
|
|
|
|
|
metadata.Features.Add(new MetadataNameProperty($"Online Multiplayer {manifest.OnlineMultiplayer.GetPlayerCount()}".Trim()));
|
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
gameMetadata.Add(metadata);
|
|
|
|
|
};
|
2023-01-05 07:07:17 +00:00
|
|
|
|
}
|
2023-01-07 04:12:03 +00:00
|
|
|
|
catch (Exception ex)
|
2023-01-05 07:07:17 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
|
2023-01-05 07:07:17 +00:00
|
|
|
|
}
|
2023-01-07 20:32:52 +00:00
|
|
|
|
|
|
|
|
|
return gameMetadata;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 04:12:03 +00:00
|
|
|
|
public override IEnumerable<InstallController> GetInstallActions(GetInstallActionsArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Game.PluginId != Id)
|
|
|
|
|
yield break;
|
|
|
|
|
|
|
|
|
|
yield return new LANCommanderInstallController(this, args.Game);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 18:46:26 +00:00
|
|
|
|
public override IEnumerable<UninstallController> GetUninstallActions(GetUninstallActionsArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Game.PluginId != Id)
|
|
|
|
|
yield break;
|
|
|
|
|
|
|
|
|
|
yield return new LANCommanderUninstallController(this, args.Game);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-15 10:56:56 +00:00
|
|
|
|
public override IEnumerable<GameMenuItem> GetGameMenuItems(GetGameMenuItemsArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Games.Count == 1 && args.Games.First().IsInstalled && !String.IsNullOrWhiteSpace(args.Games.First().InstallDirectory))
|
|
|
|
|
{
|
|
|
|
|
var nameChangeScriptPath = PowerShellRuntime.GetScriptFilePath(args.Games.First(), SDK.Enums.ScriptType.NameChange);
|
|
|
|
|
var keyChangeScriptPath = PowerShellRuntime.GetScriptFilePath(args.Games.First(), SDK.Enums.ScriptType.KeyChange);
|
|
|
|
|
|
|
|
|
|
if (File.Exists(nameChangeScriptPath))
|
|
|
|
|
yield return new GameMenuItem
|
|
|
|
|
{
|
|
|
|
|
Description = "Change Player Name",
|
|
|
|
|
Action = (nameChangeArgs) =>
|
|
|
|
|
{
|
|
|
|
|
PowerShellRuntime.RunScript(nameChangeArgs.Games.First(), SDK.Enums.ScriptType.NameChange);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (File.Exists(keyChangeScriptPath))
|
|
|
|
|
yield return new GameMenuItem
|
|
|
|
|
{
|
|
|
|
|
Description = "Change Game Key",
|
|
|
|
|
Action = (keyChangeArgs) =>
|
|
|
|
|
{
|
|
|
|
|
PowerShellRuntime.RunScript(keyChangeArgs.Games.First(), SDK.Enums.ScriptType.KeyChange);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// To add new main menu items override GetMainMenuItems
|
|
|
|
|
public override IEnumerable<MainMenuItem> GetMainMenuItems(GetMainMenuItemsArgs args)
|
|
|
|
|
{
|
|
|
|
|
yield return new MainMenuItem
|
|
|
|
|
{
|
|
|
|
|
Description = "Change Player Name (All Games)",
|
|
|
|
|
Action = (args2) =>
|
|
|
|
|
{
|
|
|
|
|
var result = PlayniteApi.Dialogs.SelectString("Enter your new player name. This will change your name across all installed games!", "Enter Name", "");
|
|
|
|
|
|
|
|
|
|
if (result.Result == true)
|
|
|
|
|
{
|
|
|
|
|
var games = PlayniteApi.Database.Games.Where(g => g.IsInstalled).ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
{
|
|
|
|
|
PowerShellRuntime.RunScript(game, SDK.Enums.ScriptType.NameChange);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 01:23:32 +00:00
|
|
|
|
public override ISettings GetSettings(bool firstRunSettings)
|
|
|
|
|
{
|
2023-01-05 07:07:17 +00:00
|
|
|
|
return Settings;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override UserControl GetSettingsView(bool firstRunView)
|
|
|
|
|
{
|
2023-01-14 21:19:14 +00:00
|
|
|
|
return new LANCommanderSettingsView(this);
|
2023-01-05 07:07:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 08:39:00 +00:00
|
|
|
|
public System.Windows.Window ShowAuthenticationWindow()
|
2023-01-05 07:07:17 +00:00
|
|
|
|
{
|
|
|
|
|
var window = PlayniteApi.Dialogs.CreateWindow(new WindowCreationOptions()
|
|
|
|
|
{
|
|
|
|
|
ShowMinimizeButton = false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
window.Title = "Authenticate to LANCommander";
|
|
|
|
|
|
2023-01-06 00:37:13 +00:00
|
|
|
|
window.Content = new Views.Authentication(this);
|
2023-01-14 21:51:17 +00:00
|
|
|
|
window.DataContext = new ViewModels.Authentication()
|
|
|
|
|
{
|
|
|
|
|
ServerAddress = Settings.ServerAddress
|
|
|
|
|
};
|
2023-01-05 07:07:17 +00:00
|
|
|
|
|
|
|
|
|
window.Owner = PlayniteApi.Dialogs.GetCurrentAppWindow();
|
|
|
|
|
window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
|
|
|
|
|
window.ShowDialog();
|
2023-01-06 08:39:00 +00:00
|
|
|
|
|
|
|
|
|
return window;
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
public void UpdateGamesFromManifest()
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
var games = PlayniteApi.Database.Games;
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
foreach (var game in games.Where(g => g.PluginId == Id && g.IsInstalled))
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
if (!Directory.Exists(game.InstallDirectory))
|
|
|
|
|
continue;
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
var manifestPath = Path.Combine(game.InstallDirectory, "_manifest.yml");
|
|
|
|
|
|
|
|
|
|
if (File.Exists(manifestPath))
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
try
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
var manifestContents = File.ReadAllText(manifestPath);
|
|
|
|
|
var deserializer = new DeserializerBuilder()
|
2023-01-08 18:10:20 +00:00
|
|
|
|
.IgnoreUnmatchedProperties()
|
2023-01-07 20:32:52 +00:00
|
|
|
|
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var manifest = deserializer.Deserialize<GameManifest>(manifestContents);
|
|
|
|
|
|
2023-01-12 02:48:39 +00:00
|
|
|
|
#region Actions
|
2023-01-07 20:32:52 +00:00
|
|
|
|
if (game.GameActions == null)
|
|
|
|
|
game.GameActions = new System.Collections.ObjectModel.ObservableCollection<PN.SDK.Models.GameAction>();
|
|
|
|
|
|
|
|
|
|
foreach (var action in manifest.Actions)
|
2023-01-07 18:34:12 +00:00
|
|
|
|
{
|
2023-01-07 20:32:52 +00:00
|
|
|
|
bool isFirstAction = !manifest.Actions.Any(a => a.IsPrimaryAction) && manifest.Actions.First().Name == action.Name;
|
|
|
|
|
|
2023-01-12 02:48:39 +00:00
|
|
|
|
foreach (var existingAction in game.GameActions)
|
|
|
|
|
if (action.Name == existingAction.Name)
|
|
|
|
|
game.GameActions.Remove(existingAction);
|
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
game.GameActions.AddMissing(new PN.SDK.Models.GameAction()
|
|
|
|
|
{
|
|
|
|
|
Name = action.Name,
|
|
|
|
|
Arguments = action.Arguments,
|
2023-01-12 02:48:39 +00:00
|
|
|
|
Path = PlayniteApi.ExpandGameVariables(game, action.Path?.Replace('/', Path.DirectorySeparatorChar)),
|
|
|
|
|
WorkingDir = action.WorkingDirectory?.Replace('/', Path.DirectorySeparatorChar) ?? game.InstallDirectory,
|
2023-01-07 20:32:52 +00:00
|
|
|
|
IsPlayAction = action.IsPrimaryAction || isFirstAction
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-12 02:48:39 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Features
|
|
|
|
|
var singlePlayerFeature = PlayniteApi.Database.Features.FirstOrDefault(f => f.Name == "Single Player");
|
|
|
|
|
|
|
|
|
|
if (manifest.LanMultiplayer != null)
|
|
|
|
|
{
|
|
|
|
|
var multiplayerInfo = manifest.LanMultiplayer;
|
|
|
|
|
|
|
|
|
|
string playerCount = multiplayerInfo.MinPlayers == multiplayerInfo.MaxPlayers ? $"({multiplayerInfo.MinPlayers} players)" : $"({multiplayerInfo.MinPlayers} - {multiplayerInfo.MaxPlayers} players)";
|
|
|
|
|
string featureName = $"LAN Multiplayer {playerCount}";
|
|
|
|
|
|
|
|
|
|
if (PlayniteApi.Database.Features.Any(f => f.Name == featureName))
|
|
|
|
|
{
|
|
|
|
|
game.Features.Add(PlayniteApi.Database.Features.FirstOrDefault(f => f.Name == featureName));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PlayniteApi.Database.Features.Add(new PN.SDK.Models.GameFeature()
|
|
|
|
|
{
|
|
|
|
|
Name = featureName
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
game.Features.Add(new PN.SDK.Models.GameFeature()
|
|
|
|
|
{
|
|
|
|
|
Name = $"LAN Multiplayer {playerCount}"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (manifest.LocalMultiplayer != null)
|
|
|
|
|
{
|
|
|
|
|
var multiplayerInfo = manifest.LocalMultiplayer;
|
|
|
|
|
|
|
|
|
|
string playerCount = multiplayerInfo.MinPlayers == multiplayerInfo.MaxPlayers ? $"({multiplayerInfo.MinPlayers} players)" : $"({multiplayerInfo.MinPlayers} - {multiplayerInfo.MaxPlayers} players)";
|
|
|
|
|
|
|
|
|
|
game.Features.Add(new PN.SDK.Models.GameFeature()
|
|
|
|
|
{
|
|
|
|
|
Name = $"Local Multiplayer {playerCount}"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (manifest.OnlineMultiplayer != null)
|
|
|
|
|
{
|
|
|
|
|
var multiplayerInfo = manifest.OnlineMultiplayer;
|
|
|
|
|
|
|
|
|
|
string playerCount = multiplayerInfo.MinPlayers == multiplayerInfo.MaxPlayers ? $"({multiplayerInfo.MinPlayers} players)" : $"({multiplayerInfo.MinPlayers} - {multiplayerInfo.MaxPlayers} players)";
|
|
|
|
|
|
|
|
|
|
game.Features.Add(new PN.SDK.Models.GameFeature()
|
|
|
|
|
{
|
|
|
|
|
Name = $"Online Multiplayer {playerCount}"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-01-07 20:32:52 +00:00
|
|
|
|
|
|
|
|
|
PlayniteApi.Database.Games.Update(game);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
2023-01-07 20:32:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-07 18:34:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-05 01:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|