コード例 #1
0
 private static void Print()
 {
     using (Stream contentStream = OpenMeta(_loadedFilePath))
     {
         var saveData = EU4SaveMeta.Load(contentStream);
         Out($"SaveType: {saveData.SaveType}");
         using (var reader = new StringReader(saveData.ToString()))
         {
             string line;
             while ((line = reader.ReadLine()) != null)
             {
                 Out("  " + line);
             }
         }
     }
 }
コード例 #2
0
        private static void ShowBackups()
        {
            string backupDirName             = GetBackupPath(_loadedFilePath);
            string ext                       = Path.GetExtension(_loadedFilePath);
            IEnumerable <string> backupFiles = Directory.EnumerateFileSystemEntries(backupDirName, $"*{ext}");
            var saveMetas                    = backupFiles.ToDictionary <string, string, EU4SaveMeta>(
                filePath => filePath,
                filePath =>
            {
                using (var stream = OpenMeta(filePath))
                {
                    return(EU4SaveMeta.Load(stream));
                }
            },
                StringComparer.OrdinalIgnoreCase);

            int i = 1;

            Out($"Backup Path: {backupDirName}");
            Out(string.Empty);
            Out("|     | Date       | Tag | IronMan | Hash                             | Version  |");
            Out("| --- | ---------- | --- | ------- | -------------------------------- | -------- |");

            var outputBuilder = new StringBuilder(100);

            foreach (var saveWithMeta in saveMetas.OrderByDescending(x => new FileInfo(x.Key).LastWriteTimeUtc))
            {
                string      backupFilePath = saveWithMeta.Key;
                EU4SaveMeta save           = saveWithMeta.Value;

                string hash = Path.GetFileNameWithoutExtension(saveWithMeta.Key);

                outputBuilder.Clear();
                outputBuilder.Append($"| {i,-3} ");
                outputBuilder.Append($"| {save.Date.ToString().PadRight(10)} ");
                outputBuilder.Append($"| {save.PlayerTag} ");
                outputBuilder.Append($"| {BoolYesNo(save.IronMan),-7} ");
                outputBuilder.Append($"| {hash} ");
                outputBuilder.Append($"| {save.SaveGameVersion.ToString("s")} ");
                outputBuilder.Append("|");

                Out(outputBuilder.ToString());
                ++i;
            }
        }
コード例 #3
0
        public static EU4SaveMeta Load(Stream stream)
        {
            const ushort dateId              = 0x284d;
            const ushort saveGameId          = 0x2c69;
            const ushort playerTagId         = 0x2a38;
            const ushort countryColorsId     = 0x3116;
            const ushort flagId              = 0x2d52;
            const ushort colorId             = 0x0056;
            const ushort symbolIndexId       = 0x34f5;
            const ushort flagColorsId        = 0x311a;
            const ushort playerCountryNameId = 0x32b8;
            const ushort saveGameVersionId   = 0x2ec9;
            const ushort saveGameVersionsId  = 0x314b;
            const ushort dlcEnabledId        = 0x2ee1;
            const ushort modEnabledId        = 0x2ee0;
            const ushort ironManId           = 0x3589;
            const ushort multiPlayerId       = 0x3329;
            const ushort notObserverId       = 0x3317;
            const ushort checkSumId          = 0x0179;

            byte[] _magic = { 0x45, 0x55, 0x34, 0x62, 0x69, 0x6e };

            var result = new EU4SaveMeta();

            using (var reader = new BinaryReader(stream, _encoding, true))
            {
                byte[] magic = reader.ReadBytes(_magic.Length);
                if (!magic.SequenceEqual(_magic))
                {
                    return(null);
                }

                while (stream.Position < stream.Length)
                {
                    (ushort fieldId, object data) = ReadField(reader);
                    if (data == null)
                    {
                        // Parsing failed. We have no way of knowing what lay ahead other that dozens of irrelevant
                        // errors. Bail out.
                        Console.Error.WriteLine($"<PARSE> Null data for {fieldId:X}");
                        break;
                    }

                    switch (fieldId)
                    {
                    case dateId:
                        result.Date = EU4Date.FromInt((int)data);
                        break;

                    case saveGameId:
                        result.SaveGame = (string)data;
                        break;

                    case playerTagId:
                        result.PlayerTag = (string)data;
                        break;

                    case countryColorsId:
                        result.CountryColors = new EU4CountryColors();
                        foreach ((ushort, object)item in ((List <object>)data).Cast <(ushort, object)>())
                        {
                            switch (item.Item1)
                            {
                            case flagId:
                                result.CountryColors.Flag = (int)item.Item2;
                                break;

                            case colorId:
                                result.CountryColors.Color = (int)item.Item2;
                                break;

                            case symbolIndexId:
                                result.CountryColors.SymbolIndex = (int)item.Item2;
                                break;

                            case flagColorsId:
                                result.CountryColors.FlagColors = new List <int>(
                                    ((List <object>)item.Item2).Cast <int>());
                                break;
                            }
                        }
                        break;

                    case playerCountryNameId:
                        result.PlayerCountryName = (string)data;
                        break;

                    case saveGameVersionId:
                    {
                        List <(ushort, object)> list = ((List <object>)data).Cast <(ushort, object)>().ToList();
                        result.SaveGameVersion = new EU4SaveGameVersion
                        {
                            First  = (int)list.First(x => x.Item1 == 0x28e2).Item2,
                            Second = (int)list.First(x => x.Item1 == 0x28e3).Item2,
                            Third  = (int)list.First(x => x.Item1 == 0x2ec7).Item2,
                            Fourth = (int)list.First(x => x.Item1 == 0x2ec8).Item2,
                            Name   = (string)list.First(x => x.Item1 == 0x001b).Item2
                        };
                    }
                    break;

                    case saveGameVersionsId:
                        result.SaveGameVersions = ((List <object>)data).Cast <string>().ToList();
                        break;

                    case dlcEnabledId:
                        result.DlcEnabled = ((List <object>)data).Cast <string>().ToList();
                        break;

                    case modEnabledId:
                        result.ModEnabled = ((List <object>)data).Cast <string>().ToList();
                        break;

                    case ironManId:
                        result.IronMan = (bool)data;
                        break;

                    case multiPlayerId:
                        result.MultiPlayer = (bool)data;
                        break;

                    case notObserverId:
                        result.NotObserver = (bool)data;
                        break;

                    case checkSumId:
                        result.CheckSum = (string)data;
                        break;
                    }
                }
            }

            return(result);
        }