cheat: add some undo logic
authornotaz <notasas@gmail.com>
Sun, 22 Jul 2012 21:29:46 +0000 (00:29 +0300)
committernotaz <notasas@gmail.com>
Sun, 22 Jul 2012 21:29:46 +0000 (00:29 +0300)
frontend/main.c
libpcsxcore/cheat.c
libpcsxcore/cheat.h

index 9d3cc33..15eae71 100644 (file)
@@ -342,6 +342,7 @@ static void parse_cwcheat(void)
                        }
                        Cheats[NumCheats].Descr = strdup(name);
                        Cheats[NumCheats].Enabled = 0;
+                       Cheats[NumCheats].WasEnabled = 0;
                        Cheats[NumCheats].First = NumCodes;
                        Cheats[NumCheats].n = 0;
                        NumCheats++;
index 9ce7ed8..a016aee 100644 (file)
@@ -108,6 +108,7 @@ void LoadCheats(const char *filename) {
                                Cheats[NumCheats].Descr = strdup(buf + 1);
                                Cheats[NumCheats].Enabled = 0;
                        }
+                       Cheats[NumCheats].WasEnabled = 0;
 
                        Cheats[NumCheats].First = NumCodes;
 
@@ -182,11 +183,15 @@ void SaveCheats(const char *filename) {
 // apply all enabled cheats
 void ApplyCheats() {
        int             i, j, k, endindex;
+       int             was_enabled;
 
        for (i = 0; i < NumCheats; i++) {
+               was_enabled = Cheats[i].WasEnabled;
                if (!Cheats[i].Enabled) {
-                       continue;
+                       if (!Cheats[i].WasEnabled)
+                               continue;
                }
+               Cheats[i].WasEnabled = Cheats[i].Enabled;
 
                // process all cheat codes
                endindex = Cheats[i].First + Cheats[i].n;
@@ -197,6 +202,22 @@ void ApplyCheats() {
                        u16             val = CheatCodes[j].Val;
                        u32             taddr;
 
+                       if (!was_enabled) {
+                               switch (type) {
+                                       case CHEAT_CONST16:
+                                               CheatCodes[j].OldVal = psxMu16(addr);
+                                               break;
+                                       case CHEAT_CONST8:
+                                               CheatCodes[j].OldVal = psxMu8(addr);
+                                               break;
+                               }
+                       }
+                       else if (!Cheats[i].Enabled) {
+                               val = CheatCodes[j].OldVal;
+                               if (type != CHEAT_CONST16 && type != CHEAT_CONST8)
+                                       continue;
+                       }
+
                        switch (type) {
                                case CHEAT_CONST8:
                                        psxMu8ref(addr) = (u8)val;
@@ -321,6 +342,7 @@ int AddCheat(const char *descr, char *code) {
 
        Cheats[NumCheats].Descr = strdup(descr[0] ? descr : _("(Untitled)"));
        Cheats[NumCheats].Enabled = 0;
+       Cheats[NumCheats].WasEnabled = 0;
        Cheats[NumCheats].First = NumCodes;
        Cheats[NumCheats].n = 0;
 
index c533b93..b3d8bc4 100644 (file)
@@ -26,6 +26,7 @@ extern "C" {
 typedef struct {
        uint32_t        Addr;
        uint16_t        Val;
+       uint16_t        OldVal;
 } CheatCode;
 
 typedef struct {
@@ -33,6 +34,7 @@ typedef struct {
        int                     First;          // index of the first cheat code
        int                     n;                      // number of cheat codes for this cheat
        int                     Enabled;
+       int                     WasEnabled;
 } Cheat;
 
 void ClearAllCheats();