// TODO: fix alternate cohesion crap void OnCompressOrDecompress(HWND sheet, bool compress) { int size, ret; char path[_MAX_PATH]; if (!GetOpenFileNameA(sheet, path, sizeof(path))) return; size = fsize(path); std::vector<unsigned char> buffer(size); AutoFile fIn(path, "rb"); fread(&buffer[0], sizeof(char), size, fIn.get()); // contiguous fIn.close(); path[0] = '\0'; // don't pre-fill path if (!GetSaveFileNameA(sheet, path, sizeof(path))) return; AutoFile fOut(path, "wb"); if (compress) ret = deflate_file(&buffer[0], size, fOut.get()); else ret = inflate_file(&buffer[0], size, fOut.get()); fOut.close(); if (ret >= 0) MessageBox(sheet, "Operation completed successfully.", "Raw Compression/Decompression", MB_OK); else MessageBox(sheet, "Operation failed.", "Raw Compression/Decompression", MB_ICONWARNING); }
/* ProcessCmdline: processes the command-line and returns whether to exit */ bool ProcessCmdline(char *cmdline) { bool ret = true; char pathIn[_MAX_PATH], pathOut[_MAX_PATH]; FILE *fileIn, *fileOut; int size, code; unsigned char *buffer; int c; switch (c = tolower(cmdline[1])) { case 'c': case 'u': cmdline += 2; cmdline = getCmdlinePath(cmdline, pathIn); if (!cmdline) break; cmdline = getCmdlinePath(cmdline, pathOut); if (!cmdline) break; if (c == 'c') printf("Compressing %s to %s... ", pathIn, pathOut); else printf("Decompressing %s to %s... ", pathIn, pathOut); size = fsize(pathIn); buffer = new unsigned char[size]; if (!buffer) { printf_log("not enough memory.\n"); break; } fileIn = fopen(pathIn, "rb"); if (!fileIn) { printf_log("couldn\'t open input file\n"); delete [] buffer; break; } fread(buffer, sizeof(char), size, fileIn); fclose(fileIn); fileOut = fopen(pathOut, "wb"); if (!fileOut) { printf_log("couldn\'t open output file\n"); delete [] buffer; break; } if (c == 'c') code = deflate_file(buffer, size, fileOut); else code = inflate_file(buffer, size, fileOut); fclose(fileOut); if (code >= 0) printf_log("done!\n"); else printf_log("failed.\n"); break; default: ret = false; } return ret; }
bool Scenario::write(const char *new_path) { printf("[W] Writing scenario %s to %s\n", path.c_str(), new_path); //open creates header.hex and scndata.hex int success=open(); if (!success) return false; printf("\t[W] Open successful\n"); //read sets trigger_start and trigger_end, but don't write triggers read(false); //prepare to write uncompressed new scn data to out.hex FILE *newdata = fopen("out.hex", "wb"); long len; //write before triggers data FILE *olddata = fopen("scndata.hex", "rb"); len=trigger_start; while(len--) fputc(fgetc(olddata), newdata); fclose(olddata); printf("\t[W] wrote trigger_start=%d bytes of pre-triggers data\n", trigger_start); //write trigger count long numtriggers = triggers.size(); fwrite(&numtriggers, sizeof(long), 1, newdata); printf("\t[W] numtriggers=%d. fpos=%d\n", numtriggers, ftell(newdata)); //write triggers for (int i=0; i<numtriggers; i++) triggers.at(i)->write(newdata); //write trigger order for (int i=0; i<numtriggers; i++) fwrite(&i, sizeof(long), 1, newdata); printf("\t[W] wrote triggers. fpos=%d, triggerbytes=%d\n", ftell(newdata), ftell(newdata)-trigger_start-4); //write after triggers data olddata = fopen("scndata.hex", "rb"); //skip to trigger_end for (int i=0; i<trigger_end; i++) fgetc(olddata); long restlen=scenario_end-trigger_end; //write the rest for (int i=0; i<restlen; i++) fputc(fgetc(olddata), newdata); fclose(olddata); printf("\t[W] wrote after triggers data. len=%d\n", restlen); //close and reopen newdata, then write it to a byte array fclose(newdata); newdata = fopen("out.hex", "rb"); long newdata_len = fsize("out.hex"); Bytef *uncompr_data = new Bytef[newdata_len]; fread(uncompr_data, sizeof(char), newdata_len, newdata); printf("\t[W] Wrote new data, out.hex, to byte array. size: %d\n", newdata_len); //open new scx file FILE *scx=fopen(new_path, "wb"); //Write header to new scx FILE *header=fopen("header.hex", "rb"); long headerlen=fsize("header.hex"); for (long i=0; i<headerlen; i++) fputc(fgetc(header), scx); fclose(header); printf("\t[W] headerlen: %d\n", headerlen); //compress new data and write it to new scx deflate_file(uncompr_data, newdata_len, scx); //done! fclose(scx); printf("\t[W] Write done!\n"); return true; }