示例#1
0
        public Dictionary <string, GameInfo> GetInstalledGames()
        {
            var contentPath = Path.Combine(Origin.DataPath, "LocalContent");
            var games       = new Dictionary <string, GameInfo>();

            if (Directory.Exists(contentPath))
            {
                var packages = Directory.GetFiles(contentPath, "*.mfst", SearchOption.AllDirectories);
                foreach (var package in packages)
                {
                    try
                    {
                        var gameId = Path.GetFileNameWithoutExtension(package);
                        if (!gameId.StartsWith("Origin"))
                        {
                            // Get game id by fixing file via adding : before integer part of the name
                            // for example OFB-EAST52017 converts to OFB-EAST:52017
                            var match = Regex.Match(gameId, @"^(.*?)(\d+)$");
                            if (!match.Success)
                            {
                                logger.Warn("Failed to get game id from file " + package);
                                continue;
                            }

                            gameId = match.Groups[1].Value + ":" + match.Groups[2].Value;
                        }

                        var newGame = new GameInfo()
                        {
                            Source      = "Origin",
                            GameId      = gameId,
                            IsInstalled = true,
                            Platform    = "PC"
                        };

                        GameLocalDataResponse localData = null;

                        try
                        {
                            localData = GetLocalManifest(gameId);
                        }
                        catch (Exception e) when(!Environment.IsDebugBuild)
                        {
                            logger.Error(e, $"Failed to get Origin manifest for a {gameId}, {package}");
                            continue;
                        }

                        if (localData == null)
                        {
                            continue;
                        }

                        if (localData.offerType != "Base Game" && localData.offerType != "DEMO")
                        {
                            continue;
                        }

                        newGame.Name = StringExtensions.NormalizeGameName(localData.localizableAttributes.displayName);
                        var platform = localData.publishing.softwareList.software.FirstOrDefault(a => a.softwarePlatform == "PCWIN");

                        if (platform == null)
                        {
                            logger.Warn(gameId + " game doesn't have windows platform, skipping install import.");
                            continue;
                        }

                        var installPath = GetPathFromPlatformPath(platform.fulfillmentAttributes.installCheckOverride);
                        if (installPath == null ||
                            installPath.CompletePath.IsNullOrEmpty() ||
                            !File.Exists(installPath.CompletePath))
                        {
                            continue;
                        }

                        var installDirReplacement = ExpandableVariables.InstallationDirectory + Path.DirectorySeparatorChar;
                        newGame.PlayAction = GetGamePlayTask(localData);
                        if (newGame.PlayAction?.Type == GameActionType.File)
                        {
                            newGame.InstallDirectory = newGame.PlayAction.WorkingDir;
                            newGame.PlayAction.Path  = newGame.PlayAction.Path.Replace(newGame.InstallDirectory, installDirReplacement);
                        }
                        else
                        {
                            newGame.InstallDirectory = Path.GetDirectoryName(installPath.CompletePath);
                        }

                        // For games like Sims 4
                        if (newGame.PlayAction?.Path.EndsWith("exe", StringComparison.OrdinalIgnoreCase) == false)
                        {
                            var task = GetGamePlayTask(newGame.InstallDirectory);
                            newGame.InstallDirectory = task.WorkingDir;
                            newGame.PlayAction.Path  = task.Path.Replace(newGame.InstallDirectory, installDirReplacement);
                        }

                        // If game uses EasyAntiCheat then use executable referenced by it
                        if (Origin.GetGameUsesEasyAntiCheat(newGame.InstallDirectory))
                        {
                            var eac = EasyAntiCheat.GetLauncherSettings(newGame.InstallDirectory);
                            if (newGame.PlayAction == null)
                            {
                                newGame.PlayAction = new GameAction
                                {
                                    Type = GameActionType.File,
                                    IsHandledByPlugin = true
                                };
                            }

                            newGame.PlayAction.Path = eac.Executable.Replace(newGame.InstallDirectory, installDirReplacement);
                            if (!string.IsNullOrEmpty(eac.Parameters) && eac.UseCmdlineParameters == "1")
                            {
                                newGame.PlayAction.Arguments = eac.Parameters;
                            }

                            if (!string.IsNullOrEmpty(eac.WorkingDirectory))
                            {
                                newGame.PlayAction.WorkingDir = Path.Combine(ExpandableVariables.InstallationDirectory, eac.WorkingDirectory);
                            }
                        }

                        if (newGame.PlayAction != null)
                        {
                            if (!newGame.PlayAction.Path.Contains(ExpandableVariables.InstallationDirectory))
                            {
                                newGame.PlayAction.Path = installDirReplacement + newGame.PlayAction.Path;
                            }

                            newGame.PlayAction.WorkingDir = null;
                            games.Add(newGame.GameId, newGame);
                        }
                        else
                        {
                            logger.Warn("Found installed Origin game that's not launchable, skipping import.");
                            logger.Warn(newGame.ToString());
                        }
                    }
                    catch (Exception e) when(!Environment.IsDebugBuild)
                    {
                        logger.Error(e, $"Failed to import installed Origin game {package}.");
                    }
                }
            }

            return(games);
        }