{Data-file linker}
{WARNING! THIS PROGRAM USES "filreg_p.pas" generated by "PACK_BIN.PAS"}

const
count_res=100;

var
res_counter:word;
res_names    :array[1..count_res] of string[16];
res_filenames:array[1..count_res] of string[64];
res_length   :array[1..count_res] of longint;
res_handler  :array[1..count_res] of word;
res_offset   :array[1..count_res] of longint; {offset in XMS handle-block}
res_numscene :array[1..count_res] of byte;
res_df_offset:array[1..count_res] of longint; {offset in data-file}

Procedure ErrorLoading(message:string; code:integer);
begin
 WriteLn(message, code);
 readln;
 Halt;
end;

procedure res_reg(num_scene:byte; name, filename:string; length:longint; pak_flag:byte);
begin
inc(res_counter);
if (res_counter>count_res) then ErrorLoading('Count of resource overflow:', res_counter);
res_numscene[res_counter]:=num_scene;
res_names[res_counter]:=name;
res_filenames[res_counter]:=filename;
res_length[res_counter]:=length;
res_handler[res_counter]:=0;
res_offset[res_counter]:=0;
{res_packed[res_counter]:=pak_flag;}
end;


procedure MakeDataFile(firstfile_non_indexed, filename, filename_info:string);
var
  fr,fw:file;
  fw_info:text;
  l, offset, n:longint;
  m, num_of_fileresource:word;
  b, firstfile_write_ok:byte;

begin

  firstfile_write_ok:=0;
  if firstfile_non_indexed<>'' then {Если указано, то записываем сначала}
    begin                           {какой-то файл (например S3M), }
                                    {который не будет прописан в индексе}

        Assign(fr, firstfile_non_indexed);
        Reset(fr, 1);
        if IOResult<>0 then ErrorLoading('File read error (for first file): '+filename+', Code:', IOResult);

        Assign(fw, filename);
        Rewrite(fw,1);
        Reset(fw, 1);
        if IOResult<>0 then ErrorLoading('File create error (with first file): '+filename+', Code:', IOResult);

        n:=0;
        for l:=1 to filesize(fr) do
           begin
               BlockRead(fr, b, 1);
               if IOResult<>0 then ErrorLoading('Read first file error: '+firstfile_non_indexed+', Code:', IOResult);
               BlockWrite(fw, b, 1);
               if IOResult<>0 then ErrorLoading('Write first file error: '+firstfile_non_indexed+', Code:', IOResult);
               inc(n);
            end;

         Close(fr); if IOResult<>0 then ErrorLoading('File close error: '+firstfile_non_indexed+', Code:', IOResult);
         firstfile_write_ok:=1;
         offset:=n;

       WriteLn('0. '+firstfile_non_indexed+' (',n,' byte), Offset: ',0 ,' ... ');

    end;






  num_of_fileresource:=0;
  for m:=1 to res_counter do
     if res_filenames[m]<>'gen' then inc(num_of_fileresource);

  if num_of_fileresource=0 then ErrorLoading('Empty resources array?', 0);


  if (firstfile_write_ok=0) then
  begin
     Assign(fw, filename);
     Rewrite(fw,1);
     Reset(fw, 1);
     if IOResult<>0 then ErrorLoading('File create error: '+filename+', Code:', IOResult);
     offset:=0;
  end;

  Assign(fw_info, filename_info);
  Rewrite(fw_info);
  if IOResult<>0 then ErrorLoading('Info file create error: '+filename_info+', Code:', IOResult);

  Writeln(fw_info, 'type TResource_info = Record');
  Writeln(fw_info, '          name: string[16];');
  Writeln(fw_info, '          offset: longint;');
  Writeln(fw_info, '          end;');
  Writeln(fw_info, 'const resource_info:array[1..', num_of_fileresource, '] of TResource_info =(');

  WriteLn('Data-file "'+filename+'" linking:');


  for m:=1 to res_counter do
     if res_filenames[m]<>'gen' then
         begin
             Assign(fr, res_filenames[m]);
             Reset(fr, 1);
             if IOResult<>0 then ErrorLoading('File open error: '+res_filenames[m]+', Code:', IOResult);

             Write(m,'. '+res_names[m]+' (',res_length[m],' byte), Offset: ',Offset ,' ... ');

             if m>1 then WriteLn(fw_info, ',') else WriteLn(fw_info);

             n:=0;
             for l:=1 to res_length[m] do
                begin
                   BlockRead(fr, b, 1);
                   if IOResult<>0 then ErrorLoading('Read file error: '+res_filenames[m]+', Code:', IOResult);
                   BlockWrite(fw, b, 1);
                   if IOResult<>0 then ErrorLoading('Write datafile error: '+res_names[m]+', Code:', IOResult);
                   inc(n);
                end;
              Close(fr); if IOResult<>0 then ErrorLoading('File close error: '+res_filenames[m]+', Code:', IOResult);
              WriteLn(n, ' byte write OK');

              Write(fw_info, '(name: ''' +res_names[m]+  '''; offset:', Offset, ')');

              Offset:=Offset+res_length[m];
         end;
  WriteLn(fw_info, ');');
  WriteLn(fw_info, 'num_of_fileresource=',num_of_fileresource, ';');
  Close(fw);
  Close(fw_info);
end;



{$I filreg_p.pas} {procedure for registration all data-files in resources array}

begin

{registration file-resources}
reg_resource_files;

WriteLn;
WriteLn('Start datafile creation:');
MakeDataFile('demoded.s3m', 'demoded.dat', 'demoded.res');{}



end.