cuju-engine 0.3.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cuju/__init__.py +217 -0
- cuju/__main__.py +262 -0
- cuju/_batch/__init__.py +4 -0
- cuju/_batch/allocator.py +52 -0
- cuju/_batch/assets.py +68 -0
- cuju/_batch/environment.py +1890 -0
- cuju/_batch/frame_stack.py +114 -0
- cuju/_batch/library.py +190 -0
- cuju/_batch/log.py +103 -0
- cuju/_batch/memory.py +70 -0
- cuju/_batch/operators.py +55 -0
- cuju/_batch/pinned.py +39 -0
- cuju/_batch/pixels.py +207 -0
- cuju/_batch/state.py +77 -0
- cuju/_checksums.py +60 -0
- cuju/_cli_contract.py +244 -0
- cuju/_cli_parsers.py +191 -0
- cuju/_completion.py +277 -0
- cuju/_dump_to_txt.py +66 -0
- cuju/_grf_config.py +331 -0
- cuju/_library_path.py +141 -0
- cuju/_media.py +113 -0
- cuju/_pixels.py +276 -0
- cuju/_plugins.py +106 -0
- cuju/_python_players.py +224 -0
- cuju/_replay_command.py +109 -0
- cuju/_replay_player.py +42 -0
- cuju/_replays.py +414 -0
- cuju/_script_helpers.py +109 -0
- cuju/_version.py +6 -0
- cuju/action_log.py +371 -0
- cuju/check.py +596 -0
- cuju/cuju_cpu.dll +0 -0
- cuju/cuju_cuda.dll +0 -0
- cuju/cuju_render.dll +0 -0
- cuju/data/SHA256SUMS +20 -0
- cuju/data/animations.bin +0 -0
- cuju/data/assets/11_vs_11_competition_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_easy_stochastic_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_hard_stochastic_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_kaggle_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_stochastic_l0_r0.cjb +0 -0
- cuju/data/assets/1_vs_1_easy_l0_r0.cjb +0 -0
- cuju/data/assets/5_vs_5_l0_r0.cjb +0 -0
- cuju/data/assets/academy_3_vs_1_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_corner_l0_r0.cjb +0 -0
- cuju/data/assets/academy_counterattack_easy_l0_r0.cjb +0 -0
- cuju/data/assets/academy_counterattack_hard_l0_r0.cjb +0 -0
- cuju/data/assets/academy_empty_goal_close_l0_r0.cjb +0 -0
- cuju/data/assets/academy_empty_goal_l0_r0.cjb +0 -0
- cuju/data/assets/academy_pass_and_shoot_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_run_pass_and_shoot_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_run_to_score_l0_r0.cjb +0 -0
- cuju/data/assets/academy_run_to_score_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_single_goal_versus_lazy_l0_r0.cjb +0 -0
- cuju/data/assets/test_example_multiagent_l0_r0.cjb +0 -0
- cuju/data/digests.json +186 -0
- cuju/data/render_digests.json.z +0 -0
- cuju/data/scenarios/11_vs_11_competition.py +45 -0
- cuju/data/scenarios/11_vs_11_easy_stochastic.py +44 -0
- cuju/data/scenarios/11_vs_11_hard_stochastic.py +44 -0
- cuju/data/scenarios/11_vs_11_kaggle.py +46 -0
- cuju/data/scenarios/11_vs_11_stochastic.py +44 -0
- cuju/data/scenarios/1_vs_1_easy.py +25 -0
- cuju/data/scenarios/5_vs_5.py +33 -0
- cuju/data/scenarios/academy_3_vs_1_with_keeper.py +28 -0
- cuju/data/scenarios/academy_corner.py +45 -0
- cuju/data/scenarios/academy_counterattack_easy.py +45 -0
- cuju/data/scenarios/academy_counterattack_hard.py +45 -0
- cuju/data/scenarios/academy_empty_goal.py +25 -0
- cuju/data/scenarios/academy_empty_goal_close.py +25 -0
- cuju/data/scenarios/academy_pass_and_shoot_with_keeper.py +27 -0
- cuju/data/scenarios/academy_run_pass_and_shoot_with_keeper.py +27 -0
- cuju/data/scenarios/academy_run_to_score.py +30 -0
- cuju/data/scenarios/academy_run_to_score_with_keeper.py +30 -0
- cuju/data/scenarios/academy_single_goal_versus_lazy.py +49 -0
- cuju/data/scenarios/test_example_multiagent.py +28 -0
- cuju/dumps.py +942 -0
- cuju/engine_status.py +106 -0
- cuju/env.py +1168 -0
- cuju/errors.py +108 -0
- cuju/gfootball/GRF_SOURCES +86 -0
- cuju/gfootball/__init__.py +60 -0
- cuju/gfootball/_cv2.py +43 -0
- cuju/gfootball/_gym/__init__.py +88 -0
- cuju/gfootball/_gym/_vendored/GYM_SOURCES +22 -0
- cuju/gfootball/_gym/_vendored/__init__.py +43 -0
- cuju/gfootball/_gym/_vendored/core.py +361 -0
- cuju/gfootball/_gym/_vendored/envs/__init__.py +19 -0
- cuju/gfootball/_gym/_vendored/envs/registration.py +284 -0
- cuju/gfootball/_gym/_vendored/error.py +230 -0
- cuju/gfootball/_gym/_vendored/logger.py +56 -0
- cuju/gfootball/_gym/_vendored/spaces/__init__.py +30 -0
- cuju/gfootball/_gym/_vendored/spaces/box.py +171 -0
- cuju/gfootball/_gym/_vendored/spaces/dict.py +162 -0
- cuju/gfootball/_gym/_vendored/spaces/discrete.py +41 -0
- cuju/gfootball/_gym/_vendored/spaces/multi_binary.py +61 -0
- cuju/gfootball/_gym/_vendored/spaces/multi_discrete.py +77 -0
- cuju/gfootball/_gym/_vendored/spaces/space.py +94 -0
- cuju/gfootball/_gym/_vendored/spaces/tuple.py +96 -0
- cuju/gfootball/_gym/_vendored/spaces/utils.py +228 -0
- cuju/gfootball/_gym/_vendored/utils/__init__.py +13 -0
- cuju/gfootball/_gym/_vendored/utils/closer.py +72 -0
- cuju/gfootball/_gym/_vendored/utils/colorize.py +36 -0
- cuju/gfootball/_gym/_vendored/utils/ezpickle.py +37 -0
- cuju/gfootball/_gym/_vendored/utils/json_utils.py +27 -0
- cuju/gfootball/_gym/_vendored/utils/seeding.py +101 -0
- cuju/gfootball/_gym/_vendored/version.py +5 -0
- cuju/gfootball/_gym/_vendored/wrappers/__init__.py +8 -0
- cuju/gfootball/_gym/_vendored/wrappers/order_enforcing.py +20 -0
- cuju/gfootball/_gym/_vendored/wrappers/time_limit.py +31 -0
- cuju/gfootball/dump_to_txt.py +49 -0
- cuju/gfootball/dump_to_video.py +33 -0
- cuju/gfootball/env/__init__.py +271 -0
- cuju/gfootball/env/config.py +163 -0
- cuju/gfootball/env/constants.py +33 -0
- cuju/gfootball/env/controller_base.py +114 -0
- cuju/gfootball/env/event_queue.py +54 -0
- cuju/gfootball/env/football_action_set.py +220 -0
- cuju/gfootball/env/football_action_set_test.py +76 -0
- cuju/gfootball/env/football_env.py +225 -0
- cuju/gfootball/env/football_env_core.py +447 -0
- cuju/gfootball/env/football_env_test.py +692 -0
- cuju/gfootball/env/gym_test.py +47 -0
- cuju/gfootball/env/observation_preprocessing.py +94 -0
- cuju/gfootball/env/observation_processor.py +526 -0
- cuju/gfootball/env/observation_rotation.py +163 -0
- cuju/gfootball/env/observation_rotation_test.py +84 -0
- cuju/gfootball/env/player_base.py +46 -0
- cuju/gfootball/env/players/__init__.py +16 -0
- cuju/gfootball/env/players/agent.py +39 -0
- cuju/gfootball/env/players/bot.py +217 -0
- cuju/gfootball/env/players/gamepad.py +81 -0
- cuju/gfootball/env/players/keyboard.py +72 -0
- cuju/gfootball/env/players/lazy.py +31 -0
- cuju/gfootball/env/players/ppo2_cnn.py +119 -0
- cuju/gfootball/env/players/replay.py +40 -0
- cuju/gfootball/env/remote_football_env.py +189 -0
- cuju/gfootball/env/scenario_builder.py +125 -0
- cuju/gfootball/env/script_helpers.py +130 -0
- cuju/gfootball/env/script_helpers_test.py +107 -0
- cuju/gfootball/env/wrappers.py +440 -0
- cuju/gfootball/env/wrappers_test.py +150 -0
- cuju/gfootball/eval_server/__init__.py +17 -0
- cuju/gfootball/eval_server/config.py +30 -0
- cuju/gfootball/eval_server/example_client.py +107 -0
- cuju/gfootball/eval_server/proto/game_server.proto +62 -0
- cuju/gfootball/eval_server/proto/game_server_pb2.py +501 -0
- cuju/gfootball/eval_server/proto/game_server_pb2_grpc.py +113 -0
- cuju/gfootball/eval_server/proto/master.proto +25 -0
- cuju/gfootball/eval_server/proto/master_pb2.py +184 -0
- cuju/gfootball/eval_server/proto/master_pb2_grpc.py +62 -0
- cuju/gfootball/eval_server/utils.py +52 -0
- cuju/gfootball/examples/__init__.py +16 -0
- cuju/gfootball/examples/models.py +66 -0
- cuju/gfootball/examples/run_multiagent_rllib.py +140 -0
- cuju/gfootball/examples/run_ppo2.py +123 -0
- cuju/gfootball/play_game.py +73 -0
- cuju/gfootball/replay.py +43 -0
- cuju/gfootball/scenarios/11_vs_11_competition.py +59 -0
- cuju/gfootball/scenarios/11_vs_11_easy_stochastic.py +57 -0
- cuju/gfootball/scenarios/11_vs_11_hard_stochastic.py +57 -0
- cuju/gfootball/scenarios/11_vs_11_kaggle.py +60 -0
- cuju/gfootball/scenarios/11_vs_11_stochastic.py +57 -0
- cuju/gfootball/scenarios/1_vs_1_easy.py +39 -0
- cuju/gfootball/scenarios/5_vs_5.py +46 -0
- cuju/gfootball/scenarios/__init__.py +31 -0
- cuju/gfootball/scenarios/academy_3_vs_1_with_keeper.py +42 -0
- cuju/gfootball/scenarios/academy_corner.py +59 -0
- cuju/gfootball/scenarios/academy_counterattack_easy.py +59 -0
- cuju/gfootball/scenarios/academy_counterattack_hard.py +59 -0
- cuju/gfootball/scenarios/academy_empty_goal.py +39 -0
- cuju/gfootball/scenarios/academy_empty_goal_close.py +39 -0
- cuju/gfootball/scenarios/academy_pass_and_shoot_with_keeper.py +41 -0
- cuju/gfootball/scenarios/academy_run_pass_and_shoot_with_keeper.py +41 -0
- cuju/gfootball/scenarios/academy_run_to_score.py +44 -0
- cuju/gfootball/scenarios/academy_run_to_score_with_keeper.py +44 -0
- cuju/gfootball/scenarios/academy_single_goal_versus_lazy.py +63 -0
- cuju/gfootball/scenarios/test_example_multiagent.py +42 -0
- cuju/gfootball/scenarios/tests/11_vs_11_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_easy_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_hard_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_single_goal_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_single_goal_stochastic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_4_offence_deterministic.py +47 -0
- cuju/gfootball/scenarios/tests/11_vs_4_offence_stochastic.py +47 -0
- cuju/gfootball/scenarios/tests/__init__.py +31 -0
- cuju/gfootball/scenarios/tests/corner_test.py +38 -0
- cuju/gfootball/scenarios/tests/goal_test.py +35 -0
- cuju/gfootball/scenarios/tests/keeper_test.py +38 -0
- cuju/gfootball/scenarios/tests/kickoff_for_goal_loosing_team_test.py +34 -0
- cuju/gfootball/scenarios/tests/multiagent_wrapper.py +60 -0
- cuju/gfootball/scenarios/tests/offside_test.py +39 -0
- cuju/gfootball/scenarios/tests/penalty.py +34 -0
- cuju/gfootball/scenarios/tests/second_half.py +33 -0
- cuju/gfootball/scenarios/tests/symmetric.py +53 -0
- cuju/gfootball_engine/__init__.py +31 -0
- cuju/gfootball_engine/_gameplayfootball.py +1624 -0
- cuju/gfootball_engine/_native.py +193 -0
- cuju/gfootball_engine/_pickle_names.py +114 -0
- cuju/gfootball_engine/_render_hooks.py +156 -0
- cuju/kaggle.py +425 -0
- cuju/multi_device.py +401 -0
- cuju/players.py +260 -0
- cuju/raw.py +93 -0
- cuju/scenarios.py +661 -0
- cuju/state.py +211 -0
- cuju/wrappers.py +325 -0
- cuju_engine-0.3.0.dist-info/METADATA +325 -0
- cuju_engine-0.3.0.dist-info/RECORD +222 -0
- cuju_engine-0.3.0.dist-info/WHEEL +5 -0
- cuju_engine-0.3.0.dist-info/entry_points.txt +4 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSE +202 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/BSD-3-Clause.txt +31 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/BSL-1.0.txt +23 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/LGPL-2.1-or-later.txt +502 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT-Mesa.txt +26 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT-gym.txt +23 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT.txt +23 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/SunPro.txt +8 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/Zlib.txt +20 -0
- cuju_engine-0.3.0.dist-info/licenses/NOTICE +56 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# Copyright 2026 The cuju Authors
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""The engine functions of the CPU library and their C structures, loaded without PyTorch."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import ctypes
|
|
9
|
+
import os
|
|
10
|
+
import threading
|
|
11
|
+
|
|
12
|
+
from .._library_path import _check_processor, library_path
|
|
13
|
+
from ..errors import LibraryError, MissingFileError
|
|
14
|
+
|
|
15
|
+
# `CUJU_ENGINE_VERSION` of the C API.
|
|
16
|
+
VERSION = 2
|
|
17
|
+
# The players that GRF's team holds at most.
|
|
18
|
+
TEAM_PLAYERS = 11
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Entry(ctypes.Structure):
|
|
22
|
+
"""`cuju_engine_entry`, a formation entry as `ScenarioConfig` stores it."""
|
|
23
|
+
|
|
24
|
+
_fields_ = [
|
|
25
|
+
('position', ctypes.c_float * 3),
|
|
26
|
+
('start_position', ctypes.c_float * 3),
|
|
27
|
+
('role', ctypes.c_int),
|
|
28
|
+
('lazy', ctypes.c_int),
|
|
29
|
+
('controllable', ctypes.c_int),
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Scenario(ctypes.Structure):
|
|
34
|
+
"""`cuju_engine_scenario`, every field of GRF's `ScenarioConfig` and its cache."""
|
|
35
|
+
|
|
36
|
+
_fields_ = [
|
|
37
|
+
('version', ctypes.c_int),
|
|
38
|
+
('ball_position', ctypes.c_float * 3),
|
|
39
|
+
('left_team', ctypes.POINTER(Entry)),
|
|
40
|
+
('left_size', ctypes.c_int),
|
|
41
|
+
('right_team', ctypes.POINTER(Entry)),
|
|
42
|
+
('right_size', ctypes.c_int),
|
|
43
|
+
('left_agents', ctypes.c_int),
|
|
44
|
+
('right_agents', ctypes.c_int),
|
|
45
|
+
('use_magnet', ctypes.c_int),
|
|
46
|
+
('offsides', ctypes.c_int),
|
|
47
|
+
('real_time', ctypes.c_int),
|
|
48
|
+
('game_engine_random_seed', ctypes.c_uint),
|
|
49
|
+
('reverse_team_processing', ctypes.c_int),
|
|
50
|
+
('left_team_difficulty', ctypes.c_float),
|
|
51
|
+
('right_team_difficulty', ctypes.c_float),
|
|
52
|
+
('deterministic', ctypes.c_int),
|
|
53
|
+
('end_episode_on_score', ctypes.c_int),
|
|
54
|
+
('end_episode_on_possession_change', ctypes.c_int),
|
|
55
|
+
('end_episode_on_out_of_play', ctypes.c_int),
|
|
56
|
+
('game_duration', ctypes.c_int),
|
|
57
|
+
('second_half', ctypes.c_int),
|
|
58
|
+
('control_all_players', ctypes.c_int),
|
|
59
|
+
('cache_computed', ctypes.c_int),
|
|
60
|
+
('cache_dynamic_player_selection', ctypes.c_int),
|
|
61
|
+
('cache_controllable_left', ctypes.c_int),
|
|
62
|
+
('cache_controllable_right', ctypes.c_int),
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class Player(ctypes.Structure):
|
|
67
|
+
"""`cuju_engine_player`, one `PlayerInfo` with the raw vectors of GRF's `Position`."""
|
|
68
|
+
|
|
69
|
+
_fields_ = [
|
|
70
|
+
('position', ctypes.c_float * 3),
|
|
71
|
+
('direction', ctypes.c_float * 3),
|
|
72
|
+
('tired_factor', ctypes.c_float),
|
|
73
|
+
('has_card', ctypes.c_int),
|
|
74
|
+
('is_active', ctypes.c_int),
|
|
75
|
+
('designated_player', ctypes.c_int),
|
|
76
|
+
('role', ctypes.c_int),
|
|
77
|
+
]
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class Info(ctypes.Structure):
|
|
81
|
+
"""`cuju_engine_info`, GRF's `SharedInfo`."""
|
|
82
|
+
|
|
83
|
+
_fields_ = [
|
|
84
|
+
('version', ctypes.c_int),
|
|
85
|
+
('ball_position', ctypes.c_float * 3),
|
|
86
|
+
('ball_direction', ctypes.c_float * 3),
|
|
87
|
+
('ball_rotation', ctypes.c_float * 3),
|
|
88
|
+
('left_size', ctypes.c_int),
|
|
89
|
+
('right_size', ctypes.c_int),
|
|
90
|
+
('left_team', Player * TEAM_PLAYERS),
|
|
91
|
+
('right_team', Player * TEAM_PLAYERS),
|
|
92
|
+
('left_controllers', ctypes.c_int * TEAM_PLAYERS),
|
|
93
|
+
('right_controllers', ctypes.c_int * TEAM_PLAYERS),
|
|
94
|
+
('left_goals', ctypes.c_int),
|
|
95
|
+
('right_goals', ctypes.c_int),
|
|
96
|
+
('game_mode', ctypes.c_int),
|
|
97
|
+
('is_in_play', ctypes.c_int),
|
|
98
|
+
('ball_owned_team', ctypes.c_int),
|
|
99
|
+
('ball_owned_player', ctypes.c_int),
|
|
100
|
+
('step', ctypes.c_int),
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
_LOCK = threading.Lock()
|
|
105
|
+
_LIBRARY = None
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def library():
|
|
109
|
+
"""Returns the CPU library with the engine's signatures declared, loaded once."""
|
|
110
|
+
global _LIBRARY
|
|
111
|
+
with _LOCK:
|
|
112
|
+
if _LIBRARY is None:
|
|
113
|
+
_LIBRARY = _load()
|
|
114
|
+
return _LIBRARY
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _load():
|
|
118
|
+
path = library_path('cpu')
|
|
119
|
+
if not os.path.isfile(path):
|
|
120
|
+
raise MissingFileError(
|
|
121
|
+
f'expected the CPU library of cuju at {path}, which runs the engine of cuju.gfootball'
|
|
122
|
+
)
|
|
123
|
+
_check_processor(path)
|
|
124
|
+
handle = ctypes.CDLL(path)
|
|
125
|
+
pointer, integer = ctypes.c_void_p, ctypes.c_int
|
|
126
|
+
size = ctypes.c_ulonglong
|
|
127
|
+
signatures = {
|
|
128
|
+
'cuju_last_error': ([], ctypes.c_char_p),
|
|
129
|
+
'cuju_engine_version': ([], integer),
|
|
130
|
+
'cuju_engine_create': ([ctypes.c_char_p], pointer),
|
|
131
|
+
'cuju_engine_destroy': ([pointer], None),
|
|
132
|
+
'cuju_engine_start': ([pointer], integer),
|
|
133
|
+
'cuju_engine_reset': ([pointer, ctypes.POINTER(Scenario), integer, integer], integer),
|
|
134
|
+
'cuju_engine_set_config': ([pointer, ctypes.POINTER(Scenario)], integer),
|
|
135
|
+
'cuju_engine_config': ([pointer, ctypes.POINTER(Scenario)], integer),
|
|
136
|
+
'cuju_engine_config_cache': ([pointer, ctypes.POINTER(ctypes.c_int)], integer),
|
|
137
|
+
'cuju_engine_action': ([pointer, integer, integer, integer], integer),
|
|
138
|
+
'cuju_engine_sticky': ([pointer, integer, integer, integer], integer),
|
|
139
|
+
'cuju_engine_step': ([pointer, integer], integer),
|
|
140
|
+
'cuju_engine_ticks': ([pointer, integer, integer], integer),
|
|
141
|
+
'cuju_engine_finish_step': ([pointer], integer),
|
|
142
|
+
'cuju_engine_info': ([pointer, integer, ctypes.POINTER(Info)], integer),
|
|
143
|
+
'cuju_engine_game_state': ([pointer], integer),
|
|
144
|
+
'cuju_engine_set_game_state': ([pointer, integer], integer),
|
|
145
|
+
'cuju_engine_waiting_count': ([pointer], integer),
|
|
146
|
+
'cuju_engine_set_waiting_count': ([pointer, integer], integer),
|
|
147
|
+
'cuju_engine_get_state': (
|
|
148
|
+
[
|
|
149
|
+
pointer,
|
|
150
|
+
ctypes.c_char_p,
|
|
151
|
+
size,
|
|
152
|
+
ctypes.POINTER(ctypes.POINTER(ctypes.c_char)),
|
|
153
|
+
ctypes.POINTER(size),
|
|
154
|
+
],
|
|
155
|
+
integer,
|
|
156
|
+
),
|
|
157
|
+
'cuju_engine_set_state': (
|
|
158
|
+
[pointer, ctypes.c_char_p, size, ctypes.POINTER(size), ctypes.POINTER(size)],
|
|
159
|
+
integer,
|
|
160
|
+
),
|
|
161
|
+
'cuju_engine_verify': ([pointer, pointer, ctypes.POINTER(ctypes.c_longlong)], integer),
|
|
162
|
+
'cuju_engine_set_render': ([pointer, integer], integer),
|
|
163
|
+
'cuju_engine_prepare_render': ([pointer], integer),
|
|
164
|
+
'cuju_engine_render_state': (
|
|
165
|
+
[pointer, pointer, size, ctypes.POINTER(ctypes.c_uint)],
|
|
166
|
+
integer,
|
|
167
|
+
),
|
|
168
|
+
'cuju_engine_render_start': ([pointer, pointer, size], integer),
|
|
169
|
+
'cuju_engine_internal_render_state': ([pointer, size, integer], integer),
|
|
170
|
+
'cuju_engine_render_state_version': ([], ctypes.c_uint),
|
|
171
|
+
'cuju_engine_render_state_size': ([], size),
|
|
172
|
+
}
|
|
173
|
+
try:
|
|
174
|
+
for name, (arguments, result) in signatures.items():
|
|
175
|
+
function = getattr(handle, name)
|
|
176
|
+
function.argtypes = arguments
|
|
177
|
+
function.restype = result
|
|
178
|
+
except AttributeError as error:
|
|
179
|
+
raise LibraryError(
|
|
180
|
+
f'the CPU library at {path} lacks the engine of cuju.gfootball, so it is older than '
|
|
181
|
+
'this package; reinstall cuju'
|
|
182
|
+
) from error
|
|
183
|
+
found = handle.cuju_engine_version()
|
|
184
|
+
if found != VERSION:
|
|
185
|
+
raise LibraryError(
|
|
186
|
+
f'expected the engine version {VERSION} in the CPU library at {path}, found {found}'
|
|
187
|
+
)
|
|
188
|
+
return handle
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def last_error():
|
|
192
|
+
"""Returns the message of the library's last failure on this thread."""
|
|
193
|
+
return library().cuju_last_error().decode('utf-8', 'replace')
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# Copyright 2026 The cuju Authors
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""Translates the module names inside the pickle of a state between cuju and GRF, unpickling none.
|
|
5
|
+
|
|
6
|
+
A state of `cuju.gfootball` pickles GRF's classes under `cuju.gfootball`, and a state of GRF under
|
|
7
|
+
`gfootball`. The engine stores GRF's names, so states move between GRF and cuju either way. The
|
|
8
|
+
rewrite walks the opcodes, replaces the module names of `GLOBAL` and `INST` and every string that
|
|
9
|
+
is exactly such a name, and keeps the frames, whose lengths it updates. A name that is no module of
|
|
10
|
+
the port stays as it is.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import functools
|
|
16
|
+
import os
|
|
17
|
+
import pickletools
|
|
18
|
+
import struct
|
|
19
|
+
|
|
20
|
+
_PACKAGE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
21
|
+
_ENGINE = ('cuju.gfootball_engine._gameplayfootball', '_gameplayfootball')
|
|
22
|
+
_ENGINE_PACKAGE = ('cuju.gfootball_engine', 'gfootball_engine')
|
|
23
|
+
_STRINGS = ('SHORT_BINUNICODE', 'BINUNICODE', 'BINUNICODE8', 'UNICODE')
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@functools.lru_cache(maxsize=1)
|
|
27
|
+
def _names():
|
|
28
|
+
"""Returns the module names of the port and of the engine, cuju's mapped to GRF's."""
|
|
29
|
+
names = dict((_ENGINE, _ENGINE_PACKAGE))
|
|
30
|
+
root = os.path.join(_PACKAGE, 'gfootball')
|
|
31
|
+
for folder, directories, files in os.walk(root):
|
|
32
|
+
directories[:] = sorted(name for name in directories if name.isidentifier())
|
|
33
|
+
relative = os.path.relpath(folder, _PACKAGE).replace(os.sep, '.')
|
|
34
|
+
names[f'cuju.{relative}'] = relative
|
|
35
|
+
for name in files:
|
|
36
|
+
stem, extension = os.path.splitext(name)
|
|
37
|
+
if extension == '.py' and stem != '__init__' and stem.isidentifier():
|
|
38
|
+
names[f'cuju.{relative}.{stem}'] = f'{relative}.{stem}'
|
|
39
|
+
return names
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@functools.lru_cache(maxsize=1)
|
|
43
|
+
def _reverse():
|
|
44
|
+
return {grf: cuju for cuju, grf in _names().items()}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def to_grf(data: bytes) -> bytes:
|
|
48
|
+
"""Returns the pickle `data` with cuju's module names of the port turned into GRF's."""
|
|
49
|
+
return _translate(data, _names())
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def from_grf(data: bytes) -> bytes:
|
|
53
|
+
"""Returns the pickle `data` with GRF's module names turned into cuju's."""
|
|
54
|
+
return _translate(data, _reverse())
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _string(kind: str, value: str) -> bytes:
|
|
58
|
+
"""Encodes `value` with the operation `kind` that held the name, as the pickler would."""
|
|
59
|
+
if kind == 'UNICODE':
|
|
60
|
+
return b'V' + value.encode('raw-unicode-escape') + b'\n'
|
|
61
|
+
encoded = value.encode('utf-8', 'surrogatepass')
|
|
62
|
+
if kind == 'SHORT_BINUNICODE' and len(encoded) < 256:
|
|
63
|
+
return b'\x8c' + bytes([len(encoded)]) + encoded
|
|
64
|
+
if kind == 'BINUNICODE8':
|
|
65
|
+
return b'\x8d' + struct.pack('<Q', len(encoded)) + encoded
|
|
66
|
+
return b'X' + struct.pack('<I', len(encoded)) + encoded
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _translate(data: bytes, mapping: dict) -> bytes:
|
|
70
|
+
if not data:
|
|
71
|
+
return data
|
|
72
|
+
try:
|
|
73
|
+
operations = list(pickletools.genops(data))
|
|
74
|
+
except Exception:
|
|
75
|
+
# Bytes that hold no pickle reach the engine as they are, as GRF's do.
|
|
76
|
+
return data
|
|
77
|
+
if not any(_rewrites(opcode.name, argument, mapping) for opcode, argument, _ in operations):
|
|
78
|
+
return data
|
|
79
|
+
# Each piece is the bytes of one operation, and each frame holds the pieces up to its end.
|
|
80
|
+
pieces = []
|
|
81
|
+
frames = []
|
|
82
|
+
frame_end = None
|
|
83
|
+
for index, (opcode, argument, position) in enumerate(operations):
|
|
84
|
+
end = operations[index + 1][2] if index + 1 < len(operations) else len(data)
|
|
85
|
+
raw = data[position:end]
|
|
86
|
+
if frame_end is not None and position >= frame_end:
|
|
87
|
+
frame_end = None
|
|
88
|
+
if opcode.name == 'FRAME':
|
|
89
|
+
frames.append([len(pieces), 0])
|
|
90
|
+
frame_end = end + argument
|
|
91
|
+
pieces.append(None)
|
|
92
|
+
continue
|
|
93
|
+
replaced = _rewrites(opcode.name, argument, mapping)
|
|
94
|
+
if replaced is not None:
|
|
95
|
+
raw = replaced
|
|
96
|
+
if frame_end is not None:
|
|
97
|
+
frames[-1][1] += len(raw)
|
|
98
|
+
pieces.append(raw)
|
|
99
|
+
for index, size in frames:
|
|
100
|
+
pieces[index] = b'\x95' + struct.pack('<Q', size)
|
|
101
|
+
return b''.join(pieces)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _rewrites(name, argument, mapping):
|
|
105
|
+
"""Returns the new bytes of an operation that names a mapped module, or `None`."""
|
|
106
|
+
if name in ('GLOBAL', 'INST'):
|
|
107
|
+
module, _, attribute = argument.partition(' ')
|
|
108
|
+
if module in mapping:
|
|
109
|
+
code = b'c' if name == 'GLOBAL' else b'i'
|
|
110
|
+
return code + mapping[module].encode() + b'\n' + attribute.encode() + b'\n'
|
|
111
|
+
return None
|
|
112
|
+
if name in _STRINGS and isinstance(argument, str) and argument in mapping:
|
|
113
|
+
return _string(name, mapping[argument])
|
|
114
|
+
return None
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# Copyright 2026 The cuju Authors
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""Draws GRF's frames for `cuju.gfootball_engine` with cuju's software renderer.
|
|
5
|
+
|
|
6
|
+
A `GameEnv` whose `game_config.render` is on at `start_game` opens one session, which holds one
|
|
7
|
+
renderer of `cuju._pixels` and calls it where GRF's `GameEnv` touches its graphics: the steps of
|
|
8
|
+
each reset and the start of its match, every render of the match's RenderState, and every save
|
|
9
|
+
or load of a state. `frame()` returns GRF's screen bytes, `render_resolution_x *
|
|
10
|
+
render_resolution_y * 3` bytes, rows from the bottom. The renderer needs libcuju_render and GRF's
|
|
11
|
+
media, from cuju-grf-media or from the folder that `CUJU_GRF_MEDIA` names. `CUJU_RENDER_THREADS`
|
|
12
|
+
sets the threads that draw each frame, which leave every frame as it is.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import ctypes
|
|
18
|
+
import os
|
|
19
|
+
|
|
20
|
+
from .. import _media, _pixels
|
|
21
|
+
from ..errors import Error, LibraryError, MissingFileError
|
|
22
|
+
from . import _native
|
|
23
|
+
|
|
24
|
+
MESSAGE = (
|
|
25
|
+
"GRF's rendering in cuju.gfootball needs cuju's renderer, libcuju_render, and GRF's media, "
|
|
26
|
+
"the package cuju-grf-media; install them with pip install 'cuju-engine[pixels]'"
|
|
27
|
+
)
|
|
28
|
+
# GRF: `start_game` resets to `ScenarioConfig::make()`, whose seed is 42.
|
|
29
|
+
INTERNAL_SEED = 42
|
|
30
|
+
# GRF: the first rendering match of a process generates the pitch, once.
|
|
31
|
+
_pitch_generated = False
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class RendererMissingError(Error, ModuleNotFoundError):
|
|
35
|
+
"""The renderer or the media that GRF's rendering needs is not installed."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _threads():
|
|
39
|
+
value = os.environ.get('CUJU_RENDER_THREADS', '')
|
|
40
|
+
if value:
|
|
41
|
+
return max(1, int(value))
|
|
42
|
+
try:
|
|
43
|
+
available = len(os.sched_getaffinity(0))
|
|
44
|
+
except (AttributeError, OSError):
|
|
45
|
+
available = os.cpu_count() or 1
|
|
46
|
+
return max(1, min(4, available))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _media_folder():
|
|
50
|
+
folder = os.environ.get('CUJU_GRF_MEDIA')
|
|
51
|
+
if folder:
|
|
52
|
+
return folder
|
|
53
|
+
return str(_media.root())
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class Session:
|
|
57
|
+
"""The graphics of one rendering engine, as GRF's `GameEnv` drives them."""
|
|
58
|
+
|
|
59
|
+
def __init__(self, env):
|
|
60
|
+
game = env.game_config
|
|
61
|
+
try:
|
|
62
|
+
if not os.path.isfile(_pixels.library_path()):
|
|
63
|
+
raise MissingFileError(f'the renderer library {_pixels.library_path()} is missing')
|
|
64
|
+
self.renderer = _pixels.Renderer(
|
|
65
|
+
game.render_resolution_x, game.render_resolution_y, _threads(), _media_folder()
|
|
66
|
+
)
|
|
67
|
+
except (MissingFileError, LibraryError) as error:
|
|
68
|
+
raise RendererMissingError(f'{MESSAGE}: {error}') from None
|
|
69
|
+
library = _native.library()
|
|
70
|
+
self.size = library.cuju_engine_render_state_size()
|
|
71
|
+
self.state = ctypes.create_string_buffer(self.size)
|
|
72
|
+
# GRF: the loading page shows at the first reset of an engine, when no match stopped.
|
|
73
|
+
self.started = False
|
|
74
|
+
self._library = library
|
|
75
|
+
# GRF: `start_game`'s own reset builds the first match of the graphics.
|
|
76
|
+
global _pitch_generated
|
|
77
|
+
self._check(
|
|
78
|
+
library.cuju_engine_internal_render_state(
|
|
79
|
+
self.state, self.size, 0 if _pitch_generated else 1
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
_pitch_generated = True
|
|
83
|
+
self._reset_steps(True)
|
|
84
|
+
self.renderer.start_match(self.state, INTERNAL_SEED)
|
|
85
|
+
|
|
86
|
+
def _check(self, status):
|
|
87
|
+
if status != 0:
|
|
88
|
+
raise LibraryError(_native.last_error())
|
|
89
|
+
|
|
90
|
+
def _reset_steps(self, rendering):
|
|
91
|
+
"""GRF's `GameEnv::reset` up to `StartMatch`."""
|
|
92
|
+
self.renderer.reset_begin()
|
|
93
|
+
self.renderer.stop_match()
|
|
94
|
+
if not self.started:
|
|
95
|
+
self.renderer.loading_page()
|
|
96
|
+
self.started = True
|
|
97
|
+
if rendering:
|
|
98
|
+
self.renderer.render_scene(True)
|
|
99
|
+
|
|
100
|
+
def reset(self, env, animations):
|
|
101
|
+
"""GRF's `GameEnv::reset` after the engine's own reset: its graphics, then StartMatch.
|
|
102
|
+
|
|
103
|
+
GRF: with `game_config.render` off since `disable_render`, the match is built without
|
|
104
|
+
rendering, on the pitch alone, and its first render raises where GRF's process crashes.
|
|
105
|
+
"""
|
|
106
|
+
rendering = bool(env.game_config.render)
|
|
107
|
+
self._reset_steps(rendering)
|
|
108
|
+
self.renderer.set_render(rendering)
|
|
109
|
+
self._check(self._library.cuju_engine_render_start(env._engine, self.state, self.size))
|
|
110
|
+
self.renderer.start_match(self.state, env._config.game_engine_random_seed)
|
|
111
|
+
|
|
112
|
+
def render(self, env, swap_buffer):
|
|
113
|
+
"""GRF's `GameEnv::render` after `PrepareRender`."""
|
|
114
|
+
self._check(
|
|
115
|
+
self._library.cuju_engine_render_state(env._engine, self.state, self.size, None)
|
|
116
|
+
)
|
|
117
|
+
self.renderer.set_render(True)
|
|
118
|
+
self.renderer.render(self.state, swap_buffer)
|
|
119
|
+
|
|
120
|
+
def state_processed(self, env):
|
|
121
|
+
"""GRF's `GameEnv::ProcessState` of a save or a load."""
|
|
122
|
+
self.renderer.state_processed()
|
|
123
|
+
|
|
124
|
+
def frame(self):
|
|
125
|
+
"""GRF's `GameEnv::get_frame`, rows from the bottom."""
|
|
126
|
+
return self.renderer.frame(top_down=False).tobytes()
|
|
127
|
+
|
|
128
|
+
def close(self):
|
|
129
|
+
self.renderer.close()
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class _Provider:
|
|
133
|
+
"""The renderer of this package."""
|
|
134
|
+
|
|
135
|
+
@staticmethod
|
|
136
|
+
def open_session(env):
|
|
137
|
+
return Session(env)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def provider():
|
|
141
|
+
"""Returns what opens the sessions, cuju's own renderer unless a test replaces it."""
|
|
142
|
+
return _Provider
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def open_session(env):
|
|
146
|
+
"""Opens the renderer's session for `env`, which `start_game` asks for when rendering."""
|
|
147
|
+
return provider().open_session(env)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def require(session):
|
|
151
|
+
"""Returns `session`, or raises when no renderer opened one at `start_game`."""
|
|
152
|
+
if session is None:
|
|
153
|
+
raise RuntimeError(
|
|
154
|
+
'GRF renders only an engine whose game_config.render was true at start_game()'
|
|
155
|
+
)
|
|
156
|
+
return session
|