Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static TextureList FromFile(GraphicsDevice Device, string FileName)
- {
- if (Device == null) throw new ArgumentException("Device is null.");
- if (String.IsNullOrEmpty(FileName)) throw new ArgumentException("File name is empty.");
- if (!FileName.EndsWith(".fpng")) throw new ArgumentException(String.Format("File {0} extension not '.fpng'. ", FileName));
- if (!File.Exists(FileName)) throw new Exception(String.Format("File {0} not found.", FileName));
- TextureList Result = null;
- ZipStorer Zip = null;
- MemoryStream DescXmlUnzip = null;
- MemoryStream DescTexUnzip = null;
- Texture2D Tex = null;
- try
- {
- Zip = ZipStorer.Open(FileName, FileAccess.Read);
- List<ZipStorer.ZipFileEntry> Entries = Zip.ReadCentralDir();
- // взять первый попавшийся хмл
- ZipStorer.ZipFileEntry? DescXml = GetEntryWithEx(Entries, ".xml");
- if (!DescXml.HasValue) throw new Exception("Invalid file format. Say No XML!");
- // распаковатьего в память
- DescXmlUnzip = new MemoryStream((int)DescXml.Value.FileSize);
- if (!Zip.ExtractFile(DescXml.Value, DescXmlUnzip)) throw new Exception(String.Format("Xml {0} unzip fail.", DescXml.Value.FilenameInZip));
- // попытаться разобрать его
- XmlTextureList XmlTex = new XmlTextureList(String.Format("{0}:{1}", FileName, DescXml.Value.FilenameInZip), DescXmlUnzip);
- // потом загрузить текстуру с нужным именем из того же архива
- ZipStorer.ZipFileEntry? DescTex = GetFileByName(Entries, XmlTex.TexName);
- if (!DescTex.HasValue) throw new Exception(String.Format("Texture with name {0} not found. XmlFile are {1}", XmlTex.TexName, DescXml.Value.FilenameInZip));
- // распаковатьего в ее память
- DescTexUnzip = new MemoryStream((int)DescTex.Value.FileSize);
- if (!Zip.ExtractFile(DescTex.Value, DescTexUnzip)) throw new Exception(String.Format("Texture {0} unzip fail.", DescTex.Value.FilenameInZip));
- // создать ее
- Tex = Texture2D.FromFile(Device, DescTexUnzip);
- // все
- Result = new TextureList(Tex, XmlTex.FrameCount);
- }
- catch (Exception E)
- {
- throw new Exception(String.Format("Fail loading file {0}", FileName), E);
- }
- finally
- {
- if (Zip != null) Zip.Close();
- if (DescXmlUnzip != null) DescXmlUnzip.Close();
- if (DescTexUnzip != null) DescTexUnzip.Close();
- // лол
- if ((Result == null) && (Tex != null)) Tex.Dispose();
- }
- return Result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement