forked from renpy/renpyweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importexport.c
213 lines (189 loc) · 5.89 KB
/
importexport.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Import/export ~/.renpy/ as .zip
Copyright (C) 2018, 2019 Sylvain Beucler
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zip.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <libgen.h>
#include <sys/types.h>
#include <utime.h>
int mkdir_rec(char* path) {
int ret;
mode_t mode = 00755;
ret = mkdir(path, mode);
if (ret == 0 || errno == EEXIST) {
return 0;
} else {
char* dir = strdup(path);
dirname(dir);
ret = mkdir_rec(dir);
free(dir);
if (ret < 0) {
return ret;
}
if ((ret = mkdir(path, mode)) < 0) {
perror("mkdir");
return ret;
}
return 0;
}
}
int IsDir(char* path) {
struct stat st;
return (stat(path, &st) == 0 && S_ISDIR(st.st_mode));
}
int AddFileToZip(zip_t* archive, char* path, char* prefix) {
zip_source_t *s;
s = zip_source_file(archive, path, 0, -1);
if (s == NULL) {
fprintf(stderr, "error opening %s\n", path);
return 0;
}
char* zippath = path + strlen(prefix);
if (zip_file_add(archive, zippath, s, ZIP_FL_ENC_UTF_8) < 0) {
zip_source_free(s);
fprintf(stderr, "error adding %s: %s\n", path, zip_strerror(archive));
return 0;
}
return 1;
}
int AddPathToZip(zip_t* archive, char* path, char* prefix) {
//printf("processing %s\n", path);
struct stat st;
if (IsDir(path)) {
DIR* dir = opendir(path);
if (dir == NULL) {
perror("opendir");
return 0;
}
struct dirent *ent;
while ((ent = readdir(dir)) != NULL) {
//printf("dir: %s\n", ent->d_name);
if (strcmp(ent->d_name, ".") == 0) continue;
if (strcmp(ent->d_name, "..") == 0) continue;
char* subpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
sprintf(subpath, "%s/%s", path, ent->d_name);
AddPathToZip(archive, subpath, prefix);
free(subpath);
}
closedir(dir);
} else {
AddFileToZip(archive, path, prefix);
}
return 1;
}
int emSavegamesExport(void) {
int errorp;
zip_t* archive = zip_open("savegames.zip", ZIP_CREATE|ZIP_TRUNCATE|ZIP_CHECKCONS, &errorp);
if (archive == NULL) {
zip_error_t error;
zip_error_init_with_code(&error, errorp);
fprintf(stderr, "error creating savegames.zip: %s\n", zip_error_strerror(&error));
zip_error_fini(&error);
return 0;
}
char* path = malloc(strlen(getenv("HOME"))+1+strlen(".renpy")+1);
sprintf(path, "%s/%s", getenv("HOME"), ".renpy");
char* prefix = malloc(strlen(getenv("HOME"))+1+strlen(".renpy")+1+1);
sprintf(prefix, "%s/%s/", getenv("HOME"), ".renpy");
AddPathToZip(archive, path, prefix);
free(path);
if (zip_get_num_entries(archive, 0) == 0) {
printf("No savegames found!\n");
return 0;
} else if (zip_close(archive) < 0) {
fprintf(stderr, "cannot write savegames.zip: %s\n", zip_strerror(archive));
return 0;
}
chmod("savegames.zip", 00666);
return 1;
}
void emSavegamesImport(void) {
int errorp;
zip_t* archive = zip_open("/savegames.zip", ZIP_RDONLY, &errorp);
if (archive == NULL) {
zip_error_t error;
zip_error_init_with_code(&error, errorp);
fprintf(stderr, "error opening savegames.zip: %s\n", zip_error_strerror(&error));
zip_error_fini(&error);
return;
}
char* prefix = malloc(strlen(getenv("HOME"))+1+strlen(".renpy")+1+1);
sprintf(prefix, "%s/%s/", getenv("HOME"), ".renpy");
for (int i = 0; i < zip_get_num_entries(archive, 0); i++) {
zip_stat_t sb;
zip_stat_index(archive, i, 0, &sb);
if (!(sb.valid & ZIP_STAT_NAME)
|| !(sb.valid & ZIP_STAT_SIZE)) {
continue;
}
if (sb.name[strlen(sb.name)-1] == '/') {
// will recursively create directories later
continue;
}
char* destFullPath = malloc(strlen(prefix)+strlen(sb.name)+1);
sprintf(destFullPath, "%s%s", prefix, sb.name);
char* dir = strdup(destFullPath);
dirname(dir);
int ret = mkdir_rec(dir);
if (ret < 0) printf("Error creating directory %s\n", dir);
free(dir);
if (ret < 0)
continue;
zip_file_t* file;
if ((file = zip_fopen_index(archive, i, 0)) == NULL) {
fprintf(stderr, "error opening file %d: %s\n", i, zip_strerror(archive));
continue;
}
FILE* out;
if ((out = fopen(destFullPath, "wb")) == NULL) {
perror("fopen");
continue;
}
unsigned char buf[4096]; // size from emscripten implementation
zip_uint64_t total = 0;
while (total < sb.size) {
zip_int64_t nb_read;
if ((nb_read = zip_fread(file, buf, sizeof(buf))) < 0) {
fprintf(stderr, "error reading %s: %s\n", sb.name, zip_file_strerror(file));
continue;
}
fwrite(buf, nb_read, 1, out);
total += nb_read;
}
fclose(out);
zip_fclose(file);
if (sb.valid | ZIP_STAT_MTIME) {
struct utimbuf times = { sb.mtime, sb.mtime };
utime(destFullPath, ×);
}
free(destFullPath);
}
}
#ifdef TEST
int main(void) {
return !emSavegamesExport();
}
#endif