-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOtherClipOverwriter.cs
497 lines (473 loc) · 25.2 KB
/
OtherClipOverwriter.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
using Microsoft.SqlServer.Server;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
//建立其余元件修复重写类
class OtherClipOverwriter
{
//生成其余元件修复重写部分
public void OtherClipOverwrite(string Fpath)
{
try
{
//创建路径文件夹实例
DirectoryInfo TheFolder = new DirectoryInfo(Fpath);
//创建文件数组
FileInfo[] files = TheFolder.GetFiles();
//其余元件帧间空帧检测//20240513新增
Console.WriteLine("修复检测图层帧间空帧中......");
//遍历文件夹内文件
foreach (FileInfo NextFile in files)
{
//流式读取文件类型
FileStream stream = new FileStream(NextFile.FullName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
string fileclass = "";
try
{
for (int i = 0; i < 2; i++)
{
fileclass += reader.ReadByte().ToString();
}
}
catch (Exception)
{
throw;
}
stream.Close();
//判定是否为xml
if (fileclass == "6068")
{
//创建新xml读取对象
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(NextFile.FullName);
//时间长而弃用//重载xml
//时间长而弃用///xmlDoc.Load(TheFolder.FullName);
//获取根节点root
XmlNode root = xmlDoc.DocumentElement;
//获取节点layers
XmlNode layers = root.FirstChild.FirstChild.FirstChild;
//获取layers图层列表
XmlNodeList layersnodeList = layers.ChildNodes;
//检测
for (int i = 0; i < layersnodeList.Count; i++)
{
//转换为Xml节点
XmlNode node = layersnodeList[i];
//转换DOMLayer为XmlElement以便于识别是否存在实帧
XmlElement DOMLayer = (XmlElement)node;
//判断是否存在DOMSymbolInstance和DOMBitmapInstance,以判定是否为帧间空帧,默认为0(遇到空帧且值为1则存在帧间空帧)
int dsitrue = 0;
//判定前空后实数
int nullandhasele = 0;
//先行检测
//记录帧间空帧所在帧数
for (int j = 0; j < DOMLayer.FirstChild.ChildNodes.Count; j++)
{
//转换为XmlElement
XmlElement DOMFrame = (XmlElement)DOMLayer.FirstChild.ChildNodes[j];
if (DOMFrame.GetElementsByTagName("DOMBitmapInstance").Count != 0 || DOMFrame.GetElementsByTagName("DOMSymbolInstance").Count != 0)
{
if (dsitrue == 0)
{
nullandhasele++;
}
else { }
dsitrue = 1;
}
else
{
if (dsitrue == 1)
{
//获取帧间空帧的图层名称
string nname = DOMLayer.GetAttribute("name");
//获取帧间空帧的帧位置
int nindex = int.Parse(DOMFrame.GetAttribute("index"));
//获取帧间空帧的帧长度
int nduration;
if (DOMFrame.GetAttribute("duration") == null || DOMFrame.GetAttribute("duration") == "")
{
nduration = 1;
}
else
{
nduration = int.Parse(DOMFrame.GetAttribute("duration"));
}
Console.WriteLine(NextFile.Name.Substring(0, NextFile.Name.Length - 4) + "元件图层" + nname + "第" + nindex + "帧(在Adobe Animate中为第" + (nindex + 1) + "帧)为帧间空帧,长度" + nduration + "帧,将会引发错误");
Console.WriteLine("元件的" + DOMLayer.GetAttribute("name") + "图层的帧间空帧已处理");
}
else { }
dsitrue = 0;
}
}
//处理
if (nullandhasele > 1)
{
//重置
dsitrue = 0;
//重置
nullandhasele = 0;
//记录图层序号
int layernum = 0;
//预置addDOMLayer节点,新建图层
XmlElement addDOMLayer = xmlDoc.CreateElement("DOMLayer", xmlDoc.DocumentElement.NamespaceURI);
//预置addframes节点
XmlElement addframes = xmlDoc.CreateElement("frames", xmlDoc.DocumentElement.NamespaceURI);
//设name为DOMLayer的name值(此处会出现极大卡顿,暂无法修复)
addDOMLayer.SetAttribute("name", DOMLayer.GetAttribute("name") + "_" + layernum);
//将addframes作为addDOMLayer的子节点
addDOMLayer.AppendChild(addframes);
//记录帧间空帧所在帧数
for (int j = 0; j < DOMLayer.FirstChild.ChildNodes.Count; j++)
{
//转换为XmlElement
XmlElement DOMFrame = (XmlElement)DOMLayer.FirstChild.ChildNodes[j];
if (DOMFrame.GetElementsByTagName("DOMBitmapInstance").Count != 0 || DOMFrame.GetElementsByTagName("DOMSymbolInstance").Count != 0)
{
if (dsitrue == 0)
{
nullandhasele++;
}
else { }
dsitrue = 1;
}
else
{
//已迁移至检测
dsitrue = 0;
}
//如果未检测到空帧
if (nullandhasele <= 1)
{
//此处填充新建图层的实帧前的空帧
//预置addDOMFrame节点
XmlElement addDOMFrame = xmlDoc.CreateElement("DOMFrame", xmlDoc.DocumentElement.NamespaceURI);
//设index为DOMFrame的index值
addDOMFrame.SetAttribute("index", DOMFrame.GetAttribute("index"));
//设duration为DOMFrame的duration值
string fduration = DOMFrame.GetAttribute("duration");
if (fduration == "" || fduration == "0" || fduration == null)
{
fduration = "1";
}
else { }
addDOMFrame.SetAttribute("duration", fduration);
//将addDOMFrame作为addframes的子节点
addframes.AppendChild(addDOMFrame);
}
else
{
//将本DOMFrame作为addframes的子节点
addframes.AppendChild(DOMFrame);
//回落
j--;
}
}
if (nullandhasele <= 1) { }
else
{
//将addDOMLayer插在DOMLayer的前面
root.FirstChild.FirstChild.FirstChild.InsertBefore(addDOMLayer, node);
Console.WriteLine("已提取" + NextFile.Name.Substring(0, NextFile.Name.Length - 4) + "元件的" + DOMLayer.GetAttribute("name") + "图层的帧间空帧并新建图层");
//图层下标增加
layernum++;
//回落
i--;
//保存xml
xmlDoc.Save(NextFile.FullName);
}
}
else { }
}
}
}
Console.WriteLine("图层帧间空帧修复检测完成");
Console.WriteLine("开始对其余元件进行删除全空和全空帧图层以及删除图层末尾空帧......");
//为文件数组排序
Array.Sort(files, new FileNameSort());
//遍历文件夹内文件
foreach (FileInfo NextFile in files)
{
//流式读取文件类型
FileStream stream = new FileStream(NextFile.FullName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
string fileclass = "";
try
{
for (int i = 0; i < 2; i++)
{
fileclass += reader.ReadByte().ToString();
}
}
catch (Exception)
{
throw;
}
stream.Close();
//判定是否为xml
if (fileclass == "6068")
{
//创建xml读取对象
XmlDocument xmlDoc = new XmlDocument();
//读取xml
xmlDoc.Load(NextFile.FullName);
//获取根节点root
XmlNode root = xmlDoc.DocumentElement;
//获取节点layers
XmlNode layers = root.FirstChild.FirstChild.FirstChild;
//获取layers图层列表
XmlNodeList layersnodeList = layers.ChildNodes;
//预置全空或全空帧图层删除列表
List<XmlNode> LayersToRemove = new List<XmlNode>();
//预置删除图层末尾空帧列表
List<XmlNode> LayersToOverwrite = new List<XmlNode>();
//预置空帧列表
List<XmlNode> FramesToRemove = new List<XmlNode>();
//判断为SPCUtil解析的元件类型并提示
if (NextFile.Name.Substring(0, 1) == "M" || NextFile.Name.Substring(0, 1) == "A" || NextFile.Name.Substring(0, NextFile.Name.Length - 4) == "A_Main")
{
Console.WriteLine("抱歉,不支持用SPCUtil解析PAM得到的元件");
}
//判断为TwinKles-ToolKit解析的元件类型并提示
if (NextFile.Name.Substring(0, 2) == "sp" || NextFile.Name.Substring(0, 2) == "an" || NextFile.Name.Substring(0, NextFile.Name.Length - 4) == "main_animation")
{
Console.WriteLine("抱歉,不支持用TwinKles-ToolKit解析PAM得到的元件");
}
//判定是i元件还是a元件
if (NextFile.Name.Substring(0, 1) == "i")
{
//删除全空或全空帧图层
foreach (XmlNode node in layersnodeList)
{
//转换DOMLayer为XmlElement以便于识别是否存在实帧
XmlElement DOMLayer = (XmlElement)node;
//判断是否存在DOMBitmapInstance,以判定是否为全空或全空帧,默认为0(结果为0则为全空或全空帧)
int dsiexist = 0;
foreach (XmlElement element in DOMLayer.ChildNodes)
{
if (element.GetElementsByTagName("DOMBitmapInstance").Count != 0 || element.GetElementsByTagName("DOMSymbolInstance").Count != 0)
{
dsiexist = 1;
}
else { }
}
//积累待删除记录列表
if (dsiexist == 0)
{
LayersToRemove.Add(node);
//node XmlElement化
XmlElement enode = (XmlElement)node;
//获取被删图层名称
string nenode = enode.GetAttribute("name");
Console.WriteLine("删除" + NextFile.Name.Substring(0, NextFile.Name.Length - 4) + "元件全空或全空帧图层" + nenode);
continue;
}
else { }
}
//根据删除列表删除全空或全空帧图层
foreach (XmlNode node in LayersToRemove)
{
node.ParentNode.RemoveChild(node);
//保存xml
xmlDoc.Save(NextFile.FullName);
}
//重载xml
xmlDoc.Load(NextFile.FullName);
//获取根节点root
root = xmlDoc.DocumentElement;
//获取节点layers
layers = root.FirstChild.FirstChild.FirstChild;
//获取layers图层列表
layersnodeList = layers.ChildNodes;
//删除末尾空帧
foreach (XmlNode node in layersnodeList)
{
//转换DOMLayer为XmlElement以便于识别是否存在实帧
XmlElement DOMLayer = (XmlElement)node;
//判断是否存在DOMBitmapInstance,以判定是否为末尾空帧,默认为0(最终结果为0则存在末尾空帧)
int dsitrue = 0;
foreach (XmlElement element in DOMLayer.FirstChild.ChildNodes)
{
if (element.GetElementsByTagName("DOMBitmapInstance").Count != 0 || element.GetElementsByTagName("DOMSymbolInstance").Count != 0)
{
dsitrue = 1;
}
else
{
dsitrue = 0;
}
}
//积累待重写记录列表
if (dsitrue == 0)
{
LayersToOverwrite.Add(node);
//node XmlElement化
XmlElement enode = (XmlElement)node;
//获取被删末尾空帧的图层名称
string nenode = enode.GetAttribute("name");
Console.WriteLine("删除" + NextFile.Name.Substring(0, NextFile.Name.Length - 4) + "元件图层" + nenode + "的末尾空帧");
continue;
}
else { }
}
//根据删除列表重写末尾空帧图层
foreach (XmlNode node in LayersToOverwrite)
{
//判断是否存在DOMSymbolInstance,以判定是否为末尾空帧,默认为0(最终结果为0则存在末尾空帧)
int dsitrue = 0;
//倒序检测空帧
for (int i = node.FirstChild.ChildNodes.Count - 1; dsitrue == 0; i--)
{
//取需要删除的空帧
XmlNode element = node.FirstChild.ChildNodes[i];
//转换DOMFrame为XmlElement以便于识别是否存在实帧
XmlElement DOMFrame = (XmlElement)element;
if (DOMFrame.GetElementsByTagName("DOMBitmapInstance").Count != 0 || DOMFrame.GetElementsByTagName("DOMSymbolInstance").Count != 0)
{
dsitrue = 1;
}
else
{
FramesToRemove.Add(element);
continue;
}
}
}
//根据删除列表删除空帧
foreach (XmlNode element in FramesToRemove)
{
element.ParentNode.RemoveChild(element);
//保存xml
xmlDoc.Save(NextFile.FullName);
}
}
//判断为a元件并修复
if (NextFile.Name.Substring(0, 1) == "a")
{
//删除全空或全空帧图层
foreach (XmlNode node in layersnodeList)
{
//转换DOMLayer为XmlElement以便于识别是否存在实帧
XmlElement DOMLayer = (XmlElement)node;
//判断是否存在DOMSymbolInstance,以判定是否为全空或全空帧,默认为0(结果为0则为全空或全空帧)
int dsiexist = 0;
foreach (XmlElement element in DOMLayer.ChildNodes)
{
if (element.GetElementsByTagName("DOMBitmapInstance").Count != 0 || element.GetElementsByTagName("DOMSymbolInstance").Count != 0)
{
dsiexist = 1;
}
else { }
}
//积累待删除记录列表
if (dsiexist == 0)
{
LayersToRemove.Add(node);
//node XmlElement化
XmlElement enode = (XmlElement)node;
//获取被删图层名称
string nenode = enode.GetAttribute("name");
Console.WriteLine("删除" + NextFile.Name.Substring(0, NextFile.Name.Length - 4) + "元件全空或全空帧图层" + nenode);
continue;
}
else { }
}
//根据删除列表删除全空或全空帧图层
foreach (XmlNode node in LayersToRemove)
{
node.ParentNode.RemoveChild(node);
//保存xml
xmlDoc.Save(NextFile.FullName);
}
//重载xml
xmlDoc.Load(NextFile.FullName);
//获取根节点root
root = xmlDoc.DocumentElement;
//获取节点layers
layers = root.FirstChild.FirstChild.FirstChild;
//获取layers图层列表
layersnodeList = layers.ChildNodes;
//删除末尾空帧
foreach (XmlNode node in layersnodeList)
{
//转换DOMLayer为XmlElement以便于识别是否存在实帧
XmlElement DOMLayer = (XmlElement)node;
//判断是否存在DOMSymbolInstance,以判定是否为末尾空帧,默认为0(最终结果为0则存在末尾空帧)
int dsitrue = 0;
foreach (XmlElement element in DOMLayer.FirstChild.ChildNodes)
{
if (element.GetElementsByTagName("DOMBitmapInstance").Count != 0 || element.GetElementsByTagName("DOMSymbolInstance").Count != 0)
{
dsitrue = 1;
}
else
{
dsitrue = 0;
}
}
//积累待重写记录列表
if (dsitrue == 0)
{
LayersToOverwrite.Add(node);
//node XmlElement化
XmlElement enode = (XmlElement)node;
//获取被删末尾空帧的图层名称
string nenode = enode.GetAttribute("name");
Console.WriteLine("删除" + NextFile.Name.Substring(0, NextFile.Name.Length - 4) + "元件图层" + nenode + "的末尾空帧");
continue;
}
else { }
}
//根据删除列表重写末尾空帧图层
foreach (XmlNode node in LayersToOverwrite)
{
//判断是否存在DOMSymbolInstance,以判定是否为末尾空帧,默认为0(最终结果为0则存在末尾空帧)
int dsitrue = 0;
//倒序检测空帧
for (int i = node.FirstChild.ChildNodes.Count - 1; dsitrue == 0; i--)
{
//取需要删除的空帧
XmlNode element = node.FirstChild.ChildNodes[i];
//转换DOMFrame为XmlElement以便于识别是否存在实帧
XmlElement DOMFrame = (XmlElement)element;
if (DOMFrame.GetElementsByTagName("DOMBitmapInstance").Count != 0 || DOMFrame.GetElementsByTagName("DOMSymbolInstance").Count != 0)
{
dsitrue = 1;
}
else
{
FramesToRemove.Add(element);
continue;
}
}
}
//根据删除列表删除空帧
foreach (XmlNode element in FramesToRemove)
{
element.ParentNode.RemoveChild(element);
//保存xml
xmlDoc.Save(NextFile.FullName);
}
}
else { }
}
else { }
}
Console.WriteLine("图层修复检测完成");
}
catch
{
Console.WriteLine("OtherClipOverwrite ERROR");
//提示按任意键继续
Console.WriteLine("Press any key to continue...");
//输入任意键退出
Console.ReadLine();
}
}
}