Advertisement
Guest User

Finally

a guest
Apr 18th, 2016
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. public static TextureList FromFile(GraphicsDevice Device, string FileName)
  2. {
  3.     if (Device == null) throw new ArgumentException("Device is null.");
  4.     if (String.IsNullOrEmpty(FileName)) throw new ArgumentException("File name is empty.");
  5.     if (!FileName.EndsWith(".fpng")) throw new ArgumentException(String.Format("File {0} extension not '.fpng'. ", FileName));
  6.  
  7.     if (!File.Exists(FileName)) throw new Exception(String.Format("File {0} not found.", FileName));
  8.  
  9.     TextureList Result = null;
  10.     ZipStorer Zip = null;
  11.     MemoryStream DescXmlUnzip = null;
  12.     MemoryStream DescTexUnzip = null;
  13.     Texture2D Tex = null;
  14.  
  15.     try
  16.     {
  17.         Zip = ZipStorer.Open(FileName, FileAccess.Read);
  18.  
  19.         List<ZipStorer.ZipFileEntry> Entries = Zip.ReadCentralDir();
  20.  
  21.         // взять первый попавшийся хмл
  22.         ZipStorer.ZipFileEntry? DescXml = GetEntryWithEx(Entries, ".xml");
  23.         if (!DescXml.HasValue) throw new Exception("Invalid file format. Say No XML!");
  24.  
  25.         // распаковатьего в память
  26.         DescXmlUnzip = new MemoryStream((int)DescXml.Value.FileSize);
  27.         if (!Zip.ExtractFile(DescXml.Value, DescXmlUnzip)) throw new Exception(String.Format("Xml {0} unzip fail.", DescXml.Value.FilenameInZip));
  28.  
  29.         // попытаться разобрать его
  30.         XmlTextureList XmlTex = new XmlTextureList(String.Format("{0}:{1}", FileName, DescXml.Value.FilenameInZip), DescXmlUnzip);
  31.  
  32.         // потом загрузить текстуру с нужным именем из того же архива
  33.         ZipStorer.ZipFileEntry? DescTex = GetFileByName(Entries, XmlTex.TexName);
  34.         if (!DescTex.HasValue) throw new Exception(String.Format("Texture with name {0} not found. XmlFile are {1}", XmlTex.TexName, DescXml.Value.FilenameInZip));
  35.        
  36.         // распаковатьего в ее память
  37.         DescTexUnzip = new MemoryStream((int)DescTex.Value.FileSize);
  38.         if (!Zip.ExtractFile(DescTex.Value, DescTexUnzip)) throw new Exception(String.Format("Texture {0} unzip fail.", DescTex.Value.FilenameInZip));
  39.  
  40.         // создать ее
  41.         Tex = Texture2D.FromFile(Device, DescTexUnzip);
  42.  
  43.         // все
  44.         Result = new TextureList(Tex, XmlTex.FrameCount);
  45.     }
  46.     catch (Exception E)
  47.     {
  48.         throw new Exception(String.Format("Fail loading file {0}", FileName), E);
  49.     }
  50.     finally
  51.     {
  52.         if (Zip != null) Zip.Close();
  53.  
  54.         if (DescXmlUnzip != null) DescXmlUnzip.Close();
  55.         if (DescTexUnzip != null) DescTexUnzip.Close();
  56.  
  57.         // лол
  58.         if ((Result == null) && (Tex != null)) Tex.Dispose();
  59.     }
  60.  
  61.     return Result;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement