DEADSOFTWARE

Revert to old wad read/write method
[d2df-editor.git] / src / editor / f_packmap.pas
index f9bb418620c837651fb936c85030e7d0d13ddd53..911700b7df8a5f18cf6aaee493d5cd6371b4546b 100644 (file)
@@ -53,7 +53,7 @@ implementation
 
 uses
   BinEditor, WADEDITOR, g_map, MAPREADER, MAPWRITER, MAPSTRUCT,
-  f_main, math, g_language, g_resources;
+  f_main, math, g_language, g_options, e_log;
 
 {$R *.lfm}
 
@@ -64,45 +64,72 @@ const
 
 procedure TPackMapForm.bSelectWADClick(Sender: TObject);
 begin
-  SaveDialog.Filter := _lc[I_FILE_FILTER_WAD];
+  SaveDialog.Filter := MsgFileFilterWad;
 
   if SaveDialog.Execute() then
     eWAD.Text := SaveDialog.FileName;
 end;
 
-function ProcessResource(wad_to, section_to, filename, section, resource: String): Boolean;
-  var
-    data: Pointer;
-    res, len: Integer;
+function ProcessResource(wad_to: TWADEditor_1; section_to, filename, section, resource: String): Boolean;
+var
+  wad2: TWADEditor_1;
+  data: Pointer;
+  reslen: Integer;
+  //s: string;
+
 begin
+  Result := False;
+
   if filename = '' then
     g_ProcessResourceStr(OpenedMap, @filename, nil, nil)
   else
-    filename := EditorDir + 'wads/' + filename;
+    filename := WadsDir + DirectorySeparator + filename;
 
-  g_ReadResource(filename, section, resource, data, len);
-  if data <> nil then
+// Читаем ресурс из WAD-файла карты или какого-то другого:
+  wad2 := TWADEditor_1.Create();
+
+  if not wad2.ReadFile(filename) then
   begin
-    (* Write resource only if it does not exists *)
-    g_ExistsResource(wad_to, section_to, resource, res);
-    if res <> 0 then
-    begin
-      g_AddResource(wad_to, section_to, resource, data, len, res);
-      ASSERT(res = 0)
-    end;
-    FreeMem(data);
-    Result := True
-  end
-  else
+    Application.MessageBox(PChar(Format(MsgMsgWadError, [ExtractFileName(filename)])), PChar(MsgMsgError), MB_OK + MB_ICONERROR);
+    wad2.Free();
+    Exit;
+  end;
+
+  if not wad2.GetResource(utf2win(section), utf2win(resource), data, reslen) then
   begin
-    //MessageBox(0, PChar(Format(_lc[I_MSG_WAD_ERROR], [ExtractFileName(filename)])), PChar(_lc[I_MSG_ERROR]), MB_OK + MB_ICONERROR);
-    MessageBox(0, PChar(Format(_lc[I_MSG_RES_ERROR], [filename, section, resource])), PChar(_lc[I_MSG_ERROR]), MB_OK + MB_ICONERROR);
-    Result := False
-  end
+    Application.MessageBox(PChar(Format(MsgMsgResError, [filename, section, resource])), PChar(MsgMsgError), MB_OK + MB_ICONERROR);
+    wad2.Free();
+    Exit;
+  end;
+
+  wad2.Free();
+
+ {if wad_to.HaveResource(utf2win(section_to), utf2win(resource)) then
+ begin
+  for a := 2 to 256 do
+  begin
+   s := IntToStr(a);
+   if not wad_to.HaveResource(utf2win(section_to), utf2win(resource+s)) then Break;
+  end;
+  resource := resource+s;
+ end;}
+
+// Если такого ресурса нет в WAD-файле-назначении, то копируем:
+  if not wad_to.HaveResource(utf2win(section_to), utf2win(resource)) then
+  begin
+    if not wad_to.HaveSection(utf2win(section_to)) then
+      wad_to.AddSection(utf2win(section_to));
+    wad_to.AddResource(data, reslen, utf2win(resource), utf2win(section_to));
+  end;
+
+  FreeMem(data);
+
+  Result := True;
 end;
 
 procedure TPackMapForm.bPackClick(Sender: TObject);
 var
+  WAD: TWADEditor_1;
   mr: TMapReader_1;
   mw: TMapWriter_1;
   data: Pointer;
@@ -127,10 +154,12 @@ begin
   if data = nil then
     Exit;
 
+  WAD := TWADEditor_1.Create();
+
 // Не перезаписывать WAD, а дополнить:
-  if not cbAdd.Checked then
-    if FileExists(eWAD.Text) then
-      ASSERT(RenameFile(eWAD.Text, eWAD.Text + '.bak0'));
+  if cbAdd.Checked then
+    if WAD.ReadFile(eWAD.Text) then
+      WAD.CreateImage();
 
 // Читаем карту из памяти:
   mr := TMapReader_1.Create();
@@ -156,9 +185,10 @@ begin
            (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
       begin
       // Копируем ресурс текстуры:
-        if not f_packmap.ProcessResource(eWAD.Text, tsection, filename, section, resource) then
+        if not f_packmap.ProcessResource(WAD, tsection, filename, section, resource) then
         begin
           mr.Free();
+          WAD.Free();
           Exit;
         end;
 
@@ -184,9 +214,10 @@ begin
          (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
     begin
     // Копируем ресурс неба:
-      if not f_packmap.ProcessResource(eWAD.Text, ssection, filename, section, resource) then
+      if not f_packmap.ProcessResource(WAD, ssection, filename, section, resource) then
       begin
         mr.Free();
+        WAD.Free();
         Exit;
       end;
 
@@ -209,9 +240,10 @@ begin
          (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
     begin
     // Копируем ресурс музыки:
-      if not f_packmap.ProcessResource(eWAD.Text, msection, filename, section, resource) then
+      if not f_packmap.ProcessResource(WAD, msection, filename, section, resource) then
       begin
         mr.Free();
+        WAD.Free();
         Exit;
       end;
 
@@ -269,7 +301,7 @@ begin
                (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
           begin
           // Копируем ресурс дополнительной текстуры:
-            if f_packmap.ProcessResource(eWAD.Text, tsection, filename, section, resource) then
+            if f_packmap.ProcessResource(WAD, tsection, filename, section, resource) then
             begin
 
               Нужно проверять есть такая текстура textures и есть ли она вообще?
@@ -301,18 +333,20 @@ begin
 
 // Сохраняем карту из памяти под новым именем в WAD-файл:
   len := mw.SaveMap(data);
-  g_AddResource(eWAD.Text, '', eResource.Text, data, len, a);
+  WAD.AddResource(data, len, eResource.Text, '');
+  WAD.SaveTo(eWAD.Text);
+
   mw.Free();
   mr.Free();
-  Close();
+  WAD.Free();
 
-  ASSERT(a = 0); (* saved *)
-  MessageDlg(Format(_lc[I_MSG_PACKED], [eResource.Text, ExtractFileName(eWAD.Text)]), mtInformation, [mbOK], 0);
+  MessageDlg(Format(MsgMsgPacked, [eResource.Text, ExtractFileName(eWAD.Text)]), mtInformation, [mbOK], 0);
+  Close();
 end;
 
 procedure TPackMapForm.FormCreate(Sender: TObject);
 begin
-  SaveDialog.InitialDir := EditorDir;
+  SaveDialog.InitialDir := MapsDir;
 end;
 
 end.