|
1 | | -from bink.Choices import Choices |
2 | | -from bink.Tags import Tags |
3 | | -from bink.loadlib import lib, BINK_OK |
| 1 | +# pylint: disable=E1101, C0116 |
| 2 | + |
| 3 | +"""Handle Ink Story.""" |
4 | 4 | import ctypes |
| 5 | +from bink.choices import Choices |
| 6 | +from bink.tags import Tags |
| 7 | +from bink.loadlib import LIB, BINK_OK |
5 | 8 |
|
6 | | -class Story: |
| 9 | +class Story: |
| 10 | + """Story is the entry point of the Blade Ink lib.""" |
7 | 11 | def __init__(self, story_string: str): |
8 | 12 | err_msg = ctypes.c_char_p() |
9 | 13 | story = ctypes.c_void_p() |
10 | | - ret = lib.bink_story_new(ctypes.byref(story), story_string.encode('utf-8'), ctypes.byref(err_msg)) |
| 14 | + ret = LIB.bink_story_new( |
| 15 | + ctypes.byref(story), |
| 16 | + story_string.encode('utf-8'), |
| 17 | + ctypes.byref(err_msg)) |
11 | 18 | if ret != BINK_OK: |
12 | 19 | err = err_msg.value.decode('utf-8') |
13 | | - lib.bink_cstring_free(err_msg) |
| 20 | + LIB.bink_cstring_free(err_msg) |
14 | 21 | raise RuntimeError(err) |
15 | | - |
| 22 | + |
16 | 23 | self._story = story |
17 | 24 |
|
18 | 25 | def can_continue(self): |
19 | 26 | can_continue = ctypes.c_bool() |
20 | | - ret = lib.bink_story_can_continue(self._story, ctypes.byref(can_continue)) |
| 27 | + ret = LIB.bink_story_can_continue( |
| 28 | + self._story, ctypes.byref(can_continue)) |
21 | 29 |
|
22 | 30 | if ret != BINK_OK: |
23 | 31 | raise RuntimeError("Error in can_continue") |
24 | 32 |
|
25 | 33 | return can_continue.value |
26 | | - |
| 34 | + |
27 | 35 | def cont(self) -> str: |
28 | 36 | err_msg = ctypes.c_char_p() |
29 | 37 | line = ctypes.c_char_p() |
30 | | - ret = lib.bink_story_cont(self._story, ctypes.byref(line), ctypes.byref(err_msg)) |
| 38 | + ret = LIB.bink_story_cont( |
| 39 | + self._story, |
| 40 | + ctypes.byref(line), |
| 41 | + ctypes.byref(err_msg)) |
31 | 42 |
|
32 | 43 | if ret != BINK_OK: |
33 | 44 | err = err_msg.value.decode('utf-8') |
34 | | - lib.bink_cstring_free(err_msg) |
| 45 | + LIB.bink_cstring_free(err_msg) |
35 | 46 | raise RuntimeError(err) |
36 | 47 |
|
37 | 48 | result = line.value.decode('utf-8') |
38 | | - lib.bink_cstring_free(line) |
| 49 | + LIB.bink_cstring_free(line) |
39 | 50 |
|
40 | 51 | return result |
41 | | - |
| 52 | + |
42 | 53 | def continue_maximally(self) -> str: |
43 | 54 | err_msg = ctypes.c_char_p() |
44 | 55 | line = ctypes.c_char_p() |
45 | | - ret = lib.bink_story_continue_maximally(self._story, ctypes.byref(line), ctypes.byref(err_msg)) |
| 56 | + ret = LIB.bink_story_continue_maximally( |
| 57 | + self._story, ctypes.byref(line), ctypes.byref(err_msg)) |
46 | 58 |
|
47 | 59 | if ret != BINK_OK: |
48 | 60 | err = err_msg.value.decode('utf-8') |
49 | | - lib.bink_cstring_free(err_msg) |
| 61 | + LIB.bink_cstring_free(err_msg) |
50 | 62 | raise RuntimeError(err) |
51 | 63 |
|
52 | 64 | result = line.value.decode('utf-8') |
53 | | - lib.bink_cstring_free(line) |
| 65 | + LIB.bink_cstring_free(line) |
54 | 66 |
|
55 | 67 | return result |
56 | | - |
| 68 | + |
57 | 69 | def get_current_choices(self) -> Choices: |
58 | 70 | choices = ctypes.c_void_p() |
59 | | - len = ctypes.c_int() |
60 | | - ret = lib.bink_story_get_current_choices(self._story, ctypes.byref(choices), ctypes.byref(len)) |
| 71 | + choice_count = ctypes.c_int() |
| 72 | + ret = LIB.bink_story_get_current_choices( |
| 73 | + self._story, ctypes.byref(choices), ctypes.byref(choice_count)) |
61 | 74 |
|
62 | 75 | if ret != BINK_OK: |
63 | 76 | raise RuntimeError("Error getting current choices") |
64 | 77 |
|
65 | | - choices = Choices(choices, len.value) |
| 78 | + choices = Choices(choices, choice_count.value) |
66 | 79 |
|
67 | 80 | return choices |
68 | | - |
| 81 | + |
69 | 82 | def choose_choice_index(self, choice_index: int): |
| 83 | + """Chooses the `Choice` from the |
| 84 | + `currentChoices` list with the given index. Internally, this |
| 85 | + sets the current content path to what the |
| 86 | + `Choice` points to, ready to continue story evaluation.""" |
70 | 87 | err_msg = ctypes.c_char_p() |
71 | 88 | cidx = ctypes.c_int(choice_index) |
72 | | - ret = lib.bink_story_choose_choice_index(self._story, cidx, ctypes.byref(err_msg)) |
| 89 | + ret = LIB.bink_story_choose_choice_index( |
| 90 | + self._story, cidx, ctypes.byref(err_msg)) |
73 | 91 |
|
74 | 92 | if ret != BINK_OK: |
75 | 93 | err = err_msg.value.decode('utf-8') |
76 | | - lib.bink_cstring_free(err_msg) |
| 94 | + LIB.bink_cstring_free(err_msg) |
77 | 95 | raise RuntimeError(err) |
78 | | - |
| 96 | + |
79 | 97 | def get_current_tags(self) -> Tags: |
80 | 98 | tags = ctypes.c_void_p() |
81 | | - len = ctypes.c_int() |
82 | | - ret = lib.bink_story_get_current_tags(self._story, ctypes.byref(tags), ctypes.byref(len)) |
| 99 | + tag_count = ctypes.c_int() |
| 100 | + ret = LIB.bink_story_get_current_tags( |
| 101 | + self._story, ctypes.byref(tags), ctypes.byref(tag_count)) |
83 | 102 |
|
84 | 103 | if ret != BINK_OK: |
85 | 104 | raise RuntimeError("Error getting current tags") |
86 | 105 |
|
87 | | - tags = Tags(tags, len.value) |
| 106 | + tags = Tags(tags, tag_count.value) |
88 | 107 |
|
89 | 108 | return tags |
90 | | - |
91 | | - def choose_path_string(self, path : str): |
| 109 | + |
| 110 | + def choose_path_string(self, path: str): |
92 | 111 | err_msg = ctypes.c_char_p() |
93 | 112 | story = ctypes.c_void_p() |
94 | | - ret = lib.bink_story_new(ctypes.byref(story), path.encode('utf-8'), ctypes.byref(err_msg)) |
| 113 | + ret = LIB.bink_story_new( |
| 114 | + ctypes.byref(story), |
| 115 | + path.encode('utf-8'), |
| 116 | + ctypes.byref(err_msg)) |
95 | 117 | if ret != BINK_OK: |
96 | 118 | err = err_msg.value.decode('utf-8') |
97 | | - lib.bink_cstring_free(err_msg) |
| 119 | + LIB.bink_cstring_free(err_msg) |
98 | 120 | raise RuntimeError(err) |
99 | | - |
| 121 | + |
100 | 122 | def __del__(self): |
101 | | - lib.bink_story_free(self._story) |
| 123 | + LIB.bink_story_free(self._story) |
| 124 | + |
102 | 125 |
|
103 | | -def from_file(story_file: str): |
104 | | - with open(story_file, 'r') as file: |
| 126 | +def story_from_file(story_file: str): |
| 127 | + with open(story_file, 'r', encoding='utf-8') as file: |
105 | 128 | content = file.read() |
106 | 129 | return Story(content) |
0 commit comments