コード例 #1
0
        public virtual FileMap EncodeTEX0Texture(Bitmap src, int mipLevels)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;

            PixelFormat fmt = src.IsIndexed() ? src.PixelFormat : PixelFormat.Format32bppArgb;

            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels) + 0x40);

            try
            {
                //Build TEX header
                TEX0v1 *header = (TEX0v1 *)fileView.Address;
                *       header = new TEX0v1(w, h, RawFormat, mipLevels);

                int sStep = bw * Image.GetPixelFormatSize(fmt) / 8;
                int dStep = bw * bh * BitsPerPixel / 8;

                using (DIB dib = DIB.FromBitmap(src, bw, bh, fmt))
                    for (int i = 1; i <= mipLevels; i++)
                    {
                        EncodeLevel(header->PixelData, dib, src, dStep, sStep, i);
                    }

                return(fileView);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }
コード例 #2
0
        //Parser commands must initialize the node before returning.
        public static ResourceNode FromFile(ResourceNode parent, string path)
        {
            ResourceNode node = null;
            FileMap      map  = FileMap.FromFile(path, FileMapProtect.Read);

            try
            {
                if (Path.GetExtension(path).ToUpper().ToString() == ".MRG")
                {
                    node = new MRGNode();
                    node.Initialize(parent, map);
                }
                else if (Path.GetExtension(path).ToUpper().ToString() == ".REL")
                {
                    node = new RELNode();
                    node.Initialize(parent, map);
                }
                else if (Path.GetExtension(path).ToUpper().ToString() == ".DOL")
                {
                    node = new DOLNode();
                    node.Initialize(parent, map);
                }
                else
                {
                    node = FromSource(parent, new DataSource(map));
                }
            }
            finally { if (node == null)
                      {
                          map.Dispose();
                      }
            }
            return(node);
        }
コード例 #3
0
ファイル: PCMStream.cs プロジェクト: 0000duck/brawltools
 public void Dispose()
 {
     if (_sourceMap != null)
     {
         _sourceMap.Dispose();
         _sourceMap = null;
     }
     GC.SuppressFinalize(this);
 }
コード例 #4
0
ファイル: DataSource.cs プロジェクト: volt72/Sm4sh-Tools
 public void Close()
 {
     if (Map != null)
     {
         Map.Dispose(); Map = null;
     }
     Address = null;
     Length  = 0;
 }
コード例 #5
0
ファイル: ResourceNode.cs プロジェクト: PeterHatch/brawltools
 public void Close()
 {
     if (Map != null)
     {
         Map.Dispose(); Map = null;
     }
     Address     = null;
     Length      = 0;
     Compression = CompressionType.None;
 }
コード例 #6
0
        //Parser commands must initialize the node before returning.
        public static ResourceNode FromFile(ResourceNode parent, string path)
        {
            ResourceNode node = null;
            FileMap      map  = FileMap.FromFile(path, FileMapProtect.Read);

            try { node = FromSource(parent, new DataSource(map)); }
            finally { if (node == null)
                      {
                          map.Dispose();
                      }
            }
            return(node);
        }
コード例 #7
0
        public virtual FileMap EncodeTPLTexture(Bitmap src, int mipLevels)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;

            PixelFormat fmt = src.IsIndexed() ? src.PixelFormat : PixelFormat.Format32bppArgb;

            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels) + TPLTextureHeader.Size);

            try
            {
                //Build TPL header
                TPLTextureHeader *tex = (TPLTextureHeader *)fileView.Address;
                tex->_wrapS      = 0;
                tex->_wrapT      = 0;
                tex->_minFilter  = 1;
                tex->_magFilter  = 1;
                tex->_minLOD     = 0;
                tex->_maxLOD     = (byte)(mipLevels - 1);
                tex->PixelFormat = RawFormat;
                tex->_width      = (ushort)w;
                tex->_height     = (ushort)h;
                tex->_data       = TPLTextureHeader.Size;

                int     sStep    = bw * Image.GetPixelFormatSize(fmt) / 8;
                int     dStep    = bw * bh * BitsPerPixel / 8;
                VoidPtr baseAddr = fileView.Address;

                using (DIB dib = DIB.FromBitmap(src, bw, bh, fmt))
                {
                    for (int i = 1; i <= mipLevels; i++)
                    {
                        EncodeLevel(baseAddr + tex->_data, dib, src, dStep, sStep, i);
                    }
                }

                return(fileView);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }
