Created
June 15, 2015 12:31
-
-
Save YaLTeR/6b4853de8069ddc435c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cstdint> | |
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <intrin.h> | |
void ScanFrame(const unsigned char frame[17]) | |
{ | |
uint32_t time; | |
std::memcpy(&time, frame, 4); | |
time = _byteswap_ulong(time); | |
int16_t pitchdif, yawdif; | |
std::memcpy(&pitchdif, frame + 4, 2); | |
pitchdif = _byteswap_ushort(pitchdif); | |
std::memcpy(&yawdif, frame + 6, 2); | |
yawdif = _byteswap_ushort(yawdif); | |
bool Sprint = (frame[13] == 0x10); | |
bool W = (frame[14] == 0x7F); | |
bool S = (frame[14] == 0x81); | |
bool D = (frame[15] == 0x7F); | |
bool A = (frame[15] == 0x81); | |
bool Jump = (frame[16] == 0x7F); | |
bool Duck = (frame[16] == 0x81); | |
std::cout << "Time = " << time << "; pitchdif = " << pitchdif << "; yawdif = " << yawdif << "; "; | |
std::cout << "Buttons:"; | |
if (Sprint) std::cout << " Sprint"; | |
if (W) std::cout << " W"; | |
if (A) std::cout << " A"; | |
if (S) std::cout << " S"; | |
if (D) std::cout << " D"; | |
if (Jump) std::cout << " Jump"; | |
if (Duck) std::cout << " Duck"; | |
std::cout << "\n"; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (argc < 2) | |
return 1; | |
if (argc == 2) { | |
std::ifstream in(argv[1], std::ios::binary); | |
in.seekg(0, std::ios::end); | |
std::size_t size = in.tellg(); | |
if (size % 17) { | |
std::cout << "Wrong file size (not divisible by 17)!\n"; | |
return 1; | |
} | |
in.seekg(0, std::ios::beg); | |
std::vector<char> buf(size); | |
std::copy(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>(), buf.begin()); | |
int lines = 0; | |
std::size_t bytesread = 0; | |
while (bytesread < size) { | |
char frame[17]; | |
std::memcpy(frame, buf.data() + bytesread, 17); | |
std::cout << "Frame " << lines << ":\t"; | |
ScanFrame(reinterpret_cast<const unsigned char*>(frame)); | |
bytesread += 17; | |
lines++; | |
} | |
} else if (argc == 4) { | |
std::ifstream in1(argv[1], std::ios::binary); | |
in1.seekg(0, std::ios::end); | |
std::size_t size1 = in1.tellg(); | |
if (size1 % 17) { | |
std::cout << "Wrong file size for the first input file (not divisible by 17)!\n"; | |
return 1; | |
} | |
in1.seekg(0, std::ios::beg); | |
std::ifstream in2(argv[2], std::ios::binary); | |
in2.seekg(0, std::ios::end); | |
std::size_t size2 = in2.tellg(); | |
if (size2 % 17) { | |
std::cout << "Wrong file size for the second input file (not divisible by 17)!\n"; | |
return 1; | |
} | |
in2.seekg(0, std::ios::beg); | |
std::ofstream out(argv[3], std::ios::binary); | |
std::copy(std::istreambuf_iterator<char>(in1), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(out)); | |
out.seekp(0, std::ios::end); | |
char temp[4]; | |
uint32_t lasttime; | |
in1.seekg(-17, std::ios::end); | |
in1.read(temp, 4); | |
std::memcpy(&lasttime, temp, 4); | |
lasttime = _byteswap_ulong(lasttime); | |
uint32_t nexttolasttime; | |
uint16_t lastpitch, lastyaw; | |
in1.seekg(-21, std::ios::cur); | |
in1.read(temp, 4); | |
std::memcpy(&nexttolasttime, temp, 4); | |
nexttolasttime = _byteswap_ulong(nexttolasttime); | |
in1.read(temp, 2); | |
std::memcpy(&lastpitch, temp, 2); | |
lastpitch = _byteswap_ushort(lastpitch); | |
in1.read(temp, 2); | |
std::memcpy(&lastyaw, temp, 2); | |
lastyaw = _byteswap_ushort(lastyaw); | |
auto frametime = lasttime - nexttolasttime; | |
auto nexttime = lasttime + frametime; | |
std::cout << "lasttime: " << lasttime << "; nexttolasttime: " << nexttolasttime << "; frametime: " << frametime << "; nexttime: " << nexttime << "\n"; | |
int32_t offset = 0; | |
std::size_t bytesread = 0; | |
while (bytesread < size2) { | |
char frame[17]; | |
in2.read(frame, 17); | |
uint32_t curtime; | |
std::memcpy(&curtime, frame, 4); | |
curtime = _byteswap_ulong(curtime); | |
if (bytesread == 0) { | |
offset = curtime - nexttime; | |
std::cout << "offset = " << offset << "\n"; | |
} | |
curtime -= offset; | |
curtime = _byteswap_ulong(curtime); | |
std::memcpy(frame, &curtime, 4); | |
uint16_t pitch, yaw; | |
std::memcpy(&pitch, frame + 4, 2); | |
pitch = _byteswap_ushort(pitch); | |
std::memcpy(&yaw, frame + 6, 2); | |
yaw = _byteswap_ushort(yaw); | |
pitch += lastpitch; | |
yaw += lastyaw; | |
pitch = _byteswap_ushort(pitch); | |
yaw = _byteswap_ushort(yaw); | |
std::memcpy(frame + 4, &pitch, 2); | |
std::memcpy(frame + 6, &yaw, 2); | |
out.write(frame, 17); | |
bytesread += 17; | |
} | |
} | |
std::system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment