Skip to content

Instantly share code, notes, and snippets.

@hastinbe
Created October 15, 2015 01:25
Show Gist options
  • Select an option

  • Save hastinbe/64f76aa37942e37205c1 to your computer and use it in GitHub Desktop.

Select an option

Save hastinbe/64f76aa37942e37205c1 to your computer and use it in GitHub Desktop.
Cleans up decompiled TerrariaServer code
#!/bin/bash
#
# fixup-ids
#
# Searches and replaces integer literals with their equivalent enumerated type
#
# Requires:
# dos2unix
# GNU awk >= 4.1.0
#
# Copyright (c) 2015 Beau Hastings. All rights reserved.
# License: GNU General Public License v3
#
# Author: Beau Hastings <[email protected]>
# Terminal colors
if tput setaf 1 &> /dev/null; then
tput sgr0
if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
MAGENTA=$(tput setaf 9)
ORANGE=$(tput setaf 172)
GREEN=$(tput setaf 190)
PURPLE=$(tput setaf 141)
WHITE=$(tput setaf 256)
else
MAGENTA=$(tput setaf 5)
ORANGE=$(tput setaf 4)
GREEN=$(tput setaf 2)
PURPLE=$(tput setaf 1)
WHITE=$(tput setaf 7)
fi
BOLD=$(tput bold)
RESET=$(tput sgr0)
else
MAGENTA="\033[1;31m"
ORANGE="\033[1;33m"
GREEN="\033[1;32m"
BLUE="\033[1;34m"
PURPLE="\033[1;35m"
WHITE="\033[1;37m"
BOLD=""
RESET="\033[m"
fi
# Function definitions
print_error() { echo " ${BOLD}${WHITE}[${RED}$@${WHITE}]${RESET}"; }
print_success() { echo " ${BOLD}${WHITE}[${GREEN}$@${WHITE}]${RESET}"; }
enter_to_continue() { echo -n "Press ENTER to continue"; read; }
###############################################################################
# Translates integer literals for Packet IDs to enumerated types.
#
# Arguments:
# A list of files to translate
#
# Returns:
# 0 on success, non-zero on failure
#
# Example: line: NetMessage.SendData(34, ...)
# new line: NetMessage.SendData((int)PacketTypes.TileKill, ...)
###############################################################################
translate_packettypes() {
local files="$@"
gawk -i inplace '
NR==FNR {
if (match($0, /enum (\w+)/, header))
class=header[1]
if (match($0, /^\s+(\w+)\s?=\s?([0-9]+),/, type))
enums[type[2]]=type[1]
print
next
}
NR!=FNR {
if (match($0, /(.*NetMessage\.SendData\()([0-9]+)(,.*)/, loc))
{
if (class)
{
print loc[1] "(int)"class"."enums[loc[2]] loc[3]
next
}
}
print
}' $files
return $?
}
###############################################################################
# Translates integer literals for NPC IDs to enumerated types.
#
# Arguments:
# A list of files to translate
#
# Returns:
# 0 on success, non-zero on failure
#
# Example: line: NetMessage.SendData(34, ...)
# new line: NetMessage.SendData((int)PacketTypes.TileKill, ...)
###############################################################################
translate_npcids() {
local files="$@"
gawk -i inplace '
NR==FNR {
class="NPCID"
if (match($0, /^\s+public const short (\w+)\s?=\s?([0-9]+);/, type))
enums[type[2]]=type[1]
print
next
}
NR!=FNR {
file=FILENAME
gsub(/\/.*\//, "", file)
# Ensure search for "this.type" is only for NPC.cs
if (file == "NPC.cs")
{
if (match($0, /this\.type (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /this\.type (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/this\.type (==|<=|>=|!=) [0-9]+/, "this.type " parts[1] " " class "." enums[parts[2]], 1, $0)
}
}
}
if (match($0, /maxNPCTypes = [0-9]+/))
{
while (match($0, /maxNPCTypes = ([0-9]+)/, parts))
{
if (length(enums[parts[1]]) < 1)
break
$0 = gensub(/maxNPCTypes = [0-9]+/, "maxNPCTypes = " class "." enums[parts[1]], 1, $0)
}
}
else if (match($0, /npc\[[a-zA-Z0-9]+\]\.type (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /(npc\[[a-zA-Z0-9]+\])\.type (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[3]]) < 1)
break
$0 = gensub(/(npc\[[a-zA-Z0-9]+\])\.type (==|<=|>=|!=) [0-9]+/, parts[1] ".type " parts[2] " " class "." enums[parts[3]], 1, $0)
}
}
else if (match($0, /NPC\.AnyNPCs\([0-9]+\)/))
{
while (match($0, /NPC\.AnyNPCs\(([0-9]+)\)/, parts))
{
if (length(enums[parts[1]]) < 1)
break
$0 = gensub(/NPC\.AnyNPCs\([0-9]+\)/, "NPC.AnyNPCs(" class "." enums[parts[1]] ")", 1, $0)
}
}
else if (match($0, /NPC\.firstNPCName\([0-9]+\)/))
{
while (match($0, /NPC\.firstNPCName\(([0-9]+)\)/, parts))
{
if (length(enums[parts[1]]) < 1)
break
$0 = gensub(/NPC\.firstNPCName\([0-9]+\)/, "NPC.firstNPCName(" class "." enums[parts[1]] ")", 1, $0)
}
}
else if (match($0, /Main\.(NPCLoaded|nextNPC|slimeRainNPC|npcCatchable|npcName) = new \w+\[[0-9]+/))
{
while (match($0, /Main\.(NPCLoaded|nextNPC|slimeRainNPC|npcCatchable|npcName) = new (\w+)\[([0-9]+)/, parts))
{
if (length(enums[parts[3]]) < 1)
break;
$0 = gensub(/Main\.(NPCLoaded|nextNPC|slimeRainNPC|npcCatchable|npcName) = new \w+\[[0-9]+/,
"Main." parts[1] " = new " parts[2] "[Main.maxNPCTypes", 1, $0)
}
}
print
next
}' $files
return $?
}
###############################################################################
# Translates integer literals for MessageIDs to enumerated types.
#
# Arguments:
# A list of files to translate
#
# Returns:
# 0 on success, non-zero on failure
###############################################################################
translate_messageids() {
local files="$@"
gawk -i inplace '
NR==FNR {
class="MessageID"
if (match($0, /^\s+public const byte (\w+)\s?=\s?([0-9]+);/, type))
enums[type[2]]=type[1]
print
next
}
NR!=FNR {
if (match($0, /case ([0-9]+)/, parts))
{
if (length(enums[parts[1]]) > 0)
$0 = gensub(/case [0-9]+/, "case " class "." enums[parts[1]], 1, $0)
}
print
}' $files
return $?
}
###############################################################################
# Translates integer literals for Tile IDs to enumerated types.
#
# Arguments:
# A list of files to translate
#
# Returns:
# 0 on success, non-zero on failure
###############################################################################
translate_tileids() {
local files="$@"
gawk -i inplace '
NR==FNR {
class="TileID"
if (match($0, /^\s+public const ushort (\w+)\s?=\s?([0-9]+);/, type))
enums[type[2]]=type[1]
# Tile Sets
if (match($0, /{?[ ]+[0-9]+?[ ,]+}/))
{
while (match($0, /\s([0-9]+)/, sets))
$0 = gensub(/\s[0-9]+/, " " class "." enums[sets[1]], 1, $0)
}
print
next
}
NR!=FNR {
if (match($0, /maxTileSets = [0-9]+/))
{
while (match($0, /maxTileSets = ([0-9]+)/, parts))
{
if (length(enums[parts[1]]) < 1)
break
$0 = gensub(/maxTileSets = [0-9]+/, "maxTileSets = " class "." enums[parts[1]], 1, $0)
}
}
else if (match($0, /tile\.type (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /tile\.type (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/tile\.type (==|<=|>=|!=) [0-9]+/, "tile.type " parts[1] " " class "." enums[parts[2]], 1, $0)
}
}
else if (match($0, /Main\.tile\w+\[[0-9]+\]/))
{
while (match($0, /Main\.tile(\w+)\[([0-9]+)\]/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/Main\.tile\w+\[[0-9]+\]/, "Main.tile" parts[1] "[" class "." enums[parts[2]] "]", 1, $0)
}
}
else if (match($0, /Main\.tile\[\w+, \w+\]\.type (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /Main\.tile\[(\w+, \w+)\]\.type (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[3]]) < 1)
break
$0 = gensub(/Main\.tile\[\w+, \w+\]\.type (==|<=|>=|!=) [0-9]+/, "Main.tile[" parts[1] "].type " parts[2] " " class "." enums[parts[3]], 1, $0)
}
}
else if (match($0, /Main\.tile\w+ = new \w+\[[0-9]+/))
{
while (match($0, /(Main\.tile\w+ = new \w+)\[([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break;
$0 = gensub(/Main\.tile\w+ = new \w+\[[0-9]+/, parts[1] "[Main.maxTileSets", 1, $0)
}
}
print
next
}' $files
return $?
}
###############################################################################
# Translates integer literals for Item IDs to enumerated types.
#
# Arguments:
# A list of files to translate
#
# Returns:
# 0 on success, non-zero on failure
###############################################################################
translate_itemids() {
local files="$@"
gawk -i inplace '
NR==FNR {
class="ItemID"
if (match($0, /^\s+public const short (\w+)\s?=\s?([0-9]+);/, type))
enums[type[2]]=type[1]
print
next
}
NR!=FNR {
file=FILENAME
gsub(/\/.*\//, "", file)
# Ensure search for "this.type" is only for Item.cs
if (file == "Item.cs")
{
if (match($0, /this\.type (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /this\.type (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/this\.type (==|<=|>=|!=) [0-9]+/, "this.type " parts[1] " " class "." enums[parts[2]], 1, $0)
}
}
}
if (match($0, /maxItemTypes = [0-9]+/))
{
while (match($0, /maxItemTypes = ([0-9]+)/, parts))
{
if (length(enums[parts[1]]) < 1)
break
$0 = gensub(/maxItemTypes = [0-9]+/, "maxItemTypes = " class "." enums[parts[1]], 1, $0)
}
}
else if (match($0, /(item|newItem|sItem|trashItem)\.type (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /(item|newItem|sItem|trashItem)\.type (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[3]]) < 1)
break
$0 = gensub(/(item|newItem|sItem|trashItem)\.type (==|<=|>=|!=) [0-9]+/, parts[1] ".type " parts[2] " " class "." enums[parts[3]], 1, $0)
}
}
print
next
}' $files
return $?
}
###############################################################################
# Translates integer literals for Wall IDs to enumerated types.
#
# Arguments:
# A list of files to translate
#
# Returns:
# 0 on success, non-zero on failure
###############################################################################
translate_wallids() {
local files="$@"
gawk -i inplace '
NR==FNR {
class="WallID"
if (match($0, /^\s+public const byte (\w+)\s?=\s?([0-9]+);/, type))
enums[type[2]]=type[1]
# Wall Sets
if (match($0, /{?[ ]+[0-9]+?[ ,]+}/))
{
while (match($0, /\s([0-9]+)/, sets))
$0 = gensub(/\s[0-9]+/, " " class "." enums[sets[1]], 1, $0)
}
print
next
}
NR!=FNR {
if (match($0, /maxWallTypes = [0-9]+/))
{
while (match($0, /maxWallTypes = ([0-9]+)/, parts))
{
if (length(enums[parts[1]]) < 1)
break
$0 = gensub(/maxWallTypes = [0-9]+/, "maxWallTypes = " class "." enums[parts[1]], 1, $0)
}
}
else if (match($0, /tile\.wall (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /tile\.wall (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/tile\.wall (==|<=|>=|!=) [0-9]+/, "tile.wall " parts[1] " " class "." enums[parts[2]], 1, $0)
}
}
else if (match($0, /Main\.wall\w+\[[0-9]+\]/))
{
while (match($0, /Main\.wall(\w+)\[([0-9]+)\]/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/Main\.wall\w+\[[0-9]+\]/, "Main.wall" parts[1] "[" class "." enums[parts[2]] "]", 1, $0)
}
}
else if (match($0, /Main\.tile\[\w+, \w+\]\.wall (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /Main\.tile\[(\w+, \w+)\]\.wall (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[3]]) < 1)
break
$0 = gensub(/Main\.tile\[\w+, \w+\]\.wall (==|<=|>=|!=) [0-9]+/, "Main.tile[" parts[1] "].wall " parts[2] " " class "." enums[parts[3]], 1, $0)
}
}
else if (match($0, /\s+(wall|wallType) = [0-9]+/))
{
while (match($0, /\s+(wall|wallType) = ([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/(wall|wallType) = [0-9]+/, parts[1] " = " class "." enums[parts[2]], 1, $0)
}
}
else if (match($0, /Main\.(wallLoaded|wallHouse|wallDungeon|wallLight|wallBlend|wallLargeFrames|wallFrame|wallFrameCounter|wallAltTextureInit|wallAltTextureDrawn) = new \w+\[[0-9]+/))
{
while (match($0, /Main\.(wallLoaded|wallHouse|wallDungeon|wallLight|wallBlend|wallLargeFrames|wallFrame|wallFrameCounter|wallAltTextureInit|wallAltTextureDrawn) = new (\w+)\[([0-9]+)/, parts))
{
if (length(enums[parts[3]]) < 1)
break;
$0 = gensub(/Main\.(wallLoaded|wallHouse|wallDungeon|wallLight|wallBlend|wallLargeFrames|wallFrame|wallFrameCounter|wallAltTextureInit|wallAltTextureDrawn) = new \w+\[[0-9]+/,
"Main." parts[1] " = new " parts[2] "[Main.maxWallTypes", 1, $0)
}
}
print
next
}' $files
return $?
}
###############################################################################
# Translates integer literals for Projectile IDs to enumerated types.
#
# Arguments:
# A list of files to translate
#
# Returns:
# 0 on success, non-zero on failure
###############################################################################
translate_projectileids() {
local files="$@"
gawk -i inplace '
NR==FNR {
class="ProjectileID"
if (match($0, /^\s+public const short (\w+)\s?=\s?([0-9]+);/, type))
enums[type[2]]=type[1]
# Sets
if (match($0, /numArray\[[0-9]+\] = ([0-9]+)/, array))
{
$0 = gensub(/= [0-9]+/, "= " class "." enums[array[1]], 1, $0)
}
else if (match($0, /{?[ ]+[0-9]+?[ ,]+}/))
{
while (match($0, /\s([0-9]+)/, sets))
$0 = gensub(/\s[0-9]+/, " " class "." enums[sets[1]], 1, $0)
}
else if (match($0, /new SetFactory\(([0-9]+)\)/, array))
{
$0 = gensub(/new SetFactory\([0-9]+\)/, "new SetFactory(" class "." enums[array[1]] ")", 1, $0)
}
print
next
}
NR!=FNR {
file=FILENAME
gsub(/\/.*\//, "", file)
# Ensure search for "this.type" is only for Projectile.cs
if (file == "Projectile.cs")
{
if (match($0, /this\.type (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /this\.type (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/this\.type (==|<=|>=|!=) [0-9]+/, "this.type " parts[1] " " class "." enums[parts[2]], 1, $0)
}
}
}
if (match($0, /maxProjectileTypes = [0-9]+/))
{
while (match($0, /maxProjectileTypes = ([0-9]+)/, parts))
{
if (length(enums[parts[1]]) < 1)
break;
$0 = gensub(/maxProjectileTypes = [0-9]+/, "maxProjectileTypes = " class "." enums[parts[1]], 1, $0)
}
}
else if (match($0, /projectile\.type (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /projectile\.type (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break;
$0 = gensub(/projectile\.type (==|<=|>=|!=) [0-9]+/, "projectile.type " parts[1] " " class "." enums[parts[2]], 1, $0)
}
}
else if (match($0, /Main\.projectile\[\w+\]\.type (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /(Main\.projectile\[\w+\]\.type) (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[3]]) < 1)
break;
$0 = gensub(/Main\.projectile\[\w+\]\.type (==|<=|>=|!=) [0-9]+/, parts[1] " " parts[2] " " class "." enums[parts[3]], 1, $0)
}
}
else if (match($0, /Projectile\.NewProjectile\(\w+, \w+, \w+, \w+, [0-9]+/))
{
while (match($0, /(Projectile\.NewProjectile\(\w+, \w+, \w+, \w+, )([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/Projectile\.NewProjectile\(\w+, \w+, \w+, \w+, [0-9]+/, parts[1] class "." enums[parts[2]], 1, $0)
}
}
else if (match($0, /ownedProjectileCounts\[[0-9]+\]/))
{
while (match($0, /ownedProjectileCounts\[([0-9]+)\]/, parts))
{
if (length(enums[parts[1]]) < 1)
break
$0 = gensub(/ownedProjectileCounts\[[0-9]+\]/, "ownedProjectileCounts[" class "." enums[parts[1]] "]", 1, $0)
}
}
else if (match($0, /Main\.(projectileLoaded|projHostile|projHook|projFrames|projPet) = new \w+\[[0-9]+/))
{
while (match($0, /Main\.(projectileLoaded|projHostile|projHook|projFrames|projPet) = new (\w+)\[([0-9]+)/, parts))
{
if (length(enums[parts[3]]) < 1)
break;
$0 = gensub(/Main\.(projectileLoaded|projHostile|projHook|projFrames|projPet) = new \w+\[[0-9]+/,
"Main." parts[1] " = new " parts[2] "[Main.maxProjectileTypes", 1, $0)
}
}
print
next
}' $files
return $?
}
###############################################################################
# Translates integer literals for Buff IDs to enumerated types.
#
# Arguments:
# A list of files to translate
#
# Returns:
# 0 on success, non-zero on failure
###############################################################################
translate_buffids() {
local files="$@"
gawk -i inplace '
NR==FNR {
class="BuffID"
if (match($0, /^\s+public const int (\w+)\s?=\s?([0-9]+);/, type))
enums[type[2]]=type[1]
print
next
}
NR!=FNR {
if (match($0, /maxBuffTypes = [0-9]+/))
{
while (match($0, /maxBuffTypes = ([0-9]+)/, parts))
{
if (length(enums[parts[1]]) < 1)
break;
$0 = gensub(/maxBuffTypes = [0-9]+/, "maxBuffTypes = " class "." enums[parts[1]], 1, $0)
}
}
else if (match($0, /(Main.buffName|Main.buffTip)\[[0-9]+\]/))
{
while (match($0, /(Main.buffName|Main.buffTip)\[([0-9]+)\]/, parts))
{
if (length(enums[parts[2]]) < 1)
break
$0 = gensub(/(Main.buffName|Main.buffTip)\[[0-9]+\]/, parts[1] "[" class "." enums[parts[2]] "]", 1, $0)
}
}
else if (match($0, /mountDatum\.buff = [0-9]+/))
{
while (match($0, /mountDatum\.buff = ([0-9]+)/, parts))
{
if (length(enums[parts[1]]) < 1)
break
$0 = gensub(/mountDatum\.buff = [0-9]+/, "mountDatum.buff = " class "." enums[parts[1]], 1, $0)
}
}
else if (match($0, /Main\.(pvpBuff|persistentBuff|meleeBuff|debuff|buffName|buffTip|buffNoSave|buffNoTimeDisplay|buffDoubleApply|buffAlpha) = new (bool|string|float)\[[0-9]+\]/))
{
while (match($0, /Main\.(pvpBuff|persistentBuff|meleeBuff|debuff|buffName|buffTip|buffNoSave|buffNoTimeDisplay|buffDoubleApply|buffAlpha) = new (bool|string|float)\[([0-9]+)\]/, parts))
{
if (length(enums[parts[3]]) < 1)
break
$0 = gensub(/Main\.(pvpBuff|persistentBuff|meleeBuff|debuff|buffName|buffTip|buffNoSave|buffNoTimeDisplay|buffDoubleApply|buffAlpha) = new (bool|string|float)\[[0-9]+\]/,
"Main." parts[1] " = new " parts[2] "[Main.maxBuffTypes]", 1, $0)
}
}
else if (match($0, /Main\.(debuff|pvpBuff|meleeBuff|buffNoSave|buffNoTimeDisplay|persistentBuff)\[[0-9]+\]/))
{
while (match($0, /Main\.(debuff|pvpBuff|meleeBuff|buffNoSave|buffNoTimeDisplay|persistentBuff)\[([0-9]+)\]/, parts))
{
if (length(enums[parts[2]]) < 1)
break;
$0 = gensub(/Main\.(debuff|pvpBuff|meleeBuff|buffNoSave|buffNoTimeDisplay|persistentBuff)\[[0-9]+\]/,
"Main." parts[1] "[" class "." enums[parts[2]] "]", 1, $0)
}
}
else if (match($0, /this\.buffType = [0-9]+/))
{
while (match($0, /this\.buffType = ([0-9]+)/, parts))
{
if (length(enums[parts[1]]) < 1)
break;
$0 = gensub(/this\.buffType = [0-9]+/, "this.buffType = " class "." enums[parts[1]], 1, $0)
}
}
else if (match($0, /(AddBuff|HasBuff)\([0-9]+/))
{
while (match($0, /(AddBuff|HasBuff)\(([0-9]+)/, parts))
{
if (length(enums[parts[2]]) < 1)
break;
$0 = gensub(/(AddBuff|HasBuff)\([0-9]+/, parts[1] "(" class "." enums[parts[2]], 1, $0)
}
}
else if (match($0, /buffImmune = new bool\[[0-9]+\]/))
{
while (match($0, /buffImmune = new bool\[([0-9]+)\]/, parts))
{
if (length(enums[parts[1]]) < 1)
break;
$0 = gensub(/buffImmune = new bool\[[0-9]+\]/,
"buffImmune = new bool[" class "." enums[parts[1]] "]", 1, $0)
}
}
else if (match($0, /this\.buffImmune\[[0-9]+\]/))
{
while (match($0, /this\.buffImmune\[([0-9]+)\]/, parts))
{
if (length(enums[parts[1]]) < 1)
break;
$0 = gensub(/this\.buffImmune\[[0-9]+\]/,
"this.buffImmune[" class "." enums[parts[1]] "]", 1, $0)
}
}
else if (match($0, /buffType\[\w+\] (==|<=|>=|!=) [0-9]+/))
{
while (match($0, /buffType\[(\w+)\] (==|<=|>=|!=) ([0-9]+)/, parts))
{
if (length(enums[parts[3]]) < 1)
break;
$0 = gensub(/buffType\[\w+\] (==|<=|>=|!=) [0-9]+/,
"buffType[" parts[1] "] " parts[2] " " class "." enums[parts[3]], 1, $0)
}
}
print
next
}' $files
return $?
}
###############################################################################
# Stages and commits files with git
#
# Arguments:
# A list of files to commit
###############################################################################
prompt_commit() {
local stage="y"
read -n 1 -ep "Stage and commit changes [Y/n]? " -i "Y" stage
stage=$(echo $stage | tr '[:upper:]' '[:lower:]')
if [ "${stage}" == "y" ]; then
git add $@ && git commit -s
fi
}
###############################################################################
# Small helper to run a command and display what's going on and if it was
# successful or not.
#
# Arguments:
# <message> A quoted status message
# <command> The command to run
#
# Returns:
# 0 on success, non-zero on failure
###############################################################################
run_command() {
echo -n $1
if $2; then
print_success "OK"
else
print_error "ERROR"
exit 1
fi
}
###############################################################################
# Begin program
###############################################################################
while getopts ":abijmnptw" opt; do
case $opt in
a) opt_all=true;;
b) opt_buffids=true;;
i) opt_itemids=true;;
j) opt_projectileids=true;;
m) opt_messageids=true;;
n) opt_npcids=true;;
p) opt_packettypes=true;;
t) opt_tileids=true;;
w) opt_wallids=true;;
\?) echo "Invalid option: -$OPTARG" >&2;;
esac
done
shift $((OPTIND-1))
# Initialize global variables
SOURCE_PATH=$(pwd)
if [ $# -eq 1 ]; then
SOURCE_PATH="$1"
fi
# NPCIDs
if [ -n "$opt_all" ] || [ -n "$opt_npcids" ]; then
FILES=$(find ${SOURCE_PATH} -name "NPCID.cs" -type f -print -quit)
FILES="${FILES[@]/%/ $(grep -E --include=\*.cs -irlw \
-e 'npc\[[a-zA-Z0-9]+\]\.type (==|<=|>=|!=) [0-9]+' \
-e 'NPC\.AnyNPCs\([0-9]+\)' \
-e 'NPC\.firstNPCName\([0-9]+\)' \
-e 'Main\.(NPCLoaded|nextNPC|slimeRainNPC|npcCatchable|npcName) = new \w+\[[0-9]+' \
${SOURCE_PATH})}"
if [ -n "$FILES" ]; then
dos2unix -- ${FILES}
run_command "Translate integer literals to enumerated NPCIDs..." \
$(translate_npcids "${FILES}")
prompt_commit "${FILES}"
fi
fi
# PacketTypes
if [ -n "$opt_all" ] || [ -n "$opt_packettypes" ]; then
FILES=$(find ${SOURCE_PATH} -name "PacketTypes.cs" -type f -print -quit)
FILES="${FILES[@]/%/ $(grep -E --include=\*.cs -rlw 'NetMessage\.SendData\([0-9]+,' ${SOURCE_PATH})}"
if [ -n "$FILES" ]; then
dos2unix -- ${FILES}
run_command "Translate integer literals to enumerated PacketTypes..." \
$(translate_packettypes "${FILES}")
prompt_commit "${FILES}"
fi
fi
# MessageIDs
if [ -n "$opt_all" ] || [ -n "$opt_messageids" ]; then
FILES=$(find ${SOURCE_PATH} -name "MessageID.cs" -type f -print -quit)
FILES="${FILES[@]/%/ $(find ${SOURCE_PATH} -name 'MessageBuffer.cs' -type f -print -quit)}"
if [ -n "$FILES" ]; then
dos2unix -- ${FILES}
run_command "Translate integer literals to enumerated MessageIDs..." $(translate_messageids "${FILES}")
prompt_commit "${FILES}"
fi
fi
# TileIDs
if [ -n "$opt_all" ] || [ -n "$opt_tileids" ]; then
FILES=$(find ${SOURCE_PATH} -name "TileID.cs" -type f -print -quit)
FILES="${FILES[@]/%/ $(grep -E --include=\*.cs -irlw \
-e 'tile.type (==|<=|>=|!=) [0-9]+' \
-e 'Main.tile\w+\[[a-zA-Z0-9]+\] (==|<=|>=|!=) [0-9]+' \
-e 'Main.tile\[\w+, \w+\]\.type (==|<=|>=|!=) [0-9]+' \
-e 'Main\.tile\w+ = new \w+\[[0-9]+' \
${SOURCE_PATH})}"
if [ -n "$FILES" ]; then
dos2unix -- ${FILES}
run_command "Translate integer literals to enumerated TileIDs..." $(translate_tileids "${FILES}")
prompt_commit "${FILES}"
fi
fi
# ItemIDs
if [ -n "$opt_all" ] || [ -n "$opt_itemids" ]; then
FILES=$(find ${SOURCE_PATH} -name "ItemID.cs" -type f -print -quit)
FILES="${FILES[@]/%/ $(grep -E --include=\*.cs -irlw \
-e '(item|newItem|sItem|trashItem)\.type (==|<=|>=|!=) [0-9]+' \
-e 'maxItemTypes = [0-9]+' \
${SOURCE_PATH})}"
if [ -n "$FILES" ]; then
dos2unix -- ${FILES}
run_command "Translate integer literals to enumerated ItemIDs..." \
$(translate_itemids "${FILES}")
prompt_commit "${FILES}"
fi
fi
# WallIDs
if [ -n "$opt_all" ] || [ -n "$opt_wallids" ]; then
FILES=$(find ${SOURCE_PATH} -name "WallID.cs" -type f -print -quit)
FILES="${FILES[@]/%/ $(grep -E --include=\*.cs -irlw -e 'tile.wall (==|<=|>=|!=) [0-9]+' \
-e 'Main.wall\w+\[[a-zA-Z0-9]+\] (==|<=|>=|!=) [0-9]+' \
-e 'Main.tile\[\w+, \w+\]\.wall (==|<=|>=|!=) [0-9]+' \
-e 'Main\.(wallLoaded|wallHouse|wallDungeon|wallLight|wallBlend|wallLargeFrames|wallFrame|wallFrameCounter|wallAltTextureInit|wallAltTextureDrawn) = new \w+\[[0-9]+' \
${SOURCE_PATH})}"
if [ -n "$FILES" ]; then
dos2unix -- ${FILES}
run_command "Translate integer literals to enumerated WallIDs..." $(translate_wallids "${FILES}")
prompt_commit "${FILES}"
fi
fi
# ProjectileIDs
if [ -n "$opt_all" ] || [ -n "$opt_projectileids" ]; then
FILES=$(find ${SOURCE_PATH} -name "ProjectileID.cs" -type f -print -quit)
FILES="${FILES[@]/%/ $(grep -E --include=\*.cs -irlw \
-e 'maxProjectileTypes = [0-9]+' \
-e 'projectile.type (==|<=|>=|!=) [0-9]+' \
-e 'Main.projectile\[[a-zA-Z0-9]+\]\.type (==|<=|>=|!=) [0-9]+' \
-e 'Projectile\.NewProjectile' \
-e 'ownedProjectileCounts\[[0-9]+\]' \
-e 'Main\.(projectileLoaded|projHostile|projHook|projFrames|projPet) = new \w+\[[0-9]+' \
${SOURCE_PATH})}"
if [ -n "$FILES" ]; then
dos2unix -- ${FILES}
run_command "Translate integer literals to enumerated ProjectileIDs..." $(translate_projectileids "${FILES}")
prompt_commit "${FILES}"
fi
fi
# BuffIDs
if [ -n "$opt_all" ] || [ -n "$opt_buffids" ]; then
FILES=$(find ${SOURCE_PATH} -name "BuffID.cs" -type f -print -quit)
FILES="${FILES[@]/%/ $(grep -E --include=\*.cs -irlw \
-e '(Main.buffName|Main.buffTip)\[[0-9]+\]' \
-e 'maxBuffTypes = [0-9]+' \
-e 'mountDatum\.buff = [0-9]+' \
-e 'Main\.(pvpBuff|persistentBuff|meleeBuff|debuff|buffName|buffTip|buffNoSave|buffNoTimeDisplay|buffDoubleApply|buffAlpha) = new (bool|string|float)\[[0-9]+\]' \
-e 'Main\.(debuff|pvpBuff|meleeBuff|buffNoSave|buffNoTimeDisplay|persistentBuff)\[[0-9]+\]' \
-e 'this\.buffType = [0-9]+' \
-e '(AddBuff|HasBuff)\([0-9]+' \
-e 'buffImmune = new bool\[[0-9]+\]' \
-e 'this\.buffImmune\[[0-9]+\]' \
-e 'buffType\[\w+\] (==|<=|>=|!=) [0-9]+' \
${SOURCE_PATH})}"
if [ -n "$FILES" ]; then
dos2unix -- ${FILES}
run_command "Translate integer literals to enumerated BuffIDs..." $(translate_buffids "${FILES}")
prompt_commit "${FILES}"
fi
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment