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 struct VDir* dir = VDirOpen(opts->savegamePath);
107 if (dir) {
108 if (dirs->save && dirs->save != dirs->base) {
109 dirs->save->close(dirs->save);
110 }
111 dirs->save = dir;
112 }
113 }
114
115 if (opts->savestatePath) {
116 struct VDir* dir = VDirOpen(opts->savestatePath);
117 if (dir) {
118 if (dirs->state && dirs->state != dirs->base) {
119 dirs->state->close(dirs->state);
120 }
121 dirs->state = dir;
122 }
123 }
124
125 if (opts->screenshotPath) {
126 struct VDir* dir = VDirOpen(opts->screenshotPath);
127 if (dir) {
128 if (dirs->screenshot && dirs->screenshot != dirs->base) {
129 dirs->screenshot->close(dirs->screenshot);
130 }
131 dirs->screenshot = dir;
132 }
133 }
134
135 if (opts->patchPath) {
136 struct VDir* dir = VDirOpen(opts->patchPath);
137 if (dir) {
138 if (dirs->patch && dirs->patch != dirs->base) {
139 dirs->patch->close(dirs->patch);
140 }
141 dirs->patch = dir;
142 }
143 }
144}