?? buildrsccfile.pm
字號:
my $line;
my $linenum = 0;
my @file_state;
my $bytesnum;
my $file_attr;
my $file_path;
my $line_iid; #file .iid's line
my $palette_file;
my $PalSaveName;
my $image_table;
my @data_array;
my $data_index;
my $total_data_num;
my @image_ids;
my @image_height;
my @image_width;
my $image_color_num;
my $image_bpp;
my $char;
my @proglist;
# this part is used to create sub dir's head info, now we remove this sub dir head.
# if (-e "$ImgBinFile")
# {
# $first_create = 0;
# }
# else #not exist
# {
# $first_create = 1;
# }
open (H_IMG_BIN,">>$ImgBinFile") || die ("Error: Can not open $ImgBinFile !");#record all image data
binmode(H_IMG_BIN);
open (H_IMG_DIR,">>$ImgDirFile") || die ("Error: Can not open $ImgDirFile !");#record every image's file info into this dir file
binmode(H_IMG_DIR);
# this part is used to create sub dir's head info, now we remove this sub dir head.
# if ($first_create == 1) #keep room for CFTH (8bytes) in $ImgDirFile
# {
# syswrite H_IMG_DIR, pack("C",0x10); #for version 1.0
# syswrite H_IMG_DIR, pack("C",0xC0); #0x80|0x40 for FT_STRUC_COMPACT|FT_ADDR_RELATIVE
# syswrite H_IMG_DIR, pack("$ui16",0xFFFF); #0xFF for no sure item number.
# syswrite H_IMG_DIR, pack("$ui32",0);
# }
#
open (H_IMG_C,"$ImgCFile") || die ("Error: Can not open file $ImgCFile\n");
# print " $ImgCFile has been opened !\n";
#### init some vars
while ($ImgCFile =~ /([a-zA-Z0-9_\.]*\\)/g)
{
$file_path .= $1;
}
# # print " file path is $file_path\n";
$temp = $ImgCFile;
$temp =~ s/[a-zA-Z0-9_\.]*\\//g; #remove the path part, letf filename only
$temp =~ tr/A-Z/a-z/;
$palette_file = $image_table = $temp;
$palette_file =~ s/\.img/\.plt/;
$image_table =~ s/\.img/_table/; #remove ext name and add _table
####
@file_state = stat ("$ImgBinFile");
$bytesnum = $file_state[7]; # length (bytes) of this file
@file_state = stat ("$ImgDirFile");
$total_data_num = ($file_state[7]-8)/8;
$linenum = 0;
@data_array = ();
$data_index = 0;
$scan_step = $STEP_OPEN_IID;
@image_ids = ();
@image_height = ();
@image_width = ();
#### deal with data
foreach $line (<H_IMG_C>)
{
$linenum ++;
$line =~ s/(\/\/.*)//; #remove lines marked by //
if ($scan_step == $STEP_OPEN_IID)
{
if ($line =~ /\#include\s+\"([a-zA-Z][a-zA-Z0-9_]*\.iid)\"/)
{
open (H_IMG_IID,$file_path."$1") || die ("Error: Can not open file $1 !");
# print " find file $1 at line $linenum, open it !\n";
$scan_step = $STEP_FIND_BPP;
}
}
elsif ($scan_step == $STEP_FIND_BPP) #find im_*_table
{
if ($line =~ /_PALETTE_COLOR\s+(\d+)/)
{
$image_color_num = $1;
if ($1 == 256)
{
$image_bpp = 8;
}
elsif ($1 == 16)
{
$image_bpp = 4;
}
elsif ($1 == 4)
{
$image_bpp = 2;
}
else
{
die ("Error: wrong color number !!! at line $linenum\n");
}
# print " find palette color is $1 at line $linenum\n";
$scan_step = $STEP_FIND_TABLE;
}
}
elsif ($scan_step == $STEP_FIND_TABLE) #find im_*_table
{
if ($line =~ /$image_table\[\]=/)
{
# print " find table $image_table\[\] at line $linenum\n";
$scan_step = $STEP_RECORD_TABLE;
}
}
elsif ($scan_step == $STEP_RECORD_TABLE) #record all im_*_data name in im_*_table
{
if ($line =~ /(IM_[0-9A-Z_]*),([0-9]+),([0-9]+),(im_[0-9a-z_]*data)/)
{
@image_ids = (@image_ids, $1);
@image_width = (@image_width, $2);
@image_height = (@image_height, $3);
@data_array = (@data_array, $4);
# # print "$1, $2, $3, $4\n";
}
if ($line =~ /};/)
{
$scan_step = $STEP_FIND_PALETTE;
}
}
elsif ($scan_step == $STEP_FIND_PALETTE)
{
if ($line =~ /const\s+LOGPALETTE2\s+[0-9a-z_]+\s+=/)
{
# print " PALETTE find at line $linenum\n";
open(H_PALETTE, ">$palette_file") || die ("Error: Can not open $palette_file !");
binmode (H_PALETTE);
syswrite H_PALETTE, pack("$ui16",0);#reserved
syswrite H_PALETTE, pack("$ui16",$image_color_num);
$scan_step = $STEP_RECORD_PALETTE;
}
}
elsif ($scan_step == $STEP_RECORD_PALETTE)
{
$line =~ s/(\s*)//g; #remove all space for easy patten
if ($line =~ /\{(\d+),(\d+),(\d+),(\d+|[A-Z_]+)\}/)
{
syswrite H_PALETTE, pack("C",$1);
syswrite H_PALETTE, pack("C",$2);
syswrite H_PALETTE, pack("C",$3);
if ($4 =~ /CRF_NULL/) #defined in guigdi.h
{
$temp = 0x04;
}
elsif ($4 =~ /CRF_TRANS/)
{
$temp = 0x02;
}
elsif ($4 =~ /CRF_MIX/)
{
$temp = 0x01;
}
else
{
$temp = $4;
}
syswrite H_PALETTE, pack("C",$temp);
$image_color_num --;
}
if ($image_color_num <= 0)
{
# print " PALETTE Record over at line $linenum\n";
close ($palette_file);
$scan_step = $STEP_FIND_DATA;
}
}
elsif ($scan_step == $STEP_FIND_DATA) #find im_*_data
{
if ($line =~ /$data_array[$data_index]\[\]=/)
{
# print " >Build $data_array[$data_index]\[\] at line $linenum, ";
$scan_step = $STEP_HANDLE_DATA;
open (H_IMG_TEMP,">image.tmp") || die ("Error: Can not open image.tmp !");#record one image data temporarily
binmode(H_IMG_TEMP);
syswrite H_IMG_TEMP, pack("$ui16",$image_width[$data_index]);
syswrite H_IMG_TEMP, pack("$ui16",$image_height[$data_index]);
syswrite H_IMG_TEMP, pack("C",$image_bpp);
syswrite H_IMG_TEMP, pack("C",0); #reserved;
}
}
elsif ($scan_step == $STEP_HANDLE_DATA) #deal with data in im_*_data
{
while ($line =~ /(0x..)/g)
{
syswrite H_IMG_TEMP, pack("C",hex($1));
}
if ($line =~ /}/) #finish a im_*_data
{
close (H_IMG_TEMP);
if ($is_zip != 0)
{
@proglist = ("7zip","e","image.tmp", "image.tmp.7z", "-lc0", "-lp2");
system(@proglist);
open (H_IMG_ZIP,"image.tmp.7z") || die ("Error: Can not open image.tmp.7z !");
binmode(H_IMG_ZIP);
while(sysread(H_IMG_ZIP,$char,1,0))
{
$bytesnum ++;
syswrite(H_IMG_BIN,$char,1,0);
}
close (H_IMG_ZIP);
@proglist = ("rm","-f","image.tmp.7z");
system(@proglist);
$file_attr = 0x41; # 0x41 is for zipped image ; refer "rfs_stru.h"
}
else
{
open (H_IMG_TEMP,"image.tmp") || die ("Error: Can not open image.tmp!");
binmode(H_IMG_TEMP);
while(sysread(H_IMG_TEMP,$char,1,0))
{
$bytesnum ++;
syswrite(H_IMG_BIN,$char,1,0);
}
# for WORD boundary
if($bytesnum%2)
{
# print "for image's WORD boundary, add a null byte \n";
$bytesnum ++;
syswrite(H_IMG_BIN,pack("C",0),1,0);
}
close (H_IMG_TEMP);
@proglist = ("rm","-f","image.tmp");
system(@proglist);
$file_attr = 0x01; # 0x01 is for not-zipped image ; refer "rfs_stru.h"
}
## file info part 1: ID (4 bytes)
seek(H_IMG_IID,0,0);
foreach $line_iid (<H_IMG_IID>)
{
if ($line_iid =~ /$image_ids[$data_index]\s+0x(....)/)
{
syswrite(H_IMG_DIR, $1,4,0);
$PalSaveName = substr($1,0,2);
last;
}
}
## file info part 2: file attr (1 byte)
syswrite H_IMG_DIR, pack("C",$file_attr);
## file info part 3: file end address (3 bytes)
if ($output_endian == $BIG_ENDIAN)
{
syswrite H_IMG_DIR, pack("C",int($bytesnum/2**16));#
syswrite H_IMG_DIR, pack("$ui16",int($bytesnum%2**16));#
}
else #$LITTLE_ENDIAN
{
syswrite H_IMG_DIR, pack("$ui16",int($bytesnum%2**16));#
syswrite H_IMG_DIR, pack("C",int($bytesnum/2**16));#
}
# print "end at byte $bytesnum\n";
$data_index ++;
$total_data_num ++;
if ($data_index == @data_array) #finish all im_*_data in this im_*.img
{
close(H_IMG_IID);
#record this palette file's info into dir file $PalDirFile
@proglist = ("mv","$palette_file","$PalSaveName".".plt");
system(@proglist);
RecordAFileIntoDir($PalDirFile,"$PalSaveName".".plt",$PAL_FILE_ATTR,0xFFFFFFFF);
last;# ALL OVER, jump out of foreach of this im_*.img
}
else
{# go on to next im_*_data in this im_*.img
$scan_step = $STEP_FIND_DATA;
}
}
}
}
####
close (H_IMG_C);
# print " total data num is $total_data_num\n";
# print " $ImgCFile has been closed !\n\n";
close(H_IMG_DIR);
close(H_IMG_BIN);
}
sub CopyAndSave
{
#### step 1 : get arguments
my ( $DatFile,$DatSaveName , $DirFile, $is_zip) = @_;
if (length($DatSaveName)>6)#save name should be within 6 chars.
{
$DatSaveName = substr($DatSaveName,0,6);
}
my $DatFileReady= "$DatSaveName".".bin";
if ($DatFile !~ /^$DatFileReady$/i)
{
@proglist = ("cp","$DatFile","$DatFileReady");
system(@proglist);
}
#### step 2: build file info in dir file
my $file_attr;
if ($is_zip != 0)
{
$file_attr = 0x47; # 0 1 xx 0111 for (file|zip|Directly-saved)
@proglist = ("7zip","e","$DatFileReady", "$DatFileReady".".7z", "-lc0", "-lp2");
system(@proglist);
@proglist = ("mv","$DatFileReady".".7z","$DatFileReady");
system(@proglist);
}
else
{
$file_attr = 0x07; # 0 0 xx 0111 for (file|not-zip|Directly-saved)
}
RecordAFileIntoDir($DirFile,$DatFileReady,$file_attr,0xFFFFFFFF);
}
sub BuildDataFile
{
#### step 1 : get arguments
my ( $DataFile, $SaveName , $DirFile, $is_zip ,$data_width) = @_;
my $pack_argu;
if ($data_width == 8)
{
$pack_argu = "C";
}
elsif ($data_width == 16)
{
$pack_argu = $ui16;
}
elsif ($data_width == 32)
{
$pack_argu = $ui32;
}
else
{
die "Error: argument data width must be 8,16 or 32 in function BuildDataFile\n";
}
if (length($SaveName)>6)#save name should be within 6 chars.
{
$SaveName = substr($SaveName,0,6);
}
my $DataBinFile= "$SaveName".".dbn";
#### step 2: build inc to lbn
open (H_DATA_C,"$DataFile")|| die ("Error: Can not open Source File $DataFile !");
open (H_DATA_BIN,">$DataBinFile")|| die ("Error: Can not open Dir File $DataBinFile !");
binmode (H_DATA_BIN);
foreach $line (<H_DATA_C>)
{
$line =~ s/(\/\/.*)//; #remove lines marked by //
$line =~ s/(\s*)//g; #remove all space for easy patten
while ($line =~ /(0x[0-9a-fA-F]+)|(\d+)/g)
{
if ($1 ne "")
{
syswrite H_DATA_BIN, pack("$pack_argu",hex($1));
}
elsif ($2 ne "")
{
syswrite H_DATA_BIN, pack("$pack_argu",$2);
}
}
}
close (H_DATA_C);
close (H_DATA_BIN);
#### step 3: build file info in dir file
my $file_attr;
if ($is_zip != 0)
{
$file_attr = 0x45; # 0 1 xx 0110 for (file|zip|data bin)
@proglist = ("7zip","e","$DataBinFile", "$DataBinFile".".7z", "-lc0", "-lp2");
system(@proglist);
@proglist = ("mv","$DataBinFile".".7z","$DataBinFile");
system(@proglist);
}
else
{
$file_attr = 0x05; # 0 0 xx 0110 for (file|not-zip|data bin)
}
RecordAFileIntoDir($DirFile,$DataBinFile,$file_attr,0xFFFFFFFF);
}
sub BuildInc
{
#### step 1 : get arguments
my ( $IncFile,$IncSaveName , $DirFile, $is_zip) = @_;
if (length($IncSaveName)>6)#save name should be within 6 chars.
{
$IncSaveName = substr($IncSaveName,0,6);
}
my $IncBinFile= "$IncSaveName".".lbn";
#### step 2: build inc to lbn
open (H_INC_C,"$IncFile") || die ("Error: Can not open Source File $IncFile !");
open (H_INC_BIN,">$IncBinFile") || die ("Error: Can not open Dir File $IncBinFile !");
binmode (H_INC_BIN);
if($IncSaveName eq "tyrs")
{
foreach $line (<H_INC_C>)
{
$line =~ s/(\/\/.*)//; #remove lines marked by //
$line =~ s/(\s*)//g; #remove all space for easy patten
if ( $line =~ /^(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..)[,]$/)
{
syswrite H_INC_BIN, pack("CCCCCCCCCCCC",hex($1),hex($2),hex($3),hex($4),hex($5),hex($6),hex($7),hex($8),hex($9),hex($10),hex($11),hex($12));
}
}
}
else
{
if($IncSaveName eq "vdec")
{
foreach $line (<H_INC_C>)
{
$line =~ s/(\/\/.*)//; #remove lines marked by //
$line =~ s/(\s*)//g; #remove all space for easy patten
if ( $line =~ /^(0x..),(0x..),(0x..)[,]$/)
{
syswrite H_INC_BIN, pack("CCC",hex($1),hex($2),hex($3));
}
}
}
else
{
foreach $line (<H_INC_C>)
{
$line =~ s/(\/\/.*)//; #remove lines marked by //
$line =~ s/(\s*)//g; #remove all space for easy patten
if ( $line =~ /^(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..),(0x..)[,]$/)
{
syswrite H_INC_BIN, pack("CCCCCCCCCCCCCCCC",hex($1),hex($2),hex($3),hex($4),hex($5),hex($6),hex($7),hex($8),hex($9),hex($10),hex($11),hex($12),hex($13),hex($14),hex($15),hex($16));
}
}
}
}
close (H_INC_C);
close (H_INC_BIN);
#### step 3: build file info in dir file
my $file_attr;
if ($is_zip != 0)
{
$file_attr = 0x46; # 0 1 xx 0110 for (file|zip|Logo bin)
@proglist = ("7zip","e","$IncBinFile", "$IncBinFile".".7z", "-lc0", "-lp2");
system(@proglist);
@proglist = ("mv","$IncBinFile".".7z","$IncBinFile");
system(@proglist);
}
else
{
$file_attr = 0x06; # 0 0 xx 0110 for (file|not-zip|Logo bin)
}
RecordAFileIntoDir($DirFile,$IncBinFile,$file_attr,0xFFFFFFFF);
}
sub BuildDir
{
#### step 1 : get arguments
my ( $ChildDirFile, $FatherDirFile) = @_;
#### step 2: build file info in dir file
my $file_attr = 0x80; # 1 0 xx 0000 for (dir|not-compact|XX)
RecordAFileIntoDir($FatherDirFile,$ChildDirFile,$file_attr,0xFFFFFFFF);
}
sub BuildCompactDir
{
#### step 1 : get arguments
my ( $ChildDirFile, $FatherDirFile) = @_;
#### step 2: build file info in dir file
my $file_attr = 0xC0; # 1 1 xx 0000 for (dir|compact|XX)
RecordAFileIntoDir($FatherDirFile,$ChildDirFile,$file_attr,0xFFFFFFFF);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -