Ejemplo n.º 1
0
        void Read(StreamSink.DataInfo info = null)
        {
            Clear();
            var rootHeader = new Header(null, _r, Format);

            if ((Format == GameFormat.TES3 && rootHeader.Type != "TES3") || (Format != GameFormat.TES3 && rootHeader.Type != "TES4"))
            {
                throw new FormatException($"{FilePath} record header {rootHeader.Type} is not valid for this {Format}");
            }
            var rootRecord = rootHeader.CreateRecord(rootHeader.Position, RecordLevel);

            rootRecord.Read(_r, FilePath, Format);
            // morrowind hack
            if (Format == GameFormat.TES3)
            {
                var header = new Header {
                    Type = "ALL_", Label = string.Empty, DataSize = (uint)(_r.BaseStream.Length - _r.Position), Position = _r.Position
                };
                ReadTES3(header, true, info);
                return;
            }
            // read groups
            var endPosition = _r.BaseStream.Length;

            while (_r.Position < endPosition)
            {
                var header = new Header(this, _r, Format);
                if (header.Type != "GRUP")
                {
                    throw new InvalidOperationException($"{header.Type} not GRUP");
                }
                ReadGRUP(header, false, info);
            }
        }
Ejemplo n.º 2
0
 public List <Record> Load(bool loadAll = false, StreamSink.DataInfo info = null)
 {
     if (_headerSkip == Headers.Count)
     {
         return(Records);
     }
     if (_r == null && Headers.Count == 1)
     {
         var bytes = Task.Run(async() => await LoadDataLabelAsync(FilePath ?? ToPath(null))).Result();
         _r = new BinaryFileReader(new MemoryStream(bytes));
         Headers.First.Value.DataSize = (uint)bytes.Length;
     }
     lock (_r ?? throw new InvalidOperationException("Should not reach here"))
     {
         if (_headerSkip == Headers.Count)
         {
             return(Records);
         }
         foreach (var header in Headers.Skip(_headerSkip))
         {
             ReadGroup(header, loadAll, info);
         }
         _headerSkip = Headers.Count;
         return(Records);
     }
 }
Ejemplo n.º 3
0
        void ReadGroup(Header header, bool loadAll, StreamSink.DataInfo info)
        {
            if (GroupByLabel == null)
            {
                GroupByLabel = new Dictionary <string, RecordGroup>();
            }
            if (!GroupByLabel.TryGetValue(header.Label, out var group))
            {
                GroupByLabel.Add(header.Label, group = new RecordGroup(_streamSink, FilePath));
            }
            else
            {
                group = new RecordGroup(_streamSink, FilePath)
                {
                    Next = group
                }
            };

            if (_r[0] != null)
            {
                using (_r[0]) CELLRecord.ReadFixed(_r[0], header, group);
            }
            if (_r[1] != null)
            {
                using (_r[1]) LANDRecord.ReadTerrain(_r[1], header, group);
            }
            //if (_r[2] != null) using (_r[2]) DYNARecord.ReadNonfixed(_r[2], header, group);
        }
    }
Ejemplo n.º 4
0
        protected RecordGroup ReadGRUP(Header header, bool loadAll, StreamSink.DataInfo info)
        {
            var headerBytes  = _r.ReadAbsoluteBytes(header.HeaderPosition, (int)(header.Position - header.HeaderPosition));
            var nextPosition = _r.Position + header.DataSize;
            var label        = header.Label;

            info?.AddGroup(label, headerBytes);
            if (GroupByLabel == null)
            {
                GroupByLabel = new Dictionary <string, RecordGroup>();
            }
            if (!GroupByLabel.TryGetValue(label, out var group))
            {
                GroupByLabel.Add(label, group = new RecordGroup(_streamSink, _r, FilePath, Format, RecordLevel, Depth + 1));
            }
            else
            {
                group = new RecordGroup(_streamSink, _r, FilePath, Format, RecordLevel, Depth + 1)
                {
                    Next = group
                }
            };
            group.AddHeader(header, info);
            _r.Position = nextPosition;
            if (loadAll || info != null)
            {
                group.Load(loadAll, info);
            }
            //Log($"Grup: {string.Join("/", GetHeaderPath(new List<string>(), parentHeader).ToArray())} {parentHeader.GroupType}");
            return(group);
        }
Ejemplo n.º 5
0
        void ReadGroup(Header parentHeader, bool loadAll, StreamSink.DataInfo info)
        {
            _r.Position = parentHeader.Position;
            var endPosition = parentHeader.Position + parentHeader.DataSize;

            while (_r.Position < endPosition)
            {
                var header = new Header(this, _r, Format);

                // STREAMING READ
                if (info != null && (info.Data != null || Format != GameFormat.TES3))
                {
                    if (header.Type == "GRUP" && Depth < MaxDepthStreamed)
                    {
                        if (info.Data != null)
                        {
                            goto next;
                        }
                        info.EnterGroup();
                        ReadGRUP(header, loadAll, info);
                        info.LeaveGroup();
                        continue;
                    }
                    else
                    {
                        info.Data?.Invoke(_r.ReadAbsoluteBytes(header.HeaderPosition, (int)(header.Position - header.HeaderPosition) + (int)header.DataSize));
                    }
                    goto next;
                }

                // LOCAL READ
                if (header.Type == "GRUP")
                {
                    ReadGRUP(header, loadAll, null);
                    continue;
                }
                // HACK to limit cells loading
                if (header.Type == "CELL" && _cellsLoaded > int.MaxValue)
                {
                    goto next;
                }
                var record = header.CreateRecord(_r.Position, RecordLevel);
                if (record == null)
                {
                    goto next;
                }
                ReadRecord(record, header.Compressed);
                Records.Add(record);
                if (header.Type == "CELL")
                {
                    _cellsLoaded++;
                }
                continue;

                // NEXT
next:
                _r.Position += header.DataSize;
            }
        }
Ejemplo n.º 6
0
 void Read(StreamSink.DataInfo info = null)
 {
     Clear();
     foreach (var file in Directory.GetFiles(Path.Combine(FilePath, "static"), "terrain.*"))
     {
         AddHeader(new Header {
             Label = Path.GetExtension(file).Substring(1)
         }, info);
     }
 }
Ejemplo n.º 7
0
 public void AddHeader(Header header, StreamSink.DataInfo info)
 {
     //Log($"Read: {header.Label}");
     Headers.Add(header);
     if (info == null)
     {
         switch (header.Label)
         {
         case "9_": header.State = 1; Load(); break;
         }
     }
 }
Ejemplo n.º 8
0
 void ReadTES3Transform(RecordGroup group, StreamSink.DataInfo info) => GroupByLabel = group.Records.GroupBy(x => x.Header.Type)
                                                                                       .ToDictionary(x => x.Key, x =>
 {
     var s = new RecordGroup(_streamSink, _r, FilePath, Format, RecordLevel, Depth + 1)
     {
         Records = x.ToList()
     };
     s.AddHeader(new Header {
         Label = x.Key
     }, info);
     return(s);
 });
Ejemplo n.º 9
0
 public void AddHeader(Header header, StreamSink.DataInfo info)
 {
     //Log($"Read: {header.Label}");
     Headers.AddLast(header);
     if (info == null && header.GroupType == Header.HeaderGroupType.Top)
     {
         switch (header.Label)
         {
         //case "DIAL":
         case "CELL":
         case "WRLD": Load(); break;
         }
     }
 }
Ejemplo n.º 10
0
 public List <Record> Load(bool loadAll = false, StreamSink.DataInfo info = null)
 {
     foreach (var header in Headers.Where(x => x.State == 1))
     {
         header.State = 2;
         string path;
         _r = new[] {
             File.Exists(path = Path.Combine(FilePath, $"static/fixed.{header.Label}")) ? new BinaryFileReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) : null,
             File.Exists(path = Path.Combine(FilePath, $"static/terrain.{header.Label}")) ? new BinaryFileReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) : null,
             File.Exists(path = Path.Combine(FilePath, $"runtime/nonfixed.{header.Label}")) ? new BinaryFileReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) : null,
         };
         ReadGroup(header, loadAll, info);
     }
     return(Records);
 }
