public static HashSet<String> GetReplaysInFolder(String folderPath, ReplayType type, bool recursive = true)
        {
            HashSet<String> replaysInFolder;

            if (type == ReplayType.Warcraft3)
            {
                //make a set of replays in the folder, and replays that exist
                replaysInFolder = new HashSet<String>(Directory.GetFiles(folderPath, "*.w3g"));

                if (recursive)
                {
                    Queue<String> directories = new Queue<String>(Directory.GetDirectories(folderPath));
                    while (directories.Count > 0)
                    {
                        String dir = directories.Dequeue();
                        replaysInFolder = new HashSet<String>(replaysInFolder.Concat(Directory.GetFiles(dir, "*.w3g")));
                        directories = new Queue<String>(directories.Concat(Directory.GetDirectories(dir)));
                    }

                    HashSet<String> existingReplays = DatabaseHandler.GetWarcraft3ReplayPaths();
                    replaysInFolder.ExceptWith(existingReplays);
                }
            }

            //add new replay types here, otherwise an exception is thrown
            else
            {
                throw new ArgumentException();
            }

            return replaysInFolder;
        }
        public static void RenameAndRestructureReplays(ReplayType type, FolderStructureFormat format, String rootPath)
        {
            HashSet<String> versions = DatabaseHandler.GetDistinctWarcraft3ReplayAttributes("Version")[0];
            FileHandler.CreateReplayFolderStructure(versions, format, rootPath);
            rootPath = Path.Combine(rootPath, "ReplayParser.NET");

            if (format == FolderStructureFormat.Default)
            {
                //we have to re-parse all of the replays to get the associated information
                var result = ParserHandler.ParseWarcraft3Replays(DatabaseHandler.GetWarcraft3ReplayPaths().ToList());
                List<String> oldPaths = new List<String>(result.Count);
                HashSet<String> newPaths = new HashSet<String>();

                for (int i = 0; i < result.Count; i++)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var team in result[i].teams)
                    {
                        foreach (var player in team.Value)
                        {
                            sb.Append(player.Name);
                            sb.Append("(");
                            sb.Append(Warcraft3DataConverter.ConvertShortRace(player.RaceFlag));
                            sb.Append(") ").Append(",");
                        }

                        //removing the last comma
                        sb.Remove(sb.Length - 2, 2);
                        sb.Append(" v ");
                    }

                    sb.Remove(sb.Length - 3, 3);
                    oldPaths.Add(result[i].Path);

                    String path = Path.Combine(rootPath, String.Concat("1.", result[i].replayHeader.GameVersion));
                    path = Path.Combine(path, sb.ToString().Replace("|", ""));
                    String proposedPath = String.Concat(path, ".w3g");

                    //duplicate handling
                    int j = 1;
                    while (newPaths.Contains(proposedPath))
                        proposedPath = String.Concat(path, j++, ".w3g");

                    newPaths.Add(proposedPath);
                }

                FileHandler.RenameFiles(oldPaths, newPaths.ToList());
                DatabaseHandler.UpdateWarcraft3ReplayNames(oldPaths, newPaths.ToList());
            }
        }
        //the recursive flag means that all of the subfolders will be traversed for replays too
        public static void AddReplayFolder(String folderPath, ReplayType type, bool recursive = true)
        {
            if (type == ReplayType.Warcraft3)
            {
                HashSet<String> replaysInFolder = FileHandler.GetReplaysInFolder(folderPath, type, recursive);
                HashSet<String> existingReplays = DatabaseHandler.GetWarcraft3ReplayPaths();
                replaysInFolder.ExceptWith(existingReplays);

                //we only want to parse the ones that are unique to the folder
                List<Warcraft3Replay> replays = ParserHandler.ParseWarcraft3Replays(replaysInFolder.ToList());
                DatabaseHandler.AddWarcraft3Replays(replays);
            }

            //add new replay types here, otherwise an exception is thrown
            else
            {
                throw new ArgumentException();
            }
        }
Exemple #4
0
        public async Task <ReplayFile> ReadFile(string filePath)
        {
            // Make sure file exists
            if (String.IsNullOrEmpty(filePath))
            {
                _log.Error("File reference is null");
                throw new ArgumentNullException($"File reference is null");
            }

            if (!File.Exists(filePath))
            {
                _log.Error("File path not found, does the file exist?");
                throw new FileNotFoundException($"File path not found, does the file exist?");
            }

            // Reads the first 4 bytes and tries to find out the replay type
            ReplayType type = await ParserHelpers.GetReplayTypeAsync(filePath);

            // Match parsers to file types
            ReplayFile result;

            switch (type)
            {
            case ReplayType.ROFL:       // Official Replays
                result = await ReadROFL(filePath);

                break;

            //case ReplayType.LRF:    // LOLReplay
            //    file.Type = ReplayType.LRF;
            //    file.Data = await ReadLRF(file.Location);
            //    break;
            //case ReplayType.LPR:    // BaronReplays
            //    file.Type = ReplayType.LPR;
            //    file.Data = null;
            //    break;
            default:
                _log.Error($"File {filePath} is not an accepted format: rofl");
                return(null);
            }

            // Make some educated guesses
            GameDetailsInferrer detailsInferrer = new GameDetailsInferrer();

            result.Players = result.BluePlayers.Union(result.RedPlayers).ToArray();

            try
            {
                result.MapId = detailsInferrer.InferMap(result.Players);
            }
            catch (ArgumentNullException ex)
            {
                _log.Warning("Could not infer map type\n" + ex.ToString());
                result.MapId = MapCode.Unknown;
            }

            result.MapName          = detailsInferrer.GetMapName(result.MapId);
            result.IsBlueVictorious = detailsInferrer.InferBlueVictory(result.BluePlayers, result.RedPlayers);

            foreach (var player in result.Players)
            {
                player.Id = $"{result.MatchId}_{player.PlayerID}";
            }

            // Set the alternate name to the default
            result.AlternativeName = result.Name;

            return(result);
        }