Ejemplo n.º 1
0
        public async Task <IReplayHeader> ReadReplayAsync(FileStream fileStream)
        {
            if (!fileStream.CanRead)
            {
                throw new IOException($"{exceptionOriginName} - Stream does not support reading");
            }

            // Read and check Magic Numbers
            ReplayType type;

            try
            {
                type = await ParserHelpers.GetReplayTypeAsync(fileStream);
            }
            catch (Exception ex)
            {
                throw new IOException($"{exceptionOriginName} - Reading Magic Number: " + ex.Message, ex);
            }

            if (type != ReplayType.ROFL)
            {
                throw new Exception($"{exceptionOriginName} - Selected file is not in valid ROFL format");
            }

            // Read and deserialize length fields
            byte[] lengthFieldBuffer;
            try
            {
                lengthFieldBuffer = await ParserHelpers.ReadBytesAsync(fileStream, 26, 262, SeekOrigin.Begin);
            }
            catch (Exception ex)
            {
                throw new IOException($"{exceptionOriginName} - Reading Length Header: " + ex.Message, ex);
            }

            LengthFields lengthFields;

            try
            {
                lengthFields = ParseLengthFields(lengthFieldBuffer);
            }
            catch (Exception ex)
            {
                throw new Exception($"{exceptionOriginName} - Parsing Length Header: " + ex.Message, ex);
            }


            // Read and deserialize metadata
            byte[] metadataBuffer;
            try
            {
                metadataBuffer = await ParserHelpers.ReadBytesAsync(fileStream, (int)lengthFields.MetadataLength, (int)lengthFields.MetadataOffset, SeekOrigin.Begin);
            }
            catch (Exception ex)
            {
                throw new IOException($"{exceptionOriginName} - Reading JSON Metadata: " + ex.Message, ex);
            }

            MatchMetadata metadataFields;

            try
            {
                metadataFields = ParseMetadata(metadataBuffer);
            }
            catch (Exception ex)
            {
                throw new Exception($"{exceptionOriginName} - Parsing Metadata Header: " + ex.Message, ex);
            }

            // Read and deserialize payload fields
            byte[] payloadBuffer;
            try
            {
                payloadBuffer = await ParserHelpers.ReadBytesAsync(fileStream, (int)lengthFields.PayloadHeaderLength, (int)lengthFields.PayloadHeaderOffset, SeekOrigin.Begin);
            }
            catch (Exception ex)
            {
                throw new IOException($"{exceptionOriginName} - Reading Match Header: " + ex.Message, ex);
            }

            PayloadFields payloadFields;

            try
            {
                payloadFields = ParsePayloadHeader(payloadBuffer);
            }
            catch (Exception ex)
            {
                throw new Exception($"{exceptionOriginName} - Parsing Payload Header: " + ex.Message, ex);
            }


            // Combine objects to create header
            ROFLHeader result = new ROFLHeader
            {
                LengthFields  = lengthFields,
                MatchMetadata = metadataFields,
                PayloadFields = payloadFields
            };

            // Create json of entire contents
            string jsonString = JsonConvert.SerializeObject(result);

            // throw it back on the object and return
            result.RawJsonString = jsonString;
            return(result);
        }
Ejemplo n.º 2
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);
        }