<commit_before>#include <fabric/map.hpp>


fabric::Map::Map(std::string fileName)
{
	Map::L = luaL_newstate();
	luaL_openlibs(Map::L);

	if (luaL_loadfile(Map::L, fileName.c_str()) || lua_pcall(Map::L, 0, 0, 0)) {
		Map::L = 0;
		std::cout << "Error" << std::endl;
	}

	
}

fabric::Map::~Map()
{
	Map::close();
	lua_close(Map::L);
}

int fabric::Map::pushToTop(std::string name) {
	Map::pushToTop(name, true);
	return 0;
}


int fabric::Map::pushToTop(std::string name, bool __first)
{
	bool first = __first;
	std::string subPropertyName = "";			//example: "foo.bar.x": here foo, bar and x are subPropertyNames as they are seperated by a .
	for (unsigned int i = 0; i < name.size(); i++) {
		
		if(name.at(i) != '.') subPropertyName = subPropertyName + name.at(i);

		if (name.at(i) == '.' || i + (unsigned int)(1) >= name.size())
		{
			if (first)
				lua_getglobal(Map::L, subPropertyName.c_str());
			else
				lua_getfield(Map::L, -1, subPropertyName.c_str());

			first = false;

			if (lua_isnil(Map::L, -1)) {
				std::cout << subPropertyName.c_str() << " in " << name.c_str() << " is nil" << std::endl;
				lua_pop(Map::L, 1);
				return 1;
			}

			subPropertyName = "";
		}

	}

	return 0;
}


int fabric::Map::close() {

	for (unsigned int gameObjectIndex = 0; gameObjectIndex < Map::gameObjects.size(); gameObjectIndex++) {
		Map::gameObjects.at(gameObjectIndex)->free();

		delete Map::gameObjects.at(gameObjectIndex);
		Map::gameObjects.erase(Map::gameObjects.begin() + gameObjectIndex);
		
	}
	return 0;
}

typedef void(*functionType)();
int fabric::Map::open(){
	pushToTop("game_objects.amount");
	int amount = (int)lua_tointeger(Map::L, -1);
	lua_pop(Map::L, 1);

	for (int idx = 0; idx < amount; idx++) {
		pushToTop("game_objects.object_" + std::to_string(idx)); // "game_objects.object_" + idx
		pushToTop("path", false); // Stack looks like:  ["path"] ["object_" + idx] ["game_objects"]
		std::string path = std::string(lua_tostring(Map::L, -1)); // ["path"] gets turned into string
		
		lua_remove(Map::L, -1); // ["path"] gets poped from stack
		
		GameObject* gObj = new GameObject();

		HINSTANCE dllHandle = LoadLibrary(("./__game/" + path).c_str()); // "./__game/" + path


		gObj->setupPointer = (functionType)(GetProcAddress(dllHandle, "setup"));
		gObj->updatePointer = (functionType)(GetProcAddress(dllHandle, "update"));

		pushToTop("amount", false); // Stack looks like:  ["amount"] ["object_" + idx] ["game_objects"]

		int amountAttributes = (int)lua_tointeger(Map::L, -1); 

		lua_remove(Map::L, -1); // ["amount"] gets poped from stack

		for (int jdx = 0; jdx < amountAttributes; jdx++) {
			pushToTop("attribute_" + std::to_string(jdx), false);<commit_msg>GameObject's dllHandel is no setup correctly<commit_after>#include <fabric/map.hpp>


fabric::Map::Map(std::string fileName)
{
	Map::L = luaL_newstate();
	luaL_openlibs(Map::L);

	if (luaL_loadfile(Map::L, fileName.c_str()) || lua_pcall(Map::L, 0, 0, 0)) {
		Map::L = 0;
		std::cout << "Error" << std::endl;
	}

	
}

fabric::Map::~Map()
{
	Map::close();
	lua_close(Map::L);
}

int fabric::Map::pushToTop(std::string name) {
	Map::pushToTop(name, true);
	return 0;
}


int fabric::Map::pushToTop(std::string name, bool __first)
{
	bool first = __first;
	std::string subPropertyName = "";			//example: "foo.bar.x": here foo, bar and x are subPropertyNames as they are seperated by a .
	for (unsigned int i = 0; i < name.size(); i++) {
		
		if(name.at(i) != '.') subPropertyName = subPropertyName + name.at(i);

		if (name.at(i) == '.' || i + (unsigned int)(1) >= name.size())
		{
			if (first)
				lua_getglobal(Map::L, subPropertyName.c_str());
			else
				lua_getfield(Map::L, -1, subPropertyName.c_str());

			first = false;

			if (lua_isnil(Map::L, -1)) {
				std::cout << subPropertyName.c_str() << " in " << name.c_str() << " is nil" << std::endl;
				lua_pop(Map::L, 1);
				return 1;
			}

			subPropertyName = "";
		}

	}

	return 0;
}


int fabric::Map::close() {

	for (unsigned int gameObjectIndex = 0; gameObjectIndex < Map::gameObjects.size(); gameObjectIndex++) {
		Map::gameObjects.at(gameObjectIndex)->free();

		delete Map::gameObjects.at(gameObjectIndex);
		Map::gameObjects.erase(Map::gameObjects.begin() + gameObjectIndex);
		
	}
	return 0;
}

typedef void(*functionType)();
int fabric::Map::open(){
	pushToTop("game_objects.amount");
	int amount = (int)lua_tointeger(Map::L, -1);
	lua_pop(Map::L, 1);

	for (int idx = 0; idx < amount; idx++) {
		pushToTop("game_objects.object_" + std::to_string(idx)); // "game_objects.object_" + idx
		pushToTop("path", false); // Stack looks like:  ["path"] ["object_" + idx] ["game_objects"]
		std::string path = std::string(lua_tostring(Map::L, -1)); // ["path"] gets turned into string
		
		lua_remove(Map::L, -1); // ["path"] gets poped from stack
		
		GameObject* gObj = new GameObject();

		HINSTANCE dllHandle = LoadLibrary(("./__game/" + path).c_str()); // "./__game/" + path
		gObj->dllHandle = dllHandle;

		gObj->setupPointer = (functionType)(GetProcAddress(dllHandle, "setup"));
		gObj->updatePointer = (functionType)(GetProcAddress(dllHandle, "update"));

		pushToTop("amount", false); // Stack looks like:  ["amount"] ["object_" + idx] ["game_objects"]

		int amountAttributes = (int)lua_tointeger(Map::L, -1); 

		lua_remove(Map::L, -1); // ["amount"] gets poped from stack

		for (int jdx = 0; jdx < amountAttributes; jdx++) {
			pushToTop("attribute_" + std::to_string(jdx), false);
{'fraction_non_alphanumeric': 0.19941295175197213, 'fraction_numerical': 0.008071913410383416, 'mean_word_length': 4.435692921236291, 'pattern_counts': {'":': 2, '<': 41, '<?xml version=': 0, '>': 14, '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': '3068629', 'n_tokens_mistral': 2193, 'n_tokens_neox': 1977, 'n_words': 424}