src/gba/rr/rr.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "rr.h"
7
8#include "core/serialize.h"
9#include "util/vfs.h"
10
11mLOG_DEFINE_CATEGORY(GBA_RR, "GBA RR");
12
13void GBARRInitRecord(struct GBA* gba) {
14 if (!gba || !gba->rr) {
15 return;
16 }
17
18 if (gba->rr->initFrom & INIT_FROM_SAVEGAME) {
19 if (gba->rr->savedata) {
20 gba->rr->savedata->close(gba->rr->savedata);
21 }
22 gba->rr->savedata = gba->rr->openSavedata(gba->rr, O_TRUNC | O_CREAT | O_WRONLY);
23 GBASavedataClone(&gba->memory.savedata, gba->rr->savedata);
24 gba->rr->savedata->close(gba->rr->savedata);
25 gba->rr->savedata = gba->rr->openSavedata(gba->rr, O_RDONLY);
26 GBASavedataMask(&gba->memory.savedata, gba->rr->savedata);
27 } else {
28 GBASavedataMask(&gba->memory.savedata, 0);
29 }
30
31 if (gba->rr->initFrom & INIT_FROM_SAVESTATE) {
32 struct VFile* vf = gba->rr->openSavestate(gba->rr, O_TRUNC | O_CREAT | O_RDWR);
33 GBASaveStateNamed(gba, vf, SAVESTATE_SAVEDATA);
34 vf->close(vf);
35 } else {
36 ARMReset(gba->cpu);
37 }
38}
39
40void GBARRInitPlay(struct GBA* gba) {
41 if (!gba || !gba->rr) {
42 return;
43 }
44
45 if (gba->rr->initFrom & INIT_FROM_SAVEGAME) {
46 if (gba->rr->savedata) {
47 gba->rr->savedata->close(gba->rr->savedata);
48 }
49 gba->rr->savedata = gba->rr->openSavedata(gba->rr, O_RDONLY);
50 GBASavedataMask(&gba->memory.savedata, gba->rr->savedata);
51 } else {
52 GBASavedataMask(&gba->memory.savedata, 0);
53 }
54
55 if (gba->rr->initFrom & INIT_FROM_SAVESTATE) {
56 struct VFile* vf = gba->rr->openSavestate(gba->rr, O_RDONLY);
57 GBALoadStateNamed(gba, vf, SAVESTATE_SCREENSHOT | SAVESTATE_SAVEDATA);
58 vf->close(vf);
59 } else {
60 ARMReset(gba->cpu);
61 }
62}
63
64void GBARRDestroy(struct GBARRContext* rr) {
65 if (rr->isPlaying(rr)) {
66 rr->stopPlaying(rr);
67 }
68 if (rr->isRecording(rr)) {
69 rr->stopRecording(rr);
70 }
71 if (rr->savedata) {
72 rr->savedata->close(rr->savedata);
73 rr->savedata = 0;
74 }
75 rr->destroy(rr);
76}