Skip to content

Instantly share code, notes, and snippets.

@xupefei
Created April 29, 2013 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xupefei/5481023 to your computer and use it in GitHub Desktop.
Save xupefei/5481023 to your computer and use it in GitHub Desktop.
CatSystem2 HG3 Offset Generator
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using DataHelper;
namespace HG3_Offset_Generator
{
internal class Program
{
internal struct SegmentInfo
{
internal string Index;
internal int X;
internal int Y;
internal int FullWidth;
internal int FullHeight;
internal SegmentInfo(string index, int x, int y, int fwidth, int fheight)
{
Index = index;
X = x;
Y = y;
FullWidth = fwidth;
FullHeight = fheight;
}
}
private static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("usage: exe <folder>");
Console.ReadKey();
return;
}
string strFolder = args[0];
foreach (string strFile in Directory.GetFiles(strFolder, "*.hg3", SearchOption.AllDirectories))
{
Console.WriteLine(strFile);
List<SegmentInfo> offsetTable = GetOffsetTable(strFile);
if (offsetTable.Count > 0)
{
MakeHG3Info(strFile);
EnlargeSegments(strFile, offsetTable);
}
}
}
internal static void MakeHG3Info(string strFile)
{
string workingDir = strFile.Substring(0, strFile.Length - 4) + "\\";
string newName = string.Format("{0}[{1}].png", workingDir, Path.GetFileNameWithoutExtension(strFile));
//save transparent img named "[XXXX]" as HG3 info
Bitmap image = new Bitmap(10, 10);
image.Save(newName, ImageFormat.Png);
}
internal static void EnlargeSegments(string strFile, List<SegmentInfo> offsetTable)
{
string workingDir = strFile.Substring(0, strFile.Length - 4) + "\\";
foreach (SegmentInfo info in offsetTable)
{
string name_bmp = string.Format("{0}_{1}.bmp", workingDir + Path.GetFileNameWithoutExtension(strFile),
info.Index.PadLeft(4, '0'));
string name_png = string.Format("{0}_{1}.bmp", workingDir + Path.GetFileNameWithoutExtension(strFile),
info.Index.PadLeft(4, '0'));
string newName = string.Format("{0}{1}.png", workingDir, info.Index);
if (!File.Exists(name_bmp))
continue;
BitmapFile32 file = new BitmapFile32(name_bmp);
Bitmap image = new Bitmap(info.FullWidth, info.FullHeight);
Graphics.FromImage(image).DrawImage(file.GetBitmap(), new Point(info.X, info.Y));
image.Save(newName, ImageFormat.Png);
File.Delete(name_bmp);
}
}
internal static List<SegmentInfo> GetOffsetTable(string strFileName)
{
BinaryReader br = new BinaryReader(new FileStream(strFileName, FileMode.Open));
byte[] byteBuffer = br.ReadBytes((int)br.BaseStream.Length);
br.Close();
//Find "stdinfo\0"
int intPos = BytesHelper.FindBytes(byteBuffer, new byte[] { 0x73, 0x74, 0x64, 0x69, 0x6E, 0x66, 0x6F, 0x00 },
0);
int intCrtPos = intPos;
List<SegmentInfo> listSegments = new List<SegmentInfo>();
while (intPos != -1)
{
int index = BitConverter.ToInt32(byteBuffer, intPos - 4);
int intX = BitConverter.ToInt32(byteBuffer, intPos + 28);
int intY = BitConverter.ToInt32(byteBuffer, intPos + 32);
int intW = BitConverter.ToInt32(byteBuffer, intPos + 36);
int intH = BitConverter.ToInt32(byteBuffer, intPos + 40);
listSegments.Add(new SegmentInfo(index.ToString(), intX, intY, intW, intH));
//Find "stdinfo\0"
intPos = BytesHelper.FindBytes(byteBuffer, new byte[] { 0x73, 0x74, 0x64, 0x69, 0x6E, 0x66, 0x6F, 0x00 },
intPos + 8);
}
return listSegments;
}
}
}
@xd2333
Copy link

xd2333 commented Apr 2, 2023

你好,这段程序的DataHelper是引用的什么呢,可以分享一下吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment