Add some functions

This commit is contained in:
Federico Cecchetto 2022-04-05 01:54:13 +02:00
parent bfed693737
commit 36d07057c5
5 changed files with 146 additions and 0 deletions

View File

@ -18,6 +18,7 @@ namespace scripting
{ {
std::unordered_map<int, std::unordered_map<std::string, int>> fields_table; std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table; std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
utils::concurrency::container<shared_table_t> shared_table;
namespace namespace
{ {

View File

@ -3,6 +3,9 @@
namespace scripting namespace scripting
{ {
using shared_table_t = std::unordered_map<std::string, std::string>;
extern std::unordered_map<int, std::unordered_map<std::string, int>> fields_table; extern std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
extern std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table; extern std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
extern utils::concurrency::container<shared_table_t> shared_table;
} }

View File

@ -9,6 +9,7 @@
#include "../../../component/command.hpp" #include "../../../component/command.hpp"
#include "../../../component/logfile.hpp" #include "../../../component/logfile.hpp"
#include "../../../component/scripting.hpp" #include "../../../component/scripting.hpp"
#include "../../../component/fastfiles.hpp"
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/io.hpp> #include <utils/io.hpp>
@ -501,6 +502,76 @@ namespace scripting::lua
return detour; return detour;
}; };
game_type["assetlist"] = [](const game&, const sol::this_state s, const std::string& type_string)
{
auto table = sol::table::create(s.lua_state());
auto index = 1;
auto type_index = -1;
for (auto i = 0; i < ::game::XAssetType::ASSET_TYPE_COUNT; i++)
{
if (type_string == ::game::g_assetNames[i])
{
type_index = i;
}
}
if (type_index == -1)
{
throw std::runtime_error("Asset type does not exist");
}
const auto type = static_cast<::game::XAssetType>(type_index);
fastfiles::enum_assets(type, [type, &table, &index](const ::game::XAssetHeader header)
{
const auto asset = ::game::XAsset{type, header};
const std::string asset_name = ::game::DB_GetXAssetName(&asset);
table[index++] = asset_name;
}, true);
return table;
};
game_type["sharedset"] = [](const game&, const std::string& key, const std::string& value)
{
scripting::shared_table.access([key, value](scripting::shared_table_t& table)
{
table[key] = value;
});
};
game_type["sharedget"] = [](const game&, const std::string& key)
{
std::string result;
scripting::shared_table.access([key, &result](scripting::shared_table_t& table)
{
result = table[key];
});
return result;
};
game_type["sharedclear"] = [](const game&)
{
scripting::shared_table.access([](scripting::shared_table_t& table)
{
table.clear();
});
};
game_type["getentbyref"] = [](const game&, const sol::this_state s,
const unsigned int entnum, const unsigned int classnum)
{
const auto id = ::game::Scr_GetEntityId(entnum, classnum);
if (id)
{
return convert(s, scripting::entity{id});
}
else
{
return sol::lua_value{s, sol::lua_nil};
}
};
} }
} }

View File

@ -43,6 +43,8 @@ namespace game
WEAK symbol<void(int localClientNum, const char* message)> CG_GameMessageBold{0x140138750, 0x140220620}; WEAK symbol<void(int localClientNum, const char* message)> CG_GameMessageBold{0x140138750, 0x140220620};
WEAK symbol<void(int localClientNum, /*mp::cg_s**/void* cg, WEAK symbol<void(int localClientNum, /*mp::cg_s**/void* cg,
const char* dvar, const char* value)> CG_SetClientDvarFromServer{0, 0x140236120}; const char* dvar, const char* value)> CG_SetClientDvarFromServer{0, 0x140236120};
WEAK symbol<char*(const unsigned int weapon,
bool isAlternate, char* outputBuffer, int bufferLen)> CG_GetWeaponDisplayName{0x14016EC30, 0x1400B5840};
WEAK symbol<bool()> CL_IsCgameInitialized{0x14017EE30, 0x140245650}; WEAK symbol<bool()> CL_IsCgameInitialized{0x14017EE30, 0x140245650};

View File

@ -10,6 +10,8 @@
#include "../../../component/updater.hpp" #include "../../../component/updater.hpp"
#include "../../../component/fps.hpp" #include "../../../component/fps.hpp"
#include "../../../component/localized_strings.hpp" #include "../../../component/localized_strings.hpp"
#include "../../../component/fastfiles.hpp"
#include "../../../component/scripting.hpp"
#include "component/game_console.hpp" #include "component/game_console.hpp"
#include "component/scheduler.hpp" #include "component/scheduler.hpp"
@ -68,6 +70,73 @@ namespace ui_scripting::lua
localized_strings::override(string, value); localized_strings::override(string, value);
}; };
game_type["sharedset"] = [](const game&, const std::string& key, const std::string& value)
{
scripting::shared_table.access([key, value](scripting::shared_table_t& table)
{
table[key] = value;
});
};
game_type["sharedget"] = [](const game&, const std::string& key)
{
std::string result;
scripting::shared_table.access([key, &result](scripting::shared_table_t& table)
{
result = table[key];
});
return result;
};
game_type["sharedclear"] = [](const game&)
{
scripting::shared_table.access([](scripting::shared_table_t& table)
{
table.clear();
});
};
game_type["assetlist"] = [](const game&, const sol::this_state s, const std::string& type_string)
{
auto table = sol::table::create(s.lua_state());
auto index = 1;
auto type_index = -1;
for (auto i = 0; i < ::game::XAssetType::ASSET_TYPE_COUNT; i++)
{
if (type_string == ::game::g_assetNames[i])
{
type_index = i;
}
}
if (type_index == -1)
{
throw std::runtime_error("Asset type does not exist");
}
const auto type = static_cast<::game::XAssetType>(type_index);
fastfiles::enum_assets(type, [type, &table, &index](const ::game::XAssetHeader header)
{
const auto asset = ::game::XAsset{type, header};
const std::string asset_name = ::game::DB_GetXAssetName(&asset);
table[index++] = asset_name;
}, true);
return table;
};
game_type["getweapondisplayname"] = [](const game&, const std::string& name)
{
const auto alternate = name.starts_with("alt_");
const auto weapon = ::game::G_GetWeaponForName(name.data());
char buffer[0x400] = {0};
::game::CG_GetWeaponDisplayName(weapon, alternate, buffer, 0x400);
return std::string(buffer);
};
auto userdata_type = state.new_usertype<userdata>("userdata_"); auto userdata_type = state.new_usertype<userdata>("userdata_");
userdata_type["new"] = sol::property( userdata_type["new"] = sol::property(