Наследование: GenericBinaryWriter
Пример #1
0
 static byte[] EventToBytes(IMapEvent e)
 {
     using var ms = new MemoryStream();
     using var bw = new BinaryWriter(ms);
     using var s  = new AlbionWriter(bw);
     MapEvent.Serdes(e, s, EventSetId.None, TextId.None, AssetMapping.Global);
     bw.Flush();
     ms.Position = 0;
     return(ms.ToArray());
 }
Пример #2
0
        public ISerializer Read(string path, AssetInfo info, IFileSystem disk, IJsonUtil jsonUtil)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }
            if (disk == null)
            {
                throw new ArgumentNullException(nameof(disk));
            }
            var subAssets = new Dictionary <int, (string, string)>(); // path and name
            // Pattern vars: 0=Index 1=SubItem 2=Name 3=Palette
            var pattern = info.Get(AssetProperty.Pattern, "{0}_{1}_{2}.dat");

            foreach (var filePath in disk.EnumerateDirectory(path, $"{info.Index}_*.*"))
            {
                var filename = Path.GetFileName(filePath);
                var(index, subAsset, paletteId, name) = AssetInfo.ParseFilename(pattern, filename);

                if (paletteId.HasValue)
                {
                    info.Set(AssetProperty.PaletteId, paletteId);
                }

                if (index != info.Index)
                {
                    continue;
                }

                subAssets[subAsset] = (filePath, name);
            }

            var ms = new MemoryStream();

            if (subAssets.Count > 0)
            {
                using var bw = new BinaryWriter(ms, Encoding.UTF8, true);
                using var s  = new AlbionWriter(bw);
                PackedChunks.PackNamed(s, subAssets.Keys.Max() + 1, i =>
                                       !subAssets.TryGetValue(i, out var pathAndName)
                        ? (Array.Empty <byte>(), null)
                        : (disk.ReadAllBytes(pathAndName.Item1), pathAndName.Item2));
            }

            ms.Position = 0;
            var br = new BinaryReader(ms);

            return(new AlbionReader(br, ms.Length, () =>
            {
                br.Dispose();
                ms.Dispose();
            }));
        }
Пример #3
0
        public static void RoundTripTest(string baseDir)
        {
            var mapping = AssetMapping.Global; // TODO: Base game mapping.
            var saveDir = Path.Combine(baseDir, "re", "TestSaves");
            var regex   = new Regex(@"\.[0-9][0-9][0-9]$");

            foreach (var file in Directory.EnumerateFiles(saveDir))
            {
                if (!regex.IsMatch(file))
                {
                    continue;
                }
                Console.WriteLine("Round-trip testing " + file);
                using var stream = File.Open(file, FileMode.Open);
                using var br     = new BinaryReader(stream);
                using var ar     = new AlbionReader(br, stream.Length);
                var save = SavedGame.Serdes(null, mapping, ar);

                using var ms = new MemoryStream();
                using var bw = new BinaryWriter(ms);
                using var aw = new AlbionWriter(bw);
                SavedGame.Serdes(save, mapping, aw);

                br.BaseStream.Position = 0;
                var originalBytes  = br.ReadBytes((int)stream.Length);
                var roundTripBytes = ms.ToArray();

                //* Save round-tripped and annotated text output for debugging
                File.WriteAllBytes(file + ".bin", roundTripBytes);
                using var ts  = new MemoryStream();
                using var tw  = new StreamWriter(ts);
                using var afw = new AnnotatedFormatWriter(tw);
                SavedGame.Serdes(save, mapping, afw);
                ts.Position = 0;
                File.WriteAllBytes(file + ".txt", ts.ToArray());
                //*/

                ApiUtil.Assert(originalBytes.Length == roundTripBytes.Length);
                ApiUtil.Assert(originalBytes.SequenceEqual(roundTripBytes));

                var sw = new StringWriter();
                using var jw = new JsonWriter(sw);
                SavedGame.Serdes(save, mapping, jw);
                File.WriteAllText(file + ".json", sw.ToString());
                break;
            }

            Console.WriteLine("Done");
            Console.ReadLine();
        }
Пример #4
0
        static int WithSerializer(SerializerMode mode, MemoryStream stream, Action <ISerializer> func, ISerializer parentSerializer, ref int fakeOffset)
        {
            switch (mode)
            {
            case SerializerMode.Writing:
            {
                using var bw = new BinaryWriter(stream, Encoding.GetEncoding(850), true);
                var s = new AlbionWriter(bw);
                func(s);
                return((int)s.Offset);
            }

            case SerializerMode.WritingAnnotated:
            {
                using var tw = new StreamWriter(stream, Encoding.GetEncoding(850), 1024, true);
                using var s  = new AnnotatedFormatWriter(tw, (AnnotatedFormatWriter)parentSerializer);
                s.Seek(fakeOffset);
                func(s);
                int length = (int)s.Offset - fakeOffset;
                fakeOffset = (int)s.Offset;
                return(length);
            }

            case SerializerMode.WritingJson:
            {
                using var tw = new StreamWriter(stream, Encoding.GetEncoding(850), 1024, true);
                using var s  = new JsonWriter(tw, (JsonWriter)parentSerializer);
                s.Seek(fakeOffset);
                func(s);
                int length = (int)s.Offset - fakeOffset;
                fakeOffset = (int)s.Offset;
                return(length);
            }

            default: throw new InvalidOperationException();
            }
        }