diff --git a/config/SOUE01/splits.txt b/config/SOUE01/splits.txt index 09cd2076..0e181c52 100644 --- a/config/SOUE01/splits.txt +++ b/config/SOUE01/splits.txt @@ -23,6 +23,14 @@ d/d_base.cpp: .data start:0x80503380 end:0x805033CC .sbss start:0x805750C0 end:0x805750CC +toBeSorted/sceneflag_manager.cpp: + .text start:0x800BD8C0 end:0x800BE7A8 + .sbss start:0x805753E0 end:0x805753F0 + +toBeSorted/flag_space.cpp: + .text start:0x800BEE00 end:0x800BEF90 + .data start:0x80510D04 end:0x80510D10 + toBeSorted/unk_flag_stuff.cpp: .text start:0x800BEF90 end:0x800BF200 diff --git a/config/SOUE01/symbols.txt b/config/SOUE01/symbols.txt index 43795a37..7bdae9c3 100644 --- a/config/SOUE01/symbols.txt +++ b/config/SOUE01/symbols.txt @@ -271,22 +271,22 @@ fn_8000A3E0 = .text:0x8000A3E0; // type:function size:0x44 fn_8000A430 = .text:0x8000A430; // type:function size:0x24 fn_8000A460 = .text:0x8000A460; // type:function size:0x44 fn_8000A4B0 = .text:0x8000A4B0; // type:function size:0x24 -fn_8000A4E0 = .text:0x8000A4E0; // type:function size:0x44 +getSceneflags__11FileManagerFv = .text:0x8000A4E0; // type:function size:0x44 fn_8000A530 = .text:0x8000A530; // type:function size:0x24 fn_8000A560 = .text:0x8000A560; // type:function size:0x44 fn_8000A5B0 = .text:0x8000A5B0; // type:function size:0x24 -fn_8000A5E0 = .text:0x8000A5E0; // type:function size:0x44 +getTempflags__11FileManagerFv = .text:0x8000A5E0; // type:function size:0x44 fn_8000A630 = .text:0x8000A630; // type:function size:0x24 -fn_8000A660 = .text:0x8000A660; // type:function size:0x44 +getZoneflags__11FileManagerFv = .text:0x8000A660; // type:function size:0x44 fn_8000A6B0 = .text:0x8000A6B0; // type:function size:0x24 fn_8000A6E0 = .text:0x8000A6E0; // type:function size:0x44 fn_8000A730 = .text:0x8000A730; // type:function size:0x58 fn_8000A790 = .text:0x8000A790; // type:function size:0x58 fn_8000A7F0 = .text:0x8000A7F0; // type:function size:0x58 -fn_8000A850 = .text:0x8000A850; // type:function size:0x58 +setSceneflags__11FileManagerFPUsUiUs = .text:0x8000A850; // type:function size:0x58 fn_8000A8B0 = .text:0x8000A8B0; // type:function size:0x58 -fn_8000A910 = .text:0x8000A910; // type:function size:0x58 -fn_8000A970 = .text:0x8000A970; // type:function size:0x58 +setTempflags__11FileManagerFPUsUsUs = .text:0x8000A910; // type:function size:0x58 +setZoneflags__11FileManagerFPUsUsUs = .text:0x8000A970; // type:function size:0x58 fn_8000A9D0 = .text:0x8000A9D0; // type:function size:0x58 fn_8000AA30 = .text:0x8000AA30; // type:function size:0xC fn_8000AA40 = .text:0x8000AA40; // type:function size:0x5C @@ -4076,39 +4076,39 @@ fn_800BD7A0 = .text:0x800BD7A0; // type:function size:0x74 fn_800BD820 = .text:0x800BD820; // type:function size:0x8 fn_800BD830 = .text:0x800BD830; // type:function size:0x78 fn_800BD8B0 = .text:0x800BD8B0; // type:function size:0x10 -fn_800BD8C0 = .text:0x800BD8C0; // type:function size:0x4 -fn_800BD8D0 = .text:0x800BD8D0; // type:function size:0xC -fn_800BD8E0 = .text:0x800BD8E0; // type:function size:0x64 -fn_800BD950 = .text:0x800BD950; // type:function size:0xC -fn_800BD960 = .text:0x800BD960; // type:function size:0x20 -fn_800BD980 = .text:0x800BD980; // type:function size:0x70 -fn_800BD9F0 = .text:0x800BD9F0; // type:function size:0x94 -fn_800BDA90 = .text:0x800BDA90; // type:function size:0x40 -fn_800BDAD0 = .text:0x800BDAD0; // type:function size:0x40 -fn_800BDB10 = .text:0x800BDB10; // type:function size:0x4C -fn_800BDB60 = .text:0x800BDB60; // type:function size:0x3C -fn_800BDBA0 = .text:0x800BDBA0; // type:function size:0x34 -fn_800BDBE0 = .text:0x800BDBE0; // type:function size:0x48 -fn_800BDC30 = .text:0x800BDC30; // type:function size:0x10 -fn_800BDC40 = .text:0x800BDC40; // type:function size:0x10 -fn_800BDC50 = .text:0x800BDC50; // type:function size:0x8 -fn_800BDC60 = .text:0x800BDC60; // type:function size:0x8 -fn_800BDC70 = .text:0x800BDC70; // type:function size:0xA8 -fn_800BDD20 = .text:0x800BDD20; // type:function size:0xA8 -fn_800BDDD0 = .text:0x800BDDD0; // type:function size:0xDC -fn_800BDEB0 = .text:0x800BDEB0; // type:function size:0x70 -fn_800BDF20 = .text:0x800BDF20; // type:function size:0x94 -fn_800BDFC0 = .text:0x800BDFC0; // type:function size:0xA0 -fn_800BE060 = .text:0x800BE060; // type:function size:0x70 -fn_800BE0D0 = .text:0x800BE0D0; // type:function size:0xB0 -fn_800BE180 = .text:0x800BE180; // type:function size:0x70 -fn_800BE1F0 = .text:0x800BE1F0; // type:function size:0xE0 -fn_800BE2D0 = .text:0x800BE2D0; // type:function size:0x104 -fn_800BE3E0 = .text:0x800BE3E0; // type:function size:0xAC -fn_800BE490 = .text:0x800BE490; // type:function size:0x70 -fn_800BE500 = .text:0x800BE500; // type:function size:0xE0 -fn_800BE5E0 = .text:0x800BE5E0; // type:function size:0x100 -fn_800BE6E0 = .text:0x800BE6E0; // type:function size:0xC8 +doNothing__16SceneflagManagerFv = .text:0x800BD8C0; // type:function size:0x4 +setShouldCommit__16SceneflagManagerFUs = .text:0x800BD8D0; // type:function size:0xC +__ct__16SceneflagManagerFv = .text:0x800BD8E0; // type:function size:0x64 +isNotTempOrZoneFlag__16SceneflagManagerFUs = .text:0x800BD950; // type:function size:0xC +isZoneFlag__16SceneflagManagerFUi = .text:0x800BD960; // type:function size:0x20 +updateFlagindex__16SceneflagManagerFUs = .text:0x800BD980; // type:function size:0x70 +copyFromSave__16SceneflagManagerFUi = .text:0x800BD9F0; // type:function size:0x94 +unsetAllTempflags__16SceneflagManagerFv = .text:0x800BDA90; // type:function size:0x40 +zoneflagsResetAll__16SceneflagManagerFv = .text:0x800BDAD0; // type:function size:0x40 +zoneflagsResetForRoom__16SceneflagManagerFUs = .text:0x800BDB10; // type:function size:0x4C +unsetZoneAndTempflags__16SceneflagManagerFv = .text:0x800BDB60; // type:function size:0x3C +unsetAllZoneflags__16SceneflagManagerFv = .text:0x800BDBA0; // type:function size:0x34 +unsetZoneflagsForRoom__16SceneflagManagerFUs = .text:0x800BDBE0; // type:function size:0x48 +getZoneflagSlot__16SceneflagManagerFUsUs = .text:0x800BDC30; // type:function size:0x10 +getSceneflagSlotGlobal__16SceneflagManagerFUsUs = .text:0x800BDC40; // type:function size:0x10 +getSceneflagSlot__16SceneflagManagerFUs = .text:0x800BDC50; // type:function size:0x8 +getTempflagSlot__16SceneflagManagerFUs = .text:0x800BDC60; // type:function size:0x8 +checkZoneFlag__16SceneflagManagerFUsUs = .text:0x800BDC70; // type:function size:0xA8 +checkUncommittedZoneflag__16SceneflagManagerFUsUs = .text:0x800BDD20; // type:function size:0xA8 +checkUncommittedTempOrSceneflag__16SceneflagManagerFUs = .text:0x800BDDD0; // type:function size:0xDC +checkFlag__16SceneflagManagerFUsUs = .text:0x800BDEB0; // type:function size:0x70 +checkSceneflagGlobal__16SceneflagManagerFUsUs = .text:0x800BDF20; // type:function size:0x94 +checkTempOrSceneflag__16SceneflagManagerFUs = .text:0x800BDFC0; // type:function size:0xA0 +checkUncommittedFlag__16SceneflagManagerFUsUs = .text:0x800BE060; // type:function size:0x70 +setZoneflag__16SceneflagManagerFUsUs = .text:0x800BE0D0; // type:function size:0xB0 +setFlag__16SceneflagManagerFUsUs = .text:0x800BE180; // type:function size:0x70 +setSceneflagGlobal__16SceneflagManagerFUsUs = .text:0x800BE1F0; // type:function size:0xE0 +setTempOrSceneflag__16SceneflagManagerFUs = .text:0x800BE2D0; // type:function size:0x104 +unsetZoneflag__16SceneflagManagerFUsUs = .text:0x800BE3E0; // type:function size:0xAC +unsetFlag__16SceneflagManagerFUsUs = .text:0x800BE490; // type:function size:0x70 +unsetSceneflagGlobal__16SceneflagManagerFUsUs = .text:0x800BE500; // type:function size:0xE0 +unsetTempOrSceneflag__16SceneflagManagerFUs = .text:0x800BE5E0; // type:function size:0x100 +doCommit__16SceneflagManagerFv = .text:0x800BE6E0; // type:function size:0xC8 fn_800BE7B0 = .text:0x800BE7B0; // type:function size:0x58 fn_800BE810 = .text:0x800BE810; // type:function size:0x58 fn_800BE870 = .text:0x800BE870; // type:function size:0x68 @@ -4129,13 +4129,13 @@ fn_800BEC90 = .text:0x800BEC90; // type:function size:0x18 fn_800BECB0 = .text:0x800BECB0; // type:function size:0xA0 fn_800BED50 = .text:0x800BED50; // type:function size:0x8 fn_800BED60 = .text:0x800BED60; // type:function size:0x94 -fn_800BEE00 = .text:0x800BEE00; // type:function size:0x3C -fn_800BEE40 = .text:0x800BEE40; // type:function size:0x8 -fn_800BEE50 = .text:0x800BEE50; // type:function size:0xC -fn_800BEE60 = .text:0x800BEE60; // type:function size:0x68 -fn_800BEED0 = .text:0x800BEED0; // type:function size:0x7C -fn_800BEF50 = .text:0x800BEF50; // type:function size:0x20 -fn_800BEF70 = .text:0x800BEF70; // type:function size:0x20 +getFlagPtrChecked__9FlagSpaceFv = .text:0x800BEE00; // type:function size:0x3C +getFlagPtrUnchecked__9FlagSpaceFv = .text:0x800BEE40; // type:function size:0x8 +unsetAll__9FlagSpaceFv = .text:0x800BEE50; // type:function size:0xC +setAllToZero__9FlagSpaceFUsUs = .text:0x800BEE60; // type:function size:0x68 +copyFromSaveFile2__9FlagSpaceFPUsUsUs = .text:0x800BEED0; // type:function size:0x7C +copyFromSaveFile__9FlagSpaceFPUsUsUs = .text:0x800BEF50; // type:function size:0x20 +filemanagerCheck__9FlagSpaceFv = .text:0x800BEF70; // type:function size:0x20 calculateMask__12UnkFlagStuffFi = .text:0x800BEF90; // type:function size:0x18 doNothing__12UnkFlagStuffFUi = .text:0x800BEFB0; // type:function size:0x4 __ct__12UnkFlagStuffFUsP17UnkFlagDefinition = .text:0x800BEFC0; // type:function size:0xC @@ -4144,9 +4144,9 @@ maskForIdx__12UnkFlagStuffFUs = .text:0x800BF050; // type:function size:0x50 getCounterOrFlag__12UnkFlagStuffFUsPUsUi = .text:0x800BF0A0; // type:function size:0x68 setCounterOrFlag__12UnkFlagStuffFUsPUsUiUi = .text:0x800BF110; // type:function size:0x9C checkFlagValid__12UnkFlagStuffFUsUi = .text:0x800BF1B0; // type:function size:0x50 -checkFlag__17BitwiseFlagHelperFUsUsPUs = .text:0x800BF200; // type:function size:0x14 -setFlag__17BitwiseFlagHelperFUsUsPUs = .text:0x800BF220; // type:function size:0x20 -unsetFlag__17BitwiseFlagHelperFUsUsPUs = .text:0x800BF240; // type:function size:0x24 +checkFlag__17BitwiseFlagHelperFUsUsPCUsUs = .text:0x800BF200; // type:function size:0x14 +setFlag__17BitwiseFlagHelperFUsUsPUsUs = .text:0x800BF220; // type:function size:0x20 +unsetFlag__17BitwiseFlagHelperFUsUsPUsUs = .text:0x800BF240; // type:function size:0x24 fn_800BF270 = .text:0x800BF270; // type:function size:0x28 fn_800BF2A0 = .text:0x800BF2A0; // type:function size:0x70 fn_800BF310 = .text:0x800BF310; // type:function size:0xC @@ -4205,7 +4205,7 @@ fn_800C01E0 = .text:0x800C01E0; // type:function size:0x8 fn_800C01F0 = .text:0x800C01F0; // type:function size:0x78 fn_800C0270 = .text:0x800C0270; // type:function size:0x58 fn_800C02D0 = .text:0x800C02D0; // type:function size:0x54 -fn_800C0330 = .text:0x800C0330; // type:function size:0x54 +checkedMemcpy = .text:0x800C0330; // type:function size:0x54 fn_800C0390 = .text:0x800C0390; // type:function size:0x8 fn_800C03A0 = .text:0x800C03A0; // type:function size:0x8 fn_800C03B0 = .text:0x800C03B0; // type:function size:0x8 @@ -31022,7 +31022,7 @@ lbl_80510C64 = .data:0x80510C64; // type:object size:0x3C lbl_80510CA0 = .data:0x80510CA0; // type:object size:0x3C lbl_80510CDC = .data:0x80510CDC; // type:object size:0x14 lbl_80510CF0 = .data:0x80510CF0; // type:object size:0x14 -lbl_80510D04 = .data:0x80510D04; // type:object size:0xC +__vt__9FlagSpace = .data:0x80510D04; // type:object size:0xC lbl_80510D10 = .data:0x80510D10; // type:object size:0x38 data:4byte lbl_80510D48 = .data:0x80510D48; // type:object size:0x18 lbl_80510D60 = .data:0x80510D60; // type:object size:0xC @@ -39639,7 +39639,7 @@ lbl_80574FE4 = .sbss:0x80574FE4; // type:object size:0x4 data:4byte lbl_80574FE8 = .sbss:0x80574FE8; // type:object size:0x4 data:4byte lbl_80574FEC = .sbss:0x80574FEC; // type:object size:0xC lbl_80574FF8 = .sbss:0x80574FF8; // type:object size:0x4 -lbl_80574FFC = .sbss:0x80574FFC; // type:object size:0x4 data:4byte +sInstance__11FileManager = .sbss:0x80574FFC; // type:object size:0x4 data:4byte lbl_80575000 = .sbss:0x80575000; // type:object size:0x8 data:4byte lbl_80575008 = .sbss:0x80575008; // type:object size:0x8 data:4byte lbl_80575010 = .sbss:0x80575010; // type:object size:0x8 data:4byte @@ -39848,8 +39848,8 @@ lbl_805753C4 = .sbss:0x805753C4; // type:object size:0x1 data:byte lbl_805753C8 = .sbss:0x805753C8; // type:object size:0x8 data:4byte lbl_805753D0 = .sbss:0x805753D0; // type:object size:0x8 data:4byte lbl_805753D8 = .sbss:0x805753D8; // type:object size:0x8 data:4byte -lbl_805753E0 = .sbss:0x805753E0; // type:object size:0x8 data:4byte -lbl_805753E8 = .sbss:0x805753E8; // type:object size:0x8 +sInstance__16SceneflagManager = .sbss:0x805753E0; // type:object size:0x8 data:4byte +sTempFlags__16SceneflagManager = .sbss:0x805753E8; // type:object size:0x8 lbl_805753F0 = .sbss:0x805753F0; // type:object size:0x4 data:4byte lbl_805753F4 = .sbss:0x805753F4; // type:object size:0x4 lbl_805753F8 = .sbss:0x805753F8; // type:object size:0x4 data:4byte @@ -48218,8 +48218,8 @@ lbl_805A7730 = .bss:0x805A7730; // type:object size:0xC4 data:byte lbl_805A77F4 = .bss:0x805A77F4; // type:object size:0xC4 data:byte lbl_805A78B8 = .bss:0x805A78B8; // type:object size:0xC lbl_805A78C4 = .bss:0x805A78C4; // type:object size:0xC data:4byte -lbl_805A78D0 = .bss:0x805A78D0; // type:object size:0x10 -lbl_805A78E0 = .bss:0x805A78E0; // type:object size:0x1F8 +sSceneFlags__16SceneflagManager = .bss:0x805A78D0; // type:object size:0x10 +sZoneFlags__16SceneflagManager = .bss:0x805A78E0; // type:object size:0x1F8 lbl_805A7AD8 = .bss:0x805A7AD8; // type:object size:0x2000 lbl_805A9AD8 = .bss:0x805A9AD8; // type:object size:0x100 lbl_805A9BD8 = .bss:0x805A9BD8; // type:object size:0x80 diff --git a/configure.py b/configure.py index 78d6c882..4e86be3b 100644 --- a/configure.py +++ b/configure.py @@ -10,6 +10,8 @@ LIBS = [ ["toBeSorted/unk_flag_stuff.cpp", True], ["toBeSorted/bitwise_flag_helper.cpp", True], ["d/d_base.cpp", True], + ["toBeSorted/sceneflag_manager.cpp", True], + ["toBeSorted/flag_space.cpp", False], ["d/a/d_a_base.cpp", False], ["d/a/obj/d_a_obj_base.cpp", False], ["toBeSorted/save_file.cpp", False], diff --git a/include/libc.h b/include/libc.h new file mode 100644 index 00000000..d8f834a6 --- /dev/null +++ b/include/libc.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +extern "C" { + +void* memset(void* dest, s32 value, u32 count); +void* memcpy(void* dest, const void* src, u32 count); +void* checkedMemcpy(void* dest, u16 destLen, const void* src, u16 count); + +} diff --git a/include/toBeSorted/bitwise_flag_helper.h b/include/toBeSorted/bitwise_flag_helper.h new file mode 100644 index 00000000..fa917cec --- /dev/null +++ b/include/toBeSorted/bitwise_flag_helper.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +class BitwiseFlagHelper { + public: + bool checkFlag(u16 slot, u16 shift, const u16 *pData, u16 flagCount); + void setFlag(u16 slot, u16 shift, u16 *pData, u16 flagCount); + void unsetFlag(u16 slot, u16 shift, u16 *pData, u16 flagCount); +}; \ No newline at end of file diff --git a/include/toBeSorted/file_manager.h b/include/toBeSorted/file_manager.h new file mode 100644 index 00000000..3dec323c --- /dev/null +++ b/include/toBeSorted/file_manager.h @@ -0,0 +1,20 @@ +#include + +// stub +struct FileManager { + u8 pad[0xa840]; + u8 isFileUnk1[3]; + static FileManager *sInstance; + u16* getSceneflags(); + u16* getTempflags(); + u16* getZoneflags(); + u16* getSceneflags2(); + u16* getTempflags2(); + u16* getZoneflags2(); + void setTempflags(u16*, u16, u16); + void setZoneflags(u16*, u16, u16); + void setSceneflags(u16*, u32, u16); + static FileManager* getInstance() { + return sInstance; + } +}; diff --git a/include/toBeSorted/flag_space.h b/include/toBeSorted/flag_space.h new file mode 100644 index 00000000..12ea3603 --- /dev/null +++ b/include/toBeSorted/flag_space.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +struct BaseFlagSpace { + u16 *mpFlags; + u16 mCount; + BaseFlagSpace(u16 *pFlags, u16 count): mpFlags(pFlags), mCount(count) {} +}; + +class FlagSpace: public BaseFlagSpace { + public: + void init(u16 *pFlags, u16 count) { + mpFlags = pFlags; + mCount = count; + } + FlagSpace(u16 *pFlags, u16 count): BaseFlagSpace(pFlags, count) {} + u16* getFlagPtrChecked(); + u16* getFlagPtrUnchecked(); + void unsetAll(); + void setAllToZero(u16 offset, u16 flagCount); + void copyFromSaveFile2(u16 *pSaved, u16 offset, u16 flagCount); + void copyFromSaveFile(u16 *pSaved, u16 offset, u16 flagCount); + virtual void filemanagerCheck(); +}; \ No newline at end of file diff --git a/include/toBeSorted/save_file.h b/include/toBeSorted/save_file.h index ce6943a7..5aff34cd 100644 --- a/include/toBeSorted/save_file.h +++ b/include/toBeSorted/save_file.h @@ -118,7 +118,7 @@ public: // 0x80009AA0 u16* getUnkFlags1(); // 0x80009AB0 - s16* getPlayerName(); // using Shift JIS i assume + s16* getPlayerName(); // UTF16-BE // 0x80009AC0 void setAreaT1(char* name); // 0x80009BE0 diff --git a/src/toBeSorted/bitwise_flag_helper.cpp b/src/toBeSorted/bitwise_flag_helper.cpp index a2e2794b..1cd8d979 100644 --- a/src/toBeSorted/bitwise_flag_helper.cpp +++ b/src/toBeSorted/bitwise_flag_helper.cpp @@ -1,18 +1,12 @@ #include +#include -class BitwiseFlagHelper { - public: - bool checkFlag(u16 slot, u16 shift, u16 *pData); - void setFlag(u16 slot, u16 shift, u16 *pData); - void unsetFlag(u16 slot, u16 shift, u16 *pData); -}; - -bool BitwiseFlagHelper::checkFlag(u16 slot, u16 shift, u16 *pData) { +bool BitwiseFlagHelper::checkFlag(u16 slot, u16 shift, const u16 *pData, u16 flagCount) { return (pData[slot] >> shift) & 1; } -void BitwiseFlagHelper::setFlag(u16 slot, u16 shift, u16 *pData) { +void BitwiseFlagHelper::setFlag(u16 slot, u16 shift, u16 *pData, u16 flagCount) { pData[slot] |= (1 << shift); } -void BitwiseFlagHelper::unsetFlag(u16 slot, u16 shift, u16 *pData) { +void BitwiseFlagHelper::unsetFlag(u16 slot, u16 shift, u16 *pData, u16 flagCount) { pData[slot] &= ~(1 << shift); } \ No newline at end of file diff --git a/src/toBeSorted/flag_space.cpp b/src/toBeSorted/flag_space.cpp new file mode 100644 index 00000000..8ec2810e --- /dev/null +++ b/src/toBeSorted/flag_space.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +u16* FlagSpace::getFlagPtrChecked() { + filemanagerCheck(); + return mpFlags; +} +u16* FlagSpace::getFlagPtrUnchecked() { + return mpFlags; +} +void FlagSpace::unsetAll() { + setAllToZero(0, mCount); +} +void FlagSpace::setAllToZero(u16 offset, u16 flagCount) { + filemanagerCheck(); + memset(mpFlags + offset, 0, (u16)(flagCount * 2)); +} +void FlagSpace::copyFromSaveFile2(u16 *pSaved, u16 offset, u16 flagCount) { + filemanagerCheck(); + checkedMemcpy(mpFlags + offset, mCount * 2, pSaved, flagCount * 2); +} + +void FlagSpace::copyFromSaveFile(u16 *pSaved, u16 offset, u16 flagCount) { + checkedMemcpy(mpFlags + offset, mCount * 2, pSaved, flagCount * 2); + +} +void FlagSpace::filemanagerCheck() { + if (FileManager::sInstance->isFileUnk1[0]) { + return; + } + while (true) {} +} \ No newline at end of file diff --git a/src/toBeSorted/sceneflag_manager.cpp b/src/toBeSorted/sceneflag_manager.cpp new file mode 100644 index 00000000..a37406be --- /dev/null +++ b/src/toBeSorted/sceneflag_manager.cpp @@ -0,0 +1,329 @@ +#include +#include + +#include "lib/rvl/macros.h" +#include "toBeSorted/flag_space.h" +#include "toBeSorted/file_manager.h" +#include + +class SceneflagManager { + public: + FlagSpace mSceneflags; + FlagSpace mTempflags; + FlagSpace mZoneflags; + BitwiseFlagHelper mFlagHelper; + u16 mSceneIdx; + u8 mShouldCommit; + + static u16 sTempFlags[4]; + static u16 sSceneFlags[8]; + static u16 sZoneFlags[0xFC]; +// public: + static SceneflagManager* sInstance; + void doNothing(); + void setShouldCommit(u16 flag); + SceneflagManager(); + s32 isNotTempOrZoneFlag(u16 flag); + s32 isZoneFlag(u32 flag); + void updateFlagindex(u16 flagindex); + void copyFromSave(u32 flagindex); + void unsetAllTempflags(); + void zoneflagsResetAll(); + void zoneflagsResetForRoom(u16 roomId); + void unsetZoneAndTempflags(); + void unsetAllZoneflags(); + void unsetZoneflagsForRoom(u16 roomId); + u16 getZoneflagSlot(u16 roomId, u16 flag); + u16 getSceneflagSlotGlobal(u16 sceneIdx, u16 flag); + u16 getSceneflagSlot(u16 flag); + u16 getTempflagSlot(u16 flag); + bool checkZoneFlag(u16 roomId, u16 flag); + bool checkUncommittedZoneflag(u16 roomId, u16 flag); + u16 checkUncommittedZoneflag2(u16 roomId, u16 flag) { + return checkUncommittedZoneflag(roomId, flag); + } + bool checkUncommittedTempOrSceneflag(u16 flag); + u16 checkUncommittedTempOrSceneflag2(u16 flag) { + return checkUncommittedTempOrSceneflag(flag); + } + bool checkFlag(u16 roomId, u16 flag); + bool checkSceneflagGlobal(u16 sceneIdx, u16 flag); + bool checkTempOrSceneflag(u16 flag); + bool checkUncommittedFlag(u16 roomId, u16 flag); + void setZoneflag(u16 roomId, u16 flag); + void setFlag(u16 roomId, u16 flag); + void setSceneflagGlobal(u16 sceneIdx, u16 flag); + void setTempOrSceneflag(u16 flag); + void unsetZoneflag(u16 roomId, u16 flag); + void unsetFlag(u16 roomId, u16 flag); + void unsetSceneflagGlobal(u16 sceneIdx, u16 flag); + void unsetTempOrSceneflag(u16 flag); + s32 doCommit(); +}; + +SceneflagManager *SceneflagManager::sInstance = nullptr; +u16 SceneflagManager::sTempFlags[] = {}; + +void SceneflagManager::doNothing() {} +void SceneflagManager::setShouldCommit(u16 flag) { + mShouldCommit = 1; +} +SceneflagManager::SceneflagManager(): + mSceneflags(sSceneFlags, ARRAY_LENGTH(sSceneFlags)), + mTempflags(sTempFlags, ARRAY_LENGTH(sTempFlags)), + mZoneflags(sZoneFlags, ARRAY_LENGTH(sZoneFlags)) + { + mSceneIdx = 0xFFFF; + mShouldCommit = 0; + } +// SceneflagManager::SceneflagManager() +// { +// mSceneflags.init(sSceneFlags, ARRAY_LENGTH(sSceneFlags)); +// mTempflags.init(sTempFlags, ARRAY_LENGTH(sTempFlags)); +// mZoneflags.init(sZoneFlags, ARRAY_LENGTH(sZoneFlags)); +// mSceneIdx = 0xFFFF; +// mShouldCommit = 0; +// } +s32 SceneflagManager::isNotTempOrZoneFlag(u16 flag) { + // return ((s32)flag - 0x80) < 0; + return flag < 0x80; +} + +inline s32 possiblyZoneFlag(u32 flag) { + return flag >= 0xC0; +} + +s32 SceneflagManager::isZoneFlag(u32 flag) { + if (possiblyZoneFlag(flag) && flag < 0x100) { + return 1; + } else { + return 0; + } + // return flag >= 0xC0 && flag < 0x100; +} +void SceneflagManager::updateFlagindex(u16 sceneIdx) { + if (sceneIdx != mSceneIdx) { + mSceneIdx = sceneIdx; + mSceneflags.copyFromSaveFile2(FileManager::sInstance->getSceneflags() + sceneIdx * 8, 0, 8); + unsetZoneAndTempflags(); + } +} +void SceneflagManager::copyFromSave(u32 sceneIdx) { + // sceneIdx &= 0xFFFF; + u16 sceneIdx2 = sceneIdx; + // missing clrlwi, more inlines? + mSceneIdx = sceneIdx2; + mSceneflags.copyFromSaveFile2(FileManager::getInstance()->getSceneflags() + sceneIdx2 * 8, 0, 8); + mTempflags.copyFromSaveFile(FileManager::getInstance()->getTempflags(), 0, mTempflags.mCount); + mZoneflags.copyFromSaveFile(FileManager::getInstance()->getZoneflags(), 0, mZoneflags.mCount); +} +void SceneflagManager::unsetAllTempflags() { + u16 flags[4]; + memset(flags, 0, sizeof(flags)); + FileManager::sInstance->setTempflags(flags, 0, 4); +} +void SceneflagManager::zoneflagsResetAll() { + u16 flags[0xFC]; + memset(flags, 0, sizeof(flags)); + FileManager::sInstance->setZoneflags(flags, 0, 0xFC); +} +void SceneflagManager::zoneflagsResetForRoom(u16 roomId) { + u16 flags[4]; + memset(flags, 0, sizeof(flags)); + // ? weird mask + FileManager::sInstance->setZoneflags(flags, (roomId & 0x3FF) * 4, 4); +} +void SceneflagManager::unsetZoneAndTempflags() { + unsetAllTempflags(); + mTempflags.unsetAll(); + unsetAllZoneflags(); +} +void SceneflagManager::unsetAllZoneflags() { + zoneflagsResetAll(); + mZoneflags.unsetAll(); +} +void SceneflagManager::unsetZoneflagsForRoom(u16 roomId) { + zoneflagsResetForRoom(roomId); + // ? weird mask + mZoneflags.setAllToZero((roomId & 0x3FF) * 4, 4); +} +u16 SceneflagManager::getZoneflagSlot(u16 roomId, u16 flag) { + return (flag + roomId * 0x40) >> 4 & 0xFFF; +} +u16 SceneflagManager::getSceneflagSlotGlobal(u16 sceneIdx, u16 flag) { + return (flag + sceneIdx * 0x80) >> 4 & 0xFFF; +} +u16 SceneflagManager::getSceneflagSlot(u16 flag) { + return flag >> 4 & 0xFFF; +} +u16 SceneflagManager::getTempflagSlot(u16 flag) { + return flag >> 4 & 0xFFF; +} +bool SceneflagManager::checkZoneFlag(u16 roomId, u16 flag) { + if (flag == 0xFF) { + return false; + } else { + u16 zoneflag = flag - 0xC0; + u16* pData = FileManager::sInstance->getZoneflags(); + u16 slot = getZoneflagSlot(roomId, zoneflag); + return mFlagHelper.checkFlag(slot, zoneflag % 16, pData, 0xFC); + } +} +bool SceneflagManager::checkUncommittedZoneflag(u16 roomId, u16 flag) { + if (flag == 0xFF) { + return false; + } else { + u16* pData; + u16 zoneflag = flag - 0xC0; + pData = mZoneflags.getFlagPtrUnchecked(); + u16 slot = getZoneflagSlot(roomId, zoneflag); + return mFlagHelper.checkFlag(slot, zoneflag % 16, pData, mZoneflags.mCount); + } +} +inline bool checkSceneflag(SceneflagManager* mgr, u16 flag) { + u16* pData; + pData = mgr->mSceneflags.getFlagPtrUnchecked(); + // u16 slot = getSceneflagSlot2(flag); + return mgr->mFlagHelper.checkFlag(mgr->getSceneflagSlot(flag), flag % 16, pData, mgr->mSceneflags.mCount); +} +bool SceneflagManager::checkUncommittedTempOrSceneflag(u16 flag) { + if (flag >= 0x80) { + u16* pData; + u16 tempflag = flag - 0x80; + pData = mTempflags.getFlagPtrUnchecked(); + u16 slot = getTempflagSlot(tempflag); + return mFlagHelper.checkFlag(slot, tempflag % 16, pData, mTempflags.mCount); + } else { + return ::checkSceneflag(this, flag); + // u16* pData; + // pData = mSceneflags.getFlagPtr(); + // // u16 slot = getSceneflagSlot2(flag); + // mFlagHelper.checkFlag(getSceneflagSlot2(flag), flag % 16, pData, mSceneflags.mCount); + } +} +bool SceneflagManager::checkFlag(u16 roomId, u16 flag) { + if (isZoneFlag(flag)) { + return checkZoneFlag(roomId, flag); + } else { + return checkTempOrSceneflag(flag); + } +} +bool SceneflagManager::checkSceneflagGlobal(u16 sceneIdx, u16 flag) { + u16* pData = FileManager::sInstance->getSceneflags(); + return mFlagHelper.checkFlag(getSceneflagSlotGlobal(sceneIdx, flag), flag % 16, pData, 0x800); +} +bool SceneflagManager::checkTempOrSceneflag(u16 flag) { + if (flag >= 0x80) { + u16 tempflag = flag - 0x80; + const u16* pData = FileManager::sInstance->getTempflags(); + u16 slot = getTempflagSlot(tempflag); + return mFlagHelper.checkFlag(slot, tempflag % 16, pData, 4); + } else { + return checkSceneflagGlobal(mSceneIdx, flag); + } +} +bool SceneflagManager::checkUncommittedFlag(u16 roomId, u16 flag) { + if (isZoneFlag(flag)) { + return checkUncommittedZoneflag(roomId, flag); + } else { + return checkUncommittedTempOrSceneflag(flag); + } +} +void SceneflagManager::setZoneflag(u16 roomId, u16 flag) { + if (checkUncommittedZoneflag2(roomId, flag) != 1 && flag != 0xFF) { + u16* pData; + u16 zoneflag = flag - 0xC0; + pData = mZoneflags.getFlagPtrChecked(); + u16 slot = getZoneflagSlot(roomId, zoneflag); + mFlagHelper.setFlag(slot, zoneflag % 16, pData, mZoneflags.mCount); + setShouldCommit(flag); + } +} +void SceneflagManager::setFlag(u16 roomId, u16 flag) { + if (isZoneFlag(flag)) { + setZoneflag(roomId, flag); + } else { + setTempOrSceneflag(flag); + } +} +void SceneflagManager::setSceneflagGlobal(u16 sceneIdx, u16 flag) { + u16 slot = getSceneflagSlotGlobal(sceneIdx, flag); + u16* pData = FileManager::sInstance->getSceneflags(); + u16 pCurData = pData[slot]; + mFlagHelper.setFlag(0, flag % 16, &pCurData, 2); + FileManager::sInstance->setSceneflags(&pCurData, slot, 1); + if (sceneIdx == mSceneIdx) { + u16* pData2 = mSceneflags.getFlagPtrChecked(); + mFlagHelper.setFlag(getSceneflagSlot(flag), flag % 16, pData2, mSceneflags.mCount); + } + setShouldCommit(flag); +} +void SceneflagManager::setTempOrSceneflag(u16 flag) { + if (checkUncommittedTempOrSceneflag2(flag) != 1) { + u16* pData; + if (flag >= 0x80) { + u16 tempflag = flag - 0x80; + pData = mTempflags.getFlagPtrChecked(); + mFlagHelper.setFlag(getTempflagSlot(tempflag), tempflag % 16, pData, mTempflags.mCount); + } else { + pData = mSceneflags.getFlagPtrChecked(); + mFlagHelper.setFlag(getSceneflagSlot(flag), flag % 16, pData, mSceneflags.mCount); + } + setShouldCommit(flag); + } +} +void SceneflagManager::unsetZoneflag(u16 roomId, u16 flag) { + if (checkUncommittedZoneflag2(roomId, flag) != 0 && flag != 0xFF) { + u16* pData; + u16 zoneflag = flag - 0xC0; + pData = mZoneflags.getFlagPtrChecked(); + u16 slot = getZoneflagSlot(roomId, zoneflag); + mFlagHelper.unsetFlag(slot, zoneflag % 16, pData, mZoneflags.mCount); + setShouldCommit(flag); + } +} +void SceneflagManager::unsetFlag(u16 roomId, u16 flag) { + if (isZoneFlag(flag)) { + unsetZoneflag(roomId, flag); + } else { + unsetTempOrSceneflag(flag); + } +} +void SceneflagManager::unsetSceneflagGlobal(u16 sceneIdx, u16 flag) { + u16 slot = getSceneflagSlotGlobal(sceneIdx, flag); + u16* pData = FileManager::sInstance->getSceneflags(); + u16 pCurData = pData[slot]; + mFlagHelper.unsetFlag(0, flag % 16, &pCurData, 2); + FileManager::sInstance->setSceneflags(&pCurData, slot, 1); + if (sceneIdx == mSceneIdx) { + u16* pData2 = mSceneflags.getFlagPtrChecked(); + mFlagHelper.unsetFlag(getSceneflagSlot(flag), flag % 16, pData2, mSceneflags.mCount); + } + setShouldCommit(flag); +} +void SceneflagManager::unsetTempOrSceneflag(u16 flag) { + if (checkUncommittedTempOrSceneflag2(flag) != 0) { + u16* pData; + if (flag >= 0x80) { + u16 tempflag = flag - 0x80; + pData = mTempflags.getFlagPtrChecked(); + mFlagHelper.unsetFlag(getTempflagSlot(tempflag), tempflag % 16, pData, mTempflags.mCount); + } else { + pData = mSceneflags.getFlagPtrChecked(); + mFlagHelper.unsetFlag(getSceneflagSlot(flag), flag % 16, pData, mSceneflags.mCount); + } + setShouldCommit(flag); + } +} +s32 SceneflagManager::doCommit() { + if (mSceneIdx == 0xFFFF) { + return 0; + } else if (mShouldCommit) { + FileManager::getInstance()->setSceneflags(mSceneflags.getFlagPtrUnchecked(), mSceneIdx * 8, 8); + FileManager::getInstance()->setTempflags(mTempflags.getFlagPtrUnchecked(), 0, mTempflags.mCount); + FileManager::getInstance()->setZoneflags(mZoneflags.getFlagPtrUnchecked(), 0, mZoneflags.mCount); + mShouldCommit = false; + return 1; + } + return 0; +}