dfwad: detect UTF-8
[d2df-editor.git] / src / editor / f_addresource.pas
blob913f19f30d7331e3a2894c6c236f1aee3fcdb75c
1 unit f_addresource;
3 {$INCLUDE ../shared/a_modes.inc}
5 interface
7 uses
8 LCLIntf, LCLType, LMessages, Messages, SysUtils, Variants, Classes,
9 Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
10 ExtDlgs, e_Log, e_textures, WADEDITOR;
12 type
13 TOKFunction = function: Boolean;
15 TAddResourceForm = class (TForm)
16 LabelWADs: TLabel;
17 cbWADList: TComboBox;
18 LabelSections: TLabel;
19 cbSectionsList: TComboBox;
20 lbResourcesList: TListBox;
21 bOK: TButton;
22 bCancel: TButton;
24 procedure FormActivate(Sender: TObject);
25 procedure bOKClick(Sender: TObject);
26 procedure cbWADListChange(Sender: TObject);
27 procedure cbSectionsListChange(Sender: TObject);
28 procedure lbResourcesListClick(Sender: TObject);
30 protected
31 FResourceName: String;
32 FFullResourceName: String;
33 FResourceSelected: Boolean;
34 FOKFunction: TOKFunction;
36 public
37 property ResourceName: String read FResourceName;
38 property FullResourceName: String read FFullResourceName;
39 property OKFunction: TOKFunction read FOKFunction write FOKFunction;
40 end;
42 var
43 AddResourceForm: TAddResourceForm;
45 implementation
47 uses
48 f_main, WADSTRUCT, g_language, utils, g_options;
50 {$R *.lfm}
52 const
53 STANDART_WAD = 'standart.wad';
55 procedure TAddResourceForm.FormActivate(Sender: TObject);
56 var
57 SR: TSearchRec;
59 begin
60 cbWADList.Clear();
61 cbSectionsList.Clear();
62 lbResourcesList.Clear();
64 FResourceName := '';
65 FFullResourceName := '';
66 FResourceSelected := False;
68 if FindFirst(WadsDir + DirectorySeparator + '*.*', faReadOnly, SR) = 0 then
69 repeat
70 cbWADList.Items.Add(SR.Name);
71 until FindNext(SR) <> 0;
72 FindClose(SR);
74 // "standart.wad" в начало списка:
75 if cbWADList.Items.IndexOf(STANDART_WAD) > 0 then
76 begin
77 cbWADList.Items.Delete(cbWADList.Items.IndexOf(STANDART_WAD));
78 cbWADList.Items.Insert(0, STANDART_WAD);
79 end;
81 // WAD карты:
82 if OpenedMap <> '' then
83 cbWADList.Items.Add(MsgWadSpecialMap);
84 end;
86 procedure TAddResourceForm.bOKClick(Sender: TObject);
87 begin
88 if FResourceName = '' then
89 begin
90 Application.MessageBox(PChar(MsgMsgChooseRes),
91 PChar(MsgMsgError), MB_OK + MB_ICONERROR);
92 Exit;
93 end;
95 if @FOKFunction <> nil then
96 begin
97 if FOKFunction() then
98 Close();
99 end
100 else
101 Close();
102 end;
104 procedure TAddResourceForm.cbWADListChange(Sender: TObject);
106 WAD: TWADEditor_1;
107 SectionList: SArray;
108 i: Integer;
109 FileName, fn, sn, rn: String;
111 begin
112 WAD := TWADEditor_1.Create();
114 // Внешний WAD:
115 if cbWADList.Text <> MsgWadSpecialMap then
116 FileName := WadsDir + DirectorySeparator + cbWADList.Text
117 else // WAD карты:
118 begin
119 g_ProcessResourceStr(OpenedMap, fn, sn, rn);
120 FileName := fn;
121 end;
123 // Читаем секции:
124 WAD.ReadFile(FileName);
125 SectionList := WAD.GetSectionList();
126 WAD.Free();
128 cbSectionsList.Clear();
129 lbResourcesList.Clear();
131 if SectionList <> nil then
132 for i := 0 to High(SectionList) do
133 if SectionList[i] <> '' then
134 cbSectionsList.Items.Add(win2utf(SectionList[i]))
135 else
136 cbSectionsList.Items.Add('..');
137 end;
139 procedure TAddResourceForm.cbSectionsListChange(Sender: TObject);
141 ResourceList: SArray;
142 WAD: TWADEditor_1;
143 i: DWORD;
144 FileName, SectionName, fn, sn, rn: String;
146 begin
147 WAD := TWADEditor_1.Create();
149 // Внешний WAD:
150 if cbWADList.Text <> MsgWadSpecialMap then
151 FileName := WadsDir + DirectorySeparator + cbWADList.Text
152 else // WAD карты:
153 begin
154 g_ProcessResourceStr(OpenedMap, fn, sn, rn);
155 FileName := fn;
156 end;
158 // Читаем WAD:
159 WAD.ReadFile(FileName);
161 if cbSectionsList.Text <> '..' then
162 SectionName := cbSectionsList.Text
163 else
164 SectionName := '';
166 // Читаем ресурсы выбранной секции:
167 ResourceList := WAD.GetResourcesList(utf2win(SectionName));
169 WAD.Free();
171 lbResourcesList.Clear();
173 if ResourceList <> nil then
174 for i := 0 to High(ResourceList) do
175 lbResourcesList.Items.Add(win2utf(ResourceList[i]));
176 end;
178 procedure TAddResourceForm.lbResourcesListClick(Sender: TObject);
180 FileName, SectionName, fn: String;
182 begin
183 FResourceSelected := (lbResourcesList.SelCount > 0) or
184 (lbResourcesList.ItemIndex > -1);
186 if not FResourceSelected then
187 begin
188 FResourceName := '';
189 FFullResourceName := '';
190 Exit;
191 end;
193 if cbSectionsList.Text = '..' then
194 SectionName := ''
195 else
196 SectionName := cbSectionsList.Text;
198 if cbWADList.Text[1] <> '<' then
199 FileName := cbWADList.Text
200 else
201 FileName := '';
203 FResourceName := FileName+':'+SectionName+'\'+lbResourcesList.Items[lbResourcesList.ItemIndex];
205 if FileName <> '' then
206 FFullResourceName := WadsDir + DirectorySeparator + FResourceName
207 else
208 begin
209 g_ProcessResourceStr(OpenedMap, @fn, nil, nil);
210 FFullResourceName := fn+FResourceName;
211 end;
212 end;
214 end.