private void LoadGameData() { ResourceClasses = new List <ResourceClass>(); foreach (var resfile in VFS.GetFiles(@"ResourceClasses", "*.dat")) { using (var resfilestream = VFS.OpenStream(resfile)) ResourceClasses.Add(new ResourceClass(new DataFile(resfilestream))); } ResourceTemplates = new Dictionary <string, Resource>(); foreach (var resfile in VFS.GetFiles(@"Resources", "*.dat")) { using (var resfilestream = VFS.OpenStream(resfile)) { var res = new Resource(new DataFile(resfilestream)); ResourceTemplates.Add(res.Name, res); } } using (var hfilestream = VFS.OpenStream("human.dat")) HumanTemplate = new Human(new DataFile(hfilestream)); BuildingClasses = new Dictionary <string, Building>(); foreach (var bfile in VFS.GetFiles(@"Buildings", "*.dat")) { using (var bfilestream = VFS.OpenStream(bfile)) { var b = Building.FromDataFile(new DataFile(bfilestream)); BuildingClasses.Add(b.Name, b); } } WorkerTypes = BuildingClasses.Values.Select(b => b.WorkerType).Where(t => t != null).ToList(); }
Stream OpenLstIndex(ArcView file, string dat_name, HibikiDatScheme scheme) { var lst_name = Path.ChangeExtension(file.Name, ".lst"); if (VFS.FileExists(lst_name)) { return(VFS.OpenStream(lst_name)); } else if ("init.dat" == dat_name) { return(file.CreateStream()); } // try to open 'init.dat' archive in the same directory var init_dat = VFS.CombinePath(VFS.GetDirectoryName(file.Name), "init.dat"); if (!VFS.FileExists(init_dat)) { return(file.CreateStream()); } try { using (var init = VFS.OpenView(init_dat)) using (var init_arc = TryOpenWithScheme(init, ReadCount(init), scheme)) { lst_name = Path.GetFileName(lst_name); var lst_entry = init_arc.Dir.First(e => e.Name == lst_name); return(init_arc.OpenEntry(lst_entry)); } } catch { return(file.CreateStream()); } }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { BitmapPalette palette = null; PixelFormat format = PixelFormats.Gray8; foreach (var pal_name in GetPaletteNames(info.FileName)) { if (!VFS.FileExists(pal_name)) { continue; } try { using (var pal = VFS.OpenStream(pal_name)) { palette = ReadPalette(pal, 0x100, PaletteFormat.Bgr); format = PixelFormats.Indexed8; } } catch { /* ignore palette read errors */ } break; } file.Position = 0x10; var pixels = file.ReadBytes((int)info.Width * (int)info.Height); return(ImageData.Create(info, format, palette, pixels)); }
public override ImageData Read(Stream stream, ImageMetaData info) { var meta = (DziMetaData)info; PixelFormat format = PixelFormats.Bgra32; var bitmap = new WriteableBitmap((int)meta.Width, (int)meta.Height, ImageData.DefaultDpiX, ImageData.DefaultDpiY, format, null); int actual_width = 0; int actual_height = 0; byte[] pixels = null; foreach (var tile in meta.Tiles.First()) { var image_entry = VFS.GetFiles(tile.FileName + ".*").FirstOrDefault(); if (null == image_entry) { throw new FileNotFoundException("Tile not found", tile.FileName); } using (var input = VFS.OpenStream(image_entry)) { var image = Read(image_entry.Name, input); if (null == image) { throw new FileFormatException("Unknown DZI tile format"); } var converted = image.Bitmap; if (converted.Format != format) { converted = new FormatConvertedBitmap(converted, format, null, 0); } int stride = converted.PixelWidth * 4; int tile_size = stride * converted.PixelHeight; if (null == pixels || pixels.Length < tile_size) { pixels = new byte[tile_size]; } converted.CopyPixels(pixels, stride, 0); var width = Math.Min(converted.PixelWidth, bitmap.PixelWidth - tile.X); var height = Math.Min(converted.PixelHeight, bitmap.PixelHeight - tile.Y); var rect = new Int32Rect(tile.X, tile.Y, width, height); bitmap.WritePixels(rect, pixels, stride, 0); if (tile.X + width > actual_width) { actual_width = tile.X + width; } if (tile.Y + height > actual_height) { actual_height = tile.Y + height; } } } BitmapSource result = bitmap; if (actual_width < bitmap.PixelWidth || actual_height < bitmap.PixelHeight) { var rect = new Int32Rect(0, 0, actual_width, actual_height); result = new CroppedBitmap(bitmap, rect); } result.Freeze(); return(new ImageData(result, meta)); }
public override ImageData Read(IBinaryStream file, ImageMetaData info) { file.Position = 12; var pixels = file.ReadBytes((int)info.Width * (int)info.Height); var format = PixelFormats.Gray8; BitmapPalette palette = null; var palette_name = VFS.ChangeFileName(file.Name, "data.act"); if (VFS.FileExists(palette_name)) { using (var pal_file = VFS.OpenStream(palette_name)) palette = ReadPalette(pal_file, 0x100, PaletteFormat.Rgb); format = PixelFormats.Indexed8; } return(ImageData.Create(info, format, palette, pixels)); }
public override ArcFile TryOpen(ArcView file) { var base_dir = VFS.GetDirectoryName(file.Name); var base_name = Path.GetFileNameWithoutExtension(file.Name); var list_file = VFS.CombinePath(base_dir, base_name + "l.dat"); if (!VFS.FileExists(list_file)) { return(null); } string index; using (var ls = VFS.OpenStream(list_file)) using (var zls = new ZLibStream(ls, CompressionMode.Decompress)) using (var reader = new StreamReader(zls, Encodings.cp932)) { index = reader.ReadToEnd(); } if (string.IsNullOrEmpty(index)) { return(null); } var dir = new List <Entry>(); var match = IndexEntryRe.Match(index); while (match.Success) { var entry = new Entry { Name = match.Groups[1].Value, Offset = UInt32.Parse(match.Groups[3].Value), Size = UInt32.Parse(match.Groups[2].Value), }; if (!entry.CheckPlacement(file.MaxOffset)) { return(null); } dir.Add(entry); match = match.NextMatch(); } if (0 == dir.Count) { return(null); } return(new ArcFile(file, this, dir)); }
internal BitmapPalette ReadDefaultPalette(string filename) { var pal_name = Path.ChangeExtension(filename, ".pal"); if (!VFS.FileExists(pal_name)) { pal_name = VFS.ChangeFileName(filename, DefaultPaletteName); } if (!VFS.FileExists(pal_name)) { return(null); } using (var input = VFS.OpenStream(pal_name)) { return(ReadPalette(input, 0x100, PaletteFormat.Rgb)); } }
public ImageData Read(IBinaryStream file, BmpMetaData info) { if (info.BPP != 24 && info.BPP != 32 || !file.CanSeek) { return(null); } var alp_name = Path.ChangeExtension(info.FileName, "alp"); if (alp_name.Equals(info.FileName, StringComparison.InvariantCultureIgnoreCase) || !VFS.FileExists(alp_name)) { return(null); } var alpha_size = info.Width * info.Height; var alpha = new byte[alpha_size]; using (var alp = VFS.OpenStream(alp_name)) { if (alpha.Length != alp.Read(alpha, 0, alpha.Length) || alp.ReadByte() != -1) { return(null); } } file.Position = info.HeaderLength; int dst_stride = (int)info.Width * 4; int gap = -((int)info.Width * info.BPP / 8) & 3; var pixels = new byte[(int)info.Height * dst_stride]; int src_pixel_size = info.BPP / 8; int dst = (int)(info.Height - 1) * dst_stride; for (int y = (int)info.Height - 1; y >= 0; --y) { int a_src = (int)info.Width * y; for (int x = 0; x < dst_stride; x += 4) { file.Read(pixels, dst + x, src_pixel_size); pixels[dst + x + 3] = alpha[a_src++]; } if (gap != 0) { file.Seek(gap, SeekOrigin.Current); } dst -= dst_stride; } return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, dst_stride)); }
private void PlayFile(Entry entry) { SoundInput sound = null; try { SetBusyState(); using (var input = VFS.OpenStream(entry)) { FormatCatalog.Instance.LastError = null; sound = AudioFormat.Read(input); if (null == sound) { if (null != FormatCatalog.Instance.LastError) { throw FormatCatalog.Instance.LastError; } return; } if (AudioDevice != null) { AudioDevice.PlaybackStopped -= OnPlaybackStopped; AudioDevice = null; } CurrentAudio = new WaveStreamImpl(sound); AudioDevice = new WaveOutEvent(); AudioDevice.Init(CurrentAudio); AudioDevice.PlaybackStopped += OnPlaybackStopped; AudioDevice.Play(); var fmt = CurrentAudio.WaveFormat; SetResourceText(string.Format("Playing {0} / {3} / {2}bps / {1}Hz", entry.Name, fmt.SampleRate, sound.SourceBitrate / 1000, CurrentAudio.TotalTime.ToString("m':'ss"))); } } catch (Exception X) { SetStatusText(X.Message); if (null != sound) { sound.Dispose(); } } }
public override ArcFile TryOpen(ArcView file) { if (!file.View.AsciiEqual(0, "BinaryCombineData")) { return(null); } var bcl_name = Path.ChangeExtension(file.Name, "bcl"); using (var bcl = VFS.OpenStream(bcl_name)) using (var index = new StreamReader(bcl, Encodings.cp932)) { if (index.ReadLine() != "[BinaryCombineData]") { return(null); } var filename = index.ReadLine(); if (!VFS.IsPathEqualsToFileName(file.Name, filename)) { return(null); } index.ReadLine(); var dir = new List <Entry>(); while ((filename = index.ReadLine()) != null) { if (!filename.StartsWith("[") || !filename.EndsWith("]")) { return(null); } filename = filename.Substring(1, filename.Length - 2); var offset = index.ReadLine(); var size = index.ReadLine(); index.ReadLine(); var entry = Create <Entry> (filename); entry.Offset = UInt32.Parse(offset); entry.Size = UInt32.Parse(size); if (!entry.CheckPlacement(file.MaxOffset)) { return(null); } dir.Add(entry); } return(new ArcFile(file, this, dir)); } }
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // overall textures GrassTexture = Content.Load <Texture2D>("Textures/grass"); // resource textures foreach (var restemplate in PlarfGame.Instance.ResourceTemplates) { if (!MiscTextures.ContainsKey(restemplate.Value.Texture) && !string.IsNullOrWhiteSpace(restemplate.Value.Texture)) { using (var stream = VFS.OpenStream(restemplate.Value.Texture)) MiscTextures.Add(restemplate.Value.Texture, TextureHelpers.FromStream(stream, GraphicsDevice)); } } // building textures foreach (var bclass in PlarfGame.Instance.BuildingClasses) { if (!MiscTextures.ContainsKey(bclass.Value.Texture) && !string.IsNullOrWhiteSpace(bclass.Value.Texture)) { using (var stream = VFS.OpenStream(bclass.Value.Texture)) MiscTextures.Add(bclass.Value.Texture, TextureHelpers.FromStream(stream, GraphicsDevice)); } } // actor resources if (!MiscTextures.ContainsKey(PlarfGame.Instance.HumanTemplate.Texture) && !string.IsNullOrWhiteSpace(PlarfGame.Instance.HumanTemplate.Texture)) { using (var stream = VFS.OpenStream(PlarfGame.Instance.HumanTemplate.Texture)) MiscTextures.Add(PlarfGame.Instance.HumanTemplate.Texture, TextureHelpers.FromStream(stream, GraphicsDevice)); } // the line texture LineTexture = new Texture2D(GraphicsDevice, 1, 1); LineTexture.SetData(new Color[] { Color.White }); // fonts DefaultFont = Content.Load <SpriteFont>("Fonts/DefaultFont"); }
public override ArcFile TryOpen(ArcView file) { if (!file.Name.EndsWith("DAT.bytes", StringComparison.OrdinalIgnoreCase)) { return(null); } var inf_name = file.Name.Substring(0, file.Name.Length - "DAT.bytes".Length); inf_name += "INF.bytes"; if (!VFS.FileExists(inf_name)) { return(null); } using (var inf = VFS.OpenStream(inf_name)) { var bin = new BinaryFormatter { Binder = new SpTypeBinder() }; var list = bin.Deserialize(inf) as List <LinkerInfo>; if (null == list || 0 == list.Count) { return(null); } var base_name = Path.GetFileNameWithoutExtension(file.Name); string type = ""; if (base_name.StartsWith("WAVE", StringComparison.OrdinalIgnoreCase)) { type = "audio"; } else if (base_name.StartsWith("CG", StringComparison.OrdinalIgnoreCase)) { type = "image"; } var dir = list.Select(e => new Entry { Name = e.name, Type = type, Offset = e.offset, Size = (uint)e.size }).ToList(); return(new ArcFile(file, this, dir)); } }
public override ArcFile TryOpen(ArcView file) { int count = file.View.ReadInt32(4); if (!IsSaneCount(count) || VFS.IsPathEqualsToFileName(file.Name, "00.mpk")) { return(null); } var list_name = VFS.ChangeFileName(file.Name, "00.mpk"); List <string> filelist; if (VFS.FileExists(list_name)) { using (var s = VFS.OpenStream(list_name)) using (var xs = new XoredStream(s, 0xA)) using (var reader = new StreamReader(xs, Encodings.cp932)) { filelist = new List <string> (count); string filename; while ((filename = reader.ReadLine()) != null) { filelist.Add(filename); } } } else { var base_name = Path.GetFileNameWithoutExtension(file.Name); filelist = Enumerable.Range(0, count).Select(x => string.Format("{0}#{1:D4}", base_name, x)).ToList(); } bool has_sizes = file.View.ReadByte(3) != 'P'; uint index_offset = 8; uint record_size = has_sizes ? 8u : 4u; var dir = new List <Entry> (count); for (int i = 0; i < count; ++i) { var entry = FormatCatalog.Instance.Create <Entry> (filelist[i]); entry.Offset = file.View.ReadUInt32(index_offset); if (has_sizes) { entry.Size = file.View.ReadUInt32(index_offset + 4); if (!entry.CheckPlacement(file.MaxOffset)) { return(null); } } else if (entry.Offset > file.MaxOffset) { return(null); } dir.Add(entry); index_offset += record_size; } if (!has_sizes) { for (int i = 1; i < count; ++i) { dir[i - 1].Size = (uint)(dir[i].Offset - dir[i - 1].Offset); } dir[dir.Count - 1].Size = (uint)(file.MaxOffset - dir[dir.Count - 1].Offset); } return(new ArcFile(file, this, dir)); }
public override ArcFile TryOpen(ArcView file) { if (!file.Name.HasExtension(".dat")) { return(null); } int count = (short)(file.View.ReadUInt16(0) ^ 0x8080); if (!IsSaneCount(count)) { return(null); } var scheme = QueryScheme(file.Name); if (null == scheme) { return(null); } var dat_name = Path.GetFileName(file.Name).ToLowerInvariant(); IList <HibikiTocRecord> toc_table = null; if (scheme.ArcMap != null && scheme.ArcMap.TryGetValue(dat_name, out toc_table)) { if (toc_table.Count != count) { toc_table = null; } } var lst_name = Path.ChangeExtension(file.Name, ".lst"); Stream input; if (VFS.FileExists(lst_name)) { input = VFS.OpenStream(lst_name); } else { input = file.CreateStream(); } using (var dec = new XoredStream(input, 0x80)) using (var index = new BinaryReader(dec)) { const int name_length = 0x100; index.BaseStream.Position = 2; Func <int, Entry> read_entry; if (null == toc_table) { var name_buf = new byte[name_length]; read_entry = i => { if (name_length != index.Read(name_buf, 0, name_length)) { return(null); } var name = Binary.GetCString(name_buf, 0); var entry = FormatCatalog.Instance.Create <Entry> (name); index.ReadUInt16(); entry.Size = index.ReadUInt32(); entry.Offset = index.ReadUInt32(); return(entry); }; } else { read_entry = i => { index.BaseStream.Seek(name_length + 6, SeekOrigin.Current); index.ReadUInt32(); // throws in case of EOF var toc_entry = toc_table[i]; var entry = FormatCatalog.Instance.Create <Entry> (toc_entry.Name); entry.Offset = toc_entry.Offset; entry.Size = toc_entry.Size; return(entry); }; } var dir = new List <Entry> (count); for (int i = 0; i < count; ++i) { var entry = read_entry(i); if (!entry.CheckPlacement(file.MaxOffset)) { return(null); } dir.Add(entry); } return(new HibikiArchive(file, this, dir, scheme.ContentKey)); } }