package skadistats.clarity;

import com.google.protobuf.ZeroCopy;
import org.xerial.snappy.Snappy;
import skadistats.clarity.model.EngineType;
import skadistats.clarity.source.InputStreamSource;
import skadistats.clarity.source.MappedFileSource;
import skadistats.clarity.source.Source;
import skadistats.clarity.wire.Packet;
import skadistats.clarity.wire.common.proto.Demo;

import java.io.IOException;
import java.io.InputStream;

public class Clarity {

    /**
     * Retrieves summary-data from the given demo-file
     *
     * @param fileName path and name of the file on disk
     * @return the {@code CDemoFileInfo} protobuf message
     * @throws IOException if the given file is non-existing or is no valid demo-file
     */
    public static Demo.CDemoFileInfo infoForFile(String fileName) throws IOException {
        return infoForSource(new MappedFileSource(fileName));
    }

    /**
     * Retrieves summary-data from the given input stream
     *
     * @param stream an {@code InputStream}, containing replay data, positioned at the beginning
     * @return the {@code CDemoFileInfo} protobuf message
     * @throws IOException if the given stream is invalid
     */
    public static Demo.CDemoFileInfo infoForStream(final InputStream stream) throws IOException {
        return infoForSource(new InputStreamSource(stream));
    }

    /**
     * Retrieves summary-data from the given input source
     *
     * @param source the {@code Source} providing the replay data
     * @return the {@code CDemoFileInfo} protobuf message
     * @throws IOException if the given source is invalid
     * @see Source
     */
    public static Demo.CDemoFileInfo infoForSource(final Source source) throws IOException {
        EngineType engineType = source.readEngineType();
        source.setPosition(source.readFixedInt32());
        int kind = source.readVarInt32();
        boolean isCompressed = (kind & engineType.getCompressedFlag()) == engineType.getCompressedFlag();
        source.skipVarInt32();
        int size = source.readVarInt32();
        byte[] data = source.readBytes(size);
        if (isCompressed) {
            data = Snappy.uncompress(data);
        }
        return Packet.parse(Demo.CDemoFileInfo.class, ZeroCopy.wrap(data));
    }

}

@@ -20,24 +20,64 @@
 clarity;%0D%0A%0D%0A
+import com.google.protobuf.ByteString;%0D%0A
 import com.g
@@ -419,16 +419,111 @@
 .Demo;%0D%0A
+import skadistats.clarity.wire.s2.proto.S2DotaMatchMetadata;%0D%0A%0D%0Aimport java.io.FileInputStream;
 %0D%0Aimport
@@ -2458,11 +2458,1050 @@
    %7D%0D%0A%0D%0A
+    /**%0D%0A     * reads a metadata file and returns it's contents%0D%0A     *%0D%0A     * @param fileName path and name of the file on disk%0D%0A     * @return the %7B@code CDOTAMatchMetadataFile%7D protobuf message%0D%0A     * @throws IOException if the given file is non-existing or is no valid metadata-file%0D%0A     */%0D%0A    public static S2DotaMatchMetadata.CDOTAMatchMetadataFile metadataForFile(String fileName) throws IOException %7B%0D%0A        return metadataForStream(new FileInputStream(fileName));%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * reads a metadata stream and returns it's contents%0D%0A     *%0D%0A     * @param stream an %7B@code InputStream%7D, containing replay data, positioned at the beginning%0D%0A     * @return the %7B@code CDOTAMatchMetadataFile%7D protobuf message%0D%0A     * @throws IOException if the given stream is invalid%0D%0A     */%0D%0A    private static S2DotaMatchMetadata.CDOTAMatchMetadataFile metadataForStream(InputStream stream) throws IOException %7B%0D%0A        return Packet.parse(S2DotaMatchMetadata.CDOTAMatchMetadataFile.class, ByteString.readFrom(stream));%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A

add a static function to parse .meta files (#123)