Ejemplo n.º 11
0
 public Task <byte[]> LoadDataLabelAsync(string filePath) => _streamSink.LoadDataLabelAsync(filePath, () =>
 {
     var labels = FromPath(filePath);
     var group  = Format != GameFormat.TES3 ? labels.Aggregate(this, (a, b) => a.GroupByLabel[b]) : this;
     var ms     = new MemoryStream();
     var info   = new StreamSink.DataInfo {
         Data = data => ms.Write(data, 0, data.Length)
     };
     foreach (var header in group.Headers)
     {
         ReadGroup(header, false, info);
     }
     ms.Position = 0;
     return(Task.FromResult(ms.ToArray()));
 });
Ejemplo n.º 12
0
 protected RecordGroup ReadTES3(Header header, bool loadAll, StreamSink.DataInfo info)
 {
     using (var ms = new MemoryStream())
         using (var w = new BinaryFileWriter(ms))
         {
             header.Write(w, Format);
             ms.Position = 0;
             var headerBytes = ms.ToArray();
             info?.AddGroup(header.Label, headerBytes);
         }
     //var group = new RecordGroup(_streamSink, _r, FilePath, Format, RecordLevel, Depth + 1);
     AddHeader(header, info);
     if (loadAll || info != null)
     {
         Load(loadAll, info);
     }
     ReadTES3Transform(this, info);
     return(this);
 }
Ejemplo n.º 13
0
        public void SinkDataContains(string path)
        {
            var bytes = _streamSink.GetDataContains(() =>
            {
                var info = new StreamSink.DataInfo();
                Read(info);
                return(info.ToArray());
            });

            if (_streamSink is StreamSinkServer)
            {
                return;
            }
            // write data
            if (path != null)
            {
                File.WriteAllBytes(Path.Combine(path, ".set"), bytes);
            }
            ReadGRUP(new StreamSink.DataInfo(bytes), path);
        }
Ejemplo n.º 14
0
        protected RecordGroup ReadGRUP(StreamSink.DataInfo info, string path)
        {
            var stack = new Stack <RecordGroup>();

            if (GroupByLabel == null)
            {
                GroupByLabel = new Dictionary <string, RecordGroup>();
            }
            var         groupByLabel = GroupByLabel;
            var         r            = new BinaryFileReader(new MemoryStream());
            var         depth        = 0;
            RecordGroup group        = this;

            info.Decoder(
                group: (label, headerData) =>
            {
                var filePath = ToPath(stack.Reverse().Select(x => x.Label).Concat(new[] { label }).ToArray());
                // write data
                if (path != null)
                {
                    var bytes = LoadDataLabelAsync(filePath).Result();
                    File.WriteAllBytes(Path.Combine(path, filePath), bytes);
                    return;
                }
                if (Format != GameFormat.TES3)
                {
                    if (!groupByLabel.TryGetValue(label, out group))
                    {
                        groupByLabel.Add(label, group = new RecordGroup(_streamSink, null, filePath, Format, RecordLevel, depth));
                    }
                    else
                    {
                        group = new RecordGroup(_streamSink, null, filePath, Format, RecordLevel, depth)
                        {
                            Next = group
                        }
                    }
                }
                ;
                r.Position = 0; r.BaseStream.Write(headerData, 0, headerData.Length); r.Position = 0;
                var header = new Header(group, r, Format)
                {
                    DataSize = 0, Position = 0
                };
                group.AddHeader(header, null);
            },
                enterGroup: () => { stack.Push(group); if (path == null)
                                    {
                                        groupByLabel = group.GroupByLabel = new Dictionary <string, RecordGroup>();
                                    }
                                    depth++; },
                leaveGroup: () => { group = stack.Pop(); if (path == null)
                                    {
                                        groupByLabel = group.GroupByLabel;
                                    }
                                    depth--; }
                );
            if (Format == GameFormat.TES3)
            {
                group.Load(true, info);
                ReadTES3Transform(group, info);
            }
            return(this);
        }