uses crt, global, packer, memory;

const count_res=102;

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}
res_packed   :array[1..count_res] of byte; {packed or not (0,1)}

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;

function IntToStr(i:longint):string;
var S:string[11];
begin
  Str(I,S);
  IntToStr:=S;
end;


const
g_path='' {'c:\bp\demoded\'};
{$I filereg.pas} {procedure for registration all data-files in resources array}

var
m:word;
ftext:text;
pack_size, sum_packsize, sum_origsize:longint;
fw_info:text;
stroka, pack_name, regfile_name:string;
pack:byte;
stroki:array[1..count_res] of string;

begin
SetUpVirtual(scr,  vaddr);
SetUpVirtual(scr2, vaddr2);
SetUpVirtual(scr2, vaddr3);

regfile_name:='filreg_p.pas'; {new file with procedure "reg_resources_files"}

sum_packsize:=0;
sum_origsize:=0;
for m:=1 to res_counter do res_names[m]:='';

{registration file-resources}
reg_resource_files;

Assign(fw_info, regfile_name);
Rewrite(fw_info);
if IOResult<>0 then begin writeln('Error create '+regfile_name); halt; end;

WriteLn(fw_info, '{THIS FILE WAS GENERATED WITH "PACK_BIN.PAS" UTILITE"}');
WriteLn(fw_info, '');
WriteLn(fw_info, 'procedure reg_resource_files;');
WriteLn(fw_info, 'begin');
WriteLn(fw_info, '{--------------------------Registration files-resources--------------------}');
WriteLn(fw_info, 'res_counter:=0;');

WriteLn(fw_info, '{$IFNDEF release_mode_compile}');


WriteLn('Star packing files:');

  for m:=1 to res_counter do
     if res_filenames[m]<>'' then
         begin


         if res_packed[m]=1 then
         begin
             if ReadFile(res_filenames[m], res_length[m], vaddr) <>0 then WriteLn('Error read file: ', res_filenames[m])
             else
               begin
                 pack_size:=CompressRLE(res_length[m], vaddr, vaddr2);
                 Write(res_names[m]+', orig.size=' ,res_length[m],', pack.size=',pack_size);
                 if DecompressRLE(pack_size, vaddr2, vaddr3)>3 then
                        Write(', CRC ok!') else begin WriteLn(', Error!'); Halt; end;

                 pack_name:=ChangeExtension(res_filenames[m], 'pak');
                 if WriteFile(pack_name, pack_size, vaddr2)
                      = 0 then WriteLn('... Write ok!') else WriteLn('...ERROR WRITE!');

                 res_filenames[m]:=pack_name;
                 pack:=1;
               end;
         end
         else begin

             WriteLn(res_names[m]+', orig.size=' ,res_length[m], ' no pack');
             pack_size:=res_length[m];
             pack:=0;
         end;

         sum_packsize:=sum_packsize+pack_size;
         sum_origsize:=sum_origsize+res_length[m];

stroka:= 'res_reg('+ IntToStr(res_numscene[m])+ ', '''
+res_names[m]+''','''+res_filenames[m]+''', '+IntToStr(pack_size)+', '+IntToStr(pack)+');';{}

{for release version, without pathfile string}
stroki[m]:= 'res_reg('+ IntToStr(res_numscene[m])+ ', '''
+res_names[m]+''','' '', '+IntToStr(pack_size)+', '+IntToStr(pack)+');';{}

WriteLn(fw_info, stroka);{}

         end;

WriteLn(fw_info, '{$ELSE}');

  for m:=1 to res_counter do WriteLn(fw_info, stroki[m]);


WriteLn(fw_info, '{$ENDIF}');
WriteLn(fw_info, 'end;');
Close(fw_info);

WriteLn('Summary orig. size: ', sum_origsize);
WriteLn('Summary packed size: ', sum_packsize);
WriteLn('Economy: ',sum_origsize - sum_packsize);
readkey;

end.