src/gba/context/directories.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 "directories.h"
7
8#include "gba/context/config.h"
9#include "util/vfs.h"
10
11void GBADirectorySetInit(struct GBADirectorySet* dirs) {
12 dirs->base = 0;
13 dirs->archive = 0;
14 dirs->save = 0;
15 dirs->patch = 0;
16 dirs->state = 0;
17 dirs->screenshot = 0;
18}
19
20void GBADirectorySetDeinit(struct GBADirectorySet* dirs) {
21 GBADirectorySetDetachBase(dirs);
22
23 if (dirs->archive) {
24 dirs->archive->close(dirs->archive);
25 dirs->archive = 0;
26 }
27
28 if (dirs->save) {
29 dirs->save->close(dirs->save);
30 dirs->save = 0;
31 }
32
33 if (dirs->patch) {
34 dirs->patch->close(dirs->patch);
35 dirs->patch = 0;
36 }
37
38 if (dirs->state) {
39 dirs->state->close(dirs->state);
40 dirs->state = 0;
41 }
42
43 if (dirs->screenshot) {
44 dirs->screenshot->close(dirs->screenshot);
45 dirs->screenshot = 0;
46 }
47}
48
49void GBADirectorySetAttachBase(struct GBADirectorySet* dirs, struct VDir* base) {
50 dirs->base = base;
51 if (!dirs->save) {
52 dirs->save = dirs->base;
53 }
54 if (!dirs->patch) {
55 dirs->patch = dirs->base;
56 }
57 if (!dirs->state) {
58 dirs->state = dirs->base;
59 }
60 if (!dirs->screenshot) {
61 dirs->screenshot = dirs->base;
62 }
63}
64
65void GBADirectorySetDetachBase(struct GBADirectorySet* dirs) {
66 if (dirs->save == dirs->base) {
67 dirs->save = 0;
68 }
69 if (dirs->patch == dirs->base) {
70 dirs->patch = 0;
71 }
72 if (dirs->state == dirs->base) {
73 dirs->state = 0;
74 }
75 if (dirs->screenshot == dirs->base) {
76 dirs->screenshot = 0;
77 }
78
79 if (dirs->base) {
80 dirs->base->close(dirs->base);
81 dirs->base = 0;
82 }
83}
84
85struct VFile* GBADirectorySetOpenPath(struct GBADirectorySet* dirs, const char* path, bool (*filter)(struct VFile*)) {
86 dirs->archive = VDirOpenArchive(path);
87 struct VFile* file;
88 if (dirs->archive) {
89 file = VDirFindFirst(dirs->archive, filter);
90 if (!file) {
91 dirs->archive->close(dirs->archive);
92 dirs->archive = 0;
93 }
94 } else {
95 file = VFileOpen(path, O_RDONLY);
96 if (file && !filter(file)) {
97 file->close(file);
98 file = 0;
99 }
100 }
101 return file;
102}
103
104void GBADirectorySetMapOptions(struct GBADirectorySet* dirs, const struct GBAOptions* opts) {
105 if (opts->savegamePath) {
106 if (dirs->save && dirs->save != dirs->base) {
107 dirs->save->close(dirs->save);
108 }
109 dirs->save = VDirOpen(opts->savegamePath);
110 }
111
112 if (opts->savestatePath) {
113 if (dirs->state && dirs->state != dirs->base) {
114 dirs->state->close(dirs->state);
115 }
116 dirs->state = VDirOpen(opts->savestatePath);
117 }
118
119 if (opts->screenshotPath) {
120 if (dirs->screenshot && dirs->screenshot != dirs->base) {
121 dirs->screenshot->close(dirs->screenshot);
122 }
123 dirs->screenshot = VDirOpen(opts->screenshotPath);
124 }
125
126 if (opts->patchPath) {
127 if (dirs->patch && dirs->patch != dirs->base) {
128 dirs->patch->close(dirs->patch);
129 }
130 dirs->patch = VDirOpen(opts->patchPath);
131 }
132}