- Add ROM renumbering support,

- Add OK, KO Missing ROM listing,
- Add dsrom.lst dump support.
- Add IN/OUT repository support.
This commit is contained in:
agibert 2009-03-22 01:18:12 +00:00
parent 48e5ab5c19
commit e4f08e2dcc

713
nds2rm
View File

@ -5,9 +5,9 @@
# (C) 2009 Arnaud G. Gibert
#-----------------------------------------------------------------------------------------------------------------------------------
# $RCSfile: nds2rm,v $
# $Revision: 1.1 $
# $Revision: 1.2 $
# $Name: $
# $Date: 2009/03/21 18:55:50 $
# $Date: 2009/03/22 01:18:12 $
# $Author: agibert $
#-----------------------------------------------------------------------------------------------------------------------------------
@ -31,11 +31,15 @@
#NDS_REPOSITORY=/opt/public/nds
NDS_REPOSITORY=/opt/public/nds.new
NDS_ROMS=${NDS_REPOSITORY}/roms2
NDS_DAT=${NDS_REPOSITORY}/misc/dsrom.dat
NDS_IDX=${NDS_REPOSITORY}/roms/dsrom.idx
NDS_REPOSITORY_IN=/opt/public/nds
#NDS_REPOSITORY_IN=/opt/public/nds.new
#NDS_REPOSITORY_IN=/opt/public/nds.new/nds.old
#NDS_REPOSITORY_IN=/opt/public/nds.new/nds.new
NDS_REPOSITORY_OUT=/opt/public/nds.new
#NDS_REPOSITORY_OUT=/opt/public/nds.new/nds.new
NDS_ROMS_IN=${NDS_REPOSITORY_IN}/roms
NDS_ROMS_OUT=${NDS_REPOSITORY_OUT}/roms
NDS_DAT=${NDS_REPOSITORY_IN}/misc/dsrom.dat
NDS_VERSION="$Name: $"
@ -75,8 +79,12 @@ function args_parse()
mode="test"
rebuild="no"
renum="no"
lok="no"
lko="no"
lmiss="no"
tmp_args=$(getopt -o CThVr --long correct,test,help,version,rebuild -n 'nds_rom_mng' -- "$@")
tmp_args=$(getopt -o CTDhVrn --long correct,test,dsrom,help,version,rebuild,renum,list_ok,list_ko,list_miss -n 'nds_rom_mng' -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
@ -87,9 +95,14 @@ function args_parse()
case "$1" in
-C|--correct) mode="correct"; shift;;
-T|--test) mode="test"; shift;;
-D|--dsrom) mode="dsrom"; shift;;
-h|--help) mode="exit"; help_print; shift;;
-V|--version) mode="exit"; version_print; shift;;
-r|--rebuild) rebuild="yes"; shift;;
-n|--renum) renum="yes"; shift;;
--list_ok) lok="yes"; shift;;
--list_ko) lko="yes"; shift;;
--list_miss) lmiss="yes"; shift;;
--) shift; break;;
*) echo "Internal error [$1] !"; exit 1;;
esac
@ -100,7 +113,157 @@ function args_parse()
# Rename Archive
# Lookup CRC
#-----------------------------------------------------------------------------------------------------------------------------------
function crc_lookup()
{
crc=${1}
i=1
while [[ ( ${i} -le ${loaded_cnt} ) && ( ${dat_crc[${i}]} != ${crc} ) ]]
do
i=$(( ${i} + 1 ))
done
if [[ ${i} -le ${loaded_cnt} ]]
then
# eval ${idx_ptr}=${i}
echo ${i}
else
# eval ${idx_ptr}=""
echo ""
fi
}
# Dump Rom List
#-----------------------------------------------------------------------------------------------------------------------------------
function rom_list_dump()
{
status=${1}
i=1
while [[ ${i} -le ${loaded_cnt} ]]
do
if [[ ${dat_status[${i}]} == "${status}" ]]
then
echo "${dat_id[${i}]} - ${dat_title[${i}]} (${dat_region[${i}]}) [${dat_lang[${i}]}] {${dat_crc[${i}]}} <${dat_fp[${i}]}>"
fi
i=$(( ${i} + 1 ))
done
}
# Get Save Type Mask
#-----------------------------------------------------------------------------------------------------------------------------------
function mask_get()
{
save_type=${1}
case "${save_type}"
in
("None") echo "000000000";;
("Eeprom - 4 kbit") echo "100000000";;
("Eeprom - 64 kbit") echo "200000000";;
("Eeprom - 512 kbit") echo "500000000";;
("Flash - 2 Mbit") echo "300000000";;
("Flash - 4 Mbit") echo "400000000";;
("Flash - 64 Mbit") echo "600000000";;
("TBC") echo "F00000000";;
esac
}
# Dump DSROM List
#-----------------------------------------------------------------------------------------------------------------------------------
function dsrom_dump()
{
i=1
while [[ ${i} -le ${loaded_cnt} ]]
do
if [[ ${dat_status[${i}]} != "?" ]]
then
mask=$(mask_get "${dat_save_type[${i}]}")
echo "${dat_id[${i}]} ${dat_fp[${i}]}-0 $mask ${dat_title[${i}]} (${dat_region[${i}]}) [${dat_lang[${i}]}] {${dat_crc[${i}]}}"
fi
i=$(( ${i} + 1 ))
done
}
# Dump OK ROMs
#-----------------------------------------------------------------------------------------------------------------------------------
function ok_dump()
{
echo "OK ROMs List:"
rom_list_dump "OK"
echo
}
# Dump KO Roms
#-----------------------------------------------------------------------------------------------------------------------------------
function ko_dump()
{
echo "KO ROMs List:"
rom_list_dump "KO"
echo
}
# Dump Missing ROMs
#-----------------------------------------------------------------------------------------------------------------------------------
function missing_dump()
{
echo "Missing ROMs List:"
rom_list_dump "?"
echo
}
# Update Statistics
#-----------------------------------------------------------------------------------------------------------------------------------
function stats_update()
@ -142,74 +305,393 @@ function stats_update()
exit 1
;;
esac
ok_cnt=$((${ss_ok_cnt} + ${cs_ok_cnt} + ${ks_ok_cnt}))
ko_cnt=$((${ss_ko_cnt} + ${cs_ko_cnt} + ${ks_ko_cnt}))
found_cnt=$((${ok_cnt} + ${ko_cnt}))
missing_cnt=$((${missing_cnt} + ${idx} - ${idx_old} - 1))
idx_old=${idx}
}
# Do Processing Action
# Build Arc
#-----------------------------------------------------------------------------------------------------------------------------------
function do_action()
function arc_build()
{
id=${1}
title=${2}
reg=${3}
lang=${4}
crc=${5}
status=${6}
file=${7}
action=${8}
mode=${9}
rebuild=${10}
fp=${6}
status=${7}
rom=${8}
file=${9}
mode=${10}
if [[ "${mode}" == "correct" ]]
then
if [[ ( "${file}" != *.zip ) || ( ${rebuild} == "yes" ) ]]
then
rom="${id} - ${title} (${reg}) [${lang}].nds"
if [[ ${in_place} == "yes" ]]
then
\rm -f "${NDS_ROMS_IN}/${file}"
fi
case "${file}"
in
(*.nds)
mv "${NDS_ROMS}/${file}" "${rom}"
;;
(*.rar)
unrar p "${NDS_ROMS}/${file}" > "${rom}"
\rm -f "${NDS_ROMS}/${file}"
;;
(*.zip)
unzip -p "${NDS_ROMS}/${file}" > "${rom}"
\rm -f "${NDS_ROMS}/${file}"
;;
(*)
echo "Internal error [$1] !";
exit 1
;;
esac
file="${id}.zip"
zip -m9 "${NDS_ROMS}/${id} - ${title} (${reg}) [${lang}] {${crc}} ${status}.zip" "${rom}" >/dev/null 2>&1 &
else
if [[ ${action} != "keep_status" ]]
then
mv "${NDS_ROMS}/${file}" "${NDS_ROMS}/${id} - ${title} (${reg}) [${lang}] {${crc}} ${status}.zip"
fi
fi
zip -m9 "${NDS_ROMS_OUT}/${id} - ${title} (${reg}) [${lang}] {${crc}} <${fp}> ${status}.zip" "${rom}" >/dev/null 2>&1 &
else
\rm -f "${rom}"
fi
stats_update "${action}" "${mode}"
}
# Rename Arc
#-----------------------------------------------------------------------------------------------------------------------------------
function arc_rename()
{
id=${1}
title=${2}
reg=${3}
lang=${4}
crc=${5}
fp=${6}
status=${7}
file=${8}
action=${9}
mode=${10}
if [[ "${mode}" == "correct" ]]
then
if [[ ${action} != "keep_status" ]]
then
if [[ ${in_place} == "yes" ]]
then
mv "${NDS_ROMS_IN}/${file}" "${NDS_ROMS_OUT}/${id} - ${title} (${reg}) [${lang}] {${crc}} <${fp}> ${status}.zip"
else
cp "${NDS_ROMS_IN}/${file}" "${NDS_ROMS_OUT}/${id} - ${title} (${reg}) [${lang}] {${crc}} <${fp}> ${status}.zip"
fi
fi
fi
}
# Extract ROM
#-----------------------------------------------------------------------------------------------------------------------------------
function rom_extract()
{
rom=$1
file=$2
case "${file}"
in
(*.nds)
cp "${NDS_ROMS_IN}/${file}" "${rom}"
;;
(*.rar)
unrar p "${NDS_ROMS_IN}/${file}" > "${rom}"
;;
(*.zip)
unzip -p "${NDS_ROMS_IN}/${file}" > "${rom}"
;;
(*)
echo "Internal error [$1] !";
exit 1
;;
esac
}
# DSROM Mode
#-----------------------------------------------------------------------------------------------------------------------------------
function dsrom_mode()
{
echo "scanning rom repository..."
IFS=' '
tmp_file=$(mktemp)
ls ${NDS_ROMS_IN} > ${tmp_file}
while read file
do
set ${file:0:4}
id=$1
idx=${id/*(0)/}
# Get info from dat
title=${dat_title[${idx}]}
reg=${dat_region[${idx}]}
lang=${dat_lang[${idx}]}
crc=${dat_crc[${idx}]}
regex=".... - .* \(.*\) \[.*\] \{.*\} <.*> ..\.zip$"
# Type of archive ?
if [[ ${file} =~ $regex ]]
then
fp=${file/*\} \</}; fp=${fp/\> */}
status=${file/* /}; status=${status/.zip/}
dat_fp[${idx}]=${fp}
dat_status[${idx}]=${status}
fi
done < ${tmp_file}
\rm -f ${tmp_file}
dsrom_dump
}
# Test and Correct Mode
#-----------------------------------------------------------------------------------------------------------------------------------
function test_correct_mode()
{
echo "scanning rom repository..."
IFS=' '
tmp_file=$(mktemp)
idx_old=0
ls ${NDS_ROMS_IN} > ${tmp_file}
while read file
do
set ${file:0:4}
id=$1
idx=${id/*(0)/}
# Get info from dat
title=${dat_title[${idx}]}
reg=${dat_region[${idx}]}
lang=${dat_lang[${idx}]}
crc=${dat_crc[${idx}]}
regex=".... - .* \(.*\) \[.*\] \{.*\} <.*> ..\.zip$"
# Type of archive ?
if [[ ${file} =~ $regex ]]
then
arc_type="rx3"
else
arc_type="unknown"
fi
# Extract ROM if needed
if [[ ( "${arc_type}" != "rx3" ) || ( "${rebuild}" == "yes" ) ]]
then
rom="${id} - ${title} (${reg}) [${lang}].nds"
rom_extract "${rom}" "${file}"
crc2=$(check -n <"${rom}" 2>&1 | sed -e 's/,.*//' -e 's/.*= //' | tr [:lower:] [:upper:])
fp=$(dd skip=12 count=4 bs=1 <"${rom}" 2>/dev/null)
else
crc2=${file/*\{/}; crc2=${crc2/\}*/}
fp=${file/*\} \</}; fp=${fp/\> */}
fi
# Extract info
if [[ "${arc_type}" == "rx3" ]]
then
status=${file/* /}; status=${status/.zip/}
if [[ "${crc}" != "${crc2}" ]]
then
if [[ "${status}" == "OK" ]]
then
action="change_status"
status="KO"
else
action="keep_status"
fi
else
if [[ "${status}" == "OK" ]]
then
action="keep_status"
else
action="change_status"
status="OK"
fi
fi
else
action="set_status"
if [[ "${crc}" != "${crc2}" ]]
then
status="KO"
else
status="OK"
fi
fi
# Try to Renumber if KO
if [[ ( ${status} == "KO" ) && ( "${renum}" == "yes" ) ]]
then
idx2=$( crc_lookup "${crc2}" )
if [[ "${idx2}" != "" ]]
then
idx=${idx2}
id=${dat_id[${idx}]}
title=${dat_title[${idx}]}
reg=${dat_region[${idx}]}
lang=${dat_lang[${idx}]}
crc=${dat_crc[${idx}]}
if [[ "${arc_type}" == "rx3" ]]
then
status=${file/* /}; status=${status/.zip/}
if [[ "${status}" == "OK" ]]
then
action="keep_status"
else
action="change_status"
status="OK"
fi
else
action="set_status"
status="OK"
fi
renum_cnt=$((${renum_cnt} + 1))
fi
fi
# Update Dat Info
if [[ ${dat_status[${idx}]} != "?" ]]
then
if [[ "${mode}" == "test" ]]
then
dup_cnt=$((${dup_cnt} + 1))
else
echo "Error: Duplicate roms not yet supported !";
exit 1
fi
else
dat_fp[${idx}]=${fp}
dat_status[${idx}]=${status}
fi
# Make / Rename Archive
if [[ ( "${arc_type}" != "rx3" ) || ( ${rebuild} == "yes" ) ]]
then
arc_build "${id}" "${title}" "${reg}" "${lang}" "${crc2}" "${fp}" "${status}" "${rom}" "${file}" "${mode}"
else
arc_rename "${id}" "${title}" "${reg}" "${lang}" "${crc2}" "${fp}" "${status}" "${file}" "${action}" "${mode}"
fi
# Update stats
stats_update "${action}" "${mode}"
printf "found: %4d OK: %4d KO: %4d renumbered: %4d duplicated: %4d missing: %4d\r" ${found_cnt} ${ok_cnt} ${ko_cnt} ${renum_cnt} ${dup_cnt} ${missing_cnt}
done < ${tmp_file}
\rm -f ${tmp_file}
wait
echo " "
# Dump ROMs List
#-----------------------------------------------------------------------------------------------------------------------------------
if [[ ${lok} == "yes" ]]
then
ok_dump
fi
if [[ ${lko} == "yes" ]]
then
ko_dump
fi
if [[ ${lmiss} == "yes" ]]
then
missing_dump
fi
# Print statistics
#-----------------------------------------------------------------------------------------------------------------------------------
missing_cnt=$((${loaded_cnt} - ${found_cnt}))
printf "%4d roms set to OK\n" ${ss_ok_cnt}
printf "%4d roms changed to OK\n" ${cs_ok_cnt}
printf "%4d roms kept to OK\n" ${ks_ok_cnt}
printf "%4d roms are OK\n" ${ok_cnt}
echo
printf "%4d roms set to KO\n" ${ss_ko_cnt}
printf "%4d roms changed to KO\n" ${cs_ko_cnt}
printf "%4d roms kept to KO\n" ${ks_ko_cnt}
printf "%4d roms are KO\n" ${ko_cnt}
echo
printf "%4d roms are in dsrom.dat\n" ${loaded_cnt}
printf "%4d roms are found\n" ${found_cnt}
printf "%4d roms are renumbered\n" ${renum_cnt}
printf "%4d roms are duplicated\n" ${dup_cnt}
printf "%4d roms are missing\n" ${missing_cnt}
}
# Main
#-----------------------------------------------------------------------------------------------------------------------------------
@ -226,9 +708,13 @@ function do_action()
ko_cnt=0
found_cnt=0
missing_cnt=0
dup_cnt=0
renum_cnt=0
shopt -s extglob
args_parse "$@"
if [[ ${mode} == "exit" ]]
@ -236,6 +722,13 @@ then
exit 0
fi
if [[ ${NDS_REPOSITORY_IN} == ${NDS_REPOSITORY_OUT} ]]
then
in_place=yes
else
in_place=no
fi
if [[ ! -f ${NDS_DAT} ]]
then
echo "error: dsrom.dat not found !"
@ -254,7 +747,7 @@ while read line
do
set ${line}
idx=$( echo ${2} | sed 's/^0*//')
idx=${2/*(0)/}
if [[ "${idx}" != "xxxx" ]]
then
@ -267,6 +760,8 @@ do
dat_lang[${idx}]="${5}"
dat_save_type[${idx}]="${6}"
dat_crc[${idx}]="${7}"
dat_fp[${idx}]="????"
dat_status[${idx}]="?"
loaded_cnt=$((${loaded_cnt} + 1))
fi
@ -275,111 +770,15 @@ done < ${NDS_DAT}
#-----------------------------------------------------------------------------------------------------------------------------------
if [[ "${mode}" == "dsrom" ]]
then
dsrom_mode
echo "scanning rom repository..."
IFS=' '
else
test_correct_mode
fi
tmp_file=$(mktemp)
idx_old=0
exit 0
ls ${NDS_ROMS} > ${tmp_file}
while read file
do
set ${file:0:4}
id=$1
idx=$( echo ${id} | sed 's/^0*//')
title=${dat_title[${idx}]}
reg=${dat_region[${idx}]}
lang=${dat_lang[${idx}]}
crc=${dat_crc[${idx}]}
regex=".... - .* \(.*\) \[.*\] \{.*\} ..\.zip$"
if [[ $file =~ $regex ]]
then
title2=$(echo ${file} | sed -e 's/.... - //' -e 's/ (.*//')
reg2=$(echo ${file} | sed -e 's/.*(//' -e 's/).*//')
lang2=$(echo ${file} | sed -e 's/.*\[//' -e 's/\].*//')
crc2=$(echo ${file} | sed -e 's/.*{//' -e 's/}.*//')
status=$(echo ${file} | sed -e 's/.* //' -e 's/\.zip$//')
if [[ "${crc}" != "${crc2}" ]]
then
if [[ "${status}" != "KO" ]]
then
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "KO" "${file}" "change_status" "${mode}" "${rebuild}"
else
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "KO" "${file}" "keep_status" "${mode}" "${rebuild}"
fi
else
if [[ "${status}" == "OK" ]]
then
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "OK" "${file}" "keep_status" "${mode}" "${rebuild}"
else
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "OK" "${file}" "change_status" "${mode}" "${rebuild}"
fi
fi
else
crc2=$( case "${file}"
in
(*.nds)
cat "${NDS_ROMS}/${file}"
;;
(*.rar)
unrar p "${NDS_ROMS}/${file}"
;;
(*.zip)
unzip -p "${NDS_ROMS}/${file}"
;;
(*)
break;
;;
esac | check -n 2>&1 | sed -e 's/,.*//' -e 's/.*= //' | tr [:lower:] [:upper:])
if [[ "${crc}" != "${crc2}" ]]
then
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "KO" "${file}" "set_status" "${mode}" "${rebuild}" "${rebuild}"
else
do_action "${id}" "${title}" "${reg}" "${lang}" "${crc}" "OK" "${file}" "set_status" "${mode}" "${rebuild}" "${rebuild}"
fi
fi
ok_cnt=$((${ss_ok_cnt} + ${cs_ok_cnt} + ${ks_ok_cnt}))
ko_cnt=$((${ss_ko_cnt} + ${cs_ko_cnt} + ${ks_ko_cnt}))
found_cnt=$((${ok_cnt} + ${ko_cnt}))
missing_cnt=$((${missing_cnt} + ${idx} - ${idx_old} - 1))
idx_old=${idx}
printf "found: %4d OK: %4d KO: %4d missing: %4d \r" ${found_cnt} ${ok_cnt} ${ko_cnt} ${missing_cnt}
done < ${tmp_file}
\rm -f ${tmp_file}
wait
# Print statistics
#-----------------------------------------------------------------------------------------------------------------------------------
echo " "
printf "%4d roms set to OK\n" ${ss_ok_cnt}
printf "%4d roms changed to OK\n" ${cs_ok_cnt}
printf "%4d roms kept to OK\n" ${ks_ok_cnt}
printf "%4d roms are OK\n" ${ok_cnt}
echo
printf "%4d roms set to KO\n" ${ss_ko_cnt}
printf "%4d roms changed to KO\n" ${cs_ko_cnt}
printf "%4d roms kept to KO\n" ${ks_ko_cnt}
printf "%4d roms are KO\n" ${ko_cnt}
echo
printf "%4d roms are in dsrom.dat\n" ${loaded_cnt}
printf "%4d roms are found\n" ${found_cnt}
printf "%4d roms are missing\n" ${missing_cnt}