34#include <SDL3/SDL_stdinc.h>
37@implementation NSFileManager (OOExtensions)
39- (NSArray*)commanderContentsOfPath:(NSString*)savePath
41 BOOL pathIsDirectory = NO;
42 if ([[NSFileManager defaultManager] fileExistsAtPath:savePath isDirectory:&pathIsDirectory] && pathIsDirectory) {
47 for (i = 0; i < [
contents count]; i++) {
48 NSString* path = [
savePath stringByAppendingPathComponent:(NSString*)[
contents objectAtIndex:i]];
51 if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&pathIsDirectory] && pathIsDirectory) {
54 if (![[path pathExtension] isEqual:
@"oolite-save"]) {
62 OOLog(
@"savedGame.read.fail.notDictionary",
@">>>> %@ could not be parsed as a saved game.", path);
69 [
contents replaceObjectAtIndex:i withObject:path];
74 OOLogERR(
@"savedGame.read.fail.fileNotFound",
@"File at path '%@' could not be found.", savePath);
81 const char* savedirEnv = SDL_getenv(
"OO_SAVEDIR");
85 savedir = [
NSString stringWithUTF8String:savedirEnv];
89 BOOL pathIsDirectory = NO;
92 if (![[NSFileManager defaultManager] fileExistsAtPath:savedir isDirectory:&pathIsDirectory]) {
94 if ([
self oo_createDirectoryAtPath:savedir attributes:
nil]) {
97 OOLogERR(
@"savedGame.defaultPath.create.failed",
@"Unable to create '%@'. Saved games will go to the home directory.", savedir);
98 return NSHomeDirectory();
103 if (!pathIsDirectory) {
104 OOLogERR(
@"savedGame.defaultPath.notDirectory",
@"'%@' is not a directory, saved games will go to the home directory.", savedir);
105 return NSHomeDirectory();
113- (NSArray*)oo_directoryContentsAtPath:(NSString*)path
115 return [
self contentsOfDirectoryAtPath:path error:NULL];
118- (BOOL)oo_createDirectoryAtPath:(NSString*)path attributes:(NSDictionary*)attributes
120 return [
self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:attributes error:NULL];
123- (NSDictionary*)oo_fileAttributesAtPath:(NSString*)path traverseLink:(BOOL)traverseLink
126 NSString* linkDest =
nil;
128 linkDest = [
self destinationOfSymbolicLinkAtPath:path error:NULL];
131 }
while (linkDest !=
nil);
134 return [
self attributesOfItemAtPath:path error:NULL];
137- (NSDictionary*)oo_fileSystemAttributesAtPath:(NSString*)path
139 return [
self attributesOfFileSystemForPath:path error:NULL];
142- (BOOL)oo_removeItemAtPath:(NSString*)path
144 return [
self removeItemAtPath:path error:NULL];
147- (BOOL)oo_moveItemAtPath:(NSString*)src toPath:(NSString*)dest
149 return [
self moveItemAtPath:src toPath:dest error:NULL];
154- (NSArray*)oo_directoryContentsAtPath:(NSString*)path
156 return [
self directoryContentsAtPath:path];
159- (BOOL)oo_createDirectoryAtPath:(NSString*)path attributes:(NSDictionary*)attributes
161 return [
self createDirectoryAtPath:path withIntermediateDirectories:YES attributes:attributes error:NULL];
164- (NSDictionary*)oo_fileAttributesAtPath:(NSString*)path traverseLink:(BOOL)yorn
166 return [
self fileAttributesAtPath:path traverseLink:yorn];
169- (NSDictionary*)oo_fileSystemAttributesAtPath:(NSString*)path
171 return [
self fileSystemAttributesAtPath:path];
174- (BOOL)oo_removeItemAtPath:(NSString*)path
176 return [
self removeFileAtPath:path handler:nil];
179- (BOOL)oo_moveItemAtPath:(NSString*)src toPath:(NSString*)dest
181 return [
self movePath:src toPath:dest handler:nil];
187- (BOOL)chdirToSnapshotPath
189 const char* snapshotEnv = SDL_getenv(
"OO_SNAPSHOTSDIR");
193 savedir = [NSString stringWithUTF8String:snapshotEnv];
196 savedir = [[NSHomeDirectory() stringByAppendingPathComponent:@SAVEDIR] stringByAppendingPathComponent:@SNAPSHOTDIR];
199 if (![
self changeCurrentDirectoryPath:savedir]) {
201 if (![
self oo_createDirectoryAtPath:savedir attributes:
nil]) {
202 OOLog(
@"savedSnapshot.defaultPath.create.failed",
@"Unable to create directory %@", savedir);
205 if (![
self changeCurrentDirectoryPath:savedir]) {
206 OOLog(
@"savedSnapshot.defaultPath.chdir.failed",
@"Created %@ but couldn't make it the current directory.", savedir);
215- (BOOL)oo_oxzFileExistsAtPath:(NSString*)path
218 NSArray* components = [
path pathComponents];
220 for (i = 0; i < cl; i++) {
221 NSString* component = [
components objectAtIndex:i];
222 if ([[[component pathExtension] lowercaseString] isEqualToString:
@"oxz"]) {
229 BOOL result = [
self fileExistsAtPath:path isDirectory:&directory];
238 range.length = i + 1;
239 NSString* zipFile = [
NSString pathWithComponents:[
components subarrayWithRange:range]];
240 range.location = i + 1;
241 range.length = cl - (i + 1);
242 NSString* containedFile = [
NSString pathWithComponents:[
components subarrayWithRange:range]];
245 const char* zipname = [
zipFile cStringUsingEncoding:NSUTF8StringEncoding];
246 if (zipname != NULL) {
#define OOLogERR(class, format,...)
#define OOLog(class, format,...)
NSDictionary * OODictionaryFromFile(NSString *path)
NSArray * oo_directoryContentsAtPath:(NSString *path)
NSString * defaultCommanderPath()
int ZEXPORT unzLocateFile(unzFile file, const char *szFileName, int iCaseSensitivity)
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
unzFile ZEXPORT unzOpen64(const void *path)
int ZEXPORT unzClose(unzFile file)
struct unz_file_info64_s unz_file_info64