Skip to content

Commit

Permalink
Expose get_filename to scripting under worldmap.settings. (#2660)
Browse files Browse the repository at this point in the history
* expose `get_filename` to scripting under `worldmap.settings`

* add missing `const`s

please tell me i put them in the right place :sadstare:

* actually add missing consts

thanks Tobbi

* one last const fix
  • Loading branch information
tylerandari13 authored Oct 20, 2023
1 parent 74f1977 commit e2f966f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/scripting/worldmap_sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ WorldMapSector::move_to_spawnpoint(const std::string& spawnpoint)
m_parent->move_to_spawnpoint(spawnpoint);
}

std::string
WorldMapSector::get_filename() const
{
SCRIPT_GUARD_WORLDMAP;
return worldmap.get_filename();
}

} // namespace scripting

/* EOF */
6 changes: 6 additions & 0 deletions src/scripting/worldmap_sector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class WorldMapSector final : public GameObjectManager
* @param string $spawnpoint
*/
void move_to_spawnpoint(const std::string& spawnpoint);

/**
* Gets the path to the worldmap file. Useful for saving worldmap specific data.
*/
std::string get_filename() const;

};

} // namespace scripting
Expand Down
34 changes: 34 additions & 0 deletions src/scripting/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11512,6 +11512,33 @@ static SQInteger WorldMapSector_move_to_spawnpoint_wrapper(HSQUIRRELVM vm)

}

static SQInteger WorldMapSector_get_filename_wrapper(HSQUIRRELVM vm)
{
SQUserPointer data;
if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, nullptr, SQTrue)) || !data) {
sq_throwerror(vm, _SC("'get_filename' called without instance"));
return SQ_ERROR;
}
scripting::WorldMapSector* _this = reinterpret_cast<scripting::WorldMapSector*> (data);


try {
std::string return_value = _this->get_filename();

assert(return_value.size() < static_cast<size_t>(std::numeric_limits<SQInteger>::max()));
sq_pushstring(vm, return_value.c_str(), static_cast<SQInteger>(return_value.size()));
return 1;

} catch(std::exception& e) {
sq_throwerror(vm, e.what());
return SQ_ERROR;
} catch(...) {
sq_throwerror(vm, _SC("Unexpected exception while executing function 'get_filename'"));
return SQ_ERROR;
}

}

static SQInteger display_wrapper(HSQUIRRELVM vm)
{
return scripting::display(vm);
Expand Down Expand Up @@ -15661,6 +15688,13 @@ void register_supertux_wrapper(HSQUIRRELVM v)
throw SquirrelError(v, "Couldn't register function 'move_to_spawnpoint'");
}

sq_pushstring(v, "get_filename", -1);
sq_newclosure(v, &WorldMapSector_get_filename_wrapper, 0);
sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, ".");
if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register function 'get_filename'");
}

if(SQ_FAILED(sq_createslot(v, -3))) {
throw SquirrelError(v, "Couldn't register class 'WorldMapSector'");
}
Expand Down
6 changes: 6 additions & 0 deletions src/worldmap/worldmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ WorldMap::set_sector(const std::string& name, const std::string& spawnpoint,
m_sector->move_to_spawnpoint(spawnpoint);
}

std::string
WorldMap::get_filename() const
{
return m_map_filename;
}

} // namespace worldmap

/* EOF */
2 changes: 2 additions & 0 deletions src/worldmap/worldmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class WorldMap final : public Currenton<WorldMap>
void set_sector(const std::string& name, const std::string& spawnpoint = "",
bool perform_full_setup = true);

std::string get_filename() const;

private:
void on_escape_press();

Expand Down

0 comments on commit e2f966f

Please sign in to comment.