Implement command-line playtesting (#163)

This commit is contained in:
leo60228
2020-04-09 12:03:24 -07:00
committed by GitHub
parent 4511ea172e
commit 94b2ebd55c
4 changed files with 129 additions and 1 deletions

View File

@@ -7,6 +7,10 @@
#include <stdlib.h>
#include <string.h>
#include <iterator>
#include <algorithm>
#include <iostream>
#include <SDL.h>
#include <physfs.h>
@@ -144,6 +148,28 @@ char *FILESYSTEM_getUserLevelDirectory()
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
size_t *len, bool addnull)
{
if (strcmp(name, "levels/special/stdin.vvvvvv") == 0) {
// this isn't *technically* necessary when piping directly from a file, but checking for that is annoying
static std::vector<char> STDIN_BUFFER;
static bool STDIN_LOADED = false;
if (!STDIN_LOADED) {
std::istreambuf_iterator<char> begin(std::cin), end;
STDIN_BUFFER.assign(begin, end);
STDIN_BUFFER.push_back(0); // there's no observable change in behavior if addnull is always true, but not vice versa
STDIN_LOADED = true;
}
size_t length = STDIN_BUFFER.size() - 1;
if (len != NULL) {
*len = length;
}
++length;
*mem = static_cast<unsigned char*>(malloc(length)); // STDIN_BUFFER.data() causes double-free
std::copy(STDIN_BUFFER.begin(), STDIN_BUFFER.end(), reinterpret_cast<char*>(*mem));
return;
}
PHYSFS_File *handle = PHYSFS_openRead(name);
if (handle == NULL)
{