Created
October 9, 2021 08:54
-
-
Save brianpow/9b2b68593b00f451adca12b195a85363 to your computer and use it in GitHub Desktop.
For conversion of decompiled Unity il2cpp.h header file to Ghidra compatible format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Originally written by https://github.com/therealchjones | |
# Original Source: https://github.com/Perfare/Il2CppDumper/issues/287#issuecomment-669544385 | |
# For conversion of decompiled Unity il2cpp.h header file to Ghidra compatible format | |
DEBUG=y | |
SOURCEFILE="$1" | |
NEWFILE="${2:-il2cpp-ghidra.h}" | |
if [ "$#" -lt 1 ]; then | |
echo "Usage: $0 <SOURCE> [TARGET]" | |
echo " Default TARGET is $NEWFILE" | |
echo "Example 1: $0 il2cpp.h" | |
echo "Example 2: $0 il2cpp.h il2cpp-fixed.h" | |
return 1 | |
fi | |
if [ "$SOURCEFILE" == "$NEWFILE" ]; then | |
echo "Error: source and target file cannot be the same!" | |
return 1 | |
fi | |
if [ ! -f "$SOURCEFILE" ]; then | |
echo "Error: source file $1 does not exists!" | |
return 1 | |
fi | |
if [ -f "$NEWFILE" ]; then | |
echo "Error: target file $2 exists!" | |
return 1 | |
fi | |
STARTTIME=`date '+%s'` | |
BASENAME=`basename "${0}"` | |
TMPDIR=`mktemp -d -t "$BASENAME".tmp.XXXXXX` | |
if [ "$?" -ne "0" ]; then | |
echo "$BASENAME: Unable to make temporary directory">&2 | |
echo "TMPDIR='$TMPDIR'">&2 | |
echo "_CS_DARWIN_USER_TEMP_DIR='$_CS_DARWIN_USER_TEMP_DIR'">&2 | |
echo "mktemp: '`which mktemp`'">&2 | |
return 1 | |
fi | |
SEDFILE=`mktemp "$TMPDIR"/"$BASENAME".sed.XXXXXX` | |
TEMPFILE=`mktemp "$TMPDIR"/"$BASENAME".tmp.XXXXXX` | |
TEMPFILE2=`mktemp "$TMPDIR"/"$BASENAME".tmp.XXXXXX` | |
TYPEDEFS=' | |
typedef int intptr_t; | |
typedef uint uintptr_t; | |
typedef uchar uint8_t; | |
typedef char int8_t; | |
typedef short int16_t; | |
typedef ushort uint16_t; | |
typedef int int32_t; | |
typedef uint uint32_t; | |
typedef longlong int64_t; | |
typedef ulonglong uint64_t; | |
' | |
# see https://github.com/Perfare/Il2CppDumper/issues/287 | |
if [ "$DEBUG" == "y" ]; then | |
exec 3>&1 | |
DEBUG=/dev/fd/3 | |
else | |
DEBUG=/dev/null; | |
fi | |
I=0 | |
DERIVATIVES=`grep ':' "$SOURCEFILE"` | |
NUMDERIVATIVE=`echo "$DERIVATIVES" | wc -l | \ | |
sed -E -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'` | |
while [ "$NUMDERIVATIVE" -gt "0" ]; do | |
echo "Derivative classes remaining: $NUMDERIVATIVE" >$DEBUG | |
I=$(( $I + 1 )) | |
echo "Starting pass $I" > $DEBUG | |
cat /dev/null > "$SEDFILE" | |
CLASSESPROCESSED=0; | |
INHERITEDS=`echo "$DERIVATIVES" | cut -f2 -d':' | \ | |
sed -E -e 's/[[:space:]]*\{?[[:space:]]*$//' \ | |
-e 's/^[[:space:]]*//' | sort -u` | |
NUMINHERITED=`echo "$INHERITEDS" | wc -l | \ | |
sed -E -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'` | |
echo "Inherited classes to process: $NUMINHERITED" >$DEBUG | |
echo "$INHERITEDS" | while read CLASS; do | |
echo -n "Processing $CLASS: " >"$DEBUG" | |
if grep -q -E '^(struct|typedef|union)[[:space:]]+'"$CLASS"'[[:space:]]*\{[[:space:]]*$' "$SOURCEFILE"; then | |
echo -n "base, will replace... " >"$DEBUG" | |
REPLACEMENT=`sed -n -E '/^(struct|typedef|union)[[:space:]]+'"$CLASS"'[[:space:]]*\{[[:space:]]*$/,/^\};/p' "$SOURCEFILE" | \ | |
sed -E -e 's/^(struct|typedef|union)[[:space:]]+'"$CLASS"'[[:space:]]*//' -e '/^\};[[:space:]]*$/d'` | |
echo 's/:[[:space:]]*'"$CLASS"'[[:space:]]*\{[[:space:]]*$/'"$REPLACEMENT"'/' | sed -E 's/([^/])$/\1\\/' >> "$SEDFILE" | |
fi | |
CLASSESPROCESSED=$(( $CLASSESPROCESSED + 1 )) | |
echo "done. $CLASSESPROCESSED/$NUMINHERITED ($(( $CLASSESPROCESSED*100/$NUMINHERITED ))%)" > $DEBUG | |
done | |
echo -n "Modifying source... " > "$DEBUG" | |
sed -E -f "$SEDFILE" "$SOURCEFILE" > "$TEMPFILE" | |
cp "$TEMPFILE" "$TEMPFILE2" | |
SOURCEFILE="$TEMPFILE2" | |
echo "done.">"$DEBUG" | |
echo "Checking result: ">"$DEBUG" | |
DERIVATIVES=`grep ':' "$SOURCEFILE"` | |
if [ -n "$DERIVATIVES" ]; then | |
NUMDERIVATIVE=`echo "$DERIVATIVES" | wc -l | \ | |
sed -E -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'` | |
else | |
NUMDERIVATIVE=0 | |
fi | |
done | |
echo "Completed processing after $I passes." >"$DEBUG" | |
echo -n "Writing new header file... ">"$DEBUG" | |
echo "$TYPEDEFS" > "$NEWFILE" | |
cat "$SOURCEFILE" >> "$NEWFILE" | |
echo "done." >"$DEBUG" | |
echo -n "Cleaning up... ">"$DEBUG" | |
rm -rf "$TMPDIR" | |
echo "done." >"$DEBUG" | |
NOWTIME=`date '+%s'` | |
echo 'Time elapsed: approximately '"$(( $NOWTIME - $STARTTIME ))"' seconds.' >"$DEBUG" | |
echo "Done." > "$DEBUG" | |
return 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment