DEADSOFTWARE

Now PackMap can read zip
[d2df-editor.git] / src / editor / f_packmap.pas
index f094d3eae8cfb4ad0f0567e9b1c9ac3f090a96ff..ac1d07d3d2f216a815d4483442e9876c5c380f75 100644 (file)
@@ -1,12 +1,12 @@
 unit f_packmap;
 
-{$MODE Delphi}
+{$INCLUDE ../shared/a_modes.inc}
 
 interface
 
 uses
   LCLIntf, LCLType, LMessages, SysUtils, Variants, Classes,
-  Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
+  Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, utils;
 
 type
   TPackMapForm = class (TForm)
@@ -53,7 +53,7 @@ implementation
 
 uses
   BinEditor, WADEDITOR, g_map, MAPREADER, MAPWRITER, MAPSTRUCT,
-  f_main, math, g_language;
+  f_main, math, g_language, g_resources;
 
 {$R *.lfm}
 
@@ -70,66 +70,35 @@ begin
     eWAD.Text := SaveDialog.FileName;
 end;
 
-function ProcessResource(wad_to: TWADEditor_1;
-           section_to, filename, section, resource: String): Boolean;
-var
-  wad2: TWADEditor_1;
-  data: Pointer;
-  reslen: Integer;
-  //s: string;
-
+function ProcessResource(wad_to: TWADEditor_1; section_to, filename, section, resource: String): Boolean;
+  var
+    data: Pointer;
+    reslen: Integer;
 begin
-  Result := False;
   if filename = '' then
     g_ProcessResourceStr(OpenedMap, @filename, nil, nil)
   else
-    filename := EditorDir+'wads\'+filename;
-
-// Читаем ресурс из WAD-файла карты или какого-то другого:
-  wad2 := TWADEditor_1.Create();
-
-  if not wad2.ReadFile(filename) then
-  begin
-    MessageBox(0, PChar(Format(_lc[I_MSG_WAD_ERROR],
-                                   [ExtractFileName(filename)])),
-               PChar(_lc[I_MSG_ERROR]), MB_OK + MB_ICONERROR);
-    wad2.Free();
-    Exit;
-  end;
-
-  if not wad2.GetResource(section, resource, data, reslen) then
-  begin
-    MessageBox(0, PChar(Format(_lc[I_MSG_RES_ERROR],
-                                   [filename, section, resource])),
-               PChar(_lc[I_MSG_ERROR]), MB_OK + MB_ICONERROR);
-    wad2.Free();
-    Exit;
-  end;
-
-  wad2.Free();
+    filename := EditorDir + 'wads/' + filename;
 
- {if wad_to.HaveResource(section_to, resource) then
- begin
-  for a := 2 to 256 do
+  g_ReadResource(filename, section, resource, data, reslen);
+  if data <> nil then
   begin
-   s := IntToStr(a);
-   if not wad_to.HaveResource(section_to, resource+s) then Break;
-  end;
-  resource := resource+s;
- end;}
-
-// Если такого ресурса нет в WAD-файле-назначении, то копируем:
-  if not wad_to.HaveResource(section_to, resource) then
+    (* Write resource only if it does not exists *)
+    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
+  else
   begin
-    if not wad_to.HaveSection(section_to) then
-      wad_to.AddSection(section_to);
-    wad_to.AddResource(data, reslen, resource, section_to);
-  end;
-
-  FreeMem(data);
-
-  Result := True;
+    //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
 end;
 
 procedure TPackMapForm.bPackClick(Sender: TObject);
@@ -178,7 +147,7 @@ begin
   if cbTextrures.Checked and (textures <> nil) then
     for a := 0 to High(textures) do
     begin
-      res := textures[a].Resource;
+      res := win2utf(textures[a].Resource);
       if IsSpecialTexture(res) then
         Continue;
 
@@ -190,7 +159,7 @@ begin
            (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
       begin
       // Копируем ресурс текстуры:
-        if not ProcessResource(WAD, tsection, filename, section, resource) then
+        if not f_packmap.ProcessResource(WAD, tsection, filename, section, resource) then
         begin
           mr.Free();
           WAD.Free();
@@ -198,7 +167,7 @@ begin
         end;
 
       // Переименовываем ресурс текстуры:
-        res := Format(':%s\%s', [tsection, resource]);
+        res := utf2win(Format(':%s\%s', [tsection, resource]));
         ZeroMemory(@textures[a].Resource[0], 64);
         CopyMemory(@textures[a].Resource[0], @res[1], Min(Length(res), 64));
       end;
@@ -210,7 +179,7 @@ begin
 // Нужно копировать небо:
   if cbSky.Checked then
   begin
-    res := header.SkyName;
+    res := win2utf(header.SkyName);
     g_ProcessResourceStr(res, @filename, @section, @resource);
 
   // Не записывать стандартное небо:
@@ -219,7 +188,7 @@ begin
          (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
     begin
     // Копируем ресурс неба:
-      if not ProcessResource(WAD, ssection, filename, section, resource) then
+      if not f_packmap.ProcessResource(WAD, ssection, filename, section, resource) then
       begin
         mr.Free();
         WAD.Free();
@@ -227,7 +196,7 @@ begin
       end;
 
     // Переименовываем ресурс неба:
-      res := Format(':%s\%s', [ssection, resource]);
+      res := utf2win(Format(':%s\%s', [ssection, resource]));
       ZeroMemory(@header.SkyName[0], 64);
       CopyMemory(@header.SkyName[0], @res[1], Min(Length(res), 64));
     end;
@@ -236,7 +205,7 @@ begin
 // Нужно копировать музыку:
   if cbMusic.Checked then
   begin
-    res := header.MusicName;
+    res := win2utf(header.MusicName);
     g_ProcessResourceStr(res, @filename, @section, @resource);
 
   // Не записывать стандартную музыку:
@@ -245,7 +214,7 @@ begin
          (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
     begin
     // Копируем ресурс музыки:
-      if not ProcessResource(WAD, msection, filename, section, resource) then
+      if not f_packmap.ProcessResource(WAD, msection, filename, section, resource) then
       begin
         mr.Free();
         WAD.Free();
@@ -253,7 +222,7 @@ begin
       end;
 
     // Переименовываем ресурс музыки:
-      res := Format(':%s\%s', [msection, resource]);
+      res := utf2win(Format(':%s\%s', [msection, resource]));
       ZeroMemory(@header.MusicName[0], 64);
       CopyMemory(@header.MusicName[0], @res[1], Min(Length(res), 64));
     end;
@@ -306,12 +275,12 @@ begin
                (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
           begin
           // Копируем ресурс дополнительной текстуры:
-            if ProcessResource(WAD, tsection, filename, section, resource) then
+            if f_packmap.ProcessResource(WAD, tsection, filename, section, resource) then
             begin
 
               Нужно проверять есть такая текстура textures и есть ли она вообще?
             // Переименовываем ресурс текстуры:
-              res := Format(':%s\%s', [tsection, resource]);
+              res := utf2win(Format(':%s\%s', [tsection, resource]));
               ZeroMemory(@textures[a].Resource[0], 64);
               CopyMemory(@textures[a].Resource[0], @res[1], Min(Length(res), 64));