Program Listing for File command_script.cpp

Return to documentation for file (blam\components\haloscript\command_script.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<std::string> csc_cmds;

void BlamScript::InitCSC()
{
    Blam::LogEvent("Loading CSC command scripts (this will take a moment)");
    csc_cmds = BlamScript::getCommandsCSC();
}

std::vector<std::string> BlamScript::getCommandsCSC()
{
    std::vector<std::string> csc_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;

        //cout << filename << endl;


        std::string final_value = ss_conv.substr(ss_conv.find_last_of("\\") + 1);



        if (Blam::Utils::String::ends_with(filename, ".csc"))
        {
            csc_script_list.push_back(Blam::get_csc_cmd_name(filename));
        }

        //cout << path_char << endl;

        //cout << p << std::endl;
    }

    return csc_script_list;
}


/*bool Utils::String::starts_with_ciE(std::string mainStr, std::string toMatch)
{
    // Convert mainStr to lower case
    std::transform(mainStr.begin(), mainStr.end(), mainStr.begin(), ::tolower);
    // Convert toMatch to lower case
    std::transform(toMatch.begin(), toMatch.end(), toMatch.begin(), ::tolower);

    if (mainStr.find(toMatch) == 0)
        return true;
    else
        return false;
}*/





Blam::command_object Blam::CreateCommandFromFile(std::string filename)
{
    Blam::command_object cmd;

    std::ifstream infile("./dev_data/scripts/" + filename);


    std::string cmd_name;
    std::string cmd_desc;
    std::string cmd_action;

    std::string line;
    while (getline(infile, line))
    {

        //cout << "file found" << endl;
        if (Blam::Utils::String::starts_with_ci(line, ";") == false)
        {
            //cout << "not comment" << endl;
            //cout << line << endl;
            if (Blam::Utils::String::starts_with_ci(line, "(") != false)
            {

                //cout << "valid thingy" << endl;


                if (Blam::Utils::String::starts_with_ci(line, "(name") == true)
                {
                    //cmd_name = line.substr(7, line.find("\"") + 1);
                    std::string part1 = line.substr(line.find("\"") + 1);
                    //cout << part1 << endl;

                    //const char* cmd_desc_char = strchr(part1.c_str(), '"');

                    //cmd_desc = part1.substr(part1.find("\"") + 0);

                    cmd_name = Blam::csc_var_process(part1);

                }
                else if (Blam::Utils::String::starts_with_ci(line, "(desc") == true)
                {
                    std::string part1 = line.substr(line.find("\"") + 1);
                    //cout << part1 << endl;

                    //const char* cmd_desc_char = strchr(part1.c_str(), '"');

                    //cmd_desc = part1.substr(part1.find("\"") + 0);

                    cmd_desc = Blam::csc_var_process(part1);

                }
                else if (Blam::Utils::String::starts_with_ci(line, "(output") == true)
                {
                    //cmd_action = line.substr(9, line.find("\"") + 1);
                    std::string part1 = line.substr(line.find("\"") + 1);
                    //cout << part1 << endl;

                    //const char* cmd_desc_char = strchr(part1.c_str(), '"');

                    //cmd_desc = part1.substr(part1.find("\"") + 0);

                    cmd_action = Blam::csc_var_process(part1);
                }






            }


        }

    }

    //cout << "cmd_name:" << cmd_name << endl;
    //cout << "cmd_desc:" << cmd_desc << endl;
    //cout << "cmd_actn:" << cmd_action << endl;

    cmd.name = cmd_name;
    cmd.description = cmd_desc;
    cmd.action = cmd_action;


    return cmd;
}

std::string Blam::get_csc_cmd_name(std::string filename)
{
    //string ss_conv = filename;
    //ss_conv.append(".csc");

    std::string to_return = "unknown";

    std::string path = "./dev_data/scripts/";

    if (Blam::Utils::String::ends_with(filename, ".csc"))
    {
        //const char* yeah = filename;
        //const char* pp = "pp";
        //csc_script_list.insert(csc_iterator, filename);
        //csc_script_list.push_back(filename);
        //csc_script_list.push_back("aaaaaa");
        //csc_script_list.push_back(pp);

        //csc_script_list.push_back("EAT A FUCKING DICK");

        //csc_script_list.push_back(final_value);
        std::ifstream infile(path + filename);

        std::string line;
        while (getline(infile, line))
        {

            //cout << "file found" << endl;
            if (Blam::Utils::String::starts_with_ci(line, ";") == false)
            {
                //cout << "not comment" << endl;
                //std::cout << line << std::endl;
                if (Blam::Utils::String::starts_with_ci(line, "(") != false)
                {

                    //cout << "valid thingy" << endl;


                    if (Blam::Utils::String::starts_with_ci(line, "(name") == true)
                    {
                        //cmd_name = line.substr(7, line.find("\"") + 1);
                        std::string part1 = line.substr(line.find("\"") + 1);
                        //cout << part1 << endl;

                        //const char* cmd_desc_char = strchr(part1.c_str(), '"');

                        //cmd_desc = part1.substr(part1.find("\"") + 0);

                        //cmd_name = Blam::csc_var_process(part1);

                        to_return = Blam::csc_var_process(part1);

                    }
                    else
                    {

                    }



                }


            }

        }


    }

    return to_return;
}


