Skip to content

Commit

Permalink
Redesigned scripting engine
Browse files Browse the repository at this point in the history
The variable itself is some data container with a type.
No full OO approach for performance reasons.
In the near future, an abstract script class can be used providing some methods that can execute directly instead of loading a script from plain text.
  • Loading branch information
matt77hias committed Oct 25, 2016
1 parent a481ae0 commit 507f700
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 127 deletions.
10 changes: 10 additions & 0 deletions MAGE/FPS/res/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#begin
a_true_bool bool true
a_false_bool bool false
an_integer int 5
a_float float 5.001000
a_float3 float3 1.010000 2.020000 3.030000
a_float4 float4 4.010000 5.020000 6.030000 7.040000
a_colour colour 8.010000 9.020000 10.030000 11.040000
a_string string "test"
#end
14 changes: 14 additions & 0 deletions MAGE/FPS/res/script_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#begin

a_true_bool bool true
a_false_bool bool false

an_integer int 5
a_float float 5.001
a_float3 float3 1.01 2.02 3.03
a_float4 float4 4.01 5.02 6.03 7.04
a_colour colour 8.01 9.02 10.03 11.04
a_string string "test"

#end
6 changes: 5 additions & 1 deletion MAGE/FPS/src/core/FPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ using namespace mage;
class TestState : public State {
virtual void Update(double elapsed_time) {
if (g_engine->GetInput()->GetKeyPress(DIK_Q)) {
PostQuitMessage(0);
//PostQuitMessage(0);

Script s("script_test.txt", "C:/Users/Matthias/Documents/Visual Studio 2015/Projects/MAGE/MAGE/FPS/res/");
s.SaveScript("C:/Users/Matthias/Documents/Visual Studio 2015/Projects/MAGE/MAGE/FPS/res/output.txt");

}
};
};
Expand Down
2 changes: 1 addition & 1 deletion MAGE/MAGE/src/resource/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace mage {
@return The filename of this resource.
*/
const string GetFilename() const {
return m_name + m_path;
return m_path + m_name;
}

private:
Expand Down
117 changes: 75 additions & 42 deletions MAGE/MAGE/src/scripting/script.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
//-----------------------------------------------------------------------------
// System Includes
//-----------------------------------------------------------------------------
#pragma region

#include <typeinfo>

#pragma endregion

//-----------------------------------------------------------------------------
// Engine Includes
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -82,62 +73,86 @@ namespace mage {
const Variable *next = it.Next();
const char *name = next->GetName().c_str();
const void *raw_value = next->GetValue();
const char *type_name = typeid(raw_value).name();

if (strcmp(type_name, "Pb") == 0) {
switch (next->GetType()) {
case BoolType: {
const bool *value = (bool *)raw_value;
if (*value) {
sprintf_s(output, sizeof(output), "%s bool true", name);
}
else {
sprintf_s(output, sizeof(output), "%s bool false", name);
}
fputs(output, file);
fputs("\n", file);
break;
}
else if (strcmp(type_name, "Pi") == 0) {
case IntType: {
const int *value = (int *)raw_value;
sprintf_s(output, sizeof(output), "%s int %d", name, *value);
fputs(output, file);
fputs("\n", file);
break;
}
else if (strcmp(type_name, "Ff") == 0) {
case FloatType: {
const float *value = (float *)raw_value;
sprintf_s(output, sizeof(output), "%s float %f", name, *value);
fputs(output, file);
fputs("\n", file);
break;
}
else if (strcmp(type_name, typeid(float3 *).name()) == 0) {
case Float3Type: {
const float3 *value = (float3 *)raw_value;
sprintf_s(output, sizeof(output), "%s float3 %f %f %f", name, value->x, value->y, value->z);
fputs(output, file);
fputs("\n", file);
break;
}
else if (strcmp(type_name, typeid(float4 *).name()) == 0) {
case Float4Type: {
const float4 *value = (float4 *)raw_value;
sprintf_s(output, sizeof(output), "%s float4 %f %f %f %f", name, value->x, value->y, value->z, value->w);
fputs(output, file);
fputs("\n", file);
break;
}
else if (strcmp(type_name, typeid(colour *).name()) == 0) {
case ColourType: {
const colour *value = (colour *)raw_value;
sprintf_s(output, sizeof(output), "%s colour %f %f %f %f", name, value->x, value->y, value->z, value->w);
fputs(output, file);
fputs("\n", file);
break;
}
else if (strcmp(type_name, "PSs") == 0) {
case StringType: {
const string *value = (string *)raw_value;
sprintf_s(output, sizeof(output), "%s string %s", name, value->c_str());
sprintf_s(output, sizeof(output), "%s string \"%s\"", name, value->c_str());
fputs(output, file);
fputs("\n", file);
break;
}
else if (strcmp(type_name, "Pv") == 0) {
case UnknownType: {
const char *value = (char *)raw_value;
sprintf_s(output, sizeof(output), "%s unknown %s", name, value);
fputs(output, file);
fputs("\n", file);
break;
}
else {
default: {
Warning("Could not export variable: %s", name);
continue;
}

fputs(output, file);
fputs("\n", file);
}
}

// Write the #end statement to the file.
fputs("#end", file);

// Close the script file.
fclose(file);
}

void Script::ImportVariable(const string &variable_name, FILE *file) {
void Script::ImportVariable(const string &name, FILE *file) {
// Ensure the file pointer is valid.
if (file == NULL) {
Warning("Could not import variable: %s", variable_name);
Warning("Could not import variable: %s", name);
return;
}

Expand All @@ -151,21 +166,21 @@ namespace mage {
bool *value = new bool;
fscanf_s(file, "%s", buffer, (unsigned int)sizeof(buffer));
*value = (strcmp(buffer, "true") == 0) ? true : false;
AddVariable(variable_name, value);
AddVariable(name, BoolType, value);
}
else if (strcmp(buffer, "int") == 0) {
// The variable is an int.
int *value = new int;
fscanf_s(file, "%s", buffer, (unsigned int)sizeof(buffer));
*value = atoi(buffer);
AddVariable(variable_name, value);
AddVariable(name, IntType, value);
}
else if (strcmp(buffer, "float") == 0) {
// The variable is a float.
float *value = new float;
fscanf_s(file, "%s", buffer, (unsigned int)sizeof(buffer));
*value = (float)atof(buffer);
AddVariable(variable_name, value);
AddVariable(name, FloatType, value);
}
else if (strcmp(buffer, "float3") == 0) {
// The variable is a float3.
Expand All @@ -176,7 +191,7 @@ namespace mage {
value->y = (float)atof(buffer);
fscanf_s(file, "%s", buffer, (unsigned int)sizeof(buffer));
value->z = (float)atof(buffer);
AddVariable(variable_name, value);
AddVariable(name, Float3Type, value);
}
else if (strcmp(buffer, "float4") == 0) {
// The variable is a float4.
Expand All @@ -189,7 +204,7 @@ namespace mage {
value->z = (float)atof(buffer);
fscanf_s(file, "%s", buffer, (unsigned int)sizeof(buffer));
value->w = (float)atof(buffer);
AddVariable(variable_name, value);
AddVariable(name, Float4Type, value);
}
else if (strcmp(buffer, "colour") == 0) {
// The variable is a colour.
Expand All @@ -202,7 +217,7 @@ namespace mage {
value->z = (float)atof(buffer);
fscanf_s(file, "%s", buffer, (unsigned int)sizeof(buffer));
value->w = (float)atof(buffer);
AddVariable(variable_name, value);
AddVariable(name, ColourType, value);
}
else if (strcmp(buffer, "string") == 0) {
// The variable is a string.
Expand Down Expand Up @@ -261,25 +276,25 @@ namespace mage {
} while (commas_found == true);

const string *value = new string(complete_string);
AddVariable(variable_name, value);
AddVariable(name, StringType, value);
}
else {
// The variable has an unknown type.
char *value = new char[strlen(buffer) + 1];
fscanf_s(file, "%s", buffer, (unsigned int)sizeof(buffer));
strcpy_s(value, sizeof(value), buffer);
AddVariable(variable_name, (void *)value);
AddVariable(name, UnknownType, (void *)value);
}
}

bool Script::RemoveVariable(const string &variable_name) {
bool Script::RemoveVariable(const string &name) {
Variable *target = NULL;

// Iterate the states looking for the specified variable.
LinkedList< Variable >::LinkedListIterator it = m_variables->GetIterator();
while (it.HasNext()) {
Variable *next = it.Next();
if (next->GetName() == variable_name) {
if (next->GetName() == name) {
target = next;
break;
}
Expand All @@ -295,12 +310,12 @@ namespace mage {
}

template < typename T >
const T *Script::GetValueOfVariable(const string &variable_name) const {
const T *Script::GetValueOfVariable(const string &name) const {
// Iterate the states looking for the specified variable.
LinkedList< Variable >::LinkedListIterator it = m_variables->GetIterator();
while (it.HasNext()) {
const Variable *next = it.Next();
if (next->GetName() == variable_name) {
if (next->GetName() == name) {
return (T *)next->GetValue();
}
}
Expand All @@ -309,10 +324,28 @@ namespace mage {
}

template < typename T >
void Script::SetValueOfVariable(const string &variable_name, const T *value) {
if (RemoveVariable(variable_name)) {
// Readd the variable with a new value.
AddVariable(variable_name, value);
void Script::SetValueOfVariable(const string &name, const T *value) {
Variable *target = NULL;

// Iterate the states looking for the specified variable.
LinkedList< Variable >::LinkedListIterator it = m_variables->GetIterator();
while (it.HasNext()) {
Variable *next = it.Next();
if (next->GetName() == name) {
target = next;
break;
}
}

if (target == NULL) {
return;
}

// Get type.
const VariableType type = target->GetType();
// Remove the variable
m_variables->Remove(&target);
// Readd the variable with a new value.
AddVariable(name, type, value);
}
}
34 changes: 18 additions & 16 deletions MAGE/MAGE/src/scripting/script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,70 +40,72 @@ namespace mage {
/**
Import the given variable from the given file to this script .
@pre No variable with the name @a variable_name
@pre No variable with the name @a name
exists in this script.
@param[in] variable_name
@param[in] name
The name of the variable.
@param[in, out] file
A pointer to a file containing the value of the variable.
*/
void ImportVariable(const string &variable_name, FILE *file);
void ImportVariable(const string &name, FILE *file);

/**
Adds the given variable to this script.
@pre No variable with the name @a variable_name
@pre No variable with the name @a name
exists in this script.
@tparam T
The type of the value.
@param[in] variable_name
@param[in] name
The name of the variable.
@param[in] type
The type of the variable.
@param[in] value
A pointer to the value of the variable.
*/
template < typename T >
void AddVariable(const string &variable_name, const T *value) {
m_variables->Add(new Variable(variable_name, value));
void AddVariable(const string &name, VariableType type, const T *value) {
m_variables->Add(new Variable(name, type, (void *)value));
}

/**
Removes the given variable from this script.
@param[in] variable_name
@param[in] name
The name of the variable.
@return @c true if a variable with the name @a variable_name
@return @c true if a variable with the name @a name
exists in this script prior to removal.
*/
bool RemoveVariable(const string &variable_name);
bool RemoveVariable(const string &name);

/**
Returns the value of the given variable in this script.
@tparam T
The type of the value.
@param[in] variable_name
@param[in] name
The name of the variable.
@return @c NULL if no variable with the name @a variable_name
@return @c NULL if no variable with the name @a name
exists in this script.
@return A pointer to the value of the variable.
*/
template < typename T >
const T *GetValueOfVariable(const string &variable_name) const;
const T *GetValueOfVariable(const string &name) const;

/**
Sets the value of the given variable in this script.
@tparam T
The type of the value.
@param[in] variable_name
@param[in] name
The name of the variable.
@param[in] value
A pointer to the value of the variable.
@note Nothing happens if no variable with the name @a variable_name
@note Nothing happens if no variable with the name @a name
exists in this script.
*/
template < typename T >
void SetValueOfVariable(const string &variable_name, const T *value);
void SetValueOfVariable(const string &name, const T *value);

private:

Expand Down
Loading

0 comments on commit 507f700

Please sign in to comment.