about summary refs log tree commit diff stats
path: root/data/maps/the_bearer/metadata.txtpb
blob: 584c71e6c6d1ee63d755610bdfc0e7f9fd5cdec1 (plain) (blame)
1
display_name: "The Bearer"
} /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include <filesystem>
#include <fstream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>

#include "script.h"

Script::Script(const std::filesystem::path& path, std::string_view file_prefix)
    : file_prefix_(file_prefix) {
  {
    std::ifstream script_file(path);
    std::string line;
    std::ostringstream text_stream;
    while (std::getline(script_file, line)) {
      text_stream << line << std::endl;
    }
    text_ = text_stream.str();
  }

  std::string search = std::string("load(\"") + std::string(file_prefix);
  std::string_view corpus = text_;
  int location;
  while ((location = corpus.find(search)) != std::string_view::npos) {
    corpus.remove_prefix(location + search.size());
    int divider = corpus.find_first_of("\"");
    if (divider == std::string_view::npos) {
      throw std::invalid_argument(
          "I don't know how this is happening but I guess I gotta report it.");
    }
    std::string script_path(corpus.substr(0, divider));
    corpus.remove_prefix(divider + 1);
    loaded_scripts_.push_back(script_path);
  }
}

void Script::ReplaceScriptReferences(
    const std::map<std::string, int>& script_ids) {
  std::ostringstream new_text;
  std::string_view corpus = text_;
  int divider = corpus.find_first_of("\n");
  if (divider == std::string_view::npos) {
    throw std::invalid_argument("Script is one line??");
  }
  new_text << corpus.substr(0, divider + 1) << std::endl;
  ;
  corpus.remove_prefix(divider + 1);

  std::map<std::string, std::string> path_to_export;
  for (const std::string& loaded_script : loaded_scripts_) {
    std::ostringstream export_str;
    export_str << "COMPACTED";

    std::filesystem::path name_path(loaded_script);
    name_path.replace_filename(name_path.stem());
    for (const auto& segment : name_path) {
      std::string segment_str(segment);
      export_str << "_" << segment_str;
    }

    std::string export_name = export_str.str();
    exports_.emplace_back(export_name, script_ids.at(loaded_script));

    new_text << "export(Resource) var " << export_name << std::endl;

    path_to_export[loaded_script] = export_name;
  }

  new_text << std::endl;

  std::string search = std::string("load(\"") + file_prefix_;
  std::string search_alt1 = std::string("preload(\"") + file_prefix_;
  std::string search_alt2 =
      std::string("ResourceLoader.load(\"") + file_prefix_;
  int location;
  while ((location = corpus.find(search)) != std::string_view::npos) {
    int alt1 = corpus.find(search_alt1);
    int alt2 = corpus.find(search_alt2);
    int remove_size = search.size();

    if (alt1 != std::string_view::npos && alt1 < location) {
      location = alt1;
      remove_size = search_alt1.size();
    } else if (alt2 != std::string_view::npos && alt2 < location) {
      location = alt2;
      remove_size = search_alt2.size();
    }

    new_text << corpus.substr(0, location);
    corpus.remove_prefix(location + remove_size);

    int divider = corpus.find_first_of("\"");
    // TODO: ehhh error reporting
    std::string referenced_path(corpus.substr(0, divider));
    corpus.remove_prefix(divider + 2);  // the quote and the close paren
    new_text << path_to_export.at(referenced_path);
  }

  new_text << corpus;

  text_ = new_text.str();
}

std::string Script::ToString() const { return text_; }