Commit 6a6d7496141daac6e420ff1f48ef84b890bea042

Authored by Imanol-Mikel Barba Sabariego
1 parent ab8cd4ec

Added getopt() options and fixed a bug caused by omission of ownerUUID parameter in search

Showing 1 changed file with 93 additions and 21 deletions
mcpetbackup.c
1 1 #include <stdio.h>
  2 +#include <getopt.h>
2 3  
3 4 #include "nbt.h"
4 5 #include "chunk.h"
5 6  
  7 +enum paramIndex
  8 +{
  9 + UNKNOWN = 0,
  10 + REGION,
  11 + SAVE,
  12 + LOAD,
  13 + NAME,
  14 + OWNER
  15 +};
  16 +
  17 +static struct option longopts[] = {
  18 + { "regiondata", required_argument, NULL, REGION },
  19 + { "save", required_argument, NULL, SAVE },
  20 + { "load", required_argument, NULL, LOAD },
  21 + { "name", required_argument, NULL, NAME },
  22 + { "owner", required_argument, NULL, OWNER },
  23 + { NULL, 0, NULL, UNKNOWN }
  24 +};
  25 +
6 26 int savePetToFile(Tag* pet, const char* filename) {
7 27 void* petData;
8 28 size_t petDataLength = composeTag(*pet, &petData);
... ... @@ -181,17 +201,69 @@ ssize_t insertPetIntoChunk(void** chunkData, Tag chunkRoot, Tag* entities, Tag p
181 201 return chunkDataLength;
182 202 }
183 203  
  204 +void printHelp() {
  205 + fprintf(stderr,"--regiondata ./industrial/world/region --save Iris.mcdata --name Iris\n");
  206 + fprintf(stderr,"--regiondata ./industrial/world/region --save Iris.mcdata --owner 32812f90-17ec-4f5a-8b7e-e500f17b1ba5\n");
  207 + fprintf(stderr,"--regiondata ./industrial/world/region --load Iris.mcdata\n");
  208 +}
  209 +
184 210 int main(int argc, char** argv) {
185   - // TODO argv
186   - // --regiondata ./industrial/world/region --save Iris.mcdata --name Iris
187   - // --regiondata ./industrial/world/region --save Iris.mcdata --owner 32812f90-17ec-4f5a-8b7e-e500f17b1ba5
188   - // --regiondata ./industrial/world/region --load Iris.mcdata
189   -
190   - const char* regionFolder = "./industrial/world/region";
191   - const char* petName = "Iris";
  211 + const char* regionFolder = NULL;
  212 + const char* file = NULL;
  213 + const char* petName = NULL;
  214 + const char* ownerUUID = NULL;
192 215 int save = 0;
193   - const char* outputFile = "Iris.mcdata";
194   - const char* inputFile = "Iris.mcdata";
  216 +
  217 + int longIndex = UNKNOWN;
  218 + int c;
  219 +
  220 + if(argc < 2) {
  221 + fprintf(stderr,"No options specified\n\n");
  222 + fprintf(stderr,"Examples:\n");
  223 + printHelp();
  224 + return 0;
  225 + }
  226 +
  227 + while ((c = getopt_long(argc, argv, "r:s:l:n:o:h", longopts, &longIndex)) != -1)
  228 + {
  229 + if(c == 'h') {
  230 + printHelp();
  231 + return 0;
  232 + }
  233 + else if(c == REGION) {regionFolder = optarg;}
  234 + else if(c == SAVE) {save = 1;file = optarg;}
  235 + else if(c == LOAD) {file = optarg;}
  236 + else if(c == NAME) {petName = optarg;}
  237 + else if(c == OWNER) {ownerUUID = optarg;}
  238 + else {
  239 + fprintf(stderr,"Unrecognised argument: %s\n",optarg);
  240 + printHelp();
  241 + return 1;
  242 + }
  243 + }
  244 + if(optind != argc) {
  245 + fprintf(stderr,"Unrecognised argument: %s\n",argv[optind+1]);
  246 + printHelp();
  247 + return 1;
  248 + }
  249 +
  250 + if(regionFolder == NULL) {
  251 + fprintf(stderr,"Region path not specified (--regionpath PATH_TO_REGION_FOLDER)\n");
  252 + printHelp();
  253 + return 2;
  254 + } else if(save && petName == NULL && ownerUUID == NULL) {
  255 + fprintf(stderr,"OwnerUUID and petName were unspecified (--owner UUID, --name PET_NAME)\n");
  256 + printHelp();
  257 + return 3;
  258 + } else if(!save && (petName != NULL || ownerUUID != NULL)) {
  259 + fprintf(stderr,"OwnerUUID and petName were unspecified (--owner UUID, --name PET_NAME)\n");
  260 + printHelp();
  261 + return 3;
  262 + } else if(file == NULL) {
  263 + fprintf(stderr,"Neither saving or loading a pet was requested (--save PATH_TO_FILE, --load PATH_TO_FILE)\n");
  264 + printHelp();
  265 + return 4;
  266 + }
195 267  
196 268 void *chunkData;
197 269 ChunkID chunk = translateCoordsToChunk(800, 200, 3041);
... ... @@ -205,45 +277,45 @@ int main(int argc, char** argv) {
205 277 if(pos != chunkLen) {
206 278 fprintf(stderr, "Didn't reach end of NBT file\n");
207 279 free(chunkData);
208   - return 1;
  280 + return 5;
209 281 }
210 282  
211 283 Tag* entities;
212 284 if(getEntitiesTag((TagCompound*)t.payload,&entities)) {
213 285 fprintf(stderr, "Unable to find Entities tag\n");
214 286 free(chunkData);
215   - return 2;
  287 + return 6;
216 288 }
217 289 if(save) {
218 290 Tag* pet;
219   - if(searchForPet(*entities,petName,NULL,&pet)) {
  291 + if(searchForPet(*entities,petName,ownerUUID,&pet)) {
220 292 fprintf(stderr, "Unable to find pet named %s\n",petName);
221 293 free(chunkData);
222   - return 3;
  294 + return 7;
223 295 }
224   - if(savePetToFile(pet,outputFile)) {
225   - fprintf(stderr, "Unable to save pet to file: %s\n",outputFile);
  296 + if(savePetToFile(pet,file)) {
  297 + fprintf(stderr, "Unable to save pet to file: %s\n",file);
226 298 free(chunkData);
227   - return 4;
  299 + return 8;
228 300 }
229 301 } else {
230 302 Tag pet;
231   - if(loadPetFromFile(&pet,inputFile)) {
232   - fprintf(stderr, "Unable to load pet from file: %s\n",inputFile);
  303 + if(loadPetFromFile(&pet,file)) {
  304 + fprintf(stderr, "Unable to load pet from file: %s\n",file);
233 305 free(chunkData);
234   - return 5;
  306 + return 9;
235 307 }
236 308 chunkLen = insertPetIntoChunk(&chunkData,t,entities,pet,801,200,3040);
237 309 if(chunkLen <= 0) {
238 310 fprintf(stderr, "Unable to insert pet into chunk\n");
239 311 free(chunkData);
240   - return 6;
  312 + return 10;
241 313 }
242 314 destroyTag(&pet);
243 315 if(overwriteChunk(regionFolder, chunk, chunkData, chunkLen)) {
244 316 fprintf(stderr, "Unable to write new chunk\n");
245 317 free(chunkData);
246   - return 7;
  318 + return 11;
247 319 }
248 320 }
249 321  
... ...