-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrp_io.c
171 lines (146 loc) · 5.27 KB
/
rp_io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <sys/stat.h>
#include <sys/errno.h>
#include <stdio.h>
#include <dirent.h>
#include "rp_io.h"
#include "rp_strings.h"
int MakeRpDirectory(char *directoryName)
{
struct stat fileStatus = {0};
int err;
// Save old permission mask in a temp
mode_t old_mask = umask(0);
if(stat(directoryName, &fileStatus) == -1)
{
// Modify permission mask to 0777 for operation
err = mkdir(directoryName, 0777);
// Set permission mask back to old mask
umask(old_mask);
}
// Return error status
return err;
}
int WriteWorldFile(Editor *editor)
{
char filePath[RP_MAX_FILE_NAME_LENGTH + 12] = {"data/worlds/"};
RpStrAppend(filePath, editor->worldInfo.worldName);
RpStrAppend(filePath, ".rpw");
// Write the world info header at front of file
FILE *ofp = fopen(filePath, "wb");
fwrite(&editor->worldInfo, sizeof(WorldInfo), 1, ofp);
fclose(ofp);
// Append world tile data after world header info
ofp = fopen(filePath, "ab");
for(int i = 0; i < editor->worldInfo.tileCount; ++i)
{
// Write out the entityBuffer of each tile
for(int j = 0; j < editor->worldInfo.tileWidth * editor->worldInfo.tileHeight; ++j)
{
fwrite(&editor->world[i].entityBuffer[j], sizeof(EnvironmentEntity), 1, ofp);
}
// Write out tile info
fwrite(&editor->world[i].zInfo, sizeof(editor->world[i].zInfo), 1, ofp);
fwrite(&editor->world[i].worldX, sizeof(editor->world[i].worldX), 1, ofp);
fwrite(&editor->world[i].worldY, sizeof(editor->world[i].worldY), 1, ofp);
fwrite(&editor->world[i].isLoaded, sizeof(editor->world[i].isLoaded), 1, ofp);
fwrite(&editor->world[i].slotInfo, sizeof(editor->world[i].slotInfo), 1, ofp);
}
fclose(ofp);
}
int LoadWorldFile(Editor *editor, char *worldName)
{
char filePath[RP_MAX_FILE_NAME_LENGTH + 12] = {"data/worlds/"};
RpStrAppend(filePath, worldName);
FILE *ifp = fopen(filePath, "rb");
// Read in world header from file
int err = fread(&editor->worldInfo, sizeof(WorldInfo), 1, ifp);
// Uses world header info to read in remaining data
for(int i = 0; i < editor->worldInfo.tileCount; ++i)
{
// Write out the entityBuffer of each tile
for(int j = 0; j < editor->worldInfo.tileWidth * editor->worldInfo.tileHeight; ++j)
{
err = fread(&editor->world[i].entityBuffer[j], sizeof(EnvironmentEntity), 1, ifp);
}
// Write out tile info
err = fread(&editor->world[i].zInfo, sizeof(editor->world[i].zInfo), 1, ifp);
err = fread(&editor->world[i].worldX, sizeof(editor->world[i].worldX), 1, ifp);
err = fread(&editor->world[i].worldY, sizeof(editor->world[i].worldY), 1, ifp);
err = fread(&editor->world[i].isLoaded, sizeof(editor->world[i].isLoaded), 1, ifp);
err = fread(&editor->world[i].slotInfo, sizeof(editor->world[i].slotInfo), 1, ifp);
}
fclose(ifp);
}
int CheckForSavedWorlds()
{
DIR *dirPointer = NULL;
struct dirent *directory;
dirPointer = opendir("data/worlds/");
int i = 0;
while((directory = readdir(dirPointer)) != NULL)
{
// Store world file extension
char compareExtension[5] = ".rpw";
char compareBuf[5] = {0};
int fileLength = RpStrLength(directory->d_name);
int k = 0;
int isWorldFile = 0;
// Get last 4 characters of current file and store in buffer
for(int j = fileLength - 4; j < fileLength; ++j)
{
compareBuf[k] = directory->d_name[j];
++k;
}
// Compare end of current file with target file extension
if(RpStrCompare(compareExtension, compareBuf) == 1)
{
isWorldFile = 1;
}
// Only add files with target extension .rpw
if(isWorldFile)
{
++i;
}
}
closedir(dirPointer);
// Return file count;
return i;
}
int GetSavedWorldList(char worldListBuffer[][RP_MAX_FILE_NAME_LENGTH], int maxFileCount)
{
DIR *dirPointer = NULL;
struct dirent *directory;
dirPointer = opendir("data/worlds/");
if(dirPointer)
{
int i = 0;
while((directory = readdir(dirPointer)) != NULL && i < maxFileCount)
{
// Store world file extension
char compareExtension[5] = ".rpw";
char compareBuf[5] = {0};
int fileLength = RpStrLength(directory->d_name);
int k = 0;
int isWorldFile = 0;
// Get last 4 characters of current file and store in buffer
for(int j = fileLength - 4; j < fileLength; ++j)
{
compareBuf[k] = directory->d_name[j];
++k;
}
// Compare end of current file with target file extension
if(RpStrCompare(compareExtension, compareBuf) == 1)
{
isWorldFile = 1;
}
// Only add files with target extension .rpw
if(isWorldFile)
{
RpStrAppend(worldListBuffer[i], directory->d_name);
++i;
}
}
closedir(dirPointer);
return i;
}
}