Skip to content

Instantly share code, notes, and snippets.

@scottstamp
Created April 28, 2020 23:55
Show Gist options
  • Save scottstamp/363de8aa3bc8581bac78328a493f8cc8 to your computer and use it in GitHub Desktop.
Save scottstamp/363de8aa3bc8581bac78328a493f8cc8 to your computer and use it in GitHub Desktop.
FlassetReplacer (SWF asset replacer using Flazzy)
using Flazzy;
using Flazzy.Tags;
using System.Drawing;
using System.IO;
using System.Linq;
namespace FlassetReplacer
{
class Program
{
private static bool Exporting = false;
private static bool Replacing = false;
static void Main(string[] args)
{
var flash = new ShockwaveFlash("Habbo.swf");
flash.Disassemble();
// Export if "out" folder doesn't exist
Exporting = !Directory.Exists("out");
// Replace if "in" folder does exist
Replacing = Directory.Exists("in");
var images = flash.Tags.Where(t => t.Kind == TagKind.DefineBitsLossless2).Select(t => t as DefineBitsLossless2Tag).ToList();
if (Exporting)
{
Directory.CreateDirectory("out");
foreach (var imageTag in images)
{
SaveARGBMap(imageTag.Id, imageTag.GetARGBMap());
}
}
if (Replacing)
{
var inFiles = Directory.EnumerateFiles("in");
foreach (var file in inFiles)
{
var id = ushort.Parse(file.Split('\\')[1].Split('.')[0]);
images.First(t => t.Id == id).SetARGBMap(ReadARGBMap(file));
}
using (var f = new StreamWriter("new.swf"))
{
flash.CopyTo(f.BaseStream, CompressionKind.LZMA);
}
}
}
private static void SaveARGBMap(ushort id, Color[,] map) => SaveARGBMap(id.ToString(), map);
private static void SaveARGBMap(string filename, Color[,] map)
{
var width = map.GetLength(0);
var height = map.GetLength(1);
using (Bitmap pic = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
for (var x = 0; x < width; x++)
{
for (var y = 0; y < height; y++)
{
Color c = map[x, y];
pic.SetPixel(x, y, c);
}
}
pic.Save("out\\" + filename + ".png");
}
}
private static Color[,] ReadARGBMap(string filename)
{
Bitmap img = new Bitmap(filename);
Color[,] map = new Color[img.Width, img.Height];
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
map[i,j] = img.GetPixel(i, j);
}
}
return map;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment