|
| 1 | +using Gamelib.Core.Util; |
| 2 | +using GameLib.Core; |
| 3 | +using GameLib.Plugin.BattleNet.Model; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using System.Reflection; |
| 6 | + |
| 7 | +namespace GameLib.Plugin.BattleNet; |
| 8 | + |
| 9 | +public static class BattleNetGameFactory |
| 10 | +{ |
| 11 | + public static IEnumerable<BattleNetGame> GetGames(ILauncher launcher, CancellationToken cancellationToken = default) |
| 12 | + { |
| 13 | + var catalog = GetCatalog(); |
| 14 | + |
| 15 | + return DeserializeProductInstalls() |
| 16 | + //.AsParallel() |
| 17 | + //.WithCancellation(cancellationToken) |
| 18 | + .Select(product => BattleNetGameBuiler(launcher, product)) |
| 19 | + .Where(game => game is not null) |
| 20 | + .Select(game => AddLauncherId(launcher, game)) |
| 21 | + .Select(game => AddCatalogData(launcher, game, catalog)) |
| 22 | + .ToList()!; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Add launcher ID to Game |
| 27 | + /// </summary> |
| 28 | + private static BattleNetGame AddLauncherId(ILauncher launcher, BattleNetGame game) |
| 29 | + { |
| 30 | + game.LauncherId = launcher.Id; |
| 31 | + return game; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Load local catalog data |
| 36 | + /// </summary> |
| 37 | + private static BNetGames? GetCatalog() |
| 38 | + { |
| 39 | + var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? ".", "Resources", "BattleNetGames.json"); |
| 40 | + |
| 41 | + if (!File.Exists(path)) |
| 42 | + { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + var json = File.ReadAllText(path); |
| 49 | + var catalog = JsonConvert.DeserializeObject<BNetGames>(json); |
| 50 | + |
| 51 | + if (catalog is null) |
| 52 | + { |
| 53 | + throw new ApplicationException("Cannot deserialize JSON stream"); |
| 54 | + } |
| 55 | + |
| 56 | + return catalog; |
| 57 | + } |
| 58 | + catch { return null; } |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Deserialize the BattlNet product.db |
| 63 | + /// </summary> |
| 64 | + private static IEnumerable<ProductInstall> DeserializeProductInstalls() |
| 65 | + { |
| 66 | + var productInstalls = new List<ProductInstall>(); |
| 67 | + |
| 68 | + var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Battle.net", "Agent", "product.db"); |
| 69 | + |
| 70 | + if (!File.Exists(path)) |
| 71 | + { |
| 72 | + throw new FileNotFoundException("Configuration file not found, probably Battle.net client hasn't been started at least once.", path); |
| 73 | + } |
| 74 | + |
| 75 | + using var file = File.OpenRead(path); |
| 76 | + productInstalls = ProtoBuf.Serializer.Deserialize<List<ProductInstall>>(file); |
| 77 | + |
| 78 | + return productInstalls.Where(p => p.Uid is not "agent" and not "battle.net"); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Convert entry from product.db into a BattleNetGame object |
| 83 | + /// </summary> |
| 84 | + private static BattleNetGame BattleNetGameBuiler(ILauncher launcher, ProductInstall productInstall) |
| 85 | + { |
| 86 | + return new BattleNetGame() |
| 87 | + { |
| 88 | + Id = productInstall.Uid, |
| 89 | + Name = Path.GetFileName(PathUtil.Sanitize(productInstall.Settings.installPath)) ?? string.Empty, |
| 90 | + InstallDir = PathUtil.Sanitize(productInstall.Settings.installPath) ?? string.Empty, |
| 91 | + WorkingDir = PathUtil.Sanitize(productInstall.Settings.installPath) ?? string.Empty, |
| 92 | + InstallDate = PathUtil.GetCreationTime(productInstall.Settings.installPath) ?? DateTime.MinValue, |
| 93 | + LaunchString = $"\"{launcher.ExecutablePath}\" --game={productInstall.productCode.ToUpper()}", |
| 94 | + ProductCode = productInstall.productCode ?? string.Empty, |
| 95 | + PlayRegion = productInstall.Settings.playRegion ?? string.Empty, |
| 96 | + SpeechLanguage = productInstall.Settings.selectedSpeechLanguage ?? string.Empty, |
| 97 | + TextLanguage = productInstall.Settings.selectedTextLanguage ?? string.Empty, |
| 98 | + Version = productInstall.cachedProductState.baseProductState.currentVersionStr ?? string.Empty |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Get the executable and game name from the catalog |
| 104 | + /// </summary> |
| 105 | + private static BattleNetGame AddCatalogData(ILauncher launcher, BattleNetGame game, BNetGames? catalog = null) |
| 106 | + { |
| 107 | + if (catalog?.Games |
| 108 | + .Where(g => g.InternalId == game.Id) |
| 109 | + .FirstOrDefault(defaultValue: null) is not BNetGame catalogItem) |
| 110 | + { |
| 111 | + return game; |
| 112 | + } |
| 113 | + |
| 114 | + if (!string.IsNullOrEmpty(catalogItem.Name)) |
| 115 | + { |
| 116 | + game.Name = catalogItem.Name; |
| 117 | + } |
| 118 | + |
| 119 | + if (!string.IsNullOrEmpty(catalogItem.ProductId)) |
| 120 | + { |
| 121 | + game.ProductCode = catalogItem.ProductId; |
| 122 | + game.LaunchString = $"\"{launcher.ExecutablePath}\" --exec=\"launch {game.ProductCode}\""; |
| 123 | + } |
| 124 | + |
| 125 | + game.Executable = catalogItem.Executables.FirstOrDefault(defaultValue: string.Empty); |
| 126 | + if (!string.IsNullOrEmpty(game.Executable)) |
| 127 | + { |
| 128 | + game.ExecutablePath = Path.Combine(game.InstallDir, game.Executable); |
| 129 | + } |
| 130 | + |
| 131 | + return game; |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | +} |
0 commit comments