<commit_before>﻿using System;
using System.IO;
using System.Linq;

using Microsoft.Xna.Framework;

using YamlDotNet.Serialization;

using ShadowsOfShadows.Entities;
using ShadowsOfShadows.Items;
using ShadowsOfShadows.Renderables;
using ShadowsOfShadows.Helpers;

namespace ShadowsOfShadows.Serialization
{
    public static class Serializer
    {

        private static string SaveFolder = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                        "ShadowsOfShadows",
                        "savedgames");

        private static FileStream OpenFile(SaveSlot slot, bool create = false)
        {
            if (!Directory.Exists(SaveFolder))
                Directory.CreateDirectory(SaveFolder);
            if (File.Exists(SaveFolder + "/" + slot + ".sav") && create)
                File.WriteAllText(SaveFolder + "/" + slot + ".sav", String.Empty);
            return File.Open(SaveFolder + "/" + slot + ".sav", create ? FileMode.OpenOrCreate : FileMode.Open);
        }

        public static void Save(SaveSlot slot, GameState state)
        {
            try
            {
                var serializer = new SerializerBuilder()
                    .EnsureRoundtrip()  //save type info
                    .EmitDefaults()     //save default values
                    .WithTypeConverter(new PrimitivesConverter())
                    .Build();

                using (var writer = new StreamWriter(OpenFile(slot, true)))
                    serializer.Serialize(writer, state);
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}\n{1}", e.GetType(), e.Message);
            }
        }

        public static GameState Load(SaveSlot slot)
        {
            var deserializer = new DeserializerBuilder()
                .WithTypeConverter(new PrimitivesConverter())
                .Build();

            using (var reader = new StreamReader(OpenFile(slot)))
                return deserializer.Deserialize<GameState>(reader);
        }
    }
}<commit_msg>Compress save files with GZip to save space (~20 times)<commit_after>﻿using System;
using System.IO;
using System.IO.Compression;
using System.Linq;

using Microsoft.Xna.Framework;

using YamlDotNet.Serialization;

using ShadowsOfShadows.Entities;
using ShadowsOfShadows.Items;
using ShadowsOfShadows.Renderables;
using ShadowsOfShadows.Helpers;

namespace ShadowsOfShadows.Serialization
{
    public static class Serializer
    {

        private static string SaveFolder = Path.Combine(
                        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                        "ShadowsOfShadows",
                        "savedgames");

        private static FileStream OpenFile(SaveSlot slot, bool create = false)
        {
            if (!Directory.Exists(SaveFolder))
                Directory.CreateDirectory(SaveFolder);
            if (File.Exists(SaveFolder + "/" + slot + ".sav") && create)
                File.WriteAllText(SaveFolder + "/" + slot + ".sav", String.Empty);
            return File.Open(SaveFolder + "/" + slot + ".sav", create ? FileMode.OpenOrCreate : FileMode.Open);
        }

        public static void Save(SaveSlot slot, GameState state)
        {
            try
            {
                var serializer = new SerializerBuilder()
                    .EnsureRoundtrip()  //save type info
                    .EmitDefaults()     //save default values
                    .WithTypeConverter(new PrimitivesConverter())
                    .Build();

                using (var compression = new GZipStream(OpenFile(slot, true), CompressionMode.Compress))
                using (var writer = new StreamWriter(compression))
                    serializer.Serialize(writer, state);
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}\n{1}", e.GetType(), e.Message);
            }
        }

        public static GameState Load(SaveSlot slot)
        {
            var deserializer = new DeserializerBuilder()
                .WithTypeConverter(new PrimitivesConverter())
                .Build();

            using (var compression = new GZipStream(OpenFile(slot), CompressionMode.Decompress))            
            using (var reader = new StreamReader(compression))
                return deserializer.Deserialize<GameState>(reader);
        }
    }
}
{'fraction_non_alphanumeric': 0.09427609427609428, 'fraction_numerical': 0.0013468013468013469, 'mean_word_length': 1.9048239895697523, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7035601', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1090, 'n_words': 253}