Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2015
2,883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. // Act Editor Script - v1.0.5.1
  2. // By Tokeiburu
  3. GrfColor textForeground = "0xffffff";
  4. var fontType = "Minecraftia"; // Yep... I'm using a minecraft font
  5. var fontSize = 12;
  6. var mobFolder = @"data\sprite\¸ó½ºÅÍ\";
  7. var dataGrfName = @"C:\data.grf";
  8. var newGrfName = @"C:\mobsWithName.grf";
  9. var mobDbPath = @"C:\mob_db.txt";
  10. var oldEncoding = Utilities.Services.EncodingService.DisplayEncoding;
  11.  
  12. try {
  13. Utilities.Services.EncodingService.DisplayEncoding = Utilities.Services.EncodingService.ANSI;
  14. Func<GrfColor, byte[]> toBgra = color => new byte[] { color.B, color.G, color.R, color.A };
  15.  
  16. TkDictionary<string, string> clientSpriteToServerName = new TkDictionary<string, string>();
  17.  
  18. Func<GrfHolder, IEnumerable<Tuple<FileEntry, FileEntry>>> mobEnumerator =
  19. grf => (from actEntry in grf.FileTable.EntriesInDirectory(mobFolder, SearchOption.TopDirectoryOnly)
  20. where actEntry.RelativePath.IsExtension(".act")
  21. let sprEntry = grf.FileTable.TryGet(actEntry.RelativePath.ReplaceExtension(".spr"))
  22. where sprEntry != null
  23. select new Tuple<FileEntry, FileEntry>(actEntry, sprEntry)).ToList();
  24.  
  25. int ival;
  26. string[] values;
  27.  
  28. // Load the mob_db file
  29. foreach (string line in new LineTextReader(mobDbPath, Utilities.Services.EncodingService.ANSI).Lines) {
  30. values = line.Split(',');
  31. if (values.Length < 4) continue;
  32. clientSpriteToServerName[values[1].ToUpper()] = values[2];
  33. }
  34.  
  35. Func<FileEntry, FileEntry, string, Act> addNameAbove = (actEntry, sprEntry, display) => {
  36. var pixels = new byte[256 * 20 * 4];
  37. BitmapSource bitmapSource = BitmapSource.Create(256, 20, 96, 96, PixelFormats.Bgra32, null, pixels, 256 * 4);
  38. var visual = new DrawingVisual();
  39. using (DrawingContext drawingContext = visual.RenderOpen()) {
  40. drawingContext.DrawImage(bitmapSource, new Rect(0, 0, 256, 20));
  41. drawingContext.DrawText(
  42. new FormattedText(display, CultureInfo.InvariantCulture, FlowDirection.LeftToRight,
  43. new Typeface(fontType), fontSize, new SolidColorBrush(textForeground.ToColor())), new Point(2, 2));
  44. }
  45.  
  46. var image = new DrawingImage(visual.Drawing);
  47. var im = new Image { Source = image };
  48. im.Measure(new Size(image.Width, image.Height));
  49. im.Arrange(new Rect(0.0, 0.0, image.Width, image.Height));
  50. im.UpdateLayout();
  51.  
  52. var grfImage = GrfToWpfBridge.Imaging.ConvertToBitmapSource(im);
  53. var transparent = new GrfColor(0, 0, 0, 0);
  54.  
  55. for (int i = 0; i < grfImage.NumberOfPixels; i++) {
  56. Buffer.BlockCopy(toBgra(grfImage.Pixels[4 * i + 3] > 0x80 ? textForeground : transparent), 0, grfImage.Pixels, 4 * i, 4);
  57. }
  58.  
  59. grfImage.Convert(GrfImageType.Indexed8); grfImage.Trim(); grfImage.Margin(2);
  60. Buffer.BlockCopy(toBgra(GrfColor.Black), 0, grfImage.Palette, 2 * 4, 4);
  61.  
  62. for (int y = 1; y < grfImage.Height - 1; y++) {
  63. for (int x = 1; x < grfImage.Width - 1; x++) {
  64. if (grfImage.Pixels[y * grfImage.Width + x] == 1) continue;
  65.  
  66. if (grfImage.Pixels[(y - 1) * grfImage.Width + (x)] == 1 || grfImage.Pixels[(y + 1) * grfImage.Width + (x)] == 1 ||
  67. grfImage.Pixels[(y) * grfImage.Width + (x - 1)] == 1 || grfImage.Pixels[(y) * grfImage.Width + (x + 1)] == 1)
  68. grfImage.Pixels[y * grfImage.Width + x] = 2;
  69. }
  70. }
  71.  
  72. act = new Act(actEntry.GetDecompressedData(), new Spr(sprEntry.GetDecompressedData()));
  73.  
  74. // Doesn't support Bgr32 only sprites
  75. if (act.Sprite.Palette == null) return act;
  76.  
  77. var unused = act.Sprite.GetUnusedPaletteIndexes().ToList();
  78. if (unused.Count <= 1) return act;
  79. if (!act.Sprite.Palette.Contains(textForeground)) Buffer.BlockCopy(toBgra(textForeground), 0, act.Sprite.Palette.BytePalette, 4 * unused[0], 4);
  80. if (!act.Sprite.Palette.Contains(GrfColor.Black)) Buffer.BlockCopy(toBgra(GrfColor.Black), 0, act.Sprite.Palette.BytePalette, 4 * unused[1], 4);
  81.  
  82. var colors = act.Sprite.Palette.Colors.ToList();
  83. var whiteIndex = (byte)colors.IndexOf(textForeground);
  84. var blackIndex = (byte)colors.IndexOf(GrfColor.Black);
  85.  
  86. for (int i = 0; i < grfImage.NumberOfPixels; i++) {
  87. if (grfImage.Pixels[i] == 1) grfImage.Pixels[i] = whiteIndex;
  88. else if (grfImage.Pixels[i] == 2) grfImage.Pixels[i] = blackIndex;
  89. }
  90.  
  91. var index = act.Sprite.InsertAny(grfImage);
  92. var max = 0;
  93.  
  94. act.AnimationExecute(0, a => a.AllLayers(p => {
  95. if ((ival = ((int)(p.ScaleY * p.Height) / 2 - p.OffsetY)) > max)
  96. max = ival;
  97. }));
  98.  
  99. max += 15;
  100.  
  101. var layer = new Layer(index, grfImage) { OffsetY = -(max + fontSize / 2) };
  102. ival = 0;
  103.  
  104. foreach (var action in act) {
  105. // Don't show for the death animation
  106. if (ival >= 32 && ival < 40)
  107. continue;
  108.  
  109. foreach (var frame in action) {
  110. frame.Layers.Add(layer);
  111. }
  112.  
  113. ival++;
  114. }
  115.  
  116. return act;
  117. };
  118.  
  119. string displayName;
  120. int from = -1;
  121.  
  122. using (var dataGrf = new GrfHolder(dataGrfName))
  123. using (var outputGrf = new GrfHolder(newGrfName, GrfLoadOptions.New)) {
  124. var mobs = mobEnumerator(dataGrf).ToList();
  125. int count = mobs.Count();
  126.  
  127. TaskManager.DisplayTaskC("Adding text label", "Processing...", () => from / (float) count * 100f, isCancelling => {
  128. try {
  129. outputGrf.Commands.Begin();
  130.  
  131. foreach (var mob in mobs) {
  132. if (isCancelling()) return;
  133.  
  134. var actEntry = mob.Item1;
  135. var sprEntry = mob.Item2;
  136.  
  137. if ((displayName = clientSpriteToServerName[actEntry.DisplayRelativePath.ReplaceExtension("").ToUpper()]) == null) { from++; continue; }
  138.  
  139. Act mobAct = addNameAbove(actEntry, sprEntry, displayName);
  140. MemoryStream actStream = new MemoryStream();
  141. MemoryStream sprStream = new MemoryStream();
  142.  
  143. try { mobAct.SaveWithSprite(actStream, sprStream); }
  144. catch { from++; continue; }
  145.  
  146. outputGrf.Commands.AddFileAbsolute(actEntry.RelativePath, actStream);
  147. outputGrf.Commands.AddFileAbsolute(sprEntry.RelativePath, sprStream);
  148.  
  149. if (from % 100 == 0) {
  150. outputGrf.Commands.End();
  151. outputGrf.Save();
  152. outputGrf.Reload();
  153. outputGrf.Commands.Begin();
  154. }
  155.  
  156. from++;
  157. }
  158. }
  159. finally {
  160. outputGrf.Commands.End();
  161. outputGrf.Save();
  162. from = count;
  163. }
  164. });
  165. }
  166. }
  167. catch (Exception err) {
  168. ErrorHandler.HandleException(err);
  169. }
  170. finally {
  171. Utilities.Services.EncodingService.DisplayEncoding = oldEncoding;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement