-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataFormat.cs
288 lines (273 loc) · 10.6 KB
/
DataFormat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using Li.Drawing;
using Li.Text;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using System.Windows.Media.Imaging;
namespace Li.krkrfgformat
{
public class DataFormat
{
private class Infos
{
public PictureInfo PicInfo
{
get;
set;
}
public Bitmap Pic
{
get;
set;
}
public Infos(string filename)
{
if (Path.GetExtension(filename).ToLower() == ".png")
{
Pic = new Bitmap(filename);
}
else
{
try
{
Pic = DataFormat.TlgFormat(filename);
}
catch(Exception ex)
{
MessageBox.Show("读取tlg文件失败,缺少必要的组件(dll)或者文件格式错误。", "警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
Pic = new Bitmap(1, 1);
}
}
if (usingSizeOnly)
{
PicInfo = GetPicInfoFromSize(Pic.Size);
}
else
{
PicInfo = (GetPicInfoFromText(GetIDFromFilePatch(filename)) ?? GetPicInfoFromSize(Pic.Size));
}
}
}
/// <summary>
/// 是否只用图片大小寻找规则
/// </summary>
public static bool usingSizeOnly;
/// <summary>
/// 文件路径
/// </summary>
public static string FilePatch
{
get;
set;
}
/// <summary>
/// 生成的新图片
/// </summary>
public static Bitmap NewPicture
{
get;
set;
}
/// <summary>
/// 文本坐标信息
/// </summary>
public static TextFile TextDatas
{
get;
set;
}
/// <summary>
/// listbox顺序存入的图片以及代号
/// </summary>
public static Dictionary<int, string> Pictures
{
get;
set;
}
/// <summary>
/// 存储的文件名字
/// </summary>
public static string SaveFileName
{
get;
set;
}
public DataFormat()
{
SaveFileName = string.Empty;
FilePatch = string.Empty;
NewPicture = null;
TextDatas = new TextFile();
Pictures = new Dictionary<int, string>();
}
/// <summary>
/// 用ID从数据里面寻找坐标数据
/// </summary>
/// <param name="id">图片ID</param>
/// <returns></returns>
private static PictureInfo GetPicInfoFromText(int id)
{
return TextDatas.TextData.Find((PictureInfo tmp) => tmp.LayerId == id);
}
/// <summary>
/// 用图片大小寻找坐标数据
/// </summary>
/// <param name="size">需要寻找的图片大小</param>
/// <returns></returns>
private static PictureInfo GetPicInfoFromSize(Size size)
{
return TextDatas.TextData.Find((PictureInfo tmp) => tmp.Width == size.Width && tmp.Height == size.Height);
}
/// <summary>
/// 获取给定文件路径文件的ID
/// </summary>
/// <param name="p">完整路径</param>
/// <returns></returns>
private static int GetIDFromFilePatch(string p)
{
string[] array = Path.GetFileNameWithoutExtension(p).Split('_');
return Convert.ToInt32(array[array.Length - 1]);
}
/// <summary>
/// 往工作类添加规则文件
/// </summary>
/// <param name="filePatch">规则文件全路径</param>
public static void AddText(string filePatch)
{
try
{
TextDatas = new TextFile(filePatch);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
/// <summary>
/// 往工作类里面添加图片以及代号
/// </summary>
/// <param name="key">代号,默认为每个listbox的代号</param>
/// <param name="filePath">完整的图片路径</param>
public static void AddPicture(int key, string filePath)
{
Pictures.Remove(key);
Pictures.Add(key, filePath);
}
/// <summary>
/// 重新按照key排序词典
/// </summary>
/// <param name="origin">原始词典</param>
/// <returns></returns>
private static Dictionary<int, string> ReSortDictionary(Dictionary<int, string> origin)
{
return (from p in origin
orderby p.Key
select p).ToDictionary((KeyValuePair<int, string> p) => p.Key, (KeyValuePair<int, string> o) => o.Value);
}
/// <summary>
/// 合成代码块
/// </summary>
public static void NowFormat()
{
SaveFileName = "";
Dictionary<int, string> dictionary = ReSortDictionary(Pictures);
Bitmap bitmap = new Bitmap(TextDatas.Fglarge.Width, TextDatas.Fglarge.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
foreach (int key in dictionary.Keys) //遍历词典依次获得图片信息并且在Graphics绘制图片。
{
Infos infos = new Infos(dictionary[key]);
if (infos.PicInfo != null)
{
#region 合成文件名称。
if (string.IsNullOrEmpty(SaveFileName))
{
SaveFileName += Path.GetFileNameWithoutExtension(dictionary[key]);
}
else
{
SaveFileName = SaveFileName + "+" + GetIDFromFilePatch(dictionary[key]);
}
#endregion
if (infos.PicInfo.Opacity != 255)
{
graphics.DrawImage(infos.Pic,
new Rectangle(infos.PicInfo.Left, infos.PicInfo.Top, infos.PicInfo.Width, infos.PicInfo.Height),
0,
0,
infos.Pic.Width,
infos.Pic.Height,
GraphicsUnit.Pixel,
Picture.GetAlphaImgAttr(infos.PicInfo.Opacity)
);
}
else
{
graphics.DrawImage(infos.Pic,
new Rectangle(infos.PicInfo.Left, infos.PicInfo.Top, infos.PicInfo.Width, infos.PicInfo.Height)
);
}
}
}
}
NewPicture = bitmap.Clone(Picture.GetRectFromPictureWithouBlank(bitmap), bitmap.PixelFormat);//裁剪生成图片多余的透明像素
}
/// <summary>
/// 废弃的方法,暂时不会用。
/// </summary>
/// <param name="Infos"></param>
/// <returns></returns>
[Obsolete]
private static Bitmap MixBitmaps(params Infos[] Infos)
{
int width = TextDatas.Fglarge.Width;
int height = TextDatas.Fglarge.Height;
Bitmap bitmap = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
foreach (Infos infos in Infos)
{
if (infos != null)
{
graphics.DrawImage(
rect: new Rectangle(infos.PicInfo.Left,
infos.PicInfo.Top,
infos.PicInfo.Width,
infos.PicInfo.Height),
image: infos.Pic);
}
}
}
return bitmap.Clone(Picture.GetRectFromPictureWithouBlank(bitmap), bitmap.PixelFormat);
}
///读取tlg用的,用:https://github.com/morkt/GARbro。里面的"ArcFormats.dll"和"GameRes.dll"。
///没有会读取不了tlg文件。
public static Bitmap TlgFormat(string file)
{
string exePath = Environment.CurrentDirectory;
string fileArcFormats = Path.Combine(exePath, "ArcFormats.dll");
string fileGameRes = Path.Combine(exePath, "GameRes.dll");
Assembly asmArcFormats = Assembly.LoadFrom(fileArcFormats);
Assembly asmGameRes = Assembly.LoadFrom(fileGameRes);
var asm_BinaryStream = asmGameRes.GetType("GameRes.BinaryStream");
var method_BinaryStream = asm_BinaryStream.GetMethod("FromFile");
var binaryStream = method_BinaryStream.Invoke(null, new object[] { file });
var asm_TlgFormat = asmArcFormats.GetType("GameRes.Formats.KiriKiri.TlgFormat");
var tlgFormat = Activator.CreateInstance(asm_TlgFormat);
var method_ReadMetaData = asm_TlgFormat.GetMethod("ReadMetaData");
var info = method_ReadMetaData.Invoke(tlgFormat, new object[] { binaryStream });
var method_Read = asm_TlgFormat.GetMethod("Read");
var imageData = method_Read.Invoke(tlgFormat, new object[] { binaryStream, info });
var bitmapSource = (BitmapSource)imageData.GetType().GetProperty("Bitmap").GetValue(imageData);
BitmapEncoder bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(bitmapSource));
MemoryStream stream = new MemoryStream();
bitmapEncoder.Save(stream);
return new Bitmap(stream);
}
}
}