Program Listing for File blamscript.cpp¶
↰ Return to documentation for file (blam\components\haloscript\blamscript.cpp
)
#include "components\haloscript\haloscript.h"
#include <experimental/filesystem>
#include <sstream>
#include <fstream>
#include <iterator>
#include "components/core/logger/logger.h"
#include "components/core/utils/string/string.h"
#include "components/core/config/config.h"
std::vector<BlamScript::cmd_script_object> hsc_cmds;
void BlamScript::InitHSC()
{
Blam::LogEvent("Loading HSC command scripts (this will take a moment)");
hsc_cmds = BlamScript::getCommandsHSC();
}
std::vector<BlamScript::cmd_script_object> BlamScript::getCommandsHSC()
{
std::vector<BlamScript::cmd_script_object> hsc_script_list;
//list<const char*>::iterator csc_iterator;
//csc_iterator = csc_script_list.begin();
//++csc_iterator;
std::string path = "./dev_data/scripts/";
for (auto & p : std::experimental::filesystem::directory_iterator(path))
{
//convert directory entry to char (c-string)
std::stringstream ss_path;
ss_path << p;
std::string ss_conv = ss_path.str();
const char* path_char = ss_conv.c_str();
//const char* path_no_bs = p.path.c_str();
//cout << ss_path.str() << endl;
//get filenames only
int path_length = path.length();
const char* filename = path_char + path_length;
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: " + std::string(filename));
}
std::string final_value = ss_conv.substr(ss_conv.find_last_of("\\") + 1);
if (Blam::Utils::String::ends_with(filename, ".hsc"))
{
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: valid hsc: " + std::string(filename));
}
hsc_script_list.push_back(BlamScript::parse_cmd_script(filename));
}
//cout << path_char << endl;
//cout << p << std::endl;
}
return hsc_script_list;
}
BlamScript::cmd_script_object BlamScript::parse_cmd_script(std::string filename)
{
BlamScript::cmd_script_object command;
std::ifstream infile("./dev_data/scripts/" + filename);
bool script_multiline = false;
std::string cmd_name;
std::string cmd_desc;
std::string line;
while (getline(infile, line))
{
//Ensures the line is not commented
if (Blam::Utils::String::starts_with_ci(line, ";") == false)
{
//Ensures line starts with (
if (Blam::Utils::String::starts_with_ci(line, "(") != false)
{
//cout << "HSCLINE=========" << line << endl;
if (Blam::Utils::String::starts_with_ci(line, "(global") == true)
{
std::string hsc_s1 = line.substr(8);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: name-s1: " + hsc_s1);
}
//Add new global types here
if (Blam::Utils::String::starts_with_ci(hsc_s1, "string") == true)
{
std::string hsc_s2 = hsc_s1.substr(7);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: name-s2: " + hsc_s2);
}
if (Blam::Utils::String::starts_with_ci(hsc_s2, "command_name") == true)
{
std::string hsc_s3 = hsc_s2.substr(13);
std::string part1 = hsc_s3.substr(hsc_s3.find("\"") + 1);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: name-s3: " + hsc_s3);
Blam::LogEvent("[HSC_DEBUG]: name-part1: " + part1);
}
cmd_name = Blam::csc_var_process(part1);
}
if (Blam::Utils::String::starts_with_ci(hsc_s2, "command_desc") == true)
{
std::string hsc_s3 = hsc_s2.substr(13);
std::string part1 = hsc_s3.substr(hsc_s3.find("\"") + 1);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: desc-s3: " + hsc_s3);
Blam::LogEvent("[HSC_DEBUG]: desc-part1: " + part1);
}
cmd_desc = Blam::csc_var_process(part1);
}
}
if (Blam::Utils::String::starts_with_ci(hsc_s1, "boolean") == true)
{
}
if (Blam::Utils::String::starts_with_ci(hsc_s1, "short") == true)
{
}
}
if (Blam::Utils::String::starts_with_ci(line, "(script") == true)
{
std::string hsc_s1 = line.substr(8);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: script-s1: " + hsc_s1);
}
//Add new global types here
if (Blam::Utils::String::starts_with_ci(hsc_s1, "static") == true)
{
std::string hsc_s2 = hsc_s1.substr(7);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: script-s2: " + hsc_s2);
}
if (Blam::Utils::String::starts_with_ci(hsc_s2, "command_action") == true)
{
std::string hsc_s3;
try
{
hsc_s3 = hsc_s2.substr(15);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: script-s3: " + hsc_s3);
}
//multiline
if (Blam::Utils::String::starts_with_ci(hsc_s3, "(") == false)
{
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: script multiline is ON");
}
script_multiline = true;
}
}
catch (std::exception ex)
{
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: script-s3: [E] " + std::string(ex.what()));
Blam::LogEvent("[HSC_DEBUG]: script-s3: was probably too long. assuming multiline.");
//multiline
Blam::LogEvent("[HSC_DEBUG]: script multiline is ON");
}
script_multiline = true;
}
}
}
}
}
else if (script_multiline == true)
{
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: line read with multiline on");
}
if (Blam::Utils::String::starts_with_ci(line, ")") == true)
{
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: script multiline is OFF");
}
script_multiline = false;
}
else
{
//line.erase(remove(line.begin(), line.end(), ' '), line.end());
std::string action = line.substr(line.find("("));
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: is not closing )");
}
if (Blam::Utils::String::starts_with_ci(action, "(script") == false)
{
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: is not script definition");
Blam::LogEvent("[HSC_DEBUG]: action:" + action);
}
if (Blam::Utils::String::starts_with_ci(action, "(") != false)
{
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: is action");
}
BlamScript::script_action new_action;
std::string hsc_s1 = action.substr(1);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: s.act-s1: " + hsc_s1);
}
std::string hsc_s2 = Blam::csc_var_process(hsc_s1, ' ');
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: s.act-s2: " + hsc_s2);
}
std::string result;
std::remove_copy(hsc_s2.begin(), hsc_s2.end(), std::back_inserter(result), ')');
new_action.type = result;
try
{
std::string hsc_s3 = hsc_s1.substr(hsc_s2.length() + 1);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: s.act-s3: " + hsc_s3);
}
std::string part1 = hsc_s3.substr(hsc_s3.find("\"") + 1);
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: s.act-part1: " + part1);
}
new_action.arguments.push_back(Blam::csc_var_process(part1));
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: action-args: " + Blam::csc_var_process(part1));
Blam::LogEvent("[HSC_DEBUG]: action-type: " + hsc_s2);
}
//handle 2nd arguments here
//if (new_action.type == "echo")
//{
// int message_length = part1.length();
//
// string hsc_s4 = hsc_s1.substr(message_length + 1);
//
// string part2 = hsc_s3.substr(hsc_s4.find("\"") + 1);
//
// if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
// {
// cout << "[HSC_DEBUG]: script-s4: " << hsc_s4 << endl;
// cout << "[HSC_DEBUG]: script-part2: " << part2 << endl;
// }
//
// new_action.arguments.push_back(Blam::csc_var_process(part2));
//}
}
catch (std::exception ex)
{
Blam::LogEvent("### WARNING No arguments found for script " + std::string(filename) + " on action " + new_action.type);
}
command.actions.push_back(new_action);
}
}
}
}
}
}
command.name = cmd_name;
command.description = cmd_desc;
if (Blam::Config::GetConfig()->GetBoolean("hsc_debug") == true)
{
Blam::LogEvent("[HSC_DEBUG]: cmd_name: " + cmd_name);
Blam::LogEvent("[HSC_DEBUG]: cmd_desc: " + cmd_desc);
}
return command;
}
std::string BlamScript::get_cmd_name(std::string filename)
{
std::ifstream infile("./dev_data/scripts/" + filename);
std::string cmd_name;
std::string line;
while (getline(infile, line))
{
//Ensures the line is not commented
if (Blam::Utils::String::starts_with_ci(line, ";") == false)
{
//Ensures line starts with (
if (Blam::Utils::String::starts_with_ci(line, "(") != false)
{
if (Blam::Utils::String::starts_with_ci(line, "(global") == true)
{
std::string hsc_s1 = line.substr(8);
//Add new global types here
if (Blam::Utils::String::starts_with_ci(hsc_s1, "string") == true)
{
std::string hsc_s2 = hsc_s1.substr(7);
if (Blam::Utils::String::starts_with_ci(hsc_s2, "command_name") == true)
{
std::string hsc_s3 = hsc_s2.substr(13);
std::string part1 = hsc_s3.substr(hsc_s3.find("\"") + 1);
cmd_name = Blam::csc_var_process(part1);
}
}
}
}
}
}
return cmd_name;
}
std::vector<BlamScript::cmd_script_object> BlamScript::get_hsc_data()
{
return hsc_cmds;
}