Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Act Editor Script - v1.0.5.1
- // By Tokeiburu
- GrfColor textForeground = "0xffffff";
- var fontType = "Minecraftia"; // Yep... I'm using a minecraft font
- var fontSize = 12;
- var mobFolder = @"data\sprite\¸ó½ºÅÍ\";
- var dataGrfName = @"C:\data.grf";
- var newGrfName = @"C:\mobsWithName.grf";
- var mobDbPath = @"C:\mob_db.txt";
- var oldEncoding = Utilities.Services.EncodingService.DisplayEncoding;
- try {
- Utilities.Services.EncodingService.DisplayEncoding = Utilities.Services.EncodingService.ANSI;
- Func<GrfColor, byte[]> toBgra = color => new byte[] { color.B, color.G, color.R, color.A };
- TkDictionary<string, string> clientSpriteToServerName = new TkDictionary<string, string>();
- Func<GrfHolder, IEnumerable<Tuple<FileEntry, FileEntry>>> mobEnumerator =
- grf => (from actEntry in grf.FileTable.EntriesInDirectory(mobFolder, SearchOption.TopDirectoryOnly)
- where actEntry.RelativePath.IsExtension(".act")
- let sprEntry = grf.FileTable.TryGet(actEntry.RelativePath.ReplaceExtension(".spr"))
- where sprEntry != null
- select new Tuple<FileEntry, FileEntry>(actEntry, sprEntry)).ToList();
- int ival;
- string[] values;
- // Load the mob_db file
- foreach (string line in new LineTextReader(mobDbPath, Utilities.Services.EncodingService.ANSI).Lines) {
- values = line.Split(',');
- if (values.Length < 4) continue;
- clientSpriteToServerName[values[1].ToUpper()] = values[2];
- }
- Func<FileEntry, FileEntry, string, Act> addNameAbove = (actEntry, sprEntry, display) => {
- var pixels = new byte[256 * 20 * 4];
- BitmapSource bitmapSource = BitmapSource.Create(256, 20, 96, 96, PixelFormats.Bgra32, null, pixels, 256 * 4);
- var visual = new DrawingVisual();
- using (DrawingContext drawingContext = visual.RenderOpen()) {
- drawingContext.DrawImage(bitmapSource, new Rect(0, 0, 256, 20));
- drawingContext.DrawText(
- new FormattedText(display, CultureInfo.InvariantCulture, FlowDirection.LeftToRight,
- new Typeface(fontType), fontSize, new SolidColorBrush(textForeground.ToColor())), new Point(2, 2));
- }
- var image = new DrawingImage(visual.Drawing);
- var im = new Image { Source = image };
- im.Measure(new Size(image.Width, image.Height));
- im.Arrange(new Rect(0.0, 0.0, image.Width, image.Height));
- im.UpdateLayout();
- var grfImage = GrfToWpfBridge.Imaging.ConvertToBitmapSource(im);
- var transparent = new GrfColor(0, 0, 0, 0);
- for (int i = 0; i < grfImage.NumberOfPixels; i++) {
- Buffer.BlockCopy(toBgra(grfImage.Pixels[4 * i + 3] > 0x80 ? textForeground : transparent), 0, grfImage.Pixels, 4 * i, 4);
- }
- grfImage.Convert(GrfImageType.Indexed8); grfImage.Trim(); grfImage.Margin(2);
- Buffer.BlockCopy(toBgra(GrfColor.Black), 0, grfImage.Palette, 2 * 4, 4);
- for (int y = 1; y < grfImage.Height - 1; y++) {
- for (int x = 1; x < grfImage.Width - 1; x++) {
- if (grfImage.Pixels[y * grfImage.Width + x] == 1) continue;
- if (grfImage.Pixels[(y - 1) * grfImage.Width + (x)] == 1 || grfImage.Pixels[(y + 1) * grfImage.Width + (x)] == 1 ||
- grfImage.Pixels[(y) * grfImage.Width + (x - 1)] == 1 || grfImage.Pixels[(y) * grfImage.Width + (x + 1)] == 1)
- grfImage.Pixels[y * grfImage.Width + x] = 2;
- }
- }
- act = new Act(actEntry.GetDecompressedData(), new Spr(sprEntry.GetDecompressedData()));
- // Doesn't support Bgr32 only sprites
- if (act.Sprite.Palette == null) return act;
- var unused = act.Sprite.GetUnusedPaletteIndexes().ToList();
- if (unused.Count <= 1) return act;
- if (!act.Sprite.Palette.Contains(textForeground)) Buffer.BlockCopy(toBgra(textForeground), 0, act.Sprite.Palette.BytePalette, 4 * unused[0], 4);
- if (!act.Sprite.Palette.Contains(GrfColor.Black)) Buffer.BlockCopy(toBgra(GrfColor.Black), 0, act.Sprite.Palette.BytePalette, 4 * unused[1], 4);
- var colors = act.Sprite.Palette.Colors.ToList();
- var whiteIndex = (byte)colors.IndexOf(textForeground);
- var blackIndex = (byte)colors.IndexOf(GrfColor.Black);
- for (int i = 0; i < grfImage.NumberOfPixels; i++) {
- if (grfImage.Pixels[i] == 1) grfImage.Pixels[i] = whiteIndex;
- else if (grfImage.Pixels[i] == 2) grfImage.Pixels[i] = blackIndex;
- }
- var index = act.Sprite.InsertAny(grfImage);
- var max = 0;
- act.AnimationExecute(0, a => a.AllLayers(p => {
- if ((ival = ((int)(p.ScaleY * p.Height) / 2 - p.OffsetY)) > max)
- max = ival;
- }));
- max += 15;
- var layer = new Layer(index, grfImage) { OffsetY = -(max + fontSize / 2) };
- ival = 0;
- foreach (var action in act) {
- // Don't show for the death animation
- if (ival >= 32 && ival < 40)
- continue;
- foreach (var frame in action) {
- frame.Layers.Add(layer);
- }
- ival++;
- }
- return act;
- };
- string displayName;
- int from = -1;
- using (var dataGrf = new GrfHolder(dataGrfName))
- using (var outputGrf = new GrfHolder(newGrfName, GrfLoadOptions.New)) {
- var mobs = mobEnumerator(dataGrf).ToList();
- int count = mobs.Count();
- TaskManager.DisplayTaskC("Adding text label", "Processing...", () => from / (float) count * 100f, isCancelling => {
- try {
- outputGrf.Commands.Begin();
- foreach (var mob in mobs) {
- if (isCancelling()) return;
- var actEntry = mob.Item1;
- var sprEntry = mob.Item2;
- if ((displayName = clientSpriteToServerName[actEntry.DisplayRelativePath.ReplaceExtension("").ToUpper()]) == null) { from++; continue; }
- Act mobAct = addNameAbove(actEntry, sprEntry, displayName);
- MemoryStream actStream = new MemoryStream();
- MemoryStream sprStream = new MemoryStream();
- try { mobAct.SaveWithSprite(actStream, sprStream); }
- catch { from++; continue; }
- outputGrf.Commands.AddFileAbsolute(actEntry.RelativePath, actStream);
- outputGrf.Commands.AddFileAbsolute(sprEntry.RelativePath, sprStream);
- if (from % 100 == 0) {
- outputGrf.Commands.End();
- outputGrf.Save();
- outputGrf.Reload();
- outputGrf.Commands.Begin();
- }
- from++;
- }
- }
- finally {
- outputGrf.Commands.End();
- outputGrf.Save();
- from = count;
- }
- });
- }
- }
- catch (Exception err) {
- ErrorHandler.HandleException(err);
- }
- finally {
- Utilities.Services.EncodingService.DisplayEncoding = oldEncoding;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement