public T FromStream <T>(Stream stream) where T : class { if (typeof(T) != typeof(Texture2D)) { throw AssetLoadException.FromInvalidReader <RawImgReader, T>(); } return(ImageIO.RawToTexture2D(_graphicsDevice, stream) as T); }
private Exception _Extract() { StreamWriter log = null; try { var dir = Path.Combine(Main.SavePath, "Mod Reader", mod.Name); if (Directory.Exists(dir)) { Directory.Delete(dir, true); } Directory.CreateDirectory(dir); log = new StreamWriter(Path.Combine(dir, "tModReader.txt")) { AutoFlush = true }; if (mod.properties.hideCode) { log.WriteLine(Language.GetTextValue("tModLoader.ExtractHideCodeMessage")); } else if (!mod.properties.includeSource) { log.WriteLine(Language.GetTextValue("tModLoader.ExtractNoSourceCodeMessage")); } if (mod.properties.hideResources) { log.WriteLine(Language.GetTextValue("tModLoader.ExtractHideResourcesMessage")); } log.WriteLine(Language.GetTextValue("tModLoader.ExtractFileListing")); int i = 0; void WriteFile(string name, byte[] content) { //this access is not threadsafe, but it should be atomic enough to not cause issues loadProgress.SetText(name); loadProgress.SetProgress(i++ / (float)mod.modFile.FileCount); bool hidden = codeExtensions.Contains(Path.GetExtension(name)) ? mod.properties.hideCode : mod.properties.hideResources; if (hidden) { log.Write("[hidden] "); } log.WriteLine(name); if (!hidden) { if (name == "Info") { name = "build.txt"; } var path = Path.Combine(dir, name); Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllBytes(path, content); } } mod.modFile.Read(TmodFile.LoadedState.Streaming, (name, len, reader) => { byte[] data = reader.ReadBytes(len); // check if subject is rawimg, then read it as rawimg and convert back to png if (name.EndsWith(".rawimg")) { using (var ms = new MemoryStream(data)) { var img = ImageIO.RawToTexture2D(Main.instance.GraphicsDevice, ms); using (var pngstream = new MemoryStream()) { img.SaveAsPng(pngstream, img.Width, img.Height); data = pngstream.ToArray(); } } name = Path.ChangeExtension(name, "png"); } WriteFile(name, data); }); foreach (var entry in mod.modFile) { WriteFile(entry.Key, entry.Value); } } catch (Exception e) { log?.WriteLine(e); return(e); } finally { log?.Close(); mod?.modFile.UnloadAssets(); } return(null); }