<commit_before>/*
 * game.cpp
 *
 *  Created on: Feb 22, 2014
 *      Author: Lukas_2
 */

#include "game.h"
#include "asset_helper.h"
#include "stdlib.h"
#include <string>
#include "sys/stat.h"
#include "errno.h"
#include "loading_screen.h"
#include "content.h"

Game* gGame;
messageHandler gMessageHandler;
bool hasLoadedResources;
LoadingScreen loadingScreen;

void gameInitialize(android_app* app)
{
	gGame = new Game();
	gGame->clock	 = new Clock();
	gGame->sensor 	 = new SensorState();
	gGame->screen    = new Screen();
	gGame->network	 = networkInitialize(app);
	gGame->renderer  = rendererInitialize(1024);
	gGame->content   = new Content(128);

	loadingScreen = LoadingScreen();

	LOGI("Initializing Game!");
	initializeLuaCore();
}

void gameTerminate()
{
	delete gGame->clock;
	delete gGame->sensor;

	networkDelete(gGame->network);
	rendererDelete(gGame->renderer);

	delete gGame;
}

void gameStart()
{
	hasLoadedResources = false;
	LOGI("Starting game!");
	clockStart(gGame->clock);
	networkConnect(gGame->network);
}

void gameRestart()
{
	LOGI("Restarting game!");
	clockStart(gGame->clock);
	networkReconnect(gGame->network);
}

void gameResume()
{
	gGame->paused = false;
}

void gamePause()
{
	gGame->paused = true;
}

void gameSurfaceCreated()
{
	rendererActivate(gGame->renderer);
	if (!hasLoadedResources)
		loadingScreen.load();
}

void gameSurfaceDestroyed()
{
}

void gameStop()
{
	networkDisconnect(gGame->network);
//	rendererDeactivate(gGame->renderer);
	gGame->content->unloadAll();
	termLuaCall();
}

void handleFileTransfer(Buffer* buffer) {

	//TODO: We don't send a type from the server yet.
	auto type = 0;//bufferReadByte(buffer);
	AutoPtr<char> path;
	auto stringSize = bufferReadUTF8(buffer, &path.ptr);
	auto fileSize = bufferReadLong(buffer);

	LOGI("Received a file! name=%s type=%d size=%llu", path.ptr, type, fileSize);

	//Open Asset here - or do something else that is nice.
	auto externalsDir = gApp->activity->externalDataPath;
	std::string filePath(externalsDir);
	filePath += "/";
	for(int i = 0; i < stringSize; i++) {
		if(path.ptr[i] == '/') {
			int err = mkdir(filePath.c_str(), 0770);
			if(err != 0 && errno != 17)
				LOGI("Failed to make directory: %d %s", errno, strerror(errno));
		}
		filePath += path.ptr[i];
	}

	auto file = fopen(filePath.c_str(), "w+");
	if(file == NULL) {
		LOGE("Unable to write/create file: %s", filePath.c_str());
	} else {
		LOGI("Created file %s", filePath.c_str());
	}

	auto count = bufferBytesRemaining(buffer);
	LOGI("Count: %d, Filesize: %d", count, (int)fileSize);
	AutoPtr<uint8_t> chunckBuffer(0xFFFF);
	while (true) {
		uint32_t toRead = fileSize < count ? fileSize : count;
		auto read = bufferReadBytes(buffer, chunckBuffer.ptr, toRead);

		fwrite(chunckBuffer.ptr, sizeof(uint8_t), read, file);
		fileSize -= read;
		if (fileSize == 0)
			break;

		count = networkReceive(gGame->network);
		if (count != 0)
			LOGI("count %d, filesize %d", count, (int)fileSize);
	}

	fclose(file);
	LOGI("SUCESSFULLY WROTE A FILE!");
}

void handleAllResourcesLoaded(Buffer* buffer)
{
	hasLoadedResources = true;
	loadingScreen.unload();

	bufferReadUTF8(buffer, &gGame->name);
	initializeLuaScripts(gGame->name);
    initLuaCall();
}

void handleFileReload(Buffer* buf, size_t size)
{
	AutoPtr<char> path;
	auto stringSize = bufferReadUTF8(buf, &path.ptr);
	std::string str(path.ptr);
	gGame->content->reloadAsset(str);
	LOGI("File reloaded!");
}

