Exemplo n.º 1
0
        public void Load(IReadOnlyFileSystem fileSystem)
        {
            Stream stream;

            if (!fileSystem.TryOpen(Filename, out stream))
            {
                return;
            }

            try
            {
                Exists = true;
                foreach (var loader in Game.ModData.SoundLoaders)
                {
                    ISoundFormat soundFormat;
                    if (loader.TryParseSound(stream, out soundFormat))
                    {
                        Length = (int)soundFormat.LengthInSeconds;
                        soundFormat.Dispose();
                        break;
                    }
                }
            }
            finally
            {
                stream.Dispose();
            }
        }
Exemplo n.º 2
0
            public LvlPackage(Stream s, string filename, IReadOnlyFileSystem context)
            {
                stream = s;
                Name   = filename;

                var lvlLookup  = new Dictionary <string, string>();
                var lookupPath = $"archives/{Path.GetFileName(filename)}.yaml";

                Stream s2;

                if (context.TryOpen(lookupPath, out s2))
                {
                    lvlLookup = MiniYaml.FromStream(s2).ToDictionary(x => x.Key, x => x.Value.Value);
                }

                var fileTypeListOffset = s.ReadInt32();

                s.Position = fileTypeListOffset;

                int firstFileListOffset = 0;

                for (var i = 0; s.Position < s.Length; i++)
                {
                    s.Position = fileTypeListOffset + i * 8;

                    var fileType       = s.ReadASCII(4);
                    var fileListOffset = s.ReadInt32();

                    // List terminator reached.
                    if (fileListOffset == 0)
                    {
                        break;
                    }

                    // We need this to calculate the last fileLength.
                    if (firstFileListOffset == 0)
                    {
                        firstFileListOffset = fileListOffset;
                    }

                    // To determine when this list ends, check the next entry
                    s.Position += 4;
                    var fileListEndOffset = s.ReadInt32();

                    // List terminator reached, so assume the list goes on till the fileTypeList starts.
                    if (fileListEndOffset == 0)
                    {
                        fileListEndOffset = fileTypeListOffset;
                    }

                    s.Position = fileListOffset;

                    for (var j = 0; s.Position < fileListEndOffset; j++)
                    {
                        var fileOffset = s.ReadInt32();

                        // Removed file, still increments fileId.
                        if (fileOffset == 0)
                        {
                            continue;
                        }

                        // As the fileLength is nowhere stored, but files always follow in order, calculate the previous fileLength.
                        if (index.Count > 0)
                        {
                            var entry = index.ElementAt(index.Count - 1).Value;
                            entry[1] = fileOffset - entry[0];
                        }

                        var assetFileName = $"{j}.{fileType.ToLower()}";

                        // Lookup assumed original filename for better readability in yaml files.
                        if (lvlLookup.ContainsKey(assetFileName))
                        {
                            assetFileName = lvlLookup[assetFileName];
                        }
                        else
                        {
                            lvlLookup.Add(assetFileName, assetFileName);
                        }

                        index.Add(assetFileName, new int[] { fileOffset, 0 });
                    }
                }

                if (index.Count <= 0)
                {
                    return;
                }

                // Calculate the last fileLength.
                var lastEntry = index.ElementAt(index.Count - 1).Value;

                lastEntry[1] = firstFileListOffset - lastEntry[0];
            }