File: GBCheatWindowController.m

package info (click to toggle)
sameboy 1.0.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 10,528 kB
  • sloc: ansic: 29,948; objc: 22,249; asm: 1,424; pascal: 1,373; makefile: 1,065; xml: 111
file content (244 lines) | stat: -rw-r--r-- 7,888 bytes parent folder | download | duplicates (2)
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
#import "GBCheatWindowController.h"
#import "GBWarningPopover.h"
#import "GBCheatTextFieldCell.h"

@implementation GBCheatWindowController

+ (NSString *)addressStringFromCheat:(const GB_cheat_t *)cheat
{
    if (cheat->bank != GB_CHEAT_ANY_BANK) {
        return [NSString stringWithFormat:@"$%x:$%04x", cheat->bank, cheat->address];
    }
    return [NSString stringWithFormat:@"$%04x", cheat->address];
}

+ (NSString *)actionDescriptionForCheat:(const GB_cheat_t *)cheat
{
    if (cheat->use_old_value) {
        return [NSString stringWithFormat:@"[%@]($%02x) = $%02x", [self addressStringFromCheat:cheat], cheat->old_value, cheat->value];
    }
    return [NSString stringWithFormat:@"[%@] = $%02x", [self addressStringFromCheat:cheat], cheat->value];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    GB_gameboy_t *gb = self.document.gameboy;
    if (!gb) return 0;
    size_t cheatCount;
    GB_get_cheats(gb, &cheatCount);
    return cheatCount + 1;
}

- (NSCell *)tableView:(NSTableView *)tableView dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    GB_gameboy_t *gb = self.document.gameboy;
    if (!gb) return nil;
    size_t cheatCount;
    GB_get_cheats(gb, &cheatCount);
    NSUInteger columnIndex = [[tableView tableColumns] indexOfObject:tableColumn];
    if (row >= cheatCount && columnIndex == 0) {
        return [[NSCell alloc] init];
    }
    return nil;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    size_t cheatCount;
    GB_gameboy_t *gb = self.document.gameboy;
    if (!gb) return nil;
    const GB_cheat_t *const *cheats = GB_get_cheats(gb, &cheatCount);
    NSUInteger columnIndex = [[tableView tableColumns] indexOfObject:tableColumn];
    if (row >= cheatCount) {
        switch (columnIndex) {
            case 0:
                return @YES;
                
            case 1:
                return @NO;
                
            case 2:
                return @"Add Cheat…";
            
            case 3:
                return @"";
        }
    }
    
    switch (columnIndex) {
        case 0:
            return @NO;
            
        case 1:
            return @(cheats[row]->enabled);
            
        case 2:
            return @(cheats[row]->description);
            
        case 3:
            return [GBCheatWindowController actionDescriptionForCheat:cheats[row]];
    }
    
    return nil;
}

