Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ _the openage authors_ are:
| Erwan Billard | iacker | 82095453+iacker à users dawt noreply dawt github dawt com |
| Deniz Kökden | denizkoekden | koekdendeniz à gmail dawt com |
| Sergey Bronnikov | ligurio | estetus à gmail dawt com |
| Nicolas Inostroza | JotaJazz | 181486793+jotajazz à users dawt noreply dawt github dawt com |

If you're a first-time committer, add yourself to the above list. This is not
just for legal reasons, but also to keep an overview of all those nicknames.
Expand Down
25 changes: 18 additions & 7 deletions libopenage/datastructure/concurrent_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ class ConcurrentQueue {
/** Returns the front item of the queue without removing it. */
T &front() {
std::unique_lock<mutex_t> lock{this->mutex};
while (this->queue.empty()) {
this->elements_available.wait(lock);
}
this->wait_for_elements(lock);
return this->queue.front();
}

/** Copies the front item in the queue and removes it from the queue. */
template <typename... None, typename U = T>
T pop([[maybe_unused]] typename std::enable_if_t<!std::is_move_constructible_v<U> and std::is_copy_constructible_v<U>> *t = nullptr) {
static_assert(sizeof...(None) == 0, "User-specified template arguments are prohibited.");
std::scoped_lock lock{this->mutex};
T ret = this->front();
std::unique_lock<mutex_t> lock{this->mutex};
this->wait_for_elements(lock);
T ret = this->queue.front();
this->queue.pop();

// Explicitly call the copy constructor since T cannot be move-constructed
Expand All @@ -63,8 +62,9 @@ class ConcurrentQueue {
template <typename... None, typename U = T>
T pop([[maybe_unused]] typename std::enable_if_t<std::is_move_constructible_v<U>> *t = nullptr) {
static_assert(sizeof...(None) == 0, "User-specified template arguments are prohibited.");
std::scoped_lock lock{this->mutex};
T ret = std::move(this->front());
std::unique_lock<mutex_t> lock{this->mutex};
this->wait_for_elements(lock);
T ret = std::move(this->queue.front());
this->queue.pop();
return ret;
}
Expand Down Expand Up @@ -98,6 +98,17 @@ class ConcurrentQueue {
}

private:
/**
* Wait until the queue has elements.
*
* The wait only releases \p lock, so it must be the only lock
* this thread holds on the recursive mutex, otherwise producers
* can't push and the wait never ends.
*/
void wait_for_elements(std::unique_lock<mutex_t> &lock) {
this->elements_available.wait(lock, [this] { return not this->queue.empty(); });
}

/** The internally used queue. */
std::queue<T> queue;

Expand Down
18 changes: 18 additions & 0 deletions libopenage/datastructure/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include "tests.h"

#include <algorithm>
#include <chrono>
#include <memory>
#include <mutex>
#include <thread>
#include <utility>

#include "testing/testing.h"
Expand Down Expand Up @@ -354,11 +356,27 @@ void concurrent_queue_copy_move_elements_compilation() {
res.reset_accumulated_construction_counter();
}

void concurrent_queue_pop_waits_for_push() {
openage::datastructure::ConcurrentQueue<int> queue;

int popped = 0;
std::thread consumer{[&] { popped = queue.pop(); }};

// the test passes either way, the delay just lets the consumer
// wait in pop() on the empty queue before the push
std::this_thread::sleep_for(std::chrono::milliseconds{10});
queue.push(157);
consumer.join();

TESTEQUALS(popped, 157);
}

// exported test
void concurrent_queue() {
concurrent_queue_copy_only_elements_compilation();
concurrent_queue_move_only_elements_compilation();
concurrent_queue_copy_move_elements_compilation();
concurrent_queue_pop_waits_for_push();
}

} // namespace openage::datastructure::tests
10 changes: 9 additions & 1 deletion libopenage/gamestate/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ void GameSimulation::start() {
this->entity_factory,
this->terrain_factory);

// the presenter may have attached its renderer while the game was not created yet
if (this->render_factory) {
this->game->attach_renderer(this->render_factory);
}

this->running = true;

log::log(MSG(info) << "Game simulation started");
Expand Down Expand Up @@ -117,7 +122,10 @@ const std::shared_ptr<gamestate::event::Commander> GameSimulation::get_commander
void GameSimulation::attach_renderer(const std::shared_ptr<renderer::RenderFactory> &render_factory) {
std::unique_lock lock{this->mutex};

this->game->attach_renderer(render_factory);
this->render_factory = render_factory;
if (this->game) {
this->game->attach_renderer(render_factory);
}
this->entity_factory->attach_renderer(render_factory);
this->terrain_factory->attach_renderer(render_factory);
}
Expand Down
10 changes: 9 additions & 1 deletion libopenage/gamestate/simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ class GameSimulation final {
/**
* Attach a renderer to the simulation.
*
* @param factory Factory for creating render entities.
* May be called before the game is started; the game gets the renderer
* attached once it is created.
*
* @param render_factory Factory for creating render entities.
*/
void attach_renderer(const std::shared_ptr<renderer::RenderFactory> &render_factory);

Expand Down Expand Up @@ -196,6 +199,11 @@ class GameSimulation final {
// TODO: The game run by the engine
std::shared_ptr<gamestate::Game> game;

/**
* Factory for creating render entities, if a renderer is attached.
*/
std::shared_ptr<renderer::RenderFactory> render_factory;

/**
* Mutex for thread-safe access to the simulation.
*/
Expand Down
2 changes: 1 addition & 1 deletion libopenage/pathfinding/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Grid::Grid(grid_id_t id,
"Grid has size " << size[0] << "x" << size[1] << " (" << size[0] * size[1] << " sectors), "
<< "but only " << this->sectors.size() << " sectors were provided");

this->sector_size = sectors.at(0)->get_cost_field()->get_size();
this->sector_size = this->sectors.at(0)->get_cost_field()->get_size();
}

grid_id_t Grid::get_id() const {
Expand Down
15 changes: 15 additions & 0 deletions libopenage/pathfinding/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include "pathfinding/cost_field.h"
#include "pathfinding/definitions.h"
#include "pathfinding/flow_field.h"
#include "pathfinding/grid.h"
#include "pathfinding/integration_field.h"
#include "pathfinding/integrator.h"
#include "pathfinding/sector.h"
#include "pathfinding/types.h"
#include "time/time.h"

Expand Down Expand Up @@ -113,6 +115,19 @@ void flow_field() {
}
}

void grid_from_sectors() {
std::vector<std::shared_ptr<Sector>> sectors;
for (size_t y = 0; y < 2; y++) {
for (size_t x = 0; x < 3; x++) {
sectors.push_back(std::make_shared<Sector>(sectors.size(), coord::chunk(x, y), 5));
}
}

Grid grid{0, util::Vector2s{3, 2}, std::move(sectors)};
TESTEQUALS(grid.get_sector_size(), 5);
TESTEQUALS(grid.get_sectors().size(), 6);
}


} // namespace tests
} // namespace path
Expand Down
2 changes: 1 addition & 1 deletion libopenage/util/fslike/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::string Python::get_native_path(const Path::subpath_t &subpath) {
return "";
}
else {
return ret.bytes();
return ret.str();
}
}

