From 17a33b2d1804e654e7af40de7125167a8ce65293 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Mon, 28 Sep 2026 18:11:06 -0500 Subject: [PATCH 01/34] Decompile iplAddressEdit to 72/94 functions (#240) * wip: salvaged iplAddressEdit (old luna, contains hacks to remove) * decompile address edit * convert address edit methods --- config/43U/symbols.txt | 66 +- include/keyboard/tiManager.h | 3 + include/math/iplMathTypes.h | 4 + include/scene/address/iplAddress.h | 3 + include/scene/address/iplAddressEdit.h | 129 +- include/system/iplKeyboard.h | 3 + src/scene/address/iplAddressEdit.cpp | 2924 +++++++++++++++++++++--- 7 files changed, 2751 insertions(+), 381 deletions(-) diff --git a/config/43U/symbols.txt b/config/43U/symbols.txt index 44359545..2ab7fbb1 100644 --- a/config/43U/symbols.txt +++ b/config/43U/symbols.txt @@ -13835,8 +13835,8 @@ scAnmTable__10homebutton = .rodata:0x8160F378; // type:object size:0x60 scope:lo scGroupAnmTable__10homebutton = .rodata:0x8160F3D8; // type:object size:0x250 scope:local data:4byte __DVDErrorMessage = .rodata:0x8160F628; // type:object size:0x28 lbl_8160F650 = .rodata:0x8160F650; // type:object size:0x16 data:2byte -lbl_8160F668 = .rodata:0x8160F668; // type:object size:0xB data:byte -lbl_8160F673 = .rodata:0x8160F673; // type:object size:0xC data:byte +specials$18760 = .rodata:0x8160F668; // type:object size:0xB data:byte +@19145 = .rodata:0x8160F673; // type:object size:0xC data:byte lbl_8160F67F = .rodata:0x8160F67F; // type:object size:0xD data:byte lbl_8160F690 = .rodata:0x8160F690; // type:object size:0x18 data:2byte scAnmFrame__Q23ipl5scene = .rodata:0x8160F6A8; // type:object size:0x20 scope:local @@ -15435,26 +15435,46 @@ __vt__Q33ipl5scene7Address = .data:0x81647B70; // type:object size:0x120 @11177 = .data:0x81647D9A; // type:object size:0xF scope:local data:string __vt__Q33ipl5scene18AddressAddSelEvent = .data:0x81647DAC; // type:object size:0x18 __vt__Q33ipl5scene13AddressAddSel = .data:0x81647DC4; // type:object size:0x88 -lbl_81647EE0 = .data:0x81647EE0; // type:object size:0xD data:string -lbl_81647EED = .data:0x81647EED; // type:object size:0xD data:string -lbl_81647EFA = .data:0x81647EFA; // type:object size:0xD data:string -lbl_81647F07 = .data:0x81647F07; // type:object size:0xE data:string -lbl_81647F15 = .data:0x81647F15; // type:object size:0xC data:string -lbl_81647F24 = .data:0x81647F24; // type:object size:0x14 -lbl_81647F38 = .data:0x81647F38; // type:object size:0xE data:string -lbl_81647F46 = .data:0x81647F46; // type:object size:0x83 -lbl_81647FC9 = .data:0x81647FC9; // type:object size:0xC data:string -lbl_81647FD5 = .data:0x81647FD5; // type:object size:0x128 -lbl_816480FD = .data:0x816480FD; // type:object size:0xE data:string -lbl_8164810B = .data:0x8164810B; // type:object size:0x1F5 -jumptable_81648300 = .data:0x81648300; // type:object size:0xC0 scope:local -jumptable_816483C0 = .data:0x816483C0; // type:object size:0xC0 scope:local -lbl_8164848D = .data:0x8164848D; // type:object size:0x16 data:string -lbl_816484A3 = .data:0x816484A3; // type:object size:0xF data:string -jumptable_816484B4 = .data:0x816484B4; // type:object size:0x44 scope:local -jumptable_81648508 = .data:0x81648508; // type:object size:0x58 scope:local -jumptable_81648560 = .data:0x81648560; // type:object size:0x8C scope:local -lbl_816485EC = .data:0x816485EC; // type:object size:0xC data:wstring +@16959 = .data:0x81647EE0; // type:object size:0xD data:string +@16960 = .data:0x81647EED; // type:object size:0xD data:string +@16961 = .data:0x81647EFA; // type:object size:0xD data:string +@16962 = .data:0x81647F07; // type:object size:0xE data:string +@16963 = .data:0x81647F15; // type:object size:0xC data:string +sButtonPaneNames = .data:0x81647F24; // type:object size:0x14 +@16964 = .data:0x81647F38; // type:object size:0xE data:string +@23886 = .data:0x81647F46; // type:object size:0xC data:string +@24207 = .data:0x81647F52; // type:object size:0x12 data:string +@24208 = .data:0x81647F64; // type:object size:0x1C data:string +@24209 = .data:0x81647F80; // type:object size:0xF data:string +@24210 = .data:0x81647F8F; // type:object size:0x19 data:string +@24211 = .data:0x81647FA8; // type:object size:0xB data:string +@24212 = .data:0x81647FB3; // type:object size:0xB data:string +@24213 = .data:0x81647FBE; // type:object size:0xB data:string +@24214 = .data:0x81647FC9; // type:object size:0xC data:string +@24215 = .data:0x81647FD5; // type:object size:0xE data:string +@24216 = .data:0x81647FE3; // type:object size:0x1A data:string +@24217 = .data:0x81647FFD; // type:object size:0x1A data:string +@24218 = .data:0x81648017; // type:object size:0x1D data:string +@24219 = .data:0x81648034; // type:object size:0x1E data:string +@24220 = .data:0x81648052; // type:object size:0xC data:string +@24221 = .data:0x8164805E; // type:object size:0x22 data:string +@24222 = .data:0x81648080; // type:object size:0x9 data:string +@24223 = .data:0x81648089; // type:object size:0x23 data:string +@24224 = .data:0x816480AC; // type:object size:0x1C data:string +@24225 = .data:0x816480C8; // type:object size:0xD data:string +@24226 = .data:0x816480D5; // type:object size:0xD data:string +@24227 = .data:0x816480E2; // type:object size:0xD data:string +@24228 = .data:0x816480EF; // type:object size:0xE data:string +@24229 = .data:0x816480FD; // type:object size:0xE data:string +@24231 = .data:0x8164810B; // type:object size:0x1F5 +@25188 = .data:0x81648300; // type:object size:0xC0 scope:local +@25187 = .data:0x816483C0; // type:object size:0xC0 scope:local +@25711 = .data:0x8164848D; // type:object size:0x16 data:string +@25835 = .data:0x816484A3; // type:object size:0xF data:string +@25838 = .data:0x816484B4; // type:object size:0x44 scope:local +@26007 = .data:0x81648508; // type:object size:0x58 scope:local +@26006 = .data:0x81648560; // type:object size:0x8C scope:local +@26030 = .data:0x816485EC; // type:object size:0xC data:wstring __vt__Q33ipl5scene17AddressInputEvent = .data:0x816485F8; // type:object size:0x18 __vt__Q33ipl5scene16AddressEditEvent = .data:0x81648610; // type:object size:0x18 __vt__Q33ipl5scene11AddressEdit = .data:0x81648628; // type:object size:0x120 @@ -21691,7 +21711,7 @@ lbl_816965C4 = .sdata:0x816965C4; // type:object size:0x6 data:string lbl_816965CA = .sdata:0x816965CA; // type:object size:0x4 smButtonName__Q33ipl5scene13AddressAddSel = .sdata:0x816965D0; // type:object size:0x8 @10971 = .sdata:0x816965D8; // type:object size:0x4 scope:local data:string -lbl_816965E0 = .sdata:0x816965E0; // type:object size:0x4 data:4byte +sInputPaneName = .sdata:0x816965E0; // type:object size:0x4 data:4byte lbl_816965E4 = .sdata:0x816965E4; // type:object size:0x4 data:string lbl_816965E8 = .sdata:0x816965E8; // type:object size:0x2 lbl_816965EA = .sdata:0x816965EA; // type:object size:0x6 data:string diff --git a/include/keyboard/tiManager.h b/include/keyboard/tiManager.h index 9e24205a..c18b8c29 100644 --- a/include/keyboard/tiManager.h +++ b/include/keyboard/tiManager.h @@ -67,6 +67,9 @@ namespace textinput { virtual void setAspectRatio(bool b4x3); virtual void setSecretInputMode(bool secretInputMode) { mpInputForm->setSecretModeOn(secretInputMode); } +#ifdef IPL_ADDRESS_EDIT_CPP + void enableKSXFilter(bool compatibleFilter); +#endif virtual void setDefaultPredictionJP(int num, const char** predicts); virtual void setFixedPredictionJP(int num, const char** predicts); diff --git a/include/math/iplMathTypes.h b/include/math/iplMathTypes.h index f1425545..0e6f9bd6 100644 --- a/include/math/iplMathTypes.h +++ b/include/math/iplMathTypes.h @@ -91,11 +91,15 @@ namespace ipl { y = r.y; z = 0.0f; } +#ifdef IPL_ADDRESS_EDIT_CPP + VEC3(f32 fx, f32 fy, f32 fz); +#else VEC3(f32 fx, f32 fy, f32 fz) { x = fx; y = fy; z = fz; } +#endif VEC3& operator+=(const VEC3& rhs) { VEC3Add(this, this, &rhs); diff --git a/include/scene/address/iplAddress.h b/include/scene/address/iplAddress.h index 8e767e17..8dea5df6 100644 --- a/include/scene/address/iplAddress.h +++ b/include/scene/address/iplAddress.h @@ -258,6 +258,9 @@ namespace ipl { static const math::VEC2 smcPageOffset; friend class AddressEvent; +#ifdef IPL_ADDRESS_EDIT_CPP + friend class AddressEdit; +#endif }; class AddressEvent : public ::gui::EventHandler { diff --git a/include/scene/address/iplAddressEdit.h b/include/scene/address/iplAddressEdit.h index 9bd7fb13..501c89bf 100644 --- a/include/scene/address/iplAddressEdit.h +++ b/include/scene/address/iplAddressEdit.h @@ -2,22 +2,125 @@ #define IPL_SCENE_ADDRESS_EDIT_H #include "iplSceneHeader.h" +#include "scene/button/iplButton.h" + +#include +#include namespace ipl { + namespace nigaoe { + class Object; + } + namespace scene { - FADER_SCENE_CLASS(AddressEdit) { + class AddressEditEvent; + class AddressInputEvent; + class FriendListCache; + + FADER_SCENE_CLASS(AddressEdit), public ButtonEventHandlerBase { public: AddressEdit(EGG::Heap * heap, int friendCode); + virtual ~AddressEdit(); + + virtual void prepare(); + virtual void create(); + virtual void draw(); + + virtual void initCalcNormal(); + virtual void initCalcFadeout(); + virtual void calcCommonAfter(); + + virtual FaderSceneCommand calcFadein(); + virtual FaderSceneCommand calcNormal(); + virtual FaderSceneCommand calcFadeout(); + virtual void onEventDerived(u32 compId, u32 event, const controller::Interface* con); void stt_msg_code_add(); void stt_msg_code_edit(); void stt_msg_parental(); void stt_msg_nwc24_error(); + void reset_gui(); + void start_point_event(const char* paneName, controller::Interface* controller); + void start_left_event(const char* paneName); + void start_trig_event(const char* paneName); + void start_ipt_point_event(const char* paneName, int channel); + void start_ipt_left_event(const char* paneName, int channel); + void start_ipt_trig_event(const char* paneName, int channel); + int get_button_no(const char*); + void add_friendinfo(); + void delete_friendinfo(); + void set_textbox(nw4r::lyt::Pane*, const wchar_t*); + void setDefaultTitleText(const wchar_t*, bool); + static void wiiid_utf16(u64, wchar_t*); + static void nigaoe_create_callback_add(ipl::nigaoe::Object*, void*); + static void nigaoe_create_callback_edit(ipl::nigaoe::Object*, void*); + void set_err_msg(wchar_t*, u32, NWC24Err); + void get_friendinfo(); + static u64 utf16_wiiid(const wchar_t*); + void stt_add_code_fadein(); + void stt_add_code_fadeout(); + void stt_add_code_input(); + void stt_add_code_normal(); + void stt_add_confirm_fadein(); + void stt_add_confirm_fadeout(); + void stt_add_confirm_normal(); + void stt_add_mii_fadein(); + void stt_add_mii_fadeout(); + void stt_add_mii_input(); + void stt_add_mii_normal(); + void stt_add_name_fadein(); + void stt_add_name_fadeout(); + void stt_add_name_input(); + void stt_add_name_normal(); + void stt_ipt_input(); + void stt_ipt_normal(); + void stt_ipt_wait_fadein(); + void stt_ipt_wait_fadeout(); + void stt_msg_add_rlt(); + void stt_msg_code_invalid(); + void stt_msg_del_rlt(); + void stt_msg_dup_email(); + void stt_msg_dup_wii_no(); + void stt_msg_my_wii_no(); + void stt_msg_net(); + void stt_msg_no_established(); + void stt_msg_no_mii(); + void stt_msg_no_mii_add(); + void stt_msg_wc(); + void stt_select_mii(); + void stt_wait_btn_fadein(); + void stt_wait_btn_fadeout(); + void stt_wait_del_msg_fadein(); + void stt_wait_del_msg_fadeout(); + void stt_wait_del_msg_fadeout_to_rlt(); + void stt_wait_delete(); + void stt_wait_decide_anm_add(); + void stt_wait_parental(); + void stt_wait_parental_dst(); + void stt_wait_parental_dst_wc(); + void stt_wait_parental_wc(); + void stt_normal(); + void stt_wait_decide_anm(); + void update_friendinfo(); + void requestSceneChange(int sceneId, void* args) { reserveSceneChange(sceneId, args); } + u32 getPreviousScene() const { return getPrevSceneID(); } + EGG::Heap* getHeap() { return getSceneHeap(); } + ::gui::Manager* getGuiManager() { return mpManager; } class String { public: void clear(); const wchar_t* getDispCodeLong() const; + void setName(const wchar_t* value); + void setEMail(const wchar_t* value); + void setWiiNo(const wchar_t* value); + + wchar_t mValue[0x102]; + wchar_t mName[0x0c]; + wchar_t mDisplayText[0x102]; + bool mbValidMail; + bool mbNameNotEmpty; + bool mbHasWiiNo; }; enum { @@ -25,8 +128,28 @@ namespace ipl { SCENE_ADD_EMAIL, }; - private: - u8 unk_0x58[0x498]; + int mState; + layout::Object* mpCodeLayout; + AddressEditEvent* mpEditEvent; + gui::PaneManager* mpEditGui; + layout::Object* mpNameLayout; + AddressInputEvent* mpInputEvent; + gui::PaneManager* mpInputGui; + layout::Object* mpBackgroundLayout; + int mMode; + int mSelectedButton; + int mSubState; + s32 mPointCount[5]; + u32 mSelectedFriend; + TextBalloon* mpBalloon; + nand::LayoutFile* mpBalloonFile; + String mString; + AddressEdit* mpCallbackOwner; + bool mbParentalOK; + nigaoe::Object* mpNigaoe; + RFLCreateID mCreateID; + int mNigaoeState; + FriendListCache* mpFriendCache; }; } // namespace scene } // namespace ipl diff --git a/include/system/iplKeyboard.h b/include/system/iplKeyboard.h index aa8c6df7..717d309d 100644 --- a/include/system/iplKeyboard.h +++ b/include/system/iplKeyboard.h @@ -54,6 +54,9 @@ namespace ipl { const wchar_t* wcString; // 0x04 u32 stringLimit; // 0x08 u32 rowLimit; // 0x0C +#ifdef IPL_ADDRESS_EDIT_CPP + KeyboardSetting(KeyboardType keyboardType, const wchar_t* value, u32 limit, u32 rows); +#endif } KeyboardSetting; typedef struct State { diff --git a/src/scene/address/iplAddressEdit.cpp b/src/scene/address/iplAddressEdit.cpp index 6971ad2b..791c5145 100644 --- a/src/scene/address/iplAddressEdit.cpp +++ b/src/scene/address/iplAddressEdit.cpp @@ -1,6 +1,18 @@ +#define IPL_ADDRESS_EDIT_CPP #include "scene/address/iplAddressEdit.h" +#include "scene/address/iplAddress.h" +#include "scene/board/iplBoard.h" +#include "scene/faceSelect/iplFaceSelect.h" +#include "scene/parentalDialog/iplParentalDialog.h" +#include "scene/iplSceneManager.h" +#include "layout/iplGuiManager.h" +#include "scene/textBalloon/iplBalloon.h" +#include "sound/iplSound.h" #include "system/iplSystem.h" +#include "system/iplNigaoe.h" +#include "scene/setting/iplNCDSetting.h" +#include "utility/iplCharacterCode.h" #include #include @@ -23,284 +35,125 @@ enum { static ChkHostNameError CheckHostName(const char* hostName, u32 hostNameLength); -u64 s_addressEditDataPool[269] = { - 0x425f6372645f6274, - 0x6e5f303000425f63, - 0x72645f62746e5f31, - 0x3000425f6372645f, - 0x62746e5f31310042, - 0x5f6d69695f69636f, - 0x6e5f303000425f63, - 0x6172645f62657461, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x425f6372645f6564, - 0x67695f3030006261, - 0x6c6c6f6f6e2e6173, - 0x680074685f416472, - 0x6573735f622e6272, - 0x6c79740074685f41, - 0x64726573735f625f, - 0x636172645f737472, - 0x742e62726c616e00, - 0x636172645f737472, - 0x745f666e73680074, - 0x685f416472657373, - 0x5f625f62746e5f69, - 0x6e2e62726c616e00, - 0x6372645f62746e5f, - 0x3030006372645f62, - 0x746e5f3130006372, - 0x645f62746e5f3131, - 0x006d69695f69636f, - 0x6e5f303000545f66, - 0x726e645f6372645f, - 0x30300074685f4164, - 0x726573735f625f62, - 0x746e5f7073682e62, - 0x726c616e0074685f, - 0x4164726573735f62, - 0x5f62746e5f6f7574, - 0x2e62726c616e0074, - 0x685f416472657373, - 0x5f625f62746e5f73, - 0x636c5f696e2e6272, - 0x6c616e0074685f41, - 0x64726573735f625f, - 0x62746e5f73636c5f, - 0x6f75742e62726c61, - 0x6e006372645f6274, - 0x6e5f677279007468, - 0x5f4164726573735f, - 0x625f636172645f6d, - 0x73675f616c705f69, - 0x6e2e62726c616e00, - 0x636172645f6d7367, - 0x0074685f41647265, - 0x73735f625f636172, - 0x645f6d73675f616c, - 0x705f6f75742e6272, - 0x6c616e0074685f41, - 0x64726573735f625f, - 0x636172645f666e73, - 0x682e62726c616e00, - 0x545f6372645f6274, - 0x6e5f303000545f63, - 0x72645f62746e5f31, - 0x3000545f6372645f, - 0x62746e5f31310054, - 0x5f6372645f62746e, - 0x5f67727900545f63, - 0x6172645f6d73675f, - 0x303000545f6e616d, - 0x655f30300074685f, - 0x4164726573735f63, - 0x2e62726c79740074, - 0x685f416472657373, - 0x5f635f636172645f, - 0x737472742e62726c, - 0x616e00475f636172, - 0x645f737472745f66, - 0x6e73680074685f41, - 0x64726573735f635f, - 0x7175657374696f6e, - 0x5f616c705f696e2e, - 0x62726c616e00475f, - 0x7175657374696f6e, - 0x5f30300074685f41, - 0x64726573735f635f, - 0x6e616d655f616c70, - 0x5f696e2e62726c61, - 0x6e00475f6e616d65, - 0x5f30300074685f41, - 0x64726573735f635f, - 0x6d73675f616c705f, - 0x696e2e62726c616e, - 0x00475f6d73675f30, - 0x300074685f416472, - 0x6573735f635f6d69, - 0x695f616c705f696e, - 0x2e62726c616e0074, - 0x685f416472657373, - 0x5f635f7175657374, - 0x696f6e5f616c705f, - 0x6f75742e62726c61, - 0x6e0074685f416472, - 0x6573735f635f6e61, - 0x6d655f616c705f6f, - 0x75742e62726c616e, - 0x0074685f41647265, - 0x73735f635f6d7367, - 0x5f616c705f6f7574, - 0x2e62726c616e0074, - 0x685f416472657373, - 0x5f635f6d69695f61, - 0x6c705f6f75742e62, - 0x726c616e0074685f, - 0x4164726573735f63, - 0x5f636172645f666e, - 0x73682e62726c616e, - 0x006d795f4261636b, - 0x5f612e62726c7974, - 0x006d795f4261636b, - 0x5f615f4170656172, - 0x2e62726c616e0050, - 0x6963747572655f30, - 0x30006d795f426163, - 0x6b5f615f4c6f7374, - 0x2e62726c616e0054, - 0x5f7175657374696f, - 0x6e5f303000545f6d, - 0x73675f3030006d79, - 0x5f49706c546f7042, - 0x616c6c6f6f6e5f61, - 0x2e62726c79740000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x545f6d69695f6d73, - 0x675f303000574950, - 0x4c5f53455f42545f, - 0x5441524745545449, - 0x4e47005749504c5f, - 0x53455f4445434944, - 0x4500000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x5749504c5f53455f, - 0x43414e43454c0000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000250030, - 0x00360064000a0000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, - 0x0000000000000000, +static const char* sButtonPaneNames[5] = { + "B_crd_btn_00", "B_crd_btn_10", "B_crd_btn_11", "B_mii_icon_00", "B_card_beta" }; -extern "C" char lbl_81647EE0; +static const char* sInputPaneName = "B_crd_edgi_00"; + +namespace ipl { + namespace scene { + class AddressEditEvent : public ::gui::EventHandler { + public: + AddressEditEvent(AddressEdit* parent) : mpParent(parent) {} + + virtual void onEvent(u32 componentId, u32 event, void* data); + + private: + AddressEdit* mpParent; + }; + + class AddressInputEvent : public ::gui::EventHandler { + public: + AddressInputEvent(AddressEdit* parent) : mpParent(parent) {} + + virtual void onEvent(u32 componentId, u32 event, void* data); + + private: + AddressEdit* mpParent; + }; + } +} + +extern "C" u8 __vt__Q33ipl5scene11AddressEdit[]; +extern "C" void __dl__FPv(void*); + +ipl::keyboard::Manager::KeyboardSetting::KeyboardSetting( + ipl::keyboard::Manager::KeyboardType keyboardType, const wchar_t* value, u32 limit, u32 rows) { + type = keyboardType; + wcString = value; + stringLimit = limit; + rowLimit = rows; +} + +ipl::scene::AddressEdit::AddressEdit(EGG::Heap* heap, int friendCode) + : FaderSceneBase(heap), ButtonEventHandlerBase() { + mMode = friendCode; + mSelectedButton = -1; + mSubState = -1; + mSelectedFriend = 0; + mpCallbackOwner = this; + mString.clear(); + mbParentalOK = false; + mpNigaoe = NULL; + mNigaoeState = 0; + setSceneParentFlags(3); + for (int i = 0; i < 5; ++i) { + mPointCount[i] = 0; + } + for (int i = 0; i < 8; ++i) { + mCreateID.data[i] = 0; + } +} + +ipl::scene::AddressEdit::~AddressEdit() { + if (mpNigaoe != NULL) { + delete mpNigaoe; + } +} + +void ipl::scene::AddressEdit::stt_msg_code_add() { + if (System::getDialog()->getLastResult() == DialogWindow::RESULT_BUTTON) { + mState = 0x22; + } +} + +void ipl::scene::AddressEdit::stt_msg_code_edit() { + if (System::getDialog()->getLastResult() == DialogWindow::RESULT_BUTTON) { + mState = 0; + } +} + +void ipl::scene::AddressEdit::stt_msg_parental() { + if (System::getDialog()->getLastResult() == DialogWindow::RESULT_BUTTON) { + mState = 0; + } +} + +void ipl::scene::AddressEdit::stt_msg_nwc24_error() { + if (System::getDialog()->getLastResult() == DialogWindow::RESULT_BUTTON) { + mState = 0; + } +} + +void ipl::scene::AddressEdit::reset_gui() { + mpEditGui->init(); + mpInputGui->init(); + for (s32 i = 0; i < 5; ++i) { + if (mPointCount[i] > 0) { + ipl::layout::Animator* animator = mpCodeLayout->getAnim(i + 0xb); + animator->initFrame(); + animator->restart(); + } + mPointCount[i] = 0; + } +} + + +extern "C" u8 __vt__Q33ipl5scene16AddressEditEvent[]; +extern "C" u8 __vt__Q33ipl5scene17AddressInputEvent[]; +extern "C" void* __nw__FUl(u32); +extern "C" BOOL RFLSearchOfficialData(const RFLCreateID*, u16*); + +extern "C" NWC24FriendInfo sFriendInfo__Q23ipl5scene; extern "C" char smArg__Q23ipl6System; extern "C" const char* smButtonName__Q33ipl5scene6Button[]; extern "C" void readLayoutAsync__Q33ipl4nand7ManagerFPQ23EGG4HeapPCcb(); extern "C" void List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs(); extern "C" void getScene__Q33ipl5scene7ManagerFi(); +extern "C" void UTF16ToANSI__Q33ipl7utility13CharacterCodeFPUcPCwl(); +extern "C" void reset_friend__Q33ipl5scene7AddressFv(); +extern "C" void callBtn1__Q23ipl12DialogWindowFUlUl(); extern "C" void setEventHandler__Q33ipl5scene6ButtonFPQ23gui12EventHandlerPQ23gui12EventHandler(); extern "C" void calc__Q33ipl5scene11TextBalloonFv(); extern "C" void calc__Q33ipl6layout6ObjectFv(); @@ -310,71 +163,195 @@ extern "C" void update__Q33ipl3gui11PaneManagerFv(); extern "C" void setOrtho__Q33ipl7utility8GraphicsFUl(); extern "C" void draw__Q33ipl6layout6ObjectFv(); extern "C" void draw__Q33ipl5scene11TextBalloonFv(); -extern "C" void callBtn1__Q23ipl12DialogWindowFUlUl(); + + + + + + + + + + +int ipl::scene::AddressEdit::get_button_no(const char*); extern "C" void callS2Btn2__Q23ipl12DialogWindowFUlUlb(); extern "C" void reserveText__Q33ipl5scene6ButtonFiUl(); extern "C" void reserveAnm__Q33ipl5scene6ButtonFi(); extern "C" void initFrame__Q33ipl7utility15FrameControllerFv(); extern "C" void getMessage__Q33ipl7message7MessageCFUl(); extern "C" void del__Q33ipl5scene15FriendListCacheFUl(); -extern "C" void reset_friend__Q33ipl5scene7AddressFv(); + + + + + + + + +extern "C" NWC24Err NWC24CheckPublicMailAddr_(const char*); extern "C" void __div2u(); extern "C" void __mod2u(); -extern "C" void UTF16ToANSI__Q33ipl7utility13CharacterCodeFPUcPCwl(); +extern "C" u64 utf16_wiiid__Q33ipl5scene11AddressEditFPCw(const wchar_t*); extern "C" void isValidId__Q33ipl5scene15FriendListCacheFRCUx(); extern "C" void isDupId__Q33ipl5scene15FriendListCacheFRCUx(); extern "C" void isDupMail__Q33ipl5scene15FriendListCacheFPCc(); -extern "C" u64 utf16_wiiid__Q33ipl5scene11AddressEditFPCw(const wchar_t*); + + + extern "C" void _savegpr_29(); extern "C" void _restgpr_29(); extern "C" void _savegpr_23(); -extern "C" void _savegpr_29(); extern "C" void _savegpr_27(); extern "C" void _restgpr_23(); -extern "C" void _restgpr_29(); extern "C" void _restgpr_27(); -void ipl::scene::AddressEdit::stt_msg_code_add() { - if (System::getDialog()->getLastResult() == DialogWindow::RESULT_BUTTON) { - *reinterpret_cast(reinterpret_cast(this) + 0x64) = 0x22; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +void ipl::scene::AddressEdit::stt_msg_no_established() { + switch (ipl::System::getDialog()->getLastResult()) { + case 1: + reset_gui(); + mState = 0; + break; + default: + break; } } -void ipl::scene::AddressEdit::stt_msg_code_edit() { - if (System::getDialog()->getLastResult() == DialogWindow::RESULT_BUTTON) { - *reinterpret_cast(reinterpret_cast(this) + 0x64) = 0; +void ipl::scene::AddressEdit::stt_msg_code_invalid() { + ipl::System::getScene(5); + switch (ipl::System::getDialog()->getLastResult()) { + case 1: + mState = 0x11; + break; + default: + break; } } -void ipl::scene::AddressEdit::stt_msg_parental() { - if (System::getDialog()->getLastResult() == DialogWindow::RESULT_BUTTON) { - *reinterpret_cast(reinterpret_cast(this) + 0x64) = 0; +void ipl::scene::AddressEdit::stt_msg_dup_wii_no() { + ipl::System::getScene(5); + switch (ipl::System::getDialog()->getLastResult()) { + case 1: + mState = 0x11; + break; + default: + break; } } -void ipl::scene::AddressEdit::stt_msg_nwc24_error() { - if (System::getDialog()->getLastResult() == DialogWindow::RESULT_BUTTON) { - *reinterpret_cast(reinterpret_cast(this) + 0x64) = 0; +void ipl::scene::AddressEdit::stt_msg_dup_email() { + ipl::System::getScene(5); + switch (ipl::System::getDialog()->getLastResult()) { + case 1: + mState = 0x11; + break; + default: + break; } } -extern "C" asm void __ct__Q43ipl8keyboard7Manager15KeyboardSettingFQ43ipl8keyboard7Manager12KeyboardTypePCwUlUl() { - nofralloc - stw r4, 0(r3) - stw r5, 4(r3) - stw r6, 8(r3) - stw r7, 0xc(r3) - blr +void ipl::scene::AddressEdit::stt_msg_my_wii_no() { + ipl::System::getScene(5); + switch (ipl::System::getDialog()->getLastResult()) { + case 1: + mState = 0x11; + break; + default: + break; + } +} + +void ipl::scene::AddressEdit::stt_msg_no_mii_add() { + ipl::System::getScene(5); + switch (ipl::System::getDialog()->getLastResult()) { + case 1: + mState = 0x1d; + break; + default: + break; + } +} + +void ipl::scene::AddressEdit::stt_msg_add_rlt() { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + switch (ipl::System::getDialog()->getLastResult()) { + case 1: + button->reserveAnm(0xf); + mState = 0x30; + break; + default: + break; + } +} + +void ipl::scene::AddressEdit::stt_add_code_normal() { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + if (button->isActive()) { + button->update(); + } + if (mState == 0x11) { + mpInputGui->update(); + } +} + +void ipl::scene::AddressEdit::stt_add_name_normal() { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + if (button->isActive()) { + button->update(); + } + if (mState == 0x19) { + mpInputGui->update(); + } } const wchar_t* ipl::scene::AddressEdit::String::getDispCodeLong() const { - if (reinterpret_cast(this)[0x422] == 0) { + if (!mbHasWiiNo) { return; } - return reinterpret_cast(reinterpret_cast(this) + 0x21c); + return mDisplayText; } -extern "C" asm void getName__Q33ipl6nigaoe6ObjectCFv() { +extern "C" asm const wchar_t* getName__Q33ipl6nigaoe6ObjectCFv(void*) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -402,15 +379,33 @@ extern "C" asm void getInputForm__Q29textinput7ManagerFv() { } void ipl::scene::AddressEdit::String::clear() { - memset(this, 0, 0x204); - memset(reinterpret_cast(this) + 0x204, 0, 0x18); - memset(reinterpret_cast(this) + 0x21c, 0, 0x204); - reinterpret_cast(this)[0x420] = 0; - reinterpret_cast(this)[0x421] = 0; - reinterpret_cast(this)[0x422] = 0; + memset(mValue, 0, sizeof(mValue)); + memset(mName, 0, sizeof(mName)); + memset(mDisplayText, 0, sizeof(mDisplayText)); + mbValidMail = false; + mbNameNotEmpty = false; + mbHasWiiNo = false; } -extern "C" asm void setWiiNo__Q43ipl5scene11AddressEdit6StringFPCw() { +void ipl::scene::AddressEdit::String::setEMail(const wchar_t* value) { + mbHasWiiNo = false; + memset(mValue, 0, sizeof(mValue)); + wcsncpy(mValue, value, 0xff); + u32 length = wcslen(value); + memset(mDisplayText, 0, sizeof(mDisplayText)); + if (length > 0x10) { + wcsncpy(mDisplayText, value, 0xe); + wcscpy(mDisplayText + 0xe, L"..."); + } else { + wcscpy(mDisplayText, value); + } + u8 address[0x100]; + memset(address, 0, sizeof(address)); + ipl::utility::CharacterCode::UTF16ToANSI(address, value, sizeof(address)); + mbValidMail = !NWC24CheckPublicMailAddr_(reinterpret_cast(address)); +} + +extern "C" asm void setWiiNo__Q43ipl5scene11AddressEdit6StringFPCw(void*, const wchar_t*) { nofralloc stwu r1, -0x20(r1) mflr r0 @@ -491,7 +486,7 @@ setWiiNo_L4: blr } -extern "C" asm void isDupCode__Q43ipl5scene11AddressEdit6StringCFv() { +extern "C" asm int isDupCode__Q43ipl5scene11AddressEdit6StringCFv(void*) { nofralloc stwu r1, -0x120(r1) mflr r0 @@ -530,7 +525,7 @@ isDupCode_L2: blr } -extern "C" asm void isMyCode__Q43ipl5scene11AddressEdit6StringCFv() { +extern "C" asm int isMyCode__Q43ipl5scene11AddressEdit6StringCFv(void*) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -562,27 +557,946 @@ isMyCode_L2: blr } -extern "C" asm void prepare__Q33ipl5scene11AddressEditFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - lis r4, smArg__Q23ipl6System@ha - lis r5, lbl_81647EE0@ha - stw r0, 0x14(r1) - addi r4, r4, smArg__Q23ipl6System@l - addi r5, r5, lbl_81647EE0@l - li r6, 0 - stw r31, 0xc(r1) - mr r31, r3 - lwz r3, 0x6c(r4) - lwz r4, 0x24(r31) - bl readLayoutAsync__Q33ipl4nand7ManagerFPQ23EGG4HeapPCcb - stw r3, 0xac(r31) - lwz r31, 0xc(r1) - lwz r0, 0x14(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::prepare() { + mpBalloonFile = ipl::System::getNandManager()->readLayoutAsync( + getSceneHeap(), "balloon.ash", false); +} + +void ipl::scene::AddressEdit::create() { + + ipl::layout::Object* bLayout; + ipl::scene::Board* board = static_cast(ipl::System::getScene(4)); + ipl::nand::LayoutFile* boardFile = board->getLayoutFile(); + + bLayout = reinterpret_cast(__nw__FUl(0x580)); + if (bLayout != NULL) { + bLayout = new (bLayout) ipl::layout::Object(getHeap(), boardFile, "arc", "th_Adress_b.brlyt"); + } + bLayout->bindToGroup("th_Adress_b_card_strt.brlan", "card_strt_fnsh", false, true); + bLayout->bindToGroup("th_Adress_b_btn_in.brlan", "crd_btn_00", false, true); + bLayout->bindToGroup("th_Adress_b_btn_in.brlan", "crd_btn_10", false, false); + bLayout->bindToGroup("th_Adress_b_btn_in.brlan", "crd_btn_11", false, false); + bLayout->bindToGroup("th_Adress_b_btn_in.brlan", "mii_icon_00", false, false); + bLayout->bind("th_Adress_b_btn_in.brlan", "T_frnd_crd_00", false, false); + bLayout->bindToGroup("th_Adress_b_btn_psh.brlan", "crd_btn_00", false, false); + bLayout->bindToGroup("th_Adress_b_btn_psh.brlan", "crd_btn_10", false, false); + bLayout->bindToGroup("th_Adress_b_btn_psh.brlan", "crd_btn_11", false, false); + bLayout->bindToGroup("th_Adress_b_btn_psh.brlan", "mii_icon_00", false, false); + bLayout->bind("th_Adress_b_btn_psh.brlan", "T_frnd_crd_00", false, false); + bLayout->bindToGroup("th_Adress_b_btn_out.brlan", "crd_btn_00", false, false); + bLayout->bindToGroup("th_Adress_b_btn_out.brlan", "crd_btn_10", false, true); + bLayout->bindToGroup("th_Adress_b_btn_out.brlan", "crd_btn_11", false, true); + bLayout->bindToGroup("th_Adress_b_btn_out.brlan", "mii_icon_00", false, true); + bLayout->bind("th_Adress_b_btn_out.brlan", "T_frnd_crd_00", false, true); + bLayout->bindToGroup("th_Adress_b_btn_scl_in.brlan", "crd_btn_00", false, false); + bLayout->bindToGroup("th_Adress_b_btn_scl_in.brlan", "crd_btn_10", false, false); + bLayout->bindToGroup("th_Adress_b_btn_scl_in.brlan", "crd_btn_11", false, false); + bLayout->bindToGroup("th_Adress_b_btn_scl_in.brlan", "mii_icon_00", false, false); + bLayout->bind("th_Adress_b_btn_scl_in.brlan", "T_frnd_crd_00", false, false); + bLayout->bindToGroup("th_Adress_b_btn_scl_out.brlan", "crd_btn_00", false, true); + bLayout->bindToGroup("th_Adress_b_btn_scl_out.brlan", "crd_btn_10", false, true); + bLayout->bindToGroup("th_Adress_b_btn_scl_out.brlan", "crd_btn_11", false, true); + bLayout->bindToGroup("th_Adress_b_btn_scl_out.brlan", "mii_icon_00", false, true); + bLayout->bind("th_Adress_b_btn_scl_out.brlan", "T_frnd_crd_00", false, true); + bLayout->bindToGroup("th_Adress_b_btn_scl_in.brlan", "crd_btn_gry", false, true); + bLayout->bindToGroup("th_Adress_b_btn_scl_out.brlan", "crd_btn_gry", false, false); + bLayout->bindToGroup("th_Adress_b_card_msg_alp_in.brlan", "card_msg", false, true); + bLayout->bindToGroup("th_Adress_b_card_msg_alp_out.brlan", "card_msg", false, false); + bLayout->bindToGroup("th_Adress_b_card_fnsh.brlan", "card_strt_fnsh", false, false); + bLayout->finishBinding(); + + ipl::scene::AddressEditEvent* editEvent = static_cast(__nw__FUl(sizeof(ipl::scene::AddressEditEvent))); + if (editEvent != NULL) { + new (editEvent) ipl::scene::AddressEditEvent(this); + } + void* editManagerMemory = __nw__FUl(0x34); + ipl::gui::PaneManager* editManager = reinterpret_cast(editManagerMemory); + if (editManagerMemory != NULL) { + editManager = new (editManagerMemory) ipl::gui::PaneManager(static_cast< ::gui::EventHandler*>(editEvent), bLayout->getDrawInfo(), NULL, NULL, false); + } + editManager->setupScene(bLayout); + editManager->setAllComponentTriggerTarget(false); + for (s32 i = 0; i < 5; i++) { + const char* paneName = sButtonPaneNames[i]; + nw4r::lyt::Pane* pane = bLayout->getNW4RLyt()->GetRootPane()->FindPaneByName(paneName, true); + editManager->setTriggerTarget(pane, true); + } + mpCodeLayout = bLayout; + mpEditEvent = editEvent; + mpEditGui = editManager; + + nw4r::lyt::Pane* textPane = bLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_crd_btn_00", true); + set_textbox(textPane, ipl::System::getMessage(0x2a)); + textPane = (mpCodeLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_crd_btn_10", true); + set_textbox(textPane, ipl::System::getMessage(0x2b)); + textPane = (mpCodeLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_crd_btn_11", true); + set_textbox(textPane, ipl::System::getMessage(0x2f)); + textPane = (mpCodeLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_crd_btn_gry", true); + set_textbox(textPane, ipl::System::getMessage(0x2a)); + textPane = (mpCodeLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_card_msg_00", true); + set_textbox(textPane, L""); + textPane = (mpCodeLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_name_00", true); + set_textbox(textPane, L""); + textPane = (mpCodeLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_frnd_crd_00", true); + set_textbox(textPane, L""); + + ipl::layout::Object* cLayout = reinterpret_cast(__nw__FUl(0x580)); + if (cLayout != NULL) { + cLayout = new (cLayout) ipl::layout::Object(getHeap(), boardFile, "arc", "th_Adress_c.brlyt"); + } + cLayout->bindToGroup("th_Adress_c_card_strt.brlan", "G_card_strt_fnsh", false, true); + cLayout->bindToGroup("th_Adress_c_question_alp_in.brlan", "G_question_00", false, true); + cLayout->bindToGroup("th_Adress_c_name_alp_in.brlan", "G_name_00", false, true); + cLayout->bindToGroup("th_Adress_c_msg_alp_in.brlan", "G_msg_00", false, true); + cLayout->bindToGroup("th_Adress_c_mii_alp_in.brlan", "G_mii", false, true); + cLayout->bindToGroup("th_Adress_c_question_alp_out.brlan", "G_question_00", false, false); + cLayout->bindToGroup("th_Adress_c_name_alp_out.brlan", "G_name_00", false, false); + cLayout->bindToGroup("th_Adress_c_msg_alp_out.brlan", "G_msg_00", false, false); + cLayout->bindToGroup("th_Adress_c_mii_alp_out.brlan", "G_mii", false, false); + cLayout->bindToGroup("th_Adress_c_card_fnsh.brlan", "G_card_strt_fnsh", false, false); + cLayout->finishBinding(); + + ipl::scene::AddressInputEvent* inputEvent = static_cast(__nw__FUl(sizeof(ipl::scene::AddressInputEvent))); + if (inputEvent != NULL) { + new (inputEvent) ipl::scene::AddressInputEvent(this); + } + void* inputManagerMemory = __nw__FUl(0x34); + ipl::gui::PaneManager* inputManager = reinterpret_cast(inputManagerMemory); + if (inputManagerMemory != NULL) { + inputManager = new (inputManagerMemory) ipl::gui::PaneManager(static_cast< ::gui::EventHandler*>(inputEvent), cLayout->getDrawInfo(), NULL, NULL, false); + } + inputManager->setupScene(cLayout); + inputManager->setAllComponentTriggerTarget(false); + textPane = cLayout->getNW4RLyt()->GetRootPane()->FindPaneByName(sInputPaneName, true); + inputManager->setTriggerTarget(textPane, true); + mpNameLayout = cLayout; + mpInputEvent = inputEvent; + mpInputGui = inputManager; + + ipl::layout::Object* background = reinterpret_cast(__nw__FUl(0x580)); + if (background != NULL) { + background = new (background) ipl::layout::Object(getHeap(), boardFile, "arc", "my_Back_a.brlyt"); + } + mpBackgroundLayout = background; + background->bind("my_Back_a_Apear.brlan", "Picture_00", false, true); + (mpBackgroundLayout) + ->bind("my_Back_a_Lost.brlan", "Picture_00", false, false); + (mpBackgroundLayout)->finishBinding(); + (mpBackgroundLayout) + ->getAnim(0)->initAnmFrame(); + + mString.clear(); + memset(&sFriendInfo__Q23ipl5scene, 0, sizeof(sFriendInfo__Q23ipl5scene)); + mpNigaoe = 0; + mNigaoeState = 0; + ipl::scene::SceneObj* addressScene = ipl::System::getScene(0x14); + ipl::scene::Address* address = static_cast(addressScene); + mpFriendCache = address->getFriendCache(); + + s32 mode = mMode; + ipl::layout::Animator* animator; + switch (mode) { + case 0: { + mSelectedFriend = address->getChosenFriendIndex(); + get_friendinfo(); + u32 friendIndex = mSelectedFriend; + if (mpFriendCache->getInfo(friendIndex).attr.status == 2) { + (mpCodeLayout)->getAnim(0x15)->initAnmFrame(); + (mpCodeLayout)->getAnim(0x1a)->initAnmFrame(); + } else { + (mpCodeLayout)->getAnim(0x10)->initAnmFrame(); + (mpCodeLayout)->getAnim(0x1b)->initAnmFrame(); + } + animator = (mpCodeLayout)->getAnim(0); + animator->initFrame(); + const wchar_t* friendText = mString.mName; + animator->restart(); + textPane = (mpCodeLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_name_00", true); + set_textbox(textPane, friendText); + textPane = (mpCodeLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_frnd_crd_00", true); + set_textbox(textPane, mString.mDisplayText); + mState = 0; + break; + } + case 1: { + animator = (mpNameLayout)->getAnim(0); + animator->initFrame(); + animator->restart(); + animator = (mpNameLayout)->getAnim(1); + animator->initFrame(); + animator->restart(); + animator = (mpNameLayout)->getAnim(3); + animator->initFrame(); + animator->restart(); + (mpCodeLayout)->getAnim(0x1a)->initAnmFrame(); + (mpCodeLayout)->getAnim(0x10)->initAnmFrame(); + textPane = (mpNameLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_question_00", true); + set_textbox(textPane, ipl::System::getMessage(0x31)); + textPane = (mpNameLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_msg_00", true); + set_textbox(textPane, ipl::System::getMessage(0x47)); + mState = 0x10; + break; + } + case 2: { + animator = (mpNameLayout)->getAnim(0); + animator->initFrame(); + animator->restart(); + animator = (mpNameLayout)->getAnim(1); + animator->initFrame(); + animator->restart(); + animator = (mpNameLayout)->getAnim(3); + animator->initFrame(); + animator->restart(); + (mpCodeLayout)->getAnim(0x1a)->initAnmFrame(); + (mpCodeLayout)->getAnim(0x10)->initAnmFrame(); + textPane = (mpNameLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_question_00", true); + set_textbox(textPane, ipl::System::getMessage(0x3f)); + textPane = (mpNameLayout) + ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_msg_00", true); + set_textbox(textPane, ipl::System::getMessage(0x48)); + mState = 0x10; + break; + } + default: + break; + } + +create_mode_done: + + s16 miiIndex[2]; + if (RFLSearchOfficialData(&mCreateID, + reinterpret_cast(miiIndex))) { + ipl::System::getMiiManager()->create(ipl::System::getMem2App(), 0x4c, 0x4c, miiIndex[0], + ipl::scene::AddressEdit::nigaoe_create_callback_edit, this); + mNigaoeState = 1; + } + + ipl::System::getKeyboard()->init(); + + ipl::scene::TextBalloon* balloon = reinterpret_cast(__nw__FUl(0x3c)); + if (balloon != NULL) { + f32 balloonWidth = 30.0f; + f32 balloonHeight = 120.0f; + EGG::Heap* heap = getHeap(); + ipl::math::VEC3 position(0.0f, 0.0f, 0.0f); + balloon = new (balloon) ipl::scene::TextBalloon(heap, mpBalloonFile, "arc", "my_IplTopBalloon_a.brlyt", position, balloonHeight, balloonWidth); + } + mpBalloon = balloon; +} + +void ipl::scene::AddressEdit::stt_wait_decide_anm() { + bool complete = true; + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + for (s32 i = 0; i < 5; ++i) { + u16 index = static_cast(mSelectedButton + 6); + complete = complete && !mpCodeLayout->getAnim(index)->isPlaying(); + } + if (!complete) { + return; + } + + switch (mSelectedButton) { + case 2: { + u32 friendIndex = mSelectedFriend; + ipl::layout::Animator* animator; + if (mpFriendCache->getInfo(friendIndex).attr.status == 2) { + animator = mpCodeLayout->getAnim(0x15); + } else { + animator = mpCodeLayout->getAnim(0x1b); + } + animator->initFrame(); + animator->restart(); + animator = mpCodeLayout->getAnim(0x16); + animator->initFrame(); + animator->restart(); + animator = mpCodeLayout->getAnim(0x17); + animator->initFrame(); + animator->restart(); + button->reserveAnm(0xc); + mState = 3; + mpBalloon->fadeoutForce(); + reset_gui(); + break; + } + case 1: { + button->reserveAnm(0xc); + button->reserveText(1, 0x2e); + button->reserveAnm(0xf); + ipl::layout::Animator* animator = mpNameLayout->getAnim(0); + animator->initFrame(); + animator->restart(); + animator = mpNameLayout->getAnim(1); + animator->initFrame(); + animator->restart(); + animator = mpNameLayout->getAnim(2); + animator->initFrame(); + animator->restart(); + mpNameLayout->getAnim(3)->initAnmFrame(); + animator = mpBackgroundLayout->getAnim(0); + animator->initFrame(); + animator->restart(); + nw4r::lyt::Pane* textPane = mpNameLayout->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_question_00", true); + set_textbox(textPane, ipl::System::getMessage(0x32)); + textPane = mpNameLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_name_00", true); + set_textbox(textPane, mString.mName); + textPane = mpNameLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_msg_00", true); + set_textbox(textPane, ipl::System::getMessage(0x49)); + mpBalloon->fadeoutForce(); + reset_gui(); + mState = 0xc; + break; + } + case 3: { + if (mNigaoeState != 1) { + mNigaoeState = 0; + if (RFLGetAvailableOfficialDataNum() != 0) { + button->reserveAnm(0xc); + button->reserveAnm(0xb); + createChildScene(0x1c, this, 0, reinterpret_cast(1)); + mState = 0xb; + } else { + ipl::System::getDialog()->callBtn1(0x17c, 0x2e); + mState = 0xa; + } + mpBalloon->fadeoutForce(); + } + break; + } + case 0: { + SCParentalControlsInfo parentalInfo; + BOOL parentalResult = SCGetParentalControl(&parentalInfo); + if (!ipl::ncd::NCDSetting::getConnectEnableFlag()) { + button->animation(0x1d); + ipl::System::getDialog()->callBtn2(0x144, 0x146, 0x25, false); + mState = 0x27; + } else if ((SCGetWCFlags() & 1) == 0) { + button->animation(0x1d); + ipl::System::getDialog()->callBtn2(0x17e, 0x146, 0x25, false); + mState = 0x2a; + } else if (parentalResult != 0 && (parentalInfo.enable & 0x80) != 0 && + (SCGetNetContentRestrictions() & 2) != 0) { + button->animation(0x1d); + ipl::System::getDialog()->callBtn1(0x14c, 0x2e); + mState = 0x2d; + } else { + s32 friendError = mpFriendCache->check(); + if (friendError == NWC24_ERR_NETWORK || + mpFriendCache->getLastErr() == NWC24_ERR_SERVER || + mpFriendCache->getLastErr() == NWC24_ERR_FULL) { + wchar_t errorMessage[0x400]; + wchar_t* errorMessagePointer = errorMessage - 1; + for (s32 i = 0; i < 0x200; i++) { + errorMessagePointer[1] = 0; + errorMessagePointer += 2; + *errorMessagePointer = 0; + } + set_err_msg(errorMessage, 0x400, + static_cast(mpFriendCache->getLastErr())); + ipl::System::getDialog()->callBtn1(errorMessage, 0x2e); + button->animation(0x1d); + mState = 0x2e; + } else { + button->reserveAnm(0xc); + button->reserveText(1, 0x27); + button->reserveText(0, 0x4f); + button->reserveAnm(0xf); + requestSceneChange(0xb, reinterpret_cast(1)); + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0x1e); + animator->initFrame(); + animator->restart(); + mState = 0x30; + } + } + mpBalloon->fadeoutForce(); + reset_gui(); + break; + } + case 4: { + ipl::System::getDialog()->callBtn1(mString.getDispCodeLong(), 0x2e); + mpBalloon->fadeoutForce(); + reset_gui(); + mState = 9; + break; + } + default: + mState = 0; + break; + } +} + +void ipl::scene::AddressEdit::stt_wait_delete() { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + switch (ipl::System::getDialog()->getLastResult()) { + case 2: { + delete_friendinfo(); + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0x1d); + animator->initFrame(); + animator->restart(); + mState = 6; + break; + } + case 1: { + button->reserveAnm(0xb); + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0x1d); + animator->initFrame(); + animator->restart(); + mState = 5; + break; + } + default: + break; + } +} + +void ipl::scene::AddressEdit::stt_add_confirm_fadein() { + if (!mpCodeLayout->getAnim(0)->isPlaying()) { + mState = 0x22; + } +} + +void ipl::scene::AddressEdit::stt_add_name_fadein() { + if (!mpNameLayout->getAnim(1)->isPlaying() && + !mpNameLayout->getAnim(3)->isPlaying() && + !mpNameLayout->getAnim(2)->isPlaying()) { + mState = 0x19; + } +} + +void ipl::scene::AddressEdit::stt_add_mii_fadein() { + if (!mpNameLayout->getAnim(1)->isPlaying() && + !mpNameLayout->getAnim(4)->isPlaying() && + !mpNameLayout->getAnim(0)->isPlaying()) { + mState = 0x1d; + } +} + +void ipl::scene::AddressEdit::stt_add_code_fadein() { + if (!mpNameLayout->getAnim(0)->isPlaying() && + !mpNameLayout->getAnim(1)->isPlaying() && + !mpNameLayout->getAnim(3)->isPlaying() && + !mpNameLayout->getAnim(4)->isPlaying() && + !mpNameLayout->getAnim(2)->isPlaying()) { + mState = 0x11; + } +} + +void ipl::scene::AddressEdit::stt_add_name_input() { + ipl::keyboard::Manager::State* state = ipl::System::getKeyboard()->getState(); + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + s32 keyboardType = state->iplType; + if (keyboardType == 3) { + goto state_disappearing; + } + if (keyboardType >= 3) { + goto state_at_or_after_disappearing; + } + if (keyboardType >= 2) { + goto done; + } + if (keyboardType >= 0) { + goto state_hidden; + } + goto done; + +state_at_or_after_disappearing: + if (keyboardType >= 5) { + goto done; + } + goto state_visible; + +state_disappearing: { + bool nameWasEmpty = mString.mName[0] == 0; + if (state->pressOK) { + mString.setName(state->wcString); + nw4r::lyt::Pane* pane = mpNameLayout->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_name_00", true); + set_textbox(pane, mString.mName); + } + if (mString.mName[0] == 0) { + if (!nameWasEmpty) { + ipl::layout::Animator* animator = mpNameLayout->getAnim(6); + animator->initFrame(); + animator->restart(); + animator = mpNameLayout->getAnim(3); + animator->initFrame(); + animator->restart(); + } + button->reserveAnm(0xb); + } else { + if (nameWasEmpty) { + ipl::layout::Animator* animator = mpNameLayout->getAnim(2); + animator->initFrame(); + animator->restart(); + animator = mpNameLayout->getAnim(7); + animator->initFrame(); + animator->restart(); + } + if (mString.mbNameNotEmpty) { + button->reserveText(1, 0x2e); + button->reserveAnm(0xf); + } else { + button->reserveAnm(0xb); + } + } + } + goto done; + +state_visible: { + mState = 0x19; + if (static_cast(ipl::System::getRegion()) == 6) { + ipl::System::getKeyboard()->baseMgr()->enableKSXFilter(false); + } + } + goto done; + +state_hidden: + setDefaultTitleText(NULL, ipl::System::getKeyboard()->baseMgr()->isVacancy()); + +done: + return; +} + +void ipl::scene::AddressEdit::stt_add_mii_normal() { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + if (button->isActive()) { + button->update(); + } + if (mState == 0x1d) { + mpInputGui->update(); + } +} + +void ipl::scene::AddressEdit::stt_add_mii_input() { + ipl::scene::Base* child = getChild(); + if (child != NULL) { + ipl::scene::FaceSelect* faceSelect = static_cast(child); + s32 faceId = faceSelect->getSelectedFaceId(); + if (faceId >= 0 && ipl::System::getMiiManager()->isAvalable(faceId) && + mNigaoeState == 0) { + RFLAdditionalInfo additionalInfo; + RFLGetAdditionalInfo(&additionalInfo, RFLDataSource_Official, NULL, faceId); + if (!RFLiIsSameID(&mCreateID, &additionalInfo.createID)) { + mCreateID = additionalInfo.createID; + ipl::System::getMiiManager()->create(ipl::System::getMem2App(), 0x4c, 0x4c, faceId, + ipl::scene::AddressEdit::nigaoe_create_callback_add, + this); + mNigaoeState = 1; + mpInputGui->init(); + mpBalloon->fadeoutForce(); + } + } + } + + if (getChild() == NULL && + ipl::System::getSceneManager()->getReservedScene() == NULL) { + static_cast(ipl::System::getScene(5))->setEventHandler(static_cast< ::gui::EventHandler*>(this), NULL); + mState = 0x1d; + mNigaoeState = 0; + } +} + +void ipl::scene::AddressEdit::stt_add_mii_fadeout() { + if (mpNameLayout->getAnim(9)->isPlaying() || + mpNameLayout->getAnim(5)->isPlaying() || + mpNameLayout->getAnim(8)->isPlaying()) { + return; + } + + if (mSubState == 5) { + ipl::layout::Animator* animator = mpNameLayout->getAnim(1); + animator->initFrame(); + animator->restart(); + animator = mpNameLayout->getAnim(2); + animator->initFrame(); + animator->restart(); + + nw4r::lyt::Pane* pane = + mpNameLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_question_00", true); + set_textbox(pane, ipl::System::getMessage(0x32)); + pane = mpNameLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_name_00", true); + set_textbox(pane, mString.mName); + pane = mpNameLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_msg_00", true); + set_textbox(pane, ipl::System::getMessage(0x49)); + mState = 0x18; + return; + } + + if (mSubState == 7) { + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0); + animator->initFrame(); + animator->restart(); + mpCodeLayout->getAnim(0x10)->initAnmFrame(); + mpCodeLayout->getAnim(0x11)->initAnmFrame(); + mpCodeLayout->getAnim(0x12)->initAnmFrame(); + animator = mpCodeLayout->getAnim(0x1c); + animator->initFrame(); + animator->restart(); + + nw4r::lyt::Pane* pane = + mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_card_msg_00", true); + set_textbox(pane, ipl::System::getMessage(0x44)); + pane = mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_name_00", true); + set_textbox(pane, mString.mName); + pane = mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_frnd_crd_00", true); + set_textbox(pane, mString.mDisplayText); + mState = 0x21; + } +} + +void ipl::scene::AddressEdit::stt_ipt_wait_fadeout() { + if (!mpNameLayout->getAnim(5)->isPlaying() && + !mpNameLayout->getAnim(7)->isPlaying() && + !mpNameLayout->getAnim(6)->isPlaying() && + !mpNameLayout->getAnim(8)->isPlaying() && + !mpNameLayout->getAnim(9)->isPlaying() && + !mpBackgroundLayout->getAnim(1)->isPlaying()) { + mState = 0; + } +} + +void ipl::scene::AddressEdit::stt_wait_del_msg_fadeout() { + if (!mpCodeLayout->getAnim(0x1d)->isPlaying()) { + u32 friendIndex = mSelectedFriend; + u32 status = mpFriendCache->getInfo(friendIndex).attr.status; + ipl::layout::Animator* animator; + if (status == 2) { + animator = mpCodeLayout->getAnim(0x10); + } else { + animator = mpCodeLayout->getAnim(0x1a); + } + animator->initFrame(); + animator->restart(); + animator = mpCodeLayout->getAnim(0x11); + animator->initFrame(); + animator->restart(); + animator = mpCodeLayout->getAnim(0x12); + animator->initFrame(); + animator->restart(); + mState = 2; + } +} + +void ipl::scene::AddressEdit::stt_msg_no_mii() { + switch (ipl::System::getDialog()->getLastResult()) { + case 1: + mpEditGui->init(); + for (u32 i = 0; i < 5; ++i) { + mPointCount[i] = 0; + } + mState = 0; + break; + default: + break; + } +} + +void ipl::scene::AddressEdit::stt_add_confirm_fadeout() { + if (!mpCodeLayout->getAnim(0x1e)->isPlaying()) { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + switch (mSubState) { + case 5: { + ipl::layout::Animator* animator = mpNameLayout->getAnim(0); + animator->initFrame(); + animator->restart(); + mState = 0x1c; + break; + } + case 7: + add_friendinfo(); + button->reserveText(1, 0x29); + static_cast(ipl::System::getScene(0x14))->reset_friend(); + ipl::System::getDialog()->callBtn1(0x4a, 0x2e); + mState = 0x26; + break; + default: + break; + } + } +} + +void ipl::scene::AddressEdit::stt_wait_decide_anm_add() { + bool complete = true; + for (s32 i = 0; i < 5; ++i) { + u16 index = static_cast(mSelectedButton + 6); + complete = (complete & !mpCodeLayout->getAnim(index)->isPlaying()) != 0; + } + if (complete) { + switch (mSelectedButton) { + case 4: + mpEditGui->init(); + ipl::System::getDialog()->callBtn1(mString.getDispCodeLong(), 0x2e); + mState = 0x25; + break; + default: + break; + } + } +} + +void ipl::scene::AddressEdit::stt_add_confirm_normal() { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + if (button->isActive()) { + button->update(); + } + if (mState == 0x22) { + mpEditGui->update(); + } +} + +void ipl::scene::AddressEdit::stt_wait_parental() { + ipl::scene::ParentalDialog* parental = + static_cast(ipl::System::getScene(0x1b)); + if (parental != NULL) { + switch (parental->getResult()) { + case 1: + mbParentalOK = true; + mState = 0x29; + break; + case 2: + case 3: + mbParentalOK = false; + mState = 0x29; + break; + default: + break; + } + } +} + +void ipl::scene::AddressEdit::stt_wait_parental_wc() { + ipl::scene::ParentalDialog* parental = + static_cast(ipl::System::getScene(0x1b)); + if (parental != NULL) { + switch (parental->getResult()) { + case 1: + mbParentalOK = true; + mState = 0x2c; + break; + case 2: + case 3: + mbParentalOK = false; + mState = 0x2c; + break; + default: + break; + } + } +} + +void ipl::scene::AddressEdit::stt_wait_parental_dst() { + if (getChild() == NULL) { + if (mbParentalOK) { + ipl::System::getFader()->fadeOut(); + reserveAllSceneDestruction(0x12, reinterpret_cast(1)); + mState = 0x30; + } else { + static_cast(ipl::System::getScene(5))->animation(0xb); + static_cast(ipl::System::getScene(5))->setEventHandler(static_cast< ::gui::EventHandler*>(this), NULL); + mState = 0; + } + } +} + +void ipl::scene::AddressEdit::stt_wait_parental_dst_wc() { + if (getChild() == NULL) { + if (mbParentalOK) { + ipl::System::getFader()->fadeOut(); + reserveAllSceneDestruction(0x12, reinterpret_cast(4)); + mState = 0x30; + } else { + static_cast(ipl::System::getScene(5))->animation(0xb); + static_cast(ipl::System::getScene(5))->setEventHandler(static_cast< ::gui::EventHandler*>(this), NULL); + mState = 0; + } + } +} + +void ipl::scene::AddressEdit::stt_msg_net() { + switch (ipl::System::getDialog()->getLastResult()) { + case 2: + mState = 0; + break; + case 1: { + SCParentalControlsInfo info; + if (SCGetParentalControl(&info) && (info.enable & SC_PARENTAL_FLAG_ENABLED)) { + static_cast(ipl::System::getScene(5))->setEventHandler(NULL, NULL); + static_cast(ipl::System::getScene(5))->animation(0xc); + createChildScene(0x1b, this, NULL, reinterpret_cast(1)); + mbParentalOK = false; + mState = 0x28; + } else { + ipl::System::getFader()->fadeOut(); + reserveAllSceneDestruction(0x12, reinterpret_cast(1)); + mState = 0x30; + } + break; + } + default: + break; + } +} + +void ipl::scene::AddressEdit::stt_msg_wc() { + switch (ipl::System::getDialog()->getLastResult()) { + case 2: + mState = 0; + break; + case 1: { + SCParentalControlsInfo info; + if (SCGetParentalControl(&info) && (info.enable & SC_PARENTAL_FLAG_ENABLED)) { + static_cast(ipl::System::getScene(5))->setEventHandler(NULL, NULL); + static_cast(ipl::System::getScene(5))->animation(0xc); + createChildScene(0x1b, this, NULL, reinterpret_cast(1)); + mbParentalOK = false; + mState = 0x2b; + } else { + ipl::System::getFader()->fadeOut(); + reserveAllSceneDestruction(0x12, reinterpret_cast(4)); + mState = 0x30; + } + break; + } + default: + break; + } +} + +void ipl::scene::AddressEdit::stt_select_mii() { + if (getChild() != NULL) { + ipl::scene::FaceSelect* faceSelect = + static_cast(getChild()); + s32 faceId = faceSelect->getSelectedFaceId(); + if (faceId >= 0 && ipl::System::getMiiManager()->isAvalable(faceId) && + mNigaoeState == 0) { + RFLAdditionalInfo info; + RFLGetAdditionalInfo(&info, RFLDataSource_Official, NULL, faceId); + if (!RFLiIsSameID(&mCreateID, &info.createID)) { + memcpy(&mCreateID, &info.createID, sizeof(mCreateID)); + ipl::System::getMiiManager()->create( + ipl::System::getMem2App(), 0x4c, 0x4c, faceId, + ipl::scene::AddressEdit::nigaoe_create_callback_edit, + this); + mNigaoeState = 1; + update_friendinfo(); + } + } + } + + if (getChild() == NULL && ipl::System::getReservedScene() == NULL) { + static_cast(ipl::System::getScene(5))->setEventHandler(static_cast< ::gui::EventHandler*>(this), NULL); + mState = 0; + mNigaoeState = 0; + mpEditGui->init(); + for (u32 i = 0; i < 5; ++i) { + mPointCount[i] = 0; + } + } +} + +void ipl::scene::AddressEdit::set_textbox(nw4r::lyt::Pane* pane, const wchar_t* text) { + nw4r::lyt::TextBox* textBox = nw4r::ut::DynamicCast(pane); + textBox->SetString(text, 0); +} + +void ipl::scene::AddressEdit::setDefaultTitleText(const wchar_t* text, bool flag) { + if (!flag && (text == NULL || *text != L'\0')) { + goto set_empty_title; + } + switch (mState) { + case 0xe: + case 0x1a: + ipl::System::getKeyboard()->baseMgr()->setTitleText(ipl::System::getMessage(0x15e)); + return; + case 0x12: + ipl::System::getKeyboard()->baseMgr()->setTitleText(ipl::System::getMessage(0x15d)); + break; + } + return; + +set_empty_title: + ipl::System::getKeyboard()->baseMgr()->setTitleText(L""); +} + +void ipl::scene::AddressEditEvent::onEvent(u32 componentId, u32 event, void* data) { + s32 signedEvent = static_cast(event); + ::gui::Manager* manager = mpManager; + ::gui::PaneComponent* component = + static_cast< ::gui::PaneComponent*>(manager->getComponent(componentId)); + const char* paneName = component->getPane()->GetName(); + + if (signedEvent == 1) { + if (data != NULL) { + mpParent->start_point_event(paneName, static_cast(data)); + } + } else if (signedEvent >= 1) { + if (signedEvent < 3) { + mpParent->start_left_event(paneName); + } + } else if (signedEvent >= 0 && data != NULL && + static_cast(data)->downTrg(0x100800)) { + mpParent->start_trig_event(paneName); + } +} + +void ipl::scene::AddressInputEvent::onEvent(u32 componentId, u32 event, void* data) { + s32 signedEvent = static_cast(event); + ::gui::Manager* manager = mpManager; + ::gui::PaneComponent* component = + static_cast< ::gui::PaneComponent*>(manager->getComponent(componentId)); + const char* paneName = component->getPane()->GetName(); + ipl::controller::Interface* controller = static_cast(data); + + if (signedEvent == 1) { + if (controller != NULL) { + mpParent->start_ipt_point_event(paneName, controller->getChannel()); + } + } else if (signedEvent >= 1) { + if (signedEvent < 3 && controller != NULL) { + mpParent->start_ipt_left_event(paneName, controller->getChannel()); + } + } else if (signedEvent >= 0 && controller != NULL && controller->downTrg(0x100800)) { + mpParent->start_ipt_trig_event(paneName, controller->getChannel()); + } +} + +void ipl::scene::AddressEdit::nigaoe_create_callback_edit(ipl::nigaoe::Object* object, void* callbackWork) { + ipl::scene::AddressEdit* self = static_cast(callbackWork); + if (self->mpNigaoe != NULL) { + delete self->mpNigaoe; + } + self->mpNigaoe = object; + nw4r::lyt::Pane* pane = self->mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName( + "mii_icon_00", true); + pane->GetMaterial()->SetTexture(0, object->getIconTexture()); + self->mNigaoeState = 2; + self->mpBalloon->init(object->getName(), 0xa); +} + +void ipl::scene::AddressEdit::nigaoe_create_callback_add(ipl::nigaoe::Object* object, void* callbackWork) { + ipl::scene::AddressEdit* self = static_cast(callbackWork); + if (self->mpNigaoe != NULL) { + delete self->mpNigaoe; + } + self->mpNigaoe = object; + nw4r::lyt::Pane* pane = self->mpNameLayout->getNW4RLyt()->GetRootPane()->FindPaneByName( + "mii_icon_00", true); + pane->GetMaterial()->SetTexture(0, object->getIconTexture()); + pane = self->mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("mii_icon_00", true); + pane->GetMaterial()->SetTexture(0, object->getIconTexture()); + self->mNigaoeState = 2; + self->mpBalloon->init(object->getName(), 0xa); } extern "C" asm void calcFadein__Q33ipl5scene11AddressEditFv() { @@ -645,6 +1559,269 @@ initCalcNormal_L1: blr } +ipl::scene::FaderSceneCommand ipl::scene::AddressEdit::calcNormal() { + switch (mState) { + case 0: + stt_normal(); + break; + case 1: + stt_wait_decide_anm(); + break; + case 2: + stt_wait_btn_fadein(); + break; + case 3: + stt_wait_btn_fadeout(); + break; + case 4: + stt_wait_del_msg_fadein(); + break; + case 5: + stt_wait_del_msg_fadeout(); + break; + case 6: + stt_wait_del_msg_fadeout_to_rlt(); + break; + case 7: + stt_wait_delete(); + break; + case 8: + stt_msg_del_rlt(); + break; + case 9: + stt_msg_code_edit(); + break; + case 10: + stt_msg_no_mii(); + break; + case 11: + stt_select_mii(); + break; + case 12: + stt_ipt_wait_fadein(); + break; + case 13: + stt_ipt_normal(); + break; + case 14: + stt_ipt_input(); + break; + case 15: + stt_ipt_wait_fadeout(); + break; + case 16: + stt_add_code_fadein(); + break; + case 17: + stt_add_code_normal(); + break; + case 18: + stt_add_code_input(); + break; + case 19: + stt_msg_code_invalid(); + break; + case 20: + stt_msg_dup_wii_no(); + break; + case 21: + stt_msg_dup_email(); + break; + case 22: + stt_msg_my_wii_no(); + break; + case 23: + stt_add_code_fadeout(); + break; + case 24: + stt_add_name_fadein(); + break; + case 25: + stt_add_name_normal(); + break; + case 26: + stt_add_name_input(); + break; + case 27: + stt_add_name_fadeout(); + break; + case 28: + stt_add_mii_fadein(); + break; + case 29: + stt_add_mii_normal(); + break; + case 30: + stt_add_mii_input(); + break; + case 31: + stt_msg_no_mii_add(); + break; + case 32: + stt_add_mii_fadeout(); + break; + case 33: + stt_add_confirm_fadein(); + break; + case 34: + stt_add_confirm_normal(); + break; + case 35: + stt_add_confirm_fadeout(); + break; + case 36: + stt_wait_decide_anm_add(); + break; + case 37: + stt_msg_code_add(); + break; + case 38: + stt_msg_add_rlt(); + break; + case 39: + stt_msg_net(); + break; + case 40: + stt_wait_parental(); + break; + case 41: + stt_wait_parental_dst(); + break; + case 42: + stt_msg_wc(); + break; + case 43: + stt_wait_parental_wc(); + break; + case 44: + stt_wait_parental_dst_wc(); + break; + case 45: + stt_msg_parental(); + break; + case 46: + stt_msg_nwc24_error(); + break; + case 47: + stt_msg_no_established(); + break; + } + + switch (mState) { + case 0: + case 3: + case 9: + case 12: + case 29: + case 32: + case 34: + case 35: + case 36: + case 39: + case 42: + case 45: + case 46: + case 47: + mpBalloon->calc(); + break; + } + + return mState == 0x30 + ? ipl::scene::FADER_SCN_NEXT + : ipl::scene::FADER_SCN_CONTINUE; +} + +void ipl::scene::AddressEdit::stt_add_name_fadeout() { + + if (!mpNameLayout->getAnim(5)->isPlaying() && + !mpNameLayout->getAnim(7)->isPlaying() && + !mpNameLayout->getAnim(6)->isPlaying() && + !mpNameLayout->getAnim(9)->isPlaying()) { + s32 state = mSubState; + if (state == 6) { + goto state_done; + } + if (state >= 6) { + goto state_six_or_more; + } + if (state >= 5) { + goto state_five; + } + goto state_done; + + state_six_or_more: + if (state >= 8) { + goto state_done; + } + goto state_seven; + + state_five: { + ipl::layout::Animator* pane = mpNameLayout->getAnim(1); + pane->initFrame(); + pane->restart(); + pane = mpNameLayout->getAnim(2); + pane->initFrame(); + pane->restart(); + + s32 inputType = mMode; + switch (inputType) { + case 2: + goto address_type_two; + case 1: + goto address_type_one; + default: + goto address_type_common; + } + + address_type_one: { + nw4r::lyt::Pane* label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_question_00", true); + set_textbox(label, ipl::System::getMessage(0x31)); + label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_msg_00", true); + set_textbox(label, ipl::System::getMessage(0x47)); + } + goto address_type_common; + + address_type_two: { + nw4r::lyt::Pane* label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_question_00", true); + set_textbox(label, ipl::System::getMessage(0x3f)); + label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_msg_00", true); + set_textbox(label, ipl::System::getMessage(0x48)); + } + + address_type_common: { + nw4r::lyt::Pane* label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_name_00", true); + set_textbox(label, mString.mDisplayText); + mState = 0x10; + } + goto state_done; + } + + state_seven: { + ipl::layout::Animator* pane = mpNameLayout->getAnim(1); + pane->initFrame(); + pane->restart(); + pane = mpNameLayout->getAnim(4); + pane->initFrame(); + pane->restart(); + + nw4r::lyt::Pane* label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_question_00", true); + set_textbox(label, ipl::System::getMessage(0x55)); + label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_mii_msg_00", true); + set_textbox(label, ipl::System::getMessage(0x8b)); + mState = 0x1c; + } + state_done: + ; + } +} + extern "C" asm void initCalcFadeout__Q33ipl5scene11AddressEditFv() { nofralloc stwu r1, -0x10(r1) @@ -699,7 +1876,7 @@ extern "C" asm void calcCommonAfter__Q33ipl5scene11AddressEditFv() { blr } -extern "C" asm void stt_normal__Q33ipl5scene11AddressEditFv() { +extern "C" asm void stt_normal__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -733,7 +1910,7 @@ stt_normal_L2: blr } -extern "C" asm void stt_wait_del_msg_fadein__Q33ipl5scene11AddressEditFv() { +extern "C" asm void stt_wait_del_msg_fadein__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -757,7 +1934,7 @@ stt_wait_del_msg_fadein_L1: blr } -extern "C" asm void delete_friendinfo__Q33ipl5scene11AddressEditFv() { +extern "C" asm void delete_friendinfo__Q33ipl5scene11AddressEditFv(void* self) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -778,18 +1955,7 @@ extern "C" asm void delete_friendinfo__Q33ipl5scene11AddressEditFv() { blr } -extern "C" int get_button_no__Q33ipl5scene11AddressEditFPCc(void*, const char* paneName) { - int result = -1; - for (int i = 0; i < 5; i++) { - if (strcmp(smButtonName__Q33ipl5scene6Button[i], paneName) == 0) { - result = i; - break; - } - } - return result; -} - -extern "C" u64 utf16_wiiid__Q33ipl5scene11AddressEditFPCw(const wchar_t* value) { +u64 ipl::scene::AddressEdit::utf16_wiiid(const wchar_t* value) { u64 result = 0; u64 multiplier = 1; for (int i = 0; i < 0x10; i++) { @@ -799,7 +1965,7 @@ extern "C" u64 utf16_wiiid__Q33ipl5scene11AddressEditFPCw(const wchar_t* value) return result; } -extern "C" asm void wiiid_utf16__Q33ipl5scene11AddressEditFUxPw() { +extern "C" asm void wiiid_utf16__Q33ipl5scene11AddressEditFUxPw(u64, wchar_t*) { nofralloc stwu r1, -0x30(r1) mflr r0 @@ -846,17 +2012,16 @@ wiiid_utf16_L1: blr } -extern "C" void setName__Q43ipl5scene11AddressEdit6StringFPCw(void* self, const wchar_t* value) { - wchar_t* name = reinterpret_cast(reinterpret_cast(self) + 0x204); - wcsncpy(name, value, 0xa); +void ipl::scene::AddressEdit::String::setName(const wchar_t* value) { + wcsncpy(mName, value, 0xa); u32 length = wcslen(value); - reinterpret_cast(self)[0x421] = 0; + mbNameNotEmpty = false; for (u32 i = 0; i < length; i++) { if (value[i] == 0) { break; } if (value[i] != L' ' && value[i] != 0x3000) { - reinterpret_cast(self)[0x421] = 1; + mbNameNotEmpty = true; break; } } @@ -948,7 +2113,7 @@ draw_L1: blr } -extern "C" asm void stt_wait_btn_fadein__Q33ipl5scene11AddressEditFv() { +extern "C" asm void stt_wait_btn_fadein__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -1016,7 +2181,31 @@ stt_wait_btn_fadein_L3: blr } -extern "C" asm void stt_wait_del_msg_fadeout_to_rlt__Q33ipl5scene11AddressEditFv() { +void ipl::scene::AddressEdit::stt_wait_btn_fadeout() { + u32 index = mSelectedFriend; + u32 friendType = mpFriendCache->getInfo(index).attr.status; + u32 friendFinished; + if (friendType == 2) { + friendFinished = !mpCodeLayout->getAnim(0x15)->isPlaying(); + } else { + friendFinished = !mpCodeLayout->getAnim(0x1b)->isPlaying(); + } + + bool finished = friendFinished && !mpCodeLayout->getAnim(0x16)->isPlaying(); + finished = finished && !mpCodeLayout->getAnim(0x17)->isPlaying(); + + if (finished) { + nw4r::lyt::Pane* label = mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_card_msg_00", true); + const wchar_t* message = ipl::System::getMessage(0x30); + set_textbox(label, message); + ipl::System::getDialog()->callS2Btn2(0x142, 0x141, true); + mpCodeLayout->getAnim(0x1c)->initFrame(); + mpCodeLayout->getAnim(0x1c)->restart(); + mState = 4; + } +} + +extern "C" asm void stt_wait_del_msg_fadeout_to_rlt__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -1046,7 +2235,7 @@ stt_wait_del_msg_fadeout_to_rlt_L1: blr } -extern "C" asm void stt_msg_del_rlt__Q33ipl5scene11AddressEditFv() { +extern "C" asm void stt_msg_del_rlt__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -1091,7 +2280,7 @@ stt_msg_del_rlt_L1: blr } -extern "C" asm void stt_ipt_wait_fadein__Q33ipl5scene11AddressEditFv() { +extern "C" asm void stt_ipt_wait_fadein__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -1136,7 +2325,7 @@ stt_ipt_wait_fadein_L1: blr } -extern "C" asm void stt_ipt_normal__Q33ipl5scene11AddressEditFv() { +extern "C" asm void stt_ipt_normal__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { nofralloc stwu r1, -0x10(r1) mflr r0 @@ -1170,6 +2359,210 @@ stt_ipt_normal_L2: blr } +void ipl::scene::AddressEdit::stt_ipt_input() { + ipl::keyboard::Manager::State* state = ipl::System::getKeyboard()->getState(); + ipl::scene::Manager* sceneManager = ipl::System::getSceneManager(); + ipl::scene::Button* button = static_cast(sceneManager->getScene(5)); + s32 keyboardType = state->iplType; + if (keyboardType == ipl::keyboard::Manager::STATE_DISAPPEARING) { + goto state_disappearing; + } + if (keyboardType >= ipl::keyboard::Manager::STATE_DISAPPEARING) { + goto state_at_or_after_disappearing; + } + if (keyboardType >= ipl::keyboard::Manager::STATE_VISIBLE) { + goto done; + } + if (keyboardType >= ipl::keyboard::Manager::STATE_HIDDEN) { + goto state_hidden; + } + goto done; + +state_at_or_after_disappearing: + if (keyboardType >= 5) { + goto done; + } + goto state_hidden_after_disappear; + +state_disappearing: { + bool nameWasEmpty = mString.mName[0] == 0; + if (state->pressOK) { + mString.setName(state->wcString); + ipl::layout::Object* layout = + mpNameLayout; + nw4r::lyt::Pane* pane = layout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_name_00", true); + set_textbox(pane, mString.mName); + } + if (mString.mName[0] == 0) { + if (!nameWasEmpty) { + ipl::layout::Animator* pane = mpNameLayout->getAnim(6); + pane->initFrame(); + pane->restart(); + pane = mpNameLayout->getAnim(3); + pane->initFrame(); + pane->restart(); + } + button->reserveAnm(0xb); + } else { + if (nameWasEmpty) { + ipl::layout::Animator* pane = mpNameLayout->getAnim(2); + pane->initFrame(); + pane->restart(); + pane = mpNameLayout->getAnim(7); + pane->initFrame(); + pane->restart(); + } + if (mString.mbNameNotEmpty != 0) { + button->reserveText(1, 0x2e); + button->reserveAnm(0xf); + } else { + button->reserveAnm(0xb); + } + } + } + goto done; + +state_hidden_after_disappear: { + mState = 0xd; + if (static_cast(ipl::System::getRegion()) == 6) { + ipl::System::getKeyboard()->baseMgr()->enableKSXFilter(false); + } + } + goto done; + +state_hidden: { + setDefaultTitleText(NULL, ipl::System::getKeyboard()->baseMgr()->isVacancy()); + } + +done: + return; +} + +void ipl::scene::AddressEdit::stt_add_code_input() { + ipl::keyboard::Manager::State* state = ipl::System::getKeyboard()->getState(); + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + switch (state->iplType) { + case ipl::keyboard::Manager::STATE_DISAPPEARING: { + bool codeWasEmpty = mString.mValue[0] == 0; + if (state->pressOK) { + if (mMode == 1) { + setWiiNo__Q43ipl5scene11AddressEdit6StringFPCw( + &mString, state->wcString); + } else { + mString.setEMail(state->wcString); + } + nw4r::lyt::Pane* label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName("T_name_00", true); + set_textbox(label, mString.mDisplayText); + } + if (mString.mValue[0] == 0) { + if (!codeWasEmpty) { + ipl::layout::Animator* pane = mpNameLayout->getAnim(6); + pane->initFrame(); + pane->restart(); + pane = mpNameLayout->getAnim(3); + pane->initFrame(); + pane->restart(); + } + button->reserveAnm(0xb); + } else { + if (codeWasEmpty) { + ipl::layout::Animator* pane = mpNameLayout->getAnim(2); + pane->initFrame(); + pane->restart(); + pane = mpNameLayout->getAnim(7); + pane->initFrame(); + pane->restart(); + } + if (mString.mbValidMail != 0 && + isDupCode__Q43ipl5scene11AddressEdit6StringCFv(&mString) == 0 && + isMyCode__Q43ipl5scene11AddressEdit6StringCFv(&mString) == 0) { + button->reserveText(1, 0x2e); + button->reserveAnm(0xf); + } else { + button->reserveAnm(0xb); + } + } + break; + } + case ipl::keyboard::Manager::STATE_HIDDEN_AFTER_DISAPPEAR: + if (state->pressOK) { + if (mString.mValue[0] != 0) { + if (mMode == 1) { + if (isMyCode__Q43ipl5scene11AddressEdit6StringCFv( + &mString) != 0) { + ipl::System::getDialog()->callBtn1(0x56, 0x2e); + mState = 0x16; + } else if (isDupCode__Q43ipl5scene11AddressEdit6StringCFv( + &mString) != 0) { + ipl::System::getDialog()->callBtn1(0x52, 0x2e); + mState = 0x14; + } else if (mString.mbValidMail == 0) { + ipl::System::getDialog()->callBtn1(0x54, 0x2e); + mState = 0x13; + } else { + mState = 0x11; + } + } else if (isDupCode__Q43ipl5scene11AddressEdit6StringCFv( + &mString) != 0) { + ipl::System::getDialog()->callBtn1(0x53, 0x2e); + mState = 0x15; + } else if (mString.mbValidMail == 0) { + ipl::System::getDialog()->callBtn1(0x1be, 0x2e); + mState = 0x13; + } else { + mState = 0x11; + } + } else { + mState = 0x11; + } + } else { + mState = 0x11; + } + break; + case ipl::keyboard::Manager::STATE_HIDDEN: + case ipl::keyboard::Manager::STATE_APPEARING: + if (mMode != 1) { + setDefaultTitleText(NULL, ipl::System::getKeyboard()->baseMgr()->isVacancy()); + } + break; + default: + break; + } +} + +void ipl::scene::AddressEdit::stt_add_code_fadeout() { + ipl::layout::Object* layout = mpNameLayout; + ipl::layout::Animator* pane = layout->getAnim(5); + if (!pane->isPlaying()) { + pane = mpNameLayout->getAnim(6); + if (!pane->isPlaying()) { + nw4r::lyt::Pane* label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + reinterpret_cast("T_question_00"), true); + set_textbox(label, ipl::System::getMessage(0x32)); + label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + reinterpret_cast("T_msg_00"), true); + set_textbox(label, ipl::System::getMessage(0x49)); + label = (mpNameLayout)->getNW4RLyt()->GetRootPane()->FindPaneByName( + reinterpret_cast("T_name_00"), true); + set_textbox(label, mString.mName); + pane = mpNameLayout->getAnim(1); + pane->initFrame(); + pane->restart(); + if (mString.mName[0] != 0) { + pane = mpNameLayout->getAnim(2); + pane->initFrame(); + pane->restart(); + } else { + pane = mpNameLayout->getAnim(3); + pane->initFrame(); + pane->restart(); + } + ipl::System::getKeyboard()->init(); + mState = 0x18; + } + } +} + /* This is a modified version of NWC24CheckPublicMailAddr. Changes: @@ -1299,3 +2692,824 @@ static ChkHostNameError CheckHostName(const char* hostName, u32 hostNameLength) return CHECK_HOSTNAME_SUCCESS; } + + + + + +void ipl::scene::AddressEdit::start_point_event( + const char* paneName, ipl::controller::Interface* controller) { + int buttonNo = get_button_no(paneName); + s32 state = mState; + + switch (state) { + case 0: + goto state0; + case 0x22: + goto state22; + default: + goto done; + } + +state0: + switch (buttonNo) { + case 0: + goto state0Button0; + case 1: + case 2: + goto state0Common; + case 3: + goto state0Button3; + default: + goto done; + } + +state0Button0: { + u32 friendIndex = mSelectedFriend; + u32 friendType = mpFriendCache->getInfo(friendIndex).attr.status; + if (friendType != 2) { + goto done; + } + s32* count = &mPointCount[buttonNo]; + if (*count == 0) { + ipl::layout::Object* layout = mpCodeLayout; + ipl::layout::Animator* pane = layout->getAnim(buttonNo + 1); + pane->initFrame(); + pane->restart(); + ipl::snd::getSystem()->startSE( + "WIPL_SE_BT_TARGETTING"); + controller->rumble(1); + } + ++*count; + goto done; +} + +state0Button3: { + s32* count = &mPointCount[buttonNo]; + if (*count == 0) { + ipl::layout::Object* layout = mpCodeLayout; + nw4r::lyt::Pane* pane = layout->getNW4RLyt()->GetRootPane()->FindPaneByName(paneName, true); + ipl::math::VEC3 position(0.0f, 0.0f, 0.0f); + PSMTXMultVec(pane->GetGlobalMtx(), reinterpret_cast(&position), reinterpret_cast(&position)); + f32 xOffset = 15.0f; + f32 height = 50.0f; + f32 scale = 0.5f; + position.x = position.x + xOffset; + position.y = position.y + scale * height; + (mpBalloon) + ->setPos(position, false, 1); + (mpBalloon)->fadein(); + } +} + +state0Common: { + s32* count = &mPointCount[buttonNo]; + if (*count == 0) { + ipl::layout::Object* layout = mpCodeLayout; + ipl::layout::Animator* pane = layout->getAnim(buttonNo + 1); + pane->initFrame(); + pane->restart(); + ipl::snd::getSystem()->startSE( + "WIPL_SE_BT_TARGETTING"); + controller->rumble(1); + } + ++*count; + goto done; +} + +state22: + switch (buttonNo) { + case 3: + goto state22Button3; + default: + goto done; + } +state22Button3: + { + s32* count = &mPointCount[buttonNo]; + if (*count == 0) { + ipl::layout::Object* layout = mpCodeLayout; + nw4r::lyt::Pane* pane = layout->getNW4RLyt()->GetRootPane()->FindPaneByName(paneName, true); + ipl::math::VEC3 position(0.0f, 0.0f, 0.0f); + PSMTXMultVec(pane->GetGlobalMtx(), reinterpret_cast(&position), reinterpret_cast(&position)); + f32 xOffset = 15.0f; + f32 height = 50.0f; + f32 scale = 0.5f; + position.x = position.x + xOffset; + position.y = position.y + scale * height; + (mpBalloon) + ->setPos(position, false, 1); + (mpBalloon)->fadein(); + } + ++*count; + } + +done: + return; +} + +void ipl::scene::AddressEdit::start_left_event( + const char* paneName) { + int buttonNo = get_button_no(paneName); + s32 state = mState; + switch (state) { + case 0x22: + goto state22; + case 0: + switch (buttonNo) { + case 3: + goto state0Button3; + case 0: + goto state0Friend; + case 1: + case 2: + goto state0Common; + default: + break; + } + break; + default: + break; + } + goto done; + +state0Friend: { + u32 friendIndex = mSelectedFriend; + u32 friendType = mpFriendCache->getInfo(friendIndex).attr.status; + if (friendType != 2) { + goto done; + } + s32* count = &mPointCount[buttonNo]; + if (*count == 1) { + ipl::layout::Object* layout = mpCodeLayout; + ipl::layout::Animator* pane = layout->getAnim(buttonNo + 0xb); + pane->initFrame(); + pane->restart(); + } + if (*count > 0) { + --*count; + } + goto done; + } + +state0Button3: { + s32* count = &mPointCount[buttonNo]; + if (*count == 1) { + (mpBalloon)->fadeoutForce(); + } + --*count; + goto done; + } + +state0Common: { + s32* count = &mPointCount[buttonNo]; + if (*count == 1) { + ipl::layout::Object* layout = mpCodeLayout; + ipl::layout::Animator* pane = layout->getAnim(buttonNo + 0xb); + pane->initFrame(); + pane->restart(); + } + if (*count > 0) { + --*count; + } + goto done; + } + +state22: + if (buttonNo != 3) { + goto done; + } + { + s32* count = &mPointCount[buttonNo]; + if (*count == 1) { + (mpBalloon)->fadeoutForce(); + } + --*count; + } + +done: + return; +} + +void ipl::scene::AddressEdit::start_trig_event( + const char* paneName) { + int buttonNo = get_button_no(paneName); + s32 state = mState; + + switch (state) { + case 0: { + if (buttonNo == 0) { + goto state0Friend; + } + if (buttonNo < 0) { + break; + } + if (buttonNo >= 5) { + break; + } + goto state0Event; + + state0Friend: { + u32 friendIndex = mSelectedFriend; + u32 friendType = mpFriendCache->getInfo(friendIndex).attr.status; + if (friendType != 2) { + goto state0FriendError; + } + reset_gui(); + ipl::layout::Object* layout = mpCodeLayout; + ipl::layout::Animator* pane = layout->getAnim(buttonNo + 6); + pane->initFrame(); + pane->restart(); + mSelectedButton = buttonNo; + ipl::snd::getSystem()->startSE("WIPL_SE_BT_TARGETTING"); + mState = 1; + break; + } + + state0FriendError: { + ipl::System::getDialog()->callBtn1(0x57, 0x2e); + (mpBalloon)->fadeoutForce(); + reset_gui(); + mState = 0x2f; + break; + } + + state0Event: { + ipl::layout::Object* layout = mpCodeLayout; + ipl::layout::Animator* pane = layout->getAnim(buttonNo + 6); + pane->initFrame(); + pane->restart(); + mSelectedButton = buttonNo; + ipl::snd::getSystem()->startSE("WIPL_SE_BT_TARGETTING"); + mState = 1; + break; + } + } + break; + + case 0x22: + switch (buttonNo) { + case 4: + goto state22Button4; + default: + break; + } + break; + + state22Button4: { + (mpBalloon)->fadeoutForce(); + for (s32 i = 0; i < 5; ++i) { + mPointCount[i] = 0; + } + ipl::layout::Object* layout = mpCodeLayout; + ipl::layout::Animator* pane = layout->getAnim(10); + pane->initFrame(); + pane->restart(); + mSelectedButton = buttonNo; + ipl::snd::getSystem()->startSE("WIPL_SE_BT_TARGETTING"); + mState = 0x24; + break; + } + } +} + +void ipl::scene::AddressEdit::start_ipt_trig_event( + const char* paneName, int channel) { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + ipl::controller::Interface* controller = ipl::System::getControllerManager()->getYoungController(); + if (controller == NULL) { + return; + } + if (controller->getChannel() != channel) { + return; + } + if (!strcmp(sInputPaneName, paneName)) { + s32 state = mState; + switch (state) { + case 0xd: { + ipl::keyboard::Manager::KeyboardSetting setting(static_cast(0xb), mString.mName, 0xa, 1); + + switch (ipl::System::getRegion()) { + case 0xb: { + void* oemDictionary; + void* systemDictionary; + systemDictionary = ipl::System::getKeyboard()->getZiSystemDic(); + oemDictionary = ipl::System::getKeyboard()->getZiOemDic(); + textinput::InputForm* inputForm = ipl::System::getKeyboard()->baseMgr()->getInputForm(); + inputForm->setZiDictionary(oemDictionary, systemDictionary); + setting.type = static_cast(0xd); + break; + } + case 6: + ipl::System::getKeyboard()->baseMgr()->enableKSXFilter(true); + setting.type = static_cast(0xd); + break; + default: + break; + } + + ipl::System::getKeyboard()->start(channel, setting); + setDefaultTitleText(mString.mName, false); + if (mString.mbNameNotEmpty != 0) { + button->reserveAnm(0x10); + } else { + button->reserveAnm(0xc); + } + button->reserveText(1, 0x2e); + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0xe; + break; + } + case 0x11: { + if (mMode == 1) { + ipl::keyboard::Manager::KeyboardSetting setting(static_cast(0xc), mString.mValue, 0x10, 2); + ipl::System::getKeyboard()->start(channel, setting); + } else { + ipl::keyboard::Manager::KeyboardSetting setting(static_cast(7), mString.mValue, 0x63, 5); + ipl::System::getKeyboard()->start(channel, setting); + } + setDefaultTitleText(mString.mValue, false); + if (mString.mbValidMail != 0 && + isDupCode__Q43ipl5scene11AddressEdit6StringCFv(&mString) == 0 && + isMyCode__Q43ipl5scene11AddressEdit6StringCFv(&mString) == 0) { + button->reserveAnm(0x10); + } else { + button->reserveAnm(0xc); + } + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0x12; + break; + } + case 0x19: { + ipl::keyboard::Manager::KeyboardSetting setting(static_cast(0xb), mString.mName, 0xa, 1); + + switch (ipl::System::getRegion()) { + case 0xb: { + void* oemDictionary; + void* systemDictionary; + systemDictionary = ipl::System::getKeyboard()->getZiSystemDic(); + oemDictionary = ipl::System::getKeyboard()->getZiOemDic(); + textinput::InputForm* inputForm = ipl::System::getKeyboard()->baseMgr()->getInputForm(); + inputForm->setZiDictionary(oemDictionary, systemDictionary); + setting.type = static_cast(0xd); + break; + } + case 6: + ipl::System::getKeyboard()->baseMgr()->enableKSXFilter(true); + setting.type = static_cast(0xd); + break; + default: + break; + } + + ipl::System::getKeyboard()->start(channel, setting); + setDefaultTitleText(mString.mName, false); + if (mString.mbNameNotEmpty != 0) { + button->reserveAnm(0x10); + } else { + button->reserveAnm(0xc); + } + button->reserveText(1, 0x2e); + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0x1a; + break; + } + case 0x1d: + if (mNigaoeState != 1) { + mNigaoeState = 0; + if (RFLGetAvailableOfficialDataNum() != 0) { + createChildScene(0x1c, this, NULL, reinterpret_cast(2)); + button->reserveAnm(0x10); + button->reserveAnm(0xb); + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0x1e; + } else { + ipl::System::getDialog()->callBtn1(0x17c, 0x2e); + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0x1f; + } + } + break; + default: + break; + } + } +} + + +void ipl::scene::AddressEdit::start_ipt_point_event( + const char* paneName, int channel) { + ipl::controller::Interface* controller = ipl::System::getControllerManager()->getYoungController(); + if (controller != NULL) { + if (controller->getChannel() == channel) { + switch (mState) { + case 0x1d: { + char paneNameCopy[12] = "mii_icon_00"; + if (strcmp(paneNameCopy, paneName) == 0) { + ipl::layout::Object* layout = mpNameLayout; + nw4r::lyt::Pane* pane = layout->getNW4RLyt()->GetRootPane()->FindPaneByName(paneName, true); + ipl::math::VEC3 position(0.0f, 0.0f, 0.0f); + PSMTXMultVec(pane->GetGlobalMtx(), reinterpret_cast(&position), reinterpret_cast(&position)); + f32 y; + f32 offset; + offset = 50.0f; + y = position.y; + position.y = y + offset; + (mpBalloon) + ->setPos(position, false, 0); + (mpBalloon)->fadein(); + } + break; + } + } + } + } +} + +void ipl::scene::AddressEdit::onEventDerived(u32 componentId, u32 event, const ipl::controller::Interface* controller) { + + ::gui::Manager* manager = getGuiManager(); + gui::PaneComponent* component = + static_cast(manager->getComponent(componentId)); + nw4r::lyt::Pane* pane = component->getPane(); + + const char* paneName = pane->GetName(); + if (event != 0) { + return; + } + if (controller == NULL) { + return; + } + if (!controller->downTrg(0x100800)) { + return; + } + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + ipl::System::getScene(0x14); + + if (strcmp(paneName, smButtonName__Q33ipl5scene6Button[5]) == 0) { + switch (mState) { + case 0: { + button->reserveAnm(0x1b); + button->reserveAnm(0xc); + button->reserveText(0, 0x23); + button->reserveText(1, 0x29); + button->reserveAnm(0xf); + ipl::layout::Animator* frame = mpCodeLayout->getAnim(0x1e); + frame->initFrame(); + frame->restart(); + mSubState = 5; + ipl::snd::getSystem()->startSE("WIPL_SE_CANCEL"); + (mpBalloon)->fadeoutForce(); + mState = 0x30; + break; + } + case 0xd: { + button->reserveAnm(0x1b); + if (mString.mbNameNotEmpty != 0) { + button->reserveAnm(0x10); + } else { + button->reserveAnm(0xc); + } + button->reserveAnm(0xb); + ipl::layout::Animator* frame = mpNameLayout->getAnim(9); + frame->initFrame(); + frame->restart(); + frame = mpBackgroundLayout->getAnim(1); + frame->initFrame(); + frame->restart(); + get_friendinfo(); + ipl::snd::getSystem()->startSE("WIPL_SE_CANCEL"); + mState = 0xf; + break; + } + case 0x11: { + requestSceneChange(getPreviousScene(), NULL); + button->reserveAnm(0x1b); + if (mString.mbValidMail != 0 && + isDupCode__Q43ipl5scene11AddressEdit6StringCFv( + &mString) == 0 && + isMyCode__Q43ipl5scene11AddressEdit6StringCFv( + &mString) == 0) { + button->reserveAnm(0x10); + } else { + button->reserveAnm(0xc); + } + ipl::layout::Animator* frame; + if (mString.mValue[0] != 0) { + frame = mpNameLayout->getAnim(6); + frame->initFrame(); + frame->restart(); + } else { + frame = mpNameLayout->getAnim(7); + frame->initFrame(); + frame->restart(); + } + button->reserveAnm(0xb); + frame = mpNameLayout->getAnim(9); + frame->initFrame(); + frame->restart(); + frame = mpNameLayout->getAnim(5); + frame->initFrame(); + frame->restart(); + mSubState = 5; + ipl::snd::getSystem()->startSE("WIPL_SE_CANCEL"); + mState = 0x30; + break; + } + case 0x19: { + button->reserveAnm(0x1b); + if (mString.mbNameNotEmpty != 0) { + button->reserveAnm(0x10); + ipl::layout::Animator* frame = mpNameLayout->getAnim(6); + frame->initFrame(); + frame->restart(); + } else { + u16 nameLength = mString.mName[0]; + if (nameLength != 0) { + button->reserveAnm(0xc); + ipl::layout::Animator* frame = mpNameLayout->getAnim(6); + frame->initFrame(); + frame->restart(); + } else { + button->reserveAnm(0xc); + ipl::layout::Animator* frame = mpNameLayout->getAnim(7); + frame->initFrame(); + frame->restart(); + } + } + button->reserveAnm(0xf); + ipl::layout::Animator* frame = mpNameLayout->getAnim(5); + frame->initFrame(); + frame->restart(); + mSubState = 5; + ipl::snd::getSystem()->startSE("WIPL_SE_CANCEL"); + mState = 0x1b; + break; + } + case 0x1d: { + button->reserveAnm(0x1b); + button->reserveAnm(0x10); + button->reserveAnm(0xf); + ipl::layout::Animator* frame = mpNameLayout->getAnim(5); + frame->initFrame(); + frame->restart(); + frame = mpNameLayout->getAnim(8); + frame->initFrame(); + frame->restart(); + mSubState = 5; + ipl::snd::getSystem()->startSE("WIPL_SE_CANCEL"); + mState = 0x20; + (mpBalloon)->fadeoutForce(); + reset_gui(); + break; + } + case 0x22: { + button->reserveAnm(0x1b); + button->reserveAnm(0x10); + button->reserveAnm(0xf); + ipl::layout::Animator* frame = mpCodeLayout->getAnim(0x1e); + frame->initFrame(); + frame->restart(); + mSubState = 5; + ipl::snd::getSystem()->startSE("WIPL_SE_CANCEL"); + mState = 0x23; + (mpBalloon)->fadeoutForce(); + reset_gui(); + break; + } + } + } else if (strcmp(paneName, smButtonName__Q33ipl5scene6Button[7]) == 0) { + switch (mState) { + case 0xd: { + button->reserveAnm(0x1d); + button->reserveAnm(0x10); + button->reserveAnm(0xb); + ipl::layout::Object* layout = mpCodeLayout; + nw4r::lyt::Pane* pane = layout->getNW4RLyt()->GetRootPane()->FindPaneByName( + "T_name_00", true); + set_textbox(pane, mString.mName); + mSubState = 7; + ipl::layout::Animator* frame = mpNameLayout->getAnim(9); + frame->initFrame(); + frame->restart(); + frame = mpBackgroundLayout->getAnim(1); + frame->initFrame(); + frame->restart(); + update_friendinfo(); + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0xf; + break; + } + case 0x11: { + button->reserveAnm(0x1d); + button->reserveAnm(0x10); + if (mString.mbNameNotEmpty != 0) { + button->reserveAnm(0xf); + } else { + button->reserveAnm(0xb); + } + ipl::layout::Animator* frame = mpNameLayout->getAnim(5); + frame->initFrame(); + frame->restart(); + frame = mpNameLayout->getAnim(6); + frame->initFrame(); + frame->restart(); + mSubState = 7; + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0x17; + break; + } + case 0x19: { + button->reserveAnm(0x1d); + button->reserveAnm(0x10); + button->reserveAnm(0xf); + ipl::layout::Animator* frame = mpNameLayout->getAnim(5); + frame->initFrame(); + frame->restart(); + frame = mpNameLayout->getAnim(6); + frame->initFrame(); + frame->restart(); + mSubState = 7; + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0x1b; + reset_gui(); + break; + } + case 0x1d: { + button->reserveAnm(0x1d); + button->reserveAnm(0x10); + button->reserveAnm(0xf); + ipl::layout::Animator* frame = mpNameLayout->getAnim(9); + frame->initFrame(); + frame->restart(); + mSubState = 7; + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0x20; + (mpBalloon)->fadeoutForce(); + reset_gui(); + break; + } + case 0x22: { + button->reserveAnm(0x1d); + button->reserveAnm(0x10); + ipl::layout::Animator* frame = mpCodeLayout->getAnim(0x1e); + frame->initFrame(); + frame->restart(); + mSubState = 7; + ipl::snd::getSystem()->startSE("WIPL_SE_DECIDE"); + mState = 0x23; + (mpBalloon)->fadeoutForce(); + break; + } + } + } +} + +void ipl::scene::AddressEdit::start_ipt_left_event( + const char* paneName, int channel) { + ipl::controller::Interface* controller = ipl::System::getControllerManager()->getYoungController(); + if (controller != NULL) { + if (controller->getChannel() == channel) { + switch (mState) { + case 0x1d: { + char paneNameCopy[12] = "mii_icon_00"; + if (strcmp(paneNameCopy, paneName) == 0) { + (mpBalloon)->fadeoutForce(); + } + break; + } + } + } + } +} + +void ipl::scene::AddressEdit::set_err_msg(wchar_t* outErrMsg, u32 outErrMsgLen, NWC24Err nwc24Err) { + memset(outErrMsg, 0, outErrMsgLen * sizeof(wchar_t)); + wcsncat(outErrMsg, ipl::System::getMessage(MESG_ERROR_CODE), outErrMsgLen - wcslen(outErrMsg)); + + wchar_t nwc24ErrStr[32]; + memset(nwc24ErrStr, 0, sizeof(nwc24ErrStr)); + void* friendListCache = mpFriendCache; + NWC24Err errorCode = static_cast(mpFriendCache->getErrCode()); + swprintf(nwc24ErrStr, sizeof(nwc24ErrStr) / sizeof(wchar_t), L"%06d\n", errorCode); + wcsncat(outErrMsg, nwc24ErrStr, outErrMsgLen - wcslen(outErrMsg)); + + u32 messageId = MESG_ERROR_NWC24_SERVER; + switch (nwc24Err) { + case NWC24_ERR_NETWORK: + messageId = MESG_ERROR_NWC24_NETWORK; + break; + case NWC24_ERR_SERVER: + case NWC24_ERR_FULL: + messageId = MESG_ERROR_NWC24_SERVER; + break; + default: + break; + } + wcsncat(outErrMsg, ipl::System::getMessage(messageId), outErrMsgLen - wcslen(outErrMsg)); +} + + + + + + + +int ipl::scene::AddressEdit::get_button_no(const char* paneName) { + int result = -1; + for (int i = 0; i < 5; i++) { + if (strcmp(smButtonName__Q33ipl5scene6Button[i], paneName) == 0) { + result = i; + break; + } + } + return result; +} + +void ipl::scene::AddressEdit::add_friendinfo() { + NWC24FriendInfo info ALIGN32; + memset(&info, 0, sizeof(info)); + switch (mMode) { + case 1: + info.attr.type = 1; + { + const wchar_t* name = mString.mName; + wchar_t* storedName = reinterpret_cast(info.attr.name); + wcsncpy(storedName, name, 0xa); + } + info.addr.wiiId = ipl::scene::AddressEdit::utf16_wiiid(mString.mValue); + memcpy(&info.attr.fdId, &mCreateID, sizeof(mCreateID)); + break; + case 2: + info.attr.type = 2; + { + const wchar_t* name = mString.mName; + wchar_t* storedName = reinterpret_cast(info.attr.name); + wcsncpy(storedName, name, 0xa); + } + { + const wchar_t* value = mString.mValue; + u8* address = reinterpret_cast(&info.addr); + ipl::utility::CharacterCode::UTF16ToANSI(address, value, sizeof(info.addr)); + } + memcpy(&info.attr.fdId, &mCreateID, sizeof(mCreateID)); + break; + default: + break; + } + + ipl::scene::Address* address = + static_cast(ipl::System::getScene(0x14)); + u32 index = address->getChosenFriendIndex(); + if (index >= 100) { + index = 0; + for (u32 remaining = 100; remaining != 0; --remaining) { + if (!mpFriendCache->isThere(index)) { + break; + } + ++index; + } + } + mpFriendCache->add(index, info); + if (mMode == 2) { + mpFriendCache->sendRegisterMail(index); + } +} + +void ipl::scene::AddressEdit::get_friendinfo() { + const NWC24FriendInfo& friendInfo = mpFriendCache->getInfo(mSelectedFriend); + memcpy(&sFriendInfo__Q23ipl5scene, &friendInfo, sizeof(sFriendInfo__Q23ipl5scene)); + mString.setName(reinterpret_cast(sFriendInfo__Q23ipl5scene.attr.name)); + const wchar_t* name = mString.mName; + nw4r::lyt::Pane* namePane = + mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_name_00", true); + set_textbox(namePane, name); + + wchar_t wiiNo[0x100]; + wchar_t email[0x102]; + if (sFriendInfo__Q23ipl5scene.attr.type == 1) { + memset(wiiNo, 0, sizeof(wiiNo)); + ipl::scene::AddressEdit::wiiid_utf16(sFriendInfo__Q23ipl5scene.addr.wiiId, wiiNo); + mString.setWiiNo(wiiNo); + } else { + memset(email, 0, sizeof(email)); + ipl::utility::CharacterCode::ANSIToUTF16(email, reinterpret_cast(&sFriendInfo__Q23ipl5scene.addr), 0x102); + mString.setEMail(email); + } + const wchar_t* displayText = mString.mDisplayText; + nw4r::lyt::Pane* friendCodePane = + mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_frnd_crd_00", true); + set_textbox(friendCodePane, displayText); + memcpy(&mCreateID, &sFriendInfo__Q23ipl5scene.attr.fdId, sizeof(mCreateID)); +} + +void ipl::scene::AddressEdit::update_friendinfo() { + memset(sFriendInfo__Q23ipl5scene.attr.name, 0, 0x18); + wcsncpy(reinterpret_cast(sFriendInfo__Q23ipl5scene.attr.name), mString.mName, 0xa); + memcpy(&sFriendInfo__Q23ipl5scene.attr.fdId, &mCreateID, sizeof(mCreateID)); + mpFriendCache->update( + mSelectedFriend, + reinterpret_cast(sFriendInfo__Q23ipl5scene.attr.name), + sFriendInfo__Q23ipl5scene.attr.fdId); + static_cast(ipl::System::getScene(0x14))->reset_friend(); +} -- 2.49.1 From 46b7c879a742b8d4c4026ce5acdae8f39afe655b Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Mon, 28 Sep 2026 23:17:24 +0000 Subject: [PATCH 02/34] decompile wad import check --- libs/RVL_SDK/src/wad/wad.c | 168 +++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 libs/RVL_SDK/src/wad/wad.c diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c new file mode 100644 index 00000000..0fe171df --- /dev/null +++ b/libs/RVL_SDK/src/wad/wad.c @@ -0,0 +1,168 @@ +#include +#include +#include + +typedef struct __attribute__((aligned(32))) WADStream { + u32 words[0x30]; +} WADStream; + +typedef union WADVersionBuffer { + WADHeader header; + struct { + u32 headerSize; + union { + ESTmdViewHeader tmd; + ESTitleId backupTitleId; + u8 bytes[0x80]; + } version; + } data; + u8 bytes[0x84]; +} WADVersionBuffer; + +static s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32, u32); +static s32 WADReadStream(WADStream* stream, void* buffer, u32 size, u32 offset); +static s32 WADCloseStream(WADStream* stream); +static s32 WAD_815C2F44(u32 headerSize, u32* headerInfo); +static s32 _WADGetTitleVer(WADHeader* header, u32, u32, u32 type, u32 headerInfo); + +extern s32 ES_GetBoot2Version(u32* version); + +s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADLocation location, u32 offset) { + WADStream stream ALIGN32; + WADVersionBuffer versionBuffer; + ESTitleId* backupTitleId; + u32 headerInfo; + u32 titleDataSize; + s32 result; + u32 titleDataOffset; + BOOL streamOpened; + u32 type; + + streamOpened = FALSE; + titleDataOffset = 0; + if (path == 0) { + result = -3000; + } else if ((offset & 0x3F) != 0) { + result = -3007; + } else { + result = WADOpenStream(location, path, &stream, 0, 0); + streamOpened = TRUE; + if (result == 0) { + result = WADReadStream(&stream, &versionBuffer.header, sizeof(WADHeader), offset); + if (result == sizeof(WADHeader)) { + type = WAD_815C2F44(versionBuffer.header.hdrSize, &versionBuffer.header.certSize); + if (type == 0) { + result = -3001; + } else if (((type == 2) && (versionBuffer.header.tmdSize == 0)) || + ((type == 1) && (versionBuffer.header.ticketSize == 0))) { + result = -3002; + if (versionBuffer.header.certSize == 2) { + titleDataOffset = 0x60; + } else { + goto done; + } + } + + if (titleDataOffset == 0) { + result = _WADGetTitleVer(&versionBuffer.header, 0, 0, type, + versionBuffer.header.certSize); + if (result == 0) { + result = -3002; + } else { + titleDataOffset = result + 0x180; + result = WADReadStream(&stream, &versionBuffer.data.version.tmd, + 0x80, offset + titleDataOffset); + if (result == 0x80) { + result = 0; + if (titleId != 0) { + titleId[0] = versionBuffer.data.version.tmd.titleId; + } + if (titleVersion != 0) { + *titleVersion = versionBuffer.data.version.tmd.titleVersion; + } + } else { + result = -3005; + } + } + } else { + result = WADReadStream(&stream, &versionBuffer.data.version.backupTitleId, + 0x20, offset + titleDataOffset); + if (result == 0x20) { + result = 0; + if (titleId != 0) { + *titleId = versionBuffer.data.version.backupTitleId; + } + } else { + result = -3005; + } + } + } else { + result = -3005; + } + } + } + +done: + if (streamOpened) { + WADCloseStream(&stream); + } + return result; +} + +s32 WADCheckImport(ESTitleId titleId, u32 titleVersion) { + u16 installedVersion; + s32 result; + + if (__OSInIPL && (titleId == 0x0001000248414141ULL) && (titleVersion == 0xFF00)) { + return 0; + } + + result = WADGetInstalledVersion(titleId, &installedVersion); + if (result == -3002) { + return 1; + } + if (result == 0) { + if (__OSInIPL) { + if (titleVersion > installedVersion) { + return 1; + } + } else if (titleVersion >= installedVersion) { + return 1; + } + } + return 0; +} + +s32 WADGetInstalledVersion(ESTitleId titleId, u16* version) { + u32 installedVersion; + u32 tmdSize; + ESTmdView tmdView __attribute__((aligned(32))); + s32 result; + + if (version == 0) { + return -3000; + } + if (titleId == 1) { + result = ES_GetBoot2Version(&installedVersion); + if (result == 0) { + *version = installedVersion; + } + return result; + } + + result = ES_GetTmdView(titleId, 0, &tmdSize); + if (result == -106) { + return -3002; + } + if (result != 0) { + return result; + } + if (tmdSize > sizeof(ESTmdView)) { + return -3000; + } + result = ES_GetTmdView(titleId, &tmdView, &tmdSize); + if (result == 0) { + *version = tmdView.head.titleVersion; + } + return result; +} -- 2.49.1 From c98c2010b2bef9e010ca3904e642aa63d3961736 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Mon, 28 Sep 2026 18:24:09 -0500 Subject: [PATCH 03/34] Split iplController into its two real units and decompile its asm (#241) * decompile controller asm bodies * model controller inheritance * decompile controller leaf destructors * split iplController into controller and manager units, decompile the asm * fix controller manager readability --- config/43U/splits.txt | 11 +- configure.py | 1 + include/math/iplMathTypes.h | 12 + include/system/iplController.h | 212 ++- libs/NW4R/include/nw4r/ut/Rect.h | 2 + src/system/iplController.cpp | 2046 +++++---------------------- src/system/iplControllerManager.cpp | 187 +++ 7 files changed, 745 insertions(+), 1726 deletions(-) create mode 100644 src/system/iplControllerManager.cpp diff --git a/config/43U/splits.txt b/config/43U/splits.txt index 8c0eaaee..e7384e5e 100644 --- a/config/43U/splits.txt +++ b/config/43U/splits.txt @@ -37,11 +37,16 @@ src/system/iplSystem.cpp: .sdata start:0x81696060 end:0x81696080 src/system/iplController.cpp: - .text start:0x81335CCC end:0x81337978 + .text start:0x81335CCC end:0x81337318 .rodata start:0x8160D2C0 end:0x8160D2D0 - .data start:0x816346A8 end:0x81634A28 - .sdata2 start:0x81694440 end:0x81694490 + .data start:0x816346A8 end:0x81634910 + .sdata2 start:0x81694440 end:0x81694478 .sdata start:0x81696080 end:0x81696088 + +src/system/iplControllerManager.cpp: + .text start:0x81337318 end:0x81337978 + .data start:0x81634910 end:0x81634A28 + .sdata2 start:0x81694478 end:0x81694490 .sbss start:0x81698680 end:0x81698690 src/system/iplChannelManager.cpp: diff --git a/configure.py b/configure.py index cf1fb457..46b142da 100644 --- a/configure.py +++ b/configure.py @@ -625,6 +625,7 @@ config.libs = [ Object(Matching, "system/RIPL_BoardRecord.c"), Object(Matching, "system/iplSystem.cpp"), Object(NonMatching, "system/iplController.cpp"), + Object(NonMatching, "system/iplControllerManager.cpp"), Object(Matching, "system/iplChannelManager.cpp"), Object(Matching, "system/iplChannelScriptManager.cpp"), Object(Matching, "system/iplNand.cpp"), diff --git a/include/math/iplMathTypes.h b/include/math/iplMathTypes.h index 0e6f9bd6..45a21ff4 100644 --- a/include/math/iplMathTypes.h +++ b/include/math/iplMathTypes.h @@ -35,6 +35,10 @@ namespace ipl { return x; } } +#ifdef IPL_CONTROLLER_OUT_OF_LINE_ABS_CLAMP + template <> + f32 abs_clamp(const f32& x, const f32& y); +#endif typedef struct MTX33 : public nw4r::math::MTX33 { } MTX33; @@ -55,19 +59,27 @@ namespace ipl { x = r.x; y = r.y; } +#ifdef IPL_CONTROLLER_OUT_OF_LINE_VEC2 + VEC2(f32 fx, f32 fy); +#else VEC2(f32 fx, f32 fy) { x = fx; y = fy; } +#endif void operator=(const VEC2& r); VEC2 operator*(f32 val) const { return VEC2(x * val, y * val); } +#ifdef IPL_CONTROLLER_OUT_OF_LINE_VEC2 + void set(f32 fx, f32 fy); +#else void set(f32 fx, f32 fy) { x = fx; y = fy; } +#endif void clear() { y = 0; diff --git a/include/system/iplController.h b/include/system/iplController.h index 8ac238f4..bba7b9a0 100644 --- a/include/system/iplController.h +++ b/include/system/iplController.h @@ -62,6 +62,200 @@ namespace ipl { }; // clang-format on +#ifdef IPL_CONTROLLER_NATIVE_HIERARCHY +#ifdef IPL_CONTROLLER_INLINE_INTERFACE +#define IPL_CONTROLLER_DEFAULT_VIRTUAL(signature, implementation) virtual signature implementation +#else +#define IPL_CONTROLLER_DEFAULT_VIRTUAL(signature, implementation) virtual signature; +#endif + class Interface { + public: + virtual ~Interface() = 0; + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getType() const, { return -1; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getChannel() const, { return -1; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(bool down(u32 button) const, { return false; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(bool downTrg(u32 button) const, { return false; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(bool upTrg(u32 button) const, { return false; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int pinch() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int pinchTrg() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int pinchOffTrg() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int decide() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(bool repeat(u32 button) const, { return false; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(BOOL rumble(int type = 0), { return FALSE; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(void cancelRumbling(), { }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getHoldFlag() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getTrigFlag() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getReleaseFlag() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getClassicHoldFlag() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getClassicTrigFlag() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getClassicReleaseFlag() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(math::VEC2 getDpdPos() const, { return math::VEC2(0.0f, 0.0f); }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(math::VEC2 getDpdProjectionPos() const, { return math::VEC2(0.0f, 0.0f); }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(math::VEC2 getHorizon() const, { return math::VEC2(0.0f, 0.0f); }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(f32 getDpdDistance() const, { return 0.0f; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(KPADStatus* getKPADStatus() const, { return NULL; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(PADStatus* getPADStatus() const, { return NULL; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(bool isValidBtn() const, { return false; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(bool isValidDpd() const, { return false; }) + virtual void setForceInvalid(bool flag) = 0; + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getMainStickX() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getMainStickY() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getSubStickX() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(int getSubStickY() const, { return 0; }) + IPL_CONTROLLER_DEFAULT_VIRTUAL(void read(), { }) + }; + +#undef IPL_CONTROLLER_DEFAULT_VIRTUAL +#ifdef IPL_CONTROLLER_INLINE_INTERFACE + inline Interface::~Interface() {} +#endif + + class Base : public Interface { + public: + Base(int chan, KPADStatus& status) { + mButton = 0; + unk_0x08 = 0; + mLastRumbleTime = 0; + mRumbleType = -1; + mChan = chan; + mType = 2; + unk_0x1C = 0; + KPADEnableDPD(chan); + unk_0x1D = 0; + unk_0x1E = 0; + unk_0x20 = &status; + } + + Base(int chan, int type, KPADStatus& status) { + mButton = 0; + unk_0x08 = 0; + mLastRumbleTime = 0; + mRumbleType = -1; + mChan = chan; + mType = type; + unk_0x1C = 0; + KPADEnableDPD(chan); + } + + virtual ~Base(); + virtual int getType() const override; + virtual int getChannel() const override; + virtual int decide() const override; + virtual BOOL rumble(int timer = 0) override; + virtual void cancelRumbling() override; + virtual void setForceInvalid(bool flag) override; + virtual void read() override; + + protected: + u8 mButton; + u32 unk_0x08; + OSTick mLastRumbleTime; + s32 mRumbleType; + u32 mChan; + u32 mType; + u8 unk_0x1C; + u8 unk_0x1D; + u8 unk_0x1E; + KPADStatus* unk_0x20; + }; + + class Revolution : public Base { + public: + Revolution(int chan, KPADStatus& status) : Base(chan, status) {} + Revolution(int chan, int type, KPADStatus& status); + virtual ~Revolution(); + virtual bool down(u32 button) const override; + virtual bool downTrg(u32 button) const override; + virtual bool upTrg(u32 button) const override; + virtual int pinch() const override; + virtual int pinchTrg() const override; + virtual int pinchOffTrg() const override; + virtual bool repeat(u32 button) const override; + virtual f32 getDpdDistance() const override; + virtual KPADStatus* getKPADStatus() const override; + virtual int getReleaseFlag() const override; + virtual int getTrigFlag() const override; + virtual int getHoldFlag() const override; + virtual math::VEC2 getDpdPos() const override; + virtual math::VEC2 getDpdProjectionPos() const override; + virtual math::VEC2 getHorizon() const override; + virtual bool isValidBtn() const override; + virtual bool isValidDpd() const override; + virtual void read() override; + }; + + class Classic : public Revolution { + public: + Classic(int chan, KPADStatus& status); + virtual ~Classic(); + virtual bool down(u32 button) const override; + virtual bool downTrg(u32 button) const override; + virtual bool upTrg(u32 button) const override; + virtual int getClassicHoldFlag() const override; + virtual int getClassicTrigFlag() const override; + virtual int getClassicReleaseFlag() const override; + virtual math::VEC2 getDpdPos() const override; + virtual math::VEC2 getDpdProjectionPos() const override; + virtual math::VEC2 getHorizon() const override; + virtual bool isValidDpd() const override; + virtual void read() override; + virtual BOOL isValidDpdClassic() const; + + private: + math::VEC2 unk_0x24; + int unk_0x2C; + }; + + class Master : public Interface { + public: + Master(Interface** controllers) : mpControllers(controllers) {} + virtual ~Master(); + virtual bool down(u32 button) const override; + virtual bool downTrg(u32 button) const override; + virtual bool upTrg(u32 button) const override; + virtual bool repeat(u32 button) const override; + virtual int decide() const override; + virtual void setForceInvalid(bool flag) override; + + private: + bool call(u32 button, bool (Interface::*func)(u32) const) const; + Interface** mpControllers; + }; + + class Core : public Revolution { + public: + Core(int chan, int type, KPADStatus& status) : Revolution(chan, type, status) {} + virtual ~Core(); + }; + + class FreeStyle : public Revolution { + public: + FreeStyle(int chan, int type, KPADStatus& status) : Revolution(chan, type, status) {} + virtual ~FreeStyle(); + }; + + class Manager { + public: + Manager(EGG::Heap* heap); + void read(); + Interface* getController(int chan); + Interface* getMasterController(); + Interface* getYoungController(); + static void* alloc(u32 size); + static int free(void* ptr); + + private: + static void* mpBuf; + static EGG::Heap* mpParentHeap; + static EGG::ExpHeap* mpHeap; + static EGG::Allocator* mpAllocator; + Interface* mControllers[4]; + u32 mControllerStorage[4][12]; + Master mMaster; + KPADStatus mKPADStatus[4]; + s32 mInvalidCount[4]; + }; +#else class Base { public: Base(int chan, KPADStatus& arg1) { @@ -118,11 +312,11 @@ namespace ipl { virtual bool setForceInvalid(bool flag); // 0x74 - virtual f32 getMainStickX() const; // 0x78 - virtual f32 getMainStickY() const; // 0x7C + virtual int getMainStickX() const; // 0x78 + virtual int getMainStickY() const; // 0x7C - virtual f32 getSubStickX() const; // 0x80 - virtual f32 getSubStickY() const; // 0x84 + virtual int getSubStickX() const; // 0x80 + virtual int getSubStickY() const; // 0x84 virtual void read(); // 0x88 @@ -169,10 +363,10 @@ namespace ipl { virtual math::VEC2 getDpdPos() const override; virtual math::VEC2 getDpdProjectionPos() const override; virtual math::VEC2 getHorizon() const override; - virtual f32 getMainStickX() const override; - virtual f32 getMainStickY() const override; - virtual f32 getSubStickX() const override; - virtual f32 getSubStickY() const override; + virtual int getMainStickX() const override; + virtual int getMainStickY() const override; + virtual int getSubStickX() const override; + virtual int getSubStickY() const override; virtual BOOL rumble(int type = 0) override; virtual void read() override; }; @@ -228,6 +422,7 @@ namespace ipl { class Classic : public Revolution { public: Classic(int arg0, KPADStatus& arg1); + virtual ~Classic(); virtual bool down(u32 button) const override; virtual bool downTrg(u32 button) const override; @@ -268,6 +463,7 @@ namespace ipl { static EGG::Allocator* mpAllocator; u8 dummy[0x2F8]; }; +#endif } // namespace controller } // namespace ipl diff --git a/libs/NW4R/include/nw4r/ut/Rect.h b/libs/NW4R/include/nw4r/ut/Rect.h index 441c702b..94551133 100644 --- a/libs/NW4R/include/nw4r/ut/Rect.h +++ b/libs/NW4R/include/nw4r/ut/Rect.h @@ -16,7 +16,9 @@ namespace nw4r { #endif Rect(f32 l, f32 t, f32 r, f32 b) : left(l), top(t), right(r), bottom(b) {} +#ifndef IPL_CONTROLLER_TRIVIAL_RECT_DTOR ~Rect() {} +#endif void SetWidth(f32 width) { right = left + width; } void SetHeight(f32 height) { bottom = top + height; } diff --git a/src/system/iplController.cpp b/src/system/iplController.cpp index a1a9d3e9..70e13e6f 100644 --- a/src/system/iplController.cpp +++ b/src/system/iplController.cpp @@ -1,8 +1,14 @@ #include +#define IPL_CONTROLLER_OUT_OF_LINE_VEC2 +#define IPL_CONTROLLER_OUT_OF_LINE_ABS_CLAMP #include +#undef IPL_CONTROLLER_OUT_OF_LINE_ABS_CLAMP +#undef IPL_CONTROLLER_OUT_OF_LINE_VEC2 +#define IPL_CONTROLLER_TRIVIAL_RECT_DTOR #define IPL_SOUND_RECT_OUT_OF_LINE #include #undef IPL_SOUND_RECT_OUT_OF_LINE +#undef IPL_CONTROLLER_TRIVIAL_RECT_DTOR #include #include #include @@ -10,909 +16,25 @@ #include #include +#define IPL_CONTROLLER_NATIVE_HIERARCHY +#define IPL_CONTROLLER_INLINE_INTERFACE #include "system/iplController.h" +#undef IPL_CONTROLLER_INLINE_INTERFACE +#undef IPL_CONTROLLER_NATIVE_HIERARCHY #include "system/iplSystem.h" -const f32 lbl_8160D2C0[] = {0.2, 0.3, 0, 0}; -extern "C" void __ct__Q34nw4r2ut4RectFv(); -typedef void (*ControllerDataFunction)(); -#pragma push -#pragma section const_type ".data" -extern "C" unsigned int const lbl_816346A8[3] = {0, 0x14, 0}; -extern "C" unsigned int const lbl_816346B4[3] = {0, 0x18, 0}; -extern "C" unsigned int const lbl_816346C0[3] = {0, 0x1c, 0}; -extern "C" unsigned int const lbl_816346CC[3] = {0, 0x30, 0}; -extern "C" void __dt__Q33ipl10controller6MasterFv(); -extern "C" void getType__Q33ipl10controller9InterfaceCFv(); -extern "C" void getChannel__Q33ipl10controller9InterfaceCFv(); -extern "C" void down__Q33ipl10controller6MasterCFUl(); -extern "C" void downTrg__Q33ipl10controller6MasterCFUl(); -extern "C" void upTrg__Q33ipl10controller6MasterCFUl(); -extern "C" void pinch__Q33ipl10controller9InterfaceCFv(); -extern "C" void pinchTrg__Q33ipl10controller9InterfaceCFv(); -extern "C" void pinchOffTrg__Q33ipl10controller9InterfaceCFv(); -extern "C" void decide__Q33ipl10controller6MasterCFv(); -extern "C" void repeat__Q33ipl10controller6MasterCFUl(); -extern "C" void rumble__Q33ipl10controller9InterfaceFi(); -extern "C" void cancelRumbling__Q33ipl10controller9InterfaceFv(); -extern "C" void getHoldFlag__Q33ipl10controller9InterfaceCFv(); -extern "C" void getTrigFlag__Q33ipl10controller9InterfaceCFv(); -extern "C" void getReleaseFlag__Q33ipl10controller9InterfaceCFv(); -extern "C" void getClassicHoldFlag__Q33ipl10controller9InterfaceCFv(); -extern "C" void getClassicTrigFlag__Q33ipl10controller9InterfaceCFv(); -extern "C" void getClassicReleaseFlag__Q33ipl10controller9InterfaceCFv(); -extern "C" void getDpdPos__Q33ipl10controller9InterfaceCFv(); -extern "C" void getDpdProjectionPos__Q33ipl10controller9InterfaceCFv(); -extern "C" void getHorizon__Q33ipl10controller9InterfaceCFv(); -extern "C" void getDpdDistance__Q33ipl10controller9InterfaceCFv(); -extern "C" void getKPADStatus__Q33ipl10controller9InterfaceCFv(); -extern "C" void getPADStatus__Q33ipl10controller9InterfaceCFv(); -extern "C" void isValidBtn__Q33ipl10controller9InterfaceCFv(); -extern "C" void isValidDpd__Q33ipl10controller9InterfaceCFv(); -extern "C" void setForceInvalid__Q33ipl10controller6MasterFb(); -extern "C" void setForceInvalid__Q33ipl10controller4BaseFb(); -extern "C" void getMainStickX__Q33ipl10controller9InterfaceCFv(); -extern "C" void getMainStickY__Q33ipl10controller9InterfaceCFv(); -extern "C" void getSubStickX__Q33ipl10controller9InterfaceCFv(); -extern "C" void getSubStickY__Q33ipl10controller9InterfaceCFv(); -extern "C" void read__Q33ipl10controller9InterfaceFv(); -extern "C" void __dt__Q33ipl10controller7ClassicFv(); -extern "C" void getType__Q33ipl10controller4BaseCFv(); -extern "C" void getChannel__Q33ipl10controller4BaseCFv(); -extern "C" void down__Q33ipl10controller7ClassicCFUl(); -extern "C" void downTrg__Q33ipl10controller7ClassicCFUl(); -extern "C" void upTrg__Q33ipl10controller7ClassicCFUl(); -extern "C" void pinch__Q33ipl10controller10RevolutionCFv(); -extern "C" void pinchTrg__Q33ipl10controller10RevolutionCFv(); -extern "C" void pinchOffTrg__Q33ipl10controller10RevolutionCFv(); -extern "C" void decide__Q33ipl10controller4BaseCFv(); -extern "C" void repeat__Q33ipl10controller10RevolutionCFUl(); -extern "C" void rumble__Q33ipl10controller4BaseFi(); -extern "C" void cancelRumbling__Q33ipl10controller4BaseFv(); -extern "C" void getHoldFlag__Q33ipl10controller10RevolutionCFv(); -extern "C" void getTrigFlag__Q33ipl10controller10RevolutionCFv(); -extern "C" void getReleaseFlag__Q33ipl10controller10RevolutionCFv(); -extern "C" void getClassicHoldFlag__Q33ipl10controller7ClassicCFv(); -extern "C" void getClassicTrigFlag__Q33ipl10controller7ClassicCFv(); -extern "C" void getClassicReleaseFlag__Q33ipl10controller7ClassicCFv(); -extern "C" void getDpdPos__Q33ipl10controller7ClassicCFv(); -extern "C" void getDpdProjectionPos__Q33ipl10controller7ClassicCFv(); -extern "C" void getHorizon__Q33ipl10controller7ClassicCFv(); -extern "C" void getDpdDistance__Q33ipl10controller10RevolutionCFv(); -extern "C" void getKPADStatus__Q33ipl10controller10RevolutionCFv(); -extern "C" void isValidBtn__Q33ipl10controller10RevolutionCFv(); -extern "C" void isValidDpd__Q33ipl10controller7ClassicCFv(); -extern "C" void read__Q33ipl10controller7ClassicFv(); -extern "C" void isValidDpdClassic__Q33ipl10controller7ClassicCFv(); -extern "C" void __dt__Q33ipl10controller10RevolutionFv(); -extern "C" void down__Q33ipl10controller10RevolutionCFUl(); -extern "C" void downTrg__Q33ipl10controller10RevolutionCFUl(); -extern "C" void upTrg__Q33ipl10controller10RevolutionCFUl(); -extern "C" void getDpdPos__Q33ipl10controller10RevolutionCFv(); -extern "C" void getDpdProjectionPos__Q33ipl10controller10RevolutionCFv(); -extern "C" void getHorizon__Q33ipl10controller10RevolutionCFv(); -extern "C" void isValidDpd__Q33ipl10controller10RevolutionCFv(); -extern "C" void read__Q33ipl10controller10RevolutionFv(); -extern "C" void __dt__Q33ipl10controller4BaseFv(); -extern "C" void down__Q33ipl10controller9InterfaceCFUl(); -extern "C" void downTrg__Q33ipl10controller9InterfaceCFUl(); -extern "C" void upTrg__Q33ipl10controller9InterfaceCFUl(); -extern "C" void repeat__Q33ipl10controller9InterfaceCFUl(); -extern "C" void read__Q33ipl10controller4BaseFv(); -extern "C" void __dt__Q33ipl10controller9FreeStyleFv(); -extern "C" void __dt__Q33ipl10controller4CoreFv(); -extern "C" ControllerDataFunction const __vt__Q33ipl10controller6Master[35] = { - 0, - 0, - __dt__Q33ipl10controller6MasterFv, - getType__Q33ipl10controller9InterfaceCFv, - getChannel__Q33ipl10controller9InterfaceCFv, - down__Q33ipl10controller6MasterCFUl, - downTrg__Q33ipl10controller6MasterCFUl, - upTrg__Q33ipl10controller6MasterCFUl, - pinch__Q33ipl10controller9InterfaceCFv, - pinchTrg__Q33ipl10controller9InterfaceCFv, - pinchOffTrg__Q33ipl10controller9InterfaceCFv, - decide__Q33ipl10controller6MasterCFv, - repeat__Q33ipl10controller6MasterCFUl, - rumble__Q33ipl10controller9InterfaceFi, - cancelRumbling__Q33ipl10controller9InterfaceFv, - getHoldFlag__Q33ipl10controller9InterfaceCFv, - getTrigFlag__Q33ipl10controller9InterfaceCFv, - getReleaseFlag__Q33ipl10controller9InterfaceCFv, - getClassicHoldFlag__Q33ipl10controller9InterfaceCFv, - getClassicTrigFlag__Q33ipl10controller9InterfaceCFv, - getClassicReleaseFlag__Q33ipl10controller9InterfaceCFv, - getDpdPos__Q33ipl10controller9InterfaceCFv, - getDpdProjectionPos__Q33ipl10controller9InterfaceCFv, - getHorizon__Q33ipl10controller9InterfaceCFv, - getDpdDistance__Q33ipl10controller9InterfaceCFv, - getKPADStatus__Q33ipl10controller9InterfaceCFv, - getPADStatus__Q33ipl10controller9InterfaceCFv, - isValidBtn__Q33ipl10controller9InterfaceCFv, - isValidDpd__Q33ipl10controller9InterfaceCFv, - setForceInvalid__Q33ipl10controller6MasterFb, - getMainStickX__Q33ipl10controller9InterfaceCFv, - getMainStickY__Q33ipl10controller9InterfaceCFv, - getSubStickX__Q33ipl10controller9InterfaceCFv, - getSubStickY__Q33ipl10controller9InterfaceCFv, - read__Q33ipl10controller9InterfaceFv, -}; -extern "C" ControllerDataFunction const __vt__Q33ipl10controller7Classic[36] = { - 0, - 0, - __dt__Q33ipl10controller7ClassicFv, - getType__Q33ipl10controller4BaseCFv, - getChannel__Q33ipl10controller4BaseCFv, - down__Q33ipl10controller7ClassicCFUl, - downTrg__Q33ipl10controller7ClassicCFUl, - upTrg__Q33ipl10controller7ClassicCFUl, - pinch__Q33ipl10controller10RevolutionCFv, - pinchTrg__Q33ipl10controller10RevolutionCFv, - pinchOffTrg__Q33ipl10controller10RevolutionCFv, - decide__Q33ipl10controller4BaseCFv, - repeat__Q33ipl10controller10RevolutionCFUl, - rumble__Q33ipl10controller4BaseFi, - cancelRumbling__Q33ipl10controller4BaseFv, - getHoldFlag__Q33ipl10controller10RevolutionCFv, - getTrigFlag__Q33ipl10controller10RevolutionCFv, - getReleaseFlag__Q33ipl10controller10RevolutionCFv, - getClassicHoldFlag__Q33ipl10controller7ClassicCFv, - getClassicTrigFlag__Q33ipl10controller7ClassicCFv, - getClassicReleaseFlag__Q33ipl10controller7ClassicCFv, - getDpdPos__Q33ipl10controller7ClassicCFv, - getDpdProjectionPos__Q33ipl10controller7ClassicCFv, - getHorizon__Q33ipl10controller7ClassicCFv, - getDpdDistance__Q33ipl10controller10RevolutionCFv, - getKPADStatus__Q33ipl10controller10RevolutionCFv, - getPADStatus__Q33ipl10controller9InterfaceCFv, - isValidBtn__Q33ipl10controller10RevolutionCFv, - isValidDpd__Q33ipl10controller7ClassicCFv, - setForceInvalid__Q33ipl10controller4BaseFb, - getMainStickX__Q33ipl10controller9InterfaceCFv, - getMainStickY__Q33ipl10controller9InterfaceCFv, - getSubStickX__Q33ipl10controller9InterfaceCFv, - getSubStickY__Q33ipl10controller9InterfaceCFv, - read__Q33ipl10controller7ClassicFv, - isValidDpdClassic__Q33ipl10controller7ClassicCFv, -}; -extern "C" ControllerDataFunction const __vt__Q33ipl10controller10Revolution[35] = { - 0, - 0, - __dt__Q33ipl10controller10RevolutionFv, - getType__Q33ipl10controller4BaseCFv, - getChannel__Q33ipl10controller4BaseCFv, - down__Q33ipl10controller10RevolutionCFUl, - downTrg__Q33ipl10controller10RevolutionCFUl, - upTrg__Q33ipl10controller10RevolutionCFUl, - pinch__Q33ipl10controller10RevolutionCFv, - pinchTrg__Q33ipl10controller10RevolutionCFv, - pinchOffTrg__Q33ipl10controller10RevolutionCFv, - decide__Q33ipl10controller4BaseCFv, - repeat__Q33ipl10controller10RevolutionCFUl, - rumble__Q33ipl10controller4BaseFi, - cancelRumbling__Q33ipl10controller4BaseFv, - getHoldFlag__Q33ipl10controller10RevolutionCFv, - getTrigFlag__Q33ipl10controller10RevolutionCFv, - getReleaseFlag__Q33ipl10controller10RevolutionCFv, - getClassicHoldFlag__Q33ipl10controller9InterfaceCFv, - getClassicTrigFlag__Q33ipl10controller9InterfaceCFv, - getClassicReleaseFlag__Q33ipl10controller9InterfaceCFv, - getDpdPos__Q33ipl10controller10RevolutionCFv, - getDpdProjectionPos__Q33ipl10controller10RevolutionCFv, - getHorizon__Q33ipl10controller10RevolutionCFv, - getDpdDistance__Q33ipl10controller10RevolutionCFv, - getKPADStatus__Q33ipl10controller10RevolutionCFv, - getPADStatus__Q33ipl10controller9InterfaceCFv, - isValidBtn__Q33ipl10controller10RevolutionCFv, - isValidDpd__Q33ipl10controller10RevolutionCFv, - setForceInvalid__Q33ipl10controller4BaseFb, - getMainStickX__Q33ipl10controller9InterfaceCFv, - getMainStickY__Q33ipl10controller9InterfaceCFv, - getSubStickX__Q33ipl10controller9InterfaceCFv, - getSubStickY__Q33ipl10controller9InterfaceCFv, - read__Q33ipl10controller10RevolutionFv, -}; -extern "C" ControllerDataFunction const __vt__Q33ipl10controller4Base[36] = { - 0, - 0, - __dt__Q33ipl10controller4BaseFv, - getType__Q33ipl10controller4BaseCFv, - getChannel__Q33ipl10controller4BaseCFv, - down__Q33ipl10controller9InterfaceCFUl, - downTrg__Q33ipl10controller9InterfaceCFUl, - upTrg__Q33ipl10controller9InterfaceCFUl, - pinch__Q33ipl10controller9InterfaceCFv, - pinchTrg__Q33ipl10controller9InterfaceCFv, - pinchOffTrg__Q33ipl10controller9InterfaceCFv, - decide__Q33ipl10controller4BaseCFv, - repeat__Q33ipl10controller9InterfaceCFUl, - rumble__Q33ipl10controller4BaseFi, - cancelRumbling__Q33ipl10controller4BaseFv, - getHoldFlag__Q33ipl10controller9InterfaceCFv, - getTrigFlag__Q33ipl10controller9InterfaceCFv, - getReleaseFlag__Q33ipl10controller9InterfaceCFv, - getClassicHoldFlag__Q33ipl10controller9InterfaceCFv, - getClassicTrigFlag__Q33ipl10controller9InterfaceCFv, - getClassicReleaseFlag__Q33ipl10controller9InterfaceCFv, - getDpdPos__Q33ipl10controller9InterfaceCFv, - getDpdProjectionPos__Q33ipl10controller9InterfaceCFv, - getHorizon__Q33ipl10controller9InterfaceCFv, - getDpdDistance__Q33ipl10controller9InterfaceCFv, - getKPADStatus__Q33ipl10controller9InterfaceCFv, - getPADStatus__Q33ipl10controller9InterfaceCFv, - isValidBtn__Q33ipl10controller9InterfaceCFv, - isValidDpd__Q33ipl10controller9InterfaceCFv, - setForceInvalid__Q33ipl10controller4BaseFb, - getMainStickX__Q33ipl10controller9InterfaceCFv, - getMainStickY__Q33ipl10controller9InterfaceCFv, - getSubStickX__Q33ipl10controller9InterfaceCFv, - getSubStickY__Q33ipl10controller9InterfaceCFv, - read__Q33ipl10controller4BaseFv, - 0, -}; -extern "C" ControllerDataFunction const __vt__Q33ipl10controller9FreeStyle[35] = { - 0, - 0, - __dt__Q33ipl10controller9FreeStyleFv, - getType__Q33ipl10controller4BaseCFv, - getChannel__Q33ipl10controller4BaseCFv, - down__Q33ipl10controller10RevolutionCFUl, - downTrg__Q33ipl10controller10RevolutionCFUl, - upTrg__Q33ipl10controller10RevolutionCFUl, - pinch__Q33ipl10controller10RevolutionCFv, - pinchTrg__Q33ipl10controller10RevolutionCFv, - pinchOffTrg__Q33ipl10controller10RevolutionCFv, - decide__Q33ipl10controller4BaseCFv, - repeat__Q33ipl10controller10RevolutionCFUl, - rumble__Q33ipl10controller4BaseFi, - cancelRumbling__Q33ipl10controller4BaseFv, - getHoldFlag__Q33ipl10controller10RevolutionCFv, - getTrigFlag__Q33ipl10controller10RevolutionCFv, - getReleaseFlag__Q33ipl10controller10RevolutionCFv, - getClassicHoldFlag__Q33ipl10controller9InterfaceCFv, - getClassicTrigFlag__Q33ipl10controller9InterfaceCFv, - getClassicReleaseFlag__Q33ipl10controller9InterfaceCFv, - getDpdPos__Q33ipl10controller10RevolutionCFv, - getDpdProjectionPos__Q33ipl10controller10RevolutionCFv, - getHorizon__Q33ipl10controller10RevolutionCFv, - getDpdDistance__Q33ipl10controller10RevolutionCFv, - getKPADStatus__Q33ipl10controller10RevolutionCFv, - getPADStatus__Q33ipl10controller9InterfaceCFv, - isValidBtn__Q33ipl10controller10RevolutionCFv, - isValidDpd__Q33ipl10controller10RevolutionCFv, - setForceInvalid__Q33ipl10controller4BaseFb, - getMainStickX__Q33ipl10controller9InterfaceCFv, - getMainStickY__Q33ipl10controller9InterfaceCFv, - getSubStickX__Q33ipl10controller9InterfaceCFv, - getSubStickY__Q33ipl10controller9InterfaceCFv, - read__Q33ipl10controller10RevolutionFv, -}; -extern "C" ControllerDataFunction const __vt__Q33ipl10controller4Core[35] = { - 0, - 0, - __dt__Q33ipl10controller4CoreFv, - getType__Q33ipl10controller4BaseCFv, - getChannel__Q33ipl10controller4BaseCFv, - down__Q33ipl10controller10RevolutionCFUl, - downTrg__Q33ipl10controller10RevolutionCFUl, - upTrg__Q33ipl10controller10RevolutionCFUl, - pinch__Q33ipl10controller10RevolutionCFv, - pinchTrg__Q33ipl10controller10RevolutionCFv, - pinchOffTrg__Q33ipl10controller10RevolutionCFv, - decide__Q33ipl10controller4BaseCFv, - repeat__Q33ipl10controller10RevolutionCFUl, - rumble__Q33ipl10controller4BaseFi, - cancelRumbling__Q33ipl10controller4BaseFv, - getHoldFlag__Q33ipl10controller10RevolutionCFv, - getTrigFlag__Q33ipl10controller10RevolutionCFv, - getReleaseFlag__Q33ipl10controller10RevolutionCFv, - getClassicHoldFlag__Q33ipl10controller9InterfaceCFv, - getClassicTrigFlag__Q33ipl10controller9InterfaceCFv, - getClassicReleaseFlag__Q33ipl10controller9InterfaceCFv, - getDpdPos__Q33ipl10controller10RevolutionCFv, - getDpdProjectionPos__Q33ipl10controller10RevolutionCFv, - getHorizon__Q33ipl10controller10RevolutionCFv, - getDpdDistance__Q33ipl10controller10RevolutionCFv, - getKPADStatus__Q33ipl10controller10RevolutionCFv, - getPADStatus__Q33ipl10controller9InterfaceCFv, - isValidBtn__Q33ipl10controller10RevolutionCFv, - isValidDpd__Q33ipl10controller10RevolutionCFv, - setForceInvalid__Q33ipl10controller4BaseFb, - getMainStickX__Q33ipl10controller9InterfaceCFv, - getMainStickY__Q33ipl10controller9InterfaceCFv, - getSubStickX__Q33ipl10controller9InterfaceCFv, - getSubStickY__Q33ipl10controller9InterfaceCFv, - read__Q33ipl10controller10RevolutionFv, -}; -#pragma pop -extern const f32 lbl_81694440 = 1000.0f; -extern const f32 lbl_81694444 = 0.058333333f; -extern const f64 lbl_81694448 = 4503599627370496.0; -extern const f32 lbl_81694450 = 1.0f / 0.0f; -extern const f32 lbl_81694454 = 0.0f; -extern const f32 lbl_81694458 = 1.10132003f; -extern const f32 lbl_8169445C = 1.15f; -extern const f32 lbl_81694460 = 100.0f; -extern const f32 lbl_81694464 = 0.01f; -extern const f32 lbl_81694468 = 0.0036f; -extern const f32 lbl_8169446C = 0.05f; -extern const f32 lbl_81694470 = 1.0f; -extern const f32 lbl_81694474 = -0.2679492f; -extern const f32 lbl_81694478 = 0.5f; -extern const f32 lbl_8169447C = 0.1f; -extern const f32 lbl_81694480 = 0.2f; -extern const f32 lbl_81694484 = -0.2f; -extern const f32 lbl_81694488 = 0.05f; -extern const f32 lbl_8169448C = 1.0f; -extern "C" void* mpBuf__Q33ipl10controller7Manager = 0; -extern "C" void* mpParentHeap__Q33ipl10controller7Manager = 0; -extern "C" void* mpHeap__Q33ipl10controller7Manager = 0; -extern "C" void* mpAllocator__Q33ipl10controller7Manager; -extern "C" void _savegpr_25(); -extern "C" void _restgpr_25(); -extern "C" void _savegpr_16(); -extern "C" void _restgpr_16(); -extern "C" void _savegpr_29(); -extern "C" void _restgpr_29(); -extern "C" void _savegpr_27(); -extern "C" void _restgpr_27(); -extern "C" void _savegpr_28(); -extern "C" void _restgpr_28(); -extern "C" void __ptmf_scall(); -extern "C" void __ct__Q33ipl4math4VEC2Fff(); -extern "C" void set__Q33ipl4math4VEC2Fff(); -extern "C" void __dt__Q33ipl10controller10RevolutionFv(); -extern "C" void read__Q33ipl10controller4BaseFv(); -extern "C" void isValidDpd__Q33ipl10controller10RevolutionCFv(); -extern "C" void getProjectionRect__Q23ipl6SystemFPQ34nw4r2ut4Rect(); -extern "C" void __as__Q33ipl4math4VEC2FRCQ33ipl4math4VEC2(); -extern "C" void __ct__Q33ipl10controller10RevolutionFiiR10KPADStatus(); -extern "C" void __ct__Q33ipl10controller7ClassicFiR10KPADStatus(); -extern "C" asm void alloc__Q33ipl10controller7ManagerFUl(); -extern "C" asm void free__Q33ipl10controller7ManagerFPv(); -extern "C" void __ct__Q23EGG9AllocatorFPQ23EGG4Heapl(); -extern "C" void create__Q23EGG7ExpHeapFPvUlUs(); -extern "C" void __nw__FUlPQ23EGG4Heapi(); -extern "C" void read__Q33ipl10controller7ManagerFv(); -extern "C" void __dl__FPv(); - -extern "C" asm void __ct__Q34nw4r2ut4RectFv() { - nofralloc - lfs f0, lbl_81694454 - stfs f0, 0(r3) - stfs f0, 4(r3) - stfs f0, 8(r3) - stfs f0, 0xc(r3) - blr -} - -extern "C" asm void __dt__Q33ipl10controller7ClassicFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - cmpwi r3, 0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r4 - stw r30, 0x8(r1) - mr r30, r3 - beq classic_dtor_done - li r4, 0 - bl __dt__Q33ipl10controller10RevolutionFv - cmpwi r31, 0 - ble classic_dtor_done - mr r3, r30 - bl __dl__FPv -classic_dtor_done: - mr r3, r30 - lwz r31, 0xc(r1) - lwz r30, 0x8(r1) - lwz r0, 0x14(r1) - mtlr r0 - addi r1, r1, 0x10 - blr -} - -extern "C" asm void __dt__Q33ipl10controller4CoreFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - cmpwi r3, 0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r4 - stw r30, 0x8(r1) - mr r30, r3 - beq core_dtor_done - li r4, 0 - bl __dt__Q33ipl10controller10RevolutionFv - cmpwi r31, 0 - ble core_dtor_done - mr r3, r30 - bl __dl__FPv -core_dtor_done: - mr r3, r30 - lwz r31, 0xc(r1) - lwz r30, 0x8(r1) - lwz r0, 0x14(r1) - mtlr r0 - addi r1, r1, 0x10 - blr -} - -extern "C" asm void __dt__Q33ipl10controller9FreeStyleFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - cmpwi r3, 0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r4 - stw r30, 0x8(r1) - mr r30, r3 - beq freestyle_dtor_done - li r4, 0 - bl __dt__Q33ipl10controller10RevolutionFv - cmpwi r31, 0 - ble freestyle_dtor_done - mr r3, r30 - bl __dl__FPv -freestyle_dtor_done: - mr r3, r30 - lwz r31, 0xc(r1) - lwz r30, 0x8(r1) - lwz r0, 0x14(r1) - mtlr r0 - addi r1, r1, 0x10 - blr -} - -extern "C" asm void getMainStickX__Q33ipl10controller9InterfaceCFv() { - nofralloc - li r3, 0 - blr -} - -extern "C" asm void getMainStickY__Q33ipl10controller9InterfaceCFv() { - nofralloc - li r3, 0 - blr -} - -extern "C" asm void getSubStickX__Q33ipl10controller9InterfaceCFv() { - nofralloc - li r3, 0 - blr -} - -extern "C" asm void getSubStickY__Q33ipl10controller9InterfaceCFv() { - nofralloc - li r3, 0 - blr -} - -extern "C" asm void getDpdPos__Q33ipl10controller10RevolutionCFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - lwz r4, 0x20(r3) - stw r0, 0x14(r1) - addi r3, r1, 8 - lfs f1, 0x20(r4) - lfs f2, 0x24(r4) - bl __ct__Q33ipl4math4VEC2Fff - lwz r0, 0x14(r1) - mr r4, r3 - lwz r3, 0(r3) - lwz r4, 4(r4) - mtlr r0 - addi r1, r1, 0x10 - blr -} - -extern "C" asm void getHorizon__Q33ipl10controller10RevolutionCFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - lwz r4, 0x20(r3) - stw r0, 0x14(r1) - addi r3, r1, 8 - lfs f1, 0x34(r4) - lfs f2, 0x38(r4) - bl __ct__Q33ipl4math4VEC2Fff - lwz r0, 0x14(r1) - mr r4, r3 - lwz r3, 0(r3) - lwz r4, 4(r4) - mtlr r0 - addi r1, r1, 0x10 - blr -} - -extern "C" asm void repeat__Q33ipl10controller10RevolutionCFUl() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - li r31, 0 - stw r30, 8(r1) - mr r30, r3 - lwz r12, 0(r3) - lwz r12, 0x14(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq iplController_Revolution_repeat_L1 - lwz r3, 0x20(r30) - lwz r0, 0(r3) - rlwinm. r0, r0, 0, 0, 0 - beq iplController_Revolution_repeat_L1 - li r31, 1 -iplController_Revolution_repeat_L1: - mr r3, r31 - lwz r31, 0xc(r1) - lwz r30, 8(r1) - lwz r0, 0x14(r1) - mtlr r0 - addi r1, r1, 0x10 - blr -} - -extern "C" asm void down__Q33ipl10controller7ClassicCFUl() { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_29 - lwz r12, 0(r3) - mr r29, r3 - mr r30, r4 - li r31, 0 - lwz r12, 0x6c(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq iplController_Classic_down_L1 - lwz r3, 0x20(r29) - li r4, 1 - lwz r0, 0(r3) - and r0, r0, r30 - clrlwi. r0, r0, 16 - bne iplController_Classic_down_L2 - lwz r3, 0x60(r3) - srwi r0, r30, 16 - and. r0, r3, r0 - bne iplController_Classic_down_L2 - li r4, 0 -iplController_Classic_down_L2: - cmpwi r4, 0 - beq iplController_Classic_down_L1 - li r31, 1 -iplController_Classic_down_L1: - addi r11, r1, 0x20 - mr r3, r31 - bl _restgpr_29 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr -} - -extern "C" asm void downTrg__Q33ipl10controller7ClassicCFUl() { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_29 - lwz r12, 0(r3) - mr r29, r3 - mr r30, r4 - li r31, 0 - lwz r12, 0x6c(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq iplController_Classic_downTrg_L1 - lwz r5, 0x20(r29) - clrlwi r0, r30, 16 - li r4, 1 - lwz r3, 4(r5) - and. r0, r3, r0 - bne iplController_Classic_downTrg_L2 - lwz r3, 0x64(r5) - srwi r0, r30, 16 - and. r0, r3, r0 - bne iplController_Classic_downTrg_L2 - li r4, 0 -iplController_Classic_downTrg_L2: - cmpwi r4, 0 - beq iplController_Classic_downTrg_L1 - li r31, 1 -iplController_Classic_downTrg_L1: - addi r11, r1, 0x20 - mr r3, r31 - bl _restgpr_29 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr -} - -extern "C" asm void upTrg__Q33ipl10controller7ClassicCFUl() { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_29 - lwz r12, 0(r3) - mr r29, r3 - mr r30, r4 - li r31, 0 - lwz r12, 0x6c(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq iplController_Classic_upTrg_L1 - lwz r5, 0x20(r29) - clrlwi r0, r30, 16 - li r4, 1 - lwz r3, 8(r5) - and. r0, r3, r0 - bne iplController_Classic_upTrg_L2 - lwz r3, 0x68(r5) - srwi r0, r30, 16 - and. r0, r3, r0 - bne iplController_Classic_upTrg_L2 - li r4, 0 -iplController_Classic_upTrg_L2: - cmpwi r4, 0 - beq iplController_Classic_upTrg_L1 - li r31, 1 -iplController_Classic_upTrg_L1: - addi r11, r1, 0x20 - mr r3, r31 - bl _restgpr_29 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr -} - -extern "C" asm void downTrg__Q33ipl10controller10RevolutionCFUl() { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_29 - lwz r12, 0(r3) - mr r29, r3 - mr r30, r4 - li r31, 0 - lwz r12, 0x6c(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq iplController_Revolution_downTrg_L1 - lwz r3, 0x20(r29) - clrlwi r0, r30, 16 - lwz r3, 4(r3) - and. r0, r3, r0 - beq iplController_Revolution_downTrg_L1 - li r31, 1 -iplController_Revolution_downTrg_L1: - addi r11, r1, 0x20 - mr r3, r31 - bl _restgpr_29 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr -} - -extern "C" asm void upTrg__Q33ipl10controller10RevolutionCFUl() { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_29 - lwz r12, 0(r3) - mr r29, r3 - mr r30, r4 - li r31, 0 - lwz r12, 0x6c(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq iplController_Revolution_upTrg_L1 - lwz r3, 0x20(r29) - clrlwi r0, r30, 16 - lwz r3, 8(r3) - and. r0, r3, r0 - beq iplController_Revolution_upTrg_L1 - li r31, 1 -iplController_Revolution_upTrg_L1: - addi r11, r1, 0x20 - mr r3, r31 - bl _restgpr_29 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr -} - -extern "C" asm void decide__Q33ipl10controller6MasterCFv() { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_27 - mr r27, r3 - li r29, 0 - li r28, 0 - li r31, 0 -iplController_Master_decide_L1: - lwz r3, 4(r27) - li r30, 0 - lwzx r3, r3, r31 - cmpwi r3, 0 - beq iplController_Master_decide_L2 - lwz r12, 0(r3) - lwz r12, 0x2c(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq iplController_Master_decide_L2 - li r30, 1 -iplController_Master_decide_L2: - addi r28, r28, 1 - or r3, r29, r30 - cmpwi r28, 4 - addi r31, r31, 4 - addic r0, r3, -1 - subfe r29, r0, r3 - blt iplController_Master_decide_L1 - addi r11, r1, 0x20 - mr r3, r29 - bl _restgpr_27 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr -} - -extern "C" asm void setForceInvalid__Q33ipl10controller6MasterFb() { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_28 - mr r28, r3 - mr r29, r4 - li r30, 0 - li r31, 0 -iplController_Master_setForceInvalid_L1: - lwz r3, 4(r28) - lwzx r3, r3, r31 - cmpwi r3, 0 - beq iplController_Master_setForceInvalid_L2 - lwz r12, 0(r3) - mr r4, r29 - lwz r12, 0x74(r12) - mtctr r12 - bctrl -iplController_Master_setForceInvalid_L2: - addi r30, r30, 1 - addi r31, r31, 4 - cmpwi r30, 4 - blt iplController_Master_setForceInvalid_L1 - addi r11, r1, 0x20 - bl _restgpr_28 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr -} - -extern "C" asm void __ct__Q33ipl10controller10RevolutionFiiR10KPADStatus() { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_29 - lis r7, __vt__Q33ipl10controller4Base@ha - li r31, 0 - addi r7, r7, __vt__Q33ipl10controller4Base@l - li r0, -1 - stw r7, 0(r3) - mr r29, r3 - mr r30, r6 - stb r31, 4(r3) - stw r31, 8(r3) - stw r31, 0xc(r3) - stw r0, 0x10(r3) - stw r4, 0x14(r3) - stw r5, 0x18(r3) - stb r31, 0x1c(r3) - mr r3, r4 - bl KPADEnableDPD - lis r3, __vt__Q33ipl10controller10Revolution@ha - stb r31, 0x1d(r29) - addi r3, r3, __vt__Q33ipl10controller10Revolution@l - addi r11, r1, 0x20 - stw r3, 0(r29) - mr r3, r29 - stb r31, 0x1e(r29) - stw r30, 0x20(r29) - bl _restgpr_29 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr -} - -extern "C" asm void call__Q33ipl10controller6MasterCFUlMQ33ipl10controller9InterfaceFPCvPCvUl_b() { - nofralloc - stwu r1, -0x30(r1) - mflr r0 - stw r0, 0x34(r1) - addi r11, r1, 0x30 - bl _savegpr_25 - mr r25, r3 - mr r26, r4 - mr r27, r5 - li r29, 0 - li r28, 0 - li r31, 0 -iplController_Master_call_L1: - lwz r3, 4(r25) - li r30, 0 - lwzx r3, r3, r31 - cmpwi r3, 0 - beq iplController_Master_call_L2 - mr r4, r26 - mr r12, r27 - bl __ptmf_scall - nop - cmpwi r3, 0 - beq iplController_Master_call_L2 - li r30, 1 -iplController_Master_call_L2: - addi r28, r28, 1 - or r3, r29, r30 - cmpwi r28, 4 - addi r31, r31, 4 - subic r0, r3, 1 - subfe r29, r0, r3 - blt iplController_Master_call_L1 - addi r11, r1, 0x30 - mr r3, r29 - bl _restgpr_25 - lwz r0, 0x34(r1) - mtlr r0 - addi r1, r1, 0x30 - blr -} - namespace ipl { - namespace math { - void VEC2::operator=(const VEC2& r) { - x = r.x; - y = r.y; - } - } - namespace controller { - void Base::read() { + static const f32 kRumbleDuration[4] = {0.2f, 0.3f, 0.0f, 0.0f}; + typedef bool (Interface::*ButtonMember)(u32) const; + static ButtonMember kDownMember = &Interface::down; + static ButtonMember kDownTrgMember = &Interface::downTrg; + static ButtonMember kUpTrgMember = &Interface::upTrg; + static ButtonMember kRepeatMember = &Interface::repeat; + } +} + + void ipl::controller::Base::read() { if (isValidBtn()) { if (downTrg(BTN_INTERACT)) { mButton = 1; @@ -939,10 +61,10 @@ namespace ipl { case 0: case 1: { u32 time = OSTicksToMilliseconds(OSGetTick() - mLastRumbleTime); - f32 f1 = (f32)time / lbl_81694440; - if (f1 < lbl_81694444) { + f32 f1 = (f32)time / 1000.0f; + if (f1 < 0.058333333f) { WPADControlMotor(mChan, 1); - } else if (f1 < lbl_8160D2C0[mRumbleType]) { + } else if (f1 < kRumbleDuration[mRumbleType]) { WPADControlMotor(mChan, 0); } else if (getKPADStatus() == NULL || getKPADStatus()->wpad_err == 0) { mLastRumbleTime = 0; @@ -957,8 +79,8 @@ namespace ipl { } case 2: { u32 time = OSTicksToMilliseconds(OSGetTick() - mLastRumbleTime); - f32 f1 = (f32)time / lbl_81694440; - if (f1 < lbl_8160D2C0[1]) { + f32 f1 = (f32)time / 1000.0f; + if (f1 < kRumbleDuration[1]) { WPADControlMotor(mChan, 0); } else if (getKPADStatus() == NULL || getKPADStatus()->wpad_err == 0) { mLastRumbleTime = 0; @@ -969,512 +91,10 @@ namespace ipl { } } } - - int Base::getType() const { - return mType; - } - - int Base::getChannel() const { - return mChan; - } - - bool Base::setForceInvalid(bool flag) { - unk_0x1C = flag; - } - - bool Interface::isValidBtn() const { - return false; - } - - bool Interface::downTrg(u32 mButton) const { - return 0; - } - - bool Interface::upTrg(u32 mButton) const { - return 0; - } - - int Interface::pinch() const { - return 0; - } - - int Interface::pinchTrg() const { - return 0; - } - - int Interface::pinchOffTrg() const { - return 0; - } - - bool Interface::down(u32 mButton) const { - return 0; - } - - bool Interface::repeat(u32 mButton) const { - return 0; - } - - bool Interface::isValidDpd() const { - return false; - } - - math::VEC2 Interface::getDpdPos() const { - math::VEC2 ret; - f32 zero = *((volatile const f32*)&lbl_81694454); - ret.x = zero; - ret.y = zero; - return ret; - } - - math::VEC2 Interface::getDpdProjectionPos() const { - math::VEC2 ret; - f32 zero = *((volatile const f32*)&lbl_81694454); - ret.x = zero; - ret.y = zero; - return ret; - } - - math::VEC2 Interface::getHorizon() const { - math::VEC2 ret; - f32 zero = *((volatile const f32*)&lbl_81694454); - ret.x = zero; - ret.y = zero; - return ret; - } - - int Interface::getClassicHoldFlag() const { - return 0; - } - - int Interface::getClassicTrigFlag() const { - return 0; - } - - int Interface::getClassicReleaseFlag() const { - return 0; - } - - KPADStatus* Interface::getKPADStatus() const { - return NULL; - } - - PADStatus* Interface::getPADStatus() const { - return NULL; - } - - f32 Interface::getDpdDistance() const { - return *((volatile const f32*)&lbl_81694454); - } - - int Interface::getHoldFlag() const { - return 0; - } - - int Interface::getReleaseFlag() const { - return 0; - } - - int Interface::getTrigFlag() const { - return 0; - } - - void Interface::cancelRumbling() { - } - - int Interface::getChannel() const { - return -1; - } - - int Interface::getType() const { - return -1; - } - - bool Master::down(u32 button) const { - return call(button, (bool (Interface::*)(u32) const)&Interface::down); - } - - bool Master::downTrg(u32 button) const { - return call(button, (bool (Interface::*)(u32) const)&Interface::downTrg); - } - - bool Master::upTrg(u32 button) const { - return call(button, (bool (Interface::*)(u32) const)&Interface::upTrg); - } - - bool Master::repeat(u32 button) const { - return call(button, (bool (Interface::*)(u32) const)&Interface::repeat); - } - - BOOL Interface::rumble(int type) { - return FALSE; - } - - void Interface::read() { - } - - EGG::Allocator* Manager::mpAllocator; - - void* Manager::alloc(u32 size) { - return mpAllocator->alloc(size); - } - - Interface* Manager::getMasterController() { - return (Interface*)((u8*)this + 0xd0); - } - - Interface* Manager::getController(int chan) { - return ((Interface**)this)[chan]; - } - - int Manager::free(void* ptr) { - mpAllocator->free(ptr); - return TRUE; - } - - extern "C" asm void __ct__Q33ipl10controller7ManagerFPQ23EGG4Heap() { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_29 - lis r5, __vt__Q33ipl10controller6Master@ha - mr r30, r4 - addi r5, r5, __vt__Q33ipl10controller6Master@l - stw r3, 0xd4(r3) - mr r29, r3 - li r4, 0 - stw r5, 0xd0(r3) - li r5, 0x210 - addi r3, r3, 0xd8 - bl memset - mr r3, r29 - li r4, 0 - li r5, 0x10 - bl memset - bl WPADGetWorkMemorySize - lwz r4, mpBuf__Q33ipl10controller7Manager - addi r31, r3, 0x400 - cmpwi r4, 0 - beq manager_ctor_parent_done - lwz r3, mpParentHeap__Q33ipl10controller7Manager - cmpwi r3, 0 - beq manager_ctor_parent_done - lwz r12, 0(r3) - lwz r12, 0x18(r12) - mtctr r12 - bctrl - manager_ctor_parent_done: - stw r30, mpParentHeap__Q33ipl10controller7Manager - mr r3, r30 - mr r4, r31 - li r5, 0x20 - lwz r12, 0(r30) - lwz r12, 0x14(r12) - mtctr r12 - bctrl - lwz r0, mpHeap__Q33ipl10controller7Manager - stw r3, mpBuf__Q33ipl10controller7Manager - cmpwi r0, 0 - beq manager_ctor_heap_done - mr r3, r0 - lwz r12, 0(r3) - lwz r12, 0x1c(r12) - mtctr r12 - bctrl - manager_ctor_heap_done: - lwz r3, mpBuf__Q33ipl10controller7Manager - mr r4, r31 - li r5, 2 - bl create__Q23EGG7ExpHeapFPvUlUs - lwz r0, mpAllocator__Q33ipl10controller7Manager - stw r3, mpHeap__Q33ipl10controller7Manager - cmpwi r0, 0 - beq manager_ctor_allocator_done - beq manager_ctor_allocator_done - mr r3, r0 - li r4, 1 - lwz r12, 0x10(r3) - lwz r12, 8(r12) - mtctr r12 - bctrl - manager_ctor_allocator_done: - mr r4, r30 - li r3, 0x14 - li r5, 4 - bl __nw__FUlPQ23EGG4Heapi - cmpwi r3, 0 - mr r0, r3 - beq manager_ctor_allocator_new_done - lwz r4, mpHeap__Q33ipl10controller7Manager - li r5, 4 - bl __ct__Q23EGG9AllocatorFPQ23EGG4Heapl - mr r0, r3 - manager_ctor_allocator_new_done: - lis r3, alloc__Q33ipl10controller7ManagerFUl@ha - lis r4, free__Q33ipl10controller7ManagerFPv@ha - stw r0, mpAllocator__Q33ipl10controller7Manager - addi r3, r3, alloc__Q33ipl10controller7ManagerFUl@l - addi r4, r4, free__Q33ipl10controller7ManagerFPv@l - bl WPADRegisterAllocator - bl KPADInit - bl SCGetBtDpdSensibility - mr r31, r3 - li r30, 0 - manager_ctor_repeat: - clrlwi r3, r31, 24 - bl WPADSetDpdSensitivity - lfs f1, lbl_81694478 - mr r3, r30 - lfs f2, lbl_8169447C - bl KPADSetBtnRepeat - addi r30, r30, 1 - cmpwi r30, 4 - blt manager_ctor_repeat - mr r3, r29 - bl read__Q33ipl10controller7ManagerFv - addi r11, r1, 0x20 - mr r3, r29 - bl _restgpr_29 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr - } - - extern "C" asm void read__Q33ipl10controller7ManagerFv() { - nofralloc - stwu r1, -0x50(r1) - mflr r0 - stw r0, 0x54(r1) - addi r11, r1, 0x50 - bl _savegpr_16 - li r25, 0 - lis r29, __vt__Q33ipl10controller4Core@ha - lis r30, __vt__Q33ipl10controller9FreeStyle@ha - mr r24, r3 - mr r28, r25 - mr r20, r25 - mr r19, r25 - mr r18, r25 - mr r31, r25 - addi r29, r29, __vt__Q33ipl10controller4Core@l - addi r30, r30, __vt__Q33ipl10controller9FreeStyle@l - li r23, 0 - li r22, 0 - li r21, 0 - li r17, 0x3c - manager_read_loop: - bl SCGetWpadSensorBarPosition - clrlwi r0, r3, 24 - cmplwi r0, 1 - bne manager_read_sensor_low - lfs f1, lbl_81694480 - mr r3, r25 - bl KPADSetSensorHeight - b manager_read_probe - manager_read_sensor_low: - lfs f1, lbl_81694484 - mr r3, r25 - bl KPADSetSensorHeight - manager_read_probe: - mr r3, r25 - addi r4, r1, 0x8 - bl WPADProbe - cmpwi r3, -1 - beq manager_read_probe_invalid - bge manager_read_probe_nonnegative - cmpwi r3, -3 - bge manager_read_probe_valid - b manager_read_probe_invalid - manager_read_probe_nonnegative: - cmpwi r3, 1 - bge manager_read_probe_invalid - manager_read_probe_valid: - add r4, r24, r23 - mr r3, r25 - addi r26, r4, 0xd8 - li r5, 1 - mr r4, r26 - bl KPADRead - cmpwi r3, 0 - ble manager_read_kpad_bad - add r4, r24, r23 - lbz r0, 0x135(r4) - extsb r0, r0 - cmpwi r0, -4 - beq manager_read_kpad_bad - lwz r0, 0x8(r1) - cmplwi r0, 0xfd - beq manager_read_kpad_bad - add r27, r24, r21 - stw r28, 0x2e8(r27) - lbz r0, 0x135(r4) - extsb r0, r0 - cmpwi r0, -7 - beq manager_read_make_revolution - lwz r0, 0x8(r1) - cmpwi r0, 0 - beq manager_read_make_revolution - cmplwi r0, 0xfb - beq manager_read_make_revolution - cmplwi r0, 0xfc - beq manager_read_make_revolution - cmplwi r0, 0xff - bne manager_read_check_classic - manager_read_make_revolution: - lwz r3, 0(r27) - cmpwi r3, 0 - beq manager_read_make_revolution_new - lwz r12, 0(r3) - lwz r12, 0xc(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq manager_read_done - manager_read_make_revolution_new: - add r0, r24, r22 - addic. r16, r0, 0x10 - beq manager_read_store_revolution - mr r3, r16 - mr r4, r25 - mr r6, r26 - li r5, 0 - bl __ct__Q33ipl10controller10RevolutionFiiR10KPADStatus - stw r29, 0(r16) - manager_read_store_revolution: - stw r16, 0(r27) - b manager_read_done - manager_read_check_classic: - cmplwi r0, 1 - bne manager_read_check_core - lwz r3, 0(r27) - cmpwi r3, 0 - beq manager_read_make_core_new - lwz r12, 0(r3) - lwz r12, 0xc(r12) - mtctr r12 - bctrl - cmpwi r3, 1 - beq manager_read_done - manager_read_make_core_new: - add r0, r24, r22 - addic. r16, r0, 0x10 - beq manager_read_store_core - mr r3, r16 - mr r4, r25 - mr r6, r26 - li r5, 1 - bl __ct__Q33ipl10controller10RevolutionFiiR10KPADStatus - stw r30, 0(r16) - manager_read_store_core: - stw r16, 0(r27) - b manager_read_done - manager_read_check_core: - cmplwi r0, 2 - bne manager_read_store_null - lwz r3, 0(r27) - cmpwi r3, 0 - beq manager_read_make_classic_new - lwz r12, 0(r3) - lwz r12, 0xc(r12) - mtctr r12 - bctrl - cmpwi r3, 2 - beq manager_read_done - manager_read_make_classic_new: - add r0, r24, r22 - addic. r3, r0, 0x10 - beq manager_read_store_classic - mr r4, r25 - mr r5, r26 - bl __ct__Q33ipl10controller7ClassicFiR10KPADStatus - manager_read_store_classic: - stw r3, 0(r27) - b manager_read_done - manager_read_store_null: - stw r31, 0(r27) - b manager_read_done - manager_read_kpad_bad: - cmpwi r3, 0 - bne manager_read_store_bad_null - add r16, r24, r21 - lwz r3, 0x2e8(r16) - addi r0, r3, 1 - cmpwi r0, 0x3c - stw r0, 0x2e8(r16) - ble manager_read_done - stw r17, 0x2e8(r16) - lwz r0, 0(r16) - cmpwi r0, 0 - beq manager_read_timeout_clear - mr r3, r25 - li r4, 0 - bl WPADControlMotor - manager_read_timeout_clear: - stw r18, 0(r16) - b manager_read_done - manager_read_store_bad_null: - stwx r19, r24, r21 - b manager_read_done - manager_read_probe_invalid: - add r4, r24, r23 - mr r3, r25 - addi r4, r4, 0xd8 - li r5, 1 - bl KPADRead - stwx r20, r24, r21 - manager_read_done: - lfs f1, lbl_81694488 - mr r3, r25 - lfs f2, lbl_8169448C - bl KPADSetPosParam - addi r25, r25, 1 - addi r22, r22, 0x30 - cmpwi r25, 4 - addi r21, r21, 4 - addi r23, r23, 0x84 - blt manager_read_loop - li r16, 0 - li r25, 0 - manager_read_controller_loop: - lwzx r3, r24, r25 - cmpwi r3, 0 - beq manager_read_controller_next - lwz r12, 0(r3) - lwz r12, 0x88(r12) - mtctr r12 - bctrl - manager_read_controller_next: - addi r16, r16, 1 - addi r25, r25, 4 - cmpwi r16, 4 - blt manager_read_controller_loop - addi r11, r1, 0x50 - bl _restgpr_16 - lwz r0, 0x54(r1) - mtlr r0 - addi r1, r1, 0x50 - blr - } - - Interface* Manager::getYoungController() { - Interface* ret = NULL; - for (int i = 0; i < 4; i++) { - Interface* controller = ((Interface**)this)[i]; - if (controller != NULL) { - ret = controller; - break; - } - } - return ret; - } - - int Base::decide() const { + int ipl::controller::Base::decide() const { return unk_0x08 == 5; } - - int Base::rumble(int type) { + int ipl::controller::Base::rumble(int type) { int ret = FALSE; if (WPADIsMotorEnabled() && mRumbleType == -1) { mLastRumbleTime = OSGetTick(); @@ -1484,121 +104,50 @@ namespace ipl { return ret; } - - void Base::cancelRumbling() { + void ipl::controller::Base::cancelRumbling() { if (mRumbleType != -1) { WPADControlMotor(mChan, 0); } mLastRumbleTime = 0; mRumbleType = -1; } + void ipl::controller::Revolution::read() { + if (!isValidDpd()) { + unk_0x20->pos.y = 1.0f / 0.0f; + unk_0x20->pos.x = 1.0f / 0.0f; + unk_0x20->speed = 0.0f; + unk_0x20->vec.y = 0.0f; + unk_0x20->vec.x = 0.0f; + } - extern "C" asm void read__Q33ipl10controller10RevolutionFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r3 - lwz r12, 0(r3) - lwz r12, 0x70(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - bne revolution_read_dpd_done - lfs f1, lbl_81694450 - lwz r3, 0x20(r31) - lfs f0, lbl_81694454 - stfs f1, 0x24(r3) - lwz r3, 0x20(r31) - stfs f1, 0x20(r3) - lwz r3, 0x20(r31) - stfs f0, 0x30(r3) - lwz r3, 0x20(r31) - stfs f0, 0x2c(r3) - lwz r3, 0x20(r31) - stfs f0, 0x28(r3) - revolution_read_dpd_done: - lbz r0, 0x1d(r31) - mr r3, r31 - stb r0, 0x1e(r31) - lwz r12, 0(r31) - lwz r12, 0x6c(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq revolution_read_btn_invalid - lbz r0, 0x1e(r31) - cmpwi r0, 0 - bne revolution_read_btn_was_down - lwz r12, 0(r31) - mr r3, r31 - li r4, 0x800 - lwz r12, 0x14(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq revolution_read_btn_end - lwz r12, 0(r31) - mr r3, r31 - li r4, 0x400 - lwz r12, 0x14(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq revolution_read_btn_end - li r0, 1 - stb r0, 0x1d(r31) - b revolution_read_btn_end - revolution_read_btn_was_down: - lwz r12, 0(r31) - mr r3, r31 - li r4, 0x800 - lwz r12, 0x14(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - beq revolution_read_clear_btn - lwz r12, 0(r31) - mr r3, r31 - li r4, 0x400 - lwz r12, 0x14(r12) - mtctr r12 - bctrl - cmpwi r3, 0 - bne revolution_read_btn_end - revolution_read_clear_btn: - li r0, 0 - stb r0, 0x1d(r31) - b revolution_read_btn_end - revolution_read_btn_invalid: - li r0, 0 - stb r0, 0x1d(r31) - revolution_read_btn_end: - mr r3, r31 - bl read__Q33ipl10controller4BaseFv - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - mtlr r0 - addi r1, r1, 0x10 - blr + unk_0x1E = unk_0x1D; + if (isValidBtn()) { + if (unk_0x1E == 0) { + if (down(0x800) && down(0x400)) { + unk_0x1D = 1; + } + } else if (!down(0x800) || !down(0x400)) { + unk_0x1D = 0; + } + } else { + unk_0x1D = 0; + } + + Base::read(); } - - bool Revolution::isValidDpd() const { + bool ipl::controller::Revolution::isValidDpd() const { if (unk_0x1C != 0) { return false; } return (unk_0x20->wpad_err == 0 || unk_0x20->wpad_err == -7) && unk_0x20->dpd_valid_fg != 0; } - - bool Revolution::isValidBtn() const { + bool ipl::controller::Revolution::isValidBtn() const { u8 val = unk_0x20->wpad_err; return (u8)(val + 7) <= 7 && ((1 << (val + 7)) & 0xA1) != 0; } - - bool Revolution::down(u32 mButton) const { + bool ipl::controller::Revolution::down(u32 mButton) const { bool ret = false; if (isValidBtn()) { if (unk_0x20->hold & (mButton & 0xFFFF)) { @@ -1608,53 +157,7 @@ namespace ipl { return ret; } - - int Revolution::pinch() const { - int ret = 0; - if (isValidBtn() && unk_0x1D != 0) { - ret = 1; - } - return ret; - } - - int Revolution::pinchTrg() const { - int ret = 0; - if (isValidBtn() && unk_0x1D != 0 && unk_0x1E == 0) { - ret = 1; - } - return ret; - } - - int Revolution::pinchOffTrg() const { - int ret = 0; - if (isValidBtn() && unk_0x1D == 0 && unk_0x1E != 0) { - ret = 1; - } - return ret; - } - - f32 Revolution::getDpdDistance() const { - return unk_0x20->dist; - } - - KPADStatus* Revolution::getKPADStatus() const { - return unk_0x20; - } - - int Revolution::getReleaseFlag() const { - return unk_0x20->release; - } - - int Revolution::getTrigFlag() const { - return unk_0x20->trig; - } - - int Revolution::getHoldFlag() const { - return unk_0x20->hold; - } - - // non-matching - math::VEC2 Revolution::getDpdProjectionPos() const { + ipl::math::VEC2 ipl::controller::Revolution::getDpdProjectionPos() const { math::VEC2 ret; Vec2 dest; Vec2 src = {unk_0x20->pos.x, unk_0x20->pos.y}; @@ -1668,21 +171,21 @@ namespace ipl { kpad_rect.right = nw4r_rect.right; kpad_rect.bottom = nw4r_rect.bottom; - KPADGetProjectionPos(&dest, &src, &kpad_rect, lbl_81694458); + KPADGetProjectionPos(&dest, &src, &kpad_rect, 1.10132003f); if (SCGetAspectRatio() == 1) { - dest.x *= lbl_8169445C; - dest.y *= lbl_8169445C; + dest.x *= 1.15f; + dest.y *= 1.15f; } // regswap - if (nw4r_rect.left - lbl_81694460 > dest.x) { - dest.x = nw4r_rect.left - lbl_81694460; - } else if (nw4r_rect.right + lbl_81694460 < dest.x) { - dest.x = nw4r_rect.right + lbl_81694460; - } else if (nw4r_rect.top - lbl_81694460 > dest.y) { - dest.y = nw4r_rect.top - lbl_81694460; - } else if (nw4r_rect.bottom + lbl_81694460 < dest.y) { - dest.y = nw4r_rect.bottom + lbl_81694460; + if (nw4r_rect.left - 100.0f > dest.x) { + dest.x = nw4r_rect.left - 100.0f; + } else if (nw4r_rect.right + 100.0f < dest.x) { + dest.x = nw4r_rect.right + 100.0f; + } else if (nw4r_rect.top - 100.0f > dest.y) { + dest.y = nw4r_rect.top - 100.0f; + } else if (nw4r_rect.bottom + 100.0f < dest.y) { + dest.y = nw4r_rect.bottom + 100.0f; } // // @@ -1690,34 +193,36 @@ namespace ipl { return ret; } - - Classic::Classic(int arg0, KPADStatus& arg1) : Revolution(arg0, arg1) { - unk_0x24.x = lbl_81694464; - unk_0x24.y = lbl_81694464; + nw4r::ut::Rect::Rect() : left(0.0f), top(0.0f), right(0.0f), bottom(0.0f) {} + void ipl::math::VEC2::set(f32 fx, f32 fy) { + x = fx; + y = fy; + } + ipl::controller::Classic::Classic(int arg0, KPADStatus& arg1) : Revolution(arg0, arg1) { + unk_0x24.x = 0.01f; + unk_0x24.y = 0.01f; unk_0x2C = 0; } - - Base::~Base() { + ipl::controller::Base::~Base() { } - - Master::~Master() { + ipl::controller::Revolution::~Revolution() { } - - Revolution::~Revolution() { + ipl::math::VEC2::VEC2(f32 fx, f32 fy) { + x = fx; + y = fy; } - - void Classic::read() { + void ipl::controller::Classic::read() { if (!Revolution::isValidDpd()) { math::VEC2 lstick(unk_0x20->ex_status.cl.lstick.x, unk_0x20->ex_status.cl.lstick.y); - if (lstick.x * lstick.x + lstick.y * lstick.y > lbl_81694468) { - unk_0x24.x = math::abs_clamp(unk_0x24.x + unk_0x20->ex_status.cl.lstick.x * lbl_8169446C, 1.8f); - unk_0x24.y = math::abs_clamp(unk_0x24.y - unk_0x20->ex_status.cl.lstick.y * lbl_8169446C, 1.2f); + if (lstick.x * lstick.x + lstick.y * lstick.y > 0.0036f) { + unk_0x24.x = math::abs_clamp(unk_0x24.x + unk_0x20->ex_status.cl.lstick.x * 0.05f, 1.8f); + unk_0x24.y = math::abs_clamp(unk_0x24.y - unk_0x20->ex_status.cl.lstick.y * 0.05f, 1.2f); } math::VEC2 rstick = math::VEC2(unk_0x20->ex_status.cl.rstick.x, unk_0x20->ex_status.cl.rstick.y); - if (getClassicHoldFlag() != 0 || lstick.x * lstick.x + lstick.y * lstick.y > lbl_81694468 || - rstick.x * rstick.x + rstick.y * rstick.y > lbl_81694468) { + if (getClassicHoldFlag() != 0 || lstick.x * lstick.x + lstick.y * lstick.y > 0.0036f || + rstick.x * rstick.x + rstick.y * rstick.y > 0.0036f) { unk_0x2C = 180; } @@ -1725,165 +230,276 @@ namespace ipl { unk_0x2C = 0; } } else { - unk_0x24.y = lbl_81694464; - unk_0x24.x = lbl_81694464; + unk_0x24.y = 0.01f; + unk_0x24.x = 0.01f; unk_0x2C = 0; } Revolution::read(); } - - bool Classic::isValidDpd() const { - if (unk_0x1C != 0) { - return false; - } - - return (Revolution::isValidDpd() != 0) || (unk_0x2C != 0); - } - - BOOL Classic::isValidDpdClassic() const { - return unk_0x2C != 0; - } - - int Classic::getClassicHoldFlag() const { + int ipl::controller::Classic::getClassicHoldFlag() const { return unk_0x20->ex_status.cl.hold; } - - int Classic::getClassicTrigFlag() const { - return unk_0x20->ex_status.cl.trig; + template <> + f32 ipl::math::abs_clamp(const f32& x, const f32& y) { + if (x > y) { + return y; + } else if (x < -y) { + return -y; + } else { + return x; + } } - - int Classic::getClassicReleaseFlag() const { - return unk_0x20->ex_status.cl.release; - } - - math::VEC2 Classic::getHorizon() const { + ipl::math::VEC2 ipl::controller::Classic::getHorizon() const { math::VEC2 ret; if (Revolution::isValidDpd()) { ret.set(unk_0x20->horizon.x, unk_0x20->horizon.y); } else { ret.x = 1.0f; - ret.y = lbl_81694474; + ret.y = -0.2679492f; } return ret; } - - math::VEC2 Classic::getDpdPos() const { + ipl::math::VEC2 ipl::controller::Classic::getDpdPos() const { math::VEC2 ret; if (Revolution::isValidDpd()) { ret.set(unk_0x20->pos.x, unk_0x20->pos.y); } else if (unk_0x2C != 0) { ret = unk_0x24; } else { - ret.x = lbl_81694450; - ret.y = lbl_81694450; + ret.x = (1.0f / 0.0f); + ret.y = (1.0f / 0.0f); } return ret; } - - extern "C" asm void getDpdProjectionPos__Q33ipl10controller7ClassicCFv() { - nofralloc - stwu r1, -0x50(r1) - mflr r0 - stw r0, 0x54(r1) - stw r31, 0x4c(r1) - mr r31, r3 - bl isValidDpd__Q33ipl10controller10RevolutionCFv - cmpwi r3, 0 - beq classic_projection_check_stick - lwz r4, 0x20(r31) - addi r3, r1, 0x18 - lfs f1, 0x20(r4) - lfs f2, 0x24(r4) - bl set__Q33ipl4math4VEC2Fff - b classic_projection_have_src - classic_projection_check_stick: - lwz r0, 0x2c(r31) - cmpwi r0, 0 - beq classic_projection_invalid - addi r3, r1, 0x18 - addi r4, r31, 0x24 - bl __as__Q33ipl4math4VEC2FRCQ33ipl4math4VEC2 - b classic_projection_have_src - classic_projection_invalid: - lfs f0, lbl_81694450 - stfs f0, 0x18(r1) - stfs f0, 0x1c(r1) - classic_projection_have_src: - lfs f1, 0x18(r1) - addi r3, r1, 0x30 - lfs f0, 0x1c(r1) - stfs f1, 0x8(r1) - stfs f0, 0xc(r1) - bl __ct__Q34nw4r2ut4RectFv - addi r3, r1, 0x30 - bl getProjectionRect__Q23ipl6SystemFPQ34nw4r2ut4Rect - lfs f1, 0x30(r1) - addi r3, r1, 0x10 - lfs f3, 0x34(r1) - addi r4, r1, 0x8 - lfs f2, 0x38(r1) - addi r5, r1, 0x20 - lfs f0, 0x3c(r1) - stfs f1, 0x20(r1) - lfs f1, lbl_81694458 - stfs f3, 0x24(r1) - stfs f2, 0x28(r1) - stfs f0, 0x2c(r1) - bl KPADGetProjectionPos - bl SCGetAspectRatio - clrlwi r0, r3, 24 - cmplwi r0, 1 - bne classic_projection_no_aspect - lfs f1, 0x10(r1) - lfs f2, lbl_8169445C - lfs f0, 0x14(r1) - fmuls f1, f1, f2 - fmuls f0, f0, f2 - stfs f1, 0x10(r1) - stfs f0, 0x14(r1) - classic_projection_no_aspect: - lfs f0, 0x30(r1) - lfs f2, lbl_81694460 - lfs f1, 0x10(r1) - fsubs f0, f0, f2 - fcmpo cr0, f0, f1 - ble classic_projection_right - stfs f0, 0x10(r1) - b classic_projection_done - classic_projection_right: - lfs f0, 0x38(r1) - fadds f0, f2, f0 - fcmpo cr0, f0, f1 - bge classic_projection_top - stfs f0, 0x10(r1) - b classic_projection_done - classic_projection_top: - lfs f0, 0x34(r1) - lfs f1, 0x14(r1) - fsubs f0, f0, f2 - fcmpo cr0, f0, f1 - ble classic_projection_bottom - stfs f0, 0x14(r1) - b classic_projection_done - classic_projection_bottom: - lfs f0, 0x3c(r1) - fadds f0, f2, f0 - fcmpo cr0, f0, f1 - bge classic_projection_done - stfs f0, 0x14(r1) - classic_projection_done: - lfs f1, 0x10(r1) - addi r3, r1, 0x18 - lfs f2, 0x14(r1) - bl set__Q33ipl4math4VEC2Fff - lwz r31, 0x4c(r1) - lwz r0, 0x54(r1) - lwz r3, 0x18(r1) - lwz r4, 0x1c(r1) - mtlr r0 - addi r1, r1, 0x50 - blr + void ipl::math::VEC2::operator=(const ipl::math::VEC2& r) { + x = r.x; + y = r.y; + } + ipl::math::VEC2 ipl::controller::Classic::getDpdProjectionPos() const { + math::VEC2 ret; + if (Revolution::isValidDpd()) { + ret.set(unk_0x20->pos.x, unk_0x20->pos.y); + } else if (unk_0x2C != 0) { + ret = unk_0x24; + } else { + ret.set(1.0f / 0.0f, 1.0f / 0.0f); + } + + Vec2 dest; + Vec2 src; + src.x = ret.x; + src.y = ret.y; + nw4r::ut::Rect nw4r_rect; + System::getProjectionRect(&nw4r_rect); + Rect rect; + rect.left = nw4r_rect.left; + rect.top = nw4r_rect.top; + rect.right = nw4r_rect.right; + rect.bottom = nw4r_rect.bottom; + KPADGetProjectionPos(&dest, &src, &rect, 1.10132003f); + if (SCGetAspectRatio() == 1) { + dest.x *= 1.15f; + dest.y *= 1.15f; + } + + if (nw4r_rect.left - 100.0f > dest.x) { + dest.x = nw4r_rect.left - 100.0f; + } else if (nw4r_rect.right + 100.0f < dest.x) { + dest.x = nw4r_rect.right + 100.0f; + } else if (nw4r_rect.top - 100.0f > dest.y) { + dest.y = nw4r_rect.top - 100.0f; + } else if (nw4r_rect.bottom + 100.0f < dest.y) { + dest.y = nw4r_rect.bottom + 100.0f; + } + + ret.set(dest.x, dest.y); + return ret; + } + bool ipl::controller::Master::down(u32 button) const { + return call(button, kDownMember); + } + bool ipl::controller::Master::downTrg(u32 button) const { + return call(button, kDownTrgMember); + } + bool ipl::controller::Master::upTrg(u32 button) const { + return call(button, kUpTrgMember); + } + bool ipl::controller::Master::repeat(u32 button) const { + return call(button, kRepeatMember); + } + int ipl::controller::Master::decide() const { + bool current = false; + bool ret = false; + for (int i = 0; i < 4; i++) { + current = false; + Interface* controller = mpControllers[i]; + if (controller != NULL) { + if (controller->decide() != 0) { + current = true; + } + } + ret = ret | current; + } + return ret; + } +void ipl::controller::Master::setForceInvalid(bool flag) { + for (int i = 0; i < 4; i++) { + if (mpControllers[i] != NULL) { + mpControllers[i]->setForceInvalid(flag); + } + } +} + bool ipl::controller::Master::call(u32 button, bool (Interface::*func)(u32) const) const { + bool current = false; + bool ret = false; + for (int i = 0; i < 4; i++) { + current = false; + Interface* controller = mpControllers[i]; + if (controller != NULL) { + if ((controller->*func)(button)) { + current = true; + } + } + ret = ret | current; + } + return ret; + } + void ipl::controller::Base::setForceInvalid(bool flag) { + unk_0x1C = flag; + } + int ipl::controller::Base::getType() const { + return mType; + } + int ipl::controller::Base::getChannel() const { + return mChan; + } + KPADStatus* ipl::controller::Revolution::getKPADStatus() const { + return unk_0x20; + } + f32 ipl::controller::Revolution::getDpdDistance() const { + return unk_0x20->dist; + } + int ipl::controller::Revolution::getReleaseFlag() const { + return unk_0x20->release; + } + int ipl::controller::Revolution::getTrigFlag() const { + return unk_0x20->trig; + } + int ipl::controller::Revolution::getHoldFlag() const { + return unk_0x20->hold; + } + int ipl::controller::Revolution::pinchOffTrg() const { + int ret = 0; + if (isValidBtn() && unk_0x1D == 0 && unk_0x1E != 0) { + ret = 1; + } + return ret; + } + int ipl::controller::Revolution::pinch() const { + int ret = 0; + if (isValidBtn() && unk_0x1D != 0) { + ret = 1; + } + return ret; + } + int ipl::controller::Revolution::pinchTrg() const { + int ret = 0; + if (isValidBtn() && unk_0x1D != 0 && unk_0x1E == 0) { + ret = 1; + } + return ret; + } + bool ipl::controller::Revolution::repeat(u32 button) const { + return down(button) && (unk_0x20->hold & 0x80000000) != 0; + } + BOOL ipl::controller::Classic::isValidDpdClassic() const { + return unk_0x2C != 0; + } + bool ipl::controller::Classic::isValidDpd() const { + if (unk_0x1C != 0) { + return false; + } + + return (Revolution::isValidDpd() != 0) || (unk_0x2C != 0); + } + int ipl::controller::Classic::getClassicReleaseFlag() const { + return unk_0x20->ex_status.cl.release; + } + int ipl::controller::Classic::getClassicTrigFlag() const { + return unk_0x20->ex_status.cl.trig; + } + bool ipl::controller::Classic::upTrg(u32 button) const { + bool ret = false; + if (isValidBtn()) { + bool pressed = true; + KPADStatus* status = unk_0x20; + if ((status->release & (button & 0xFFFF)) == 0) { + if ((status->ex_status.cl.release & (button >> 16)) == 0) { + pressed = false; + } + } + if (pressed) { + ret = true; + } + } + return ret; + } + bool ipl::controller::Classic::downTrg(u32 button) const { + bool ret = false; + if (isValidBtn()) { + bool pressed = true; + KPADStatus* status = unk_0x20; + if ((status->trig & (button & 0xFFFF)) == 0) { + if ((status->ex_status.cl.trig & (button >> 16)) == 0) { + pressed = false; + } + } + if (pressed) { + ret = true; + } + } + return ret; + } + bool ipl::controller::Classic::down(u32 button) const { + bool ret = false; + if (isValidBtn()) { + bool pressed = true; + if ((unk_0x20->hold & (button & 0xFFFF)) == 0) { + if ((unk_0x20->ex_status.cl.hold & (button >> 16)) == 0) { + pressed = false; + } + } + if (pressed) { + ret = true; + } + } + return ret; + } + ipl::math::VEC2 ipl::controller::Revolution::getHorizon() const { + return math::VEC2(unk_0x20->horizon.x, unk_0x20->horizon.y); + } + ipl::math::VEC2 ipl::controller::Revolution::getDpdPos() const { + return math::VEC2(unk_0x20->pos.x, unk_0x20->pos.y); + } + bool ipl::controller::Revolution::upTrg(u32 button) const { + bool ret = false; + if (isValidBtn()) { + if (unk_0x20->release & (button & 0xFFFF)) { + ret = true; + } + } + return ret; + } + bool ipl::controller::Revolution::downTrg(u32 button) const { + bool ret = false; + if (isValidBtn()) { + if (unk_0x20->trig & (button & 0xFFFF)) { + ret = true; + } + } + return ret; + } + ipl::controller::Classic::~Classic() {} + ipl::controller::Master::~Master() { } - } // namespace controller -} // namespace ipl diff --git a/src/system/iplControllerManager.cpp b/src/system/iplControllerManager.cpp new file mode 100644 index 00000000..3daaec5d --- /dev/null +++ b/src/system/iplControllerManager.cpp @@ -0,0 +1,187 @@ +#include +#include +#define IPL_SOUND_RECT_OUT_OF_LINE +#include +#undef IPL_SOUND_RECT_OUT_OF_LINE +#include +#include +#include +#include +#include +#include + +#define IPL_CONTROLLER_NATIVE_HIERARCHY +#include "system/iplController.h" +#undef IPL_CONTROLLER_NATIVE_HIERARCHY +#include "system/iplSystem.h" + +extern "C" void _savegpr_16(); +extern "C" void _restgpr_16(); +extern "C" void _savegpr_29(); +extern "C" void _restgpr_29(); +extern "C" void _savegpr_27(); +extern "C" void _restgpr_27(); +extern "C" void _savegpr_28(); +extern "C" void _restgpr_28(); + +namespace ipl { + namespace controller { + void* Manager::mpBuf; + EGG::Heap* Manager::mpParentHeap; + EGG::ExpHeap* Manager::mpHeap; + EGG::Allocator* Manager::mpAllocator; + + void* Manager::alloc(u32 size) { + return mpAllocator->alloc(size); + } + + int Manager::free(void* ptr) { + mpAllocator->free(ptr); + return TRUE; + } + + Manager::Manager(EGG::Heap* heap) : mMaster(mControllers) { + memset(mKPADStatus, 0, sizeof(mKPADStatus)); + memset(mControllers, 0, sizeof(mControllers)); + + u32 workSize = WPADGetWorkMemorySize() + 0x400; + if (mpBuf != NULL && mpParentHeap != NULL) { + mpParentHeap->free(mpBuf); + } + + mpParentHeap = heap; + mpBuf = heap->alloc(workSize, 0x20); + if (mpHeap != NULL) { + mpHeap->destroy(); + } + + mpHeap = EGG::ExpHeap::create(mpBuf, workSize, 2); + if (mpAllocator != NULL) { + delete mpAllocator; + } + + mpAllocator = new (heap, 4) EGG::Allocator(mpHeap, 4); + WPADRegisterAllocator(alloc, free); + KPADInit(); + + u32 sensitivity = SCGetBtDpdSensibility(); + for (int i = 0; i < 4; i++) { + WPADSetDpdSensitivity(sensitivity); + KPADSetBtnRepeat(i, 0.5f, 0.1f); + } + + read(); + } + + void Manager::read() { + for (int chan = 0; chan < 4; chan++) { + if (SCGetWpadSensorBarPosition() == 1) { + KPADSetSensorHeight(chan, 0.2f); + } else { + KPADSetSensorHeight(chan, -0.2f); + } + + u32 deviceType; + s32 probe = WPADProbe(chan, &deviceType); + if (probe == -1) { + goto probe_invalid; + } + if (probe < -1) { + if (probe >= -3) { + goto probe_valid; + } + goto probe_invalid; + } else if (probe >= 1) { + goto probe_invalid; + } + + probe_valid: + { + s32 read = KPADRead(chan, &mKPADStatus[chan], 1); + if (read > 0) { + if (mKPADStatus[chan].wpad_err == -4 || deviceType == 0xfd) { + goto store_null; + } + + mInvalidCount[chan] = 0; + if (mKPADStatus[chan].wpad_err == -7 || deviceType == 0 || deviceType == 0xfb || + deviceType == 0xfc || deviceType == 0xff) { + if (mControllers[chan] == NULL || mControllers[chan]->getType() != 0) { + mControllers[chan] = new (mControllerStorage[chan]) Core(chan, 0, mKPADStatus[chan]); + } + } else if (deviceType == 1) { + if (mControllers[chan] == NULL || mControllers[chan]->getType() != 1) { + mControllers[chan] = new (mControllerStorage[chan]) FreeStyle(chan, 1, mKPADStatus[chan]); + } + } else if (deviceType == 2) { + if (mControllers[chan] == NULL || mControllers[chan]->getType() != 2) { + mControllers[chan] = new (mControllerStorage[chan]) Classic(chan, mKPADStatus[chan]); + } + } else { + mControllers[chan] = NULL; + } + } else if (read == 0) { + mInvalidCount[chan]++; + if (mInvalidCount[chan] > 0x3c) { + mInvalidCount[chan] = 0x3c; + if (mControllers[chan] != NULL) { + WPADControlMotor(chan, 0); + } + mControllers[chan] = NULL; + } + } else { + goto store_null; + } + } + goto channel_done; + + store_null: + mControllers[chan] = NULL; + goto channel_done; + + probe_invalid: + KPADRead(chan, &mKPADStatus[chan], 1); + mControllers[chan] = NULL; + + channel_done: + KPADSetPosParam(chan, 0.05f, 1.0f); + } + + for (int chan = 0; chan < 4; chan++) { + if (mControllers[chan] != NULL) { + mControllers[chan]->read(); + } + } + } + + Revolution::Revolution(int chan, int type, KPADStatus& status) : Base(chan, type, status) { + unk_0x1D = 0; + unk_0x1E = 0; + unk_0x20 = &status; + } + + Interface* Manager::getMasterController() { + return &mMaster; + } + + Interface* Manager::getController(int chan) { + return ((Interface**)this)[chan]; + } + + Interface* Manager::getYoungController() { + Interface* ret = NULL; + for (int i = 0; i < 4; i++) { + Interface* controller = ((Interface**)this)[i]; + if (controller != NULL) { + ret = controller; + break; + } + } + return ret; + } + + Core::~Core() {} + + FreeStyle::~FreeStyle() {} + } +} -- 2.49.1 From 094cfd811e99655a6221eca42c8a31674272cb75 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Mon, 28 Sep 2026 18:56:01 -0500 Subject: [PATCH 04/34] Link iplController and iplControllerManager (#242) * link iplController translation unit * match controller manager read flow * link iplControllerManager translation unit --- configure.py | 4 +- include/system/iplController.h | 6 +- src/system/iplController.cpp | 88 +++++++++++++++++++++++++++++ src/system/iplControllerManager.cpp | 13 ++--- 4 files changed, 100 insertions(+), 11 deletions(-) diff --git a/configure.py b/configure.py index 46b142da..6bbcff16 100644 --- a/configure.py +++ b/configure.py @@ -624,8 +624,8 @@ config.libs = [ Object(Matching, "system/rvl_dec.c"), Object(Matching, "system/RIPL_BoardRecord.c"), Object(Matching, "system/iplSystem.cpp"), - Object(NonMatching, "system/iplController.cpp"), - Object(NonMatching, "system/iplControllerManager.cpp"), + Object(Matching, "system/iplController.cpp"), + Object(Matching, "system/iplControllerManager.cpp"), Object(Matching, "system/iplChannelManager.cpp"), Object(Matching, "system/iplChannelScriptManager.cpp"), Object(Matching, "system/iplNand.cpp"), diff --git a/include/system/iplController.h b/include/system/iplController.h index bba7b9a0..f637f3a2 100644 --- a/include/system/iplController.h +++ b/include/system/iplController.h @@ -63,10 +63,14 @@ namespace ipl { // clang-format on #ifdef IPL_CONTROLLER_NATIVE_HIERARCHY +#ifdef IPL_CONTROLLER_OUT_OF_LINE_INTERFACE +#define IPL_CONTROLLER_DEFAULT_VIRTUAL(signature, implementation) virtual signature; +#else #ifdef IPL_CONTROLLER_INLINE_INTERFACE #define IPL_CONTROLLER_DEFAULT_VIRTUAL(signature, implementation) virtual signature implementation #else #define IPL_CONTROLLER_DEFAULT_VIRTUAL(signature, implementation) virtual signature; +#endif #endif class Interface { public: @@ -79,7 +83,7 @@ namespace ipl { IPL_CONTROLLER_DEFAULT_VIRTUAL(int pinch() const, { return 0; }) IPL_CONTROLLER_DEFAULT_VIRTUAL(int pinchTrg() const, { return 0; }) IPL_CONTROLLER_DEFAULT_VIRTUAL(int pinchOffTrg() const, { return 0; }) - IPL_CONTROLLER_DEFAULT_VIRTUAL(int decide() const, { return 0; }) + virtual int decide() const = 0; IPL_CONTROLLER_DEFAULT_VIRTUAL(bool repeat(u32 button) const, { return false; }) IPL_CONTROLLER_DEFAULT_VIRTUAL(BOOL rumble(int type = 0), { return FALSE; }) IPL_CONTROLLER_DEFAULT_VIRTUAL(void cancelRumbling(), { }) diff --git a/src/system/iplController.cpp b/src/system/iplController.cpp index 70e13e6f..2449cbae 100644 --- a/src/system/iplController.cpp +++ b/src/system/iplController.cpp @@ -18,7 +18,9 @@ #define IPL_CONTROLLER_NATIVE_HIERARCHY #define IPL_CONTROLLER_INLINE_INTERFACE +#define IPL_CONTROLLER_OUT_OF_LINE_INTERFACE #include "system/iplController.h" +#undef IPL_CONTROLLER_OUT_OF_LINE_INTERFACE #undef IPL_CONTROLLER_INLINE_INTERFACE #undef IPL_CONTROLLER_NATIVE_HIERARCHY #include "system/iplSystem.h" @@ -91,6 +93,21 @@ namespace ipl { } } } + bool ipl::controller::Interface::isValidBtn() const { + return false; + } + bool ipl::controller::Interface::downTrg(u32 button) const { + return false; + } + int ipl::controller::Interface::pinch() const { + return 0; + } + bool ipl::controller::Interface::down(u32 button) const { + return false; + } + KPADStatus* ipl::controller::Interface::getKPADStatus() const { + return NULL; + } int ipl::controller::Base::decide() const { return unk_0x08 == 5; } @@ -325,9 +342,15 @@ namespace ipl { bool ipl::controller::Master::upTrg(u32 button) const { return call(button, kUpTrgMember); } + bool ipl::controller::Interface::upTrg(u32 button) const { + return false; + } bool ipl::controller::Master::repeat(u32 button) const { return call(button, kRepeatMember); } + bool ipl::controller::Interface::repeat(u32 button) const { + return false; + } int ipl::controller::Master::decide() const { bool current = false; bool ret = false; @@ -365,6 +388,71 @@ void ipl::controller::Master::setForceInvalid(bool flag) { } return ret; } + void ipl::controller::Interface::read() {} + int ipl::controller::Interface::getSubStickY() const { + return 0; + } + int ipl::controller::Interface::getSubStickX() const { + return 0; + } + int ipl::controller::Interface::getMainStickY() const { + return 0; + } + int ipl::controller::Interface::getMainStickX() const { + return 0; + } + bool ipl::controller::Interface::isValidDpd() const { + return false; + } + PADStatus* ipl::controller::Interface::getPADStatus() const { + return NULL; + } + f32 ipl::controller::Interface::getDpdDistance() const { + return 0.0f; + } + ipl::math::VEC2 ipl::controller::Interface::getHorizon() const { + return ipl::math::VEC2(0.0f, 0.0f); + } + ipl::math::VEC2 ipl::controller::Interface::getDpdProjectionPos() const { + return ipl::math::VEC2(0.0f, 0.0f); + } + ipl::math::VEC2 ipl::controller::Interface::getDpdPos() const { + return ipl::math::VEC2(0.0f, 0.0f); + } + int ipl::controller::Interface::getClassicReleaseFlag() const { + return 0; + } + int ipl::controller::Interface::getClassicTrigFlag() const { + return 0; + } + int ipl::controller::Interface::getClassicHoldFlag() const { + return 0; + } + int ipl::controller::Interface::getReleaseFlag() const { + return 0; + } + int ipl::controller::Interface::getTrigFlag() const { + return 0; + } + int ipl::controller::Interface::getHoldFlag() const { + return 0; + } + void ipl::controller::Interface::cancelRumbling() {} + BOOL ipl::controller::Interface::rumble(int type) { + return FALSE; + } + int ipl::controller::Interface::pinchOffTrg() const { + return 0; + } + int ipl::controller::Interface::pinchTrg() const { + return 0; + } + int ipl::controller::Interface::getChannel() const { + return -1; + } + int ipl::controller::Interface::getType() const { + return -1; + } void ipl::controller::Base::setForceInvalid(bool flag) { unk_0x1C = flag; } diff --git a/src/system/iplControllerManager.cpp b/src/system/iplControllerManager.cpp index 3daaec5d..eb8e0261 100644 --- a/src/system/iplControllerManager.cpp +++ b/src/system/iplControllerManager.cpp @@ -98,11 +98,7 @@ namespace ipl { probe_valid: { s32 read = KPADRead(chan, &mKPADStatus[chan], 1); - if (read > 0) { - if (mKPADStatus[chan].wpad_err == -4 || deviceType == 0xfd) { - goto store_null; - } - + if (read > 0 && mKPADStatus[chan].wpad_err != -4 && deviceType != 0xfd) { mInvalidCount[chan] = 0; if (mKPADStatus[chan].wpad_err == -7 || deviceType == 0 || deviceType == 0xfb || deviceType == 0xfc || deviceType == 0xff) { @@ -120,7 +116,10 @@ namespace ipl { } else { mControllers[chan] = NULL; } - } else if (read == 0) { + } else { + if (read != 0) { + goto store_null; + } mInvalidCount[chan]++; if (mInvalidCount[chan] > 0x3c) { mInvalidCount[chan] = 0x3c; @@ -129,8 +128,6 @@ namespace ipl { } mControllers[chan] = NULL; } - } else { - goto store_null; } } goto channel_done; -- 2.49.1 From b719ba47a5d65bc53944c051b2a0f9d19ea37ee5 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Mon, 28 Sep 2026 19:05:02 -0500 Subject: [PATCH 05/34] Decompile KPAD to 11/29 functions (#243) * decompile kpad unit * model kpad sample fields * refine kpad ring buffer matching * restore kpad sample history processing * match kpad sampling branches * restore probe callback branch * refine kpad acceleration clamps * refine kpad first object selection * match kpad object candidate loops * match kpad pointer based dpd scans * preserve dpd sample words * align kpad device sample handling * refine kpad acceleration format dispatch * match kpad init ring defaults * align kpad init field order * match kpad probe type dispatch * align kpad horizon calculation order * clean kpad field names and match repeat setup * name kpad calibration fields * match kpad ring buffer traversal * refine kpad ring buffer matching --- libs/RVL_SDK/src/kpad/KPAD.c | 1824 ++++++++++++++++++++++++++++++++++ 1 file changed, 1824 insertions(+) create mode 100644 libs/RVL_SDK/src/kpad/KPAD.c diff --git a/libs/RVL_SDK/src/kpad/KPAD.c b/libs/RVL_SDK/src/kpad/KPAD.c new file mode 100644 index 00000000..7766aa17 --- /dev/null +++ b/libs/RVL_SDK/src/kpad/KPAD.c @@ -0,0 +1,1824 @@ +#include +#include +#include +#include +#include + +typedef struct KPADSampleFS { + s16 accX; + s16 accY; + s16 accZ; + s8 stickX; + s8 stickY; +} KPADSampleFS; + +typedef struct KPADSampleCL { + u16 buttons; + s16 lStickX; + s16 lStickY; + s16 rStickX; + s16 rStickY; + u8 triggerL; + u8 triggerR; +} KPADSampleCL; + +typedef struct KPADSample { + u16 buttons; + s16 accX; + s16 accY; + s16 accZ; + DPDObject objects[WPAD_MAX_DPD_OBJECTS]; + u8 device; + s8 error; + union { + KPADSampleFS fs; + KPADSampleCL cl; + } extension; + u8 dataFormat; +} KPADSample; + +typedef struct KPADDPDObject { + f32 x; + f32 y; + union { + u32 value; + struct { + u8 flags; + u8 status; + } bytes; + } metadata; +} KPADDPDObject; + +typedef struct KPADInside { + KPADStatus status; + f32 posParamX; + f32 posParamY; + f32 value8C; + f32 value90; + f32 value94; + f32 value98; + f32 value9C; + f32 valueA0; + f32 referenceDistance; + f32 accelNormalX; + f32 accelNormalY; + f32 horizonTangentX; + f32 horizonTangentY; + f32 sensorBarCenterX; + f32 sensorBarCenterY; + f32 sensorBarScale; + struct { + KPADDPDObject objects[4]; + KPADDPDObject candidates[2]; + } dpdState; + u16 dpdCount; + u8 ringIndex; + u8 ringCount; + KPADSample ringData[16]; + f32 dpdObjectDistance; + f32 dpdObjectDirectionX; + f32 dpdObjectDirectionY; + f32 dpdReferenceDistance; + f32 dpdObjectScale; + f32 accelerationX; + f32 accelerationY; + f32 accelerationZ; + f32 dpdAxisX; + f32 dpdAxisY; + f32 horizonAxisX; + f32 horizonAxisY; + f32 horizonCircleX; + f32 horizonCircleY; + u16 horizonCircleCount; + u8 value4CA; + u16 repeatCount; + u16 repeatCount2; + u16 repeatDelay; + u16 repeatInterval; + u16 repeatCurrent; + u16 repeatCurrent2; + KPADCallback dpdCallback; + f32 value4DC; + f32 value4E0; + f32 value4E4; + f32 value4E8; + f32 value4EC; + f32 value4F0; + f32 value4F4; + f32 value4F8; + f32 value4FC; + f32 value500; + f32 value504; + f32 value508; + f32 value50C; + f32 value510; + f32 value514; + WPADSamplingCallback samplingCallback; + u8 samplingInProgress; + u8 flag51D; + u8 flag51E; + u8 flag51F; + u8 dpdEnable; + u8 dpdFormat; + u8 dpdCallbackFired; + u8 dpdCallbackPending; + u8 sensorHeightPending; + u8 sensorBarPosition; + u16 freeStyleAccelRotation; +} KPADInside; + +static KPADInside inside_kpads[4]; +f32 initial_rotation_matrix[16]; + +static const char kpadVersion[] = "<< RVL_SDK - KPAD \trelease build: Apr 20 2010 11:20:37 (0x4199_60831) >>"; +const char* __KPADVersion = kpadVersion; +static const u8 dpdModeTable[12] = {0, 1, 3, 2, 0, 4, 1, 5, 0, 7, 1, 8}; + +static f32 idist_org = 1.0f; +static Vec2 iaccXY_nrm_hori = {0.0f, -1.0f}; +static Vec2 isec_nrm_hori = {1.0f, 0.0f}; +f32 kp_obj_interval = 0.2f; +f32 kp_acc_horizon_pw = 0.05f; +f32 kp_ah_circle_radius = 0.07f; +f32 kp_ah_circle_pw = 0.06f; +u16 kp_ah_circle_ct = 100; +f32 kp_err_outside_frame = 0.05f; +f32 kp_err_dist_max = 3.0f; +f32 kp_err_dist_speed = 0.04f; +f32 kp_err_first_inpr = 0.9f; +f32 kp_err_next_inpr = 0.9f; +f32 kp_err_acc_inpr = 0.9f; +f32 kp_err_up_inpr = 0.7f; +f32 kp_err_near_pos = 0.1f; +static u32 kp_fs_fstick_min = 15; +static u32 kp_fs_fstick_max = 71; +static u32 kp_cl_stick_min = 60; +static u32 kp_cl_stick_max = 308; +static u32 kp_cl_trigger_min = 30; +static u32 kp_cl_trigger_max = 180; +static f32 kp_rm_acc_max = 3.4f; +static f32 kp_fs_acc_max = 2.1f; +f32 sensor_bar_angle_degrees = 24.0f; + +static Vec2 icenter_org; +u32 kp_stick_clamp_cross; +static Vec2 Vec2_0; +static f32 kp_dist_vv1; +f32 kp_err_dist_min; + +static void KPADiSamplingCallback(s32 chan); +static void KPADiControlDpdCallback(s32 chan, s32 result); +static void calc_dpd_variable(KPADInside* kpad, s8 valid); + +static void* get_ring_buffer_by_kpad1_style(s32 chan, void* buffer, s32 style) { + KPADInside* kpad = &inside_kpads[chan]; + u32 i; + s32 index; + s32 latest; + int enabled; + s32 size; + u32 type; + switch (style) { + case 0: + size = 0x2A; + goto process; + case 1: + size = 0x32; + goto process; + case 2: + size = 0x36; + goto process; + default: + return buffer; + } +process: + enabled = OSDisableInterrupts(); + index = kpad->ringIndex - 1; + latest = WPADGetLatestIndexInBuf(chan); + for (i = 0; i < 16; i++) { + if (index < 0) { + index = 15; + } + if (latest < 0) { + latest = 15; + } + type = kpad->ringData[index].device; + switch (type) { + case 0: + case 0xFB: + case 0xFC: + case 0xFF: + type = 0; + break; + case 1: + type = 1; + break; + case 2: + type = 2; + break; + default: + type = 0xFF; + break; + } + if (type == style) { + if (WPADGetStatus() != 3) { + kpad->ringData[index].error = 0xFC; + } + memcpy((u8*)buffer + latest * size, &kpad->ringData[index], size); + } + index--; + latest--; + } + OSRestoreInterrupts(enabled); + return buffer; +} + +void* KPADGetWPADRingBuffer(s32 chan) { + static u8 status[0x2A0]; + return get_ring_buffer_by_kpad1_style(chan, status, 0); +} + +void* KPADGetWPADFSRingBuffer(s32 chan) { + static u8 status[0x320]; + return get_ring_buffer_by_kpad1_style(chan, status, 1); +} + +void* KPADGetWPADCLRingBuffer(s32 chan) { + static u8 status[0x360]; + return get_ring_buffer_by_kpad1_style(chan, status, 2); +} + +void KPADSetBtnRepeat(s32 chan, f32 delay, f32 pulse) { + KPADInside* kpad = &inside_kpads[chan]; + if (pulse) { + kpad->repeatDelay = (u16)(0.5f + 200.0f * delay); + kpad->repeatInterval = (u16)(0.5f + 200.0f * pulse); + } else { + kpad->repeatDelay = 40000; + kpad->repeatInterval = 0; + } + kpad->repeatCount = 0; + kpad->repeatCount2 = kpad->repeatDelay; + kpad->repeatCurrent = 0; + kpad->repeatCurrent2 = kpad->repeatDelay; +} + +void KPADSetPosParam(s32 chan, f32 x, f32 y) { + KPADInside* kpad = &inside_kpads[chan]; + kpad->posParamX = x; + kpad->posParamY = y; +} + +static void calc_button_repeat(KPADInside* kpad, u8 extension, s32 elapsed) { + u16 count; + u16 threshold; + u16 current; + u16 next; + if (kpad->status.trig != 0 || kpad->status.release != 0) { + kpad->repeatCount = 0; + kpad->repeatCount2 = kpad->repeatDelay; + if (kpad->status.trig != 0 && kpad->repeatInterval != 0) { + kpad->status.hold |= 0x80000000; + } + } else if (kpad->status.hold != 0) { + count = kpad->repeatCount + elapsed; + kpad->repeatCount = count; + if (count >= 40000) { + kpad->repeatCount = count - 40000; + } + threshold = kpad->repeatCount2; + current = kpad->repeatCount; + if (current >= threshold) { + kpad->status.hold |= 0x80000000; + next = threshold + kpad->repeatInterval; + kpad->repeatCount2 = next; + if (current >= 20000) { + kpad->repeatCount = current - 20000; + kpad->repeatCount2 = next - 20000; + } + } + } + if (extension == 2) { + if (kpad->status.ex_status.cl.trig != 0 || kpad->status.ex_status.cl.release != 0) { + kpad->repeatCurrent = 0; + kpad->repeatCurrent2 = kpad->repeatDelay; + if (kpad->status.ex_status.cl.trig != 0 && kpad->repeatInterval != 0) { + kpad->status.ex_status.cl.hold |= 0x80000000; + } + } else if (kpad->status.ex_status.cl.hold != 0) { + count = kpad->repeatCurrent + elapsed; + kpad->repeatCurrent = count; + if (count >= 40000) { + kpad->repeatCurrent = count - 40000; + } + threshold = kpad->repeatCurrent2; + current = kpad->repeatCurrent; + if (current >= threshold) { + kpad->status.ex_status.cl.hold |= 0x80000000; + next = threshold + kpad->repeatInterval; + kpad->repeatCurrent2 = next; + if (current >= 20000) { + kpad->repeatCurrent = current - 20000; + kpad->repeatCurrent2 = next - 20000; + } + } + } + } +} + +static void calc_acc_horizon(KPADInside* kpad) { + f32 accelX = kpad->accelerationX; + f32 accelY = kpad->accelerationY; + f32 magnitude = (f32)sqrt(accelX * accelX + accelY * accelY); + f32 normalizedX; + f32 normalizedY; + f32 targetY; + f32 targetX; + f32 productX; + f32 oldX; + f32 oldY; + f32 blend; + f32 projectedX; + f32 smoothing; + f32 deltaX; + f32 nextX; + f32 nextY; + f32 normalized; + f32 unitX; + f32 oldCircleX; + f32 oldCircleY; + f32 unitY; + f32 circleX; + f32 deltaCircleX; + f32 circleY; + f32 deltaCircleY; + if (magnitude != 0.0f) { + if (magnitude >= 2.0f) { + return; + } + normalizedX = kpad->accelerationX / magnitude; + normalizedY = kpad->accelerationY / magnitude; + if (magnitude > 1.0f) { + magnitude = 2.0f - magnitude; + } + targetY = kpad->accelNormalY; + targetX = kpad->accelNormalX; + productX = targetX * normalizedX; + oldX = kpad->horizonAxisX; + oldY = kpad->horizonAxisY; + blend = magnitude * kp_acc_horizon_pw; + projectedX = productX + targetY * normalizedY; + smoothing = magnitude * blend; + deltaX = smoothing * (projectedX - oldX); + nextX = oldX + deltaX; + nextY = oldY + smoothing * (((targetY * normalizedX) - (targetX * normalizedY)) - oldY); + normalized = (f32)sqrt(nextX * nextX + nextY * nextY); + if (normalized != 0.0f) { + unitX = nextX / normalized; + oldCircleX = kpad->horizonCircleX; + oldCircleY = kpad->horizonCircleY; + unitY = nextY / normalized; + kpad->horizonAxisX = unitX; + kpad->horizonAxisY = unitY; + circleX = oldCircleX + kp_ah_circle_pw * (unitX - oldCircleX); + deltaCircleX = unitX - circleX; + kpad->horizonCircleX = circleX; + circleY = oldCircleY + kp_ah_circle_pw * (unitY - oldCircleY); + deltaCircleY = unitY - circleY; + kpad->horizonCircleY = circleY; + if (deltaCircleX * deltaCircleX + deltaCircleY * deltaCircleY == kpad->value50C) { + if (kpad->horizonCircleCount != 0) { + kpad->horizonCircleCount--; + } + } else { + kpad->horizonCircleCount = kp_ah_circle_ct; + } + } + } +} + +static void calc_acc_vertical(KPADInside* kpad) { + f32 horizontal; + f32 horizontalMagnitude; + f32 accelZ; + f32 blend; + f32 magnitude; + f32 normalizedX; + f32 nextZ; + f32 normalized; + horizontalMagnitude = kpad->accelerationX * kpad->accelerationX + kpad->accelerationY * kpad->accelerationY; + horizontal = (f32)sqrt(horizontalMagnitude); + accelZ = -kpad->accelerationZ; + magnitude = (f32)sqrt(horizontalMagnitude + accelZ * accelZ); + if (magnitude == 0.0f || magnitude >= 2.0f) { + return; + } + normalizedX = horizontal / magnitude; + accelZ /= magnitude; + if (magnitude > 1.0f) { + magnitude = 2.0f - magnitude; + } + blend = magnitude * kp_acc_horizon_pw; + magnitude *= blend; + horizontal = kpad->status.acc_vertical.x + magnitude * (normalizedX - kpad->status.acc_vertical.x); + nextZ = kpad->status.acc_vertical.y + magnitude * (accelZ - kpad->status.acc_vertical.y); + normalized = (f32)sqrt(horizontal * horizontal + nextZ * nextZ); + if (normalized != 0.0f) { + kpad->status.acc_vertical.x = horizontal / normalized; + kpad->status.acc_vertical.y = nextZ / normalized; + } +} + +static void read_kpad_acc(KPADInside* kpad, KPADSample* status) { + u8 format = status->dataFormat; + if (format == 6) { + return; + } + if (format < 6) { + if (format == 3) { + return; + } + if (format < 3 && format < 1) { + return; + } + } else if (format >= 9) { + return; + } + { + f32 oldX = kpad->status.acc.x; + f32 oldY = kpad->status.acc.y; + f32 oldZ = kpad->status.acc.z; + f32 target; + f32 delta; + f32 magnitude; + f32 blend; + f32 currentX; + f32 currentY; + f32 currentZ; + f32 limit; + limit = kp_rm_acc_max; + target = (f32)-(s32)status->accX * kpad->value4DC; + if (target < 0.0f) { + limit = -limit; + if (target >= limit) { + limit = target; + } + } else if (target <= limit) { + limit = target; + } + kpad->accelerationX = limit; + limit = kp_rm_acc_max; + target = (f32)-(s32)status->accZ * kpad->value4E4; + if (target < 0.0f) { + limit = -limit; + if (target >= limit) { + limit = target; + } + } else if (target <= limit) { + limit = target; + } + kpad->accelerationY = limit; + limit = kp_rm_acc_max; + target = (f32)status->accY * kpad->value4E0; + if (target < 0.0f) { + limit = -limit; + if (target >= limit) { + limit = target; + } + } else if (target <= limit) { + limit = target; + } + kpad->accelerationZ = limit; + delta = kpad->accelerationX - oldX; + magnitude = delta; + if (magnitude < 0.0f) { + magnitude = -magnitude; + } + if (magnitude == kpad->value9C) { + blend = 1.0f; + } else { + blend = magnitude / kpad->value9C; + blend *= blend; + blend *= blend; + } + kpad->status.acc.x = oldX + blend * kpad->valueA0 * delta; + delta = kpad->accelerationY - oldY; + magnitude = delta; + if (magnitude < 0.0f) { + magnitude = -magnitude; + } + if (magnitude == kpad->value9C) { + blend = 1.0f; + } else { + blend = magnitude / kpad->value9C; + blend *= blend; + blend *= blend; + } + kpad->status.acc.y = oldY + blend * kpad->valueA0 * delta; + delta = kpad->accelerationZ - oldZ; + magnitude = delta; + if (magnitude < 0.0f) { + magnitude = -magnitude; + } + if (magnitude == kpad->value9C) { + blend = 1.0f; + } else { + blend = magnitude / kpad->value9C; + blend *= blend; + blend *= blend; + } + kpad->status.acc.z = oldZ + blend * kpad->valueA0 * delta; + currentX = kpad->status.acc.x; + currentY = kpad->status.acc.y; + currentZ = kpad->status.acc.z; + kpad->status.acc_value = (f32)sqrt(currentX * currentX + (currentY * currentY + currentZ * currentZ)); + delta = currentX - oldX; + currentX = currentY - oldY; + currentY = currentZ - oldZ; + kpad->status.acc_speed = (f32)sqrt(delta * delta + (currentX * currentX + currentY * currentY)); + calc_acc_horizon(kpad); + calc_acc_vertical(kpad); + } + if (status->error != 0 || status->device != 1) { + return; + } + if (format != 4 && format != 5) { + return; + } + { + Vec raw; + f32* values = (f32*)&raw; + f32 oldX; + f32 oldY; + f32 oldZ; + f32 delta; + f32 magnitude; + f32 blend; + f32 currentX; + f32 currentY; + f32 currentZ; + f32 limit; + values[0] = (f32)-(s32)status->extension.fs.accX * kpad->value4E8; + limit = kp_fs_acc_max; + if (values[0] < -kp_fs_acc_max) { + limit = -kp_fs_acc_max; + } else if (values[0] <= limit) { + limit = values[0]; + } + values[0] = limit; + values[1] = (f32)-(s32)status->extension.fs.accZ * kpad->value4F0; + limit = kp_fs_acc_max; + if (values[1] < -kp_fs_acc_max) { + limit = -kp_fs_acc_max; + } else if (values[1] <= limit) { + limit = values[1]; + } + values[1] = limit; + values[2] = (f32)status->extension.fs.accY * kpad->value4EC; + limit = kp_fs_acc_max; + if (values[2] < -kp_fs_acc_max) { + limit = -kp_fs_acc_max; + } else if (values[2] <= limit) { + limit = values[2]; + } + values[2] = limit; + if (kpad->freeStyleAccelRotation != 0) { + PSMTXMultVec((const f32 (*)[4])initial_rotation_matrix, &raw, &raw); + } + oldX = kpad->status.ex_status.fs.acc.x; + oldY = kpad->status.ex_status.fs.acc.y; + oldZ = kpad->status.ex_status.fs.acc.z; + delta = values[0] - oldX; + magnitude = delta; + if (magnitude < 0.0f) { + magnitude = -magnitude; + } + if (magnitude == kpad->value9C) { + blend = 1.0f; + } else { + blend = magnitude / kpad->value9C; + blend *= blend; + blend *= blend; + } + kpad->status.ex_status.fs.acc.x = oldX + blend * kpad->valueA0 * delta; + delta = values[1] - oldY; + magnitude = delta; + if (magnitude < 0.0f) { + magnitude = -magnitude; + } + if (magnitude == kpad->value9C) { + blend = 1.0f; + } else { + blend = magnitude / kpad->value9C; + blend *= blend; + blend *= blend; + } + kpad->status.ex_status.fs.acc.y = oldY + blend * kpad->valueA0 * delta; + delta = values[2] - oldZ; + magnitude = delta; + if (magnitude < 0.0f) { + magnitude = -magnitude; + } + if (magnitude == kpad->value9C) { + blend = 1.0f; + } else { + blend = magnitude / kpad->value9C; + blend *= blend; + blend *= blend; + } + kpad->status.ex_status.fs.acc.z = oldZ + blend * kpad->valueA0 * delta; + currentX = kpad->status.ex_status.fs.acc.x; + currentY = kpad->status.ex_status.fs.acc.y; + currentZ = kpad->status.ex_status.fs.acc.z; + kpad->status.ex_status.fs.acc_value = (f32)sqrt(currentX * currentX + (currentY * currentY + currentZ * currentZ)); + delta = currentX - oldX; + currentX = currentY - oldY; + currentY = currentZ - oldZ; + kpad->status.ex_status.fs.acc_speed = (f32)sqrt(delta * delta + (currentX * currentX + currentY * currentY)); + } +} + +static s8 select_2obj_first(KPADInside* kpad) { + KPADDPDObject* objects = kpad->dpdState.objects; + KPADDPDObject* first = 0; + KPADDPDObject* second = 0; + f32 best = kp_err_first_inpr; + s32 i; + s32 j; + for (i = 0; i < 4; i++) { + if ((s8)objects[i].metadata.bytes.flags != 0) { + continue; + } + for (j = i + 1; j < 4; j++) { + f32 dx; + f32 dy; + f32 scale; + f32 nx; + f32 ny; + f32 dist; + f32 x; + f32 y; + f32 dot; + f32 score; + if ((s8)objects[j].metadata.bytes.flags != 0) { + continue; + } + dx = objects[j].x - objects[i].x; + dy = objects[j].y - objects[i].y; + scale = 1.0f / (f32)sqrt(dx * dx + dy * dy); + nx = dx * scale; + ny = dy * scale; + dist = kpad->value510 * scale; + if (dist == kpad->value514 || dist == kp_err_dist_max) { + continue; + } + x = kpad->horizonTangentX * nx + kpad->horizonTangentY * ny; + y = kpad->horizonTangentY * nx - kpad->horizonTangentX * ny; + dot = kpad->horizonAxisX * x + kpad->horizonAxisY * y; + score = dot; + if (score < 0.0f) { + score = -score; + if (score > best) { + best = score; + first = &objects[j]; + second = &objects[i]; + } + } else if (score > best) { + best = score; + first = &objects[i]; + second = &objects[j]; + } + } + } + if (best == kp_err_first_inpr) { + return 0; + } + ((u32*)&kpad->dpdState.candidates[0].x)[0] = ((u32*)first)[0]; + ((u32*)&kpad->dpdState.candidates[0].x)[1] = ((u32*)first)[1]; + ((u32*)&kpad->dpdState.candidates[0].x)[2] = ((u32*)first)[2]; + ((u32*)&kpad->dpdState.candidates[1].x)[0] = ((u32*)second)[0]; + ((u32*)&kpad->dpdState.candidates[1].x)[1] = ((u32*)second)[1]; + ((u32*)&kpad->dpdState.candidates[1].x)[2] = ((u32*)second)[2]; + return 2; +} + +static s8 select_2obj_continue(KPADInside* kpad) { + KPADDPDObject* objects = kpad->dpdState.objects; + KPADDPDObject* first = 0; + KPADDPDObject* second = 0; + f32 best = -1.0f; + s32 i; + s32 j; + for (i = 0; i < 4; i++) { + if ((s8)objects[i].metadata.bytes.flags != 0) { + continue; + } + for (j = i + 1; j < 4; j++) { + f32 dx; + f32 dy; + f32 scale; + f32 nx; + f32 ny; + f32 distance; + f32 distanceError; + f32 distanceScore; + f32 dot; + f32 score; + BOOL reverse; + if ((s8)objects[j].metadata.bytes.flags != 0) { + continue; + } + dx = objects[j].x - objects[i].x; + dy = objects[j].y - objects[i].y; + scale = 1.0f / (f32)sqrt(dx * dx + dy * dy); + distance = scale * kpad->value510; + nx = dx * scale; + ny = dy * scale; + if (distance == kpad->value514 || distance == kp_err_dist_max) { + continue; + } + distanceError = distance - kpad->dpdReferenceDistance; + if (distanceError < 0.0f) { + distanceScore = distanceError * kpad->value508; + } else { + distanceScore = distanceError * kpad->value504; + } + if (distanceScore == 1.0f) { + continue; + } + dot = kpad->dpdObjectDirectionX * nx + kpad->dpdObjectDirectionY * ny; + reverse = dot < 0.0f; + if (reverse) { + dot = -dot; + } + if (dot == kp_err_next_inpr) { + continue; + } + score = distanceScore + ((1.0f - dot) / (1.0f - kp_err_next_inpr)); + if (score < best || best < 0.0f) { + best = score; + if (reverse) { + first = &objects[j]; + second = &objects[i]; + } else { + first = &objects[i]; + second = &objects[j]; + } + } + } + } + if (best < 0.0f) { + return 0; + } + ((u32*)&kpad->dpdState.candidates[0].x)[0] = ((u32*)first)[0]; + ((u32*)&kpad->dpdState.candidates[0].x)[1] = ((u32*)first)[1]; + ((u32*)&kpad->dpdState.candidates[0].x)[2] = ((u32*)first)[2]; + ((u32*)&kpad->dpdState.candidates[1].x)[0] = ((u32*)second)[0]; + ((u32*)&kpad->dpdState.candidates[1].x)[1] = ((u32*)second)[1]; + ((u32*)&kpad->dpdState.candidates[1].x)[2] = ((u32*)second)[2]; + return 2; +} + +static s8 select_1obj_first(KPADInside* kpad) { + KPADDPDObject* objects = kpad->dpdState.objects; + KPADDPDObject* object; + KPADDPDObject* end = kpad->dpdState.candidates; + f32 xAxis = kpad->horizonTangentX; + f32 xNorm = kpad->horizonAxisX; + f32 yAxis = kpad->horizonTangentY; + f32 yNorm = kpad->horizonAxisY; + f32 scale = kpad->dpdObjectScale; + f32 offsetX = (xAxis * xNorm + yAxis * yNorm) * scale; + f32 offsetY = (yAxis * xNorm - xAxis * yNorm) * scale; + object = objects; + do { + if ((s8)object->metadata.bytes.flags == 0) { + f32 x = object->x; + f32 y = object->y; + f32 left = x - offsetX; + f32 bottom = y - offsetY; + f32 right = x + offsetX; + f32 top = y + offsetY; + f32 leftBound = kpad->value4F4; + f32 rightBound = kpad->value4FC; + f32 topBound = kpad->value4F8; + f32 bottomBound = kpad->value500; + if (left == leftBound || left == rightBound || bottom == topBound || bottom == bottomBound) { + if (right > leftBound && right < rightBound && top > topBound && top < bottomBound) { + u32* objectWords = (u32*)object; + u32* statusWords = (u32*)&kpad->dpdState.candidates[1].x; + statusWords[0] = objectWords[0]; + statusWords[1] = objectWords[1]; + statusWords[2] = objectWords[2]; + kpad->dpdState.candidates[0].x = left; + kpad->dpdState.candidates[0].y = bottom; + *(u32*)&kpad->dpdState.candidates[0].metadata.value = 0; + kpad->dpdState.candidates[0].metadata.bytes.status = 0xFF; + return -1; + } + } else if (right == leftBound || right == rightBound || top == topBound || top == bottomBound) { + u32* objectWords = (u32*)object; + u32* statusWords = (u32*)&*(u32*)&kpad->dpdState.candidates[0].metadata.value; + statusWords[0] = objectWords[0]; + statusWords[1] = objectWords[1]; + statusWords[2] = objectWords[2]; + kpad->dpdState.candidates[1].x = right; + kpad->dpdState.candidates[1].y = top; + *(u32*)&kpad->dpdState.candidates[1].metadata.value = 0; + kpad->dpdState.candidates[1].metadata.bytes.status = 0xFF; + return -1; + } + } + } while (++object < end); + return 0; +} + +static s8 select_1obj_continue(KPADInside* kpad) { + KPADDPDObject* objects = kpad->dpdState.objects; + KPADDPDObject* candidates = kpad->dpdState.candidates; + KPADDPDObject* matchedCandidate = 0; + KPADDPDObject* source = 0; + KPADDPDObject* candidate; + f32 best = kp_err_near_pos * kp_err_near_pos; + candidate = candidates; + do { + if ((s8)candidate->metadata.bytes.flags == 0 && (s8)candidate->metadata.bytes.status == 0) { + KPADDPDObject* object = objects; + do { + if ((s8)object->metadata.bytes.flags == 0) { + f32 dx = candidate->x - object->x; + f32 dy = candidate->y - object->y; + f32 distance = dx * dx + dy * dy; + if (distance < best) { + best = distance; + matchedCandidate = candidate; + source = object; + } + } + } while (++object < candidates); + } + } while (++candidate < (kpad->dpdState.candidates + 2)); + if (best == kp_err_near_pos * kp_err_near_pos) { + return 0; + } + ((u32*)matchedCandidate)[0] = ((u32*)source)[0]; + ((u32*)matchedCandidate)[1] = ((u32*)source)[1]; + ((u32*)matchedCandidate)[2] = ((u32*)source)[2]; + { + f32 axisX = kpad->horizonTangentX; + f32 normX = kpad->horizonAxisX; + f32 axisY = kpad->horizonTangentY; + f32 normY = kpad->horizonAxisY; + f32 distance = kpad->dpdObjectDistance; + f32 horizontal = axisX * normX; + f32 vertical = axisY * normX; + f32 offsetX; + f32 directionX; + f32 directionY; + f32 offsetY; + directionX = horizontal + axisY * normY; + directionY = vertical - axisX * normY; + offsetX = distance * directionX; + kpad->dpdObjectDirectionX = directionX; + offsetY = distance * directionY; + kpad->dpdObjectDirectionY = directionY; + if (matchedCandidate == candidates) { + kpad->dpdState.candidates[1].x = matchedCandidate->x + offsetX; + *(u32*)&kpad->dpdState.candidates[1].metadata.value = 0; + kpad->dpdState.candidates[1].metadata.bytes.status = 0xFF; + kpad->dpdState.candidates[1].y = matchedCandidate->y + offsetY; + } else { + kpad->dpdState.candidates[0].x = matchedCandidate->x - offsetX; + *(u32*)&kpad->dpdState.candidates[0].metadata.value = 0; + kpad->dpdState.candidates[0].metadata.bytes.status = 0xFF; + kpad->dpdState.candidates[0].y = matchedCandidate->y - offsetY; + } + } + if (kpad->status.dpd_valid_fg < 0) { + return -1; + } + return 1; +} + +static void read_kpad_dpd(KPADInside* kpad, KPADSample* status) { + KPADDPDObject* objects = kpad->dpdState.objects; + u8 format = status->dataFormat; + KPADDPDObject* object; + s8 selected = 0; + if (format == 2 || format == 5 || format == 8) { + DPDObject* source = &status->objects[3]; + object = objects + 3; + for (;;) { + if (source->size != 0) { + object->x = (f32)source->x * 0.001953125f - 0.9990234375f; + object->y = (f32)source->y * 0.001953125f - 0.7490234375f; + object->metadata.bytes.flags = 0; + object->metadata.bytes.status = 0; + } else { + object->metadata.bytes.flags = 0xFF; + } + if (object == objects) { + break; + } + object--; + source--; + } + } else { + object = objects + 3; + for (;;) { + object->metadata.bytes.flags = 0xFF; + if (object == objects) { + break; + } + object--; + } + } + object = objects + 3; + for (;;) { + if ((s8)object->metadata.bytes.flags >= 0 && + (object->x == kpad->value4F4 || object->x == kpad->value4FC || + object->y == kpad->value4F8 || object->y == kpad->value500)) { + object->metadata.bytes.flags |= 1; + } + if (object == objects) { + break; + } + object--; + } + for (object = objects; object < objects + 3; object++) { + if ((s8)object->metadata.bytes.flags == 0) { + KPADDPDObject* other = object + 1; + for (;;) { + if ((s8)other->metadata.bytes.flags == 0 && object->x == other->x && object->y == other->y) { + other->metadata.bytes.flags |= 2; + } + if (other == objects + 3) { + break; + } + other++; + } + } + } + kpad->dpdCount = 0; + object = objects + 3; + for (;;) { + if ((s8)object->metadata.bytes.flags == 0) { + kpad->dpdCount++; + } + if (object == objects) { + break; + } + object--; + } + if (kpad->status.acc_vertical.x != kp_err_up_inpr) { + switch (kpad->status.dpd_valid_fg) { + case 2: + case -2: + if (kpad->dpdCount >= 2) { + selected = select_2obj_continue(kpad); + } + if (selected == 0 && kpad->dpdCount >= 1) { + selected = select_1obj_continue(kpad); + } + break; + case 1: + case -1: + if (kpad->dpdCount >= 2) { + selected = select_2obj_first(kpad); + } + if (selected == 0 && kpad->dpdCount >= 1) { + selected = select_1obj_continue(kpad); + } + break; + default: + if (kpad->dpdCount >= 2) { + selected = select_2obj_first(kpad); + } + if (selected == 0 && kpad->dpdCount == 1) { + selected = select_1obj_first(kpad); + } + break; + } + } + if (selected != 0) { + f32 dx = kpad->dpdState.candidates[1].x - kpad->dpdState.candidates[0].x; + f32 dy = kpad->dpdState.candidates[1].y - kpad->dpdState.candidates[0].y; + f32 length = (f32)sqrt(dx * dx + dy * dy); + f32 scale = 1.0f / length; + f32 axisX; + f32 axisY; + kpad->dpdObjectDistance = length; + kpad->dpdObjectDirectionX = dx * scale; + kpad->dpdReferenceDistance = kpad->value510 * scale; + kpad->dpdObjectDirectionY = dy * scale; + axisX = kpad->horizonTangentX * kpad->dpdObjectDirectionX + kpad->horizonTangentY * kpad->dpdObjectDirectionY; + axisY = kpad->horizonTangentY * kpad->dpdObjectDirectionX - kpad->horizonTangentX * kpad->dpdObjectDirectionY; + kpad->dpdAxisX = axisX; + kpad->dpdAxisY = axisY; + if (kpad->horizonCircleCount == 0 && axisX * kpad->horizonAxisX + axisY * kpad->horizonAxisY == kp_err_acc_inpr) { + selected = 0; + *(u32*)&kpad->dpdState.candidates[1].metadata.value = 1; + *(u32*)&kpad->dpdState.candidates[0].metadata.value = 1; + } + if (kpad->status.dpd_valid_fg == 2 && selected == 2) { + if (kpad->value4CA == 0xC8) { + kpad->dpdObjectScale = kpad->dpdObjectDistance; + } else { + kpad->value4CA++; + } + } else { + kpad->value4CA = 0; + } + } else { + kpad->value4CA = 0; + } + calc_dpd_variable(kpad, selected); +} + +static void clamp_stick_circle(Vec2* stick, s32 x, s32 y, s32 minimum, s32 maximum) { + f32 axisX = x; + f32 axisY = y; + f32 minimumValue = minimum; + f32 maximumValue = maximum; + f32 radius = (f32)sqrt(axisX * axisX + axisY * axisY); + if (radius <= minimumValue) { + stick->y = 0.0f; + stick->x = 0.0f; + return; + } + if (radius >= maximumValue) { + stick->x = axisX / radius; + stick->y = axisY / radius; + return; + } + radius = ((radius - minimumValue) / (maximumValue - minimumValue)) / radius; + stick->x = axisX * radius; + stick->y = axisY * radius; +} + +static void clamp_stick_cross(Vec2* stick, s32 x, s32 y, s32 minimum, s32 maximum) { + f32 value; + f32 scale; + s32 magnitude; + if (x < 0) { + magnitude = -x; + if (magnitude <= minimum) { + stick->x = 0.0f; + } else if (magnitude >= maximum) { + stick->x = 1.0f; + } else { + stick->x = (f32)-(x + minimum) / (maximum - minimum); + } + stick->x = -stick->x; + } else if (x <= minimum) { + stick->x = 0.0f; + } else if (x >= maximum) { + stick->x = 1.0f; + } else { + stick->x = (f32)(x - minimum) / (maximum - minimum); + } + if (y < 0) { + magnitude = -y; + if (magnitude <= minimum) { + stick->y = 0.0f; + } else if (magnitude >= maximum) { + stick->y = 1.0f; + } else { + stick->y = (f32)-(y + minimum) / (maximum - minimum); + } + stick->y = -stick->y; + } else if (y <= minimum) { + stick->y = 0.0f; + } else if (y >= maximum) { + stick->y = 1.0f; + } else { + stick->y = (f32)(y - minimum) / (maximum - minimum); + } + value = stick->x; + scale = stick->y; + if (value * value + scale * scale > 1.0f) { + f32 length = (f32)sqrt(value * value + scale * scale); + stick->x /= length; + stick->y /= length; + } +} + +static void read_kpad_stick(KPADInside* kpad, KPADSample* status) { + typedef void (*StickClamp)(Vec2*, s32, s32, s32, s32); + StickClamp clamp = clamp_stick_cross; + KPADEXStatus* extension = &kpad->status.ex_status; + u8 device; + u8 format; + if (kp_stick_clamp_cross != 0) { + clamp = clamp_stick_circle; + } + device = status->device; + format = status->dataFormat; + switch (device) { + case 1: + if ((u8)(format + 0xFD) <= 2) { + if (kpad->flag51E != 0) { + kpad->flag51E = 0; + extension->fs.stick = Vec2_0; + extension->fs.acc.z = 0.0f; + extension->fs.acc.x = 0.0f; + extension->fs.acc.y = -1.0f; + extension->fs.acc_value = 1.0f; + extension->fs.acc_speed = 0.0f; + } + clamp(&extension->fs.stick, status->extension.fs.stickX, status->extension.fs.stickY, kp_fs_fstick_min, kp_fs_fstick_max); + } + break; + case 2: + if ((u8)(format + 0xFA) > 2) { + break; + } + if (kpad->flag51E != 0) { + kpad->flag51E = 0; + extension->cl.lstick = Vec2_0; + extension->cl.rstick = Vec2_0; + extension->cl.rtrigger = 0.0f; + extension->cl.ltrigger = 0.0f; + extension->cl.release = 0; + extension->cl.trig = 0; + extension->cl.hold = 0; + kpad->repeatCurrent = 0; + kpad->repeatCurrent2 = kpad->repeatDelay; + } + clamp(&extension->cl.lstick, status->extension.cl.lStickX, status->extension.cl.lStickY, kp_cl_stick_min, kp_cl_stick_max); + clamp(&extension->cl.rstick, (s8)status->extension.cl.rStickX, (s8)status->extension.cl.rStickY, kp_cl_stick_min, kp_cl_stick_max); + if (status->extension.cl.triggerL <= kp_cl_trigger_min) { + extension->cl.ltrigger = 0.0f; + } else if (status->extension.cl.triggerL >= kp_cl_trigger_max) { + extension->cl.ltrigger = 1.0f; + } else { + extension->cl.ltrigger = (f32)(status->extension.cl.triggerL - kp_cl_trigger_min) / (kp_cl_trigger_max - kp_cl_trigger_min); + } + if (status->extension.cl.triggerR <= kp_cl_trigger_min) { + extension->cl.rtrigger = 0.0f; + break; + } + if (status->extension.cl.triggerR >= kp_cl_trigger_max) { + extension->cl.rtrigger = 1.0f; + break; + } + extension->cl.rtrigger = (f32)(status->extension.cl.triggerR - kp_cl_trigger_min) / (kp_cl_trigger_max - kp_cl_trigger_min); + break; + default: + break; + } +} + +static void reset_kpad(KPADInside* kpad) { + f32 distanceValue; + f32 sensorDistance; + u32* words; + KPADDPDObject* object; + sensorDistance = kpad->referenceDistance; + kpad->flag51D = 0; + kpad->value4F4 = -1.0f + kp_err_outside_frame; + kpad->value4FC = 1.0f - kp_err_outside_frame; + kpad->value4F8 = -0.75f + kp_err_outside_frame; + kpad->value500 = 0.75f - kp_err_outside_frame; + kpad->value504 = 1.0f / kp_err_dist_speed; + kpad->value508 = -1.0f / kp_err_dist_speed; + kpad->value50C = kp_ah_circle_radius * kp_ah_circle_radius; + kpad->value514 = kp_err_dist_min; + distanceValue = kp_dist_vv1 / sensorDistance; + kpad->value510 = kp_dist_vv1; + kpad->status.release = 0; + kpad->status.trig = 0; + kpad->status.hold = 0; + kpad->repeatCount = 0; + kpad->repeatCount2 = kpad->repeatDelay; + kpad->status.dpd_valid_fg = 0; + kpad->value4CA = 0; + kpad->horizonAxisX = 1.0f; + kpad->horizonAxisY = 0.0f; + kpad->status.vec = Vec2_0; + kpad->status.pos = Vec2_0; + kpad->status.speed = 0.0f; + kpad->dpdAxisX = 1.0f; + kpad->status.horizon.x = 1.0f; + kpad->dpdAxisY = 0.0f; + kpad->status.horizon.y = 0.0f; + kpad->status.hori_speed = 0.0f; + kpad->status.acc.z = 0.0f; + kpad->status.acc.x = 0.0f; + words = (u32*)&kpad->status.acc; + kpad->status.acc.y = -1.0f; + kpad->status.hori_vec = Vec2_0; + kpad->status.dist_speed = 0.0f; + kpad->status.acc_vertical.x = 1.0f; + kpad->status.acc_vertical.y = 0.0f; + kpad->status.dist = sensorDistance; + kpad->status.dist_vec = 0.0f; + kpad->dpdReferenceDistance = sensorDistance; + kpad->dpdObjectScale = distanceValue; + kpad->dpdObjectDistance = distanceValue; + kpad->dpdObjectDirectionX = kpad->horizonTangentX; + kpad->dpdObjectDirectionY = kpad->horizonTangentY; + kpad->status.acc_value = 1.0f; + kpad->status.acc_speed = 0.0f; + *(u32*)&kpad->accelerationX = words[0]; + *(u32*)&kpad->accelerationY = words[1]; + *(u32*)&kpad->accelerationZ = words[2]; + *(u32*)&kpad->horizonCircleX = *(u32*)&kpad->horizonAxisX; + *(u32*)&kpad->horizonCircleY = *(u32*)&kpad->horizonAxisY; + kpad->horizonCircleCount = kp_ah_circle_ct; + kpad->dpdCount = 0; + object = &kpad->dpdState.objects[4]; + do { + --object; + object->metadata.bytes.flags = 0xFF; + } while (object != kpad->dpdState.objects); + object = &kpad->dpdState.candidates[2]; + do { + --object; + object->metadata.bytes.flags = 0xFF; + } while (object != kpad->dpdState.candidates); + kpad->ringCount = 0; + kpad->flag51E = 1; +} + +void KPADGetProjectionPos(Vec2* dest, Vec2* src, const Rect* rect, f32 scale) { + f64 scaled; + f32 height = rect->bottom - rect->top; + f32 halfHeight = height * 0.5f; + f32 x = src->x * halfHeight; + f32 y = src->y * halfHeight; + x *= 1.2f; + y *= 1.2f; + scaled = 0.908 * scale; + dest->y = y; + dest->x = (f32)(x * scaled); +} + +void KPADSetSensorHeight(s32 chan, f32 sensorHeight) { + KPADInside* kpad; + f32 halfHeight; + f32 halfWidth; + f32 negativeSensorHeight; + f32 halfWidthSquared; + f32 halfHeightSquared; + f32 barDiagonalSquared; + f32 barOffsetX; + f32 barDiagonal; + halfWidth = 1.0f; + kpad = &inside_kpads[chan]; + halfHeight = 0.75f; + negativeSensorHeight = -sensorHeight; + halfWidthSquared = halfWidth * halfWidth; + halfHeightSquared = halfHeight * halfHeight; + kpad->sensorBarCenterX = 0.0f; + kpad->sensorBarCenterY = negativeSensorHeight; + barDiagonalSquared = halfWidthSquared + halfHeightSquared; + barDiagonal = (f32)sqrt(barDiagonalSquared); + barOffsetX = kpad->sensorBarCenterX; + if (barOffsetX < 0.0f) { + halfWidth = halfWidth + barOffsetX; + } else { + halfWidth = halfWidth - barOffsetX; + } + negativeSensorHeight = kpad->sensorBarCenterY; + if (negativeSensorHeight < 0.0f) { + halfHeight = halfHeight + negativeSensorHeight; + } else { + halfHeight = halfHeight - negativeSensorHeight; + } + if (halfWidth >= halfHeight) { + halfWidth = halfHeight; + } + kpad->sensorBarScale = barDiagonal / halfWidth; +} + +static void calc_dpd_variable(KPADInside* kpad, s8 valid) { + if (valid == 0) { + kpad->status.dpd_valid_fg = 0; + return; + } + { + f32 x = kpad->horizonTangentX * kpad->dpdObjectDirectionX + kpad->horizonTangentY * kpad->dpdObjectDirectionY; + f32 y = kpad->horizonTangentY * kpad->dpdObjectDirectionX - kpad->horizonTangentX * kpad->dpdObjectDirectionY; + if (kpad->status.dpd_valid_fg == 0) { + kpad->status.horizon.x = x; + kpad->status.horizon.y = y; + kpad->status.hori_vec = Vec2_0; + kpad->status.hori_speed = 0.0f; + } else { + f32 dx = x - kpad->status.horizon.x; + f32 dy = y - kpad->status.horizon.y; + f32 length = (f32)sqrt(dx * dx + dy * dy); + f32 amount; + if (length == kpad->value8C) { + amount = 1.0f; + } else { + amount = length / kpad->value8C; + amount *= amount; + amount *= amount; + } + amount *= kpad->value90; + x = kpad->status.horizon.x + amount * dx; + y = kpad->status.horizon.y + amount * dy; + length = (f32)sqrt(x * x + y * y); + x /= length; + y /= length; + kpad->status.hori_vec.x = x - kpad->status.horizon.x; + kpad->status.hori_vec.y = y - kpad->status.horizon.y; + kpad->status.horizon.x = x; + kpad->status.horizon.y = y; + kpad->status.hori_speed = (f32)sqrt(kpad->status.hori_vec.x * kpad->status.hori_vec.x + kpad->status.hori_vec.y * kpad->status.hori_vec.y); + } + } + { + f32 value = kpad->value510 / kpad->dpdObjectDistance; + if (kpad->status.dpd_valid_fg == 0) { + kpad->status.dist = value; + kpad->status.dist_vec = 0.0f; + kpad->status.dist_speed = 0.0f; + } else { + f32 dx = value - kpad->status.dist; + f32 magnitude = dx; + f32 amount; + f32 next; + if (magnitude < 0.0f) { + magnitude = -magnitude; + } + if (magnitude == kpad->value94) { + amount = 1.0f; + } else { + amount = magnitude / kpad->value94; + amount *= amount; + amount *= amount; + } + next = amount * kpad->value98 * dx; + kpad->status.dist_vec = next; + if (next < 0.0f) { + kpad->status.dist_speed = -next; + } else { + kpad->status.dist_speed = next; + } + kpad->status.dist += kpad->status.dist_vec; + } + } + { + f32 rotatedX = kpad->dpdObjectDirectionX * kpad->horizonTangentX + kpad->dpdObjectDirectionY * kpad->horizonTangentY; + f32 rotatedY = -kpad->dpdObjectDirectionY * kpad->horizonTangentX + kpad->dpdObjectDirectionX * kpad->horizonTangentY; + f32 scaleX = 0.5f * (kpad->dpdState.candidates[0].x + kpad->dpdState.candidates[1].x); + f32 scaleY = 0.5f * (kpad->dpdState.candidates[0].y + kpad->dpdState.candidates[1].y); + f32 pointX = kpad->sensorBarScale * (kpad->sensorBarCenterX - (rotatedX * scaleX - rotatedY * scaleY)); + f32 pointY = kpad->sensorBarScale * (kpad->sensorBarCenterY - (rotatedY * scaleX + rotatedX * scaleY)); + f32 x = -kpad->accelNormalY * pointX + kpad->accelNormalX * pointY; + f32 y = -kpad->accelNormalX * pointX - kpad->accelNormalY * pointY; + if (kpad->status.dpd_valid_fg == 0) { + kpad->status.pos.x = x; + kpad->status.pos.y = y; + kpad->status.vec = Vec2_0; + kpad->status.speed = 0.0f; + } else { + f32 dx = x - kpad->status.pos.x; + f32 dy = y - kpad->status.pos.y; + f32 length = (f32)sqrt(dx * dx + dy * dy); + f32 amount; + if (length == kpad->posParamX) { + amount = 1.0f; + } else { + amount = length / kpad->posParamX; + amount *= amount; + amount *= amount; + } + amount *= kpad->posParamY; + kpad->status.vec.x = amount * dx; + kpad->status.vec.y = amount * dy; + kpad->status.speed = (f32)sqrt(kpad->status.vec.x * kpad->status.vec.x + kpad->status.vec.y * kpad->status.vec.y); + kpad->status.pos.x += kpad->status.vec.x; + kpad->status.pos.y += kpad->status.vec.y; + } + } + kpad->status.dpd_valid_fg = valid; +} + +void KPADDisableDPD(s32 chan) { + inside_kpads[chan].dpdEnable = 0; +} + +void KPADEnableDPD(s32 chan) { + inside_kpads[chan].dpdEnable = 1; +} + +void KPADSetControlDpdCallback(s32 chan, KPADCallback* callback) { + BOOL enabled; + enabled = OSDisableInterrupts(); + inside_kpads[chan].dpdCallback = (KPADCallback)callback; + OSRestoreInterrupts(enabled); +} + +s32 KPADRead(s32 chan, KPADStatus* statuses, u32 count) { + KPADInside* kpad = &inside_kpads[chan]; + s32 probe; + u16 interruptState; + u32 available; + u32 ringCount; + s32 start; + u32 sampleIndex; + u32 remaining; + u32 remainingSamples; + u32 outputIndex; + u32 buttons; + u32 previousButtons; + u32 changed; + u16 extensionButtons; + u16 coreButtons; + u8 device; + KPADSample latestSample; + KPADSample* sample; + KPADStatus* output; + WPADAccGravityUnit gravity; + if (WPADGetStatus() != 3) { + return 0; + } + interruptState = OSDisableInterrupts(); + if (kpad->samplingInProgress != 0) { + OSRestoreInterrupts(interruptState); + return 0; + } + kpad->samplingInProgress = 1; + probe = WPADProbe(chan, 0); + if (probe == -1 && kpad->dpdCallback != 0 && kpad->dpdCallbackFired != 0 && kpad->dpdCallbackPending == 0) { + if (kpad->dpdCallback == 0) { + kpad->dpdCallbackPending = 1; + kpad->dpdCallback(chan, 1); + kpad->dpdCallbackFired = 0; + } + kpad->flag51F = 0; + } + OSRestoreInterrupts(interruptState); + if (kpad->flag51D != 0) { + kpad->status.wpad_err = probe; + reset_kpad(kpad); + } + WPADSetSamplingCallback(chan, KPADiSamplingCallback); + if (kpad->ringCount == 0 || statuses == 0 || count == 0) { + kpad->samplingInProgress = 0; + return 0; + } + interruptState = OSDisableInterrupts(); + ringCount = kpad->ringCount; + available = ringCount; + if (available > count) { + available = count; + } + kpad->ringCount = 0; + start = kpad->ringIndex - available; + if (start < 0) { + start += 16; + } + sampleIndex = start; + remaining = available; + output = &statuses[available - 1]; + while (--remaining != 0) { + output--; + *(KPADSample*)output = kpad->ringData[sampleIndex]; + sampleIndex++; + if (sampleIndex >= 16) { + sampleIndex = 0; + } + } + latestSample = kpad->ringData[sampleIndex]; + OSRestoreInterrupts(interruptState); + WPADGetAccGravityUnit(chan, WPAD_ACC_GRAVITY_UNIT_CORE, &gravity); + if (gravity.z * gravity.x * gravity.y != 0) { + kpad->value4DC = 1.0f / gravity.x; + kpad->value4E0 = 1.0f / gravity.y; + kpad->value4E4 = 1.0f / gravity.z; + } else { + kpad->value4DC = 0.01f; + kpad->value4E0 = 0.01f; + kpad->value4E4 = 0.01f; + } + WPADGetAccGravityUnit(chan, WPAD_ACC_GRAVITY_UNIT_FS, &gravity); + if (gravity.z * gravity.x * gravity.y != 0) { + kpad->value4E8 = 1.0f / gravity.x; + kpad->value4EC = 1.0f / gravity.y; + kpad->value4F0 = 1.0f / gravity.z; + } else { + kpad->value4E8 = 0.005f; + kpad->value4EC = 0.005f; + kpad->value4F0 = 0.005f; + } + remainingSamples = available; + output = &statuses[available - 1]; + device = 0xFD; + coreButtons = 0xFFFF; + extensionButtons = 0xFFFF; + buttons = 0xFFFF; + while (remainingSamples != 0) { + output--; + sample = remainingSamples > 1 ? (KPADSample*)output : &latestSample; + switch (sample->error) { + case 0: + device = sample->device; + if (device == 1) { + coreButtons = sample->buttons; + extensionButtons = 0; + } else if (device == 2) { + coreButtons = 0; + extensionButtons = sample->extension.cl.buttons; + } else { + coreButtons = 0; + extensionButtons = 0; + } + buttons = sample->buttons; + break; + case -2: + case -7: + buttons = sample->buttons; + break; + } + remainingSamples--; + } + if (buttons == 0xFFFF) { + output = statuses; + remaining = available; + while (remaining != 0) { + *output++ = kpad->status; + remaining--; + } + } else { + if (coreButtons == 0xFFFF) { + coreButtons = kpad->status.hold; + } + if (extensionButtons == 0xFFFF) { + extensionButtons = kpad->status.ex_status.cl.hold; + } + buttons = (buttons & 0x9FFF & ~0x6000) | (coreButtons & 0x6000); + previousButtons = kpad->status.hold; + changed = previousButtons ^ buttons; + kpad->status.hold = buttons; + kpad->status.trig = changed & buttons; + kpad->status.release = changed & previousButtons; + if (device == 2) { + previousButtons = kpad->status.ex_status.cl.hold; + kpad->status.ex_status.cl.hold = extensionButtons; + changed = previousButtons ^ extensionButtons; + kpad->status.ex_status.cl.trig = changed & extensionButtons; + kpad->status.ex_status.cl.release = changed & previousButtons; + } + calc_button_repeat(kpad, device, ringCount); + remainingSamples = available; + outputIndex = available - 1; + output = &statuses[outputIndex]; + while (remainingSamples != 0) { + output--; + sample = remainingSamples > 1 ? (KPADSample*)output : &latestSample; + kpad->status.wpad_err = sample->error; + if (kpad->status.dev_type != sample->device && (u8)(sample->error + 2) <= 2) { + kpad->status.dev_type = sample->device; + kpad->flag51E = 1; + } + kpad->status.data_format = sample->dataFormat; + switch (sample->error) { + case 0: + read_kpad_stick(kpad, sample); + case -7: + read_kpad_acc(kpad, sample); + read_kpad_dpd(kpad, sample); + break; + default: + kpad->status.dpd_valid_fg = 0; + } + statuses[outputIndex] = kpad->status; + outputIndex--; + remainingSamples--; + } + } + kpad->samplingInProgress = 0; + return available; +} + +void KPADInit(void) { + KPADInside* kpad; + f32 distanceValue; + f32 sensorDistance; + f32 rotationElement; + f32 referenceWidth; + f32 referenceHeight; + f32 objectInterval; + f32 zero; + f32 degreesToRadians; + f32 one; + f32 adjustedWidth; + f32 adjustedHeight; + f32* matrix; + u32 i; + s32 chan; + BOOL enabled; + WPADInit(); + memset(inside_kpads, 0, 0x14A0); + matrix = initial_rotation_matrix; + chan = 0; + one = 1.0f; + zero = 0.0f; + degreesToRadians = 0.017453292f; + kp_err_dist_max = one + (f32)WPADGetDpdSensitivity(); + kpad = inside_kpads; + do { + kpad->dpdEnable = 1; + referenceWidth = 1.0f; + kpad->dpdFormat = 0; + referenceHeight = 0.75f; + kpad->status.dev_type = 0xFD; + kpad->status.data_format = 0; + kpad->referenceDistance = idist_org; + *(u32*)&kpad->accelNormalX = ((u32*)&iaccXY_nrm_hori)[0]; + *(u32*)&kpad->accelNormalY = ((u32*)&iaccXY_nrm_hori)[1]; + *(u32*)&kpad->horizonTangentX = ((u32*)&isec_nrm_hori)[0]; + *(u32*)&kpad->horizonTangentY = ((u32*)&isec_nrm_hori)[1]; + *(u32*)&kpad->sensorBarCenterX = ((u32*)&icenter_org)[0]; + *(u32*)&kpad->sensorBarCenterY = ((u32*)&icenter_org)[1]; + distanceValue = kpad->sensorBarCenterX; + sensorDistance = (f32)sqrt(referenceWidth * referenceWidth + referenceHeight * referenceHeight); + if (distanceValue < zero) { + adjustedWidth = referenceWidth + distanceValue; + } else { + adjustedWidth = referenceWidth - distanceValue; + } + distanceValue = kpad->sensorBarCenterY; + if (distanceValue < zero) { + adjustedHeight = referenceHeight + distanceValue; + } else { + adjustedHeight = referenceHeight - distanceValue; + } + if (adjustedWidth < adjustedHeight) { + } else { + adjustedWidth = adjustedHeight; + } + matrix[0] = one; + matrix[1] = zero; + matrix[2] = zero; + matrix[3] = zero; + kpad->sensorBarScale = sensorDistance / adjustedWidth; + kpad->value9C = zero; + kpad->value94 = zero; + kpad->value8C = zero; + kpad->posParamX = zero; + kpad->valueA0 = one; + kpad->value98 = one; + kpad->value90 = one; + kpad->posParamY = one; + kpad->repeatDelay = 40000; + kpad->repeatInterval = 0; + kpad->repeatCount = 0; + kpad->repeatCount2 = 40000; + kpad->repeatCurrent = 0; + kpad->repeatCurrent2 = 40000; + kpad->sensorHeightPending = 1; + kpad->sensorBarPosition = 1; + kpad->freeStyleAccelRotation = 0; + matrix[4] = zero; + rotationElement = (f32)cos(degreesToRadians * sensor_bar_angle_degrees); + matrix[5] = rotationElement; + matrix[7] = zero; + matrix[8] = zero; + rotationElement = (f32)-sin(degreesToRadians * sensor_bar_angle_degrees); + matrix[6] = rotationElement; + rotationElement = (f32)sin(degreesToRadians * sensor_bar_angle_degrees); + matrix[9] = rotationElement; + rotationElement = (f32)cos(degreesToRadians * sensor_bar_angle_degrees); + matrix[10] = rotationElement; + matrix[11] = zero; + { + KPADSample* record = kpad->ringData; + i = 0; + do { + i++; + record->error = -1; + record++; + } while (i < 16); + } + chan++; + kpad++; + } while (chan < 4); + objectInterval = kp_obj_interval; + enabled = OSDisableInterrupts(); + kp_obj_interval = objectInterval; + distanceValue = objectInterval / 0.383864f; + kp_err_dist_min = distanceValue; + kp_dist_vv1 = distanceValue; + OSRestoreInterrupts(enabled); + chan = 3; + kpad = &inside_kpads[3]; + do { + if (WPADGetStatus() == 3) { + WPADControlMotor(chan, 0); + } + chan--; + kpad->flag51D = 1; + kpad--; + } while (chan >= 0); + OSRegisterVersion(__KPADVersion); +} + +static void KPADiSamplingCallback(s32 chan) { + KPADInside* kpad = &inside_kpads[chan]; + u32 device; + if (WPADProbe(chan, &device) != -1) { + u8 index = kpad->ringIndex; + KPADSample* status; + u32 tier; + u32 enabled; + u32 tableIndex; + if (index >= 16) { + index = 0; + } + status = &kpad->ringData[index]; + WPADRead(chan, (WPADStatus*)status); + status->dataFormat = WPADGetDataFormat(chan); + kpad->ringIndex = index + 1; + if (kpad->ringCount < 16) { + kpad->ringCount++; + } + if (kpad->sensorHeightPending != 0) { + f32 sensor = 0.0f; + f32 angle; + f32 height = 0.75f; + f32 distance; + f32 x = 1.0f; + f32 y; + if (kpad->sensorBarPosition != 0) { + if (WPADGetSensorBarPosition() == 1) { + sensor = 0.2f; + } else { + sensor = -0.2f; + } + } + y = -sensor; + kpad->sensorBarCenterX = 0.0f; + kpad->sensorBarCenterY = y; + angle = (f32)sqrt(x * x + height * height); + if (kpad->sensorBarCenterX < 0.0f) { + x += kpad->sensorBarCenterX; + } else { + x -= kpad->sensorBarCenterX; + } + if (kpad->sensorBarCenterY < 0.0f) { + height += kpad->sensorBarCenterY; + } else { + height -= kpad->sensorBarCenterY; + } + if (x >= height) { + x = height; + } + distance = angle / x; + kpad->sensorBarScale = distance; + kpad->sensorHeightPending = 0; + } + switch (device) { + case 0xFF: + tier = 0; + break; + case 1: + tier = 2; + break; + case 2: + tier = 4; + break; + default: + tier = 0; + break; + } + if (kpad->dpdEnable != 0) { + tier++; + } + enabled = WPADIsDpdEnabled(chan) != 0 ? kpad->dpdFormat : 0; + tableIndex = tier * 2; + if (enabled != dpdModeTable[tableIndex]) { + if (kpad->dpdCallback != 0 && kpad->dpdCallbackFired == 0) { + kpad->dpdCallbackFired = 1; + kpad->dpdCallback(chan, 0); + kpad->dpdCallbackPending = 0; + } + if (kpad->flag51F == 0) { + kpad->flag51F = 1; + if (WPADControlDpd(chan, dpdModeTable[tableIndex], KPADiControlDpdCallback) == 0) { + kpad->dpdFormat = dpdModeTable[tableIndex]; + } + } + } else if (status->dataFormat != dpdModeTable[tableIndex + 1]) { + WPADSetDataFormat(chan, dpdModeTable[tableIndex + 1]); + } + } + if (kpad->samplingCallback != 0) { + kpad->samplingCallback(chan); + } +} + +static void KPADiControlDpdCallback(s32 chan, s32 result) { + KPADInside* kpad = &inside_kpads[chan]; + if (result == 0 && kpad->dpdCallback != 0 && kpad->dpdCallbackPending == 0) { + kpad->dpdCallbackPending = 1; + kpad->dpdCallback(chan, 1); + kpad->dpdCallbackFired = 0; + } + kpad->flag51F = 0; +} -- 2.49.1 From b28a931741e404e08e47b6d7a4b28ad99d90473b Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 00:07:51 +0000 Subject: [PATCH 06/34] decompile wad stream helpers --- libs/RVL_SDK/src/wad/wad.c | 1005 +++++++++++++++++++++++++++++++++--- 1 file changed, 930 insertions(+), 75 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 0fe171df..ed966021 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -1,42 +1,153 @@ #include +#include #include +#include +#include +#include +#include +#include #include +typedef union WADStreamHandle { + DVDFileInfo dvd; + NANDFileInfo nand; + CNTFileInfoDVD contentDvd; + CNTFileInfoNAND contentNand; + FAFILE* fa; + void* memoryBase; + u8 storage[0xB8]; +} WADStreamHandle; + typedef struct __attribute__((aligned(32))) WADStream { - u32 words[0x30]; + WADLocation location; + WADStreamHandle handle; } WADStream; -typedef union WADVersionBuffer { +typedef union WADTitleMetadata { + ESTitleMetaHeader tmd; + ESTitleId backupTitleId; + u8 bytes[0x80]; +} WADTitleMetadata; + +typedef struct __attribute__((aligned(32))) WADVersionWorkspace { WADHeader header; - struct { - u32 headerSize; - union { - ESTmdViewHeader tmd; - ESTitleId backupTitleId; - u8 bytes[0x80]; - } version; - } data; - u8 bytes[0x84]; -} WADVersionBuffer; + WADTitleMetadata metadata; + WADStream stream; +} WADVersionWorkspace; + +typedef struct WADUnpackInfo { + s32 type; + s32 headerInfo; + u16 cidxMode; + u8 reserved_0x0a[0x0A]; + void* buffer_0x14; + u32 size_0x18; + u8 reserved_0x1c[8]; + void* buffer_0x24; + u32 size_0x28; + u8 reserved_0x2c[8]; + void* buffer_0x34; + u32 size_0x38; + u32 sectionSize; + u32 sectionOffset; + ESTitleMeta* titleMeta; + u32 titleMetaSize; + u32 contentOffset; + u8 reserved_0x54[8]; + void* fileList; + u32 fileOffset; + u32 fileCount; + void* contentIndex; + void* fileNames; +} WADUnpackInfo; + +typedef struct WADFileHeader { + u32 headerSize; + u32 fileSize; + u8 flags[3]; + char name[0x75]; +} WADFileHeader; + +typedef struct WADFileEntry { + u32 fileSize; + u8 flags[4]; + char name[0x40]; +} WADFileEntry; + +#pragma pack(push, 4) +typedef struct WADFileMetadataView { + u8 reserved_0x00[0x18C]; + ESTitleId titleId; + u8 reserved_0x194[0x48]; + u16 titleVersion; +} WADFileMetadataView; +#pragma pack(pop) + +typedef struct __attribute__((aligned(32))) WADImportWorkspace { + u8 transferId[0x20]; + WADFileHeader fileHeader; + u8 wadHeader[0x80]; + WADUnpackInfo unpackInfo; + WADStream stream; +} WADImportWorkspace; + +typedef struct WADImportTransfer { + s32 error; + u32 chunkSize; + BOOL ready[2]; + void* buffers[2]; + OSMutex mutex[2]; + OSCond signalCond[2]; + OSCond waitCond[2]; +} WADImportTransfer; + +typedef struct WADImportLoopArgs { + s32 fd; + u32 size; + WADImportTransfer* transfer; +} WADImportLoopArgs; + +#define WAD_STREAM_ALIGNMENT 0x40 +#define WAD_READ_ALIGNMENT 0x20 static s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32, u32); -static s32 WADReadStream(WADStream* stream, void* buffer, u32 size, u32 offset); -static s32 WADCloseStream(WADStream* stream); -static s32 WAD_815C2F44(u32 headerSize, u32* headerInfo); -static s32 _WADGetTitleVer(WADHeader* header, u32, u32, u32 type, u32 headerInfo); +static s32 WADReadStream(WADStream* stream, void** buffer, u32 size, u32 offset); +static void WADCloseStream(WADStream* stream); +static s32 WADWriteStream(WADStream* stream, void* buffer, u32 size); +static s32 WADSeekStream(WADStream* stream, u32 offset, s32 origin); +static s32 WAD_815C2F44(WADHeader* header, s32* headerInfo); +static s32 _WADGetTitleVer(WADHeader* header, ESTitleId* titleId, u16* titleVersion, s32 type, + s32 headerInfo); +static s32 _WADUnpackIRD(WADHeader* header, WADStream* stream, WADUnpackInfo* info, + MEMAllocator* allocator, u32 offset, u32 flags, u32 mode); +static s32 _WADUnpackBroadOn(WADHeader* header, WADStream* stream, WADUnpackInfo* info, + MEMAllocator* allocator, u32 offset, u32 flags, u32 mode); +static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* info, + MEMAllocator* allocator, u32 offset, u32 flags); +static s32 _WADUnpack(void* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, + u32 offset, u32 flags, u32 mode); +s32 _WADGetCidxCount(const ESContentMask* contentMask); +static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber); +static s32 _WADGetTransferId(void* transferId); +static void* _WADMemAlloc(MEMAllocator* allocator, u32 size); +static void _WADMemFree(MEMAllocator* allocator, void* buffer); +static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, const void* fileNames, + const void* transferIdBuffer); +static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator); +static s32 WAD_815BFFA8(WADImportLoopArgs* args); +u32 _WADIsTerminated(const char* text, u32 maxLength); extern s32 ES_GetBoot2Version(u32* version); s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADLocation location, u32 offset) { - WADStream stream ALIGN32; - WADVersionBuffer versionBuffer; - ESTitleId* backupTitleId; - u32 headerInfo; - u32 titleDataSize; + WADVersionWorkspace workspace ALIGN32; + s32 headerInfo[2]; + WADTitleMetadata* metadataBuffer = &workspace.metadata; + WADHeader* headerBuffer = &workspace.header; s32 result; u32 titleDataOffset; BOOL streamOpened; - u32 type; + s32 type; streamOpened = FALSE; titleDataOffset = 0; @@ -45,66 +156,67 @@ s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADL } else if ((offset & 0x3F) != 0) { result = -3007; } else { - result = WADOpenStream(location, path, &stream, 0, 0); + result = WADOpenStream(location, path, &workspace.stream, 0, 0); streamOpened = TRUE; if (result == 0) { - result = WADReadStream(&stream, &versionBuffer.header, sizeof(WADHeader), offset); - if (result == sizeof(WADHeader)) { - type = WAD_815C2F44(versionBuffer.header.hdrSize, &versionBuffer.header.certSize); - if (type == 0) { - result = -3001; - } else if (((type == 2) && (versionBuffer.header.tmdSize == 0)) || - ((type == 1) && (versionBuffer.header.ticketSize == 0))) { - result = -3002; - if (versionBuffer.header.certSize == 2) { - titleDataOffset = 0x60; - } else { - goto done; - } - } - - if (titleDataOffset == 0) { - result = _WADGetTitleVer(&versionBuffer.header, 0, 0, type, - versionBuffer.header.certSize); - if (result == 0) { - result = -3002; - } else { - titleDataOffset = result + 0x180; - result = WADReadStream(&stream, &versionBuffer.data.version.tmd, - 0x80, offset + titleDataOffset); - if (result == 0x80) { - result = 0; - if (titleId != 0) { - titleId[0] = versionBuffer.data.version.tmd.titleId; - } - if (titleVersion != 0) { - *titleVersion = versionBuffer.data.version.tmd.titleVersion; - } - } else { - result = -3005; - } - } + result = WADReadStream(&workspace.stream, (void**)&headerBuffer, 0x20, offset); + if (result != 0x20) { + result = -3005; + goto done; + } + type = WAD_815C2F44(headerBuffer, headerInfo); + if (type == 0) { + result = -3001; + goto done; + } + if (((type == 2) && (headerBuffer->tmdSize == 0)) || + ((type == 1) && (headerBuffer->ticketSize == 0))) { + result = -3002; + if (headerInfo[0] == 2) { + titleDataOffset = 0x60; } else { - result = WADReadStream(&stream, &versionBuffer.data.version.backupTitleId, - 0x20, offset + titleDataOffset); - if (result == 0x20) { - result = 0; - if (titleId != 0) { - *titleId = versionBuffer.data.version.backupTitleId; - } - } else { - result = -3005; - } + goto done; + } + } + + if (titleDataOffset == 0) { + result = _WADGetTitleVer(headerBuffer, 0, 0, type, headerInfo[0]); + if (result == 0) { + result = -3002; + goto done; + } + titleDataOffset = result + 0x180; + result = WADReadStream(&workspace.stream, (void**)&metadataBuffer, 0x80, + offset + titleDataOffset); + if ((u32)result != 0x80) { + result = -3005; + goto done; + } + result = 0; + if (titleId != 0) { + titleId[0] = metadataBuffer->tmd.titleId; + } + if (titleVersion != 0) { + *titleVersion = metadataBuffer->tmd.titleVersion; } } else { - result = -3005; + result = WADReadStream(&workspace.stream, (void**)&metadataBuffer, 0x20, + offset + titleDataOffset); + if (result != 0x20) { + result = -3005; + goto done; + } + result = 0; + if (titleId != 0) { + *titleId = metadataBuffer->backupTitleId; + } } } } done: if (streamOpened) { - WADCloseStream(&stream); + WADCloseStream(&workspace.stream); } return result; } @@ -134,15 +246,16 @@ s32 WADCheckImport(ESTitleId titleId, u32 titleVersion) { } s32 WADGetInstalledVersion(ESTitleId titleId, u16* version) { - u32 installedVersion; u32 tmdSize; + u32 installedVersion; ESTmdView tmdView __attribute__((aligned(32))); + ESTmdView* viewBuffer = &tmdView; s32 result; if (version == 0) { return -3000; } - if (titleId == 1) { + if (titleId == 0x0000000100000001ULL) { result = ES_GetBoot2Version(&installedVersion); if (result == 0) { *version = installedVersion; @@ -160,9 +273,751 @@ s32 WADGetInstalledVersion(ESTitleId titleId, u16* version) { if (tmdSize > sizeof(ESTmdView)) { return -3000; } - result = ES_GetTmdView(titleId, &tmdView, &tmdSize); + result = ES_GetTmdView(titleId, viewBuffer, &tmdSize); if (result == 0) { - *version = tmdView.head.titleVersion; + *version = viewBuffer->head.titleVersion; } return result; } + +s32 WADImportGetBlocks(char* path, MEMAllocator* allocator, WADLocation location, u32 offset, u32 flags, + WADBlocks* blocks, u32* fileListOut) { + WADImportWorkspace workspace ALIGN32; + void* wadHeader = workspace.wadHeader; + s32 result; + u32 fileOffset; + void* allocatedFiles; + BOOL streamOpened; + u32 transferId; + u32 contentCount; + ESTitleMeta* titleMeta; + ESContentMeta* contentMeta; + u32 index; + + streamOpened = FALSE; + fileOffset = 0; + allocatedFiles = 0; + memset(&workspace.unpackInfo, 0, sizeof(WADUnpackInfo)); + if (fileListOut != 0) { + *fileListOut = 0; + } + if ((allocator == 0) || (allocator->heap == 0) || (path == 0) || (blocks == 0)) { + result = -3000; + } else if ((offset & 0x3F) != 0) { + result = -3007; + } else { + result = WADOpenStream(location, path, &workspace.stream, 0, 0); + streamOpened = TRUE; + if (result == 0) { + result = WADReadStream(&workspace.stream, &wadHeader, 0x80, offset); + if (result != 0x80) { + result = -3005; + } else { + result = _WADUnpack(wadHeader, &workspace.stream, &workspace.unpackInfo, allocator, + offset, flags & ~4, 1); + if (result == 0) { + memset(blocks, 0, sizeof(WADBlocks)); + titleMeta = workspace.unpackInfo.titleMeta; + if (titleMeta != 0) { + if ((flags & 4) == 0) { + blocks->unkBlocks += (workspace.unpackInfo.sectionSize + 0x3FFF) >> 14; + blocks->unkInodes = 1; + } + if (workspace.unpackInfo.cidxMode < 1) { + contentCount = titleMeta->head.numContents; + } else { + contentCount = _WADGetCidxCount(workspace.unpackInfo.contentIndex); + if (contentCount > titleMeta->head.numContents) { + result = -3001; + goto cleanup; + } + } + contentMeta = titleMeta->contents; + for (index = 0; index < contentCount; index++) { + if (workspace.unpackInfo.cidxMode >= 1) { + s32 selectedIndex = _WADGetCidx(workspace.unpackInfo.contentIndex, index); + if ((selectedIndex < 0) || + (titleMeta->head.numContents <= selectedIndex)) { + result = -3001; + goto cleanup; + } + contentMeta = titleMeta->contents + selectedIndex; + result = 0; + } + if ((contentMeta->type & 0x8000) == 0) { + blocks->privateInodes++; + blocks->privateBlocks += ((u32)contentMeta->size + 0x3FFF) >> 14; + } else { + blocks->sharedInodes++; + blocks->sharedBlocks += ((u32)contentMeta->size + 0x3FFF) >> 14; + } + contentMeta++; + } + } + if ((workspace.unpackInfo.fileList != 0) && + (workspace.unpackInfo.fileCount != 0)) { + transferId = _WADGetTransferId(workspace.transferId); + if ((fileListOut == 0) || + ((allocatedFiles = _WADMemAlloc(allocator, + workspace.unpackInfo.fileCount * 0x48)) != 0)) { + fileOffset = workspace.unpackInfo.fileOffset; + for (index = 0; index < workspace.unpackInfo.fileCount; index++) { + WADFileHeader* fileHeader = &workspace.fileHeader; + result = WADReadStream(&workspace.stream, (void**)&fileHeader, 0x80, + offset + fileOffset); + if (result != 0x80) { + result = -3005; + goto cleanup; + } + result = 0; + if (_WADCanImportFile(fileHeader, transferId, + workspace.unpackInfo.fileNames, + workspace.transferId) != 0) { + blocks->fileBlocks += (fileHeader->fileSize + 0x3FFF) >> 14; + if (fileListOut != 0) { + WADFileEntry* fileEntries = allocatedFiles; + fileEntries += blocks->fileInodes; + fileEntries->fileSize = fileHeader->fileSize; + fileEntries->flags[0] = fileHeader->flags[0]; + fileEntries->flags[1] = fileHeader->flags[1]; + fileEntries->flags[2] = fileHeader->flags[2]; + fileEntries->flags[3] = 0; + strncpy(fileEntries->name, fileHeader->name, 0x40); + } + blocks->fileInodes++; + } + fileOffset = ((fileOffset + WAD_STREAM_ALIGNMENT - 1) & + ~(WAD_STREAM_ALIGNMENT - 1)) + fileHeader->fileSize; + fileOffset = (fileOffset + WAD_STREAM_ALIGNMENT - 1) & + ~(WAD_STREAM_ALIGNMENT - 1); + } + if (fileListOut != 0) { + *fileListOut = (u32)allocatedFiles; + } + } else { + result = -3003; + } + } + } + } + } + } + +cleanup: + _WADFreeMemory(&workspace.unpackInfo, allocator); + if ((fileListOut != 0) && (result != 0)) { + if (allocatedFiles != 0) { + _WADMemFree(allocator, allocatedFiles); + } + *fileListOut = 0; + } + if (streamOpened) { + WADCloseStream(&workspace.stream); + } + return result; +} + +static s32 WAD_815BFFA8(WADImportLoopArgs* args) { + WADImportTransfer* transfer = args->transfer; + u32 remaining = args->size; + s32 result = 0; + u32 bufferIndex = 0; + + while ((remaining != 0) && (result == 0)) { + u32 size = remaining; + OSMutex* mutex; + + if (transfer->chunkSize < remaining) { + size = transfer->chunkSize; + } + mutex = &transfer->mutex[bufferIndex]; + OSLockMutex(mutex); + while (transfer->ready[bufferIndex] == 0) { + OSWaitCond(&transfer->waitCond[bufferIndex], mutex); + } + result = ES_ImportContentData(args->fd, transfer->buffers[bufferIndex], size); + transfer->ready[bufferIndex] = 0; + if (result != 0) { + transfer->error = 1; + } + OSUnlockMutex(mutex); + OSSignalCond(&transfer->signalCond[bufferIndex]); + remaining -= size; + bufferIndex ^= 1; + } + return result; +} + +s32 _WADGetCidxCount(const ESContentMask* contentMask) { + s32 bitIndex = 0; + s32 count = 0; + s32 groupIndex; + s32 bitMask = 1; + + for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { + if ((contentMask->data[bitIndex >> 3] & (bitMask << (bitIndex & 7))) != 0) { + count++; + } + bitIndex++; + if ((contentMask->data[bitIndex >> 3] & (bitMask << (bitIndex & 7))) != 0) { + count++; + } + bitIndex++; + if ((contentMask->data[bitIndex >> 3] & (bitMask << (bitIndex & 7))) != 0) { + count++; + } + bitIndex++; + if ((contentMask->data[bitIndex >> 3] & (bitMask << (bitIndex & 7))) != 0) { + count++; + } + bitIndex++; + } + return count; +} + +static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber) { + u32 bitIndex = 0; + u32 groupIndex; + + contentNumber++; + for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + contentNumber--; + } + if (contentNumber == 0) { + return bitIndex; + } + bitIndex++; + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + contentNumber--; + } + if (contentNumber == 0) { + return bitIndex; + } + bitIndex++; + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + contentNumber--; + } + if (contentNumber == 0) { + return bitIndex; + } + bitIndex++; + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + contentNumber--; + } + if (contentNumber == 0) { + return bitIndex; + } + bitIndex++; + } + return -1; +} + +static s32 _WADGetTransferId(void* transferId) { + NANDFileInfo fileInfo; + BOOL valid; + s32 result; + + valid = FALSE; + result = NANDPrivateOpen("/shared2/succession/transfer.id", &fileInfo, 1); + if (result == 0) { + result = NANDRead(&fileInfo, transferId, 0x20); + valid = result == 0x20; + NANDClose(&fileInfo); + } + return valid; +} + +static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, const void* fileNames, + const void* transferIdBuffer) { + s32 result; + + result = strncmp(fileHeader->name, "banner.bin", 6); + if (result == 0) { + return FALSE; + } + result = strncmp(fileHeader->name, "zeldaTp.dat", 10); + if (result == 0) { + if ((transferId == 0) || (fileNames == 0) || (transferIdBuffer == 0)) { + return FALSE; + } + result = memcmp(fileNames, transferIdBuffer, 6); + return result == 0; + } + return TRUE; +} + +static void* _WADMemAlloc(MEMAllocator* allocator, u32 size) { + u32 heapType; + void* buffer; + + if ((allocator == 0) || (allocator->heap == 0)) { + return 0; + } + heapType = ((MEMiHeapHead*)allocator->heap)->magic; + if (heapType == 0x46524D48) { + return MEMAllocFromFrmHeapEx(allocator->heap, size, 0x40); + } + if (heapType == 0x45585048) { + return MEMAllocFromExpHeapEx(allocator->heap, size, 0x40); + } + buffer = MEMAllocFromAllocator(allocator, size); + if (((u32)buffer & 0x3F) != 0) { + OSReport("%s: Memory Allocator must return 64B aligned memBlocks\n", "_WADMemAlloc"); + MEMFreeToAllocator(allocator, buffer); + return 0; + } + return buffer; +} + +static void _WADMemFree(MEMAllocator* allocator, void* buffer) { + if ((allocator != 0) && (allocator->heap != 0) && (buffer != 0)) { + MEMFreeToAllocator(allocator, buffer); + } +} + +static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator) { + if ((info->size_0x18 != 0) && (info->buffer_0x14 != 0)) { + _WADMemFree(allocator, info->buffer_0x14); + info->buffer_0x14 = 0; + } + if ((info->size_0x28 != 0) && (info->buffer_0x24 != 0)) { + _WADMemFree(allocator, info->buffer_0x24); + info->buffer_0x24 = 0; + } + if ((info->size_0x38 != 0) && (info->buffer_0x34 != 0)) { + _WADMemFree(allocator, info->buffer_0x34); + info->buffer_0x34 = 0; + } + if ((info->titleMetaSize != 0) && (info->titleMeta != 0)) { + _WADMemFree(allocator, info->titleMeta); + info->titleMeta = 0; + } +} + +typedef struct WADContentPath { + const char* name; + void* handle; +} WADContentPath; + +typedef union WADFileInfoView { + FAFileInfo info; + struct { + u32 unknown; + u32 fileSize; + u8 reserved[0x18]; + } fields; +} WADFileInfoView; + +static s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32 write, + u32 offset) { + s32 result; + + stream->location = location; + if (location == WAD_LOCATION_SD_CARD) { + if (write == 0) { + stream->handle.fa = FAFopen(path, "r"); + } else { + stream->handle.fa = FAFopen(path, "r+"); + if (stream->handle.fa == 0) { + stream->handle.fa = FACreate(path, 0); + } + if (stream->handle.fa == 0) { + return -3004; + } + if ((offset != 0) && (WADSeekStream(stream, offset, 0) != offset)) { + return -3004; + } + } + return stream->handle.fa == 0 ? -3004 : 0; + } + if (location < WAD_LOCATION_SD_CARD) { + if (location == WAD_LOCATION_DVD) { + if (write != 0) { + return -3004; + } + return DVDOpen(path, &stream->handle.dvd) ? 0 : -3004; + } + if (location == WAD_LOCATION_NAND) { + if (write == 0) { + return NANDPrivateOpen(path, &stream->handle.nand, 1) == 0 ? 0 : -3004; + } + result = NANDPrivateOpen(path, &stream->handle.nand, 3); + if (result == -12) { + result = NANDPrivateCreate(path, 0x3F, 0); + if (result != 0) { + return -3004; + } + result = NANDPrivateOpen(path, &stream->handle.nand, 3); + } + if (result != 0) { + return -3004; + } + result = NANDSeek(&stream->handle.nand, offset, 0); + if (result == -8) { + u8 zeroes[0x4000] ALIGN32; + u32 fileSize; + + fileSize = NANDSeek(&stream->handle.nand, 0, 2); + if (offset <= fileSize) { + return -3004; + } + memset(zeroes, 0, sizeof(zeroes)); + fileSize = offset - fileSize; + while (fileSize != 0) { + u32 writeSize = fileSize; + if (writeSize > sizeof(zeroes)) { + writeSize = sizeof(zeroes); + } + if (NANDWrite(&stream->handle.nand, zeroes, writeSize) != writeSize) { + return -3004; + } + fileSize -= writeSize; + } + result = NANDSeek(&stream->handle.nand, offset, 0); + } + return result == offset ? 0 : -3004; + } + if ((s32)location >= 0) { + if ((((u32)path + offset) & 0x3F) != 0) { + return -3007; + } + stream->handle.memoryBase = (void*)((u32)path + offset); + return 0; + } + } else if (location == WAD_LOCATION_CNT_NAND) { + WADContentPath* contentPath = (WADContentPath*)path; + if (write != 0) { + return -3004; + } + return contentOpenNAND(contentPath->handle, contentPath->name, + &stream->handle.contentNand) == 0 + ? 0 + : -3004; + } else if (location < WAD_LOCATION_CNT_NAND) { + WADContentPath* contentPath = (WADContentPath*)path; + if (write != 0) { + return -3004; + } + return contentOpenDVD(contentPath->handle, contentPath->name, + &stream->handle.contentDvd) == 0 + ? 0 + : -3004; + } + return -3000; +} + +static s32 WADReadStream(WADStream* stream, void** buffer, u32 size, u32 offset) { + s32 result; + u32 alignedSize; + + if ((s32)size <= 0) { + result = 0; + } else if ((stream == 0) || (buffer == 0)) { + result = -3000; + } else { + alignedSize = (size + WAD_READ_ALIGNMENT - 1) & ~(WAD_READ_ALIGNMENT - 1); + if (size == alignedSize) { + if (stream->location == WAD_LOCATION_SD_CARD) { + if (stream->handle.fa == 0) { + result = -3000; + } else { + result = FAFseek(stream->handle.fa, offset, 0); + if (result == 0) { + result = FAFread(*buffer, 1, size, stream->handle.fa); + } + } + } else if (stream->location < WAD_LOCATION_SD_CARD) { + if (stream->location == WAD_LOCATION_DVD) { + return DVDReadPrio(&stream->handle.dvd, *buffer, alignedSize, offset, 2); + } + if (stream->location > WAD_LOCATION_DVD) { + result = NANDSeek(&stream->handle.nand, offset, 0); + if (result < 0) { + return result; + } + return NANDRead(&stream->handle.nand, *buffer, alignedSize); + } + if (stream->location >= 0) { + memcpy(*buffer, (u8*)stream->handle.memoryBase + offset, size); + return size; + } + result = -3000; + } else if (stream->location == WAD_LOCATION_CNT_NAND) { + return contentReadNAND(&stream->handle.contentNand, *buffer, alignedSize, offset); + } else if (stream->location < WAD_LOCATION_CNT_NAND) { + return contentReadDVD(&stream->handle.contentDvd, *buffer, alignedSize, offset); + } else { + result = -3000; + } + } else { + result = -3000; + } + } + return result; +} + +static void WADCloseStream(WADStream* stream) { + if (stream == 0) { + return; + } + if (stream->location == WAD_LOCATION_SD_CARD) { + if (stream->handle.fa == 0) { + return; + } + FAFclose(stream->handle.fa); + return; + } + if (stream->location < WAD_LOCATION_SD_CARD) { + if (stream->location == WAD_LOCATION_DVD) { + DVDClose(&stream->handle.dvd); + return; + } + if (stream->location < WAD_LOCATION_DVD) { + return; + } + if (stream->location > WAD_LOCATION_DVD) { + NANDClose(&stream->handle.nand); + } + return; + } + if (stream->location == WAD_LOCATION_CNT_NAND) { + contentCloseNAND(&stream->handle.contentNand); + return; + } + if (stream->location >= WAD_LOCATION_CNT_NAND) { + return; + } + contentCloseDVD(&stream->handle.contentDvd); +} + +static s32 WADWriteStream(WADStream* stream, void* buffer, u32 size) { + if ((stream == 0) || (buffer == 0)) { + return -3000; + } + if (stream->location == WAD_LOCATION_NAND) { + return NANDWrite(&stream->handle.nand, buffer, size); + } + if (stream->location == WAD_LOCATION_DVD - 1) { + memcpy(stream->handle.memoryBase, buffer, size); + return size; + } + if ((stream->location > WAD_LOCATION_NAND) && + (stream->location < WAD_LOCATION_CNT_DVD)) { + if (stream->handle.fa == 0) { + return -3000; + } + return FAFwrite(buffer, 1, size, stream->handle.fa); + } + return -3000; +} + +static s32 WADSeekStream(WADStream* stream, u32 offset, s32 origin) { + s32 result; + + if ((stream == 0) || (origin < 0) || (origin > 2)) { + return -3000; + } + if (stream->location == WAD_LOCATION_SD_CARD) { + WADFileInfoView info; + if (stream->handle.fa == 0) { + return -3000; + } + result = FAFseek(stream->handle.fa, offset, origin); + if ((result == 0) && (FAFinfo(stream->handle.fa, &info.info) == 0)) { + result = info.fields.fileSize; + } + return result; + } + if (stream->location < WAD_LOCATION_SD_CARD) { + if (stream->location > WAD_LOCATION_DVD) { + return NANDSeek(&stream->handle.nand, offset, origin); + } + } else if (stream->location == WAD_LOCATION_CNT_NAND) { + return contentSeekNAND(&stream->handle.contentNand, offset, origin); + } else if (stream->location < WAD_LOCATION_CNT_NAND) { + return contentSeekDVD(&stream->handle.contentDvd, offset, origin); + } + return -3000; +} + +static s32 _WADGetTitleVer(WADHeader* header, ESTitleId* titleId, u16* titleVersion, s32 type, + s32 headerInfo) { + s32 titleDataOffset = 0; + + if (type == 2) { + if (header->tmdSize == 0) { + return 0; + } + if ((headerInfo == 0) || (headerInfo == 1) || (headerInfo == 3)) { + titleDataOffset = header->wadVersion == 1 ? 0x80 : 0x40; + titleDataOffset += (header->certSize + 0x3F) & ~0x3F; + titleDataOffset += (header->crlSize + 0x3F) & ~0x3F; + titleDataOffset += (header->ticketSize + 0x3F) & ~0x3F; + } else if (headerInfo == 2) { + titleDataOffset = 0x80; + } + } else if (type == 1) { + if (header->ticketSize == 0) { + return 0; + } + titleDataOffset = header->certSize + 0x20; + titleDataOffset += header->crlSize; + } + + { + WADFileMetadataView* metadata = (WADFileMetadataView*)((u8*)header + titleDataOffset); + if (titleId != 0) { + *titleId = metadata->titleId; + } + if (titleVersion != 0) { + *titleVersion = metadata->titleVersion; + } + } + return titleDataOffset; +} + +static s32 WAD_815C2F44(WADHeader* header, s32* headerInfo) { + u8 firstTypeByte; + u8 secondTypeByte; + + if (header == 0) { + return 0; + } + firstTypeByte = header->wadType[0]; + secondTypeByte = header->wadType[1]; + if ((firstTypeByte == 'I') && (secondTypeByte == 's')) { + if (headerInfo != 0) { + *headerInfo = 0; + } + return 2; + } + if ((firstTypeByte == 'X') && (secondTypeByte == 'p')) { + if (headerInfo != 0) { + *headerInfo = 1; + } + return 2; + } + if ((firstTypeByte == 'B') && (secondTypeByte == 'k')) { + if (headerInfo != 0) { + *headerInfo = 2; + } + return 2; + } + if ((firstTypeByte == 'i') && (secondTypeByte == 'b')) { + if (headerInfo != 0) { + *headerInfo = 3; + } + return 2; + } + if (header->hdrSize == 0x20) { + if (headerInfo != 0) { + *headerInfo = 0; + } + return 1; + } + return 0; +} + +static s32 _WADUnpack(void* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, + u32 offset, u32 flags, u32 mode) { + s32 type; + + if ((header == 0) || (info == 0)) { + return -3000; + } + memset(info, 0, 0x70); + type = WAD_815C2F44(header, &info->headerInfo); + info->type = type; + if (type == 2) { + if (info->headerInfo == 2) { + return _WADUnpackBackup(header, stream, info, allocator, offset, flags); + } + return _WADUnpackIRD(header, stream, info, allocator, offset, flags, mode); + } + if (type == 1) { + return _WADUnpackBroadOn(header, stream, info, allocator, offset, flags, mode); + } + return -3001; +} + +typedef struct WADSaveDataRecord { + u8 reserved_0x00[0x4E]; + char fileName[8]; + u8 reserved_0x56[2]; + char bannerName[8]; + u8 reserved_0x60[0x12]; + char saveName[8]; + u8 reserved_0x7A[0x14]; + char titleName[8]; + u8 reserved_0x96[0x11E]; + char description[0x11]; + char subtitle[0x11]; + u8 reserved_0x1D6[0x8BE]; +} WADSaveDataRecord; + +typedef struct WADSaveDataFile { + u8 reserved_0x00[8]; + WADSaveDataRecord primary[3]; + u8 reserved_0x1FC4[0x44]; + WADSaveDataRecord secondary[3]; +} WADSaveDataFile; + +s32 WADCheckSavedataZD(const WADSaveDataFile* saveData) { + const WADSaveDataRecord* record; + u32 index; + + for (index = 0; index < 3; index++) { + record = &saveData->primary[index]; + if (!_WADIsTerminated(record->fileName, 8)) { + return FALSE; + } + if (!_WADIsTerminated(record->bannerName, 8)) { + return FALSE; + } + if (!_WADIsTerminated(record->saveName, 8)) { + return FALSE; + } + if (!_WADIsTerminated(record->titleName, 8)) { + return FALSE; + } + if (!_WADIsTerminated(record->description, 0x11)) { + return FALSE; + } + if (!_WADIsTerminated(record->subtitle, 0x11)) { + return FALSE; + } + } + for (index = 0; index < 3; index++) { + record = &saveData->secondary[index]; + if (!_WADIsTerminated(record->fileName, 8)) { + return FALSE; + } + if (!_WADIsTerminated(record->bannerName, 8)) { + return FALSE; + } + if (!_WADIsTerminated(record->saveName, 8)) { + return FALSE; + } + if (!_WADIsTerminated(record->titleName, 8)) { + return FALSE; + } + if (!_WADIsTerminated(record->description, 0x11)) { + return FALSE; + } + if (!_WADIsTerminated(record->subtitle, 0x11)) { + return FALSE; + } + } + return TRUE; +} + +u32 _WADIsTerminated(const char* text, u32 maxLength) { + for (; maxLength > 0; maxLength--) { + if (*text == '\0') { + return TRUE; + } + text++; + } + return FALSE; +} -- 2.49.1 From 6e61d82e4be71c4a88c598e00cd23cd015076fb6 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 00:24:06 +0000 Subject: [PATCH 07/34] match wad unpack routines --- libs/RVL_SDK/src/wad/wad.c | 198 ++++++++++++++++++++++++++++++------- 1 file changed, 162 insertions(+), 36 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index ed966021..83f1c334 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -29,31 +29,31 @@ typedef union WADTitleMetadata { u8 bytes[0x80]; } WADTitleMetadata; -typedef struct __attribute__((aligned(32))) WADVersionWorkspace { - WADHeader header; - WADTitleMetadata metadata; - WADStream stream; -} WADVersionWorkspace; - typedef struct WADUnpackInfo { s32 type; s32 headerInfo; u16 cidxMode; - u8 reserved_0x0a[0x0A]; + u8 reserved_0x0a[2]; + u32 size_0x0c; + u32 offset_0x10; void* buffer_0x14; u32 size_0x18; - u8 reserved_0x1c[8]; + u32 size_0x1c; + u32 offset_0x20; void* buffer_0x24; u32 size_0x28; - u8 reserved_0x2c[8]; + u32 size_0x2c; + u32 offset_0x30; void* buffer_0x34; u32 size_0x38; u32 sectionSize; u32 sectionOffset; ESTitleMeta* titleMeta; u32 titleMetaSize; + u32 contentSize; u32 contentOffset; - u8 reserved_0x54[8]; + u32 metaSize; + u32 metaOffset; void* fileList; u32 fileOffset; u32 fileCount; @@ -109,6 +109,7 @@ typedef struct WADImportLoopArgs { #define WAD_STREAM_ALIGNMENT 0x40 #define WAD_READ_ALIGNMENT 0x20 +#define WAD_ALIGN32(value) (((value) + WAD_READ_ALIGNMENT - 1) & ~(WAD_READ_ALIGNMENT - 1)) static s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32, u32); static s32 WADReadStream(WADStream* stream, void** buffer, u32 size, u32 offset); @@ -119,9 +120,9 @@ static s32 WAD_815C2F44(WADHeader* header, s32* headerInfo); static s32 _WADGetTitleVer(WADHeader* header, ESTitleId* titleId, u16* titleVersion, s32 type, s32 headerInfo); static s32 _WADUnpackIRD(WADHeader* header, WADStream* stream, WADUnpackInfo* info, - MEMAllocator* allocator, u32 offset, u32 flags, u32 mode); + MEMAllocator* allocator, u32 offset, u32 mode); static s32 _WADUnpackBroadOn(WADHeader* header, WADStream* stream, WADUnpackInfo* info, - MEMAllocator* allocator, u32 offset, u32 flags, u32 mode); + MEMAllocator* allocator, u32 offset, u32 mode); static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, u32 offset, u32 flags); static s32 _WADUnpack(void* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, @@ -140,10 +141,12 @@ u32 _WADIsTerminated(const char* text, u32 maxLength); extern s32 ES_GetBoot2Version(u32* version); s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADLocation location, u32 offset) { - WADVersionWorkspace workspace ALIGN32; - s32 headerInfo[2]; - WADTitleMetadata* metadataBuffer = &workspace.metadata; - WADHeader* headerBuffer = &workspace.header; + WADStream stream ALIGN32; + WADTitleMetadata metadata; + u8 headerBytes[0x20]; + s32 headerInfo; + WADTitleMetadata* metadataBuffer = &metadata; + WADHeader* headerBuffer = (WADHeader*)headerBytes; s32 result; u32 titleDataOffset; BOOL streamOpened; @@ -156,15 +159,15 @@ s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADL } else if ((offset & 0x3F) != 0) { result = -3007; } else { - result = WADOpenStream(location, path, &workspace.stream, 0, 0); + result = WADOpenStream(location, path, &stream, 0, 0); streamOpened = TRUE; if (result == 0) { - result = WADReadStream(&workspace.stream, (void**)&headerBuffer, 0x20, offset); + result = WADReadStream(&stream, (void**)&headerBuffer, 0x20, offset); if (result != 0x20) { result = -3005; goto done; } - type = WAD_815C2F44(headerBuffer, headerInfo); + type = WAD_815C2F44(headerBuffer, &headerInfo); if (type == 0) { result = -3001; goto done; @@ -172,7 +175,7 @@ s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADL if (((type == 2) && (headerBuffer->tmdSize == 0)) || ((type == 1) && (headerBuffer->ticketSize == 0))) { result = -3002; - if (headerInfo[0] == 2) { + if (headerInfo == 2) { titleDataOffset = 0x60; } else { goto done; @@ -180,13 +183,13 @@ s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADL } if (titleDataOffset == 0) { - result = _WADGetTitleVer(headerBuffer, 0, 0, type, headerInfo[0]); + result = _WADGetTitleVer(headerBuffer, 0, 0, type, headerInfo); if (result == 0) { result = -3002; goto done; } titleDataOffset = result + 0x180; - result = WADReadStream(&workspace.stream, (void**)&metadataBuffer, 0x80, + result = WADReadStream(&stream, (void**)&metadataBuffer, 0x80, offset + titleDataOffset); if ((u32)result != 0x80) { result = -3005; @@ -200,7 +203,7 @@ s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADL *titleVersion = metadataBuffer->tmd.titleVersion; } } else { - result = WADReadStream(&workspace.stream, (void**)&metadataBuffer, 0x20, + result = WADReadStream(&stream, (void**)&metadataBuffer, 0x20, offset + titleDataOffset); if (result != 0x20) { result = -3005; @@ -216,7 +219,7 @@ s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADL done: if (streamOpened) { - WADCloseStream(&workspace.stream); + WADCloseStream(&stream); } return result; } @@ -547,6 +550,7 @@ static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, co return TRUE; } +#pragma dont_inline on static void* _WADMemAlloc(MEMAllocator* allocator, u32 size) { u32 heapType; void* buffer; @@ -575,6 +579,7 @@ static void _WADMemFree(MEMAllocator* allocator, void* buffer) { MEMFreeToAllocator(allocator, buffer); } } +#pragma dont_inline reset static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator) { if ((info->size_0x18 != 0) && (info->buffer_0x14 != 0)) { @@ -919,26 +924,145 @@ static s32 WAD_815C2F44(WADHeader* header, s32* headerInfo) { return 0; } +#pragma dont_inline on static s32 _WADUnpack(void* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, u32 offset, u32 flags, u32 mode) { + s32 result; s32 type; if ((header == 0) || (info == 0)) { - return -3000; - } - memset(info, 0, 0x70); - type = WAD_815C2F44(header, &info->headerInfo); - info->type = type; - if (type == 2) { - if (info->headerInfo == 2) { - return _WADUnpackBackup(header, stream, info, allocator, offset, flags); + result = -3000; + } else { + memset(info, 0, 0x70); + type = WAD_815C2F44(header, &info->headerInfo); + info->type = type; + if (type == 2) { + if (info->headerInfo != 2) { + result = _WADUnpackIRD(header, stream, info, allocator, offset, mode); + } else { + result = _WADUnpackBackup(header, stream, info, allocator, offset, flags); + } + } else if (type == 1) { + result = _WADUnpackBroadOn(header, stream, info, allocator, offset, mode); + } else { + result = -3001; } - return _WADUnpackIRD(header, stream, info, allocator, offset, flags, mode); } - if (type == 1) { - return _WADUnpackBroadOn(header, stream, info, allocator, offset, flags, mode); + return result; +} +#pragma dont_inline reset + +static s32 _WADUnpackIRD(WADHeader* header, WADStream* stream, WADUnpackInfo* info, + MEMAllocator* allocator, u32 offset, u32 mode) { + u16 wadVersion = header->wadVersion; + s32 sectionSize; + u32 sectionOffset; + s32 alignedSize; + s32 bytesRead; + + if ((wadVersion != 0) && (wadVersion != 1)) { + return -3001; } - return -3001; + if ((header->ticketSize == 0) && (header->tmdSize == 0)) { + return -3001; + } + if ((wadVersion == 0) && (header->hdrSize != 0x20)) { + return -3001; + } + if ((wadVersion == 1) && (header->hdrSize != 0x60)) { + return -3001; + } + + info->cidxMode = wadVersion; + if (wadVersion >= 1) { + info->contentIndex = &((WADBackupHeader*)header)->cidx; + } + sectionOffset = (header->hdrSize + 0x3F) & ~0x3F; + + sectionSize = header->certSize; + if (sectionSize != 0) { + info->size_0x0c = sectionSize; + info->offset_0x10 = sectionOffset; + if (mode == 0) { + alignedSize = WAD_ALIGN32(sectionSize); + info->buffer_0x14 = _WADMemAlloc(allocator, alignedSize); + if (info->buffer_0x14 == 0) { + return -3003; + } + info->size_0x18 = 1; + bytesRead = WADReadStream(stream, &info->buffer_0x14, alignedSize, + offset + info->offset_0x10); + if (bytesRead != alignedSize) { + return -3005; + } + } + sectionOffset += (header->certSize + 0x3F) & ~0x3F; + } + sectionSize = header->crlSize; + if (sectionSize != 0) { + info->size_0x1c = sectionSize; + info->offset_0x20 = sectionOffset; + if (mode == 0) { + alignedSize = WAD_ALIGN32(sectionSize); + info->buffer_0x24 = _WADMemAlloc(allocator, alignedSize); + if (info->buffer_0x24 == 0) { + return -3003; + } + info->size_0x28 = 1; + bytesRead = WADReadStream(stream, &info->buffer_0x24, alignedSize, + offset + info->offset_0x20); + if (bytesRead != alignedSize) { + return -3005; + } + } + sectionOffset += (header->crlSize + 0x3F) & ~0x3F; + } + sectionSize = header->ticketSize; + if (sectionSize != 0) { + info->size_0x2c = sectionSize; + info->offset_0x30 = sectionOffset; + if (mode == 0) { + alignedSize = WAD_ALIGN32(sectionSize); + info->buffer_0x34 = _WADMemAlloc(allocator, alignedSize); + if (info->buffer_0x34 == 0) { + return -3003; + } + info->size_0x38 = 1; + bytesRead = WADReadStream(stream, &info->buffer_0x34, alignedSize, + offset + info->offset_0x30); + if (bytesRead != alignedSize) { + return -3005; + } + } + sectionOffset += (header->ticketSize + 0x3F) & ~0x3F; + } + sectionSize = header->tmdSize; + if (sectionSize != 0) { + info->sectionSize = sectionSize; + info->sectionOffset = sectionOffset; + alignedSize = WAD_ALIGN32(sectionSize); + info->titleMeta = _WADMemAlloc(allocator, alignedSize); + if (info->titleMeta == 0) { + return -3003; + } + info->titleMetaSize = 1; + bytesRead = WADReadStream(stream, (void**)&info->titleMeta, alignedSize, + offset + info->sectionOffset); + if (bytesRead != alignedSize) { + return -3005; + } + sectionOffset += (header->tmdSize + 0x3F) & ~0x3F; + } + if (header->contentSize != 0) { + info->contentSize = header->contentSize; + info->contentOffset = sectionOffset; + sectionOffset += (header->contentSize + 0x3F) & ~0x3F; + } + if (header->metaSize != 0) { + info->metaSize = header->metaSize; + info->metaOffset = sectionOffset; + } + return 0; } typedef struct WADSaveDataRecord { @@ -1012,6 +1136,7 @@ s32 WADCheckSavedataZD(const WADSaveDataFile* saveData) { return TRUE; } +#pragma dont_inline on u32 _WADIsTerminated(const char* text, u32 maxLength) { for (; maxLength > 0; maxLength--) { if (*text == '\0') { @@ -1021,3 +1146,4 @@ u32 _WADIsTerminated(const char* text, u32 maxLength) { } return FALSE; } +#pragma dont_inline reset -- 2.49.1 From 258f4a6f911be249e17023962307324d34b74337 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Mon, 28 Sep 2026 19:31:31 -0500 Subject: [PATCH 08/34] iplAddressEdit: 77/94 functions, no inline asm left (#244) * remove stale address edit externs * use keyboard input form member * use nigaoe name member * decompile address edit fade-in * decompile address edit normal init * decompile address edit fadeout init * decompile address edit common calc * decompile address edit normal state * decompile delete message fade-in * decompile friend info removal * decompile delete result transition * decompile delete result state * decompile input wait fade-in * decompile input normal state * decompile address edit draw * model string owner pointer * decompile friend code formatting * decompile duplicate friend code check * decompile own friend code check * decompile Wii ID formatting * decompile address edit fadeout * decompile address edit button fade-in * remove stale address edit symbols * match address edit event dispatch * simplify address edit pane access * match address edit decision animations * match address edit event dispatch * match address edit button fadeout * match address edit delete animation * match address edit mii fadeout * simplify address edit state handlers * simplify address edit state dispatch * simplify address edit scene creation --- include/scene/address/iplAddressEdit.h | 4 +- src/scene/address/iplAddressEdit.cpp | 1137 ++++++------------------ 2 files changed, 277 insertions(+), 864 deletions(-) diff --git a/include/scene/address/iplAddressEdit.h b/include/scene/address/iplAddressEdit.h index 501c89bf..20f4f466 100644 --- a/include/scene/address/iplAddressEdit.h +++ b/include/scene/address/iplAddressEdit.h @@ -114,6 +114,8 @@ namespace ipl { void setName(const wchar_t* value); void setEMail(const wchar_t* value); void setWiiNo(const wchar_t* value); + BOOL isDupCode() const; + BOOL isMyCode() const; wchar_t mValue[0x102]; wchar_t mName[0x0c]; @@ -121,6 +123,7 @@ namespace ipl { bool mbValidMail; bool mbNameNotEmpty; bool mbHasWiiNo; + AddressEdit* mpCallbackOwner; }; enum { @@ -144,7 +147,6 @@ namespace ipl { TextBalloon* mpBalloon; nand::LayoutFile* mpBalloonFile; String mString; - AddressEdit* mpCallbackOwner; bool mbParentalOK; nigaoe::Object* mpNigaoe; RFLCreateID mCreateID; diff --git a/src/scene/address/iplAddressEdit.cpp b/src/scene/address/iplAddressEdit.cpp index 791c5145..271ffeab 100644 --- a/src/scene/address/iplAddressEdit.cpp +++ b/src/scene/address/iplAddressEdit.cpp @@ -65,9 +65,6 @@ namespace ipl { } } -extern "C" u8 __vt__Q33ipl5scene11AddressEdit[]; -extern "C" void __dl__FPv(void*); - ipl::keyboard::Manager::KeyboardSetting::KeyboardSetting( ipl::keyboard::Manager::KeyboardType keyboardType, const wchar_t* value, u32 limit, u32 rows) { type = keyboardType; @@ -82,7 +79,7 @@ ipl::scene::AddressEdit::AddressEdit(EGG::Heap* heap, int friendCode) mSelectedButton = -1; mSubState = -1; mSelectedFriend = 0; - mpCallbackOwner = this; + mString.mpCallbackOwner = this; mString.clear(); mbParentalOK = false; mpNigaoe = NULL; @@ -140,29 +137,11 @@ void ipl::scene::AddressEdit::reset_gui() { } -extern "C" u8 __vt__Q33ipl5scene16AddressEditEvent[]; -extern "C" u8 __vt__Q33ipl5scene17AddressInputEvent[]; extern "C" void* __nw__FUl(u32); extern "C" BOOL RFLSearchOfficialData(const RFLCreateID*, u16*); extern "C" NWC24FriendInfo sFriendInfo__Q23ipl5scene; -extern "C" char smArg__Q23ipl6System; extern "C" const char* smButtonName__Q33ipl5scene6Button[]; -extern "C" void readLayoutAsync__Q33ipl4nand7ManagerFPQ23EGG4HeapPCcb(); -extern "C" void List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs(); -extern "C" void getScene__Q33ipl5scene7ManagerFi(); -extern "C" void UTF16ToANSI__Q33ipl7utility13CharacterCodeFPUcPCwl(); -extern "C" void reset_friend__Q33ipl5scene7AddressFv(); -extern "C" void callBtn1__Q23ipl12DialogWindowFUlUl(); -extern "C" void setEventHandler__Q33ipl5scene6ButtonFPQ23gui12EventHandlerPQ23gui12EventHandler(); -extern "C" void calc__Q33ipl5scene11TextBalloonFv(); -extern "C" void calc__Q33ipl6layout6ObjectFv(); -extern "C" void isActive__Q33ipl5scene6ButtonCFv(); -extern "C" void update__Q33ipl5scene6ButtonFv(); -extern "C" void update__Q33ipl3gui11PaneManagerFv(); -extern "C" void setOrtho__Q33ipl7utility8GraphicsFUl(); -extern "C" void draw__Q33ipl6layout6ObjectFv(); -extern "C" void draw__Q33ipl5scene11TextBalloonFv(); @@ -174,12 +153,6 @@ extern "C" void draw__Q33ipl5scene11TextBalloonFv(); int ipl::scene::AddressEdit::get_button_no(const char*); -extern "C" void callS2Btn2__Q23ipl12DialogWindowFUlUlb(); -extern "C" void reserveText__Q33ipl5scene6ButtonFiUl(); -extern "C" void reserveAnm__Q33ipl5scene6ButtonFi(); -extern "C" void initFrame__Q33ipl7utility15FrameControllerFv(); -extern "C" void getMessage__Q33ipl7message7MessageCFUl(); -extern "C" void del__Q33ipl5scene15FriendListCacheFUl(); @@ -189,21 +162,6 @@ extern "C" void del__Q33ipl5scene15FriendListCacheFUl(); extern "C" NWC24Err NWC24CheckPublicMailAddr_(const char*); -extern "C" void __div2u(); -extern "C" void __mod2u(); -extern "C" u64 utf16_wiiid__Q33ipl5scene11AddressEditFPCw(const wchar_t*); -extern "C" void isValidId__Q33ipl5scene15FriendListCacheFRCUx(); -extern "C" void isDupId__Q33ipl5scene15FriendListCacheFRCUx(); -extern "C" void isDupMail__Q33ipl5scene15FriendListCacheFPCc(); - - - -extern "C" void _savegpr_29(); -extern "C" void _restgpr_29(); -extern "C" void _savegpr_23(); -extern "C" void _savegpr_27(); -extern "C" void _restgpr_23(); -extern "C" void _restgpr_27(); @@ -351,33 +309,6 @@ const wchar_t* ipl::scene::AddressEdit::String::getDispCodeLong() const { return mDisplayText; } -extern "C" asm const wchar_t* getName__Q33ipl6nigaoe6ObjectCFv(void*) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - stw r0, 0x14(r1) - lwz r0, 0x3c(r3) - cmpwi r0, 0 - blt getName_L1 - clrlwi r3, r0, 16 - bl RFLiGetCharData - addi r3, r3, 2 - b getName_L2 -getName_L1: - li r3, 0 -getName_L2: - lwz r0, 0x14(r1) - mtlr r0 - addi r1, r1, 0x10 - blr -} - -extern "C" asm void getInputForm__Q29textinput7ManagerFv() { - nofralloc - lwz r3, 0x1c(r3) - blr -} - void ipl::scene::AddressEdit::String::clear() { memset(mValue, 0, sizeof(mValue)); memset(mName, 0, sizeof(mName)); @@ -405,156 +336,52 @@ void ipl::scene::AddressEdit::String::setEMail(const wchar_t* value) { mbValidMail = !NWC24CheckPublicMailAddr_(reinterpret_cast(address)); } -extern "C" asm void setWiiNo__Q43ipl5scene11AddressEdit6StringFPCw(void*, const wchar_t*) { - nofralloc - stwu r1, -0x20(r1) - mflr r0 - stw r0, 0x24(r1) - addi r11, r1, 0x20 - bl _savegpr_29 - li r0, 1 - mr r31, r4 - stb r0, 0x422(r3) - mr r30, r3 - li r4, 0 - li r5, 0x204 - bl memset - mr r3, r30 - mr r4, r31 - li r5, 0x10 - bl wcsncpy - addi r3, r30, 0x21c - li r4, 0 - li r5, 0x204 - bl memset - li r0, 0x10 - li r7, 0 - li r3, 0 - li r5, 0x3f - mtctr r0 -setWiiNo_L1: - lhzx r6, r31, r3 - cmplwi r6, 0x30 - blt setWiiNo_L2 - cmplwi r6, 0x39 - bgt setWiiNo_L2 - srawi r0, r7, 2 - addze r0, r0 - add r0, r7, r0 - slwi r0, r0, 1 - add r4, r30, r0 - sth r6, 0x21c(r4) - b setWiiNo_L3 -setWiiNo_L2: - srawi r0, r7, 2 - addze r0, r0 - add r0, r7, r0 - slwi r0, r0, 1 - add r4, r30, r0 - sth r5, 0x21c(r4) -setWiiNo_L3: - addi r7, r7, 1 - addi r3, r3, 2 - bdnz setWiiNo_L1 - li r0, 0x20 - mr r3, r31 - sth r0, 0x238(r30) - li r29, 0 - sth r0, 0x22e(r30) - sth r0, 0x224(r30) - bl wcslen - cmplwi r3, 0x10 - bne setWiiNo_L4 - mr r3, r31 - bl utf16_wiiid__Q33ipl5scene11AddressEditFPCw - stw r4, 0xc(r1) - addi r4, r1, 8 - stw r3, 8(r1) - lwz r3, 0x424(r30) - lwz r3, 0x4ec(r3) - bl isValidId__Q33ipl5scene15FriendListCacheFRCUx - cmpwi r3, 0 - beq setWiiNo_L4 - li r29, 1 -setWiiNo_L4: - stb r29, 0x420(r30) - addi r11, r1, 0x20 - bl _restgpr_29 - lwz r0, 0x24(r1) - mtlr r0 - addi r1, r1, 0x20 - blr +void ipl::scene::AddressEdit::String::setWiiNo(const wchar_t* value) { + mbHasWiiNo = true; + memset(mValue, 0, sizeof(mValue)); + wcsncpy(mValue, value, 0x10); + memset(mDisplayText, 0, sizeof(mDisplayText)); + + for (s32 i = 0; i < 0x10; ++i) { + wchar_t digit = value[i]; + if (digit >= L'0' && digit <= L'9') { + mDisplayText[i + i / 4] = digit; + } else { + mDisplayText[i + i / 4] = L'?'; + } + } + mDisplayText[14] = L' '; + mDisplayText[9] = L' '; + mDisplayText[4] = L' '; + + bool valid = false; + if (wcslen(value) == 0x10) { + u64 userId = AddressEdit::utf16_wiiid(value); + if (mpCallbackOwner->mpFriendCache->isValidId(userId)) { + valid = true; + } + } + mbValidMail = valid; } -extern "C" asm int isDupCode__Q43ipl5scene11AddressEdit6StringCFv(void*) { - nofralloc - stwu r1, -0x120(r1) - mflr r0 - stw r0, 0x124(r1) - lbz r0, 0x422(r3) - stw r31, 0x11c(r1) - mr r31, r3 - cmpwi r0, 0 - beq isDupCode_L1 - bl utf16_wiiid__Q33ipl5scene11AddressEditFPCw - stw r4, 0xc(r1) - addi r4, r1, 8 - lwz r5, 0x424(r31) - stw r3, 8(r1) - lwz r3, 0x4ec(r5) - bl isDupId__Q33ipl5scene15FriendListCacheFRCUx - b isDupCode_L2 -isDupCode_L1: - addi r3, r1, 0x10 - li r4, 0 - li r5, 0x101 - bl memset - mr r4, r31 - addi r3, r1, 0x10 - li r5, 0x100 - bl UTF16ToANSI__Q33ipl7utility13CharacterCodeFPUcPCwl - lwz r3, 0x424(r31) - addi r4, r1, 0x10 - lwz r3, 0x4ec(r3) - bl isDupMail__Q33ipl5scene15FriendListCacheFPCc -isDupCode_L2: - lwz r0, 0x124(r1) - lwz r31, 0x11c(r1) - mtlr r0 - addi r1, r1, 0x120 - blr +BOOL ipl::scene::AddressEdit::String::isDupCode() const { + if (mbHasWiiNo) { + u64 userId = AddressEdit::utf16_wiiid(mValue); + return mpCallbackOwner->mpFriendCache->isDupId(userId); + } + + u8 mail[0x101]; + memset(mail, 0, sizeof(mail)); + ipl::utility::CharacterCode::UTF16ToANSI(mail, mValue, 0x100); + return mpCallbackOwner->mpFriendCache->isDupMail(reinterpret_cast(mail)); } -extern "C" asm int isMyCode__Q43ipl5scene11AddressEdit6StringCFv(void*) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - stw r0, 0x14(r1) - lbz r0, 0x422(r3) - stw r31, 0xc(r1) - cmpwi r0, 0 - stw r30, 8(r1) - beq isMyCode_L1 - lwz r4, 0x424(r3) - lwz r4, 0x4ec(r4) - lwz r31, 0x7d68(r4) - lwz r30, 0x7d6c(r4) - bl utf16_wiiid__Q33ipl5scene11AddressEditFPCw - xor r4, r30, r4 - xor r0, r31, r3 - or r0, r4, r0 - cntlzw r0, r0 - srwi r3, r0, 5 - b isMyCode_L2 -isMyCode_L1: - li r3, 0 -isMyCode_L2: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - lwz r30, 8(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +BOOL ipl::scene::AddressEdit::String::isMyCode() const { + if (mbHasWiiNo) { + NWC24UserId myUserId = mpCallbackOwner->mpFriendCache->getMyUserId(); + return AddressEdit::utf16_wiiid(mValue) == myUserId; + } + return false; } void ipl::scene::AddressEdit::prepare() { @@ -564,14 +391,11 @@ void ipl::scene::AddressEdit::prepare() { void ipl::scene::AddressEdit::create() { - ipl::layout::Object* bLayout; - ipl::scene::Board* board = static_cast(ipl::System::getScene(4)); - ipl::nand::LayoutFile* boardFile = board->getLayoutFile(); + ipl::nand::LayoutFile* boardFile = + static_cast(ipl::System::getScene(4))->getLayoutFile(); - bLayout = reinterpret_cast(__nw__FUl(0x580)); - if (bLayout != NULL) { - bLayout = new (bLayout) ipl::layout::Object(getHeap(), boardFile, "arc", "th_Adress_b.brlyt"); - } + ipl::layout::Object* bLayout = + new ipl::layout::Object(getHeap(), boardFile, "arc", "th_Adress_b.brlyt"); bLayout->bindToGroup("th_Adress_b_card_strt.brlan", "card_strt_fnsh", false, true); bLayout->bindToGroup("th_Adress_b_btn_in.brlan", "crd_btn_00", false, true); bLayout->bindToGroup("th_Adress_b_btn_in.brlan", "crd_btn_10", false, false); @@ -605,15 +429,9 @@ void ipl::scene::AddressEdit::create() { bLayout->bindToGroup("th_Adress_b_card_fnsh.brlan", "card_strt_fnsh", false, false); bLayout->finishBinding(); - ipl::scene::AddressEditEvent* editEvent = static_cast(__nw__FUl(sizeof(ipl::scene::AddressEditEvent))); - if (editEvent != NULL) { - new (editEvent) ipl::scene::AddressEditEvent(this); - } - void* editManagerMemory = __nw__FUl(0x34); - ipl::gui::PaneManager* editManager = reinterpret_cast(editManagerMemory); - if (editManagerMemory != NULL) { - editManager = new (editManagerMemory) ipl::gui::PaneManager(static_cast< ::gui::EventHandler*>(editEvent), bLayout->getDrawInfo(), NULL, NULL, false); - } + ipl::scene::AddressEditEvent* editEvent = new ipl::scene::AddressEditEvent(this); + ipl::gui::PaneManager* editManager = new ipl::gui::PaneManager( + static_cast< ::gui::EventHandler*>(editEvent), bLayout->getDrawInfo(), NULL, NULL, false); editManager->setupScene(bLayout); editManager->setAllComponentTriggerTarget(false); for (s32 i = 0; i < 5; i++) { @@ -646,10 +464,8 @@ void ipl::scene::AddressEdit::create() { ->getNW4RLyt()->GetRootPane()->FindPaneByName("T_frnd_crd_00", true); set_textbox(textPane, L""); - ipl::layout::Object* cLayout = reinterpret_cast(__nw__FUl(0x580)); - if (cLayout != NULL) { - cLayout = new (cLayout) ipl::layout::Object(getHeap(), boardFile, "arc", "th_Adress_c.brlyt"); - } + ipl::layout::Object* cLayout = + new ipl::layout::Object(getHeap(), boardFile, "arc", "th_Adress_c.brlyt"); cLayout->bindToGroup("th_Adress_c_card_strt.brlan", "G_card_strt_fnsh", false, true); cLayout->bindToGroup("th_Adress_c_question_alp_in.brlan", "G_question_00", false, true); cLayout->bindToGroup("th_Adress_c_name_alp_in.brlan", "G_name_00", false, true); @@ -662,15 +478,9 @@ void ipl::scene::AddressEdit::create() { cLayout->bindToGroup("th_Adress_c_card_fnsh.brlan", "G_card_strt_fnsh", false, false); cLayout->finishBinding(); - ipl::scene::AddressInputEvent* inputEvent = static_cast(__nw__FUl(sizeof(ipl::scene::AddressInputEvent))); - if (inputEvent != NULL) { - new (inputEvent) ipl::scene::AddressInputEvent(this); - } - void* inputManagerMemory = __nw__FUl(0x34); - ipl::gui::PaneManager* inputManager = reinterpret_cast(inputManagerMemory); - if (inputManagerMemory != NULL) { - inputManager = new (inputManagerMemory) ipl::gui::PaneManager(static_cast< ::gui::EventHandler*>(inputEvent), cLayout->getDrawInfo(), NULL, NULL, false); - } + ipl::scene::AddressInputEvent* inputEvent = new ipl::scene::AddressInputEvent(this); + ipl::gui::PaneManager* inputManager = new ipl::gui::PaneManager( + static_cast< ::gui::EventHandler*>(inputEvent), cLayout->getDrawInfo(), NULL, NULL, false); inputManager->setupScene(cLayout); inputManager->setAllComponentTriggerTarget(false); textPane = cLayout->getNW4RLyt()->GetRootPane()->FindPaneByName(sInputPaneName, true); @@ -679,10 +489,8 @@ void ipl::scene::AddressEdit::create() { mpInputEvent = inputEvent; mpInputGui = inputManager; - ipl::layout::Object* background = reinterpret_cast(__nw__FUl(0x580)); - if (background != NULL) { - background = new (background) ipl::layout::Object(getHeap(), boardFile, "arc", "my_Back_a.brlyt"); - } + ipl::layout::Object* background = + new ipl::layout::Object(getHeap(), boardFile, "arc", "my_Back_a.brlyt"); mpBackgroundLayout = background; background->bind("my_Back_a_Apear.brlan", "Picture_00", false, true); (mpBackgroundLayout) @@ -800,7 +608,7 @@ void ipl::scene::AddressEdit::stt_wait_decide_anm() { ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); for (s32 i = 0; i < 5; ++i) { u16 index = static_cast(mSelectedButton + 6); - complete = complete && !mpCodeLayout->getAnim(index)->isPlaying(); + complete = (complete & !mpCodeLayout->getAnim(index)->isPlaying()) != 0; } if (!complete) { return; @@ -808,16 +616,16 @@ void ipl::scene::AddressEdit::stt_wait_decide_anm() { switch (mSelectedButton) { case 2: { - u32 friendIndex = mSelectedFriend; - ipl::layout::Animator* animator; - if (mpFriendCache->getInfo(friendIndex).attr.status == 2) { - animator = mpCodeLayout->getAnim(0x15); + if (mpFriendCache->getInfo(mSelectedFriend).attr.status == 2) { + ipl::layout::Animator* statusAnimator = mpCodeLayout->getAnim(0x15); + statusAnimator->initFrame(); + statusAnimator->restart(); } else { - animator = mpCodeLayout->getAnim(0x1b); + ipl::layout::Animator* statusAnimator = mpCodeLayout->getAnim(0x1b); + statusAnimator->initFrame(); + statusAnimator->restart(); } - animator->initFrame(); - animator->restart(); - animator = mpCodeLayout->getAnim(0x16); + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0x16); animator->initFrame(); animator->restart(); animator = mpCodeLayout->getAnim(0x17); @@ -1108,13 +916,11 @@ void ipl::scene::AddressEdit::stt_add_mii_input() { } void ipl::scene::AddressEdit::stt_add_mii_fadeout() { - if (mpNameLayout->getAnim(9)->isPlaying() || - mpNameLayout->getAnim(5)->isPlaying() || - mpNameLayout->getAnim(8)->isPlaying()) { - return; - } - - if (mSubState == 5) { + if (!mpNameLayout->getAnim(9)->isPlaying() && + !mpNameLayout->getAnim(5)->isPlaying() && + !mpNameLayout->getAnim(8)->isPlaying()) { + switch (mSubState) { + case 5: { ipl::layout::Animator* animator = mpNameLayout->getAnim(1); animator->initFrame(); animator->restart(); @@ -1130,10 +936,10 @@ void ipl::scene::AddressEdit::stt_add_mii_fadeout() { pane = mpNameLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_msg_00", true); set_textbox(pane, ipl::System::getMessage(0x49)); mState = 0x18; - return; + break; } - if (mSubState == 7) { + case 7: { ipl::layout::Animator* animator = mpCodeLayout->getAnim(0); animator->initFrame(); animator->restart(); @@ -1152,6 +958,11 @@ void ipl::scene::AddressEdit::stt_add_mii_fadeout() { pane = mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_frnd_crd_00", true); set_textbox(pane, mString.mDisplayText); mState = 0x21; + break; + } + default: + break; + } } } @@ -1170,15 +981,16 @@ void ipl::scene::AddressEdit::stt_wait_del_msg_fadeout() { if (!mpCodeLayout->getAnim(0x1d)->isPlaying()) { u32 friendIndex = mSelectedFriend; u32 status = mpFriendCache->getInfo(friendIndex).attr.status; - ipl::layout::Animator* animator; if (status == 2) { - animator = mpCodeLayout->getAnim(0x10); + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0x10); + animator->initFrame(); + animator->restart(); } else { - animator = mpCodeLayout->getAnim(0x1a); + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0x1a); + animator->initFrame(); + animator->restart(); } - animator->initFrame(); - animator->restart(); - animator = mpCodeLayout->getAnim(0x11); + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0x11); animator->initFrame(); animator->restart(); animator = mpCodeLayout->getAnim(0x12); @@ -1203,26 +1015,28 @@ void ipl::scene::AddressEdit::stt_msg_no_mii() { } void ipl::scene::AddressEdit::stt_add_confirm_fadeout() { - if (!mpCodeLayout->getAnim(0x1e)->isPlaying()) { - ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); - switch (mSubState) { - case 5: { - ipl::layout::Animator* animator = mpNameLayout->getAnim(0); - animator->initFrame(); - animator->restart(); - mState = 0x1c; - break; - } - case 7: - add_friendinfo(); - button->reserveText(1, 0x29); - static_cast(ipl::System::getScene(0x14))->reset_friend(); - ipl::System::getDialog()->callBtn1(0x4a, 0x2e); - mState = 0x26; - break; - default: - break; - } + if (mpCodeLayout->getAnim(0x1e)->isPlaying()) { + return; + } + + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + switch (mSubState) { + case 5: { + ipl::layout::Animator* animator = mpNameLayout->getAnim(0); + animator->initFrame(); + animator->restart(); + mState = 0x1c; + break; + } + case 7: + add_friendinfo(); + button->reserveText(1, 0x29); + static_cast(ipl::System::getScene(0x14))->reset_friend(); + ipl::System::getDialog()->callBtn1(0x4a, 0x2e); + mState = 0x26; + break; + default: + break; } } @@ -1436,17 +1250,23 @@ void ipl::scene::AddressEditEvent::onEvent(u32 componentId, u32 event, void* dat static_cast< ::gui::PaneComponent*>(manager->getComponent(componentId)); const char* paneName = component->getPane()->GetName(); - if (signedEvent == 1) { + switch (signedEvent) { + case 1: if (data != NULL) { mpParent->start_point_event(paneName, static_cast(data)); } - } else if (signedEvent >= 1) { - if (signedEvent < 3) { - mpParent->start_left_event(paneName); + break; + case 2: + mpParent->start_left_event(paneName); + break; + case 0: + if (data != NULL && + static_cast(data)->downTrg(0x100800)) { + mpParent->start_trig_event(paneName); } - } else if (signedEvent >= 0 && data != NULL && - static_cast(data)->downTrg(0x100800)) { - mpParent->start_trig_event(paneName); + break; + default: + break; } } @@ -1456,18 +1276,29 @@ void ipl::scene::AddressInputEvent::onEvent(u32 componentId, u32 event, void* da ::gui::PaneComponent* component = static_cast< ::gui::PaneComponent*>(manager->getComponent(componentId)); const char* paneName = component->getPane()->GetName(); - ipl::controller::Interface* controller = static_cast(data); - if (signedEvent == 1) { - if (controller != NULL) { - mpParent->start_ipt_point_event(paneName, controller->getChannel()); + switch (signedEvent) { + case 0: + if (data != NULL && + static_cast(data)->downTrg(0x100800)) { + mpParent->start_ipt_trig_event( + paneName, static_cast(data)->getChannel()); } - } else if (signedEvent >= 1) { - if (signedEvent < 3 && controller != NULL) { - mpParent->start_ipt_left_event(paneName, controller->getChannel()); + break; + case 1: + if (data != NULL) { + mpParent->start_ipt_point_event( + paneName, static_cast(data)->getChannel()); } - } else if (signedEvent >= 0 && controller != NULL && controller->downTrg(0x100800)) { - mpParent->start_ipt_trig_event(paneName, controller->getChannel()); + break; + case 2: + if (data != NULL) { + mpParent->start_ipt_left_event( + paneName, static_cast(data)->getChannel()); + } + break; + default: + break; } } @@ -1499,64 +1330,16 @@ void ipl::scene::AddressEdit::nigaoe_create_callback_add(ipl::nigaoe::Object* ob self->mpBalloon->init(object->getName(), 0xa); } -extern "C" asm void calcFadein__Q33ipl5scene11AddressEditFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - li r4, 0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - li r31, 0 - stw r30, 8(r1) - mr r30, r3 - lwz r5, 0x68(r3) - addi r3, r5, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq calcFadein_L1 - lwz r3, 0x74(r30) - li r4, 0 - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq calcFadein_L1 - li r31, 1 -calcFadein_L1: - mr r3, r31 - lwz r31, 0xc(r1) - lwz r30, 8(r1) - lwz r0, 0x14(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +ipl::scene::FaderSceneCommand ipl::scene::AddressEdit::calcFadein() { + FaderSceneCommand command = FADER_SCN_CONTINUE; + if (!mpCodeLayout->getAnim(0)->isPlaying() && !mpNameLayout->getAnim(0)->isPlaying()) { + command = FADER_SCN_NEXT; + } + return command; } -extern "C" asm void initCalcNormal__Q33ipl5scene11AddressEditFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - cmpwi r3, 0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r3 - beq initCalcNormal_L1 - addi r31, r3, 0x58 -initCalcNormal_L1: - lis r3, smArg__Q23ipl6System@ha - li r4, 5 - addi r3, r3, smArg__Q23ipl6System@l - lwz r3, 0x64(r3) - bl getScene__Q33ipl5scene7ManagerFi - mr r4, r31 - li r5, 0 - bl setEventHandler__Q33ipl5scene6ButtonFPQ23gui12EventHandlerPQ23gui12EventHandler - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::initCalcNormal() { + static_cast(ipl::System::getScene(5))->setEventHandler(this); } ipl::scene::FaderSceneCommand ipl::scene::AddressEdit::calcNormal() { @@ -1822,137 +1605,38 @@ void ipl::scene::AddressEdit::stt_add_name_fadeout() { } } -extern "C" asm void initCalcFadeout__Q33ipl5scene11AddressEditFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - lis r5, smArg__Q23ipl6System@ha - li r4, 5 - stw r0, 0x14(r1) - addi r5, r5, smArg__Q23ipl6System@l - stw r31, 0xc(r1) - mr r31, r3 - lwz r3, 0x64(r5) - bl getScene__Q33ipl5scene7ManagerFi - li r4, 0 - li r5, 0 - bl setEventHandler__Q33ipl5scene6ButtonFPQ23gui12EventHandlerPQ23gui12EventHandler - lwz r3, 0xa8(r31) - bl calc__Q33ipl5scene11TextBalloonFv - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::initCalcFadeout() { + static_cast(ipl::System::getScene(5))->setEventHandler(NULL); + mpBalloon->calc(); } -extern "C" asm void calcCommonAfter__Q33ipl5scene11AddressEditFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r3 - lwz r3, 0x70(r3) - lwz r12, 0(r3) - lwz r12, 0x10(r12) - mtctr r12 - bctrl - lwz r3, 0x68(r31) - bl calc__Q33ipl6layout6ObjectFv - lwz r3, 0x7c(r31) - lwz r12, 0(r3) - lwz r12, 0x10(r12) - mtctr r12 - bctrl - lwz r3, 0x74(r31) - bl calc__Q33ipl6layout6ObjectFv - lwz r3, 0x80(r31) - bl calc__Q33ipl6layout6ObjectFv - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::calcCommonAfter() { + mpEditGui->calc(); + mpCodeLayout->calc(); + mpInputGui->calc(); + mpNameLayout->calc(); + mpBackgroundLayout->calc(); } -extern "C" asm void stt_normal__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - lis r5, smArg__Q23ipl6System@ha - li r4, 5 - stw r0, 0x14(r1) - addi r5, r5, smArg__Q23ipl6System@l - stw r31, 0xc(r1) - stw r30, 8(r1) - mr r30, r3 - lwz r3, 0x64(r5) - bl getScene__Q33ipl5scene7ManagerFi - mr r31, r3 - bl isActive__Q33ipl5scene6ButtonCFv - cmpwi r3, 0 - beq stt_normal_L1 - mr r3, r31 - bl update__Q33ipl5scene6ButtonFv -stt_normal_L1: - lwz r0, 0x64(r30) - cmpwi r0, 0 - bne stt_normal_L2 - lwz r3, 0x70(r30) - bl update__Q33ipl3gui11PaneManagerFv -stt_normal_L2: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - lwz r30, 8(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::stt_normal() { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + if (button->isActive()) { + button->update(); + } + if (mState == 0) { + mpEditGui->update(); + } } -extern "C" asm void stt_wait_del_msg_fadein__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - li r4, 0x1c - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r3 - lwz r5, 0x68(r3) - addi r3, r5, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq stt_wait_del_msg_fadein_L1 - li r0, 7 - stw r0, 0x64(r31) -stt_wait_del_msg_fadein_L1: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::stt_wait_del_msg_fadein() { + if (!mpCodeLayout->getAnim(0x1c)->isPlaying()) { + mState = 7; + } } -extern "C" asm void delete_friendinfo__Q33ipl5scene11AddressEditFv(void* self) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - mr r4, r3 - stw r0, 0x14(r1) - lwz r3, 0x4ec(r3) - lwz r4, 0xa4(r4) - bl del__Q33ipl5scene15FriendListCacheFUl - lis r3, smArg__Q23ipl6System@ha - li r4, 0x14 - addi r3, r3, smArg__Q23ipl6System@l - lwz r3, 0x64(r3) - bl getScene__Q33ipl5scene7ManagerFi - bl reset_friend__Q33ipl5scene7AddressFv - lwz r0, 0x14(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::delete_friendinfo() { + mpFriendCache->del(mSelectedFriend); + static_cast(ipl::System::getScene(0x14))->reset_friend(); } u64 ipl::scene::AddressEdit::utf16_wiiid(const wchar_t* value) { @@ -1965,51 +1649,13 @@ u64 ipl::scene::AddressEdit::utf16_wiiid(const wchar_t* value) { return result; } -extern "C" asm void wiiid_utf16__Q33ipl5scene11AddressEditFUxPw(u64, wchar_t*) { - nofralloc - stwu r1, -0x30(r1) - mflr r0 - stw r0, 0x34(r1) - addi r11, r1, 0x30 - bl _savegpr_23 - mr r24, r3 - mr r23, r4 - mr r25, r5 - li r27, 1 - li r28, 0 - li r26, 0 - li r29, 0x30 - li r30, 0 - li r31, 0xa -wiiid_utf16_L1: - mr r3, r24 - mr r4, r23 - mr r5, r28 - mr r6, r27 - bl __div2u - li r6, 0xa - li r5, 0 - bl __mod2u - addc r5, r4, r29 - adde r3, r3, r30 - subfic r3, r26, 0xf - addi r26, r26, 1 - slwi r4, r3, 1 - mulhwu r0, r27, r31 - cmpwi r26, 0x10 - sthx r5, r25, r4 - mullw r3, r28, r31 - mulli r27, r27, 0xa - add r28, r0, r3 - blt wiiid_utf16_L1 - li r0, 0 - addi r11, r1, 0x30 - sth r0, 0x20(r25) - bl _restgpr_23 - lwz r0, 0x34(r1) - mtlr r0 - addi r1, r1, 0x30 - blr +void ipl::scene::AddressEdit::wiiid_utf16(u64 value, wchar_t* output) { + u64 multiplier = 1; + for (int i = 0; i < 0x10; ++i) { + output[0xf - i] = static_cast((value / multiplier) % 10 + L'0'); + multiplier *= 10; + } + output[0x10] = 0; } void ipl::scene::AddressEdit::String::setName(const wchar_t* value) { @@ -2027,342 +1673,111 @@ void ipl::scene::AddressEdit::String::setName(const wchar_t* value) { } } -extern "C" asm void calcFadeout__Q33ipl5scene11AddressEditFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - stw r30, 8(r1) - mr r30, r3 - lwz r3, 0xa8(r3) - bl calc__Q33ipl5scene11TextBalloonFv - lis r31, smArg__Q23ipl6System@ha - addi r31, r31, smArg__Q23ipl6System@l - lwz r3, 0xc4(r31) - lwz r12, 0(r3) - lwz r12, 0xc(r12) - mtctr r12 - bctrl - cmpwi r3, 1 - bne calcFadeout_L1 - lwz r3, 0x68(r30) - li r31, 0 - li r4, 0x1e - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq calcFadeout_L2 - lwz r3, 0x74(r30) - li r4, 9 - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq calcFadeout_L2 - li r31, 1 -calcFadeout_L2: - mr r3, r31 - b calcFadeout_L3 -calcFadeout_L1: - lwz r3, 0xc4(r31) - lwz r12, 0(r3) - lwz r12, 0xc(r12) - mtctr r12 - bctrl - cntlzw r0, r3 - srwi r3, r0, 5 -calcFadeout_L3: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - lwz r30, 8(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +ipl::scene::FaderSceneCommand ipl::scene::AddressEdit::calcFadeout() { + mpBalloon->calc(); + if (ipl::System::getFader()->getStatus() == EGG::Fader::PREPARE_OUT) { + FaderSceneCommand command = FADER_SCN_CONTINUE; + if (!mpCodeLayout->getAnim(0x1e)->isPlaying() && !mpNameLayout->getAnim(9)->isPlaying()) { + command = FADER_SCN_NEXT; + } + return command; + } + return ipl::System::getFader()->getStatus() == EGG::Fader::PREPARE_IN ? FADER_SCN_NEXT + : FADER_SCN_CONTINUE; } -extern "C" asm void draw__Q33ipl5scene11AddressEditFv() { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - lis r4, smArg__Q23ipl6System@ha - stw r0, 0x14(r1) - addi r4, r4, smArg__Q23ipl6System@l - stw r31, 0xc(r1) - mr r31, r3 - lwz r3, 0x64(r4) - lwz r0, 0x100(r3) - cmpwi r0, 1 - bne draw_L1 - li r3, 0 - bl setOrtho__Q33ipl7utility8GraphicsFUl - lwz r3, 0x68(r31) - bl draw__Q33ipl6layout6ObjectFv - lwz r3, 0x80(r31) - bl draw__Q33ipl6layout6ObjectFv - lwz r3, 0x74(r31) - bl draw__Q33ipl6layout6ObjectFv - lwz r3, 0xa8(r31) - bl draw__Q33ipl5scene11TextBalloonFv -draw_L1: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::draw() { + if (ipl::System::onDefaultDrawLayer()) { + ipl::utility::Graphics::setOrtho(0); + mpCodeLayout->draw(); + mpBackgroundLayout->draw(); + mpNameLayout->draw(); + mpBalloon->draw(); + } } -extern "C" asm void stt_wait_btn_fadein__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r3 - stw r30, 8(r1) - lwz r0, 0xa4(r3) - lwz r4, 0x4ec(r3) - mulli r0, r0, 0x140 - add r4, r4, r0 - lwz r0, 4(r4) - cmplwi r0, 2 - bne stt_wait_btn_fadein_L1 - lwz r3, 0x68(r3) - li r4, 0x10 - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r3, 0x14(r3) - addi r3, r3, -1 - addic r0, r3, -1 - subfe r30, r0, r3 - b stt_wait_btn_fadein_L2 -stt_wait_btn_fadein_L1: - lwz r3, 0x68(r3) - li r4, 0x1a - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r3, 0x14(r3) - addi r3, r3, -1 - addic r0, r3, -1 - subfe r30, r0, r3 -stt_wait_btn_fadein_L2: - lwz r3, 0x68(r31) - li r4, 0x11 - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r5, 0x14(r3) - li r4, 0x12 - lwz r3, 0x68(r31) - addi r5, r5, -1 - addic r0, r5, -1 - addi r3, r3, 0x28c - subfe r0, r0, r5 - and r5, r30, r0 - addic r0, r5, -1 - subfe r30, r0, r5 - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r3, 0x14(r3) - addi r3, r3, -1 - addic r0, r3, -1 - subfe r0, r0, r3 - and r3, r30, r0 - addic r0, r3, -1 - subfe. r0, r0, r3 - beq stt_wait_btn_fadein_L3 - li r0, 0 - stw r0, 0x64(r31) -stt_wait_btn_fadein_L3: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - lwz r30, 8(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::stt_wait_btn_fadein() { + u32 friendStatus = mpFriendCache->getInfo(mSelectedFriend).attr.status; + bool finished; + if (friendStatus == 2) { + finished = !mpCodeLayout->getAnim(0x10)->isPlaying(); + } else { + finished = !mpCodeLayout->getAnim(0x1a)->isPlaying(); + } + finished = finished & !mpCodeLayout->getAnim(0x11)->isPlaying(); + finished = finished & !mpCodeLayout->getAnim(0x12)->isPlaying(); + if (finished) { + mState = 0; + } } void ipl::scene::AddressEdit::stt_wait_btn_fadeout() { - u32 index = mSelectedFriend; - u32 friendType = mpFriendCache->getInfo(index).attr.status; - u32 friendFinished; - if (friendType == 2) { + bool friendFinished; + if (mpFriendCache->getInfo(mSelectedFriend).attr.status == 2) { friendFinished = !mpCodeLayout->getAnim(0x15)->isPlaying(); } else { friendFinished = !mpCodeLayout->getAnim(0x1b)->isPlaying(); } - bool finished = friendFinished && !mpCodeLayout->getAnim(0x16)->isPlaying(); - finished = finished && !mpCodeLayout->getAnim(0x17)->isPlaying(); + friendFinished &= !mpCodeLayout->getAnim(0x16)->isPlaying(); + friendFinished &= !mpCodeLayout->getAnim(0x17)->isPlaying(); - if (finished) { + if (friendFinished) { nw4r::lyt::Pane* label = mpCodeLayout->getNW4RLyt()->GetRootPane()->FindPaneByName("T_card_msg_00", true); const wchar_t* message = ipl::System::getMessage(0x30); set_textbox(label, message); ipl::System::getDialog()->callS2Btn2(0x142, 0x141, true); - mpCodeLayout->getAnim(0x1c)->initFrame(); - mpCodeLayout->getAnim(0x1c)->restart(); + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0x1c); + animator->initFrame(); + animator->restart(); mState = 4; } } -extern "C" asm void stt_wait_del_msg_fadeout_to_rlt__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - li r4, 0x1d - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r3 - lwz r5, 0x68(r3) - addi r3, r5, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq stt_wait_del_msg_fadeout_to_rlt_L1 - li r0, 8 - lis r3, smArg__Q23ipl6System@ha - stw r0, 0x64(r31) - addi r3, r3, smArg__Q23ipl6System@l - li r4, 0x51 - li r5, 0x2e - lwz r3, 0xac(r3) - bl callBtn1__Q23ipl12DialogWindowFUlUl -stt_wait_del_msg_fadeout_to_rlt_L1: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::stt_wait_del_msg_fadeout_to_rlt() { + if (!mpCodeLayout->getAnim(0x1d)->isPlaying()) { + mState = 8; + ipl::System::getDialog()->callBtn1(0x51, 0x2e); + } } -extern "C" asm void stt_msg_del_rlt__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - li r4, 5 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - lis r31, smArg__Q23ipl6System@ha - addi r31, r31, smArg__Q23ipl6System@l - stw r30, 8(r1) - mr r30, r3 - lwz r3, 0x64(r31) - bl getScene__Q33ipl5scene7ManagerFi - lwz r4, 0xac(r31) - mr r31, r3 - lwz r0, 0x24(r4) - cmpwi r0, 1 - beq stt_msg_del_rlt_L2 - b stt_msg_del_rlt_L1 -stt_msg_del_rlt_L2: - li r4, 1 - li r5, 0x29 - bl reserveText__Q33ipl5scene6ButtonFiUl - mr r3, r31 - li r4, 0xf - bl reserveAnm__Q33ipl5scene6ButtonFi - lwz r3, 0x68(r30) - li r4, 0x1e - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - mr r31, r3 - bl initFrame__Q33ipl7utility15FrameControllerFv - li r3, 1 - li r0, 0x30 - stw r3, 0x14(r31) - stw r0, 0x64(r30) -stt_msg_del_rlt_L1: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - lwz r30, 8(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::stt_msg_del_rlt() { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + switch (ipl::System::getDialog()->getLastResult()) { + case 1: + button->reserveText(1, 0x29); + button->reserveAnm(0xf); + ipl::layout::Animator* animator = mpCodeLayout->getAnim(0x1e); + animator->initFrame(); + animator->restart(); + mState = 0x30; + break; + default: + break; + } } -extern "C" asm void stt_ipt_wait_fadein__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - li r4, 0 - stw r0, 0x14(r1) - stw r31, 0xc(r1) - mr r31, r3 - lwz r5, 0x74(r3) - addi r3, r5, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq stt_ipt_wait_fadein_L1 - lwz r3, 0x74(r31) - li r4, 1 - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq stt_ipt_wait_fadein_L1 - lwz r3, 0x74(r31) - li r4, 2 - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq stt_ipt_wait_fadein_L1 - lwz r3, 0x80(r31) - li r4, 0 - addi r3, r3, 0x28c - bl List_GetNth__Q24nw4r2utFPCQ34nw4r2ut4ListUs - lwz r0, 0x14(r3) - cmpwi r0, 1 - beq stt_ipt_wait_fadein_L1 - li r0, 0xd - stw r0, 0x64(r31) -stt_ipt_wait_fadein_L1: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::stt_ipt_wait_fadein() { + if (!mpNameLayout->getAnim(0)->isPlaying() && !mpNameLayout->getAnim(1)->isPlaying() && + !mpNameLayout->getAnim(2)->isPlaying() && !mpBackgroundLayout->getAnim(0)->isPlaying()) { + mState = 0xd; + } } -extern "C" asm void stt_ipt_normal__Q33ipl5scene11AddressEditFv(ipl::scene::AddressEdit*) { - nofralloc - stwu r1, -0x10(r1) - mflr r0 - lis r5, smArg__Q23ipl6System@ha - li r4, 5 - stw r0, 0x14(r1) - addi r5, r5, smArg__Q23ipl6System@l - stw r31, 0xc(r1) - stw r30, 8(r1) - mr r30, r3 - lwz r3, 0x64(r5) - bl getScene__Q33ipl5scene7ManagerFi - mr r31, r3 - bl isActive__Q33ipl5scene6ButtonCFv - cmpwi r3, 0 - beq stt_ipt_normal_L1 - mr r3, r31 - bl update__Q33ipl5scene6ButtonFv -stt_ipt_normal_L1: - lwz r0, 0x64(r30) - cmpwi r0, 0xd - bne stt_ipt_normal_L2 - lwz r3, 0x7c(r30) - bl update__Q33ipl3gui11PaneManagerFv -stt_ipt_normal_L2: - lwz r0, 0x14(r1) - lwz r31, 0xc(r1) - lwz r30, 8(r1) - mtlr r0 - addi r1, r1, 0x10 - blr +void ipl::scene::AddressEdit::stt_ipt_normal() { + ipl::scene::Button* button = static_cast(ipl::System::getScene(5)); + if (button->isActive()) { + button->update(); + } + if (mState == 0xd) { + mpInputGui->update(); + } } void ipl::scene::AddressEdit::stt_ipt_input() { ipl::keyboard::Manager::State* state = ipl::System::getKeyboard()->getState(); - ipl::scene::Manager* sceneManager = ipl::System::getSceneManager(); - ipl::scene::Button* button = static_cast(sceneManager->getScene(5)); + ipl::scene::Button* button = + static_cast(ipl::System::getSceneManager()->getScene(5)); s32 keyboardType = state->iplType; if (keyboardType == ipl::keyboard::Manager::STATE_DISAPPEARING) { goto state_disappearing; @@ -2446,8 +1861,7 @@ void ipl::scene::AddressEdit::stt_add_code_input() { bool codeWasEmpty = mString.mValue[0] == 0; if (state->pressOK) { if (mMode == 1) { - setWiiNo__Q43ipl5scene11AddressEdit6StringFPCw( - &mString, state->wcString); + mString.setWiiNo(state->wcString); } else { mString.setEMail(state->wcString); } @@ -2474,8 +1888,8 @@ void ipl::scene::AddressEdit::stt_add_code_input() { pane->restart(); } if (mString.mbValidMail != 0 && - isDupCode__Q43ipl5scene11AddressEdit6StringCFv(&mString) == 0 && - isMyCode__Q43ipl5scene11AddressEdit6StringCFv(&mString) == 0) { + mString.isDupCode() == 0 && + mString.isMyCode() == 0) { button->reserveText(1, 0x2e); button->reserveAnm(0xf); } else { @@ -2488,12 +1902,10 @@ void ipl::scene::AddressEdit::stt_add_code_input() { if (state->pressOK) { if (mString.mValue[0] != 0) { if (mMode == 1) { - if (isMyCode__Q43ipl5scene11AddressEdit6StringCFv( - &mString) != 0) { + if (mString.isMyCode() != 0) { ipl::System::getDialog()->callBtn1(0x56, 0x2e); mState = 0x16; - } else if (isDupCode__Q43ipl5scene11AddressEdit6StringCFv( - &mString) != 0) { + } else if (mString.isDupCode() != 0) { ipl::System::getDialog()->callBtn1(0x52, 0x2e); mState = 0x14; } else if (mString.mbValidMail == 0) { @@ -2502,8 +1914,7 @@ void ipl::scene::AddressEdit::stt_add_code_input() { } else { mState = 0x11; } - } else if (isDupCode__Q43ipl5scene11AddressEdit6StringCFv( - &mString) != 0) { + } else if (mString.isDupCode() != 0) { ipl::System::getDialog()->callBtn1(0x53, 0x2e); mState = 0x15; } else if (mString.mbValidMail == 0) { @@ -2531,8 +1942,7 @@ void ipl::scene::AddressEdit::stt_add_code_input() { } void ipl::scene::AddressEdit::stt_add_code_fadeout() { - ipl::layout::Object* layout = mpNameLayout; - ipl::layout::Animator* pane = layout->getAnim(5); + ipl::layout::Animator* pane = mpNameLayout->getAnim(5); if (!pane->isPlaying()) { pane = mpNameLayout->getAnim(6); if (!pane->isPlaying()) { @@ -2811,8 +2221,7 @@ done: void ipl::scene::AddressEdit::start_left_event( const char* paneName) { int buttonNo = get_button_no(paneName); - s32 state = mState; - switch (state) { + switch (mState) { case 0x22: goto state22; case 0: @@ -3030,8 +2439,8 @@ void ipl::scene::AddressEdit::start_ipt_trig_event( } setDefaultTitleText(mString.mValue, false); if (mString.mbValidMail != 0 && - isDupCode__Q43ipl5scene11AddressEdit6StringCFv(&mString) == 0 && - isMyCode__Q43ipl5scene11AddressEdit6StringCFv(&mString) == 0) { + !mString.isDupCode() && + !mString.isMyCode()) { button->reserveAnm(0x10); } else { button->reserveAnm(0xc); @@ -3134,9 +2543,8 @@ void ipl::scene::AddressEdit::onEventDerived(u32 componentId, u32 event, const i nw4r::lyt::Pane* pane = component->getPane(); const char* paneName = pane->GetName(); - if (event != 0) { - return; - } + switch (event) { + case 0: { if (controller == NULL) { return; } @@ -3186,10 +2594,8 @@ void ipl::scene::AddressEdit::onEventDerived(u32 componentId, u32 event, const i requestSceneChange(getPreviousScene(), NULL); button->reserveAnm(0x1b); if (mString.mbValidMail != 0 && - isDupCode__Q43ipl5scene11AddressEdit6StringCFv( - &mString) == 0 && - isMyCode__Q43ipl5scene11AddressEdit6StringCFv( - &mString) == 0) { + mString.isDupCode() == 0 && + mString.isMyCode() == 0) { button->reserveAnm(0x10); } else { button->reserveAnm(0xc); @@ -3363,6 +2769,11 @@ void ipl::scene::AddressEdit::onEventDerived(u32 componentId, u32 event, const i } } } + break; + } + default: + break; + } } void ipl::scene::AddressEdit::start_ipt_left_event( @@ -3477,8 +2888,8 @@ void ipl::scene::AddressEdit::add_friendinfo() { } void ipl::scene::AddressEdit::get_friendinfo() { - const NWC24FriendInfo& friendInfo = mpFriendCache->getInfo(mSelectedFriend); - memcpy(&sFriendInfo__Q23ipl5scene, &friendInfo, sizeof(sFriendInfo__Q23ipl5scene)); + memcpy(&sFriendInfo__Q23ipl5scene, &mpFriendCache->getInfo(mSelectedFriend), + sizeof(sFriendInfo__Q23ipl5scene)); mString.setName(reinterpret_cast(sFriendInfo__Q23ipl5scene.attr.name)); const wchar_t* name = mString.mName; nw4r::lyt::Pane* namePane = -- 2.49.1 From 15eeb6894ce6dced73205fe2e28b91de6592f551 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 00:32:11 +0000 Subject: [PATCH 09/34] match wad termination scan --- libs/RVL_SDK/src/wad/wad.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 83f1c334..65179306 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -452,10 +452,10 @@ static s32 WAD_815BFFA8(WADImportLoopArgs* args) { } s32 _WADGetCidxCount(const ESContentMask* contentMask) { - s32 bitIndex = 0; s32 count = 0; - s32 groupIndex; - s32 bitMask = 1; + u32 bitMask = 1; + u32 bitIndex = 0; + u32 groupIndex; for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { if ((contentMask->data[bitIndex >> 3] & (bitMask << (bitIndex & 7))) != 0) { @@ -1137,12 +1137,13 @@ s32 WADCheckSavedataZD(const WADSaveDataFile* saveData) { } #pragma dont_inline on -u32 _WADIsTerminated(const char* text, u32 maxLength) { - for (; maxLength > 0; maxLength--) { - if (*text == '\0') { +static u32 _WADIsTerminated(const char* text, u32 maxLength) { + u32 index; + + for (index = 0; index < maxLength; index++) { + if (text[index] == '\0') { return TRUE; } - text++; } return FALSE; } -- 2.49.1 From 0bde0266df21fe106f9962ee2ba12d0e60fe3f4b Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 00:38:59 +0000 Subject: [PATCH 10/34] decompile backup WAD header --- libs/RVL_SDK/src/wad/wad.c | 81 +++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 65179306..ffda9391 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -54,7 +54,7 @@ typedef struct WADUnpackInfo { u32 contentOffset; u32 metaSize; u32 metaOffset; - void* fileList; + u32 fileListSize; u32 fileOffset; u32 fileCount; void* contentIndex; @@ -127,6 +127,7 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* MEMAllocator* allocator, u32 offset, u32 flags); static s32 _WADUnpack(void* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, u32 offset, u32 flags, u32 mode); +static s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size); s32 _WADGetCidxCount(const ESContentMask* contentMask); static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber); static s32 _WADGetTransferId(void* transferId); @@ -357,7 +358,7 @@ s32 WADImportGetBlocks(char* path, MEMAllocator* allocator, WADLocation location contentMeta++; } } - if ((workspace.unpackInfo.fileList != 0) && + if ((workspace.unpackInfo.fileListSize != 0) && (workspace.unpackInfo.fileCount != 0)) { transferId = _WADGetTransferId(workspace.transferId); if ((fileListOut == 0) || @@ -1065,6 +1066,82 @@ static s32 _WADUnpackIRD(WADHeader* header, WADStream* stream, WADUnpackInfo* in return 0; } +static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* info, + MEMAllocator* allocator, u32 offset, u32 flags) { + WADBackupHeader* backupHeader = (WADBackupHeader*)header; + ESDeviceId currentDeviceId; + s32 result; + u32 sectionOffset; + u32 alignedSize; + + if (backupHeader->wadVersion != 1) { + return -3001; + } + if (backupHeader->hdrSize != sizeof(WADBackupHeader)) { + return -3001; + } + if (backupHeader->contentSize != 0) { + result = ES_GetDeviceId(¤tDeviceId); + if (result != 0) { + return result; + } + if (currentDeviceId != backupHeader->deviceId) { + return -3008; + } + } + + info->cidxMode = backupHeader->wadVersion; + if (backupHeader->wadVersion >= 1) { + info->contentIndex = &backupHeader->cidx; + } + info->fileNames = backupHeader->deviceMac; + sectionOffset = (backupHeader->hdrSize + WAD_STREAM_ALIGNMENT - 1) & + ~(WAD_STREAM_ALIGNMENT - 1); + + if (backupHeader->tmdSize != 0) { + if ((flags & 4) == 0) { + info->sectionSize = backupHeader->tmdSize; + info->sectionOffset = sectionOffset; + alignedSize = WAD_ALIGN32(backupHeader->tmdSize); + info->titleMeta = _WADMemAlloc(allocator, alignedSize); + if (info->titleMeta == 0) { + return -3003; + } + info->titleMetaSize = 1; + result = WADReadStream(stream, (void**)&info->titleMeta, alignedSize, + offset + info->sectionOffset); + if (result != alignedSize) { + return -3005; + } + result = 0; + } + sectionOffset += (backupHeader->tmdSize + WAD_STREAM_ALIGNMENT - 1) & + ~(WAD_STREAM_ALIGNMENT - 1); + if ((flags & 2) != 0) { + return 0; + } + } + + if (backupHeader->contentSize != 0) { + info->contentSize = backupHeader->contentSize; + info->contentOffset = sectionOffset; + sectionOffset += (backupHeader->contentSize + WAD_STREAM_ALIGNMENT - 1) & + ~(WAD_STREAM_ALIGNMENT - 1); + } + if (backupHeader->fileSize != 0) { + info->fileListSize = backupHeader->fileSize; + info->fileOffset = sectionOffset; + info->fileCount = backupHeader->numFiles; + if ((flags & 1) == 0) { + result = WADVerify(stream, allocator, offset, backupHeader->backupAreaLen); + if (result != 0) { + return result; + } + } + } + return 0; +} + typedef struct WADSaveDataRecord { u8 reserved_0x00[0x4E]; char fileName[8]; -- 2.49.1 From 443d8141e3e56a75f17523eebaca863121130468 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 00:41:41 +0000 Subject: [PATCH 11/34] decompile WAD unpack formats --- libs/RVL_SDK/src/wad/wad.c | 116 ++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 2 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index ffda9391..8fbb0d3f 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -29,6 +29,17 @@ typedef union WADTitleMetadata { u8 bytes[0x80]; } WADTitleMetadata; +typedef struct WADBroadOnHeader { + u32 headerSize; + u32 contentOffset; + u32 certSize; + u32 crlSize; + u32 ticketSize; + u32 titleMetaSize; + u32 contentSize; + u32 fileListSize; +} WADBroadOnHeader; + typedef struct WADUnpackInfo { s32 type; s32 headerInfo; @@ -121,7 +132,7 @@ static s32 _WADGetTitleVer(WADHeader* header, ESTitleId* titleId, u16* titleVers s32 headerInfo); static s32 _WADUnpackIRD(WADHeader* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, u32 offset, u32 mode); -static s32 _WADUnpackBroadOn(WADHeader* header, WADStream* stream, WADUnpackInfo* info, +static s32 _WADUnpackBroadOn(WADBroadOnHeader* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, u32 offset, u32 mode); static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, u32 offset, u32 flags); @@ -944,7 +955,8 @@ static s32 _WADUnpack(void* header, WADStream* stream, WADUnpackInfo* info, MEMA result = _WADUnpackBackup(header, stream, info, allocator, offset, flags); } } else if (type == 1) { - result = _WADUnpackBroadOn(header, stream, info, allocator, offset, mode); + result = _WADUnpackBroadOn((WADBroadOnHeader*)header, stream, info, allocator, offset, + mode); } else { result = -3001; } @@ -1066,6 +1078,106 @@ static s32 _WADUnpackIRD(WADHeader* header, WADStream* stream, WADUnpackInfo* in return 0; } +static s32 _WADUnpackBroadOn(WADBroadOnHeader* header, WADStream* stream, WADUnpackInfo* info, + MEMAllocator* allocator, u32 offset, u32 mode) { + u32 currentOffset; + s32 sectionSize; + s32 alignedSize; + s32 bytesRead; + + if ((header->crlSize == 0) && (header->ticketSize == 0)) { + return -3001; + } + + currentOffset = header->headerSize; + sectionSize = header->certSize; + if (sectionSize != 0) { + info->size_0x0c = sectionSize; + info->offset_0x10 = currentOffset; + if (mode == 0) { + alignedSize = WAD_ALIGN32(sectionSize); + info->buffer_0x14 = _WADMemAlloc(allocator, alignedSize); + if (info->buffer_0x14 == 0) { + return -3003; + } + info->size_0x18 = 1; + bytesRead = WADReadStream(stream, &info->buffer_0x14, alignedSize, + offset + info->offset_0x10); + if (bytesRead != alignedSize) { + return -3005; + } + } + currentOffset += sectionSize; + } + + sectionSize = header->crlSize; + if (sectionSize != 0) { + info->size_0x2c = sectionSize; + info->offset_0x30 = currentOffset; + if (mode == 0) { + alignedSize = WAD_ALIGN32(sectionSize); + info->buffer_0x34 = _WADMemAlloc(allocator, alignedSize); + if (info->buffer_0x34 == 0) { + return -3003; + } + info->size_0x38 = 1; + bytesRead = WADReadStream(stream, &info->buffer_0x34, alignedSize, + offset + info->offset_0x30); + if (bytesRead != alignedSize) { + return -3005; + } + } + currentOffset += sectionSize; + } + + sectionSize = header->ticketSize; + if (sectionSize != 0) { + info->sectionSize = sectionSize; + info->sectionOffset = currentOffset; + alignedSize = WAD_ALIGN32(sectionSize); + info->titleMeta = _WADMemAlloc(allocator, alignedSize); + if (info->titleMeta == 0) { + return -3003; + } + info->titleMetaSize = 1; + bytesRead = WADReadStream(stream, (void**)&info->titleMeta, alignedSize, + offset + info->sectionOffset); + if (bytesRead != alignedSize) { + return -3005; + } + currentOffset += sectionSize; + } + + sectionSize = header->titleMetaSize; + if (sectionSize != 0) { + info->metaSize = sectionSize; + info->metaOffset = currentOffset; + currentOffset += sectionSize; + } + + sectionSize = header->fileListSize; + if (sectionSize != 0) { + info->size_0x1c = sectionSize; + info->offset_0x20 = currentOffset; + if (mode == 0) { + alignedSize = WAD_ALIGN32(sectionSize); + info->buffer_0x24 = _WADMemAlloc(allocator, alignedSize); + if (info->buffer_0x24 == 0) { + return -3003; + } + info->size_0x28 = 1; + bytesRead = WADReadStream(stream, &info->buffer_0x24, alignedSize, + offset + info->offset_0x20); + if (bytesRead != alignedSize) { + return -3005; + } + } + } + + info->contentOffset = header->contentOffset; + return 0; +} + static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, u32 offset, u32 flags) { WADBackupHeader* backupHeader = (WADBackupHeader*)header; -- 2.49.1 From da16fdd16f8b18356ae413064986613540b4bb63 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 00:44:59 +0000 Subject: [PATCH 12/34] decompile wad export worker --- libs/RVL_SDK/src/wad/wad.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 8fbb0d3f..4e56b718 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -108,8 +108,8 @@ typedef struct WADImportTransfer { BOOL ready[2]; void* buffers[2]; OSMutex mutex[2]; - OSCond signalCond[2]; OSCond waitCond[2]; + OSCond signalCond[2]; } WADImportTransfer; typedef struct WADImportLoopArgs { @@ -118,6 +118,8 @@ typedef struct WADImportLoopArgs { WADImportTransfer* transfer; } WADImportLoopArgs; +typedef WADImportLoopArgs WADExportLoopArgs; + #define WAD_STREAM_ALIGNMENT 0x40 #define WAD_READ_ALIGNMENT 0x20 #define WAD_ALIGN32(value) (((value) + WAD_READ_ALIGNMENT - 1) & ~(WAD_READ_ALIGNMENT - 1)) @@ -463,6 +465,38 @@ static s32 WAD_815BFFA8(WADImportLoopArgs* args) { return result; } +s32 WAD_815C1288(WADExportLoopArgs* args) { + ESFd fd = args->fd; + s32 result = 0; + u32 remaining = args->size; + u32 bufferIndex = 0; + WADImportTransfer* transfer = args->transfer; + + while ((remaining != 0) && (result == 0)) { + u32 size = remaining; + OSMutex* mutex; + + if (transfer->chunkSize < remaining) { + size = transfer->chunkSize; + } + mutex = &transfer->mutex[bufferIndex]; + OSLockMutex(mutex); + while (transfer->ready[bufferIndex] != 0) { + OSWaitCond(&transfer->waitCond[bufferIndex], mutex); + } + result = ES_ExportContentData(fd, transfer->buffers[bufferIndex], size); + transfer->ready[bufferIndex] = size; + if (result != 0) { + transfer->error = 1; + } + OSUnlockMutex(mutex); + OSSignalCond(&transfer->signalCond[bufferIndex]); + remaining -= size; + bufferIndex ^= 1; + } + return result; +} + s32 _WADGetCidxCount(const ESContentMask* contentMask) { s32 count = 0; u32 bitMask = 1; -- 2.49.1 From 47afa75449b0b587d8409ea897643a50573d44b6 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 00:46:38 +0000 Subject: [PATCH 13/34] refine content index scan --- libs/RVL_SDK/src/wad/wad.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 4e56b718..be15da76 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -499,27 +499,23 @@ s32 WAD_815C1288(WADExportLoopArgs* args) { s32 _WADGetCidxCount(const ESContentMask* contentMask) { s32 count = 0; - u32 bitMask = 1; u32 bitIndex = 0; u32 groupIndex; for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { - if ((contentMask->data[bitIndex >> 3] & (bitMask << (bitIndex & 7))) != 0) { + if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { count++; } - bitIndex++; - if ((contentMask->data[bitIndex >> 3] & (bitMask << (bitIndex & 7))) != 0) { + if ((contentMask->data[(bitIndex + 1) >> 3] & (1 << ((bitIndex + 1) & 7))) != 0) { count++; } - bitIndex++; - if ((contentMask->data[bitIndex >> 3] & (bitMask << (bitIndex & 7))) != 0) { + if ((contentMask->data[(bitIndex + 2) >> 3] & (1 << ((bitIndex + 2) & 7))) != 0) { count++; } - bitIndex++; - if ((contentMask->data[bitIndex >> 3] & (bitMask << (bitIndex & 7))) != 0) { + if ((contentMask->data[(bitIndex + 3) >> 3] & (1 << ((bitIndex + 3) & 7))) != 0) { count++; } - bitIndex++; + bitIndex += 4; } return count; } -- 2.49.1 From ef2a65cec5c978a734320668b1ade3cbc7b9de4f Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 00:54:15 +0000 Subject: [PATCH 14/34] decompile WAD signature verification --- libs/RVL_SDK/src/wad/wad.c | 133 +++++++++++++++++++++++++++++++++---- 1 file changed, 121 insertions(+), 12 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index be15da76..7281af36 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -85,6 +85,28 @@ typedef struct WADFileEntry { char name[0x40]; } WADFileEntry; +typedef struct WADBackupSignature { + u8 header[0x40]; + u8 firstCertificate[0x180]; + u8 secondCertificate[0x180]; +} WADBackupSignature; + +typedef struct WADVerificationCertificateBundle { + u8 caProduction[0x400]; + u8 msProduction[0x240]; + u8 caDevelopment[0x400]; + u8 msDevelopment[0x240]; + u8 firstCertificate[0x180]; + u8 secondCertificate[0x180]; +} WADVerificationCertificateBundle; + +typedef struct __attribute__((aligned(64))) WADVerificationWorkspace { + u8 reserved_0x00[0x40]; + void* readBuffer[16]; + u8 digest[0x40]; + u8 hashContext[0xC0]; +} WADVerificationWorkspace; + #pragma pack(push, 4) typedef struct WADFileMetadataView { u8 reserved_0x00[0x18C]; @@ -140,7 +162,9 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* MEMAllocator* allocator, u32 offset, u32 flags); static s32 _WADUnpack(void* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, u32 offset, u32 flags, u32 mode); -static s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size); +static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void* buffer, + u32 bufferSize, u32 flags, u32 mode, u32 chunkSize); +s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size); s32 _WADGetCidxCount(const ESContentMask* contentMask); static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber); static s32 _WADGetTransferId(void* transferId); @@ -526,34 +550,31 @@ static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber) { contentNumber++; for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { - if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + if ((contentMask->data[(s32)bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { contentNumber--; } if (contentNumber == 0) { return bitIndex; } - bitIndex++; - if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + if ((contentMask->data[(s32)(bitIndex + 1) >> 3] & (1 << ((bitIndex + 1) & 7))) != 0) { contentNumber--; } if (contentNumber == 0) { - return bitIndex; + return bitIndex + 1; } - bitIndex++; - if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + if ((contentMask->data[(s32)(bitIndex + 2) >> 3] & (1 << ((bitIndex + 2) & 7))) != 0) { contentNumber--; } if (contentNumber == 0) { - return bitIndex; + return bitIndex + 2; } - bitIndex++; - if ((contentMask->data[bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { + if ((contentMask->data[(s32)(bitIndex + 3) >> 3] & (1 << ((bitIndex + 3) & 7))) != 0) { contentNumber--; } if (contentNumber == 0) { - return bitIndex; + return bitIndex + 3; } - bitIndex++; + bitIndex += 4; } return -1; } @@ -1355,6 +1376,94 @@ s32 WADCheckSavedataZD(const WADSaveDataFile* saveData) { return TRUE; } +extern const u8 ca_ppki[]; +extern const u8 ms_ppki[]; +extern const u8 ca_dpki[]; +extern const u8 ms_dpki[]; +extern s32 SHA1Reset(void* context); +extern s32 SHA1Result(void* context, void* digest); + +s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size) { + WADVerificationWorkspace workspace; + void* verificationBuffer = 0; + WADBackupSignature* backupSignature; + WADVerificationCertificateBundle* certificateBundle; + s32 result; + + workspace.readBuffer[0] = 0; + if (size != WAD_ALIGN32(size)) { + result = -3000; + } else if (size <= 0x340) { + result = -3000; + } else { + workspace.readBuffer[0] = _WADMemAlloc(allocator, 0x8000); + if (workspace.readBuffer[0] == 0) { + result = -3003; + } else if (((u32)workspace.readBuffer[0] & 0x3F) != 0) { + result = -3007; + } else { + u32 hashSize = size - 0x340; + + SHA1Reset(workspace.hashContext); + result = _WADHash(stream, offset, hashSize, workspace.hashContext, + workspace.readBuffer[0], 0x8000, 0, 0, 0x1000); + if (result == 0) { + result = SHA1Result(workspace.hashContext, workspace.digest); + if (result == 0) { + result = WADReadStream(stream, workspace.readBuffer, 0x340, + offset + hashSize); + if (result == 0x340) { + verificationBuffer = _WADMemAlloc(allocator, 0xF80); + if (verificationBuffer == 0) { + result = -3003; + } else { + backupSignature = workspace.readBuffer[0]; + certificateBundle = verificationBuffer; + memcpy(certificateBundle->caProduction, ca_ppki, 0x400); + memcpy(certificateBundle->msProduction, ms_ppki, 0x240); + memcpy(certificateBundle->caDevelopment, ca_dpki, 0x400); + memcpy(certificateBundle->msDevelopment, ms_dpki, 0x240); + memcpy(certificateBundle->firstCertificate, + backupSignature->firstCertificate, 0x180); + memcpy(certificateBundle->secondCertificate, + backupSignature->secondCertificate, 0x180); + result = ES_VerifySign(workspace.digest, 0x14, + workspace.readBuffer[0], verificationBuffer, + 0xF80); + } + } else { + result = -3005; + } + } + } + } + } + + if (workspace.readBuffer[0] != 0) { + _WADMemFree(allocator, workspace.readBuffer[0]); + } + if (verificationBuffer != 0) { + _WADMemFree(allocator, verificationBuffer); + } + return result; +} + +static void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBuffer, + u32 chunkSize) { + transfer->chunkSize = chunkSize; + transfer->error = 0; + transfer->ready[1] = 0; + transfer->ready[0] = 0; + transfer->buffers[0] = firstBuffer; + transfer->buffers[1] = secondBuffer; + OSInitMutex(&transfer->mutex[0]); + OSInitMutex(&transfer->mutex[1]); + OSInitCond(&transfer->waitCond[0]); + OSInitCond(&transfer->waitCond[1]); + OSInitCond(&transfer->signalCond[0]); + OSInitCond(&transfer->signalCond[1]); +} + #pragma dont_inline on static u32 _WADIsTerminated(const char* text, u32 maxLength) { u32 index; -- 2.49.1 From 2faf199db4360a4d9098e0f0edbf268504f1767f Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:18:33 +0000 Subject: [PATCH 15/34] decompile wad hash path --- libs/RVL_SDK/src/wad/wad.c | 229 ++++++++++++++++++++++++++++++++----- 1 file changed, 202 insertions(+), 27 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 7281af36..e9b38871 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -15,7 +15,6 @@ typedef union WADStreamHandle { CNTFileInfoNAND contentNand; FAFILE* fa; void* memoryBase; - u8 storage[0xB8]; } WADStreamHandle; typedef struct __attribute__((aligned(32))) WADStream { @@ -100,8 +99,7 @@ typedef struct WADVerificationCertificateBundle { u8 secondCertificate[0x180]; } WADVerificationCertificateBundle; -typedef struct __attribute__((aligned(64))) WADVerificationWorkspace { - u8 reserved_0x00[0x40]; +typedef struct WADVerificationWorkspace { void* readBuffer[16]; u8 digest[0x40]; u8 hashContext[0xC0]; @@ -142,6 +140,12 @@ typedef struct WADImportLoopArgs { typedef WADImportLoopArgs WADExportLoopArgs; +typedef struct WADHashThreadArgs { + void* context; + u32 size; + WADImportTransfer* transfer; +} WADHashThreadArgs; + #define WAD_STREAM_ALIGNMENT 0x40 #define WAD_READ_ALIGNMENT 0x20 #define WAD_ALIGN32(value) (((value) + WAD_READ_ALIGNMENT - 1) & ~(WAD_READ_ALIGNMENT - 1)) @@ -163,7 +167,9 @@ static s32 _WADUnpackBackup(WADHeader* header, WADStream* stream, WADUnpackInfo* static s32 _WADUnpack(void* header, WADStream* stream, WADUnpackInfo* info, MEMAllocator* allocator, u32 offset, u32 flags, u32 mode); static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void* buffer, - u32 bufferSize, u32 flags, u32 mode, u32 chunkSize); + u32 chunkSize, void* secondBuffer, void* threadStack, u32 threadStackSize); +static void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBuffer, + u32 chunkSize); s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size); s32 _WADGetCidxCount(const ESContentMask* contentMask); static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber); @@ -615,6 +621,7 @@ static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, co #pragma dont_inline on static void* _WADMemAlloc(MEMAllocator* allocator, u32 size) { + s32 allocationKind; u32 heapType; void* buffer; @@ -623,9 +630,25 @@ static void* _WADMemAlloc(MEMAllocator* allocator, u32 size) { } heapType = ((MEMiHeapHead*)allocator->heap)->magic; if (heapType == 0x46524D48) { + allocationKind = 1; + } else { + if (heapType < 0x46524D48) { + if (heapType == 0x45585048) { + allocationKind = 0; + goto allocate; + } + } else if (heapType == 0x554E5448) { + allocationKind = 2; + goto allocate; + } + allocationKind = 3; + } + +allocate: + if (allocationKind == 1) { return MEMAllocFromFrmHeapEx(allocator->heap, size, 0x40); } - if (heapType == 0x45585048) { + if (allocationKind == 0) { return MEMAllocFromExpHeapEx(allocator->heap, size, 0x40); } buffer = MEMAllocFromAllocator(allocator, size); @@ -638,8 +661,12 @@ static void* _WADMemAlloc(MEMAllocator* allocator, u32 size) { } static void _WADMemFree(MEMAllocator* allocator, void* buffer) { - if ((allocator != 0) && (allocator->heap != 0) && (buffer != 0)) { - MEMFreeToAllocator(allocator, buffer); + if (allocator != 0) { + if (allocator->heap != 0) { + if (buffer != 0) { + MEMFreeToAllocator(allocator, buffer); + } + } } } #pragma dont_inline reset @@ -1328,52 +1355,57 @@ typedef struct WADSaveDataFile { } WADSaveDataFile; s32 WADCheckSavedataZD(const WADSaveDataFile* saveData) { - const WADSaveDataRecord* record; + const WADSaveDataRecord* record = saveData->primary; u32 index; + s32 result = FALSE; - for (index = 0; index < 3; index++) { - record = &saveData->primary[index]; + index = 0; + do { if (!_WADIsTerminated(record->fileName, 8)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->bannerName, 8)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->saveName, 8)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->titleName, 8)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->description, 0x11)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->subtitle, 0x11)) { - return FALSE; + goto done; } - } - for (index = 0; index < 3; index++) { - record = &saveData->secondary[index]; + index++; + record++; + } while (index < 3); + record = saveData->secondary; + for (index = 0; index < 3; index++, record++) { if (!_WADIsTerminated(record->fileName, 8)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->bannerName, 8)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->saveName, 8)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->titleName, 8)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->description, 0x11)) { - return FALSE; + goto done; } if (!_WADIsTerminated(record->subtitle, 0x11)) { - return FALSE; + goto done; } } - return TRUE; + result = TRUE; +done: + return result; } extern const u8 ca_ppki[]; @@ -1383,8 +1415,151 @@ extern const u8 ms_dpki[]; extern s32 SHA1Reset(void* context); extern s32 SHA1Result(void* context, void* digest); +static s32 WAD_815C43E0(WADHashThreadArgs* args) { + void* context; + s32 result; + u32 remaining; + u32 bufferIndex; + WADImportTransfer* transfer; + + context = args->context; + result = 0; + remaining = args->size; + bufferIndex = 0; + transfer = args->transfer; + + while ((remaining != 0) && (result == 0)) { + u32 size = remaining; + OSMutex* mutex; + + if (transfer->chunkSize < remaining) { + size = transfer->chunkSize; + } + mutex = &transfer->mutex[bufferIndex]; + OSLockMutex(mutex); + while (transfer->ready[bufferIndex] == 0) { + OSWaitCond(&transfer->signalCond[bufferIndex], mutex); + } + result = SHA1Input(context, transfer->buffers[bufferIndex], size); + transfer->ready[bufferIndex] = 0; + if (result != 0) { + transfer->error = 1; + } + OSUnlockMutex(mutex); + OSSignalCond(&transfer->waitCond[bufferIndex]); + remaining -= size; + bufferIndex ^= 1; + } + return result; +} + +static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void* buffer, + u32 chunkSize, void* secondBuffer, void* threadStack, u32 threadStackSize) { + s32 result = 0; + + if ((stream == 0) || (context == 0) || (buffer == 0)) { + return -3000; + } + if (size != WAD_ALIGN32(size)) { + return -3000; + } + if (((u32)buffer & 0x1F) != 0) { + return -3007; + } + if (chunkSize != WAD_ALIGN32(chunkSize)) { + return -3000; + } + if ((secondBuffer == 0) || (threadStack == 0)) { + u32 completed = 0; + + while (size != 0) { + void* readBuffer = buffer; + u32 readSize = size; + + if (chunkSize < size) { + readSize = chunkSize; + } + result = WADReadStream(stream, &readBuffer, readSize, offset + completed); + if ((u32)result != readSize) { + result = -3005; + break; + } + result = SHA1Input(context, readBuffer, readSize); + if (result != 0) { + break; + } + completed += readSize; + size -= readSize; + } + } else { + WADImportTransfer transfer; + WADHashThreadArgs args; + OSThread thread; + s32 threadResult = 0; + u32 completed = 0; + u32 bufferIndex = 0; + BOOL readFailed = FALSE; + + WAD_815C4A2C(&transfer, buffer, secondBuffer, chunkSize); + args.context = context; + args.size = size; + args.transfer = &transfer; + if (!OSCreateThread(&thread, (void* (*)(void*))WAD_815C43E0, &args, + (u8*)threadStack + threadStackSize, threadStackSize, + OSGetThreadPriority(OSGetCurrentThread()), 0)) { + return -3009; + } + OSResumeThread(&thread); + while ((size != 0) && (transfer.error == 0)) { + void* readBuffer; + u32 readSize = size; + OSMutex* mutex = &transfer.mutex[bufferIndex]; + + if (chunkSize < size) { + readSize = chunkSize; + } + OSLockMutex(mutex); + while ((transfer.ready[bufferIndex] != 0) && (transfer.error == 0)) { + OSWaitCond(&transfer.waitCond[bufferIndex], mutex); + } + if (transfer.error != 0) { + OSUnlockMutex(mutex); + break; + } + readBuffer = transfer.buffers[bufferIndex]; + result = WADReadStream(stream, &readBuffer, readSize, offset + completed); + if ((u32)result != readSize) { + OSMutex* otherMutex = &transfer.mutex[bufferIndex ^ 1]; + + OSLockMutex(otherMutex); + OSCancelThread(&thread); + OSJoinThread(&thread, 0); + OSUnlockMutex(otherMutex); + OSUnlockMutex(mutex); + result = -3005; + readFailed = TRUE; + break; + } + transfer.ready[bufferIndex] = readSize; + OSUnlockMutex(mutex); + OSSignalCond(&transfer.signalCond[bufferIndex]); + completed += readSize; + size -= readSize; + bufferIndex ^= 1; + } + if (!readFailed) { + if (!OSJoinThread(&thread, &threadResult)) { + result = -3009; + } else { + result = threadResult; + } + } + } + return result; +} + s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size) { - WADVerificationWorkspace workspace; + WADVerificationWorkspace workspace ALIGN64; void* verificationBuffer = 0; WADBackupSignature* backupSignature; WADVerificationCertificateBundle* certificateBundle; -- 2.49.1 From 5074acbfe66c1abe9e551bebfa09b6b667cc15a0 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:19:34 +0000 Subject: [PATCH 16/34] match savedata validation --- libs/RVL_SDK/src/wad/wad.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index e9b38871..e2b03ede 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -1355,10 +1355,11 @@ typedef struct WADSaveDataFile { } WADSaveDataFile; s32 WADCheckSavedataZD(const WADSaveDataFile* saveData) { - const WADSaveDataRecord* record = saveData->primary; - u32 index; s32 result = FALSE; + u32 index; + const WADSaveDataRecord* record; + record = saveData->primary; index = 0; do { if (!_WADIsTerminated(record->fileName, 8)) { -- 2.49.1 From f1119195cf590c4cb5c4d3f9d0ba0f5808a94d20 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:23:01 +0000 Subject: [PATCH 17/34] match wad title metadata query --- libs/RVL_SDK/src/wad/wad.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index e2b03ede..d484bd4e 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -185,15 +185,15 @@ u32 _WADIsTerminated(const char* text, u32 maxLength); extern s32 ES_GetBoot2Version(u32* version); s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADLocation location, u32 offset) { + BOOL streamOpened; WADStream stream ALIGN32; - WADTitleMetadata metadata; - u8 headerBytes[0x20]; + u8 headerBytes[0x20] ALIGN32; + WADTitleMetadata metadata ALIGN32; s32 headerInfo; WADTitleMetadata* metadataBuffer = &metadata; WADHeader* headerBuffer = (WADHeader*)headerBytes; s32 result; u32 titleDataOffset; - BOOL streamOpened; s32 type; streamOpened = FALSE; -- 2.49.1 From f20242a2b158250d43881cebffabc94c3af07b73 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:25:00 +0000 Subject: [PATCH 18/34] match BroadOn unpack offsets --- libs/RVL_SDK/src/wad/wad.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index d484bd4e..24fe4c1d 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -1185,7 +1185,7 @@ static s32 _WADUnpackBroadOn(WADBroadOnHeader* header, WADStream* stream, WADUnp return -3005; } } - currentOffset += sectionSize; + currentOffset += header->certSize; } sectionSize = header->crlSize; @@ -1205,7 +1205,7 @@ static s32 _WADUnpackBroadOn(WADBroadOnHeader* header, WADStream* stream, WADUnp return -3005; } } - currentOffset += sectionSize; + currentOffset += header->crlSize; } sectionSize = header->ticketSize; @@ -1223,14 +1223,14 @@ static s32 _WADUnpackBroadOn(WADBroadOnHeader* header, WADStream* stream, WADUnp if (bytesRead != alignedSize) { return -3005; } - currentOffset += sectionSize; + currentOffset += header->ticketSize; } sectionSize = header->titleMetaSize; if (sectionSize != 0) { info->metaSize = sectionSize; info->metaOffset = currentOffset; - currentOffset += sectionSize; + currentOffset += header->titleMetaSize; } sectionSize = header->fileListSize; -- 2.49.1 From f8d35cc7141b88ac61ff9610319b8d79cdf1f77a Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:31:45 +0000 Subject: [PATCH 19/34] tune WAD export chunk comparison --- libs/RVL_SDK/src/wad/wad.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 24fe4c1d..62998f07 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -506,7 +506,7 @@ s32 WAD_815C1288(WADExportLoopArgs* args) { u32 size = remaining; OSMutex* mutex; - if (transfer->chunkSize < remaining) { + if (remaining > transfer->chunkSize) { size = transfer->chunkSize; } mutex = &transfer->mutex[bufferIndex]; -- 2.49.1 From 20ba04b362ed9c26db8cbce5daae34b6f20ccdce Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:34:03 +0000 Subject: [PATCH 20/34] reuse WAD content index offsets --- libs/RVL_SDK/src/wad/wad.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 62998f07..9f8f19d9 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -553,6 +553,7 @@ s32 _WADGetCidxCount(const ESContentMask* contentMask) { static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber) { u32 bitIndex = 0; u32 groupIndex; + u32 nextIndex; contentNumber++; for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { @@ -562,23 +563,26 @@ static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber) { if (contentNumber == 0) { return bitIndex; } - if ((contentMask->data[(s32)(bitIndex + 1) >> 3] & (1 << ((bitIndex + 1) & 7))) != 0) { + nextIndex = bitIndex + 1; + if ((contentMask->data[(s32)nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { contentNumber--; } if (contentNumber == 0) { - return bitIndex + 1; + return nextIndex; } - if ((contentMask->data[(s32)(bitIndex + 2) >> 3] & (1 << ((bitIndex + 2) & 7))) != 0) { + nextIndex++; + if ((contentMask->data[(s32)nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { contentNumber--; } if (contentNumber == 0) { - return bitIndex + 2; + return nextIndex; } - if ((contentMask->data[(s32)(bitIndex + 3) >> 3] & (1 << ((bitIndex + 3) & 7))) != 0) { + nextIndex++; + if ((contentMask->data[(s32)nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { contentNumber--; } if (contentNumber == 0) { - return bitIndex + 3; + return nextIndex; } bitIndex += 4; } -- 2.49.1 From a6235b6018014dc546813953b4ea09a8d6e059cd Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:40:15 +0000 Subject: [PATCH 21/34] tune WAD hash chunk comparison --- libs/RVL_SDK/src/wad/wad.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 9f8f19d9..03467c72 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -1437,7 +1437,7 @@ static s32 WAD_815C43E0(WADHashThreadArgs* args) { u32 size = remaining; OSMutex* mutex; - if (transfer->chunkSize < remaining) { + if (remaining > transfer->chunkSize) { size = transfer->chunkSize; } mutex = &transfer->mutex[bufferIndex]; -- 2.49.1 From fb81577a0fa6c2cc15aa3a8d6b209d9013c664b3 Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:44:43 +0000 Subject: [PATCH 22/34] Link fa/pf_api_util + pf_w_api_util (clean subset) (#245) * fa: link pf_api_util + pf_w_api_util (genuine implementations, objdiff 100%) Co-Authored-By: wch * configure: flip fa/pf_api_util + fa/pf_w_api_util to Matching Co-Authored-By: wch --------- Co-authored-by: devin Co-authored-by: wch --- configure.py | 4 +- libs/RVL_SDK/src/fa/pf_api_util.c | 127 ++++++++++++++++++++++++++++ libs/RVL_SDK/src/fa/pf_w_api_util.c | 53 ++++++++++++ 3 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 libs/RVL_SDK/src/fa/pf_api_util.c create mode 100644 libs/RVL_SDK/src/fa/pf_w_api_util.c diff --git a/configure.py b/configure.py index 6bbcff16..52f48215 100644 --- a/configure.py +++ b/configure.py @@ -1707,7 +1707,7 @@ config.libs = [ Object(NonMatching, "fa/pf_volume.c"), Object(NonMatching, "fa/pf_volume_apiadd.c"), Object(NonMatching, "fa/pf_cp932.c"), - Object(NonMatching, "fa/pf_api_util.c"), + Object(Matching, "fa/pf_api_util.c"), Object(NonMatching, "fa/pf_attach.c"), Object(NonMatching, "fa/pf_buffering.c"), Object(NonMatching, "fa/pf_cdelete.c"), @@ -1774,7 +1774,7 @@ config.libs = [ Object(NonMatching, "fa/pf_unmount.c"), Object(NonMatching, "fa/pf_unregctx.c"), Object(NonMatching, "fa/pf_xdivide.c"), - Object(NonMatching, "fa/pf_w_api_util.c"), + Object(Matching, "fa/pf_w_api_util.c"), Object(NonMatching, "fa/pf_w_cdelete.c"), Object(NonMatching, "fa/pf_w_chdir.c"), Object(NonMatching, "fa/pf_w_chdmod.c"), diff --git a/libs/RVL_SDK/src/fa/pf_api_util.c b/libs/RVL_SDK/src/fa/pf_api_util.c new file mode 100644 index 00000000..f70a9f9b --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_api_util.c @@ -0,0 +1,127 @@ +#include +#include + +static s32 pf_error_to_api_error[] = {FA_ERR_SUCCESS, FA_ERR_EINVAL, FA_ERR_EINVAL, FA_ERR_ENOENT, FA_ERR_EBUSY, FA_ERR_ENOTEMPTY, FA_ERR_ENOSPC, + FA_ERR_ENOEXEC, FA_ERR_EEXIST, FA_ERR_ENOEXEC, FA_ERR_EINVAL, FA_ERR_EWRTPROTECT, FA_ERR_ENOSYS, FA_ERR_ENOEXEC, + FA_ERR_ENOEXEC, FA_ERR_ENOEXEC, FA_ERR_EACCES, FA_ERR_EIO, FA_ERR_ENOEXEC, FA_ERR_EACCES, FA_ERR_ENOENT, + FA_ERR_ENFILE, FA_ERR_EMFILE, FA_ERR_EISDIR, FA_ERR_EACCES, FA_ERR_EPERM, FA_ERR_ENOEXEC, FA_ERR_ENOEXEC, + FA_ERR_ENOEXEC, FA_ERR_ENOEXEC, FA_ERR_ENOMEM, FA_ERR_EINVAL, FA_ERR_EINVAL, FA_ERR_ENOEXEC, FA_ERR_ENOENT, + FA_ERR_ENOEXEC, FA_ERR_ENOEXEC, FA_ERR_EFBIG, FA_ERR_EBADF, FA_ERR_ENOLCK}; + +s32 PFAPI_ParseOpenModeString(const char* mode_str) { + s32 open_mode = 0; + u32 i = 0; + + if (mode_str == NULL) { + open_mode = 10; + return open_mode; + } + + switch (mode_str[i++]) { + case 'r': { + open_mode = 2; + break; + } + case 'w': { + open_mode = 1; + break; + } + case 'a': { + open_mode = 4; + break; + } + default: { + return 0; + } + } + + if (mode_str[i] == 'b') { + i++; + } + switch (mode_str[i++]) { + case 0: { + return open_mode; + } + case 't': + default: { + return 0; + } + case '+': { + switch (mode_str[i]) { + case 0: { + return open_mode | 8; + } + default: { + return 0; + } + } + } + } + return 0; +} + +s32 PFAPI_convertError(s32 err) { + if (err == 0) { + return 0; + } + if (err == -1) { + return -1; + } + if (err > 0 && err < 0xA0) { + return pf_error_to_api_error[err]; + } + if (err == 0x1000) { + err = FA_ERR_EIO; + } + return err; +} + +s32 PFAPI_convertDriverError(s32 err) { + if (err == 0) { + return 0; + } + if (err <= 0) { + return err; + } + if ((u32)err < 0xA0) { + err = 1; + } + return err; +} + +s32 PFAPI_convertReturnValue(s32 err) { + if (err == 0) { + return 0; + } + return -1; +} + +s32 PFAPI_convertReturnValue4feof(s32 err) { + if (err == 1) { + return 1; + } + if (err == 0) { + return 0; + } + return -1; +} + +void* PFAPI_convertReturnValue2NULL(s32 err, void* p_stream) { + void* ret; + if (err) { + ret = NULL; + } else { + ret = p_stream; + } + return ret; +} + +s32 PFAPI_convertReturnValue4unmount(s32 err) { + if (err == 0) { + return 0; + } + if (err == 1) { + return 1; + } + return -1; +} diff --git a/libs/RVL_SDK/src/fa/pf_w_api_util.c b/libs/RVL_SDK/src/fa/pf_w_api_util.c new file mode 100644 index 00000000..cf7a7b2c --- /dev/null +++ b/libs/RVL_SDK/src/fa/pf_w_api_util.c @@ -0,0 +1,53 @@ +#include + +s32 PFWAPI_ParseOpenModeString(const unsigned short* mode_str) { + s32 open_mode = 0; + u32 i = 0; + + if (mode_str == NULL) { + open_mode = 10; + return open_mode; + } + + switch (mode_str[i++]) { + case 'r': { + open_mode = 2; + break; + } + case 'w': { + open_mode = 1; + break; + } + case 'a': { + open_mode = 4; + break; + } + default: { + return 0; + } + } + + if (mode_str[i] == 'b') { + i++; + } + switch (mode_str[i++]) { + case 0: { + return open_mode; + } + case 't': + default: { + return 0; + } + case '+': { + switch (mode_str[i]) { + case 0: { + return open_mode | 8; + } + default: { + return 0; + } + } + } + } + return 0; +} -- 2.49.1 From 1faf16bb362cc1d8afcb0b0b152ef1e7de5e3643 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:53:41 +0000 Subject: [PATCH 23/34] tune WAD content index lookup --- libs/RVL_SDK/src/wad/wad.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 03467c72..782b5510 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -172,7 +172,7 @@ static void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* s u32 chunkSize); s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size); s32 _WADGetCidxCount(const ESContentMask* contentMask); -static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber); +static s32 _WADGetCidx(const ESContentMask* contentMask, u32 contentNumber); static s32 _WADGetTransferId(void* transferId); static void* _WADMemAlloc(MEMAllocator* allocator, u32 size); static void _WADMemFree(MEMAllocator* allocator, void* buffer); @@ -550,41 +550,41 @@ s32 _WADGetCidxCount(const ESContentMask* contentMask) { return count; } -static s32 _WADGetCidx(const ESContentMask* contentMask, s32 contentNumber) { +static s32 _WADGetCidx(const ESContentMask* contentMask, u32 contentNumber) { + u32 remaining = contentNumber + 1; u32 bitIndex = 0; u32 groupIndex; u32 nextIndex; - contentNumber++; for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { if ((contentMask->data[(s32)bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { - contentNumber--; + remaining--; } - if (contentNumber == 0) { + if (remaining == 0) { return bitIndex; } - nextIndex = bitIndex + 1; + nextIndex = ++bitIndex; if ((contentMask->data[(s32)nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { - contentNumber--; + remaining--; } - if (contentNumber == 0) { + if (remaining == 0) { return nextIndex; } - nextIndex++; + nextIndex = ++bitIndex; if ((contentMask->data[(s32)nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { - contentNumber--; + remaining--; } - if (contentNumber == 0) { + if (remaining == 0) { return nextIndex; } - nextIndex++; + nextIndex = ++bitIndex; if ((contentMask->data[(s32)nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { - contentNumber--; + remaining--; } - if (contentNumber == 0) { + if (remaining == 0) { return nextIndex; } - bitIndex += 4; + bitIndex++; } return -1; } -- 2.49.1 From 255aa56b83da88876fe68dff0bc15ecc5a9036ce Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:59:03 +0000 Subject: [PATCH 24/34] match WAD transfer metadata helpers --- libs/RVL_SDK/src/wad/wad.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 782b5510..a76680eb 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -589,6 +589,7 @@ static s32 _WADGetCidx(const ESContentMask* contentMask, u32 contentNumber) { return -1; } +#pragma dont_inline on static s32 _WADGetTransferId(void* transferId) { NANDFileInfo fileInfo; BOOL valid; @@ -597,8 +598,11 @@ static s32 _WADGetTransferId(void* transferId) { valid = FALSE; result = NANDPrivateOpen("/shared2/succession/transfer.id", &fileInfo, 1); if (result == 0) { + valid = TRUE; result = NANDRead(&fileInfo, transferId, 0x20); - valid = result == 0x20; + if (result != 0x20) { + valid = FALSE; + } NANDClose(&fileInfo); } return valid; @@ -608,11 +612,11 @@ static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, co const void* transferIdBuffer) { s32 result; - result = strncmp(fileHeader->name, "banner.bin", 6); + result = strncmp(fileHeader->name, "nocopy", 6); if (result == 0) { return FALSE; } - result = strncmp(fileHeader->name, "zeldaTp.dat", 10); + result = strncmp(fileHeader->name, "notransfer", 10); if (result == 0) { if ((transferId == 0) || (fileNames == 0) || (transferIdBuffer == 0)) { return FALSE; @@ -622,6 +626,7 @@ static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, co } return TRUE; } +#pragma dont_inline reset #pragma dont_inline on static void* _WADMemAlloc(MEMAllocator* allocator, u32 size) { -- 2.49.1 From 8dc59f866cf28bb15bd1ecbaf35c8dd763360e84 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 02:04:25 +0000 Subject: [PATCH 25/34] refine WAD allocator dispatch --- libs/RVL_SDK/src/wad/wad.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index a76680eb..09e27ee4 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -638,22 +638,21 @@ static void* _WADMemAlloc(MEMAllocator* allocator, u32 size) { return 0; } heapType = ((MEMiHeapHead*)allocator->heap)->magic; - if (heapType == 0x46524D48) { + switch (heapType) { + case 0x46524D48: allocationKind = 1; - } else { - if (heapType < 0x46524D48) { - if (heapType == 0x45585048) { - allocationKind = 0; - goto allocate; - } - } else if (heapType == 0x554E5448) { - allocationKind = 2; - goto allocate; - } + break; + case 0x45585048: + allocationKind = 0; + break; + case 0x554E5448: + allocationKind = 2; + break; + default: allocationKind = 3; + break; } -allocate: if (allocationKind == 1) { return MEMAllocFromFrmHeapEx(allocator->heap, size, 0x40); } -- 2.49.1 From 7346032f32e0fed76d92951f4bcf4b9b21388f88 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 02:14:42 +0000 Subject: [PATCH 26/34] refine WAD stream dispatch --- libs/RVL_SDK/src/wad/wad.c | 100 +++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 09e27ee4..94e6fdd0 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -151,7 +151,7 @@ typedef struct WADHashThreadArgs { #define WAD_ALIGN32(value) (((value) + WAD_READ_ALIGNMENT - 1) & ~(WAD_READ_ALIGNMENT - 1)) static s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32, u32); -static s32 WADReadStream(WADStream* stream, void** buffer, u32 size, u32 offset); +static size_t WADReadStream(WADStream* stream, void** buffer, size_t size, s32 offset); static void WADCloseStream(WADStream* stream); static s32 WADWriteStream(WADStream* stream, void* buffer, u32 size); static s32 WADSeekStream(WADStream* stream, u32 offset, s32 origin); @@ -810,9 +810,10 @@ static s32 WADOpenStream(WADLocation location, const char* path, WADStream* stre return -3000; } -static s32 WADReadStream(WADStream* stream, void** buffer, u32 size, u32 offset) { - s32 result; +static size_t WADReadStream(WADStream* stream, void** buffer, size_t size, s32 offset) { + size_t result; u32 alignedSize; + s32 location; if ((s32)size <= 0) { result = 0; @@ -821,37 +822,43 @@ static s32 WADReadStream(WADStream* stream, void** buffer, u32 size, u32 offset) } else { alignedSize = (size + WAD_READ_ALIGNMENT - 1) & ~(WAD_READ_ALIGNMENT - 1); if (size == alignedSize) { - if (stream->location == WAD_LOCATION_SD_CARD) { - if (stream->handle.fa == 0) { - result = -3000; + location = stream->location; + if (location != WAD_LOCATION_SD_CARD) { + if (location < WAD_LOCATION_SD_CARD) { + if (location != WAD_LOCATION_DVD) { + if (location > 0) { + result = NANDSeek(&stream->handle.nand, offset, 0); + if ((s32)result < 0) { + return result; + } + return NANDRead(&stream->handle.nand, *buffer, alignedSize); + } + if (location >= 0) { + memcpy(*buffer, (u8*)stream->handle.memoryBase + offset, size); + return size; + } + } else { + return DVDReadPrio(&stream->handle.dvd, *buffer, alignedSize, offset, 2); + } } else { - result = FAFseek(stream->handle.fa, offset, 0); - if (result == 0) { - result = FAFread(*buffer, 1, size, stream->handle.fa); + if (location != WAD_LOCATION_CNT_NAND) { + if (location < WAD_LOCATION_CNT_NAND) { + return contentReadDVD(&stream->handle.contentDvd, *buffer, alignedSize, + offset); + } + } else { + return contentReadNAND(&stream->handle.contentNand, *buffer, alignedSize, + offset); } } - } else if (stream->location < WAD_LOCATION_SD_CARD) { - if (stream->location == WAD_LOCATION_DVD) { - return DVDReadPrio(&stream->handle.dvd, *buffer, alignedSize, offset, 2); - } - if (stream->location > WAD_LOCATION_DVD) { - result = NANDSeek(&stream->handle.nand, offset, 0); - if (result < 0) { - return result; - } - return NANDRead(&stream->handle.nand, *buffer, alignedSize); - } - if (stream->location >= 0) { - memcpy(*buffer, (u8*)stream->handle.memoryBase + offset, size); - return size; - } result = -3000; - } else if (stream->location == WAD_LOCATION_CNT_NAND) { - return contentReadNAND(&stream->handle.contentNand, *buffer, alignedSize, offset); - } else if (stream->location < WAD_LOCATION_CNT_NAND) { - return contentReadDVD(&stream->handle.contentDvd, *buffer, alignedSize, offset); + } else if (stream->handle.fa == 0) { + result = -3000; } else { - result = -3000; + result = FAFseek(stream->handle.fa, offset, 0); + if (result == 0) { + result = FAFread(*buffer, 1, size, stream->handle.fa); + } } } else { result = -3000; @@ -864,34 +871,31 @@ static void WADCloseStream(WADStream* stream) { if (stream == 0) { return; } - if (stream->location == WAD_LOCATION_SD_CARD) { - if (stream->handle.fa == 0) { - return; - } - FAFclose(stream->handle.fa); - return; - } - if (stream->location < WAD_LOCATION_SD_CARD) { - if (stream->location == WAD_LOCATION_DVD) { + if (stream->location != WAD_LOCATION_SD_CARD) { + if (stream->location < WAD_LOCATION_SD_CARD) { + if (stream->location != WAD_LOCATION_DVD) { + if (stream->location < WAD_LOCATION_DVD) { + return; + } + NANDClose(&stream->handle.nand); + return; + } DVDClose(&stream->handle.dvd); return; } - if (stream->location < WAD_LOCATION_DVD) { + if (stream->location != WAD_LOCATION_CNT_NAND) { + if (stream->location > WAD_LOCATION_CNT_NAND) { + return; + } + contentCloseDVD(&stream->handle.contentDvd); return; } - if (stream->location > WAD_LOCATION_DVD) { - NANDClose(&stream->handle.nand); - } - return; - } - if (stream->location == WAD_LOCATION_CNT_NAND) { contentCloseNAND(&stream->handle.contentNand); return; } - if (stream->location >= WAD_LOCATION_CNT_NAND) { - return; + if (stream->handle.fa != 0) { + FAFclose(stream->handle.fa); } - contentCloseDVD(&stream->handle.contentDvd); } static s32 WADWriteStream(WADStream* stream, void* buffer, u32 size) { -- 2.49.1 From 108da968f19d97ecd2354df77cceae0d451cac14 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 02:33:52 +0000 Subject: [PATCH 27/34] decompile WAD import dispatch --- libs/RVL_SDK/src/wad/wad.c | 648 ++++++++++++++++++++++++++++++++++++- 1 file changed, 640 insertions(+), 8 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 94e6fdd0..2f2a818a 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -72,12 +72,26 @@ typedef struct WADUnpackInfo { } WADUnpackInfo; typedef struct WADFileHeader { - u32 headerSize; + u32 magic; u32 fileSize; u8 flags[3]; - char name[0x75]; + char name[0x40]; + u8 reserved_0x4b[5]; + u8 iv[0x10]; + u8 reserved_0x60[0x20]; } WADFileHeader; +typedef struct WADSaveDataHeader { + u8 reserved_0x00[0x60]; + u32 fileSize; + u32 magic; + u8 reserved_0x68[0x18]; +} WADSaveDataHeader; + +typedef struct WADThreadStack { + u8 bytes[0x1000]; +} WADThreadStack; + typedef struct WADFileEntry { u32 fileSize; u8 flags[4]; @@ -180,6 +194,8 @@ static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, co const void* transferIdBuffer); static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator); static s32 WAD_815BFFA8(WADImportLoopArgs* args); +static s32 _WADVerifySavedataZD(void* header, WADStream* stream, MEMAllocator* allocator, u32 offset); +static void _WADCleanTmpDir(MEMAllocator* allocator); u32 _WADIsTerminated(const char* text, u32 maxLength); extern s32 ES_GetBoot2Version(u32* version); @@ -465,36 +481,652 @@ cleanup: } static s32 WAD_815BFFA8(WADImportLoopArgs* args) { - WADImportTransfer* transfer = args->transfer; - u32 remaining = args->size; + s32 fd = args->fd; s32 result = 0; + u32 remaining = args->size; u32 bufferIndex = 0; + WADImportTransfer* transfer = args->transfer; while ((remaining != 0) && (result == 0)) { u32 size = remaining; OSMutex* mutex; - if (transfer->chunkSize < remaining) { + if (remaining > transfer->chunkSize) { size = transfer->chunkSize; } mutex = &transfer->mutex[bufferIndex]; OSLockMutex(mutex); while (transfer->ready[bufferIndex] == 0) { - OSWaitCond(&transfer->waitCond[bufferIndex], mutex); + OSWaitCond(&transfer->signalCond[bufferIndex], mutex); } - result = ES_ImportContentData(args->fd, transfer->buffers[bufferIndex], size); + result = ES_ImportContentData(fd, transfer->buffers[bufferIndex], size); transfer->ready[bufferIndex] = 0; if (result != 0) { transfer->error = 1; } OSUnlockMutex(mutex); - OSSignalCond(&transfer->signalCond[bufferIndex]); + OSSignalCond(&transfer->waitCond[bufferIndex]); remaining -= size; bufferIndex ^= 1; } return result; } +s32 WADImportEx(char* path, MEMAllocator* allocator, WADLocation location, u32 offset, u32 flags, + WADProcessCallback processCallback) { + WADImportWorkspace workspace ALIGN32; + WADSaveDataHeader wadHeader ALIGN32; + WADFileHeader backupHeader ALIGN32; + WADImportTransfer transfer; + WADImportLoopArgs threadArgs; + OSThread importThread; + NANDFileInfo backupFile; + NANDStatus fileStatus; + NANDStatus updatedFileStatus; + ESContentId* installedContentIds = 0; + ESTitleMeta* installedTitleMeta = 0; + ESContentMeta* matchingContents = 0; + ESHash* sharedContentHashes = 0; + void* firstBuffer = 0; + void* secondBuffer = 0; + WADThreadStack* threadStack = 0; + void* wadHeaderBuffer = &wadHeader; + void* fileHeaderBuffer = &backupHeader; + u8 transferId[0x20] ALIGN32; + u32 fileOffset = 0; + u32 importedBytes = 0; + u32 installedContentCount = 0; + u32 installedTmdSize = 0; + u32 sharedContentCount = 0; + u32 contentCount = 0; + u32 contentIndex; + u32 listIndex; + u32 sizeRemaining; + u32 transferIdValue; + s32 result = 0; + s32 streamOpened = FALSE; + s32 backupFileOpened = FALSE; + s32 titleImportStarted = FALSE; + s32 contentImportStarted = FALSE; + s32 threadCreated = FALSE; + s32 contentFd = 0; + s32 importExistingTitle = TRUE; + s32 existingTmdAvailable = FALSE; + s32 fileReadResult; + u32 importedContentCount = 0; + u32 bufferIndex = 0; + u32 callbackTotal = 0; + u32 installedContentIndex = 0; + u32 sharedContentIndex; + u32 contentFileOffset; + u32 pathFileIndex; + s32 matchFound; + s32 listResult; + s32 titleVersion; + u32 contentSize; + u32 chunkSize; + u32 readSize; + u32 bootSize; + u32 alignedSize; + u32 callbackDone; + u32 currentTitleId; + u32 currentTitleVersion; + u32 currentContentId; + u32 importedFileCount; + u32 savedataFileIndex; + s32 importResult; + u32 threadPriority; + const char* functionName = "WADImportEx"; + + memset(&workspace.unpackInfo, 0, sizeof(WADUnpackInfo)); + memset(&transfer, 0, sizeof(transfer)); + if ((path == 0) || (allocator == 0) || (allocator->heap == 0)) { + result = -3000; + goto cleanup; + } + if ((offset & 0x3F) != 0) { + result = -3007; + goto cleanup; + } + if (processCallback != 0) { + processCallback(0, 0, FALSE); + } + result = WADOpenStream(location, path, &workspace.stream, 0, 0); + streamOpened = TRUE; + if (result != 0) { + goto cleanup; + } + result = WADReadStream(&workspace.stream, &wadHeaderBuffer, sizeof(wadHeader), offset); + if (result != sizeof(wadHeader)) { + result = -3005; + goto cleanup; + } + result = _WADUnpack(&wadHeader, &workspace.stream, &workspace.unpackInfo, allocator, offset, + flags, 0); + if (result != 0) { + goto cleanup; + } + importedBytes = workspace.unpackInfo.contentOffset; + callbackTotal = workspace.unpackInfo.contentSize + workspace.unpackInfo.fileListSize; + if (((flags & 8) != 0) && (workspace.unpackInfo.titleMeta != 0)) { + if (workspace.unpackInfo.headerInfo != 2) { + result = -3001; + goto cleanup; + } + result = ES_GetTicketViews(workspace.unpackInfo.titleMeta->head.titleId, 0, + &installedContentCount); + if (result != 0) { + goto cleanup; + } + if (installedContentCount == 0) { + result = -1028; + goto cleanup; + } + } + if ((workspace.unpackInfo.titleMeta != 0) && + (((workspace.unpackInfo.size_0x18 == 1) && + (workspace.unpackInfo.size_0x0c == 1)) || + (workspace.unpackInfo.headerInfo == 3))) { + bootSize = (workspace.unpackInfo.titleMetaSize + 0x0F) & ~0x0F; + alignedSize = (bootSize + 0x1F) & ~0x1F; + firstBuffer = _WADMemAlloc(allocator, alignedSize); + if (firstBuffer == 0) { + result = -3003; + goto cleanup; + } + result = WADReadStream(&workspace.stream, &firstBuffer, alignedSize, + offset + importedBytes); + if ((u32)result != alignedSize) { + result = -3005; + goto cleanup; + } + result = ES_ImportBoot(workspace.unpackInfo.buffer_0x34, + workspace.unpackInfo.buffer_0x14, + workspace.unpackInfo.size_0x0c, + workspace.unpackInfo.titleMeta, + workspace.unpackInfo.sectionSize, + workspace.unpackInfo.buffer_0x14, + workspace.unpackInfo.size_0x0c, + workspace.unpackInfo.buffer_0x24, + workspace.unpackInfo.size_0x1c, firstBuffer, + workspace.unpackInfo.titleMetaSize); + goto cleanup; + } + if ((workspace.unpackInfo.titleMeta != 0) && + (workspace.unpackInfo.buffer_0x34 != 0)) { + result = ES_ImportTicket(workspace.unpackInfo.buffer_0x34, + workspace.unpackInfo.buffer_0x14, + workspace.unpackInfo.size_0x0c, + workspace.unpackInfo.buffer_0x24, + workspace.unpackInfo.size_0x1c, 0); + if (result != 0) { + goto cleanup; + } + } + if (processCallback != 0) { + processCallback(0, callbackTotal, FALSE); + } + if (workspace.unpackInfo.titleMeta != 0) { + if (workspace.unpackInfo.cidxMode == 0) { + contentCount = workspace.unpackInfo.titleMeta->head.numContents; + } else { + contentCount = _WADGetCidxCount(workspace.unpackInfo.contentIndex); + if (workspace.unpackInfo.titleMeta->head.numContents < contentCount) { + result = -3001; + goto cleanup; + } + } + result = ES_ListTmdContentsOnCard(workspace.unpackInfo.titleMeta, + workspace.unpackInfo.sectionSize, 0, + &installedContentCount); + if ((result == 0) && (installedContentCount != 0)) { + installedContentIds = _WADMemAlloc(allocator, installedContentCount * sizeof(ESContentId)); + if (installedContentIds == 0) { + result = -3003; + goto cleanup; + } + result = ES_ListTmdContentsOnCard(workspace.unpackInfo.titleMeta, + workspace.unpackInfo.sectionSize, + installedContentIds, &installedContentCount); + if ((result == 0) && + (ES_GetTmd(workspace.unpackInfo.titleMeta->head.titleId, 0, + &installedTmdSize) == 0)) { + alignedSize = (installedTmdSize + 0x1F) & ~0x1F; + installedTitleMeta = _WADMemAlloc(allocator, alignedSize); + if (installedTitleMeta == 0) { + result = -3003; + goto cleanup; + } + result = ES_GetTmd(workspace.unpackInfo.titleMeta->head.titleId, + installedTitleMeta, &installedTmdSize); + if (result == 0) { + importExistingTitle = FALSE; + } + } + } + if (!importExistingTitle && (installedContentCount != 0)) { + matchingContents = _WADMemAlloc(allocator, installedContentCount * sizeof(ESContentMeta)); + if (matchingContents == 0) { + result = -3003; + goto cleanup; + } + installedContentIndex = 0; + for (listIndex = 0; listIndex < installedContentCount; listIndex++) { + for (contentIndex = 0; + contentIndex < installedTitleMeta->head.numContents; contentIndex++) { + if (installedContentIds[listIndex] == + installedTitleMeta->contents[contentIndex].cid) { + memcpy(&matchingContents[installedContentIndex], + &installedTitleMeta->contents[contentIndex], sizeof(ESContentMeta)); + installedContentIndex++; + } + } + } + } + result = ES_ListSharedContents(&sharedContentCount, 0); + if (result == 0) { + alignedSize = (sharedContentCount * sizeof(ESHash) + 0x1F) & ~0x1F; + sharedContentHashes = _WADMemAlloc(allocator, alignedSize); + if ((sharedContentHashes == 0) && (sharedContentCount != 0)) { + result = -3003; + goto cleanup; + } + result = ES_ListSharedContents(&sharedContentCount, sharedContentHashes); + if ((result != 0) && (sharedContentHashes != 0)) { + _WADMemFree(allocator, sharedContentHashes); + sharedContentHashes = 0; + } + } + result = ES_ImportTitleInit(workspace.unpackInfo.titleMeta, + workspace.unpackInfo.sectionSize, + workspace.unpackInfo.buffer_0x14, + workspace.unpackInfo.size_0x0c, + workspace.unpackInfo.buffer_0x24, + workspace.unpackInfo.size_0x1c, + workspace.unpackInfo.headerInfo, 1); + if (result != 0) { + goto cleanup; + } + titleImportStarted = TRUE; + if ((flags & 2) == 0) { + for (contentIndex = 0; contentIndex < contentCount; contentIndex++) { + ESContentMeta* content; + s32 titleMetaIndex = contentIndex; + matchFound = FALSE; + if (workspace.unpackInfo.cidxMode != 0) { + titleMetaIndex = _WADGetCidx(workspace.unpackInfo.contentIndex, contentIndex); + if ((titleMetaIndex < 0) || + ((s32)workspace.unpackInfo.titleMeta->head.numContents <= titleMetaIndex)) { + result = -3001; + goto cleanup; + } + } + content = &workspace.unpackInfo.titleMeta->contents[titleMetaIndex]; + if (!importExistingTitle) { + for (listIndex = 0; listIndex < installedContentCount; listIndex++) { + ESContentMeta* installedContent = &matchingContents[listIndex]; + if ((content->cid == installedContent->cid) && + (memcmp(content->hash, installedContent->hash, sizeof(ESHash)) == 0) && + (content->type == installedContent->type)) { + matchFound = TRUE; + } + } + } + if (((content->type & 0x8000) != 0) && + (sharedContentHashes != 0) && !matchFound) { + for (sharedContentIndex = 0; sharedContentIndex < sharedContentCount; + sharedContentIndex++) { + if (memcmp(content->hash, sharedContentHashes[sharedContentIndex], + sizeof(ESHash)) == 0) { + matchFound = TRUE; + } + } + } + if (matchFound) { + importedBytes += (content->size + 0x0F) & ~0x0F; + if (processCallback != 0) { + processCallback(importedBytes - workspace.unpackInfo.contentOffset, + callbackTotal, FALSE); + } + } else { + if (firstBuffer == 0) { + firstBuffer = _WADMemAlloc(allocator, 0x10000); + if (firstBuffer == 0) { + result = -3003; + goto cleanup; + } + } + if (secondBuffer == 0) { + secondBuffer = _WADMemAlloc(allocator, 0x10000); + if (secondBuffer == 0) { + result = -3003; + goto cleanup; + } + } + if (threadStack == 0) { + threadStack = _WADMemAlloc(allocator, sizeof(WADThreadStack)); + if (threadStack == 0) { + result = -3003; + goto cleanup; + } + } + if ((firstBuffer == 0) || (secondBuffer == 0) || (threadStack == 0)) { + result = -3003; + goto cleanup; + } + contentFd = ES_ImportContentBegin( + workspace.unpackInfo.titleMeta->head.titleId, content->cid); + result = contentFd; + if (contentFd < 0) { + goto cleanup; + } + contentImportStarted = TRUE; + sizeRemaining = (content->size + 0x0F) & ~0x0F; + WAD_815C4A2C(&transfer, firstBuffer, secondBuffer, 0x10000); + threadArgs.fd = contentFd; + threadArgs.transfer = &transfer; + threadArgs.size = sizeRemaining; + threadPriority = OSGetThreadPriority(OSGetCurrentThread()); + threadCreated = OSCreateThread(&importThread, + (void* (*)(void*))WAD_815BFFA8, + &threadArgs, + &threadStack->bytes[sizeof(threadStack->bytes)], + sizeof(threadStack->bytes), + threadPriority, 0); + if (!threadCreated) { + result = -3009; + goto cleanup; + } + OSResumeThread(&importThread); + transfer.error = 0; + bufferIndex = 0; + while ((sizeRemaining != 0) && (transfer.error == 0)) { + chunkSize = sizeRemaining; + if (transfer.chunkSize < chunkSize) { + chunkSize = transfer.chunkSize; + } + OSLockMutex(&transfer.mutex[bufferIndex]); + while ((transfer.ready[bufferIndex] != 0) && + (transfer.error == 0)) { + OSWaitCond(&transfer.waitCond[bufferIndex], + &transfer.mutex[bufferIndex]); + } + if (transfer.error != 0) { + break; + } + readSize = (chunkSize + 0x1F) & ~0x1F; + result = WADReadStream(&workspace.stream, + &transfer.buffers[bufferIndex], readSize, + offset + importedBytes); + if ((u32)result != readSize) { + OSLockMutex(&transfer.mutex[bufferIndex ^ 1]); + OSCancelThread(&importThread); + OSJoinThread(&importThread, 0); + OSUnlockMutex(&transfer.mutex[bufferIndex ^ 1]); + OSUnlockMutex(&transfer.mutex[bufferIndex]); + result = -3005; + goto cleanup; + } + transfer.ready[bufferIndex] = chunkSize; + OSUnlockMutex(&transfer.mutex[bufferIndex]); + OSSignalCond(&transfer.signalCond[bufferIndex]); + importedBytes += chunkSize; + sizeRemaining -= chunkSize; + if (processCallback != 0) { + processCallback(importedBytes - workspace.unpackInfo.contentOffset, + callbackTotal, FALSE); + } + bufferIndex ^= 1; + } + if (threadCreated) { + importResult = OSJoinThread(&importThread, (void**)&result); + if (!importResult) { + result = -3009; + goto cleanup; + } + threadCreated = FALSE; + } + if ((result != 0) || (transfer.error != 0)) { + goto cleanup; + } + result = ES_ImportContentEnd(contentFd); + if (result != 0) { + goto cleanup; + } + contentImportStarted = FALSE; + } + if (workspace.unpackInfo.type == 2) { + importedBytes = (importedBytes + 0x3F) & ~0x3F; + } + } + } + result = ES_ImportTitleDone(); + if (result != 0) { + goto cleanup; + } + titleImportStarted = FALSE; + } + if (((flags & 2) != 0) || (workspace.unpackInfo.fileListSize == 0) || + (workspace.unpackInfo.fileCount == 0)) { + goto cleanup; + } + if (firstBuffer == 0) { + firstBuffer = _WADMemAlloc(allocator, 0x10000); + if (firstBuffer == 0) { + result = -3003; + goto cleanup; + } + } + if (secondBuffer == 0) { + secondBuffer = _WADMemAlloc(allocator, 0x10000); + if (secondBuffer == 0) { + result = -3003; + goto cleanup; + } + } + transferIdValue = _WADGetTransferId(transferId); + fileOffset = workspace.unpackInfo.fileOffset; + if (((wadHeader.magic & 0xFFFFFF00) == 0x525A4400) && + (wadHeader.fileSize == 0x10000)) { + result = _WADVerifySavedataZD(&wadHeader, &workspace.stream, allocator, + offset + workspace.unpackInfo.fileOffset); + goto cleanup; + } + for (pathFileIndex = 0; pathFileIndex < workspace.unpackInfo.fileCount; pathFileIndex++) { + result = WADReadStream(&workspace.stream, &fileHeaderBuffer, sizeof(backupHeader), + offset + fileOffset); + if (result != sizeof(backupHeader)) { + result = -3005; + goto cleanup; + } + fileOffset = (fileOffset + 0xBF) & ~0x3F; + if (backupHeader.magic != 0x3ADF17E) { + result = -3000; + goto cleanup; + } + result = 0; + if (_WADCanImportFile(&backupHeader, transferIdValue, + workspace.unpackInfo.fileNames, transferId) == 0) { + if ((backupHeader.flags[2] == 1) && (backupHeader.fileSize != 0)) { + fileOffset = (fileOffset + backupHeader.fileSize + 0x3F) & + ~0x3F; + } + continue; + } + if (backupHeader.flags[2] == 1) { + result = NANDPrivateCreate(backupHeader.name, backupHeader.flags[0] | 0x20, + backupHeader.flags[1]); + if (result != 0) { + goto cleanup; + } + if (backupHeader.fileSize != 0) { + result = NANDPrivateOpen(backupHeader.name, &backupFile, 2); + if (result != 0) { + goto cleanup; + } + backupFileOpened = TRUE; + sizeRemaining = backupHeader.fileSize; + while (sizeRemaining != 0) { + chunkSize = sizeRemaining; + if (chunkSize > 0x10000) { + chunkSize = 0x10000; + } + result = WADReadStream(&workspace.stream, &firstBuffer, + (chunkSize + 0x1F) & ~0x1F, + offset + fileOffset); + readSize = (chunkSize + 0x1F) & ~0x1F; + if ((u32)result != readSize) { + result = -3005; + goto cleanup; + } + result = ES_Decrypt(6, backupHeader.iv, + firstBuffer, readSize, secondBuffer); + if (result != 0) { + goto cleanup; + } + result = NANDWrite(&backupFile, secondBuffer, chunkSize); + if ((u32)result != chunkSize) { + result = -3006; + goto cleanup; + } + importedBytes += chunkSize; + sizeRemaining -= chunkSize; + if (processCallback != 0) { + processCallback(workspace.unpackInfo.fileListSize + + importedBytes - workspace.unpackInfo.fileOffset, + callbackTotal, FALSE); + } + fileOffset += chunkSize; + } + result = NANDClose(&backupFile); + if (result != 0) { + goto cleanup; + } + backupFileOpened = FALSE; + } + if ((backupHeader.flags[0] & 0x20) == 0) { + result = NANDPrivateGetStatus(backupHeader.name, &fileStatus); + if (result != 0) { + goto cleanup; + } + updatedFileStatus = fileStatus; + updatedFileStatus.permission = backupHeader.flags[0]; + updatedFileStatus.attribute = backupHeader.flags[1]; + result = NANDPrivateSetStatus(backupHeader.name, &updatedFileStatus); + if (result != 0) { + goto cleanup; + } + } + } else if (backupHeader.flags[2] == 2) { + result = NANDPrivateCreateDir(backupHeader.name, + backupHeader.flags[0] | 0x20, + backupHeader.flags[1]); + if (result != -6) { + if (result != 0) { + goto cleanup; + } + } else { + result = NANDPrivateGetStatus(backupHeader.name, &fileStatus); + if (result != 0) { + goto cleanup; + } + if ((fileStatus.permission & 0x20) == 0) { + fileStatus.permission = backupHeader.flags[0] | 0x20; + fileStatus.attribute = backupHeader.flags[1]; + result = NANDPrivateSetStatus(backupHeader.name, &fileStatus); + if (result != 0) { + goto cleanup; + } + } + } + } + fileOffset = (fileOffset + 0x3F) & ~0x3F; + if ((backupHeader.fileSize == 0) && (processCallback != 0)) { + processCallback(workspace.unpackInfo.fileListSize + + importedBytes - workspace.unpackInfo.fileOffset, + callbackTotal, FALSE); + } + fileOffset += backupHeader.fileSize; + fileOffset = (fileOffset + 0x3F) & ~0x3F; + } + fileOffset = workspace.unpackInfo.fileOffset; + for (savedataFileIndex = 0; savedataFileIndex < workspace.unpackInfo.fileCount; + savedataFileIndex++) { + fileReadResult = WADReadStream(&workspace.stream, &fileHeaderBuffer, + sizeof(backupHeader), + offset + fileOffset); + if (fileReadResult != sizeof(backupHeader)) { + result = -3005; + break; + } + result = 0; + fileOffset = (fileOffset + 0xBF) & ~0x3F; + if ((_WADCanImportFile(&backupHeader, transferIdValue, + workspace.unpackInfo.fileNames, transferId) != 0) && + (((result = NANDPrivateGetStatus(backupHeader.name, &fileStatus)) != + 0) || + ((backupHeader.flags[0] & 0x20) == 0 && + ((fileStatus.permission = backupHeader.flags[0]), + (result = NANDPrivateSetStatus(backupHeader.name, &fileStatus)) != + 0)))) { + break; + } + fileOffset = (fileOffset + backupHeader.fileSize + 0x3F) & ~0x3F; + } + +cleanup: + _WADFreeMemory(&workspace.unpackInfo, allocator); + if (firstBuffer != 0) { + _WADMemFree(allocator, firstBuffer); + } + if (secondBuffer != 0) { + _WADMemFree(allocator, secondBuffer); + } + if (installedTitleMeta != 0) { + _WADMemFree(allocator, installedTitleMeta); + } + if (installedContentIds != 0) { + _WADMemFree(allocator, installedContentIds); + } + if (matchingContents != 0) { + _WADMemFree(allocator, matchingContents); + } + if (sharedContentHashes != 0) { + _WADMemFree(allocator, sharedContentHashes); + } + if (threadStack != 0) { + _WADMemFree(allocator, threadStack); + } + if (backupFileOpened) { + NANDClose(&backupFile); + } + if (streamOpened) { + WADCloseStream(&workspace.stream); + } + if (contentImportStarted) { + ES_ImportContentEnd(contentFd); + OSReport("%s:%d Cancel importing the content.\n", functionName, 0x8DF); + } + if (titleImportStarted) { + ES_ImportTitleCancel(); + _WADCleanTmpDir(allocator); + OSReport("%s:%d Cancel importing the title.\n", functionName, 0x8E6); + } + if (processCallback != 0) { + if (importedBytes == 0) { + processCallback(0, 0, TRUE); + } else if ((callbackTotal == 0) || (workspace.unpackInfo.fileCount == 0)) { + processCallback(importedBytes - workspace.unpackInfo.contentOffset, callbackTotal, + TRUE); + } else { + processCallback(workspace.unpackInfo.fileListSize + + importedBytes - workspace.unpackInfo.fileOffset, + callbackTotal, TRUE); + } + } + return result; +} + s32 WAD_815C1288(WADExportLoopArgs* args) { ESFd fd = args->fd; s32 result = 0; -- 2.49.1 From 7901a0ebc41a160e735867ce431aa9ebc3d8801e Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 02:52:28 +0000 Subject: [PATCH 28/34] decompile WAD backup helpers --- libs/RVL_SDK/src/wad/wad.c | 944 ++++++++++++++++++++++++++++++++++++- 1 file changed, 919 insertions(+), 25 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 2f2a818a..22882a2d 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -81,8 +81,19 @@ typedef struct WADFileHeader { u8 reserved_0x60[0x20]; } WADFileHeader; +typedef struct WADBackupFileHeader { + u32 magic; + u32 fileSize; + u8 flags[3]; + char path[0x75]; +} WADBackupFileHeader; + typedef struct WADSaveDataHeader { - u8 reserved_0x00[0x60]; + u32 format; + u32 flags; + u32 reserved_0x08; + u32 saveType; + u8 reserved_0x10[0x50]; u32 fileSize; u32 magic; u8 reserved_0x68[0x18]; @@ -98,6 +109,16 @@ typedef struct WADFileEntry { char name[0x40]; } WADFileEntry; +typedef union WADBackupHeaderBlock { + WADBackupHeader header; + u8 bytes[0x80]; +} WADBackupHeaderBlock; + +typedef union WADBackupTitleMetaBuffer { + ESTmdView view; + u8 data[0x4A00]; +} WADBackupTitleMetaBuffer; + typedef struct WADBackupSignature { u8 header[0x40]; u8 firstCertificate[0x180]; @@ -164,11 +185,11 @@ typedef struct WADHashThreadArgs { #define WAD_READ_ALIGNMENT 0x20 #define WAD_ALIGN32(value) (((value) + WAD_READ_ALIGNMENT - 1) & ~(WAD_READ_ALIGNMENT - 1)) -static s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32, u32); -static size_t WADReadStream(WADStream* stream, void** buffer, size_t size, s32 offset); -static void WADCloseStream(WADStream* stream); -static s32 WADWriteStream(WADStream* stream, void* buffer, u32 size); -static s32 WADSeekStream(WADStream* stream, u32 offset, s32 origin); +s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32, u32); +size_t WADReadStream(WADStream* stream, void** buffer, size_t size, s32 offset); +void WADCloseStream(WADStream* stream); +s32 WADWriteStream(WADStream* stream, void* buffer, u32 size); +s32 WADSeekStream(WADStream* stream, u32 offset, s32 origin); static s32 WAD_815C2F44(WADHeader* header, s32* headerInfo); static s32 _WADGetTitleVer(WADHeader* header, ESTitleId* titleId, u16* titleVersion, s32 type, s32 headerInfo); @@ -182,8 +203,8 @@ static s32 _WADUnpack(void* header, WADStream* stream, WADUnpackInfo* info, MEMA u32 offset, u32 flags, u32 mode); static s32 _WADHash(WADStream* stream, u32 offset, u32 size, void* context, void* buffer, u32 chunkSize, void* secondBuffer, void* threadStack, u32 threadStackSize); -static void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBuffer, - u32 chunkSize); +void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBuffer, + u32 chunkSize); s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size); s32 _WADGetCidxCount(const ESContentMask* contentMask); static s32 _WADGetCidx(const ESContentMask* contentMask, u32 contentNumber); @@ -194,11 +215,23 @@ static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, co const void* transferIdBuffer); static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator); static s32 WAD_815BFFA8(WADImportLoopArgs* args); -static s32 _WADVerifySavedataZD(void* header, WADStream* stream, MEMAllocator* allocator, u32 offset); +static s32 _WADBackupGetFiles(const char* path, u32 flags, MEMAllocator* allocator, + u32* fileCount, WADBackupFileHeader* files); +static s32 _WADBackupGetSize(u32 flags, ESTmdView* titleMeta, ESContentMask* contentMask, + u32 fileCount, WADBackupFileHeader* files, u32* titleMetaSize, + u32* contentDataSize, u32* totalSize); +static s32 _WADCheckContents(ESTmdView* titleMeta, ESContentMask* contentMask); +static void _WADRandPad(void* buffer, u32 size); +static s32 _WADVerifySavedataZD(WADSaveDataHeader* header, WADStream* stream, + MEMAllocator* allocator, u32 offset); static void _WADCleanTmpDir(MEMAllocator* allocator); u32 _WADIsTerminated(const char* text, u32 maxLength); extern s32 ES_GetBoot2Version(u32* version); +extern s32 SHA1Reset(void* context); +extern s32 SHA1Input(void* context, const void* data, u32 size); +extern s32 SHA1Result(void* context, void* digest); +extern char* strrchr(const char* string, int character); s32 WADGetTitleVersionEx(char* path, ESTitleId* titleId, u16* titleVersion, WADLocation location, u32 offset) { BOOL streamOpened; @@ -1127,16 +1160,23 @@ cleanup: return result; } -s32 WAD_815C1288(WADExportLoopArgs* args) { - ESFd fd = args->fd; - s32 result = 0; - u32 remaining = args->size; - u32 bufferIndex = 0; - WADImportTransfer* transfer = args->transfer; +static s32 WAD_815C1288(WADExportLoopArgs* args) { + s32 result; + u32 remaining; + u32 bufferIndex; + ESFd fd; + WADImportTransfer* transfer; + u32 size; + OSMutex* mutex; + + fd = args->fd; + result = 0; + remaining = args->size; + bufferIndex = 0; + transfer = args->transfer; while ((remaining != 0) && (result == 0)) { - u32 size = remaining; - OSMutex* mutex; + size = remaining; if (remaining > transfer->chunkSize) { size = transfer->chunkSize; @@ -1159,6 +1199,530 @@ s32 WAD_815C1288(WADExportLoopArgs* args) { return result; } +s32 WADBackupEx(u64 titleId, u32 ticketId, MEMAllocator* allocator, char* path, u32* sizeOut, + WADLocation location, u32 offset, WADProcessCallback processCallback) { + WADBackupTitleMetaBuffer* titleMetaBuffer = 0; + ESTmdView* titleMeta = 0; + WADBackupFileHeader* files = 0; + WADStream stream ALIGN32; + WADImportTransfer transfer; + WADExportLoopArgs threadArgs; + OSThread exportThread; + NANDFileInfo savedFile ALIGN32; + WADBackupHeaderBlock headerBlock ALIGN32; + ESContentMask existingContentMask ALIGN32; + ESContentId installedContentIds[520] ALIGN32; + u8 hashContext[0xC0] ALIGN32; + u8 digest[0x40] ALIGN32; + u8 signature[0x40] ALIGN32; + u8 deviceCertificate[0x180] ALIGN32; + ESCertSignature signerCertificate ALIGN32; + u8 encryptionIv[0x10] ALIGN32; + void* outputBuffer = 0; + void* encryptionBuffer = 0; + WADThreadStack* threadStack = 0; + u32 flags = ticketId; + u32 titleMetaSize = 0; + u32 contentDataSize = 0; + u32 fileDataSize = 0; + u32 fileCount = 0; + u32 installedContentCount = 0; + u32 currentContentCount = 0; + u32 fileIndex; + u32 contentIndex; + u32 importedSize = 0; + u32 paddedSize; + u32 transferId = 0; + u32 totalSize; + u32 priority; + u32 sizeRemaining; + u32 bufferIndex; + u32 chunkSize; + u32 readSize; + s32 result = 0; + s32 streamOpened = FALSE; + s32 titleExportStarted = FALSE; + s32 contentExportStarted = FALSE; + s32 threadCreated = FALSE; + s32 fileOpened = FALSE; + s32 fileFd; + u32 callbackDone = 0; + + if (flags == 0) { + flags = 0x0F; + } + memset(&transfer, 0, sizeof(transfer)); + memset(&headerBlock, 0, sizeof(headerBlock)); + memset(&existingContentMask, 0, sizeof(existingContentMask)); + memset(encryptionIv, 0, sizeof(encryptionIv)); + if (((flags & 3) == 0) || ((u32)(titleId >> 32) == 1) || + (allocator == 0) || (sizeOut == 0)) { + result = -3000; + goto cleanup; + } + if ((offset & 0x3F) != 0) { + result = -3007; + goto cleanup; + } + if (processCallback != 0) { + processCallback(0, 0, FALSE); + } + if (((flags & 1) != 0) || ((flags & 0x20) != 0)) { + result = ES_GetTmdView(titleId, 0, &titleMetaSize); + if (result != 0) { + goto cleanup; + } + titleMetaBuffer = _WADMemAlloc(allocator, sizeof(WADBackupTitleMetaBuffer)); + if (titleMetaBuffer == 0) { + result = -3003; + goto cleanup; + } + titleMeta = &titleMetaBuffer->view; + result = ES_GetTmdView(titleId, titleMeta, &titleMetaSize); + if (result != 0) { + goto cleanup; + } + } + if ((flags & 1) != 0) { + result = ES_ListTitleContentsOnCard(titleId, 0, &installedContentCount); + if (result == -106) { + result = -3002; + goto cleanup; + } + if ((result != 0) || (installedContentCount == 0)) { + result = -3002; + goto cleanup; + } + result = ES_ListTitleContentsOnCard(titleId, installedContentIds, + &installedContentCount); + if (result != 0) { + goto cleanup; + } + result = _WADCheckContents(titleMeta, &existingContentMask); + if (result != 0) { + goto cleanup; + } + } + if ((flags & 2) != 0) { + char currentDirectory[NAND_MAX_PATH] ALIGN32; + result = NANDGetCurrentDir(currentDirectory); + if (result != 0) { + goto cleanup; + } + result = _WADBackupGetFiles(0, flags, allocator, &fileCount, 0); + if (result != 0) { + goto cleanup; + } + if (fileCount != 0) { + files = _WADMemAlloc(allocator, fileCount * sizeof(WADBackupFileHeader)); + if (files == 0) { + result = -3003; + goto cleanup; + } + result = _WADBackupGetFiles(0, flags, allocator, &fileCount, files); + if (result != 0) { + goto cleanup; + } + } + } + result = _WADBackupGetSize(flags, titleMeta, &existingContentMask, fileCount, files, + &titleMetaSize, &contentDataSize, &fileDataSize); + if (result != 0) { + goto cleanup; + } + if ((contentDataSize == 0) && (fileDataSize == 0)) { + result = -3002; + goto cleanup; + } + totalSize = titleMetaSize + contentDataSize + fileDataSize + 0x340; + if (path == 0) { + *sizeOut = totalSize; + result = 0; + goto cleanup; + } + outputBuffer = _WADMemAlloc(allocator, 0x10000); + if (outputBuffer == 0) { + result = -3003; + goto cleanup; + } + if (processCallback != 0) { + processCallback(0, contentDataSize + fileDataSize, FALSE); + } + result = WADOpenStream(location, path, &stream, 1, offset); + streamOpened = TRUE; + if (result != 0) { + goto cleanup; + } + headerBlock.header.hdrSize = sizeof(WADBackupHeader); + headerBlock.header.wadType[0] = 'B'; + headerBlock.header.wadType[1] = 'k'; + headerBlock.header.wadVersion = 1; + headerBlock.header.numFiles = fileCount; + headerBlock.header.fileSize = fileDataSize; + headerBlock.header.tmdSize = titleMetaSize; + headerBlock.header.contentSize = contentDataSize; + headerBlock.header.backupAreaLen = titleMetaSize + contentDataSize + fileDataSize; + headerBlock.header.titleId = titleId; + result = ES_GetDeviceId(&headerBlock.header.deviceId); + if (result != 0) { + goto cleanup; + } + if ((flags & 1) != 0) { + headerBlock.header.cidx = existingContentMask; + } + result = WADWriteStream(&stream, &headerBlock, sizeof(headerBlock)); + if (result != sizeof(headerBlock)) { + result = -3006; + goto cleanup; + } + importedSize = sizeof(headerBlock); + result = SHA1Reset(hashContext); + if (result != 0) { + goto cleanup; + } + result = SHA1Input(hashContext, &headerBlock, sizeof(headerBlock)); + if (result != 0) { + goto cleanup; + } + srand(ticketId); + if (((flags & 1) != 0) || ((flags & 0x20) != 0)) { + result = ES_ExportTitleInit(titleId, headerBlock.header.deviceId, 0, (void*)2, 0, 0, 0, + 0, 0, 0, 0); + if (result != 0) { + goto cleanup; + } + titleExportStarted = TRUE; + paddedSize = (titleMetaSize + 0x3F) & ~0x3F; + _WADRandPad(&titleMetaBuffer->data[titleMetaSize], + paddedSize - titleMetaSize); + result = SHA1Input(hashContext, titleMeta, paddedSize); + if (result != 0) { + goto cleanup; + } + result = WADWriteStream(&stream, titleMeta, paddedSize); + if ((u32)result != paddedSize) { + result = -3006; + goto cleanup; + } + importedSize += paddedSize; + } + if ((flags & 1) != 0) { + encryptionBuffer = _WADMemAlloc(allocator, 0x10000); + if (encryptionBuffer == 0) { + result = -3003; + goto cleanup; + } + threadStack = _WADMemAlloc(allocator, sizeof(WADThreadStack)); + if (threadStack == 0) { + result = -3003; + goto cleanup; + } + currentContentCount = _WADGetCidxCount(&existingContentMask); + for (contentIndex = 0; contentIndex < currentContentCount; contentIndex++) { + s32 tmdIndex = _WADGetCidx(&existingContentMask, contentIndex); + ESContentMeta* content; + if ((tmdIndex < 0) || (titleMeta->head.numContents <= tmdIndex)) { + result = -3009; + goto cleanup; + } + content = (ESContentMeta*)&titleMeta->contents[tmdIndex]; + fileFd = ES_ExportContentBegin(titleId, content->cid); + result = fileFd; + if (fileFd < 0) { + goto cleanup; + } + contentExportStarted = TRUE; + sizeRemaining = (content->size + 0x0F) & ~0x0F; + WAD_815C4A2C(&transfer, outputBuffer, encryptionBuffer, 0x10000); + threadArgs.fd = fileFd; + threadArgs.size = sizeRemaining; + threadArgs.transfer = &transfer; + priority = OSGetThreadPriority(OSGetCurrentThread()); + threadCreated = OSCreateThread(&exportThread, (void* (*)(void*))WAD_815C1288, + &threadArgs, + &threadStack->bytes[sizeof(threadStack->bytes)], + sizeof(threadStack->bytes), + priority, 0); + if (!threadCreated) { + result = -3009; + goto cleanup; + } + OSResumeThread(&exportThread); + sizeRemaining = (sizeRemaining + 0x3F) & ~0x3F; + bufferIndex = 0; + while ((sizeRemaining != 0) && (transfer.error == 0)) { + chunkSize = sizeRemaining; + if (transfer.chunkSize < chunkSize) { + chunkSize = transfer.chunkSize; + } + OSLockMutex(&transfer.mutex[bufferIndex]); + while ((transfer.ready[bufferIndex] == 0) && (transfer.error == 0)) { + OSWaitCond(&transfer.waitCond[bufferIndex], &transfer.mutex[bufferIndex]); + } + if (transfer.error != 0) { + break; + } + readSize = (chunkSize + 0x3F) & ~0x3F; + if (readSize > chunkSize) { + _WADRandPad((u8*)transfer.buffers[bufferIndex] + chunkSize, + readSize - chunkSize); + } + result = SHA1Input(hashContext, transfer.buffers[bufferIndex], chunkSize); + if (result != 0) { + goto cleanup; + } + result = WADWriteStream(&stream, transfer.buffers[bufferIndex], chunkSize); + if ((u32)result != chunkSize) { + OSLockMutex(&transfer.mutex[bufferIndex ^ 1]); + OSCancelThread(&exportThread); + OSJoinThread(&exportThread, 0); + OSUnlockMutex(&transfer.mutex[bufferIndex ^ 1]); + OSUnlockMutex(&transfer.mutex[bufferIndex]); + result = -3006; + goto cleanup; + } + transfer.ready[bufferIndex] = 0; + OSUnlockMutex(&transfer.mutex[bufferIndex]); + OSSignalCond(&transfer.signalCond[bufferIndex]); + importedSize += chunkSize; + sizeRemaining -= chunkSize; + if (processCallback != 0) { + processCallback(importedSize, contentDataSize + fileDataSize, FALSE); + } + bufferIndex ^= 1; + } + if (threadCreated) { + result = OSJoinThread(&exportThread, (void**)&fileFd); + threadCreated = FALSE; + if (result == 0) { + result = -3009; + goto cleanup; + } + } + if (fileFd < 0) { + result = fileFd; + goto cleanup; + } + result = ES_ExportContentEnd(fileFd); + contentExportStarted = FALSE; + if (result != 0) { + goto cleanup; + } + } + } + if (titleExportStarted) { + result = ES_ExportTitleDone(); + titleExportStarted = FALSE; + if (result != 0) { + goto cleanup; + } + } + if ((flags & 2) != 0) { + for (fileIndex = 0; fileIndex < fileCount; fileIndex++) { + WADBackupFileHeader* file = &files[fileIndex]; + s32 filePosition = WADSeekStream(&stream, 0, 1); + size_t nameLength = strlen(file->path); + _WADRandPad(file->path + nameLength + 1, + 0x75 - (nameLength + 1)); + result = SHA1Input(hashContext, file, sizeof(WADBackupHeaderBlock)); + if (result != 0) { + goto cleanup; + } + result = WADWriteStream(&stream, file, sizeof(WADBackupHeaderBlock)); + if (result != sizeof(WADBackupHeaderBlock)) { + result = -3006; + goto cleanup; + } + importedSize += sizeof(WADBackupHeaderBlock); + if (processCallback != 0) { + processCallback(importedSize, contentDataSize + fileDataSize, FALSE); + } + if ((file->flags[2] == 1) && (file->fileSize != 0)) { + result = NANDPrivateOpen(file->path, &savedFile, 1); + if (result != 0) { + goto cleanup; + } + fileOpened = TRUE; + sizeRemaining = (file->fileSize + 0x1F) & ~0x1F; + while (sizeRemaining != 0) { + chunkSize = sizeRemaining; + if (chunkSize > 0x10000) { + chunkSize = 0x10000; + } + readSize = NANDRead(&savedFile, outputBuffer, chunkSize); + if (((readSize + 0x1F) & ~0x1F) != chunkSize) { + result = -3005; + goto cleanup; + } + result = ES_Encrypt(6, encryptionIv, outputBuffer, chunkSize, + encryptionBuffer); + if (result != 0) { + goto cleanup; + } + result = SHA1Input(hashContext, encryptionBuffer, chunkSize); + if (result != 0) { + goto cleanup; + } + result = WADWriteStream(&stream, encryptionBuffer, chunkSize); + if ((u32)result != chunkSize) { + result = -3006; + goto cleanup; + } + importedSize += chunkSize; + sizeRemaining -= chunkSize; + if (processCallback != 0) { + processCallback(importedSize, contentDataSize + fileDataSize, FALSE); + } + } + result = NANDClose(&savedFile); + fileOpened = FALSE; + if (result != 0) { + goto cleanup; + } + paddedSize = (file->fileSize + 0x3F) & ~0x3F; + if (paddedSize > file->fileSize) { + _WADRandPad(encryptionBuffer, paddedSize - file->fileSize); + result = SHA1Input(hashContext, encryptionBuffer, + paddedSize - file->fileSize); + if (result != 0) { + goto cleanup; + } + result = WADWriteStream(&stream, encryptionBuffer, + paddedSize - file->fileSize); + if ((u32)result != paddedSize - file->fileSize) { + result = -3006; + goto cleanup; + } + importedSize += paddedSize - file->fileSize; + } + } + if (filePosition < 0) { + result = filePosition; + goto cleanup; + } + } + } + result = WADSeekStream(&stream, 0, 1); + if (result < 0) { + goto cleanup; + } + if ((u32)result != offset + totalSize - 0x340) { + result = -3006; + goto cleanup; + } + result = SHA1Result(hashContext, digest); + if (result != 0) { + goto cleanup; + } + result = ES_Sign(digest, 0x14, signature, &signerCertificate); + if (result != 0) { + goto cleanup; + } + result = ES_GetDeviceCert(deviceCertificate); + if (result != 0) { + goto cleanup; + } + result = WADWriteStream(&stream, signature, sizeof(signature)); + if (result != sizeof(signature)) { + result = -3006; + goto cleanup; + } + result = WADWriteStream(&stream, deviceCertificate, sizeof(deviceCertificate)); + if (result != sizeof(deviceCertificate)) { + result = -3006; + goto cleanup; + } + result = WADWriteStream(&stream, &signerCertificate, sizeof(signerCertificate)); + if (result != sizeof(signerCertificate)) { + result = -3006; + goto cleanup; + } + if (WADSeekStream(&stream, offset, 0) != (s32)offset) { + result = -3006; + goto cleanup; + } + result = WADWriteStream(&stream, &headerBlock, sizeof(headerBlock)); + if (result != sizeof(headerBlock)) { + result = -3006; + goto cleanup; + } + *sizeOut = totalSize; + result = 0; + +cleanup: + if (titleMetaBuffer != 0) { + _WADMemFree(allocator, titleMetaBuffer); + } + if (outputBuffer != 0) { + _WADMemFree(allocator, outputBuffer); + } + if (encryptionBuffer != 0) { + _WADMemFree(allocator, encryptionBuffer); + } + if (threadStack != 0) { + _WADMemFree(allocator, threadStack); + } + if (files != 0) { + _WADMemFree(allocator, files); + } + if (fileOpened) { + NANDClose(&savedFile); + } + if (streamOpened) { + WADCloseStream(&stream); + } + if (contentExportStarted) { + ES_ExportContentEnd(fileFd); + } + if (titleExportStarted) { + ES_ExportTitleDone(); + } + if ((processCallback != 0) && (sizeOut != 0)) { + processCallback(importedSize, contentDataSize + fileDataSize, TRUE); + } + return result; +} + +static s32 _WADCheckContents(ESTmdView* titleMeta, ESContentMask* contentMask) { + ESContentId installedContentIds[520] ALIGN32; + u32 installedContentCount; + u32 contentIndex; + s32 result; + + result = ES_ListTitleContentsOnCard(titleMeta->head.titleId, 0, &installedContentCount); + if (result == -106) { + result = -3002; + } else { + result = ES_ListTitleContentsOnCard(titleMeta->head.titleId, installedContentIds, + &installedContentCount); + if (result == 0) { + memset(contentMask, 0, sizeof(*contentMask)); + for (contentIndex = 0; contentIndex < titleMeta->head.numContents; contentIndex++) { + ESCmdView* content = &titleMeta->contents[contentIndex]; + u32 installedIndex; + + if ((content->type & 0x8000) == 0) { + for (installedIndex = 0; installedIndex < installedContentCount; + installedIndex++) { + if (installedContentIds[installedIndex] == content->cid) { + contentMask->data[contentIndex >> 3] |= 1 << (contentIndex & 7); + break; + } + } + if ((installedIndex == installedContentCount) && + ((content->type & 0x4000) == 0)) { + result = -3002; + goto done; + } + result = 0; + } + } + } + } +done: + return result; +} + s32 _WADGetCidxCount(const ESContentMask* contentMask) { s32 count = 0; u32 bitIndex = 0; @@ -1258,6 +1822,195 @@ static s32 _WADCanImportFile(const WADFileHeader* fileHeader, u32 transferId, co } return TRUE; } + +static s32 _WADBackupGetFiles(const char* directoryPath, u32 flags, MEMAllocator* allocator, + u32* fileCount, WADBackupFileHeader* files) { + char currentDirectory[NAND_MAX_PATH] ALIGN32; + char fullDirectoryPath[NAND_MAX_PATH] ALIGN32; + char fullFilePath[NAND_MAX_PATH] ALIGN32; + char* names = 0; + char* nameList = 0; + u32 nameCount = 0; + u32 nameIndex; + u32 totalFiles = 0; + u32 childFileCount; + u8 fileType; + NANDFileInfo fileInfo; + NANDStatus status; + s32 result; + + if ((allocator == 0) || (fileCount == 0)) { + return -3000; + } + result = NANDGetCurrentDir(currentDirectory); + if (result != 0) { + return result; + } + if (directoryPath == 0) { + directoryPath = ""; + } + if (directoryPath[0] == '\0') { + strncpy(fullDirectoryPath, currentDirectory, sizeof(fullDirectoryPath)); + } else if (directoryPath[0] == '/') { + strncpy(fullDirectoryPath, directoryPath, sizeof(fullDirectoryPath)); + } else { + snprintf(fullDirectoryPath, sizeof(fullDirectoryPath), "%s/%s", currentDirectory, + directoryPath); + } + result = NANDPrivateReadDir(fullDirectoryPath, 0, &nameCount); + if (result == -1) { + *fileCount = 0; + return 0; + } + if (result != 0) { + return result; + } + if (nameCount != 0) { + nameList = _WADMemAlloc(allocator, nameCount * 0x40); + if (nameList == 0) { + return -3003; + } + names = nameList; + result = NANDPrivateReadDir(fullDirectoryPath, nameList, &nameCount); + if (result != 0) { + goto cleanup; + } + for (nameIndex = 0; nameIndex < nameCount; nameIndex++) { + char* name = names; + WADBackupFileHeader* file = 0; + u32 fileSize = 0; + BOOL closeFile = FALSE; + + if ((flags & 4) != 0) { + if (directoryPath[0] == '\0') { + snprintf(fullFilePath, sizeof(fullFilePath), "/%s", name); + } else { + snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, name); + } + } else if (directoryPath[0] == '\0') { + snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", currentDirectory, name); + } else if (directoryPath[0] == '/') { + snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s", directoryPath, name); + } else { + snprintf(fullFilePath, sizeof(fullFilePath), "%s/%s/%s", currentDirectory, + directoryPath, name); + } + result = NANDPrivateGetType(fullFilePath, &fileType); + if (result != 0) { + break; + } + if ((fileType == 2) && ((flags & 8) == 0)) { + goto nextName; + } + if (fileType == 1) { + result = NANDPrivateOpen(fullFilePath, &fileInfo, 1); + if (result != 0) { + break; + } + closeFile = TRUE; + result = NANDGetLength(&fileInfo, &fileSize); + if (result != 0) { + NANDClose(&fileInfo); + break; + } + result = NANDClose(&fileInfo); + closeFile = FALSE; + if (result != 0) { + break; + } + } else if (fileType != 2) { + goto nextName; + } + result = NANDPrivateGetStatus(fullFilePath, &status); + if (result != 0) { + break; + } + if (files != 0) { + file = &files[totalFiles]; + file->magic = 0x3ADF17E; + file->fileSize = fileSize; + file->flags[0] = status.permission; + file->flags[1] = status.attribute; + file->flags[2] = fileType; + strncpy(file->path, fullFilePath, 0x40); + } + totalFiles++; + if (fileType == 2) { + childFileCount = 0; + result = _WADBackupGetFiles(fullFilePath, flags, allocator, &childFileCount, + files == 0 ? 0 : &files[totalFiles]); + if (result != 0) { + break; + } + totalFiles += childFileCount; + } + +nextName: + names += strlen(names) + 1; + if (closeFile) { + NANDClose(&fileInfo); + } + } + } + if (result == 0) { + *fileCount = totalFiles; + } + +cleanup: + if (nameList != 0) { + _WADMemFree(allocator, nameList); + } + return result; +} + +static s32 _WADBackupGetSize(u32 flags, ESTmdView* titleMeta, ESContentMask* contentMask, + u32 fileCount, WADBackupFileHeader* files, u32* titleMetaSize, + u32* contentDataSize, u32* fileDataSize) { + u32 titleSize = 0; + u32 contentSize = 0; + u32 filesSize = 0; + u32 selectedCount; + u32 contentIndex; + u32 fileIndex; + s32 result = 0; + + if (titleMeta == 0) { + if (titleMetaSize != 0) { + *titleMetaSize = 0; + } + } else if (((flags & 1) != 0) || ((flags & 0x20) != 0)) { + result = ES_GetTmdSizeFromView(titleMeta, &titleSize); + if (result != 0) { + return result; + } + titleSize = (titleSize + 0x3F) & ~0x3F; + } + if (((flags & 1) != 0) && (titleMeta != 0)) { + selectedCount = _WADGetCidxCount(contentMask); + for (contentIndex = 0; contentIndex < selectedCount; contentIndex++) { + s32 tmdIndex = _WADGetCidx(contentMask, contentIndex); + if ((tmdIndex < 0) || (titleMeta->head.numContents <= tmdIndex)) { + return -3009; + } + contentSize += (titleMeta->contents[tmdIndex].size + 0x3F) & ~0x3F; + } + } + if ((fileCount != 0) && (files != 0)) { + for (fileIndex = 0; fileIndex < fileCount; fileIndex++) { + filesSize += 0x80 + ((files[fileIndex].fileSize + 0x3F) & ~0x3F); + } + } + if (titleMetaSize != 0) { + *titleMetaSize = titleSize; + } + if (contentDataSize != 0) { + *contentDataSize = contentSize; + } + if (fileDataSize != 0) { + *fileDataSize = filesSize; + } + return result; +} #pragma dont_inline reset #pragma dont_inline on @@ -1330,6 +2083,33 @@ static void _WADFreeMemory(WADUnpackInfo* info, MEMAllocator* allocator) { } } +static void _WADRandPad(void* buffer, u32 size) { + u32* words = buffer; + u32 wordCount = size >> 2; + u32 remainder = size & 3; + u32 wordIndex; + + for (wordIndex = 0; wordIndex < wordCount; wordIndex++) { + u32 first = rand(); + u32 second = rand(); + u32 third = rand(); + words[wordIndex] = (third & 3) | (second << 17) | (first << 2); + } + if (remainder != 0) { + u32 first = rand(); + u32 second = rand(); + u32 third = rand(); + u32 randomWord = (third & 3) | (second << 17) | (first << 2); + u8* tail = (u8*)&words[wordCount]; + u8* randomBytes = (u8*)&randomWord; + u32 byteIndex; + + for (byteIndex = 0; byteIndex < remainder; byteIndex++) { + tail[byteIndex] = randomBytes[byteIndex]; + } + } +} + typedef struct WADContentPath { const char* name; void* handle; @@ -1344,8 +2124,8 @@ typedef union WADFileInfoView { } fields; } WADFileInfoView; -static s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32 write, - u32 offset) { +s32 WADOpenStream(WADLocation location, const char* path, WADStream* stream, u32 write, + u32 offset) { s32 result; stream->location = location; @@ -1442,7 +2222,7 @@ static s32 WADOpenStream(WADLocation location, const char* path, WADStream* stre return -3000; } -static size_t WADReadStream(WADStream* stream, void** buffer, size_t size, s32 offset) { +size_t WADReadStream(WADStream* stream, void** buffer, size_t size, s32 offset) { size_t result; u32 alignedSize; s32 location; @@ -1499,7 +2279,7 @@ static size_t WADReadStream(WADStream* stream, void** buffer, size_t size, s32 o return result; } -static void WADCloseStream(WADStream* stream) { +void WADCloseStream(WADStream* stream) { if (stream == 0) { return; } @@ -1530,7 +2310,7 @@ static void WADCloseStream(WADStream* stream) { } } -static s32 WADWriteStream(WADStream* stream, void* buffer, u32 size) { +s32 WADWriteStream(WADStream* stream, void* buffer, u32 size) { if ((stream == 0) || (buffer == 0)) { return -3000; } @@ -1551,7 +2331,7 @@ static s32 WADWriteStream(WADStream* stream, void* buffer, u32 size) { return -3000; } -static s32 WADSeekStream(WADStream* stream, u32 offset, s32 origin) { +s32 WADSeekStream(WADStream* stream, u32 offset, s32 origin) { s32 result; if ((stream == 0) || (origin < 0) || (origin > 2)) { @@ -2053,6 +2833,120 @@ done: return result; } +static s32 _WADVerifySavedataZD(WADSaveDataHeader* header, WADStream* stream, + MEMAllocator* allocator, u32 offset) { + WADFileHeader* fileHeader = 0; + void* encryptedData = 0; + void* decryptedData = 0; + NANDFileInfo fileInfo; + s32 fileOpened = FALSE; + s32 result; + + if (header->saveType != 1) { + return -3009; + } + fileHeader = MEMAllocFromAllocator(allocator, sizeof(WADFileHeader)); + if (fileHeader == 0) { + result = -3003; + goto cleanup; + } + result = WADReadStream(stream, (void**)&fileHeader, sizeof(WADFileHeader), offset); + if (result != sizeof(WADFileHeader)) { + result = -3005; + goto cleanup; + } + if (fileHeader->fileSize < 0x4000) { + result = -3009; + goto cleanup; + } + encryptedData = MEMAllocFromAllocator(allocator, 0x4000); + decryptedData = MEMAllocFromAllocator(allocator, 0x4000); + if ((encryptedData == 0) || (decryptedData == 0)) { + result = -3003; + goto cleanup; + } + memset(encryptedData, 0, 0x4000); + memset(decryptedData, 0, 0x4000); + if (strcmp(fileHeader->name, "zeldaTp.dat") != 0) { + result = -3009; + goto cleanup; + } + result = WADReadStream(stream, &encryptedData, 0x4000, offset + 0x80); + if (result != 0x4000) { + result = -3005; + goto cleanup; + } + result = ES_Decrypt(6, fileHeader->iv, encryptedData, 0x4000, decryptedData); + if (result != 0) { + result = -3005; + goto cleanup; + } + if (WADCheckSavedataZD(decryptedData) == 0) { + result = -3009; + goto cleanup; + } + result = NANDPrivateCreate(fileHeader->name, 0x34, 0); + if (result != 0) { + goto cleanup; + } + result = NANDPrivateOpen(fileHeader->name, &fileInfo, 2); + if (result != 0) { + goto cleanup; + } + fileOpened = TRUE; + result = NANDWrite(&fileInfo, decryptedData, 0x4000); + if (result != 0x4000) { + result = -3006; + goto cleanup; + } + result = NANDClose(&fileInfo); + fileOpened = FALSE; + +cleanup: + if (fileHeader != 0) { + MEMFreeToAllocator(allocator, fileHeader); + } + if (encryptedData != 0) { + MEMFreeToAllocator(allocator, encryptedData); + } + if (decryptedData != 0) { + MEMFreeToAllocator(allocator, decryptedData); + } + if (fileOpened) { + NANDClose(&fileInfo); + } + return result; +} + +static void _WADCleanTmpDir(MEMAllocator* allocator) { + char* names = 0; + char* name; + char filePath[NAND_MAX_PATH] ALIGN32; + u32 fileCount = 0; + u32 fileIndex; + s32 result; + + result = NANDPrivateReadDir("/tmp", 0, &fileCount); + if ((result == 0) && (fileCount != 0)) { + names = MEMAllocFromAllocator(allocator, fileCount * 0x41); + if ((names != 0) && (NANDPrivateReadDir("/tmp", names, &fileCount) == 0)) { + name = names; + for (fileIndex = 0; fileIndex < fileCount; fileIndex++) { + char* extension = strrchr(name, '.'); + if ((extension != 0) && (strncmp(extension + 1, "tmp", 3) == 0)) { + snprintf(filePath, sizeof(filePath), "/tmp/%s", name); + NANDPrivateDelete(filePath); + OSReport("%s:%d remove %s", "WADImportEx", 0x1777, filePath); + } + name += strlen(name) + 1; + } + } + } + if (names != 0) { + MEMFreeToAllocator(allocator, names); + } +} + extern const u8 ca_ppki[]; extern const u8 ms_ppki[]; extern const u8 ca_dpki[]; @@ -2268,8 +3162,8 @@ s32 WADVerify(WADStream* stream, MEMAllocator* allocator, u32 offset, u32 size) return result; } -static void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBuffer, - u32 chunkSize) { +void WAD_815C4A2C(WADImportTransfer* transfer, void* firstBuffer, void* secondBuffer, + u32 chunkSize) { transfer->chunkSize = chunkSize; transfer->error = 0; transfer->ready[1] = 0; -- 2.49.1 From e5e6a777d64ca6bb162cbc795ffb7560a7452736 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 02:58:34 +0000 Subject: [PATCH 29/34] refine WAD savedata verification --- libs/RVL_SDK/src/wad/wad.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 22882a2d..170943f3 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -2886,21 +2886,18 @@ static s32 _WADVerifySavedataZD(WADSaveDataHeader* header, WADStream* stream, goto cleanup; } result = NANDPrivateCreate(fileHeader->name, 0x34, 0); - if (result != 0) { - goto cleanup; + if ((result == 0) && (NANDPrivateOpen(fileHeader->name, &fileInfo, 2) == 0)) { + fileOpened = TRUE; + result = NANDWrite(&fileInfo, decryptedData, 0x4000); + if (result != 0x4000) { + result = -3006; + } else { + result = NANDClose(&fileInfo); + if (result == 0) { + fileOpened = FALSE; + } + } } - result = NANDPrivateOpen(fileHeader->name, &fileInfo, 2); - if (result != 0) { - goto cleanup; - } - fileOpened = TRUE; - result = NANDWrite(&fileInfo, decryptedData, 0x4000); - if (result != 0x4000) { - result = -3006; - goto cleanup; - } - result = NANDClose(&fileInfo); - fileOpened = FALSE; cleanup: if (fileHeader != 0) { -- 2.49.1 From e02767f39559bddb0561131b76ca897e47147677 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 03:07:12 +0000 Subject: [PATCH 30/34] decompile WAD DVD boot import --- libs/RVL_SDK/src/wad/wad.c | 109 +++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 170943f3..954e5c3b 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -81,6 +81,18 @@ typedef struct WADFileHeader { u8 reserved_0x60[0x20]; } WADFileHeader; +typedef struct WADBootImportParts { + s32 headerInfo[2]; + u32 certificateSize; + void* certificates; + u32 crlSize; + void* crls; + u32 ticketSize; + void* ticket; + u32 titleMetaSize; + void* titleMeta; +} WADBootImportParts; + typedef struct WADBackupFileHeader { u32 magic; u32 fileSize; @@ -2944,6 +2956,103 @@ static void _WADCleanTmpDir(MEMAllocator* allocator) { } } +s32 WADImportDVDForBS(const char* path, void* buffer, u32 bufferSize) { + DVDFileInfo fileInfo; + WADBootImportParts parts; + WADHeader* header; + ESTitleMeta* titleMeta; + u8* readBuffer = buffer; + void* applicationData; + u32 paddedFileSize; + u32 sectionOffset; + u32 applicationSize; + BOOL fileOpened = FALSE; + s32 result; + + if ((path == 0) || (buffer == 0)) { + result = -3000; + goto cleanup; + } + if (!DVDOpen(path, &fileInfo)) { + return -3004; + } + fileOpened = TRUE; + paddedFileSize = (fileInfo.length + 0x1F) & ~0x1F; + if (paddedFileSize > bufferSize) { + result = -3003; + goto cleanup; + } + if (((s32)readBuffer % 0x40) != 0) { + readBuffer += 0x20; + } + result = DVDReadPrio(&fileInfo, readBuffer, paddedFileSize, 0, 2); + if ((u32)result != paddedFileSize) { + result = -3005; + goto cleanup; + } + + memset(&parts, 0, sizeof(parts)); + header = (WADHeader*)readBuffer; + result = WAD_815C2F44(header, parts.headerInfo); + if (result != 2) { + OSReport("%s:%d Format should be iRD format: %s\n", "WADImportDVDForBS", 0x17D2, + path); + result = -3000; + goto cleanup; + } + if (parts.headerInfo[0] == 3) { + sectionOffset = (header->hdrSize + 0x3F) & ~0x3F; + if (header->certSize != 0) { + parts.certificateSize = header->certSize; + parts.certificates = readBuffer + sectionOffset; + sectionOffset += (header->certSize + 0x3F) & ~0x3F; + } + if (header->crlSize != 0) { + parts.crlSize = header->crlSize; + parts.crls = readBuffer + sectionOffset; + sectionOffset += (header->crlSize + 0x3F) & ~0x3F; + } + if (header->ticketSize != 0) { + parts.ticketSize = header->ticketSize; + parts.ticket = readBuffer + sectionOffset; + sectionOffset += (header->ticketSize + 0x3F) & ~0x3F; + } + if (header->tmdSize != 0) { + parts.titleMetaSize = header->tmdSize; + parts.titleMeta = readBuffer + sectionOffset; + sectionOffset += (header->tmdSize + 0x3F) & ~0x3F; + } + titleMeta = parts.titleMeta; + applicationSize = ((u32)titleMeta->contents[0].size + 0xF) & ~0xF; + applicationData = readBuffer + sectionOffset; + if ((titleMeta == 0) || (titleMeta->head.titleId != ES_TITLE_ID(1, 1))) { + OSReport("%s:%d This function only works for boot2 import.\n", "WADImportDVDForBS", + 0x180D); + result = -3000; + } else { + result = ES_ImportBoot(parts.ticket, parts.certificates, parts.certificateSize, + parts.titleMeta, parts.titleMetaSize, parts.certificates, + parts.certificateSize, parts.crls, parts.crlSize, applicationData, + applicationSize); + if (result != 0) { + OSReport("%s: Import Boot Failed: %d\n", "WADImportDVDForBS", result); + } else { + OSReport("%s: Import Boot Successful\n", "WADImportDVDForBS"); + } + } + } else { + OSReport("%s:%d This function only works for boot2 import.\n", "WADImportDVDForBS", + 0x1814); + result = -3000; + } + +cleanup: + if (fileOpened) { + DVDClose(&fileInfo); + } + return result; +} + extern const u8 ca_ppki[]; extern const u8 ms_ppki[]; extern const u8 ca_dpki[]; -- 2.49.1 From 11831750381b21eb8b7690f689e72386376ee05e Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 03:12:36 +0000 Subject: [PATCH 31/34] decompile WAD DVD title import --- libs/RVL_SDK/src/wad/wad.c | 180 +++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 954e5c3b..9ffc8f1f 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -3053,6 +3053,186 @@ cleanup: return result; } +s32 WADImportDVDExForBS(const char* path, void* buffer, u32 bufferSize) { + WADHeader header ALIGN32; + WADBootImportParts parts; + DVDFileInfo fileInfo; + u8* readBuffer = buffer; + u8* contentBuffer; + ESTitleMeta* titleMeta; + ESContentMeta* contentMeta; + u32 sectionOffset; + u32 remainingBufferSize; + u32 contentCount; + u32 contentIndex; + s32 contentFd; + s32 result = 0; + s32 readResult; + BOOL fileOpened = FALSE; + + if ((path == 0) || (buffer == 0)) { + result = -3000; + goto cleanup; + } + if (!DVDOpen(path, &fileInfo)) { + return -3004; + } + fileOpened = TRUE; + if (buffer == 0) { + result = -3003; + goto cleanup; + } + if (((s32)readBuffer % 0x40) != 0) { + readBuffer += 0x20; + bufferSize -= 0x20; + } + readResult = DVDReadPrio(&fileInfo, &header, sizeof(header), 0, 2); + if (readResult != sizeof(header)) { + result = -3005; + goto cleanup; + } + + memset(&parts, 0, sizeof(parts)); + result = WAD_815C2F44(&header, parts.headerInfo); + if (result != 2) { + result = -3000; + goto cleanup; + } + + sectionOffset = (header.hdrSize + 0x3F) & ~0x3F; + if (header.certSize != 0) { + parts.certificateSize = header.certSize; + parts.certificates = readBuffer + sectionOffset; + sectionOffset += (header.certSize + 0x3F) & ~0x3F; + } + if (header.crlSize != 0) { + parts.crlSize = header.crlSize; + parts.crls = readBuffer + sectionOffset; + sectionOffset += (header.crlSize + 0x3F) & ~0x3F; + } + if (header.ticketSize != 0) { + parts.ticketSize = header.ticketSize; + parts.ticket = readBuffer + sectionOffset; + sectionOffset += (header.ticketSize + 0x3F) & ~0x3F; + } + if (header.tmdSize != 0) { + parts.titleMetaSize = header.tmdSize; + parts.titleMeta = readBuffer + sectionOffset; + sectionOffset += (header.tmdSize + 0x3F) & ~0x3F; + } + titleMeta = parts.titleMeta; + remainingBufferSize = bufferSize - sectionOffset; + contentBuffer = readBuffer + sectionOffset; + if (sectionOffset > bufferSize) { + result = -3003; + goto cleanup; + } + readResult = DVDReadPrio(&fileInfo, readBuffer, sectionOffset, 0, 2); + if (readResult != sectionOffset) { + result = -3005; + goto cleanup; + } + if (parts.headerInfo[0] == 3) { + result = -3001; + goto cleanup; + } + if (parts.ticket != 0) { + result = ES_ImportTicket(parts.ticket, parts.certificates, parts.certificateSize, + parts.crls, parts.crlSize, 1); + if (result != 0) { + goto cleanup; + } + } + if (titleMeta == 0) { + goto cleanup; + } + if (parts.headerInfo[1] >= 1) { + contentCount = _WADGetCidxCount((ESContentMask*)parts.headerInfo); + if (contentCount > titleMeta->head.numContents) { + goto cleanup; + } + } else { + contentCount = titleMeta->head.numContents; + } + + result = ES_ImportTitleInit(parts.titleMeta, parts.titleMetaSize, parts.certificates, + parts.certificateSize, parts.crls, parts.crlSize, + parts.headerInfo[0], 1); + if (result != 0) { + ES_ImportTitleCancel(); + goto cleanup; + } + + contentIndex = 0; + while (contentIndex < contentCount) { + s32 titleContentIndex; + u32 remainingContentSize; + + if (parts.headerInfo[1] >= 1) { + titleContentIndex = _WADGetCidx((ESContentMask*)parts.headerInfo, contentIndex); + if ((titleContentIndex < 0) || + (titleContentIndex >= titleMeta->head.numContents)) { + ES_ImportTitleCancel(); + goto cleanup; + } + contentMeta = &titleMeta->contents[titleContentIndex]; + } else { + contentMeta = &titleMeta->contents[contentIndex]; + } + contentFd = ES_ImportContentBegin(titleMeta->head.titleId, contentMeta->cid); + if (contentFd < 0) { + result = contentFd; + ES_ImportContentEnd(contentFd); + ES_ImportTitleCancel(); + goto cleanup; + } + + remainingContentSize = ((u32)contentMeta->size + 0xF) & ~0xF; + while (remainingContentSize != 0) { + u32 chunkSize = remainingContentSize; + u32 readSize; + + if (chunkSize > remainingBufferSize) { + chunkSize = remainingBufferSize; + } + readSize = (chunkSize + 0x1F) & ~0x1F; + readResult = DVDReadPrio(&fileInfo, contentBuffer, readSize, sectionOffset, 2); + if ((u32)readResult != readSize) { + result = -3005; + ES_ImportContentEnd(contentFd); + ES_ImportTitleCancel(); + goto cleanup; + } + sectionOffset += readResult; + result = ES_ImportContentData(contentFd, contentBuffer, chunkSize); + if (result < 0) { + ES_ImportContentEnd(contentFd); + ES_ImportTitleCancel(); + goto cleanup; + } + remainingContentSize -= chunkSize; + } + sectionOffset = (sectionOffset + 0x3F) & ~0x3F; + result = ES_ImportContentEnd(contentFd); + if (result != 0) { + ES_ImportTitleCancel(); + goto cleanup; + } + contentIndex++; + } + + result = ES_ImportTitleDone(); + if (result != 0) { + ES_ImportTitleCancel(); + } + +cleanup: + if (fileOpened) { + DVDClose(&fileInfo); + } + return result; +} + extern const u8 ca_ppki[]; extern const u8 ms_ppki[]; extern const u8 ca_dpki[]; -- 2.49.1 From 3e3d34b46953720637820c1381f35617870bb847 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 03:20:19 +0000 Subject: [PATCH 32/34] refine WAD DVD stream parsing --- libs/RVL_SDK/src/wad/wad.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 9ffc8f1f..836c7c4f 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -3137,8 +3137,9 @@ s32 WADImportDVDExForBS(const char* path, void* buffer, u32 bufferSize) { goto cleanup; } if (parts.ticket != 0) { - result = ES_ImportTicket(parts.ticket, parts.certificates, parts.certificateSize, - parts.crls, parts.crlSize, 1); + result = ES_ImportTicket(parts.ticket, parts.certificates, + parts.certificateSize, parts.crls, + parts.crlSize, 1); if (result != 0) { goto cleanup; } @@ -3155,8 +3156,9 @@ s32 WADImportDVDExForBS(const char* path, void* buffer, u32 bufferSize) { contentCount = titleMeta->head.numContents; } - result = ES_ImportTitleInit(parts.titleMeta, parts.titleMetaSize, parts.certificates, - parts.certificateSize, parts.crls, parts.crlSize, + result = ES_ImportTitleInit(parts.titleMeta, parts.titleMetaSize, + parts.certificates, parts.certificateSize, + parts.crls, parts.crlSize, parts.headerInfo[0], 1); if (result != 0) { ES_ImportTitleCancel(); @@ -3169,7 +3171,8 @@ s32 WADImportDVDExForBS(const char* path, void* buffer, u32 bufferSize) { u32 remainingContentSize; if (parts.headerInfo[1] >= 1) { - titleContentIndex = _WADGetCidx((ESContentMask*)parts.headerInfo, contentIndex); + titleContentIndex = _WADGetCidx((ESContentMask*)parts.headerInfo, + contentIndex); if ((titleContentIndex < 0) || (titleContentIndex >= titleMeta->head.numContents)) { ES_ImportTitleCancel(); -- 2.49.1 From a01d17113f8ccc156d0b6cab4117881449bcc321 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 03:31:24 +0000 Subject: [PATCH 33/34] match WAD memory free --- libs/RVL_SDK/src/wad/wad.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index 836c7c4f..f323b6db 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -1761,38 +1761,36 @@ s32 _WADGetCidxCount(const ESContentMask* contentMask) { static s32 _WADGetCidx(const ESContentMask* contentMask, u32 contentNumber) { u32 remaining = contentNumber + 1; u32 bitIndex = 0; - u32 groupIndex; u32 nextIndex; - for (groupIndex = 0; groupIndex < 0x80; groupIndex++) { + for (bitIndex = 0; bitIndex < 0x200; bitIndex += 4) { if ((contentMask->data[(s32)bitIndex >> 3] & (1 << (bitIndex & 7))) != 0) { remaining--; } if (remaining == 0) { return bitIndex; } - nextIndex = ++bitIndex; + nextIndex = bitIndex + 1; if ((contentMask->data[(s32)nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { remaining--; } if (remaining == 0) { return nextIndex; } - nextIndex = ++bitIndex; + nextIndex = bitIndex + 2; if ((contentMask->data[(s32)nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { remaining--; } if (remaining == 0) { return nextIndex; } - nextIndex = ++bitIndex; + nextIndex = bitIndex + 3; if ((contentMask->data[(s32)nextIndex >> 3] & (1 << (nextIndex & 7))) != 0) { remaining--; } if (remaining == 0) { return nextIndex; } - bitIndex++; } return -1; } @@ -2066,13 +2064,10 @@ static void* _WADMemAlloc(MEMAllocator* allocator, u32 size) { } static void _WADMemFree(MEMAllocator* allocator, void* buffer) { - if (allocator != 0) { - if (allocator->heap != 0) { - if (buffer != 0) { - MEMFreeToAllocator(allocator, buffer); - } - } + if ((allocator == 0) || (allocator->heap == 0) || (buffer == 0)) { + return; } + MEMFreeToAllocator(allocator, buffer); } #pragma dont_inline reset -- 2.49.1 From 2defef0e3a97ba8181f40127329b31d333f3e767 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Tue, 29 Sep 2026 03:40:28 +0000 Subject: [PATCH 34/34] match WAD allocator and content checks --- libs/RVL_SDK/src/wad/wad.c | 77 ++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/libs/RVL_SDK/src/wad/wad.c b/libs/RVL_SDK/src/wad/wad.c index f323b6db..0f2c89e1 100644 --- a/libs/RVL_SDK/src/wad/wad.c +++ b/libs/RVL_SDK/src/wad/wad.c @@ -1696,13 +1696,12 @@ cleanup: } static s32 _WADCheckContents(ESTmdView* titleMeta, ESContentMask* contentMask) { - ESContentId installedContentIds[520] ALIGN32; + ESContentId installedContentIds[512] ALIGN32; u32 installedContentCount; u32 contentIndex; s32 result; - result = ES_ListTitleContentsOnCard(titleMeta->head.titleId, 0, &installedContentCount); - if (result == -106) { + if (ES_ListTitleContentsOnCard(titleMeta->head.titleId, 0, &installedContentCount) == -106) { result = -3002; } else { result = ES_ListTitleContentsOnCard(titleMeta->head.titleId, installedContentIds, @@ -1710,19 +1709,19 @@ static s32 _WADCheckContents(ESTmdView* titleMeta, ESContentMask* contentMask) { if (result == 0) { memset(contentMask, 0, sizeof(*contentMask)); for (contentIndex = 0; contentIndex < titleMeta->head.numContents; contentIndex++) { - ESCmdView* content = &titleMeta->contents[contentIndex]; u32 installedIndex; - if ((content->type & 0x8000) == 0) { + if ((titleMeta->contents[contentIndex].type & 0x8000) == 0) { for (installedIndex = 0; installedIndex < installedContentCount; installedIndex++) { - if (installedContentIds[installedIndex] == content->cid) { + if (installedContentIds[installedIndex] == + titleMeta->contents[contentIndex].cid) { contentMask->data[contentIndex >> 3] |= 1 << (contentIndex & 7); break; } } if ((installedIndex == installedContentCount) && - ((content->type & 0x4000) == 0)) { + ((titleMeta->contents[contentIndex].type & 0x4000) == 0)) { result = -3002; goto done; } @@ -2027,38 +2026,42 @@ static s32 _WADBackupGetSize(u32 flags, ESTmdView* titleMeta, ESContentMask* con static void* _WADMemAlloc(MEMAllocator* allocator, u32 size) { s32 allocationKind; u32 heapType; - void* buffer; + void* buffer = 0; - if ((allocator == 0) || (allocator->heap == 0)) { - return 0; - } - heapType = ((MEMiHeapHead*)allocator->heap)->magic; - switch (heapType) { - case 0x46524D48: - allocationKind = 1; - break; - case 0x45585048: - allocationKind = 0; - break; - case 0x554E5448: - allocationKind = 2; - break; - default: - allocationKind = 3; - break; - } + if ((allocator != 0) && (allocator->heap != 0)) { + heapType = ((MEMiHeapHead*)allocator->heap)->magic; + switch (heapType) { + case 0x45585048: + allocationKind = 0; + break; + case 0x46524D48: + allocationKind = 1; + break; + case 0x554E5448: + allocationKind = 2; + break; + default: + allocationKind = 3; + break; + } - if (allocationKind == 1) { - return MEMAllocFromFrmHeapEx(allocator->heap, size, 0x40); - } - if (allocationKind == 0) { - return MEMAllocFromExpHeapEx(allocator->heap, size, 0x40); - } - buffer = MEMAllocFromAllocator(allocator, size); - if (((u32)buffer & 0x3F) != 0) { - OSReport("%s: Memory Allocator must return 64B aligned memBlocks\n", "_WADMemAlloc"); - MEMFreeToAllocator(allocator, buffer); - return 0; + switch (allocationKind) { + case 0: + buffer = MEMAllocFromExpHeapEx(allocator->heap, size, 0x40); + break; + case 1: + buffer = MEMAllocFromFrmHeapEx(allocator->heap, size, 0x40); + break; + case 2: + default: + buffer = MEMAllocFromAllocator(allocator, size); + if (((u32)buffer & 0x3F) != 0) { + OSReport("%s: Memory Allocator must return 64B aligned memBlocks\n", "_WADMemAlloc"); + MEMFreeToAllocator(allocator, buffer); + buffer = 0; + } + break; + } } return buffer; } -- 2.49.1