Exemplo n.º 1
0
        internal override Object Get(ResourceType resType, IEnumerable<String> locator)
        {
            if (locator.Count() > 1) {
                var inner = GetInnerArchive(locator.First());
                if (inner == null) return null;
                return inner.Get(resType, locator.Skip(1));
            }

            var joined = Path.Combine(_directory, Path.Combine(locator.ToArray()));
            var path = resType.Extensions.Select(x => String.Format("{0}{1}", joined, x))
                .FirstOrDefault(x => File.Exists(x));

            if (path == null) return null;

            using (var stream = File.OpenRead(path)) {
                return resType.Load(stream);
            }
        }
Exemplo n.º 2
0
        internal override Object Get(ResourceType resType, IEnumerable<String> locator)
        {
            if (locator.Count() == 0) {
                throw new ArgumentException("No resource location given");
            }
            
            var name = locator.First();

            if (locator.Count() > 1) {
                if (!_innerArchives.ContainsKey(name)) return null;

                return _innerArchives[name].Get(resType, locator.Skip(1));
            }

            if (!_resPositions.ContainsKey(resType)) return null;

            var dict = _resPositions[resType];

            if (!dict.ContainsKey(name)) return null;

            lock (_stream) {
                var position = dict[name];
                var bytes = new byte[position.Length];

                _stream.Seek(position.Offset, SeekOrigin.Begin);
                _stream.Read(bytes, 0, (int) position.Length);
                
                if (resType.Format.HasFlag(ResourceFormat.Compressed)) {
                    using (var srcStream = new MemoryStream(bytes)) {
                        using (var zipStream = new GZipStream(srcStream, CompressionMode.Decompress)) {
                            using (var dstStream = new MemoryStream()) {
                                zipStream.CopyTo(dstStream);
                                dstStream.Seek(0, SeekOrigin.Begin);
                                return resType.Load(dstStream);
                            }
                        }
                    }
                } else {
                    using (var memStream = new MemoryStream(bytes)) {
                        return resType.Load(memStream);
                    }
                }    
            }
        }