Merge branch 'master' into jgrpp
# Conflicts: # .github/workflows/ci-build.yml # .github/workflows/release-linux.yml # .github/workflows/release-macos.yml # .github/workflows/release-source.yml # .github/workflows/release.yml # CMakeLists.txt # COMPILING.md # src/ai/ai_core.cpp # src/ai/ai_gui.cpp # src/bridge_gui.cpp # src/company_gui.cpp # src/console_cmds.cpp # src/core/CMakeLists.txt # src/core/smallmap_type.hpp # src/disaster_vehicle.h # src/effectvehicle_base.h # src/fontcache.cpp # src/game/game_core.cpp # src/game/game_gui.cpp # src/gamelog.cpp # src/gamelog_internal.h # src/group_gui.cpp # src/linkgraph/linkgraph.h # src/misc.cpp # src/network/core/config.h # src/network/core/udp.cpp # src/network/network_chat_gui.cpp # src/network/network_content_gui.cpp # src/network/network_gui.cpp # src/newgrf.cpp # src/newgrf_gui.cpp # src/newgrf_profiling.cpp # src/newgrf_profiling.h # src/object_gui.cpp # src/openttd.cpp # src/openttd.h # src/order_gui.cpp # src/os/windows/font_win32.cpp # src/rail_gui.cpp # src/road.cpp # src/road_gui.cpp # src/saveload/afterload.cpp # src/saveload/saveload.h # src/script/api/script_controller.cpp # src/script/api/script_roadtypelist.cpp # src/script/script_config.cpp # src/script/script_config.hpp # src/script/script_instance.cpp # src/script/script_scanner.cpp # src/script/squirrel.cpp # src/script/squirrel_helper.hpp # src/settings_gui.cpp # src/settings_internal.h # src/settings_type.h # src/table/settings/network_private_settings.ini # src/timetable_gui.cpp # src/vehicle.cpp # src/vehicle_base.h # src/window_gui.h
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "../string_func.h"
|
||||
#include "../tile_type.h"
|
||||
#include "squirrel_helper_type.hpp"
|
||||
#include <optional>
|
||||
|
||||
template <class CL, ScriptType ST> const char *GetClassName();
|
||||
|
||||
@@ -23,19 +24,6 @@ template <class CL, ScriptType ST> const char *GetClassName();
|
||||
* The Squirrel convert routines
|
||||
*/
|
||||
namespace SQConvert {
|
||||
/**
|
||||
* Pointers assigned to this class will be free'd when this instance
|
||||
* comes out of scope. Useful to make sure you can use stredup(),
|
||||
* without leaking memory.
|
||||
*/
|
||||
struct SQAutoFreePointers : std::vector<void *> {
|
||||
~SQAutoFreePointers()
|
||||
{
|
||||
for (void * p : *this) free(p);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* To return a value to squirrel, we use this helper class. It converts to the right format.
|
||||
* We use a class instead of a plain function to allow us to use partial template specializations.
|
||||
@@ -52,48 +40,58 @@ namespace SQConvert {
|
||||
template <> struct Return<Money> { static inline int Set(HSQUIRRELVM vm, Money res) { sq_pushinteger(vm, res); return 1; } };
|
||||
//template <> struct Return<TileIndex> { static inline int Set(HSQUIRRELVM vm, TileIndex res) { sq_pushinteger(vm, (int32)res.value); return 1; } };
|
||||
template <> struct Return<bool> { static inline int Set(HSQUIRRELVM vm, bool res) { sq_pushbool (vm, res); return 1; } };
|
||||
template <> struct Return<char *> { static inline int Set(HSQUIRRELVM vm, char *res) { if (res == nullptr) sq_pushnull(vm); else { sq_pushstring(vm, res, -1); free(res); } return 1; } };
|
||||
template <> struct Return<const char *> { static inline int Set(HSQUIRRELVM vm, const char *res) { if (res == nullptr) sq_pushnull(vm); else { sq_pushstring(vm, res, -1); } return 1; } };
|
||||
template <> struct Return<char *> { /* Do not use char *, use std::optional<std::string> instead. */ };
|
||||
template <> struct Return<const char *> { /* Do not use const char *, use std::optional<std::string> instead. */ };
|
||||
template <> struct Return<void *> { static inline int Set(HSQUIRRELVM vm, void *res) { sq_pushuserpointer(vm, res); return 1; } };
|
||||
template <> struct Return<HSQOBJECT> { static inline int Set(HSQUIRRELVM vm, HSQOBJECT res) { sq_pushobject(vm, res); return 1; } };
|
||||
|
||||
template <> struct Return<std::optional<std::string>> {
|
||||
static inline int Set(HSQUIRRELVM vm, std::optional<std::string> res) {
|
||||
if (res.has_value()) {
|
||||
sq_pushstring(vm, res.value(), -1);
|
||||
} else {
|
||||
sq_pushnull(vm);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* To get a param from squirrel, we use this helper class. It converts to the right format.
|
||||
* We use a class instead of a plain function to allow us to use partial template specializations.
|
||||
*/
|
||||
template <typename T> struct Param;
|
||||
|
||||
template <> struct Param<uint8> { static inline uint8 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<uint16> { static inline uint16 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<uint32> { static inline uint32 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<int8> { static inline int8 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<int16> { static inline int16 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<int32> { static inline int32 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<int64> { static inline int64 Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
//template <> struct Param<TileIndex> { static inline TileIndex Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return TileIndex((uint32)(int32)tmp); } };
|
||||
template <> struct Param<Money> { static inline Money Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<bool> { static inline bool Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; } };
|
||||
template <> struct Param<void *> { static inline void *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<uint8> { static inline uint8 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<uint16> { static inline uint16 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<uint32> { static inline uint32 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<int8> { static inline int8 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<int16> { static inline int16 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<int32> { static inline int32 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<int64> { static inline int64 Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
//template <> struct Param<TileIndex> { static inline TileIndex Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return TileIndex((uint32)(int32)tmp); } };
|
||||
template <> struct Param<Money> { static inline Money Get(HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
|
||||
template <> struct Param<bool> { static inline bool Get(HSQUIRRELVM vm, int index) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; } };
|
||||
template <> struct Param<const char *> { /* Do not use const char *, use std::string& instead. */ };
|
||||
template <> struct Param<void *> { static inline void *Get(HSQUIRRELVM vm, int index) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } };
|
||||
|
||||
template <> struct Param<const char *> {
|
||||
static inline const char *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
|
||||
template <> struct Param<const std::string &> {
|
||||
static inline const std::string Get(HSQUIRRELVM vm, int index)
|
||||
{
|
||||
/* Convert what-ever there is as parameter to a string */
|
||||
sq_tostring(vm, index);
|
||||
|
||||
const SQChar *tmp;
|
||||
sq_getstring(vm, -1, &tmp);
|
||||
char *tmp_str = stredup(tmp);
|
||||
std::string result = StrMakeValid(tmp);
|
||||
sq_poptop(vm);
|
||||
ptr->push_back((void *)tmp_str);
|
||||
StrMakeValidInPlace(tmp_str);
|
||||
return tmp_str;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Titem>
|
||||
struct Param<Array<Titem>> {
|
||||
static inline Array<Titem> Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
|
||||
struct Param<Array<Titem> &&> {
|
||||
static inline Array<Titem> Get(HSQUIRRELVM vm, int index)
|
||||
{
|
||||
/* Sanity check of the size. */
|
||||
if (sq_getsize(vm, index) > UINT16_MAX) throw sq_throwerror(vm, "an array used as parameter to a function is too large");
|
||||
@@ -106,7 +104,7 @@ namespace SQConvert {
|
||||
Array<Titem> data;
|
||||
|
||||
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
|
||||
data.emplace_back(Param<Titem>::Get(vm, -1, ptr));
|
||||
data.emplace_back(Param<Titem>::Get(vm, -1));
|
||||
sq_pop(vm, 2);
|
||||
}
|
||||
sq_pop(vm, 2);
|
||||
@@ -136,15 +134,14 @@ namespace SQConvert {
|
||||
template <size_t... i>
|
||||
static int SQCall(void *instance, Tretval(*func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
|
||||
{
|
||||
[[maybe_unused]] SQAutoFreePointers ptr;
|
||||
if constexpr (std::is_void_v<Tretval>) {
|
||||
(*func)(
|
||||
Param<Targs>::Get(vm, 2 + i, &ptr)...
|
||||
Param<Targs>::Get(vm, 2 + i)...
|
||||
);
|
||||
return 0;
|
||||
} else {
|
||||
Tretval ret = (*func)(
|
||||
Param<Targs>::Get(vm, 2 + i, &ptr)...
|
||||
Param<Targs>::Get(vm, 2 + i)...
|
||||
);
|
||||
return Return<Tretval>::Set(vm, ret);
|
||||
}
|
||||
@@ -170,15 +167,14 @@ namespace SQConvert {
|
||||
template <size_t... i>
|
||||
static int SQCall(Tcls *instance, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
|
||||
{
|
||||
[[maybe_unused]] SQAutoFreePointers ptr;
|
||||
if constexpr (std::is_void_v<Tretval>) {
|
||||
(instance->*func)(
|
||||
Param<Targs>::Get(vm, 2 + i, &ptr)...
|
||||
Param<Targs>::Get(vm, 2 + i)...
|
||||
);
|
||||
return 0;
|
||||
} else {
|
||||
Tretval ret = (instance->*func)(
|
||||
Param<Targs>::Get(vm, 2 + i, &ptr)...
|
||||
Param<Targs>::Get(vm, 2 + i)...
|
||||
);
|
||||
return Return<Tretval>::Set(vm, ret);
|
||||
}
|
||||
@@ -187,9 +183,8 @@ namespace SQConvert {
|
||||
template <size_t... i>
|
||||
static Tcls *SQConstruct(Tcls *, Tretval(Tcls:: *func)(Targs...), [[maybe_unused]] HSQUIRRELVM vm, std::index_sequence<i...>)
|
||||
{
|
||||
[[maybe_unused]] SQAutoFreePointers ptr;
|
||||
Tcls *inst = new Tcls(
|
||||
Param<Targs>::Get(vm, 2 + i, &ptr)...
|
||||
Param<Targs>::Get(vm, 2 + i)...
|
||||
);
|
||||
|
||||
return inst;
|
||||
|
Reference in New Issue
Block a user