-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_textures.pas
190 lines (151 loc) · 5.53 KB
/
check_textures.pas
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
{
Tries to use nconvert.exe to check the textures in the target file's Textures.ba2 for correct sizes
}
unit userscript;
const
nconvPath = DataPath + '..\Tools\NConvert\nconvert.exe';
var
targetFile: IInterface;
numErrors: integer;
// Called before processing
// You can remove it if script doesn't require initialization code
function Initialize: integer;
begin
Result := 0;
numErrors := 0;
end;
// called for every record selected in xEdit
function Process(e: IInterface): integer;
begin
Result := 0;
// comment this out if you don't want those messages
targetFile := GetFile(e);
// processing code goes here
end;
function checkDim(x: integer): boolean;
begin
if(x = 0) then begin
Result := false;
exit;
end;
Result := (x and (x - 1)) = 0;
end;
procedure checkTextureDims(texPath: string; w, h: integer);
begin
if(not checkDim(w)) or (not checkDim(h)) then begin
numErrors := numErrors+1;
AddMessage('ERROR! '+texPath+' has invalid dimensions! '+IntToStr(w)+'x'+IntToStr(h));
end else begin
// AddMessage('OK: '+texPath+' '+IntToStr(w)+'x'+IntToStr(h));
end;
end;
function ExtractFileBasename(filename: string): string;
var
curExt: string;
begin
curExt := ExtractFileExt(filename);
Result := copy(filename, 0, length(filename)-length(curExt));
end;
function checkTextureUsingExternalTool(src, texPath: string): boolean;
var
tmpFile, outPath: string;
proc: TProcess;
procResult: TStringList;
i: integer;
res: cardinal;
curLine, firstPart, lastPart: string;
foundW, foundH, p: integer;
begin
foundW := -1;
foundH := -1;
Result := false;
if(not FileExists(nconvPath)) then begin
exit;
end;
// extract...
tmpFile := ProgramPath+'tmp.dds';
outPath := ProgramPath+'OUTPUT.txt';
if(FileExists(tmpFile)) then begin
DeleteFile(tmpFile);
end;
if(FileExists(outPath)) then begin
DeleteFile(outPath);
end;
ResourceCopy(src, texPath, tmpFile);
// now try using nconv
res := ShellExecuteWait(
TForm(frmMain).Handle, // parent window handle, use 0 for none
'open', // verb
'cmd.exe', // application
// found via trial&error...
'"/C" "'+nconvPath+'" "-info" "'+tmpFile+'" > "'+outPath+'"', // parameters
'', // working directory
SW_HIDE // window mode
);
if(res <> 0) or (not FileExists(outPath)) then begin
AddMessage('failed to use nconv');
exit;
end;
procResult := TStringList.create;
procResult.loadFromFile(outPath);
Result := true;
for i:=0 to procResult.count-1 do begin
curLine := procResult[i];
p := Pos(':', curLine);
if (p > 0) then begin
firstPart := trim(Copy(curLine, 1, p-1));
lastPart := trim(Copy(curLine, p+1, length(curLine)-p));
if(firstPart = 'Width') then begin
foundW := StrToInt(lastPart);
end else if(firstPart = 'Height') then begin
foundH := StrToInt(lastPart);
end;
end;
end;
// do we have them?
if(foundW > 0) and (foundH > 0) then begin
Result := true;
checkTextureDims(texPath, foundW, foundH);
end;
procResult.free();
end;
procedure checkTexture(src, texPath: string);
var
texStream: TBytesStream;
bmp: TBitmap;
begin
texStream := ResourceOpenData(src, texPath);
bmp := TBitmap.create;
if(wbDDSDataToBitmap(texStream, bmp)) then begin
//AddMessage(IntToStr(bmp.height)+'x'+IntToStr(bmp.width));
checkTextureDims(texPath, bmp.width, bmp.height);
end else begin
if(not checkTextureUsingExternalTool(src, texPath)) then begin
AddMessage('WARNING! Failed to process '+texPath+': seems we can''t parse that');
end;
end;
bmp.free();
//texStream.free();
end;
// Called after processing
// You can remove it if script doesn't require finalization code
function Finalize: integer;
var
textures: TStringList;
i: integer;
modFileName, ba2name: string;
begin
modFileName := ExtractFileBasename(GetFileName(targetFile));
ba2name := modFileName + ' - Textures.ba2';
AddMessage('checking '+ba2name);
Result := 0;
textures := TStringList.create();
ResourceList(DataPath+ba2name, textures);
for i:=0 to textures.count-1 do begin
//AddMessage(textures[i]);
checkTexture(DataPath+ba2name, textures[i]);
end;
AddMessage('Finished. Found '+IntToStr(numErrors)+' issues.');
textures.free();
end;
end.