/*
This is a wrapper for AMGC2 plugins I wrote to be able to play games without using amgc2.
It's usefull if you don't want the burden of having to enter the GUI.
*/
#ifndef AMGC_WRAPPER_HPP
#define AMGC_WRAPPER_HPP


#include <allegro.h>
#include <stdio.h>
#include <string>
#include "debug.h"
#include "IGame.h"
#include "plugin.h"
#include "KeySettings.cpp"
#include "global.cpp"

const char *getCompleteFilePath(const char *filename)
{
	char tempStr[1024];
	return (make_absolute_filename(tempStr,"",filename,sizeof(tempStr)));
}

const char *getFileName(const char *filename)
{
	return get_filename(getCompleteFilePath(filename));
}

const char *getFilePath(const char *filename)
{
	std::string tempStr1(getCompleteFilePath(filename));
	char *tempStr2 = (char *)getFileName(filename);
	tempStr1.replace((tempStr1.length()-std::string(tempStr2).length()),std::string(tempStr2).length(),"",0);
	return tempStr1.c_str();
}

IGame *loadPlugin(const char *filename) 
{
DEBUG("main.log")
	if (exists(filename))
	{
DEBUG("main.log")
		IGame *game = *(new Plugin<IGame>(getCompleteFilePath(filename)));
		if(game)
		{
			game->SetGamePath(getFilePath(filename));
		}
		return game;
	}
	else
	{
DEBUG("main.log")
		return 0;
	}
}

bool loadSettings(GameSettings *settings, const char *file)
{
	std::string tmpStr(file);
	tmpStr+=".cfg";
	if(!exists(tmpStr.c_str()))return false;
	load_settings(settings, tmpStr.c_str());
	return true;
}

void saveSettings(GameSettings *settings, const char *file)
{
	std::string tmpStr(file);
	tmpStr+=".cfg";
	save_settings(settings,tmpStr.c_str());
}

int count_tokens(const char *str) {
	static char buf[256];
	usprintf(buf, str);
	char *tok = ustrtok(buf, "|");
	int i = 0;
	while (tok) {
		++i;
		tok = ustrtok(0, "|");
	}
	return i;
}

const char *get_token(const char *str, int i) {
	static char buf[256];
	usprintf(buf, str);
	char *tok = ustrtok(buf, "|");
	int count = 0;
	while (tok && count < i) {
		++count;
		tok = ustrtok(0, "|");
	}
	return tok;
}

#endif		/* AMGC_WRAPPER_HPP */