void gameHandleReceive()
{
	auto count = networkReceive(gGame->network);
	if(count == 0) return;

	auto buffer = gGame->network->in_;
	size_t remaining;
	while(true)
	{
		if((remaining = bufferBytesRemaining(buffer)) == 0) {
			count = networkReceive(gGame->network);
			if(count == 0) return;
		}

		auto size = bufferReadShort(buffer);<commit_msg>WHy oh great master why<commit_after>/*
 * game.cpp
 *
 *  Created on: Feb 22, 2014
 *      Author: Lukas_2
 */

#include "game.h"
#include "asset_helper.h"
#include "stdlib.h"
#include <string>
#include "sys/stat.h"
#include "errno.h"
#include "loading_screen.h"
#include "content.h"
#include "path.h"

Game* gGame;
messageHandler gMessageHandler;
bool hasLoadedResources;
LoadingScreen loadingScreen;

void gameInitialize(android_app* app)
{
	gGame = new Game();
	gGame->clock	 = new Clock();
	gGame->sensor 	 = new SensorState();
	gGame->screen    = new Screen();
	gGame->network	 = networkInitialize(app);
	gGame->renderer  = rendererInitialize(1024);
	gGame->content   = new Content(128);

	loadingScreen = LoadingScreen();

	LOGI("Initializing Game!");
	initializeLuaCore();
}

void gameTerminate()
{
	delete gGame->clock;
	delete gGame->sensor;

	networkDelete(gGame->network);
	rendererDelete(gGame->renderer);

	delete gGame;
}

void gameStart()
{
	hasLoadedResources = false;
	LOGI("Starting game!");
	clockStart(gGame->clock);
	networkConnect(gGame->network);
}

void gameRestart()
{
	LOGI("Restarting game!");
	clockStart(gGame->clock);
	networkReconnect(gGame->network);
}

void gameResume()
{
	gGame->paused = false;
}

void gamePause()
{
	gGame->paused = true;
}

void gameSurfaceCreated()
{
	rendererActivate(gGame->renderer);
	if (!hasLoadedResources)
		loadingScreen.load();
}

void gameSurfaceDestroyed()
{
}

void gameStop()
{
	networkDisconnect(gGame->network);
	gGame->content->unloadAll();
	termLuaCall();
}

void handleFileTransfer(Buffer* buffer) {
	AutoPtr<char> path;
	auto stringSize = bufferReadUTF8(buffer, &path.ptr);
	auto fileSize = bufferReadLong(buffer);

	LOGI("Received a file! name=%s size=%llu", path.ptr, fileSize);

	//Open Asset here - or do something else that is nice.
	auto externalsDir = gApp->activity->externalDataPath;
	std::string filePath(externalsDir);
	filePath += "/";
	for(int i = 0; i < stringSize; i++) {
		if(path.ptr[i] == '/') {
			int err = mkdir(filePath.c_str(), 0770);
			if(err != 0 && errno != 17)
				LOGI("Failed to make directory: %d %s", errno, strerror(errno));
		}
		filePath += path.ptr[i];
	}

	auto file = fopen(filePath.c_str(), "w+");
	if(file == NULL) {
		LOGE("Unable to write/create file: %s", filePath.c_str());
	} else {
		LOGI("Created file %s", filePath.c_str());
	}

	auto count = bufferBytesRemaining(buffer);
	LOGI("Count: %d, Filesize: %d", count, (int)fileSize);
	AutoPtr<uint8_t> chunckBuffer(0xFFFF);
	while (true) {
		uint32_t toRead = fileSize < count ? fileSize : count;
		auto read = bufferReadBytes(buffer, chunckBuffer.ptr, toRead);

		fwrite(chunckBuffer.ptr, sizeof(uint8_t), read, file);
		fileSize -= read;
		if (fileSize == 0)
			break;

		count = networkReceive(gGame->network);
		if (count != 0)
			LOGI("count %d, filesize %d", count, (int)fileSize);
	}

	fclose(file);
	LOGI("SUCESSFULLY WROTE A FILE!");
}

void handleAllResourcesLoaded(Buffer* buffer)
{
	hasLoadedResources = true;
	loadingScreen.unload();

	bufferReadUTF8(buffer, &gGame->name);
	LOGI("Game %s", gGame->name);
	initializeLuaScripts(gGame->name);
    initLuaCall();
}

void handleFileReload(Buffer* buf, size_t size)
{
	AutoPtr<char> path;
	auto stringSize = bufferReadUTF8(buf, &path.ptr);
	std::string str(path.ptr);
	if(path::hasExtension(str, ".lua"))
		loadLuaScript(str);
	else
		gGame->content->reloadAsset(str);

	LOGI("File reloaded! %s", str.c_str());
}

void gameHandleReceive()
{
	auto count = networkReceive(gGame->network);
	if(count == 0) return;

	auto buffer = gGame->network->in_;
	size_t remaining;
	while(true)
	{
		if((remaining = bufferBytesRemaining(buffer)) == 0) {
			count = networkReceive(gGame->network);
			if(count == 0) return;
		}

		auto size = bufferReadShort(buffer);
{'fraction_non_alphanumeric': 0.15941262071702605, 'fraction_numerical': 0.009392776822330997, 'mean_word_length': 4.924764890282131, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4596201', 'n_tokens_mistral': 2889, 'n_tokens_neox': 2730, 'n_words': 596}