1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #if defined(__WII__)
24 
25 #define FORBIDDEN_SYMBOL_EXCEPTION_printf
26 #define FORBIDDEN_SYMBOL_EXCEPTION_getcwd
27 
28 #include <unistd.h>
29 
30 #include "backends/fs/wii/wii-fs-factory.h"
31 #include "backends/fs/wii/wii-fs.h"
32 
33 #ifdef USE_WII_DI
34 #include <di/di.h>
35 #include <iso9660.h>
36 #ifdef GAMECUBE
37 #include <ogc/dvd.h>
38 #endif
39 #endif
40 
41 #ifdef USE_WII_SMB
42 #include <network.h>
43 #include <smb.h>
44 #endif
45 
46 namespace Common {
47 DECLARE_SINGLETON(WiiFilesystemFactory);
48 }
49 
WiiFilesystemFactory()50 WiiFilesystemFactory::WiiFilesystemFactory() :
51 	_dvdMounted(false),
52 	_smbMounted(false),
53 	_dvdError(false),
54 	_smbError(false) {
55 }
56 
makeRootFileNode() const57 AbstractFSNode *WiiFilesystemFactory::makeRootFileNode() const {
58 	return new WiiFilesystemNode();
59 }
60 
makeCurrentDirectoryFileNode() const61 AbstractFSNode *WiiFilesystemFactory::makeCurrentDirectoryFileNode() const {
62 	char buf[MAXPATHLEN];
63 
64 	if (getcwd(buf, MAXPATHLEN))
65 		return new WiiFilesystemNode(buf);
66 	else
67 		return new WiiFilesystemNode();
68 }
69 
makeFileNodePath(const Common::String & path) const70 AbstractFSNode *WiiFilesystemFactory::makeFileNodePath(const Common::String &path) const {
71 	return new WiiFilesystemNode(path);
72 }
73 
asyncInit()74 void WiiFilesystemFactory::asyncInit() {
75 #ifdef USE_WII_SMB
76 	asyncInitNetwork();
77 #endif
78 }
79 
asyncDeinit()80 void WiiFilesystemFactory::asyncDeinit() {
81 #ifdef USE_WII_DI
82 	umount(kDVD);
83 #ifndef GAMECUBE
84 	DI_Close();
85 #endif
86 #endif
87 #ifdef USE_WII_SMB
88 	umount(kSMB);
89 	net_deinit();
90 #endif
91 }
92 
93 #ifdef USE_WII_SMB
asyncInitNetwork()94 void WiiFilesystemFactory::asyncInitNetwork() {
95 	net_init_async(NULL, NULL);
96 }
97 
setSMBLoginData(const String & server,const String & share,const String & username,const String & password)98 void WiiFilesystemFactory::setSMBLoginData(const String &server,
99 											const String &share,
100 											const String &username,
101 											const String &password) {
102 	_smbServer = server;
103 	_smbShare = share;
104 	_smbUsername = username;
105 	_smbPassword = password;
106 }
107 #endif
108 
isMounted(FileSystemType type)109 bool WiiFilesystemFactory::isMounted(FileSystemType type) {
110 	switch (type) {
111 	case kDVD:
112 		return _dvdMounted;
113 	case kSMB:
114 		return _smbMounted;
115 	}
116 
117 	return false;
118 }
119 
failedToMount(FileSystemType type)120 bool WiiFilesystemFactory::failedToMount(FileSystemType type) {
121 	switch (type) {
122 	case kDVD:
123 		return _dvdError;
124 	case kSMB:
125 		return _smbError;
126 	}
127 
128 	return false;
129 }
130 
131 #ifdef USE_WII_DI
132 #ifndef GAMECUBE
133   const DISC_INTERFACE* dvd = &__io_wiidvd;
134 #else
135   const DISC_INTERFACE* dvd = &__io_gcdvd;
136 #endif
137 #endif
138 
mount(FileSystemType type)139 void WiiFilesystemFactory::mount(FileSystemType type) {
140 	switch (type) {
141 	case kDVD:
142 #ifdef USE_WII_DI
143 		if (_dvdMounted)
144 			break;
145 
146 		printf("mount dvd\n");
147 		if (ISO9660_Mount("dvd", dvd)) {
148 			_dvdMounted = true;
149 			_dvdError = false;
150 			printf("ISO9660 mounted\n");
151 		} else {
152 			_dvdError = true;
153 			printf("ISO9660 mount failed\n");
154 		}
155 #endif
156 		break;
157 
158 	case kSMB:
159 #ifdef USE_WII_SMB
160 		if (_smbMounted)
161 			break;
162 
163 		printf("mount smb\n");
164 
165 		if (net_get_status()) {
166 			printf("network not inited\n");
167 			break;
168 		}
169 
170 		if (smbInit(_smbUsername.c_str(), _smbPassword.c_str(),
171 					_smbShare.c_str(), _smbServer.c_str())) {
172 			_smbMounted = true;
173 			_smbError = false;
174 			printf("smb mounted\n");
175 		} else {
176 			_smbError = true;
177 			printf("error mounting smb\n");
178 		}
179 #endif
180 		break;
181 	}
182 }
183 
umount(FileSystemType type)184 void WiiFilesystemFactory::umount(FileSystemType type) {
185 	switch (type) {
186 	case kDVD:
187 #ifdef USE_WII_DI
188 		if (!_dvdMounted)
189 			break;
190 
191 		printf("umount dvd\n");
192 
193 		ISO9660_Unmount("dvd:");
194 
195 		_dvdMounted = false;
196 		_dvdError = false;
197 #endif
198 		break;
199 
200 	case kSMB:
201 #ifdef USE_WII_SMB
202 		if (!_smbMounted)
203 			break;
204 
205 		printf("umount smb\n");
206 
207 		smbClose("smb:");
208 
209 		_smbMounted = false;
210 		_smbError = false;
211 #endif
212 		break;
213 	}
214 }
215 
mountByPath(const String & path)216 void WiiFilesystemFactory::mountByPath(const String &path) {
217 	if (path.hasPrefix("dvd:/"))
218 		mount(kDVD);
219 	else if (path.hasPrefix("smb:/"))
220 		mount(kSMB);
221 }
222 
umountUnused(const String & path)223 void WiiFilesystemFactory::umountUnused(const String &path) {
224 	if (!path.hasPrefix("dvd:/"))
225 		umount(kDVD);
226 
227 	if (!path.hasPrefix("smb:/"))
228 		umount(kSMB);
229 }
230 
231 #endif
232