-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexturePorter.cs
190 lines (177 loc) · 7.13 KB
/
TexturePorter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SoulsFormats;
using DSPorterUtil;
namespace DSRPorter
{
public class TexturePorter
{
private readonly DSPorter _porter;
private readonly Dictionary<string, HashSet<TPF>> tpfCache = new();
public TexturePorter(DSPorter porter)
{
_porter = porter;
}
private TPF? CreateTPFforBND(BND3 bnd)
{
HashSet<FLVER2.Texture> textures = GetFlverTextures(bnd);
return CreateTPF(textures);
}
private bool AddMatchingTextures(string matchName, TPF sourceTPF, TPF? targetTPF)
{
foreach (var tpfTex in sourceTPF.Textures)
{
if (tpfTex.Name == matchName)
{
targetTPF ??= new TPF()
{
Compression = DCX.Type.None,
Encoding = sourceTPF.Encoding,
Flag2 = sourceTPF.Flag2,
Platform = sourceTPF.Platform,
};
targetTPF.Textures.Add(tpfTex);
return true;
}
}
return false;
}
private TPF? CreateTPF(HashSet<FLVER2.Texture> textures)
{
TPF? tpfTarget = null;
foreach (var tex in textures)
{
var split = tex.Path.Split("\\");
var fileName = Path.GetFileNameWithoutExtension(split.Last());
if (fileName.StartsWith('m'))
{
string mapFolder = fileName.Split("_")[0];
if (mapFolder == "m19")
mapFolder = "m18"; // Asylum used to be m19
var bhdDirectory = $@"{_porter.DataPath_DSR}\map\{mapFolder}";
foreach (var bhd in Directory.GetFiles(bhdDirectory, "*.tpfbhd"))
{
restart:
if (tpfCache.TryGetValue(bhd, out HashSet<TPF>? tpfSourceList))
{
// TPF cache exists, search through it for the right texture.
foreach (var tpfSource in tpfSourceList)
{
foreach (var tpfTex in tpfSource.Textures)
{
if (tpfTex.Name == fileName)
{
tpfTarget ??= new TPF()
{
Compression = DCX.Type.None,
Encoding = tpfSource.Encoding,
Flag2 = tpfSource.Flag2,
Platform = tpfSource.Platform,
};
tpfTarget.Textures.Add(tpfTex);
goto next;
}
}
}
}
else
{
// TPF cache doesn't exist, build it.
tpfCache.Add(bhd, new HashSet<TPF>());
var bx = BXF3.Read(bhd, bhd.Replace(".tpfbhd", ".tpfbdt"));
foreach (var file in bx.Files)
{
if (TPF.Is(file.Bytes))
{
var sourceTPF = TPF.Read(file.Bytes);
this.tpfCache[bhd].Add(sourceTPF);
}
}
goto restart;
}
}
}
next:;
}
return tpfTarget;
}
private HashSet<FLVER2.Texture> GetFlverTextures(BND3 bnd)
{
HashSet<FLVER2.Texture> flverTextures = new();
GetFlvers(flverTextures, bnd);
return flverTextures;
}
private void GetFlvers(HashSet<FLVER2.Texture> flverTextures, BND3 bnd)
{
foreach (var file in bnd.Files)
{
if (BND3.Is(file.Bytes))
{
GetFlvers(flverTextures, BND3.Read(file.Bytes));
}
else if (FLVER2.Is(file.Bytes))
{
var flver = FLVER2.Read(file.Bytes);
foreach (var mat in flver.Materials)
{
flverTextures.UnionWith(mat.Textures);
}
}
}
}
/// <summary>
/// Modifies an objBND to contain its own textures.
/// </summary>
/// <param name="modelName"></param>
/// <param name="alwaysPort">Port even if self-contained texture processing fails (will not port in all cases).</param>
/// <exception cref="NotImplementedException"></exception>
public void SelfContainTextures_Objbnd(string modelName)
{
string bndPath = $@"{_porter.DataPath_Output}\obj\{modelName}.objbnd.dcx";
string dataPath = _porter.DataPath_Output;
if (!File.Exists(bndPath))
{
bndPath = $@"{_porter.DataPath_DSR}\obj\{modelName}.objbnd.dcx";
dataPath = _porter.DataPath_DSR;
}
if (!File.Exists(bndPath))
{
_porter.OutputLog.Add($"TEXTURE: Couldn't find \"{bndPath}\" in DSR data, skipped self-contained texture processing.");
return;
}
BND3 objBND = BND3.Read(bndPath);
foreach (var file in objBND.Files)
{
if (TPF.Is(file.Bytes))
{
// Already has self contained textures.
_porter.OutputLog.Add($"TEXTURE: Skipped self-contained texture processing for \"{bndPath}\" since it already has texture data.");
return;
}
if (file.ID == 100)
{
throw new NotImplementedException($"Unhandled issue: \"{bndPath}\" has a file with an ID of 100 ({file.Name}).");
}
}
TPF? newObjTPF = CreateTPFforBND(objBND);
if (newObjTPF != null && newObjTPF.Textures.Any())
{
BinderFile binder = new()
{
Name = $@"obj\{modelName}\{modelName}.tpf",
ID = 100,
Bytes = newObjTPF.Write()
};
objBND.Files.Insert(0, binder);
Util.WritePortedSoulsFile(objBND, dataPath, bndPath, _porter.CompressionType);
}
else
{
_porter.OutputLog.Add($"TEXTURE: Couldn't finish self-contained texture processing for \"{bndPath}\", object was skipped.");
}
}
}
}