Show ConsoleFunctions.h syntax highlighted
/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG *
*Copyright (C) 2006, Tony Sparks *
* *
*This library is free software; you can redistribute it and/or *
*modify it under the terms of the GNU Lesser General Public *
*License as published by the Free Software Foundation; either *
*version 2.1 of the License, or (at your option) any later version. *
* *
*This library is distributed in the hope that it will be useful, *
*but WITHOUT ANY WARRANTY; without even the implied warranty of *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
*Lesser General Public License for more details. *
* *
*You should have received a copy of the GNU Lesser General Public *
*License along with this library; if not, write to the Free Software *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
**************************************************************************************
*/
#pragma once
#include "kernel.h"
#include "shared/stringutil.h"
#include "Cvars.h"
#include <stdlib.h>
#include <sstream>
extern Kernel* g_kernel;
//-------------
// Common Functions availible through the console
/**
========================
Exit the game
========================
*/
class Exit_f : public ICommand
{
public:
Exit_f() { };
void Exec(std::string &s) {
g_kernel->KillAll();
};
~Exit_f() { };
};
/**
======================
Clear the console window
======================
*/
class Clear_f : public ICommand
{
public:
Clear_f() {};
void Exec(std::string &s) {
g_kernel->GetConsole()->GetDisplay()->m_printStack.clear();
};
~Clear_f() {};
};
/**
====================
Print to the console
====================
*/
class Echo_f : public ICommand
{
public:
Echo_f() {};
void Exec(std::string &s) {
g_kernel->GetConsole()->Print( s.c_str() );
};
~Echo_f() {};
};
/**
====================
Execute a script file
====================
*/
class Exec_f : public ICommand
{
public:
Exec_f();
void Exec(std::string &file);
~Exec_f() {};
private:
int ParseFile(std::string &file);
};
/**
===================
Set a cvar
==================
*/
class Set_f : public ICommand
{
public:
Set_f() {};
void Exec(std::string &s) {
std::string name, value;
StringUtil token(s);
// parse the cvar name
if ( token.HasNext() ) {
name = token.GetNext();
if( !g_kernel->GetCvars()->IsValid(name) ) {
g_kernel->GetConsole()->Print( ("Parse error, invalid cvar name --> " + name).c_str());
return;
}
}
else
{
g_kernel->GetConsole()->Print( ("Parse error, no cvar name specified! --> " + s).c_str());
return;
}
std::cout << name << std::endl;
// get the value
if ( token.HasNext() ) {
value = token.GetNext();
}
else
{
g_kernel->GetConsole()->Print(g_kernel->GetCvars()->GetStringValue(name).c_str() );
return;
}
// register it
if( !g_kernel->GetCvars()->SetCvarValue( name , value) )
g_kernel->GetConsole()->Print( (name+ " is write protected").c_str());
}
~Set_f() {};
};
/**
======================
Create a dynamic cvar
Used for scripting
======================
*/
class Create_f : public ICommand
{
public:
Create_f() {};
void Exec(std::string &s)
{
std::string name ="", value = "";
StringUtil token(s);
// parse the cvar name
if ( token.HasNext() ) {
name = token.GetNext();
}
else
{
g_kernel->GetConsole()->Print( ("Parse error, no cvar name specified! --> " + s).c_str());
return;
}
// get the value
if ( token.HasNext() ) {
value = token.GetNext();
}
else
{
g_kernel->GetConsole()->Print( ("Parse error, no cvar value specified! --> "+ s).c_str());
return;
}
// register it -we only allow dynamic cvars that are saveable
g_kernel->GetCvars()->RegisterCvar( name , value, (float)atof(value.c_str()), CVAR_SAVE );
g_kernel->GetConsole()->Print( ("Cvar created: " + name + " " + value).c_str() );
}
~Create_f() {};
};
/**
=====================
List available cvars
=====================
*/
class List_f : public ICommand
{
public:
List_f() {};
void Exec(std::string &s) {
std::map< std::string, CMMPointer<cvar>>::iterator it;
int count = 0;
/**
* Print each cvar
*/
for( it = g_kernel->GetCvars()->GetCvars().begin(); it != g_kernel->GetCvars()->GetCvars().end(); )
{
++count;
g_kernel->GetConsole()->Print( (it->second->name + " = " + it->second->svalue).c_str() );
++it;
}
// convert a float to a string
std::stringstream buf;
buf << count;
g_kernel->GetConsole()->Print( (" Total cvars: " + buf.str()).c_str() );
};
~List_f() {};
};
/**
=============================
Toggle logging
=============================
*/
class Log_f : public ICommand
{
public:
Log_f() {};
void Exec(std::string &s) {
float v = (float)atof(s.c_str());
if ( v < 0 )
v = 0;
else if ( v > 0 )
v = 1;
g_kernel->GetCvars()->SetCvarValue("logger", v );
g_kernel->GetConsole()->SetLog( v );
};
~Log_f() {};
};
/**
=============================
Display all the commands
=============================
*/
//class CmdList_f : public ICommand
//{
//public:
// CmdList_f() {};
// void Exec(std::string &s) {
// // TODO
// };
//
// ~CmdList_f() {};
//};
/**
========================
Run a script
========================
*/
class Run_f : public ICommand
{
public:
Run_f() {};
void Exec(std::string &s) {
if ( s.size() )
g_kernel->GetScriptEngine()->RunFile( s );
else
g_kernel->GetConsole()->Print("<usage> run \"script_file\"");
};
~Run_f() {};
};
See more files for this project here