Replace metaforce_renames.sh with a python script

Former-commit-id: 4e8a8ed09e
This commit is contained in:
Henrique Gemignani Passos Lima 2022-10-23 16:43:51 +03:00
parent 8173a671c2
commit 057b66a82f
3 changed files with 81 additions and 17 deletions

View File

@ -41,6 +41,8 @@ Metaforce is a non-matching decompilation, and often uses modern C++ features th
### Converting types ### Converting types
The python script at `tool/metaforce_renames.py` automates many simple renames listed next, but not everything works perfectly.
Metaforce -> decomp Metaforce -> decomp
- `s8` -> `char` - `s8` -> `char`
- `u8` -> `uchar` - `u8` -> `uchar`

View File

@ -0,0 +1,79 @@
import re
import argparse
from pathlib import Path
_LITERAL_REPLACEMENTS = [
("std::string_view", "const rstl::string&"),
("std::vector", "rstl::vector"),
("std::list", "rstl::list"),
("std::pair", "rstl::pair"),
("std::optional", "rstl::optional_object"),
("std::nullopt", "rstl::optional_object_null()"),
("zeus::CTransform", "CTransform4f"),
("zeus::skZero2f", "CVector2f(0.f, 0.f)"),
("zeus::skZero3f", "CVector3f::Zero()"),
("zeus::skPurple", "CColor::Purple()"),
("zeus::skWhite", "CColor::White()"),
("zeus::CFrustum", "CFrustumPlanes"),
("zeus::lookAt", "CTransform4f::LookAt"),
("zeus::", ""),
("u32 ", "uint "),
("s32 ", "int "),
("std::min(", "rstl::min_val("),
("std::max(", "rstl::max_val("),
("DEFINE_ENTITY", ""),
("visitor.Visit(this)", "visitor.Visit(*this)"),
("g_SimplePool", "gpSimplePool"),
("g_Renderer", "gpRender"),
# Vector
(".magnitude()", ".Magnitude()"),
(".normalized()", ".AsNormalized()"),
# Transform
(".frontVector()", ".GetForward()"),
# Enums
("EMaterialTypes::", "kMT_"),
("CPlayer::EPlayerOrbitState::", "CPlayer::kOS_"),
("CPlayer::ESurfaceRestraints::", "CPlayer::kSR_"),
("CPlayer::EPlayerCameraState::", "CPlayer::kCS_"),
("CPlayer::EPlayerMorphBallState::", "CPlayer::kMS_"),
("CPlayer::EPlayerOrbitType::", "CPlayer::kOT_"),
("CPlayer::EPlayerOrbitRequest::", "CPlayer::kOR_"),
("CPlayer::EPlayerZoneInfo::", "CPlayer::kZI_"),
("CPlayer::EPlayerZoneType::", "CPlayer::kZT_"),
("CPlayer::EPlayerScanState::", "CPlayer::kSS_"),
("CPlayer::EGrappleState::", "CPlayer::kGS_"),
("CPlayer::EFootstepSfx::", "CPlayer::kFS_"),
("CPlayer::EGunHolsterState::", "CPlayer::kGH_"),
("CPlayerState::EPlayerVisor::", "CPlayerState::kPV_"),
("CPlayerState::EPlayerSuit::", "CPlayerState::kPS_"),
("CPlayerState::EBeamId::", "CPlayerState::kBI_"),
("CPlayerState::EItemType::", "CPlayerState::kIT_"),
]
_RE_REPLACEMENTS = [
(re.compile(r"SObjectTag{FOURCC\('(.*)'\),\s*(.*)}"), r"SObjectTag('\1', \2)"),
]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("file", type=Path)
args = parser.parse_args()
contents: str = args.file.read_text()
for sub, replacement in _LITERAL_REPLACEMENTS:
contents = contents.replace(sub, replacement)
for re_obj, replacement in _RE_REPLACEMENTS:
contents = re_obj.sub(replacement, contents)
args.file.write_text(contents)
if __name__ == "__main__":
main()

View File

@ -1,17 +0,0 @@
#!/bin/bash
set -e
path=$1
sed -i "s/std::string_view/const rstl::string\&/g" "$path"
sed -i "s/std::optional/rstl::optional_object/g" "$path"
sed -i "s/std::nullopt/rstl::optional_object_null()/g" "$path"
sed -i "s/zeus::CTransform/CTransform4f/g" "$path"
sed -i "s/zeus::skZero2f/CVector2f(0.f, 0.f)/g" "$path"
sed -i "s/zeus::skZero3f/CVector3f::Zero()/g" "$path"
sed -i "s/zeus::skPurple/CColor::Purple()/g" "$path"
sed -i "s/zeus::skWhite/CColor::White()/g" "$path"
sed -i "s/zeus::CFrustum/CFrustumPlanes/g" "$path"
sed -i "s/zeus:://g" "$path"
sed -i "s/DEFINE_ENTITY//g" "$path"
sed -i "s/visitor.Visit(this)/visitor.Visit(*this)/g" "$path"