コード例 #8
0
        public static FileMap EncodePalette(ColorPalette pal, WiiPaletteFormat format)
        {
            FileMap fileView = FileMap.FromTempFile((pal.Entries.Length * 2));

            try
            {
                EncodePalette(fileView.Address, pal, format);
                return(fileView);
            }
            catch (Exception x)
            {
                fileView.Dispose();
                throw;
                //MessageBox.Show(x.ToString());
                //fileView.Dispose();
                //return null;
            }
        }
コード例 #9
0
        public static ResourceNode FromFile(ResourceNode parent, string path, FileOptions options)
        {
            ResourceNode node = null;
            FileMap      map  = FileMap.FromFile(path, FileMapProtect.Read, 0, 0, options);

            try
            {
                DataSource source = new DataSource(map);
                if ((node = FromSource(parent, source)) == null)
                {
                    string ext = path.Substring(path.LastIndexOf('.') + 1).ToUpper(CultureInfo.InvariantCulture);

                    if (Forced.ContainsKey(ext) &&
                        (node = Activator.CreateInstance(Forced[ext]) as ResourceNode) != null)
                    {
                        FileMap uncompressedMap = Compressor.TryExpand(ref source, false);
                        if (uncompressedMap != null)
                        {
                            node.Initialize(parent, source, new DataSource(uncompressedMap));
                        }
                        else
                        {
                            node.Initialize(parent, source);
                        }
                    }
#if DEBUG
                    else
                    {
                        node = new RawDataNode(Path.GetFileNameWithoutExtension(path));
                        node.Initialize(parent, source);
                    }
#endif
                }
            }
            finally
            {
                if (node == null)
                {
                    map.Dispose();
                }
            }

            return(node);
        }
コード例 #10
0
        public static FileMap EncodePLT0Palette(ColorPalette pal, WiiPaletteFormat format)
        {
            FileMap fileView = FileMap.FromTempFile((pal.Entries.Length * 2) + 0x40);

            try
            {
                PLT0v1 *header = (PLT0v1 *)fileView.Address;
                *       header = new PLT0v1(pal.Entries.Length, format);

                EncodePalette(fileView.Address + 0x40, pal, format);
                return(fileView);
            }
            catch (Exception x)
            {
                fileView.Dispose();
                throw;
                //MessageBox.Show(x.ToString());
                //fileView.Dispose();
                //return null;
            }
        }
コード例 #11
0
        //Parser commands must initialize the node before returning.
        public unsafe static ResourceNode FromFile(ResourceNode parent, string path, FileOptions options = FileOptions.RandomAccess)
        {
            ResourceNode node = null;
            FileMap      map  = FileMap.FromFile(path, FileMapProtect.Read, 0, 0, options);

            try
            {
                DataSource source = new DataSource(map);
                if ((node = FromSource(parent, source)) == null)
                {
                    string ext = path.Substring(path.LastIndexOf('.') + 1).ToUpper();
                    if (Forced.ContainsKey(ext))
                    {
                        node = Activator.CreateInstance(Forced[ext]) as ResourceNode;
                        FileMap uncomp = Compressor.TryExpand(ref source, false);
                        if (uncomp != null)
                        {
                            node.Initialize(parent, source, new DataSource(uncomp));
                        }
                        else
                        {
                            node.Initialize(parent, source);
                        }
                    }
                    else if (UseRawDataNode)
                    {
                        (node = new RawDataNode(Path.GetFileNameWithoutExtension(path))).Initialize(parent, source);
                    }
                }
            }
            finally
            {
                if (node == null)
                {
                    map.Dispose();
                }
            }
            return(node);
        }
コード例 #12
0
        public static FileMap EncodeTPLPalette(ColorPalette pal, WiiPaletteFormat format)
        {
            FileMap fileView = FileMap.FromTempFile((pal.Entries.Length * 2) + 0xC);

            try
            {
                TPLPaletteHeader *header = (TPLPaletteHeader *)fileView.Address;
                header->_format     = (uint)format;
                header->_numEntries = (ushort)pal.Entries.Length;
                header->_data       = 0xC;

                EncodePalette(fileView.Address + 0xC, pal, format);
                return(fileView);
            }
            catch (Exception x)
            {
                fileView.Dispose();
                throw;
                //MessageBox.Show(x.ToString());
                //fileView.Dispose();
                //return null;
            }
        }
コード例 #13
0
        public virtual FileMap EncodeTexture(Bitmap src, int mipLevels)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;
            //int aw = w.Align(bw), ah = h.Align(bh);

            PixelFormat fmt = src.IsIndexed() ? src.PixelFormat : PixelFormat.Format32bppArgb;

            //int fileSize = GetMipOffset(w, h, mipLevels + 1) + 0x40;
            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels, false));

            //FileMap fileView = FileMap.FromTempFile(fileSize);
            try
            {
                //Build TEX header
                TEX0 *header = (TEX0 *)fileView.Address;
                *     header = new TEX0(w, h, RawFormat, mipLevels);

                int     sStep    = bw * Image.GetPixelFormatSize(fmt) / 8;
                int     dStep    = bw * bh * BitsPerPixel / 8;
                VoidPtr baseAddr = header->PixelData;

                using (DIB dib = DIB.FromBitmap(src, bw, bh, fmt))
                    for (int i = 1; i <= mipLevels; i++)
                    {
                        EncodeLevel(header, dib, src, dStep, sStep, i);
                    }

                return(fileView);
            }
            catch (Exception x)
            {
                //MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }
コード例 #14
0
ファイル: GCTNode.cs プロジェクト: 0000duck/brawltools
        internal static GCTNode IsParsable(string path)
        {
            FileMap map = FileMap.FromFile(path, FileMapProtect.ReadWrite);

            GCTCodeLine *data = (GCTCodeLine *)map.Address;

            if (GCTCodeLine.Tag._1 != data->_1 || GCTCodeLine.Tag._2 != data->_2)
            {
                map.Dispose();
                return(null);
            }

            data = (GCTCodeLine *)(map.Address + (uint)Helpers.RoundDown((uint)map.Length, 8) - GCTCodeLine.Size);
            bool endFound = false;
            int  i        = 0;

            while (!endFound)
            {
                GCTCodeLine line = *data--;
                if (line._1 == GCTCodeLine.End._1 && line._2 == GCTCodeLine.End._2)
                {
                    endFound = true;
                    break;
                }

                i++;
            }

            if (endFound && i <= 0)
            {
                data = (GCTCodeLine *)map.Address + 1;

                string s = "";
                while (true)
                {
                    GCTCodeLine line = *data++;
                    if (line._1 == GCTCodeLine.End._1 && line._2 == GCTCodeLine.End._2)
                    {
                        break;
                    }

                    s += line.ToStringNoSpace();
                }

                GCTNode g = new GCTNode();

                List <string> _unrecognized = new List <string>();

                foreach (CodeStorage c in Properties.Settings.Default.Codes)
                {
                    int index = -1;
                    if ((index = s.IndexOf(c._code)) >= 0)
                    {
                        g.AddChild(new GCTCodeEntryNode()
                        {
                            _name = c._name, _description = c._description, LinesNoSpaces = s.Substring(index, c._code.Length)
                        });
                        s = s.Remove(index, c._code.Length);
                    }
                }

                if (g.Children.Count > 0)
                {
                    if (s.Length > 0)
                    {
                        MessageBox.Show(String.Format("{0} code{1} w{2} recognized.", g.Children.Count.ToString(), g.Children.Count > 1 ? "s" : "", g.Children.Count > 1 ? "ere" : "as"));
                    }
                }
                else
                {
                    MessageBox.Show("This GCT does not contain any recognizable codes.");
                }

                if (s.Length > 0)
                {
                    g.AddChild(new GCTCodeEntryNode()
                    {
                        _name = "Unrecognized Code(s)", LinesNoSpaces = s
                    });
                }

                return(g);
            }
            else if (endFound && i > 0)
            {
                GCTNode g = new GCTNode();
                g.Initialize(null, new DataSource(map));
                return(g);
            }

            map.Dispose();
            return(null);
        }
コード例 #15
0
        public virtual FileMap EncodeREFTTexture(Bitmap src, int mipLevels, WiiPaletteFormat format)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;

            ColorPalette pal = src.Palette;

            PixelFormat fmt = src.IsIndexed() ? src.PixelFormat : PixelFormat.Format32bppArgb;

            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels) + 0x20 + (pal != null ? (pal.Entries.Length * 2) : 0));

            try
            {
                //Build REFT image header
                REFTImageHeader *header = (REFTImageHeader *)fileView.Address;
                *header = new REFTImageHeader((ushort)w, (ushort)h, (byte)RawFormat, (byte)format, (ushort)(pal != null ? pal.Entries.Length : 0), (uint)fileView.Length - 0x20 - (uint)(pal != null ? (pal.Entries.Length * 2) : 0), (byte)(mipLevels - 1));

                int sStep = bw * Image.GetPixelFormatSize(fmt) / 8;
                int dStep = bw * bh * BitsPerPixel / 8;

                using (DIB dib = DIB.FromBitmap(src, bw, bh, fmt))
                    for (int i = 1; i <= mipLevels; i++)
                    {
                        EncodeLevel((VoidPtr)header + 0x20, dib, src, dStep, sStep, i);
                    }

                if (pal != null)
                {
                    int count = pal.Entries.Length;

                    switch (format)
                    {
                    case WiiPaletteFormat.IA8:
                    {
                        IA8Pixel *dPtr = (IA8Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (IA8Pixel)pal.Entries[i];
                        }
                        break;
                    }

                    case WiiPaletteFormat.RGB565:
                    {
                        wRGB565Pixel *dPtr = (wRGB565Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (wRGB565Pixel)pal.Entries[i];
                        }
                        break;
                    }

                    case WiiPaletteFormat.RGB5A3:
                    {
                        wRGB5A3Pixel *dPtr = (wRGB5A3Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (wRGB5A3Pixel)pal.Entries[i];
                        }
                        break;
                    }
                    }
                }

                return(fileView);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }
コード例 #16
0
ファイル: NodeFactory.cs プロジェクト: 0000duck/brawltools
        //Parser commands must initialize the node before returning.
        public unsafe static ResourceNode FromFile(ResourceNode parent, string path)
        {
            ResourceNode node = null;
            FileMap      map  = FileMap.FromFile(path, FileMapProtect.Read);

            try
            {
                DataSource source = new DataSource(map);
                if (String.Equals(Path.GetExtension(path), ".mrg", StringComparison.OrdinalIgnoreCase) || String.Equals(Path.GetExtension(path), ".mrgc", StringComparison.OrdinalIgnoreCase))
                {
                    node = new MRGNode();
                    if (Compressor.IsDataCompressed(source.Address, source.Length))
                    {
                        CompressionHeader *cmpr = (CompressionHeader *)source.Address;
                        source.Compression = cmpr->Algorithm;
                        if (Compressor.Supports(cmpr->Algorithm))
                        {
                            try
                            {
                                //Expand the whole resource and initialize
                                FileMap uncompMap = FileMap.FromTempFile(cmpr->ExpandedSize);
                                Compressor.Expand(cmpr, uncompMap.Address, uncompMap.Length);
                                node.Initialize(parent, source, new DataSource(uncompMap));
                            }
                            catch (InvalidCompressionException e) { MessageBox.Show(e.ToString()); }
                        }
                        else
                        {
                            node.Initialize(parent, source);
                        }
                    }
                    else
                    {
                        node.Initialize(parent, source);
                    }
                }
                else if (String.Equals(Path.GetExtension(path), ".rel", StringComparison.OrdinalIgnoreCase))
                {
                    node = new RELNode();
                    node.Initialize(parent, map);
                }
                else if (String.Equals(Path.GetExtension(path), ".dol", StringComparison.OrdinalIgnoreCase))
                {
                    node = new DOLNode();
                    node.Initialize(parent, map);
                }
                else if ((node = FromSource(parent, source)) == null)
                {
                    //if (Compressor.IsDataCompressed(source.Address, source.Length))
                    //{
                    //    CompressionHeader* cmpr = (CompressionHeader*)source.Address;
                    //    if (!Compressor.Supports(cmpr->Algorithm))
                    //        MessageBox.Show("File uses unsupported " + cmpr->Algorithm.ToString() + " compression.");
                    //}
                }
            }
            finally { if (node == null)
                      {
                          map.Dispose();
                      }
            }
            return(node);
        }