std::string GetCmdName(std::string filename)
{
    std::ifstream infile("./dev_data/scripts/" + filename);


    std::string cmd_name;
    std::string cmd_desc;
    std::string cmd_action;

    std::string line;
    while (getline(infile, line))
    {

        //cout << "file found" << endl;
        if (Blam::Utils::String::starts_with_ci(line, ";") == false)
        {
            //cout << "not comment" << endl;
            Blam::LogEvent(line);
            if (Blam::Utils::String::starts_with_ci(line, "(") != false)
            {

                //cout << "valid thingy" << endl;


                if (Blam::Utils::String::starts_with_ci(line, "(name") == true)
                {
                    cmd_name = line.substr(7, line.find("\"") + 1);

                }
                else if (Blam::Utils::String::starts_with_ci(line, "(desc") == true)
                {
                    cmd_desc = line.substr(7, line.find("\"") + 1);
                }
                else if (Blam::Utils::String::starts_with_ci(line, "(output") == true)
                {
                    cmd_action = line.substr(9, line.find("\"") + 1);
                }






            }


        }

    }

    Blam::LogEvent("cmd_name:" + cmd_name);
    Blam::LogEvent("cmd_desc:" + cmd_name);
    Blam::LogEvent("cmd_actn:" + cmd_name);

    return "pp";
}
std::string GetCmdDesc(std::string filename)
{
    std::ifstream infile("./dev_data/scripts/" + filename);


    std::string cmd_name;
    std::string cmd_desc;
    std::string cmd_action;

    std::string line;
    while (getline(infile, line))
    {

        if (Blam::Utils::String::starts_with_ci(line, ";") == false)
        {
            if (Blam::Utils::String::starts_with_ci(line, "(") != false)
            {

                if (Blam::Utils::String::starts_with_ci(line, "(desc") == true)
                {
                    cmd_desc = line.substr(7, line.find("\"") + 1);
                }






            }


        }

    }

    Blam::LogEvent("cmd_desc:" + cmd_desc);

    return "pp";
}
std::string GetCmdAction(std::string filename)
{
    std::ifstream infile("./dev_data/scripts/" + filename);

    std::string cmd_action;

    std::string line;
    while (getline(infile, line))
    {

        //cout << "file found" << endl;
        if (Blam::Utils::String::starts_with_ci(line, ";") == false)
        {
            //cout << "not comment" << endl;
            //cout << line << endl;
            if (Blam::Utils::String::starts_with_ci(line, "(") != false)
            {

                if (Blam::Utils::String::starts_with_ci(line, "(output") == true)
                {
                    cmd_action = line.substr(9, line.find("\"") + 1);
                }

            }


        }

    }

    //cout << "cmd_actn:" << cmd_name << endl;

    return cmd_action;
}





Blam::command_object CreateCommandFromFileHSC(std::string filename)
{
    Blam::command_object cmd;


    cmd.name = filename.substr(0, filename.find(".hsc"));

    std::ifstream infile("./dev_data/scripts/" + filename);

    std::string final_value = "UNKNOWN_VAL";


    std::string line;
    while (getline(infile, line))
    {

        Blam::LogEvent("file found");
        if (Blam::Utils::String::starts_with_ci(";", line) == false)
        {
            Blam::LogEvent("not comment");
            /*if (Utils::String::starts_with_ciE("(", line) == true)
            {*/

                Blam::LogEvent("valid thingy");
                std::string cmd_scope = line.substr(2, line.find(" ")); //should be global
                std::string cmd_type = cmd_scope.substr(0, cmd_scope.find(" ")); //should be string
                std::string cmd_var_name = cmd_type.substr(0, cmd_type.find(" ")); //should be string

                Blam::LogEvent(cmd_scope);
                Blam::LogEvent(cmd_type);
                Blam::LogEvent(cmd_var_name);

                if (cmd_var_name == "command_desc")
                {
                    if (cmd_var_name.substr(0, cmd_var_name.find(" ")) == "\"")
                    {
                        //string cmd_v_00 = cmd_var_name.substr(0, cmd_var_name.find(" "));
                        //string cmd_v_01 = cmd_v_00.substr(0, cmd_v_00.find(" "));

                        std::string cmd_value = cmd_var_name.substr(2, cmd_var_name.find("\""));

                        cmd.description = cmd_value;
                    }
                }





            //}


        }

    }




    return cmd;
}


std::vector<std::string> BlamScript::get_csc_data()
{
    return csc_cmds;
}

std::string Blam::csc_var_process(std::string const& s)
{
    std::string::size_type pos = s.find('"');
    if (pos != std::string::npos)
    {
        return s.substr(0, pos);
    }
    else
    {
        return s;
    }
}

std::string Blam::csc_var_process(std::string const& s, char read_until)
{
    std::string::size_type pos = s.find(read_until);
    if (pos != std::string::npos)
    {
        return s.substr(0, pos);
    }
    else
    {
        return s;
    }
}