Use gsc-tool tokens

This commit is contained in:
Federico Cecchetto 2022-09-01 19:37:27 +02:00
parent c72cfe510d
commit 34e3a57b57
7 changed files with 52 additions and 2231 deletions

2
deps/gsc-tool-h2 vendored

@ -1 +1 @@
Subproject commit b18d79c1da68c8bd97bbba91b72321a1302e651f
Subproject commit 77170a40a6558bb6f54c6d796f1574ba2de497a3

View File

@ -428,15 +428,15 @@ namespace gsc
auto function_id_start = 0x320;
void add_function(const std::string& name, scripting::script_function function)
{
if (scripting::function_map.find(name) != scripting::function_map.end())
if (xsk::gsc::h2::resolver::find_function(name))
{
const auto id = scripting::function_map[name];
const auto id = xsk::gsc::h2::resolver::function_id(name);
functions[function] = id;
}
else
{
const auto id = ++function_id_start;
scripting::function_map[name] = id;
xsk::gsc::h2::resolver::add_function(name, static_cast<std::uint16_t>(id));
functions[function] = id;
}
}
@ -512,25 +512,6 @@ namespace gsc
{
developer_script = dvars::register_bool("developer_script", false, 0, "Print GSC errors");
// wait for other tokens to be added
scheduler::once([]()
{
for (const auto& function : scripting::function_map)
{
xsk::gsc::h2::resolver::add_function(function.first, static_cast<std::uint16_t>(function.second));
}
for (const auto& method : scripting::method_map)
{
xsk::gsc::h2::resolver::add_method(method.first, static_cast<std::uint16_t>(method.second));
}
for (const auto& token : scripting::token_map)
{
xsk::gsc::h2::resolver::add_token(token.first, static_cast<std::uint16_t>(token.second));
}
}, scheduler::pipeline::main);
utils::hook::call(0x1405C6177, find_script);
utils::hook::call(0x1405C6187, db_is_xasset_default);

View File

@ -11,6 +11,10 @@
#include "command.hpp"
#include "game/scripting/functions.hpp"
#include <xsk/gsc/types.hpp>
#include <xsk/resolver.hpp>
#include <xsk/utils/compression.hpp>
#include <utils/hook.hpp>
#include <utils/concurrency.hpp>
#include <utils/string.hpp>
@ -169,13 +173,14 @@ namespace mapents
}
const auto key_ = key.substr(1, key.size() - 2);
if (scripting::token_map.find(key_) == scripting::token_map.end())
const auto id = xsk::gsc::h2::resolver::token_id(key_);
if (id == 0)
{
console::warn("[addon_map_ents parser] Key '%s' not found, on line %i", key_.data(), line_index);
continue;
}
out_buffer.append(utils::string::va("%i \"%s\"\n", scripting::token_map[key_], value.data()));
out_buffer.append(utils::string::va("%i \"%s\"\n", id, value.data()));
}
return out_buffer;
@ -288,13 +293,13 @@ namespace mapents
{
const auto id = token_id_start++;
custom_fields[id] = type;
scripting::token_map[name] = id;
xsk::gsc::h2::resolver::add_token(name, static_cast<std::uint16_t>(id));
}
void add_field(const std::string& name, game::scriptType_e type, unsigned int id)
{
custom_fields[id] = type;
scripting::token_map[name] = id;
xsk::gsc::h2::resolver::add_token(name, static_cast<std::uint16_t>(id));
}
utils::hook::detour scr_find_field_hook;
@ -334,15 +339,8 @@ namespace mapents
}
const auto id = static_cast<unsigned int>(std::atoi(line.substr(0, first_space).data()));
std::string key = std::to_string(id);
for (const auto& [token, value] : scripting::token_map)
{
if (value == id)
{
key = "\"" + token + "\"";
break;
}
}
const auto token = xsk::gsc::h2::resolver::token_name(static_cast<std::uint16_t>(id));
const auto key = "\"" + token + "\"";
const auto new_line = key + line.substr(first_space);
buffer.append(new_line);

File diff suppressed because it is too large Load Diff

View File

