DEADSOFTWARE

Main: Fixup encoding inconsistences
[d2df-editor.git] / src / editor / f_packmap.pas
1 unit f_packmap;
3 {$INCLUDE ../shared/a_modes.inc}
5 interface
7 uses
8 LCLIntf, LCLType, LMessages, SysUtils, Variants, Classes,
9 Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, utils;
11 type
12 TPackMapForm = class (TForm)
13 bPack: TButton;
14 SaveDialog: TSaveDialog;
15 Panel1: TPanel;
16 // Сохранить в:
17 LabelSaveTo: TLabel;
18 eWAD: TEdit;
19 bSelectWAD: TButton;
20 // Имя карты:
21 LabelMapName: TLabel;
22 eResource: TEdit;
23 // Текстуры:
24 cbTextrures: TCheckBox;
25 LabelTextures: TLabel;
26 eTSection: TEdit;
27 // Небо:
28 cbSky: TCheckBox;
29 LabelSky: TLabel;
30 eSSection: TEdit;
31 // Музыка:
32 cbMusic: TCheckBox;
33 LabelMusic: TLabel;
34 eMSection: TEdit;
35 // Дополнительно:
36 cbAdd: TCheckBox;
37 cbNonStandart: TCheckBox;
39 procedure bSelectWADClick(Sender: TObject);
40 procedure bPackClick(Sender: TObject);
41 procedure FormCreate(Sender: TObject);
43 private
44 { Private declarations }
45 public
46 { Public declarations }
47 end;
49 var
50 PackMapForm: TPackMapForm;
52 implementation
54 uses
55 BinEditor, WADEDITOR, g_map, MAPREADER, MAPWRITER, MAPSTRUCT,
56 f_main, math, g_language, g_resources, e_log;
58 {$R *.lfm}
60 const
61 STANDART_WAD = 'standart.wad';
62 SHRSHADE_WAD = 'shrshade.wad';
65 procedure TPackMapForm.bSelectWADClick(Sender: TObject);
66 begin
67 SaveDialog.Filter := _lc[I_FILE_FILTER_WAD];
69 if SaveDialog.Execute() then
70 eWAD.Text := SaveDialog.FileName;
71 end;
73 function ProcessResource(wad_to, section_to, filename, section, resource: String): Boolean;
74 var
75 data: Pointer;
76 res, len: Integer;
77 us, un: String;
78 begin
79 Result := True;
80 if filename = '' then
81 g_GetResourceSection(OpenedMap, filename, us, un)
82 else
83 filename := EditorDir + 'wads/' + filename;
84 e_WriteLog('ProcessResource: "' + wad_to + '" "' + section_to + '" "' + filename + '" "' + section + '" "' + resource + '"', MSG_NOTIFY);
86 if resource = '' then Exit;
88 g_ReadResource(filename, section, resource, data, len);
89 if data <> nil then
90 begin
91 (* Write resource only if it does not exists *)
92 g_ExistsResource(wad_to, section_to, resource, res);
93 if res <> 0 then
94 begin
95 g_AddResource(wad_to, section_to, resource, data, len, res);
96 ASSERT(res = 0)
97 end;
98 FreeMem(data);
99 end
100 else
101 begin
102 //MessageBox(0, PChar(Format(_lc[I_MSG_WAD_ERROR], [ExtractFileName(filename)])), PChar(_lc[I_MSG_ERROR]), MB_OK + MB_ICONERROR);
103 MessageBox(0, PChar(Format(_lc[I_MSG_RES_ERROR], [filename, section, resource])), PChar(_lc[I_MSG_ERROR]), MB_OK + MB_ICONERROR);
104 Result := False
105 end
106 end;
108 procedure TPackMapForm.bPackClick(Sender: TObject);
109 var
110 mr: TMapReader_1;
111 mw: TMapWriter_1;
112 data: Pointer;
113 len: LongWord;
114 textures: TTexturesRec1Array;
115 header: TMapHeaderRec_1;
116 a: Integer;
117 res, tsection, ssection, msection, filename, section, resource: String;
119 begin
120 if eWAD.Text = '' then
121 Exit;
122 if eResource.Text = '' then
123 Exit;
125 tsection := eTSection.Text;
126 ssection := eSSection.Text;
127 msection := eMSection.Text;
129 // Сохраняем карту в память:
130 data := SaveMap('');
131 if data = nil then
132 Exit;
134 if not cbAdd.Checked then
135 g_DeleteFile(eWAD.Text, '.bak0');
137 // Читаем карту из памяти:
138 mr := TMapReader_1.Create();
139 mr.LoadMap(data);
140 FreeMem(data);
142 // Получаем текстуры:
143 textures := mr.GetTextures();
145 // Нужно копировать текстуры:
146 if cbTextrures.Checked and (textures <> nil) then
147 for a := 0 to High(textures) do
148 begin
149 res := win2utf(textures[a].Resource);
150 if IsSpecialTexture(res) then
151 Continue;
153 g_GetResourceSection(res, filename, section, resource);
155 // Не записывать стандартные текстуры:
156 if (not cbNonStandart.Checked) or
157 ( (AnsiLowerCase(filename) <> STANDART_WAD) and
158 (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
159 begin
160 // Копируем ресурс текстуры:
161 if not f_packmap.ProcessResource(eWAD.Text, tsection, filename, section, resource) then
162 begin
163 mr.Free();
164 Exit;
165 end;
167 // Переименовываем ресурс текстуры:
168 res := utf2win(Format(':%s\%s', [tsection, resource]));
169 ZeroMemory(@textures[a].Resource[0], 64);
170 CopyMemory(@textures[a].Resource[0], @res[1], Min(Length(res), 64));
171 end;
172 end;
174 // Получаем заголовок карты:
175 header := mr.GetMapHeader();
177 // Нужно копировать небо:
178 if cbSky.Checked then
179 begin
180 res := win2utf(header.SkyName);
181 g_GetResourceSection(res, filename, section, resource);
183 // Не записывать стандартное небо:
184 if (not cbNonStandart.Checked) or
185 ( (AnsiLowerCase(filename) <> STANDART_WAD) and
186 (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
187 begin
188 // Копируем ресурс неба:
189 if not f_packmap.ProcessResource(eWAD.Text, ssection, filename, section, resource) then
190 begin
191 mr.Free();
192 Exit;
193 end;
195 // Переименовываем ресурс неба:
196 res := utf2win(Format(':%s\%s', [ssection, resource]));
197 ZeroMemory(@header.SkyName[0], 64);
198 CopyMemory(@header.SkyName[0], @res[1], Min(Length(res), 64));
199 end;
200 end;
202 // Нужно копировать музыку:
203 if cbMusic.Checked then
204 begin
205 res := win2utf(header.MusicName);
206 g_GetResourceSection(res, filename, section, resource);
208 // Не записывать стандартную музыку:
209 if (not cbNonStandart.Checked) or
210 ( (AnsiLowerCase(filename) <> STANDART_WAD) and
211 (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
212 begin
213 // Копируем ресурс музыки:
214 if not f_packmap.ProcessResource(eWAD.Text, msection, filename, section, resource) then
215 begin
216 mr.Free();
217 Exit;
218 end;
220 // Переименовываем ресурс музыки:
221 res := utf2win(Format(':%s\%s', [msection, resource]));
222 ZeroMemory(@header.MusicName[0], 64);
223 CopyMemory(@header.MusicName[0], @res[1], Min(Length(res), 64));
224 end;
225 end;
228 // Нужно копировать дополнительные текстуры:
229 if cbTextrures.Checked and (textures <> nil) and
230 (gPanels <> nil) and (gTriggers <> nil) then
231 begin
232 for a := 0 to High(gPanels) do
233 begin
234 ok := False;
236 // Ссылаются ли на эту панель триггеры:
237 for b := 0 to High(gTriggers) do
238 if ( (gTriggers[b].TriggerType in [TRIGGER_OPENDOOR,
239 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
240 TRIGGER_CLOSETRAP, TRIGGER_TRAP, TRIGGER_LIFTUP,
241 TRIGGER_LIFTDOWN, TRIGGER_LIFT]) and
242 (gTriggers[b].Data.PanelID = a) ) or
243 (gTriggers[b].TexturePanel = a) then
244 begin
245 ok := True;
246 Break;
247 end;
249 // Есть триггеры на эту панель:
250 if ok and (gPanels[a].TextureName <> '') and
251 (not IsSpecialTexture(gPanels[a].TextureName) and
252 g_Texture_NumNameFindStart(gPanels[a].TextureName) then
253 begin
254 while True do
255 begin
256 r := g_Texture_NumNameFindNext(res);
257 case r of
258 NNF_NAME_FOUND: ;
259 NNF_NAME_EQUALS: Continue;
260 else Break;
261 end;
263 if res = '' then
264 Break;
266 g_GetResourceSection(res, @filename, @section, @resource);
268 // Не записывать стандартные дополнительные текстуры:
269 if (not cbNonStandart.Checked) or
270 ( (AnsiLowerCase(filename) <> STANDART_WAD) and
271 (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
272 begin
273 // Копируем ресурс дополнительной текстуры:
274 if f_packmap.ProcessResource(eWAD.Text, tsection, filename, section, resource) then
275 begin
277 Нужно проверять есть такая текстура textures и есть ли она вообще?
278 // Переименовываем ресурс текстуры:
279 res := utf2win(Format(':%s\%s', [tsection, resource]));
280 ZeroMemory(@textures[a].Resource[0], 64);
281 CopyMemory(@textures[a].Resource[0], @res[1], Min(Length(res), 64));
285 end;
286 end;
287 end; // while True
288 end;
289 end;
290 end;
293 // Записываем изменения карты:
294 mw := TMapWriter_1.Create();
296 mw.AddHeader(header);
297 mw.AddTextures(textures);
298 mw.AddPanels(mr.GetPanels());
299 mw.AddItems(mr.GetItems());
300 mw.AddAreas(mr.GetAreas());
301 mw.AddMonsters(mr.GetMonsters());
302 mw.AddTriggers(mr.GetTriggers());
304 // Сохраняем карту из памяти под новым именем в WAD-файл:
305 len := mw.SaveMap(data);
306 g_AddResource(eWAD.Text, '', eResource.Text, data, len, a);
307 mw.Free();
308 mr.Free();
309 Close();
311 ASSERT(a = 0); (* saved *)
312 MessageDlg(Format(_lc[I_MSG_PACKED], [eResource.Text, ExtractFileName(eWAD.Text)]), mtInformation, [mbOK], 0);
313 end;
315 procedure TPackMapForm.FormCreate(Sender: TObject);
316 begin
317 SaveDialog.InitialDir := EditorDir;
318 end;
320 end.