Expand Down
2 changes: 1 addition & 1 deletion libopenage/util/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Path::Path(
// test if fsobj is fslike.Directory
if (fslike::pyx_fs_is_fslike_directory.call(fsobj_in.get_ref())) {
this->fsobj = std::make_shared<fslike::Directory>(
fsobj_in.getattr("path").bytes());
fsobj_in.getattr("path").str());
}
else {
// we can't create a c++-variant of the path,
Expand Down
6 changes: 6 additions & 0 deletions libopenage/util/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/


// pxd: from libcpp cimport bool
// pxd: from libcpp.string cimport string
#include <string>
// pxd: from libcpp.vector cimport vector
Expand Down Expand Up @@ -42,6 +43,11 @@ class FSLike;
*
* FSLike *get_fsobj() except +
* const vector[string] &get_subpath() except +
*
* bool is_file() except +
* bool is_dir() except +
* vector[string] list() except +
* string resolve_native_path(const string &mode) except +
*/
class OAAPI Path {
public:
Expand Down
2 changes: 2 additions & 0 deletions openage/testing/testlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def tests_py():
)
yield ("openage.testing.misc_cpp.enum", "tests the interface for C++'s util::Enum class")
yield ("openage.util.fslike.test.test", "test the filesystem abstraction subsystem")
yield ("openage.util.fslike.cpp_tests.test", "access Python fslikes through C++ paths")
yield "openage.util.threading.test_concurrent_chain"


Expand Down Expand Up @@ -79,6 +80,7 @@ def tests_cpp():
yield "openage::job::tests::test_job_manager"
yield "openage::path::tests::path_node", "pathfinding"
yield "openage::path::tests::flow_field", "pathfinding"
yield "openage::path::tests::grid_from_sectors", "pathfinding"
yield "openage::pyinterface::tests::pyobject"
yield "openage::pyinterface::tests::err_py_to_cpp"
yield "openage::renderer::tests::font"
Expand Down
1 change: 1 addition & 0 deletions openage/util/fslike/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ add_pxds(

add_cython_modules(
cpp.pyx
cpp_tests.pyx
)
9 changes: 4 additions & 5 deletions openage/util/fslike/cpp.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2017-2021 the openage authors. See copying.md for legal info.
# cython: c_string_type=unicode, c_string_encoding=utf8

"""
Functions called from C++ to perform method calls on
Expand Down Expand Up @@ -92,10 +93,8 @@ cdef class FSLikeCPPWrapper:

def get_native_path(self, subpath):
cdef string native_path = self.fsobj.get().get_native_path(subpath)
txt = bytes(native_path)

if txt:
return txt
if not native_path.empty():
return native_path
else:
return None

Expand Down Expand Up @@ -284,7 +283,7 @@ cdef check_file_exists(object path, object fslike, const vector[string]& subpath
raise FileNotFoundError("file could not be found in filesystem %s "
"for path '%s'" % (
fslike,
b"/".join(subpath).decode(errors='ignore')
"/".join(subpath)
))


Expand Down
55 changes: 55 additions & 0 deletions openage/util/fslike/cpp_tests.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2026-2026 the openage authors. See copying.md for legal info.
# cython: c_string_type=unicode, c_string_encoding=utf8

"""
Tests for accessing Python fslike objects from C++ through cpp.pyx.
"""

import os
from tempfile import TemporaryDirectory

from cpython.ref cimport PyObject

from libopenage.pyinterface.pyobject cimport PyObj
from libopenage.util.path cimport Path as Path_cpp

from ...testing.testing import assert_value
from .directory import Directory
from .union import Union
from .wrapper import WriteBlocker


def test():
"""
Query Python fslikes through C++ paths, like the engine does with the
root union it gets from Python.
"""
cdef Path_cpp converted
cdef Path_cpp modpack_file
cdef Path_cpp blocked_file

with TemporaryDirectory() as tmp:
os.makedirs(os.path.join(tmp, "converted"))
native_file = os.path.join(tmp, "converted", "modpack.toml")
with open(native_file, "w", encoding="utf-8"):
pass

union = Union()
union.root["assets"].mount(Directory(tmp).root)

converted = Path_cpp(["assets", "converted"], PyObj(<PyObject *>union))
assert_value(converted.is_dir(), True)
assert_value(converted.is_file(), False)
assert_value(list(converted.list()), ["modpack.toml"])

# resolving through the union yields a Directory, which C++ converts
# to its native fslike implementation
modpack_file = Path_cpp(["assets", "converted", "modpack.toml"], PyObj(<PyObject *>union))
assert_value(modpack_file.is_file(), True)
assert_value(modpack_file.resolve_native_path("r"), native_file)

# the outer wrapper resolves to the inner one, which stays a Python
# fslike, so C++ asks Python for the native path
blocker = WriteBlocker(WriteBlocker(Directory(tmp).root).root)
blocked_file = Path_cpp(["converted", "modpack.toml"], PyObj(<PyObject *>blocker))
assert_value(blocked_file.resolve_native_path("r"), native_file)
Loading