forked from Wirless/IdlersMapEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.cpp
More file actions
1043 lines (912 loc) · 27.4 KB
/
action.cpp
File metadata and controls
1043 lines (912 loc) · 27.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#include "main.h"
#include "action.h"
#include "settings.h"
#include "map.h"
#include "editor.h"
#include "gui.h"
// Add necessary includes for exception handling and file operations
#include <exception>
#include <fstream>
#include <wx/filename.h>
#include <wx/datetime.h>
#include <wx/stdpaths.h>
Change::Change() :
type(CHANGE_NONE), data(nullptr) {
////
}
Change::Change(Tile* t) :
type(CHANGE_TILE) {
ASSERT(t);
data = t;
}
Change* Change::Create(House* house, const Position& where) {
Change* c = newd Change();
c->type = CHANGE_MOVE_HOUSE_EXIT;
std::pair<uint32_t, Position>* p = newd std::pair<uint32_t, Position>;
p->first = house->getID();
p->second = where;
c->data = p;
return c;
}
Change* Change::Create(Waypoint* wp, const Position& where) {
Change* c = newd Change();
c->type = CHANGE_MOVE_WAYPOINT;
std::pair<std::string, Position>* p = newd std::pair<std::string, Position>;
p->first = wp->name;
p->second = where;
c->data = p;
return c;
}
Change::~Change() {
clear();
}
void Change::clear() {
switch (type) {
case CHANGE_TILE:
ASSERT(data);
delete reinterpret_cast<Tile*>(data);
break;
case CHANGE_MOVE_HOUSE_EXIT:
ASSERT(data);
delete reinterpret_cast<std::pair<uint32_t, Position>*>(data);
break;
case CHANGE_MOVE_WAYPOINT:
ASSERT(data);
delete reinterpret_cast<std::pair<std::string, Position>*>(data);
break;
case CHANGE_NONE:
break;
default:
#ifdef __DEBUG_MODE__
if (data) {
printf("UNHANDLED CHANGE TYPE! Leak!");
}
#endif
break;
}
type = CHANGE_NONE;
data = nullptr;
}
uint32_t Change::memsize() const {
uint32_t mem = sizeof(*this);
switch (type) {
case CHANGE_TILE:
ASSERT(data);
mem += reinterpret_cast<Tile*>(data)->memsize();
break;
default:
break;
}
return mem;
}
Action::Action(Editor& editor, ActionIdentifier ident) :
commited(false),
editor(editor),
type(ident) {
}
Action::~Action() {
ChangeList::const_reverse_iterator it = changes.rbegin();
while (it != changes.rend()) {
delete *it;
++it;
}
}
size_t Action::approx_memsize() const {
uint32_t mem = sizeof(*this);
mem += changes.size() * (sizeof(Change) + sizeof(Tile) + sizeof(Item) + 6 /* approx overhead*/);
return mem;
}
size_t Action::memsize() const {
uint32_t mem = sizeof(*this);
mem += sizeof(Change*) * 3 * changes.size();
ChangeList::const_iterator it = changes.begin();
while (it != changes.end()) {
Change* c = *it;
switch (c->type) {
case CHANGE_TILE: {
ASSERT(c->data);
mem += reinterpret_cast<Tile*>(c->data)->memsize();
break;
}
default:
break;
}
++it;
}
return mem;
}
void Action::commit(DirtyList* dirty_list) {
editor.selection.start(Selection::INTERNAL);
ChangeList::const_iterator it = changes.begin();
while (it != changes.end()) {
Change* c = *it;
switch (c->type) {
case CHANGE_TILE: {
void** data = &c->data;
Tile* newtile = reinterpret_cast<Tile*>(*data);
ASSERT(newtile);
Position pos = newtile->getPosition();
if (editor.IsLiveClient()) {
QTreeNode* nd = editor.map.getLeaf(pos.x, pos.y);
if (!nd || !nd->isVisible(pos.z > GROUND_LAYER)) {
// Delete all changes that affect tiles outside our view
c->clear();
++it;
continue;
}
}
Tile* oldtile = editor.map.swapTile(pos, newtile);
TileLocation* location = newtile->getLocation();
// Update other nodes in the network
if (editor.IsLiveServer() && dirty_list) {
dirty_list->AddPosition(pos.x, pos.y, pos.z);
}
newtile->update();
// std::cout << "\tSwitched tile at " << pos.x << ";" << pos.y << ";" << pos.z << " from " << (void*)oldtile << " to " << *data << std::endl;
if (newtile->isSelected()) {
editor.selection.addInternal(newtile);
}
if (oldtile) {
if (newtile->getHouseID() != oldtile->getHouseID()) {
// oooooomggzzz we need to add it to the appropriate house!
House* house = editor.map.houses.getHouse(oldtile->getHouseID());
if (house) {
house->removeTile(oldtile);
}
house = editor.map.houses.getHouse(newtile->getHouseID());
if (house) {
house->addTile(newtile);
}
}
if (oldtile->spawn) {
if (newtile->spawn) {
if (*oldtile->spawn != *newtile->spawn) {
editor.map.removeSpawn(oldtile);
editor.map.addSpawn(newtile);
}
} else {
// Spawn has been removed
editor.map.removeSpawn(oldtile);
}
} else if (newtile->spawn) {
editor.map.addSpawn(newtile);
}
// oldtile->update();
if (oldtile->isSelected()) {
editor.selection.removeInternal(oldtile);
}
*data = oldtile;
} else {
*data = editor.map.allocator(location);
if (newtile->getHouseID() != 0) {
// oooooomggzzz we need to add it to the appropriate house!
House* house = editor.map.houses.getHouse(newtile->getHouseID());
if (house) {
house->addTile(newtile);
}
}
if (newtile->spawn) {
editor.map.addSpawn(newtile);
}
}
// Mark the tile as modified
newtile->modify();
// Update client dirty list
if (editor.IsLiveClient() && dirty_list && type != ACTION_REMOTE) {
// Local action, assemble changes
dirty_list->AddChange(c);
}
break;
}
case CHANGE_MOVE_HOUSE_EXIT: {
std::pair<uint32_t, Position>* p = reinterpret_cast<std::pair<uint32_t, Position>*>(c->data);
ASSERT(p);
House* whathouse = editor.map.houses.getHouse(p->first);
if (whathouse) {
Position oldpos = whathouse->getExit();
whathouse->setExit(p->second);
p->second = oldpos;
}
break;
}
case CHANGE_MOVE_WAYPOINT: {
std::pair<std::string, Position>* p = reinterpret_cast<std::pair<std::string, Position>*>(c->data);
ASSERT(p);
Waypoint* wp = editor.map.waypoints.getWaypoint(p->first);
if (wp) {
// Change the tiles
TileLocation* oldtile = editor.map.getTileL(wp->pos);
TileLocation* newtile = editor.map.getTileL(p->second);
// Only need to remove from old if it actually exists
if (p->second != Position()) {
if (oldtile && oldtile->getWaypointCount() > 0) {
oldtile->decreaseWaypointCount();
}
}
newtile->increaseWaypointCount();
// Update shit
Position oldpos = wp->pos;
wp->pos = p->second;
p->second = oldpos;
}
break;
}
default:
break;
}
++it;
}
editor.selection.finish(Selection::INTERNAL);
commited = true;
}
void Action::undo(DirtyList* dirty_list) {
if (changes.empty()) {
return;
}
editor.selection.start(Selection::INTERNAL);
ChangeList::reverse_iterator it = changes.rbegin();
while (it != changes.rend()) {
Change* c = *it;
switch (c->type) {
case CHANGE_TILE: {
void** data = &c->data;
Tile* oldtile = reinterpret_cast<Tile*>(*data);
ASSERT(oldtile);
Position pos = oldtile->getPosition();
if (editor.IsLiveClient()) {
QTreeNode* nd = editor.map.getLeaf(pos.x, pos.y);
if (!nd || !nd->isVisible(pos.z > GROUND_LAYER)) {
// Delete all changes that affect tiles outside our view
c->clear();
++it;
continue;
}
}
Tile* newtile = editor.map.swapTile(pos, oldtile);
// Update server side change list (for broadcast)
if (editor.IsLiveServer() && dirty_list) {
dirty_list->AddPosition(pos.x, pos.y, pos.z);
}
if (oldtile->isSelected()) {
editor.selection.addInternal(oldtile);
}
if (newtile->isSelected()) {
editor.selection.removeInternal(newtile);
}
if (newtile->getHouseID() != oldtile->getHouseID()) {
// oooooomggzzz we need to remove it from the appropriate house!
House* house = editor.map.houses.getHouse(newtile->getHouseID());
if (house) {
house->removeTile(newtile);
} else {
// Set tile house to 0, house has been removed
newtile->setHouse(nullptr);
}
house = editor.map.houses.getHouse(oldtile->getHouseID());
if (house) {
house->addTile(oldtile);
}
}
if (oldtile->spawn) {
if (newtile->spawn) {
if (*oldtile->spawn != *newtile->spawn) {
editor.map.removeSpawn(newtile);
editor.map.addSpawn(oldtile);
}
} else {
editor.map.addSpawn(oldtile);
}
} else if (newtile->spawn) {
editor.map.removeSpawn(newtile);
}
*data = newtile;
// Update client dirty list
if (editor.IsLiveClient() && dirty_list && type != ACTION_REMOTE) {
// Local action, assemble changes
dirty_list->AddChange(c);
}
break;
}
case CHANGE_MOVE_HOUSE_EXIT: {
std::pair<uint32_t, Position>* p = reinterpret_cast<std::pair<uint32_t, Position>*>(c->data);
ASSERT(p);
House* whathouse = editor.map.houses.getHouse(p->first);
if (whathouse) {
Position oldpos = whathouse->getExit();
whathouse->setExit(p->second);
p->second = oldpos;
}
break;
}
case CHANGE_MOVE_WAYPOINT: {
std::pair<std::string, Position>* p = reinterpret_cast<std::pair<std::string, Position>*>(c->data);
ASSERT(p);
Waypoint* wp = editor.map.waypoints.getWaypoint(p->first);
if (wp) {
// Change the tiles
TileLocation* oldtile = editor.map.getTileL(wp->pos);
TileLocation* newtile = editor.map.getTileL(p->second);
// Only need to remove from old if it actually exists
if (p->second != Position()) {
if (oldtile && oldtile->getWaypointCount() > 0) {
oldtile->decreaseWaypointCount();
}
}
if (newtile) {
newtile->increaseWaypointCount();
}
// Update shit
Position oldpos = wp->pos;
wp->pos = p->second;
p->second = oldpos;
}
break;
}
default:
break;
}
++it;
}
editor.selection.finish(Selection::INTERNAL);
commited = false;
}
BatchAction::BatchAction(Editor& editor, ActionIdentifier ident) :
editor(editor),
timestamp(0),
memory_size(0),
type(ident) {
////
}
BatchAction::~BatchAction() {
try {
// Make a copy of the batch to avoid modifying while iterating
ActionVector batchCopy;
batchCopy.swap(batch); // Swap is safer than copying
// Now safely delete each action in the copy
for (ActionVector::iterator it = batchCopy.begin(); it != batchCopy.end(); ++it) {
try {
Action* action = *it;
if (action) {
delete action;
}
} catch (const std::exception& e) {
// Log error but continue cleanup
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error in BatchAction destructor: " << e.what() << "\n";
logFile.close();
}
}
}
// Ensure batch is empty (should already be from the swap)
batch.clear();
} catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Critical error in BatchAction destructor: " << e.what() << "\n";
logFile.close();
}
}
}
size_t BatchAction::memsize(bool recalc) const {
// Expensive operation, only evaluate once (won't change anyways)
if (!recalc && memory_size > 0) {
return memory_size;
}
uint32_t mem = sizeof(*this);
mem += sizeof(Action*) * 3 * batch.size();
for (Action* action : batch) {
#ifdef __USE_EXACT_MEMSIZE__
mem += action->memsize();
#else
// Less exact but MUCH faster
mem += action->approx_memsize();
#endif
}
const_cast<BatchAction*>(this)->memory_size = mem;
return mem;
}
void BatchAction::addAction(Action* action) {
try {
// Safety check for null action
if (!action) {
return;
}
// If empty, do nothing.
if (action->size() == 0) {
delete action;
return;
}
ASSERT(action->getType() == type);
if (!editor.CanEdit()) {
delete action;
return;
}
// Add it!
batch.push_back(action);
timestamp = time(nullptr);
}
catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error in BatchAction::addAction: " << e.what() << "\n";
logFile.close();
}
// Try to clean up action if possible
try {
delete action;
}
catch (...) {
// Ignore cleanup errors
}
}
}
void BatchAction::addAndCommitAction(Action* action) {
// If empty, do nothing.
if (action->size() == 0) {
delete action;
return;
}
if (!editor.CanEdit()) {
delete action;
return;
}
// Add it!
action->commit(nullptr);
batch.push_back(action);
timestamp = time(nullptr);
}
void BatchAction::commit() {
for (Action* action : batch) {
if (!action->isCommited()) {
action->commit(nullptr);
}
}
}
void BatchAction::undo() {
for (Action* action : boost::adaptors::reverse(batch)) {
action->undo(nullptr);
}
}
void BatchAction::redo() {
for (Action* action : batch) {
action->redo(nullptr);
}
}
void BatchAction::merge(BatchAction* other) {
try {
// Safety check for null pointer
if (!other) {
return;
}
// Reserve space to avoid multiple reallocations
batch.reserve(batch.size() + other->batch.size());
// Move actions from other batch to this one
for (ActionVector::iterator it = other->batch.begin(); it != other->batch.end(); /* no increment here */) {
Action* action = *it;
it = other->batch.erase(it); // Remove from source vector before adding to destination
if (action) {
batch.push_back(action);
}
}
// Clear the other batch's vector (should already be empty)
other->batch.clear();
} catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error merging batch actions: " << e.what() << "\n";
logFile.close();
}
}
}
ActionQueue::ActionQueue(Editor& editor) :
current(0), memory_size(0), editor(editor) {
////
}
ActionQueue::~ActionQueue() {
try {
for (auto it = actions.begin(); it != actions.end(); /* no increment here */) {
BatchAction* action = *it;
it = actions.erase(it); // Remove from container before deleting
try {
delete action;
} catch (const std::exception& e) {
// Log error but continue cleanup
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error deleting action in queue destructor: " << e.what() << "\n";
logFile.close();
}
}
}
actions.clear(); // Ensure container is empty
} catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error in ActionQueue destructor: " << e.what() << "\n";
logFile.close();
}
}
}
Action* ActionQueue::createAction(ActionIdentifier ident) {
return newd Action(editor, ident);
}
Action* ActionQueue::createAction(BatchAction* batch) {
return newd Action(editor, batch->getType());
}
BatchAction* ActionQueue::createBatch(ActionIdentifier ident) {
return newd BatchAction(editor, ident);
}
void ActionQueue::resetTimer() {
if (!actions.empty()) {
actions.back()->resetTimer();
}
}
void ActionQueue::addAction(Action* action, int stacking_delay) {
// Ensure we're on the main thread
if (!wxThread::IsMain()) {
// Use CallAfter with a copy of parameters to prevent race conditions
Action* actionPtr = action;
int delay = stacking_delay;
wxTheApp->CallAfter([this, actionPtr, delay]() {
this->addAction(actionPtr, delay);
});
return;
}
// Safety check for null action
if (!action) {
return;
}
try {
// If empty, do nothing.
if (action->size() == 0) {
delete action;
return;
}
BatchAction* batch = createBatch(action->getType());
if (!batch) {
delete action;
return;
}
try {
batch->addAndCommitAction(action);
// If batch is empty after adding action, clean up and return
if (batch->size() == 0) {
delete batch;
return;
}
addBatch(batch, stacking_delay);
}
catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error in addAction: " << e.what() << "\n";
logFile.close();
}
// Clean up if exception occurred
delete batch;
}
}
catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Critical error in addAction: " << e.what() << "\n";
logFile.close();
}
// Try to clean up action if possible
try {
delete action;
}
catch (...) {
// Ignore cleanup errors
}
}
}
void ActionQueue::addBatch(BatchAction* batch, int stacking_delay) {
// Ensure we're on the main thread
if (!wxThread::IsMain()) {
// Use CallAfter but with a 'copy' of needed parameters
// to prevent race conditions
ActionQueue* self = this;
int delay = stacking_delay;
wxTheApp->CallAfter([self, batch, delay]() {
self->addBatch(batch, delay);
});
return;
}
// Safety check - if batch is null, just return
if (!batch) {
return;
}
// Additional safety check to catch any potential crashes
try {
ASSERT(current <= actions.size());
if (batch->size() == 0) {
delete batch;
return;
}
// Commit any uncommited actions...
batch->commit();
// Update title
if (editor.map.doChange()) {
// Use a safer version that doesn't trigger UI updates
// during the first drawing operation
static bool isFirstOperation = true;
if (!isFirstOperation) {
g_gui.UpdateTitle();
} else {
isFirstOperation = false;
}
}
// Special handling for remote actions
if (batch->type == ACTION_REMOTE) {
try {
// For remote actions, we need to be extra careful
// First clear the batch to ensure no dangling pointers
ActionVector batchCopy;
// Swap the batch contents to avoid issues during deletion
batchCopy.swap(batch->batch);
// Now safely delete the batch (which should be empty)
delete batch;
// Then safely delete each action in the copy
for (Action* action : batchCopy) {
try {
if (action) {
delete action;
}
} catch (const std::exception& e) {
// Log error but continue cleanup
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error deleting remote action: " << e.what() << "\n";
logFile.close();
}
}
}
} catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error handling remote batch: " << e.what() << "\n";
logFile.close();
}
}
return;
}
// Protect against memory corruption when clearing redo history
while (current < actions.size()) {
try {
if (actions.empty()) break;
BatchAction* todelete = actions.back();
actions.pop_back(); // Remove from container before deleting to avoid invalid references
if (todelete) {
memory_size -= todelete->memsize();
delete todelete;
}
} catch (const std::exception& e) {
// Log error but continue processing
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error clearing action: " << e.what() << "\n";
logFile.close();
}
}
}
// Safely manage memory for undo size limits
try {
// Limit by memory size
while (memory_size > size_t(1024 * 1024 * g_settings.getInteger(Config::UNDO_MEM_SIZE)) && !actions.empty()) {
BatchAction* todelete = actions.front();
actions.pop_front(); // Remove from container before accessing or deleting
if (todelete) {
memory_size -= todelete->memsize();
delete todelete;
}
if (current > 0) {
current--;
}
}
// Limit by action count
while (actions.size() > size_t(g_settings.getInteger(Config::UNDO_SIZE)) && !actions.empty()) {
BatchAction* todelete = actions.front();
actions.pop_front(); // Remove from container before accessing or deleting
if (todelete) {
memory_size -= todelete->memsize();
delete todelete;
}
if (current > 0) {
current--;
}
}
// Process action with additional safety
bool actionMerged = false;
if (!actions.empty()) {
BatchAction* lastAction = actions.back();
if (lastAction && batch &&
lastAction->type == batch->type &&
g_settings.getInteger(Config::GROUP_ACTIONS) &&
time(nullptr) - stacking_delay < lastAction->timestamp) {
// Save current memory size before merge
memory_size -= lastAction->memsize();
// Perform the merge
lastAction->merge(batch);
lastAction->timestamp = time(nullptr);
// Update memory size after merge
memory_size += lastAction->memsize(true);
delete batch;
actionMerged = true;
}
}
// Add as new action if not merged
if (!actionMerged) {
memory_size += batch->memsize();
actions.push_back(batch);
batch->timestamp = time(nullptr);
current++;
}
} catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error processing batch: " << e.what() << "\n";
logFile.close();
}
// If we caught an exception and haven't added or deleted the batch yet, delete it now
if (batch) {
delete batch;
}
}
} catch (const std::exception& e) {
// Last resort error handler for entire function
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Critical error in addBatch: " << e.what() << "\n";
logFile.close();
}
// Clean up batch if we haven't processed it yet
if (batch) {
delete batch;
}
}
}
void ActionQueue::undo() {
try {
if (current > 0 && current <= actions.size()) {
current--;
BatchAction* batch = actions[current];
if (batch) {
batch->undo();
}
}
} catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error in undo(): " << e.what() << "\n";
logFile.close();
}
}
}
void ActionQueue::redo() {
try {
if (current < actions.size()) {
BatchAction* batch = actions[current];
if (batch) {
batch->redo();
}
current++;
}
} catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error in redo(): " << e.what() << "\n";
logFile.close();
}
}
}
void ActionQueue::clear() {
try {
// Make a copy of the actions to avoid modifying the container while iterating
ActionList actionsCopy = actions;
// Clear the original container first to prevent double deletions
actions.clear();
current = 0;
memory_size = 0;
// Now safely delete each action
for (BatchAction* action : actionsCopy) {
try {
if (action) {
delete action;
}
} catch (const std::exception& e) {
// Log error but continue cleanup
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Error in clear() deleting action: " << e.what() << "\n";
logFile.close();
}
}
}
} catch (const std::exception& e) {
// Log error but don't crash
std::ofstream logFile((wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + "action_error.log").ToStdString(), std::ios::app);
if (logFile.is_open()) {
wxDateTime now = wxDateTime::Now();
logFile << now.FormatISOCombined() << ": Critical error in clear(): " << e.what() << "\n";
logFile.close();
}