@ -3,57 +3,36 @@
#include "../../component/gsc.hpp"
#include <xsk/gsc/types.hpp>
#include <xsk/resolver.hpp>
#include <xsk/utils/compression.hpp>
#include <utils/string.hpp>
namespace scripting
{
namespace
{
std::unordered_map<std::string, unsigned> lowercase_map(
const std::unordered_map<std::string, unsigned>& old_map)
{
std::unordered_map<std::string, unsigned> new_map{};
for (auto& entry : old_map)
{
new_map[utils::string::to_lower(entry.first)] = entry.second;
}
return new_map;
}
const std::unordered_map<std::string, unsigned>& get_methods()
{
static auto methods = lowercase_map(method_map);
return methods;
}
const std::unordered_map<std::string, unsigned>& get_functions()
{
static auto function = lowercase_map(function_map);
return function;
}
int find_function_index(const std::string& name, const bool prefer_global)
{
const auto target = utils::string::to_lower(name);
const auto& primary_map = prefer_global
? get_functions()
: get_methods();
const auto& secondary_map = !prefer_global
? get_functions()
: get_methods();
auto function_entry = primary_map.find(target);
if (function_entry != primary_map.end())
auto first = xsk::gsc::h2::resolver::function_id;
auto second = xsk::gsc::h2::resolver::method_id;
if (!prefer_global)
{
return function_entry->second;
std::swap(first, second);
}
function_entry = secondary_map.find(target);
if (function_entry != secondary_map.end())
const auto first_res = first(target);
if (first_res)
{
return function_entry->second;
return first_res;
}
const auto second_res = second(target);
if (second_res)
{
return second_res;
}
return -1;
@ -79,11 +58,6 @@ namespace scripting
return static_cast<unsigned int>(std::strtol(name.substr(3).data(), nullptr, 10));
}
if (name.starts_with("_id_"))
{
return static_cast<unsigned int>(std::strtol(name.substr(4).data(), nullptr, 16));
}
return 0;
}
}
@ -92,43 +66,24 @@ namespace scripting
{
std::vector<std::string> results;
results.push_back(utils::string::va("_id_%X", id));
results.push_back(utils::string::va("_ID%i", id));
for (const auto& token : token_map)
{
if (token.second == id)
{
results.push_back(token.first);
break;
}
}
results.push_back(utils::string::va("_id_%04X", id));
results.push_back(xsk::gsc::h2::resolver::token_name(static_cast<std::uint16_t>(id)));
return results;
}
std::string find_token_single(unsigned int id)
{
std::vector<std::string> results;
for (const auto& token : token_map)
{
if (token.second == id)
{
return token.first;
}
}
return utils::string::va("_id_%X", id);
return xsk::gsc::h2::resolver::token_name(static_cast<std::uint16_t>(id));
}
unsigned int find_token_id(const std::string& name)
{
const auto result = token_map.find(name);
if (result != token_map.end())
const auto id = xsk::gsc::h2::resolver::token_id(name);
if (id != 0)
{
return result->second;
return id;
}
const auto parsed_id = parse_token_id(name);
@ -143,7 +98,10 @@ namespace scripting
script_function find_function(const std::string& name, const bool prefer_global)
{
const auto index = find_function_index(name, prefer_global);
if (index < 0) return nullptr;
if (index < 0)
{
return nullptr;
}
return get_function_by_index(index);
}

View File

@ -3,11 +3,6 @@
namespace scripting
{
extern std::unordered_map<std::string, unsigned> method_map;
extern std::unordered_map<std::string, unsigned> function_map;
extern std::unordered_map<std::string, unsigned> token_map;
extern std::unordered_map<unsigned, std::string> file_list;
using script_function = void(*)(game::scr_entref_t);
std::vector<std::string> find_token(unsigned int id);

View File

@ -16,6 +16,10 @@
#include "game/ui_scripting/execution.hpp"
#include <xsk/gsc/types.hpp>
#include <xsk/resolver.hpp>
#include <xsk/utils/compression.hpp>
#include <utils/string.hpp>
#include <utils/io.hpp>
#include <utils/nt.hpp>
@ -336,9 +340,10 @@ namespace scripting::lua
auto entity_type = state.new_usertype<entity>("entity");
for (const auto& func : method_map)
for (const auto& func : xsk::gsc::h2::resolver::get_methods())
{
const auto name = utils::string::to_lower(func.first);
const auto func_name = std::string(func.first);
const auto name = utils::string::to_lower(func_name);
entity_type[name.data()] = [name](const entity& entity, const sol::this_state s, sol::variadic_args va)
{
std::vector<script_value> arguments{};
@ -469,9 +474,10 @@ namespace scripting::lua
auto game_type = state.new_usertype<game>("game_");
state["game"] = game();
for (const auto& func : function_map)
for (const auto& func : xsk::gsc::h2::resolver::get_functions())
{
const auto name = utils::string::to_lower(func.first);
const auto func_name = std::string(func.first);
const auto name = utils::string::to_lower(func_name);
game_type[name] = [name](const game&, const sol::this_state s, sol::variadic_args va)
{
std::vector<script_value> arguments{};