mirror of
https://github.com/fedddddd/iw5-gsc-utils.git
synced 2025-04-21 05:15:44 +00:00
Add some functions
This commit is contained in:
parent
d2c9e8c2e8
commit
a58e8c95b6
@ -198,17 +198,9 @@ namespace gsc
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
function::add("lol", [](function_args args) -> scripting::script_value
|
||||
function::add("executecommand", [](function_args args) -> scripting::script_value
|
||||
{
|
||||
const auto str = args[0].as<std::string>();
|
||||
|
||||
return str;
|
||||
});
|
||||
|
||||
method::add("test", [](game::scr_entref_t, function_args args) -> scripting::script_value
|
||||
{
|
||||
printf("here\n");
|
||||
|
||||
game::Cbuf_AddText(0, args[0].as<const char*>());
|
||||
return {};
|
||||
});
|
||||
|
||||
|
139
src/component/io.cpp
Normal file
139
src/component/io.cpp
Normal file
@ -0,0 +1,139 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "scheduler.hpp"
|
||||
|
||||
#include "game/scripting/event.hpp"
|
||||
#include "game/scripting/execution.hpp"
|
||||
#include "game/scripting/functions.hpp"
|
||||
|
||||
#include "gsc.hpp"
|
||||
|
||||
namespace io
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::string load_path()
|
||||
{
|
||||
const auto fs_basegame = game::Dvar_FindVar("fs_basegame");
|
||||
|
||||
return fs_basegame->current.string;
|
||||
}
|
||||
|
||||
std::string get_path()
|
||||
{
|
||||
static const auto path = load_path();
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
const auto path = get_path();
|
||||
std::filesystem::current_path(path);
|
||||
|
||||
gsc::function::add("fremove", [](gsc::function_args args)
|
||||
{
|
||||
const auto path = args[0].as<const char*>();
|
||||
|
||||
const auto result = std::remove(path);
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
gsc::function::add("fopen", [](gsc::function_args args)
|
||||
{
|
||||
const auto* path = args[0].as<const char*>();
|
||||
const auto* mode = args[1].as<const char*>();
|
||||
|
||||
const auto handle = fopen(path, mode);
|
||||
|
||||
if (!handle)
|
||||
{
|
||||
printf("fopen: Invalid path\n");
|
||||
}
|
||||
|
||||
return handle;
|
||||
});
|
||||
|
||||
gsc::function::add("fgetc", [](gsc::function_args args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
|
||||
const char c = fgetc(handle);
|
||||
const char str[2] = {c, '\0'};
|
||||
|
||||
return std::string(str);
|
||||
});
|
||||
|
||||
gsc::function::add("fgets", [](gsc::function_args args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
const auto n = args[1].as<int>();
|
||||
|
||||
char* buffer = (char*)calloc(n, sizeof(char));
|
||||
|
||||
fgets(buffer, n, handle);
|
||||
|
||||
const std::string result = buffer;
|
||||
|
||||
free(buffer);
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
gsc::function::add("feof", [](gsc::function_args args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
return feof(handle);
|
||||
});
|
||||
|
||||
gsc::function::add("fclose", [](gsc::function_args args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
return fclose(handle);
|
||||
});
|
||||
|
||||
gsc::function::add("fputs", [](gsc::function_args args)
|
||||
{
|
||||
const auto text = args[0].as<const char*>();
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
|
||||
return fputs(text, handle);
|
||||
});
|
||||
|
||||
gsc::function::add("fprintf", [](gsc::function_args args)
|
||||
{
|
||||
const auto text = args[0].as<const char*>();
|
||||
const auto handle = args[1].as_ptr<FILE>();
|
||||
|
||||
return fprintf(handle, text);
|
||||
});
|
||||
|
||||
gsc::function::add("fread", [](gsc::function_args args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
|
||||
fseek(handle, 0, SEEK_END);
|
||||
const auto length = ftell(handle);
|
||||
|
||||
fseek(handle, 0, SEEK_SET);
|
||||
char* buffer = (char*)calloc(length, sizeof(char));
|
||||
|
||||
fread(buffer, sizeof(char), length, handle);
|
||||
|
||||
const std::string result = buffer;
|
||||
|
||||
free(buffer);
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(io::component)
|
Loading…
x
Reference in New Issue
Block a user