Skip to content

Commit f7cb7e6

Browse files
author
kritsu
committed
1.7.1.4 update
1 parent f623fe5 commit f7cb7e6

File tree

20 files changed

+157
-150
lines changed

20 files changed

+157
-150
lines changed

ExtractorSharp.Core/ExtractorSharp.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ProjectGuid>{09014DD5-3314-437E-BB2E-6FBD0D1E7864}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>ExtractorSharp.Core</RootNamespace>
10+
<RootNamespace>ExtractorSharp</RootNamespace>
1111
<AssemblyName>ExtractorSharp.Core</AssemblyName>
1212
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>

ExtractorSharp.Core/Handle/FifthHandler.cs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,44 @@ namespace ExtractorSharp.Handle {
1111
/// Ver5处理器
1212
/// </summary>
1313
public class FifthHandler : SecondHandler{
14-
private readonly Dictionary<Sprite, DDS_Info> Map = new Dictionary<Sprite, DDS_Info>();
15-
private readonly List<DDS> List = new List<DDS>();
14+
private readonly Dictionary<int, DDS_Info> Map = new Dictionary<int, DDS_Info>();
15+
public readonly List<DDS> List = new List<DDS>();
1616
public FifthHandler(Album Album) : base(Album) {}
1717

1818

1919
public override Bitmap ConvertToBitmap(Sprite entity) {
2020
if (entity.Type < ColorBits.LINK && entity.Length > 0) {
2121
return base.ConvertToBitmap(entity);
2222
}
23-
if (Map.ContainsKey(entity)) {
24-
var index = Map[entity];
25-
var dds = index.DDS;
26-
var bmp = dds.Pictrue.Clone(index.Rectangle, PixelFormat.DontCare);
27-
if (index.Top != 0) {
28-
bmp.RotateFlip(RotateFlipType.Rotate90FlipXY);
29-
}
30-
return bmp;
23+
24+
if (!Map.ContainsKey(entity.Index)) {
25+
return new Bitmap(1, 1);
3126
}
32-
return new Bitmap(1, 1);
27+
var index = Map[entity.Index];
28+
var dds = index.DDS;
29+
var bmp = dds.Pictrue.Clone(index.Rectangle, PixelFormat.DontCare);
30+
if (index.Top != 0) {
31+
bmp.RotateFlip(RotateFlipType.Rotate90FlipXY);
32+
}
33+
return bmp;
3334

3435
}
3536

3637
public override byte[] ConvertToByte(Sprite entity) {
3738
if (entity.Type < ColorBits.LINK && entity.Length > 0) {
3839
return base.ConvertToByte(entity);
3940
}
40-
var type = entity.Type < ColorBits.LINK ? entity.Type : entity.Type - 4;
41-
var dds = DDS.CreateFromBitmap(entity.Picture, type);
42-
Map[entity] = new DDS_Info() {
41+
42+
if (entity.Width * entity.Height == 1) {
43+
entity.Compress = Compress.NONE;
44+
return base.ConvertToByte(entity);
45+
}
46+
if (entity.Compress == Compress.ZLIB) {
47+
//非DDS_ZLIB 不识别
48+
entity.Compress = Compress.DDS_ZLIB;
49+
}
50+
var dds = DDS.CreateFromBitmap(entity);
51+
Map[entity.Index] = new DDS_Info{
4352
DDS = dds,
4453
RightDown = new Point(dds.Width, dds.Height)
4554
};
@@ -55,10 +64,11 @@ public override byte[] AdjustData() {
5564
List.Clear();
5665
foreach (var index in Map.Values) {
5766
var dds = index.DDS;
58-
if (!List.Contains(dds)) {
59-
dds.Index = List.Count;
60-
List.Add(dds);
67+
if (List.Contains(dds)) {
68+
continue;
6169
}
70+
dds.Index = List.Count;
71+
List.Add(dds);
6272
}
6373

6474
var ms = new MemoryStream();
@@ -69,13 +79,14 @@ public override byte[] AdjustData() {
6979
ms.WriteInt((int)dds.Version);
7080
ms.WriteInt((int)dds.Type);
7181
ms.WriteInt(dds.Index);
72-
ms.WriteInt(dds.Size);
73-
ms.WriteInt(dds.DDS_Size);
82+
ms.WriteInt(dds.Length);
83+
ms.WriteInt(dds.FullLength);
7484
ms.WriteInt(dds.Width);
7585
ms.WriteInt(dds.Height);
7686
}
7787

7888
var ver2List = new List<Sprite>();
89+
var start = ms.Length;
7990
foreach (var entity in Album.List) {
8091
ms.WriteInt((int)entity.Type);
8192
if (entity.Type == ColorBits.LINK) {
@@ -94,7 +105,7 @@ public override byte[] AdjustData() {
94105
ver2List.Add(entity);
95106
continue;
96107
}
97-
var info = Map[entity];
108+
var info = Map[entity.Index];
98109
ms.WriteInt(info.Unknown);
99110
ms.WriteInt(info.DDS.Index);
100111
ms.WriteInt(info.LeftUp.X);
@@ -103,9 +114,11 @@ public override byte[] AdjustData() {
103114
ms.WriteInt(info.RightDown.Y);
104115
ms.WriteInt(info.Top);
105116
}
117+
Album.Info_Length = ms.Length - start;
106118
foreach (var dds in List) {
107119
ms.Write(dds.Data);
108120
}
121+
109122
foreach(var sprite in ver2List) {
110123
ms.Write(sprite.Data);
111124
}
@@ -121,19 +134,19 @@ public override byte[] AdjustData() {
121134
}
122135

123136
public override void CreateFromStream(Stream stream) {
124-
int index_count = stream.ReadInt();
137+
var index_count = stream.ReadInt();
125138
Album.Length = stream.ReadInt();
126-
int count = stream.ReadInt();
139+
var count = stream.ReadInt();
127140
var table = new List<Color>(Colors.ReadPalette(stream, count));
128141
Album.Tables = new List<List<Color>> { table };
129142
var list = new List<DDS>();
130-
for (int i = 0; i < index_count; i++) {
143+
for (var i = 0; i < index_count; i++) {
131144
var dds = new DDS {
132145
Version = (DDS_Version)stream.ReadInt(),
133146
Type = (ColorBits)stream.ReadInt(),
134147
Index = stream.ReadInt(),
135-
Size = stream.ReadInt(),
136-
DDS_Size = stream.ReadInt(),
148+
Length = stream.ReadInt(),
149+
FullLength = stream.ReadInt(),
137150
Width = stream.ReadInt(),
138151
Height = stream.ReadInt()
139152
};
@@ -162,7 +175,7 @@ public override void CreateFromStream(Stream stream) {
162175
ver2List.Add(entity);
163176
continue;
164177
}
165-
int j = stream.ReadInt();
178+
var j = stream.ReadInt();
166179
var k = stream.ReadInt();
167180
var dds = list[k];
168181
var leftup = new Point(stream.ReadInt(), stream.ReadInt());
@@ -175,13 +188,14 @@ public override void CreateFromStream(Stream stream) {
175188
RightDown = rightdown,
176189
Top = top
177190
};
178-
Map.Add(entity, info);
191+
Map.Add(entity.Index, info);
179192
}
193+
180194
foreach (var entity in dic.Keys) {
181195
entity.Target = Album.List[dic[entity]];
182196
}
183197
foreach (var dds in list) {
184-
var data = new byte[dds.Size];
198+
var data = new byte[dds.Length];
185199
stream.Read(data);
186200
dds.Data = data;
187201
}

ExtractorSharp.Core/Handle/SixthHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public override byte[] ConvertToByte(Sprite entity) {
3636
}
3737
var data = entity.Picture.ToArray();
3838
var ms = new MemoryStream();
39-
for (var i = 0; i < data.Length; i += 4) {
39+
var table = Album.CurrentTable;
40+
for (var i = 0; i < data.Length && table.Count <= 256; i += 4) {
4041
var color = Color.FromArgb(data[i + 3], data[i + 2], data[i + 1], data[i]);
41-
var table = Album.CurrentTable;
4242
if (!table.Contains(color)) {
4343
table.Add(color);
4444
}
45-
ms.WriteByte((byte)table.IndexOf(color));
45+
ms.WriteByte((byte) table.IndexOf(color));
4646
}
4747
ms.Close();
4848
data = ms.ToArray();

ExtractorSharp.Core/Lib/Reader/Npks.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public static List<Album> ReadNPK(this Stream stream, string file) {
191191
/// 保存为NPK
192192
/// </summary>
193193
/// <param name="fileName"></param>
194-
public static void WriteNpk(Stream stream, List<Album> List) {
194+
public static void WriteNpk(this Stream stream, List<Album> List) {
195195
var position = 52 + List.Count * 264;
196196
for (var i = 0; i < List.Count; i++) {
197197
List[i].Adjust();
@@ -221,18 +221,18 @@ public static void WriteNpk(Stream stream, List<Album> List) {
221221
public static List<Album> Load(string file) => Load(false, file);
222222

223223
public static List<Album> Load(bool onlyPath, string file) {
224-
var List = new List<Album>();
224+
var list = new List<Album>();
225225
if (Directory.Exists(file)) {
226226
return Load(onlyPath, Directory.GetFiles(file));
227227
}
228228
if (!File.Exists(file)) {
229-
return List;
229+
return list;
230230
}
231231
using (var stream = File.OpenRead(file)) {
232232
if (onlyPath) {
233233
return ReadInfo(stream);
234234
}
235-
List<Album> enums = stream.ReadNPK(file);
235+
var enums = stream.ReadNPK(file);
236236
return enums;
237237
}
238238
}

ExtractorSharp.Core/Model/Album.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public void Save(Stream stream) {
209209
}
210210

211211
public void Save(string file) {
212-
using(var fs=File.OpenWrite(file)) {
212+
using(var fs=new FileStream(file,FileMode.Create)) {
213213
Save(fs);
214214
}
215215
}

ExtractorSharp.Core/Model/DDS.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Drawing;
3-
using System.IO;
43
using ExtractorSharp.Core.Lib;
54

65
namespace ExtractorSharp.Data {
@@ -11,18 +10,19 @@ public class DDS {
1110
public int Index { set; get; }
1211
public int Width { set; get; } = 4;
1312
public int Height { set; get; } = 4;
14-
public int Size { set; get; }
15-
public int DDS_Size { set; get; }
13+
public int Length { set; get; }
14+
public int FullLength { set; get; }
1615
public byte[] Data { set; get; }
1716
public DDS_Version Version { set; get; } = DDS_Version.DXT1;
1817
public ColorBits Type { set; get; } = ColorBits.DXT_1;
18+
1919
public Bitmap Pictrue {
2020
get {
2121
if (image != null) {
2222
return image;
2323
}
24-
var data = Zlib.Decompress(Data, DDS_Size);
25-
if (Type < ColorBits.DXT_1) {
24+
var data = Zlib.Decompress(Data, FullLength);
25+
if (Type < ColorBits.LINK) {
2626
return Bitmaps.FromArray(data, new Size(Width, Height), Type);
2727
}
2828
var dds = Ddss.Decode(data);
@@ -35,16 +35,21 @@ public Bitmap Pictrue {
3535

3636
private Bitmap image;
3737

38-
public static DDS CreateFromBitmap(Bitmap bmp, ColorBits type) {
38+
public static DDS CreateFromBitmap(Sprite sprite) {
39+
var bmp = sprite.Picture;
40+
var type = sprite.Type;
41+
if (type > ColorBits.LINK) {
42+
type -= 4;
43+
}
3944
var data = bmp.ToArray(type);
40-
var dds_size = data.Length;
45+
var fullLength = data.Length;
4146
var width = bmp.Width;
4247
var height = bmp.Height;
4348
data = Zlib.Compress(data);
44-
var dds = new DDS() {
49+
var dds = new DDS {
4550
Data = data,
46-
DDS_Size = dds_size,
47-
Size = data.Length,
51+
FullLength = fullLength,
52+
Length = data.Length,
4853
Width = width,
4954
Height = height,
5055
Type = type
@@ -55,21 +60,24 @@ public static DDS CreateFromBitmap(Bitmap bmp, ColorBits type) {
5560

5661
public class DDS_Info {
5762
public DDS DDS { set; get; }
63+
5864
/// <summary>
5965
/// 左上角顶点坐标
6066
/// </summary>
6167
public Point LeftUp { set; get; }
68+
6269
/// <summary>
6370
/// 右下角顶点坐标
6471
/// </summary>
6572
public Point RightDown { set; get; }
73+
6674
/// <summary>
6775
/// 大小
6876
/// </summary>
6977
public Size Size => new Size(RightDown.X - LeftUp.X, RightDown.Y - LeftUp.Y);
7078

7179
public int Top { set; get; }
72-
80+
7381
public Rectangle Rectangle => new Rectangle(LeftUp, Size);
7482

7583
public int Unknown { get; set; }

ExtractorSharp.Core/Model/Sprite.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ public void Load() {
147147
/// <summary>
148148
/// 替换贴图
149149
/// </summary>
150+
/// <param name="type"></param>
151+
/// <param name="isAdjust"></param>
150152
/// <param name="bmp"></param>
151153
public void ReplaceImage(ColorBits type, bool isAdjust, Bitmap bmp) {
152154
if (bmp == null) {
@@ -158,7 +160,7 @@ public void ReplaceImage(ColorBits type, bool isAdjust, Bitmap bmp) {
158160
if (type == ColorBits.UNKOWN) {
159161
if (Type == ColorBits.LINK) {
160162
type = ColorBits.ARGB_1555;
161-
} else if (Type > ColorBits.LINK) {
163+
} else if (Version != Img_Version.Ver5 && Type > ColorBits.LINK) {
162164
type = Type - 4;
163165
} else {
164166
type = Type;
@@ -229,7 +231,7 @@ public virtual void Adjust() {
229231
if (!IsOpen)
230232
return;
231233
Data = Parent.ConvertToByte(this);
232-
if (Data.Length > 0 && Compress == Compress.ZLIB) {
234+
if (Data.Length > 0 && Compress >= Compress.ZLIB) {
233235
Data = Zlib.Compress(Data);
234236
}
235237
Length = Data.Length; //不压缩时,按原长度保存

ExtractorSharp.Core/Properties/Resources.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ExtractorSharp.UnitTest/TestHandler.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@
77
using System.Linq;
88
using System.Text;
99
using System.Threading.Tasks;
10+
using ExtractorSharp.Json;
1011

1112
namespace ExtractorSharp.UnitTest {
1213
[TestClass]
1314
public class TestHandler {
1415

1516
static TestHandler() {
16-
Handler.Regisity(Img_Version.Other, typeof(OtherHandler));
17-
Handler.Regisity(Img_Version.Ver1, typeof(FirstHandler));
18-
Handler.Regisity(Img_Version.Ver2, typeof(SecondHandler));
19-
Handler.Regisity(Img_Version.Ver4, typeof(FourthHandler));
2017
Handler.Regisity(Img_Version.Ver5, typeof(FifthHandler));
21-
Handler.Regisity(Img_Version.Ver6, typeof(SixthHandler));
2218

2319
}
2420

2521
[TestMethod]
2622
public void Test() {
27-
foreach(string f in Directory.GetFiles(@"D:\地下城与勇士\ImagePacks2")) {
28-
Npks.Load(f);
23+
foreach(var f in Directory.GetFiles(@"D:\地下城与勇士\ImagePacks2")) {
24+
var list=Npks.Load(f);
25+
foreach (var e in list) {
26+
if (e.Version == Img_Version.Ver5) {
27+
var handler = e.Handler as FifthHandler;
28+
foreach (var a in handler.List) {
29+
var image = a.Pictrue;
30+
}
31+
}
32+
33+
GC.Collect();
34+
}
2935
}
3036
}
3137
}

0 commit comments

Comments
 (0)