public static BitmapSource Load(string databaseRootPath, string package, string entryKey, double dpi = 96)
        {
            var packageFile = Path.Combine(databaseRootPath, package);
            var cacheKey    = $"{packageFile}:{entryKey}";

            return(PackageImageCache.GetOrCreate(cacheKey,
                                                 () =>
            {
                try
                {
                    using (var stream = new PackageStream(packageFile, entryKey))
                    {
                        var image = BitmapImageEx.FromStream(stream);
                        if (image.DpiX != dpi || image.DpiY != dpi)
                        {
                            return image.ChangeDPI(dpi);
                        }
                        return image;
                    }
                }
                catch (Exception)
                {
                    return null;
                }
            }));
        }
        public PackageStream(string packageFile, string path)
        {
#if !MOBILE
            log.InfoFormat("open package file {0} from {1}", path, packageFile);
#endif
            var zipFile = PackageStream.GetZipFile(PackageStream.NormalizePath(packageFile));

            if (zipFile == null)
            {
                throw PackageStream.FileNotFoundException(path, packageFile);
            }

            var entryIndex = zipFile.FindEntry(path, true);
            if (entryIndex == -1)
            {
                throw PackageStream.FileNotFoundException(path, packageFile);
            }

            try
            {
                _zipStream = zipFile.GetInputStream(entryIndex);
            }
            catch (Exception ex)
            {
                throw new IOException(string.Format("failed to open {0} in {1}", path, packageFile), ex);
            }
        }
Beispiel #3
0
        public static bool Exists(string databaseRootPath, string hyphenKey, TankIconType type = TankIconType.Normal)
        {
            if (hyphenKey == VirtualTankIconKey)
            {
                return(true);
            }

            return(PackageStream.IsFileExisted(Path.Combine(databaseRootPath, PackageImage.GuiPackage), TankIcon.GetEntryKey(hyphenKey, type)));
        }
Beispiel #4
0
        public static string[] GetFileEntries(string packageFile)
        {
            if (!File.Exists(packageFile))
            {
                return(null);
            }

            var zipFile = PackageStream.GetCachedZipFile(packageFile);

            return(zipFile.Cast <ZipEntry>().Select(e => e.Name).ToArray());
        }
Beispiel #5
0
        public static bool IsFileExisted(string packageFile, string path)
        {
            if (!File.Exists(packageFile))
            {
                return(false);
            }

            var zipFile = PackageStream.GetCachedZipFile(packageFile);

            return(zipFile.FindEntry(path, true) != -1);
        }
        public static bool Exists(string path)
        {
            string packagePath, localPath;

            UnifiedPath.ParsePath(path, out packagePath, out localPath);

            if (packagePath == null)
            {
                return(File.Exists(localPath));
            }

            return(PackageStream.IsFileExisted(packagePath, localPath));
        }
Beispiel #7
0
        public PackageStream(string packageFile, string path)
        {
            Log.InfoFormat("open package file {0} from {1}", path, packageFile);
            var zipFile = PackageStream.GetCachedZipFile(PackageStream.NormalizePath(packageFile));

            try
            {
                var entryIndex = zipFile.FindEntry(path, true);
                _zipStream = zipFile.GetInputStream(entryIndex);
            }
            catch (Exception)
            {
                throw new FileNotFoundException($"cannot find {path} in {packageFile}");
            }
        }
Beispiel #8
0
        public static BitmapSource Load(string databaseRootPath, string hyphenKey, double dpi = 96, TankIconType type = TankIconType.Normal)
        {
            if (hyphenKey == VirtualTankIconKey)
            {
                hyphenKey = "ussr-Observer";
            }

            var path = TankIcon.GetEntryKey(hyphenKey, type);

            if (PackageStream.IsFileExisted(Path.Combine(databaseRootPath, PackageImage.GuiPackage), path))
            {
                return(PackageImage.Load(databaseRootPath, PackageImage.GuiPackage, path));
            }
            return(PackageImage.Load(databaseRootPath, PackageImage.GuiPackage, TankIcon.GetEntryKey("noImage", type)));
        }
        public static long?GetCrc(string packageFile, string path)
        {
#if !MOBILE
            log.InfoFormat("open package file {0} from {1}", path, packageFile);
#endif
            var zipFile = PackageStream.GetZipFile(PackageStream.NormalizePath(packageFile));
            try
            {
                var entryIndex = zipFile.FindEntry(path, true);
                var entry      = zipFile[entryIndex];
                return(entry.HasCrc ? entry.Crc : (long?)null);
            }
            catch (Exception)
            {
                throw PackageStream.FileNotFoundException(path, packageFile);
            }
        }
Beispiel #10
0
        private static ZipFile GetCachedZipFile(string packageFile)
        {
            if (!File.Exists(packageFile))
            {
                return(null);
            }

            packageFile = PackageStream.NormalizePath(packageFile);

            if (!SCachedZipFiles.TryGetValue(packageFile, out ZipFile zipFile))
            {
                var stream = File.OpenRead(packageFile);

                zipFile = new ZipFile(stream);

                lock (SCachedZipFilesSyncObject)
                    SCachedZipFiles[packageFile] = zipFile;
            }

            return(zipFile);
        }