コード例 #17
0
        private void btnOkay_Click(object sender, EventArgs e)
        {
            TextureConverter format = TextureConverter.Get((WiiPixelFormat)cboFormat.SelectedItem);

            if (format.IsIndexed)
            {
                _textureData = format.EncodeTextureIndexed(_indexed, (int)numLOD.Value, (WiiPaletteFormat)cboPaletteFormat.SelectedItem, out _paletteData);
            }
            else
            {
                if ((format.RawFormat == WiiPixelFormat.CMPR) && (_cmprBuffer != null))
                {
                    _textureData = ((CMPR)format).EncodeTextureCached(_source, (int)numLOD.Value, _cmprBuffer);
                }
                else
                {
                    _textureData = format.EncodeTexture(_source, (int)numLOD.Value);
                }
            }

            if (_parent != null)
            {
                _original = _parent.CreateResource <TEX0Node>(Path.GetFileNameWithoutExtension(_imageSource));
                if (_paletteData != null)
                {
                    _originalPalette      = _parent.CreateResource <PLT0Node>(_original.Name);
                    _originalPalette.Name = _original.Name;
                    _originalPalette.ReplaceRaw(_paletteData);
                }
                _original.ReplaceRaw(_textureData);
            }
            else if (_original != null)
            {
                if (_originalPalette != null)
                {
                    if (_paletteData != null)
                    {
                        _originalPalette.ReplaceRaw(_paletteData);
                    }
                    else
                    {
                        _originalPalette.Remove();
                        _originalPalette.Dispose();
                    }
                }
                else if (_paletteData != null)
                {
                    if ((_original.Parent == null) || (_original.Parent.Parent == null))
                    {
                        _paletteData.Dispose();
                        _paletteData = null;
                    }
                    else
                    {
                        _parent               = _original.Parent.Parent as BRESNode;
                        _originalPalette      = _parent.CreateResource <PLT0Node>(_original.Name);
                        _originalPalette.Name = _original.Name;
                        _originalPalette.ReplaceRaw(_paletteData);
                    }
                }
                _original.ReplaceRaw(_textureData);
            }

            DialogResult = DialogResult.OK;
            Close();
        }
コード例 #18
0
        public virtual FileMap EncodeREFTTexture(Bitmap src, int mipLevels, WiiPaletteFormat format, bool usePalette)
        {
            int w = src.Width, h = src.Height;
            int bw = BlockWidth, bh = BlockHeight;
            //int aw = w.Align(bw), ah = h.Align(bh);
            ColorPalette pal = src.Palette;

            PixelFormat fmt = src.IsIndexed() ? src.PixelFormat : PixelFormat.Format32bppArgb;

            //int fileSize = GetMipOffset(w, h, mipLevels + 1) + 0x20;
            FileMap fileView = FileMap.FromTempFile(GetFileSize(w, h, mipLevels, true) + (usePalette ? (pal.Entries.Length * 2) : 0));

            //FileMap fileView = FileMap.FromTempFile(fileSize);
            try
            {
                //Build REFT image header
                REFTData *header = (REFTData *)fileView.Address;
                *         header = new REFTData((ushort)w, (ushort)h, (byte)RawFormat);
                header->_imagelen = (uint)fileView.Length - 0x20;

                int     sStep    = bw * Image.GetPixelFormatSize(fmt) / 8;
                int     dStep    = bw * bh * BitsPerPixel / 8;
                VoidPtr baseAddr = (byte *)header + 0x20;

                using (DIB dib = DIB.FromBitmap(src, bw, bh, fmt))
                    for (int i = 1; i <= mipLevels; i++)
                    {
                        EncodeLevel(baseAddr, dib, src, dStep, sStep, i);
                    }

                if (usePalette)
                {
                    int count = pal.Entries.Length;

                    header->_colorCount = (ushort)count;
                    header->_pltFormat  = (byte)format;

                    switch (format)
                    {
                    case WiiPaletteFormat.IA8:
                    {
                        IA8Pixel *dPtr = (IA8Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (IA8Pixel)pal.Entries[i];
                        }
                        break;
                    }

                    case WiiPaletteFormat.RGB565:
                    {
                        wRGB565Pixel *dPtr = (wRGB565Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (wRGB565Pixel)pal.Entries[i];
                        }
                        break;
                    }

                    case WiiPaletteFormat.RGB5A3:
                    {
                        wRGB5A3Pixel *dPtr = (wRGB5A3Pixel *)header->PaletteData;
                        for (int i = 0; i < count; i++)
                        {
                            dPtr[i] = (wRGB5A3Pixel)pal.Entries[i];
                        }
                        break;
                    }
                    }
                }

                return(fileView);
            }
            catch (Exception x)
            {
                //MessageBox.Show(x.ToString());
                fileView.Dispose();
                return(null);
            }
        }