Skip to content

Commit 774c6b4

Browse files
committed
use a reference in ReplaySystem::get_instance
thx zmx
1 parent 4fe1b81 commit 774c6b4

4 files changed

Lines changed: 54 additions & 57 deletions

File tree

src/hooks.cpp

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,26 @@ void Hooks::init() {
3939
}
4040

4141
void __fastcall Hooks::CCScheduler_update_H(CCScheduler* self, int, float dt) {
42-
auto rs = ReplaySystem::get_instance();
43-
if (rs->is_playing() || rs->is_recording() && gd::GameManager::sharedState()->getPlayLayer()) {
44-
auto fps = rs->get_replay().get_fps();
42+
auto& rs = ReplaySystem::get_instance();
43+
if (rs.is_playing() || rs.is_recording() && gd::GameManager::sharedState()->getPlayLayer()) {
44+
auto fps = rs.get_replay().get_fps();
4545
auto speedhack = self->getTimeScale();
4646
dt = 1.f / fps / speedhack;
4747
}
4848
CCScheduler_update(self, dt);
4949
}
5050

5151
void __fastcall Hooks::CCKeyboardDispatcher_dispatchKeyboardMSG_H(CCKeyboardDispatcher* self, int, int key, bool down) {
52-
auto rs = ReplaySystem::get_instance();
52+
auto& rs = ReplaySystem::get_instance();
5353
if (down) {
5454
auto play_layer = gd::GameManager::sharedState()->getPlayLayer();
55-
if (rs->is_recording() && play_layer) {
55+
if (rs.is_recording() && play_layer) {
5656
if (key == 'C') {
57-
rs->set_frame_advance(false);
58-
PlayLayer::update_H(play_layer, 0, 1.f / rs->get_default_fps());
59-
rs->set_frame_advance(true);
57+
rs.set_frame_advance(false);
58+
PlayLayer::update_H(play_layer, 0, 1.f / rs.get_default_fps());
59+
rs.set_frame_advance(true);
6060
} else if (key == 'F') {
61-
rs->set_frame_advance(false);
61+
rs.set_frame_advance(false);
6262
} else if (key == 'R') {
6363
PlayLayer::resetLevel_H(play_layer, 0);
6464
}
@@ -73,19 +73,19 @@ bool __fastcall Hooks::PlayLayer::init_H(gd::PlayLayer* self, int, void* level)
7373
}
7474

7575
void __fastcall Hooks::PlayLayer::update_H(gd::PlayLayer* self, int, float dt) {
76-
auto rs = ReplaySystem::get_instance();
77-
if (rs->get_frame_advance()) return;
78-
if (rs->is_playing())
79-
rs->handle_playing();
76+
auto& rs = ReplaySystem::get_instance();
77+
if (rs.get_frame_advance()) return;
78+
if (rs.is_playing())
79+
rs.handle_playing();
8080
update(self, dt);
8181
}
8282

8383

8484
bool _player_button_handler(bool hold, bool button) {
8585
if (gd::GameManager::sharedState()->getPlayLayer()) {
86-
auto rs = ReplaySystem::get_instance();
87-
if (rs->is_playing()) return true;
88-
rs->record_action(hold, button);
86+
auto& rs = ReplaySystem::get_instance();
87+
if (rs.is_playing()) return true;
88+
rs.record_action(hold, button);
8989
}
9090
return false;
9191
}
@@ -102,18 +102,18 @@ int __fastcall Hooks::PlayLayer::releaseButton_H(gd::PlayLayer* self, int, int i
102102

103103
int __fastcall Hooks::PlayLayer::resetLevel_H(gd::PlayLayer* self, int) {
104104
auto ret = resetLevel(self);
105-
ReplaySystem::get_instance()->on_reset();
105+
ReplaySystem::get_instance().on_reset();
106106
return ret;
107107
}
108108

109109

110110
void __fastcall Hooks::PlayLayer::pauseGame_H(gd::PlayLayer* self, int, bool idk) {
111111
auto addr = cast<void*>(gd::base + 0x20D43C);
112-
auto rs = ReplaySystem::get_instance();
113-
if (rs->is_recording())
114-
rs->record_action(false, true, false);
112+
auto& rs = ReplaySystem::get_instance();
113+
if (rs.is_recording())
114+
rs.record_action(false, true, false);
115115

116-
bool should_patch = rs->is_playing();
116+
bool should_patch = rs.is_playing();
117117
if (should_patch)
118118
patch(addr, {0x83, 0xC4, 0x04, 0x90, 0x90});
119119

@@ -125,26 +125,26 @@ void __fastcall Hooks::PlayLayer::pauseGame_H(gd::PlayLayer* self, int, bool idk
125125

126126

127127
int __fastcall Hooks::PlayLayer::createCheckpoint_H(gd::PlayLayer* self, int) {
128-
auto rs = ReplaySystem::get_instance();
129-
if (rs->is_recording()) rs->get_practice_fixes().add_checkpoint();
128+
auto& rs = ReplaySystem::get_instance();
129+
if (rs.is_recording()) rs.get_practice_fixes().add_checkpoint();
130130
return createCheckpoint(self);
131131
}
132132

133133
void* __fastcall Hooks::PlayLayer::removeLastCheckpoint_H(gd::PlayLayer* self, int) {
134-
ReplaySystem::get_instance()->get_practice_fixes().remove_checkpoint();
134+
ReplaySystem::get_instance().get_practice_fixes().remove_checkpoint();
135135
return removeLastCheckpoint(self);
136136
}
137137

138138

139139
void* __fastcall Hooks::PlayLayer::levelComplete_H(gd::PlayLayer* self, int) {
140-
ReplaySystem::get_instance()->reset_state();
140+
ReplaySystem::get_instance().reset_state();
141141
return levelComplete(self);
142142
}
143143

144144
void _on_exit_level() {
145-
auto rs = ReplaySystem::get_instance();
146-
rs->get_practice_fixes().clear_checkpoints();
147-
rs->reset_state();
145+
auto& rs = ReplaySystem::get_instance();
146+
rs.get_practice_fixes().clear_checkpoints();
147+
rs.reset_state();
148148
}
149149

150150
void* __fastcall Hooks::PlayLayer::onQuit_H(gd::PlayLayer* self, int) {
@@ -182,18 +182,18 @@ bool __fastcall Hooks::PauseLayer_init_H(gd::PauseLayer* self, int) {
182182

183183
void __fastcall Hooks::PlayerObject_ringJump_H(gd::PlayerObject* self, int, gd::GameObject* ring) {
184184
PlayerObject_ringJump(self, ring);
185-
auto rs = ReplaySystem::get_instance();
185+
auto& rs = ReplaySystem::get_instance();
186186
auto play_layer = gd::GameManager::sharedState()->getPlayLayer();
187-
if (play_layer && play_layer->is_practice_mode && rs->is_recording() && ring->m_hasBeenActivated) {
188-
rs->get_practice_fixes().add_activated_object(ring);
187+
if (play_layer && play_layer->is_practice_mode && rs.is_recording() && ring->m_hasBeenActivated) {
188+
rs.get_practice_fixes().add_activated_object(ring);
189189
}
190190
}
191191

192192
void __fastcall Hooks::GameObject_activateObject_H(gd::GameObject* self, int, gd::PlayerObject* player) {
193193
GameObject_activateObject(self, player);
194-
auto rs = ReplaySystem::get_instance();
194+
auto& rs = ReplaySystem::get_instance();
195195
auto play_layer = gd::GameManager::sharedState()->getPlayLayer();
196-
if (play_layer && play_layer->is_practice_mode && rs->is_recording() && self->m_hasBeenActivated) {
197-
rs->get_practice_fixes().add_activated_object(self);
196+
if (play_layer && play_layer->is_practice_mode && rs.is_recording() && self->m_hasBeenActivated) {
197+
rs.get_practice_fixes().add_activated_object(self);
198198
}
199199
}

src/overlay_layer.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ bool OverlayLayer::init() {
99
setZOrder(20);
1010

1111
auto win_size = CCDirector::sharedDirector()->getWinSize();
12-
auto rs = ReplaySystem::get_instance();
12+
auto& rs = ReplaySystem::get_instance();
1313

1414
auto menu = CCMenu::create();
1515
menu->setPosition({0, win_size.height});
@@ -81,7 +81,7 @@ bool OverlayLayer::init() {
8181
addChild(sprite);
8282

8383
m_fps_input = gd::CCTextInputNode::create("fps", nullptr, "bigFont.fnt", 100.f, 100.f);
84-
m_fps_input->setString(std::to_string(static_cast<int>(rs->get_default_fps())).c_str());
84+
m_fps_input->setString(std::to_string(static_cast<int>(rs.get_default_fps())).c_str());
8585
m_fps_input->setLabelPlaceholderColor({200, 200, 200});
8686
m_fps_input->setLabelPlaceholerScale(0.5f);
8787
m_fps_input->setMaxLabelScale(0.7f);
@@ -110,8 +110,8 @@ bool OverlayLayer::init() {
110110
}
111111

112112
void OverlayLayer::update_info_text() {
113-
auto rs = ReplaySystem::get_instance();
114-
auto& replay = rs->get_replay();
113+
auto& rs = ReplaySystem::get_instance();
114+
auto& replay = rs.get_replay();
115115
std::stringstream stream;
116116
stream << "Current Replay:\nFPS: " << replay.get_fps() << "\nActions: " << replay.get_actions().size();
117117
m_replay_info->setString(stream.str().c_str());
@@ -120,15 +120,15 @@ void OverlayLayer::update_info_text() {
120120
void OverlayLayer::_update_default_fps() {
121121
auto text = m_fps_input->getString();
122122
if (text[0])
123-
ReplaySystem::get_instance()->set_default_fps(std::stof(text));
123+
ReplaySystem::get_instance().set_default_fps(std::stof(text));
124124
}
125125

126126
void OverlayLayer::FLAlert_Clicked(gd::FLAlertLayer* alert, bool btn2) {
127127
if (!btn2) {
128-
auto rs = ReplaySystem::get_instance();
128+
auto& rs = ReplaySystem::get_instance();
129129
int tag = alert->getTag();
130130
if (tag == 1) {
131-
rs->toggle_recording();
131+
rs.toggle_recording();
132132
update_info_text();
133133
} else if (tag == 2) {
134134
_handle_load_replay();
@@ -139,10 +139,10 @@ void OverlayLayer::FLAlert_Clicked(gd::FLAlertLayer* alert, bool btn2) {
139139

140140
void OverlayLayer::on_record(CCObject*) {
141141
_update_default_fps();
142-
auto rs = ReplaySystem::get_instance();
143-
if (!rs->is_recording()) {
144-
if (rs->get_replay().get_actions().empty()) {
145-
rs->toggle_recording();
142+
auto& rs = ReplaySystem::get_instance();
143+
if (!rs.is_recording()) {
144+
if (rs.get_replay().get_actions().empty()) {
145+
rs.toggle_recording();
146146
update_info_text();
147147
} else {
148148
auto alert = gd::FLAlertLayer::create(this,
@@ -154,12 +154,12 @@ void OverlayLayer::on_record(CCObject*) {
154154
alert->show();
155155
}
156156
} else {
157-
rs->toggle_recording();
157+
rs.toggle_recording();
158158
}
159159
}
160160

161161
void OverlayLayer::on_play(CCObject*) {
162-
ReplaySystem::get_instance()->toggle_playing();
162+
ReplaySystem::get_instance().toggle_playing();
163163
}
164164

165165
void OverlayLayer::on_save(CCObject*) {
@@ -168,7 +168,7 @@ void OverlayLayer::on_save(CCObject*) {
168168
if (result == NFD_OKAY) {
169169
std::string s_path(path);
170170
// why doesnt it just add the extension for me
171-
ReplaySystem::get_instance()->get_replay().save(s_path + ".replay");
171+
ReplaySystem::get_instance().get_replay().save(s_path + ".replay");
172172
gd::FLAlertLayer::create(nullptr, "Info", "Ok", nullptr, "Replay saved.")->show();
173173
free(path);
174174
}
@@ -178,16 +178,16 @@ void OverlayLayer::_handle_load_replay() {
178178
nfdchar_t* path = nullptr;
179179
auto result = NFD_OpenDialog("replay", nullptr, &path);
180180
if (result == NFD_OKAY) {
181-
ReplaySystem::get_instance()->get_replay() = Replay::load(path);
181+
ReplaySystem::get_instance().get_replay() = Replay::load(path);
182182
update_info_text();
183183
gd::FLAlertLayer::create(nullptr, "Info", "Ok", nullptr, "Replay loaded.")->show();
184184
free(path);
185185
}
186186
}
187187

188188
void OverlayLayer::on_load(CCObject*) {
189-
auto rs = ReplaySystem::get_instance();
190-
if (rs->get_replay().get_actions().empty()) {
189+
auto& rs = ReplaySystem::get_instance();
190+
if (rs.get_replay().get_actions().empty()) {
191191
_handle_load_replay();
192192
} else {
193193
auto alert = gd::FLAlertLayer::create(this,

src/replay_system.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "replay_system.hpp"
22
#include "hooks.hpp"
3-
ReplaySystem* ReplaySystem::instance;
43

54
void ReplaySystem::record_action(bool hold, bool player1, bool flip) {
65
if (is_recording()) {

src/replay_system.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ enum RSState {
1010
};
1111

1212
class ReplaySystem {
13-
static ReplaySystem* instance;
14-
1513
float default_fps;
1614

1715
Replay replay;
@@ -27,9 +25,9 @@ class ReplaySystem {
2725

2826
void _update_status_label();
2927
public:
30-
static ReplaySystem* get_instance() {
31-
if (!instance) instance = new ReplaySystem();
32-
return instance;
28+
static auto& get_instance() {
29+
static ReplaySystem instance;
30+
return instance;
3331
}
3432

3533
inline auto& get_replay() { return replay; }

0 commit comments

Comments
 (0)