- (IBAction)importCheat:(id)sender
{
    GB_gameboy_t *gb = self.document.gameboy;
    if (!gb) return;

    [self.document performAtomicBlock:^{
        if (GB_import_cheat(gb,
                            self.importCodeField.stringValue.UTF8String,
                            self.importDescriptionField.stringValue.UTF8String,
                            true)) {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.importCodeField.stringValue = @"";
                self.importDescriptionField.stringValue = @"";
                [self.cheatsTable reloadData];
                [self tableViewSelectionDidChange:nil];
            });
        }
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSBeep();
                [GBWarningPopover popoverWithContents:@"This code is not a valid GameShark or Game Genie code" onView:self.importCodeField];
            });
        }
    }];
}

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    GB_gameboy_t *gb = self.document.gameboy;
    if (!gb) return;
    size_t cheatCount;
    const GB_cheat_t *const *cheats = GB_get_cheats(gb, &cheatCount);
    NSUInteger columnIndex = [[tableView tableColumns] indexOfObject:tableColumn];
    [self.document performAtomicBlock:^{
        if (columnIndex == 1) {
            if (row >= cheatCount) {
                GB_add_cheat(gb, "New Cheat", 0, 0, 0, 0, false, true);
            }
            else {
                GB_update_cheat(gb, cheats[row], cheats[row]->description, cheats[row]->address, cheats[row]->bank, cheats[row]->value, cheats[row]->old_value, cheats[row]->use_old_value, !cheats[row]->enabled);
            }
        }
        else if (row < cheatCount) {
            GB_remove_cheat(gb, cheats[row]);
        }
    }];
    [self.cheatsTable reloadData];
    [self tableViewSelectionDidChange:nil];
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification
{
    GB_gameboy_t *gb = self.document.gameboy;
    if (!gb) return;

    size_t cheatCount;
    const GB_cheat_t *const *cheats = GB_get_cheats(gb, &cheatCount);
    unsigned row = self.cheatsTable.selectedRow;
    const GB_cheat_t *cheat = NULL;
    if (row >= cheatCount) {
        static const GB_cheat_t template = {
            .address = 0,
            .bank = 0,
            .value = 0,
            .old_value = 0,
            .use_old_value = false,
            .enabled = false,
            .description = "New Cheat",
        };
        cheat = &template;
    }
    else {
        cheat = cheats[row];
    }
    
    self.addressField.stringValue = [GBCheatWindowController addressStringFromCheat:cheat];
    self.valueField.stringValue = [NSString stringWithFormat:@"$%02x", cheat->value];
    self.oldValueField.stringValue = [NSString stringWithFormat:@"$%02x", cheat->old_value];
    self.oldValueCheckbox.state = cheat->use_old_value;
    self.descriptionField.stringValue = @(cheat->description);
}

- (void)awakeFromNib
{
    [self tableViewSelectionDidChange:nil];
    ((GBCheatTextFieldCell *)self.addressField.cell).usesAddressFormat = true;
}

- (void)controlTextDidChange:(NSNotification *)obj
{
    [self updateCheat:nil];
}

- (IBAction)updateCheat:(id)sender
{
    GB_gameboy_t *gb = self.document.gameboy;
    if (!gb) return;

    uint16_t address = 0;
    uint16_t bank = GB_CHEAT_ANY_BANK;
    if ([self.addressField.stringValue rangeOfString:@":"].location != NSNotFound) {
        sscanf(self.addressField.stringValue.UTF8String, "$%hx:$%hx", &bank, &address);
    }
    else {
        sscanf(self.addressField.stringValue.UTF8String, "$%hx", &address);
    }
    
    uint8_t value = 0;
    if ([self.valueField.stringValue characterAtIndex:0] == '$') {
        sscanf(self.valueField.stringValue.UTF8String, "$%02hhx", &value);
    }
    else {
        sscanf(self.valueField.stringValue.UTF8String, "%hhd", &value);
    }
    
    uint8_t oldValue = 0;
    if ([self.oldValueField.stringValue characterAtIndex:0] == '$') {
        sscanf(self.oldValueField.stringValue.UTF8String, "$%02hhx", &oldValue);
    }
    else {
        sscanf(self.oldValueField.stringValue.UTF8String, "%hhd", &oldValue);
    }
    
    size_t cheatCount;
    const GB_cheat_t *const *cheats = GB_get_cheats(gb, &cheatCount);
    unsigned row = self.cheatsTable.selectedRow;
    
    [self.document performAtomicBlock:^{
        if (row >= cheatCount) {
            GB_add_cheat(gb,
                         self.descriptionField.stringValue.UTF8String,
                         address,
                         bank,
                         value,
                         oldValue,
                         self.oldValueCheckbox.state,
                         false);
        }
        else {
            GB_update_cheat(gb,
                            cheats[row],
                            self.descriptionField.stringValue.UTF8String,
                            address,
                            bank,
                            value,
                            oldValue,
                            self.oldValueCheckbox.state,
                            cheats[row]->enabled);
        }
    }];
    [self.cheatsTable reloadData];
}

- (void)cheatsUpdated
{
    [self.cheatsTable reloadData];
    [self tableViewSelectionDidChange:nil];
}

@end