public override void Import (Stream input, Stream output, string filename) { var reader = new BinaryReader (input); int chunkID = reader.ReadInt32 (); if (chunkID != RIFF) throw new InvalidDataException (); reader.ReadInt32 (); // fileSize int riffType = reader.ReadInt32 (); if (riffType != WAVE) throw new InvalidDataException (); while (reader.ReadInt32 () != FMT_) { var dummy = reader.ReadInt32 (); reader.ReadBytes (dummy); } int fmtSize = reader.ReadInt32 (); int fmtCode = reader.ReadInt16 (); int channels = reader.ReadInt16 (); int sampleRate = reader.ReadInt32 (); reader.ReadInt32 (); // fmtAvgBPS reader.ReadInt16 (); // fmtBlockAlign int bitDepth = reader.ReadInt16 (); if (fmtSize == 18) { int fmtExtraSize = reader.ReadInt16 (); reader.ReadBytes (fmtExtraSize); } while (reader.ReadInt32 () != DATA) { var dummy = reader.ReadInt32 (); reader.ReadBytes (dummy); } int dataSize = reader.ReadInt32 (); byte[] byteArray = reader.ReadBytes (dataSize); if (fmtCode != WAVE_FORMAT_PCM && fmtCode != WAVE_FORMAT_IEEE_FLOAT) throw new NotSupportedException ("Wave files must be PCM or IEEE_FLOAT format."); var md = new SfxMetadata () { Bits = (fmtCode == WAVE_FORMAT_IEEE_FLOAT) ? 32 : bitDepth, Rate = sampleRate, Channels = channels, Length = dataSize / (bitDepth / 8) / channels, }; using (var tw = new TarWriter(output)) { using (var ms = new MemoryStream()) { using (var bw = new BinaryWriter(ms)) { md.Write (bw); ms.Position = 0; tw.Write (ms, ms.Length, "sound.bin"); } } tw.Write (new MemoryStream (byteArray), byteArray.Length, "sound.pcm"); } }
public override void Import (string input, Stream output) { var ser = new JsonSerializer(); AtlasMetadata metadata = null; string metaPath = "atlas.meta"; if (File.Exists(metaPath)) { using (var sr = new StreamReader(metaPath)) { metadata = ser.Deserialize<AtlasMetadata>(new JsonTextReader(sr)); } } else metadata = new AtlasMetadata(); var lp = new LayoutProperties { inputFilePaths = (Directory.Exists("./sprites") ? Directory.GetFiles("./sprites") : Directory.GetFiles(".")) .Where(p => SupportedFormats.Contains(Path.GetExtension(p).ToLower())).ToArray(), distanceBetweenImages = metadata.Padding, marginWidth = metadata.Margin, powerOfTwo = metadata.PowerOfTwo, maxSpriteWidth = metadata.MaxSpriteWidth > 0 ? metadata.MaxSpriteWidth : 16384, maxSpriteHeight = metadata.MaxSpriteHeight > 0 ? metadata.MaxSpriteHeight : 16384, filterMode = metadata.FilterMode, }; var sheetMaker = new AtlasBuilder(lp); using (var tw = new TarWriter(output)) { using (MemoryStream atlasStream = new MemoryStream(), sheetStream = new MemoryStream()) { using (var bw = new BinaryWriter(atlasStream)) { sheetMaker.Create(bw, sheetStream, ImageFormat.Png, metadata.NoPreMultiply); bw.Flush(); atlasStream.Position = 0; sheetStream.Position = 0; tw.Write(atlasStream, atlasStream.Length, "atlas.bin"); tw.Write(sheetStream, sheetStream.Length, "sheet.png"); } } } }
public override void Import (Stream input, Stream output, string filename) { using (var importer = new AssimpContext()) { var scene = importer.ImportFileFromStream(input, PostProcessSteps.Triangulate | PostProcessSteps.SortByPrimitiveType | PostProcessSteps.GenerateNormals, Path.GetExtension(filename)); using (var tw = new TarWriter(output)) { var textures = new Dictionary<string, string>(); using (var ms = new MemoryStream()) { using (var bw = new BinaryWriter(ms)) { this.WriteModel(scene, bw, textures); bw.Flush(); //this.PrintNode(scene, scene.RootNode, 0); ms.Position = 0; tw.Write(ms, ms.Length, "model.bin"); } } this.WriteTextures(scene, tw, textures); } } }
unsafe void WriteTextures (Scene scene, TarWriter tw, Dictionary<string, string> textures) { for (var i = 0; i < scene.TextureCount; i++) { var tex = scene.Textures[i]; var name = "*" + i; if (!textures.ContainsKey(name)) continue; textures.Remove(name); Bitmap bmp = null; if (tex.HasCompressedData) { using (var ms = new MemoryStream(tex.CompressedData)) { bmp = new Bitmap(ms); } } else if (tex.HasNonCompressedData) { fixed(Texel *p = tex.NonCompressedData) { bmp = new Bitmap(tex.Width, tex.Height, tex.Width * 4, PixelFormat.Format32bppArgb, (IntPtr)p); } } if (bmp != null) { using (var ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Png); ms.Position = 0; tw.Write(ms, ms.Length, name + ".png"); } } } foreach (var kvp in textures) { try { var img = ImageLoader.Load(kvp.Key); using (var ms = new MemoryStream()) { img.Save(ms, ImageFormat.Png); ms.Position = 0; tw.Write(ms, ms.Length, kvp.Value + ".png"); } } catch (Exception ex) { throw new ContentException("Could not load referenced texture: " + kvp.Key, ex); } } }
public override void Import (string input, Stream output) { var sr = new StreamReader(Path.GetFileNameWithoutExtension(input) + ".fnt"); string fontFace = "", textureFile = null; int fontSize = 0; float lineHeight = 0f, lineBase = 0f, scaleW = 0f, scaleH = 0f, pixelScale = 0f; string line; var attrs = new Dictionary<string, string> (); var kernings = new Dictionary<ulong, float> (); var chars = new List<Char> (); while ((line = sr.ReadLine ()) != null) { var inQuotes = false; var ch = line.ToCharArray (); for (var i = 0; i < line.Length; i++) { if (ch [i] == '\"' /*&& (i == 0 || ch [i - 1] != '\\')*/) inQuotes = !inQuotes; if (!inQuotes && ch [i] == ' ') ch [i] = '\t'; if (!inQuotes && (ch[i] == '<' || ch[i] == '>' || ch[i] == '/' && i + 1 < ch.Length && ch[i + 1] == '>')) ch[i] = '\t'; } line = new string (ch); var parts = line.Split (new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); for (var i = 1; i < parts.Length; i++) { var idx = parts [i].IndexOf ("="); if (idx > 0) { var value = parts [i].Substring (idx + 1, parts [i].Length - idx - 1); if (value [0] == '\"' && value [value.Length - 1] == '\"') { value = value.Substring (1, value.Length - 2); } attrs.Add (parts [i].Substring (0, idx), value); } } switch (parts [0]) { case "info": fontFace = attrs ["face"]; fontSize = int.Parse (attrs ["size"]); break; case "common": if (attrs.ContainsKey("lineHeight")) lineHeight = float.Parse (attrs ["lineHeight"]); if (attrs.ContainsKey("base")) lineBase = float.Parse (attrs ["base"]); if (attrs.ContainsKey("scaleW")) scaleW = float.Parse (attrs ["scaleW"]); if (attrs.ContainsKey("scaleH")) scaleH = float.Parse (attrs ["scaleH"]); if (attrs.ContainsKey("pixelScale")) pixelScale = float.Parse (attrs ["pixelScale"]); break; case "page": if (textureFile != null) throw new ContentException ("Only one page per font is supported."); textureFile = attrs ["file"]; break; case "char": chars.Add (new Char { id = int.Parse (attrs ["id"]), x = float.Parse (attrs ["x"]), y = float.Parse (attrs ["y"]), width = float.Parse (attrs ["width"]), height = float.Parse (attrs ["height"]), xoffset = float.Parse (attrs ["xoffset"]), yoffset = float.Parse (attrs ["yoffset"]), xadvance = float.Parse (attrs ["xadvance"]) }); break; case "kerning": var first = ulong.Parse (attrs ["first"]); var second = ulong.Parse (attrs ["second"]); var combined = (first << 32) | second; kernings.Add (combined, float.Parse (attrs ["amount"])); break; } attrs.Clear (); } sr.Dispose (); using (var img = Image.FromFile(textureFile)) { if (lineHeight == 0f) lineHeight = fontSize; if (lineBase == 0f) lineBase = lineHeight - 1; if (scaleW == 0f || scaleH == 0f) { scaleW = img.Width; scaleH = img.Height; } if (pixelScale == 0f) pixelScale = 1f; var ms = new MemoryStream (); using (var bw = new BinaryWriter(ms)) { bw.Write(fontFace); bw.Write(fontSize); bw.Write(pixelScale); bw.Write(lineHeight / pixelScale); bw.Write(lineBase / pixelScale); bw.Write(chars.Count); foreach (var ch in chars) { bw.Write(ch.id); bw.Write(ch.x / scaleW); bw.Write(((ch.y + ch.height) / scaleH)); bw.Write((ch.x + ch.width) / scaleW); bw.Write(ch.y / scaleH); bw.Write(ch.width / pixelScale); bw.Write(ch.height / pixelScale); bw.Write(ch.xoffset / pixelScale); bw.Write(((lineHeight - ch.height) - ch.yoffset) / pixelScale); bw.Write(ch.xadvance / pixelScale); } bw.Write(kernings.Count); foreach (var kvp in kernings) { bw.Write(kvp.Key); bw.Write(kvp.Value / pixelScale); } bw.Flush(); ms.Position = 0; using (var tw = new TarWriter(output)) { tw.Write(ms, ms.Length, "font.atlas"); using (var ts = new MemoryStream()) { using (var texture = ImageHelper.PremultiplyAlpha(img)) { texture.Save(ts, ImageFormat.Png); } ts.Position = 0; tw.Write(ts, ts.Length, "font.png"); } } } } }