Skip to content

Commit ff70ab0

Browse files
committed
Increases library resilience
1 parent 0fc5c4c commit ff70ab0

File tree

2 files changed

+162
-12
lines changed

2 files changed

+162
-12
lines changed

akgear/lib/2daHandling.tph

Lines changed: 161 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ DEFINE_DIMORPHIC_FUNCTION ~ADD_ITEMTYPE_IN_2DA~
2727
RET
2828
typeNumber
2929
BEGIN
30-
// Checks if grab sound name has more than 8 characters
30+
// Checks if grab sound name has more than 8 characters.
3131
ACTION_IF (STRING_LENGTH ~%soundGrab%~ > 8) THEN
3232
BEGIN
3333
OUTER_SET numbers = STRING_LENGTH ~%soundGrab%~
34-
FAIL ~DEFINE_DIMORPHIC_FUNCTION ADD_ITEMTYPE_IN_2DA -> The argument '%soundGrab%' for sound is %numbers% characters long. The maximum allowed length for this argument is 8 characters~
34+
FAIL ~DEFINE_DIMORPHIC_FUNCTION ADD_ITEMTYPE_IN_2DA -> The argument '%soundGrab%' for soundGrab is %numbers% characters long. The maximum allowed length for this argument is 8 characters.~
3535
END
36-
// Checks if drop sound name has more than 8 characters
36+
// Checks if drop sound name has more than 8 characters.
3737
ACTION_IF (STRING_LENGTH ~%soundDrop%~ > 8) THEN
3838
BEGIN
3939
OUTER_SET numbers = STRING_LENGTH ~%soundDrop%~
40-
FAIL ~DEFINE_DIMORPHIC_FUNCTION ADD_ITEMTYPE_IN_2DA -> The argument '%soundDrop%' for sound is %numbers% characters long. The maximum allowed length for this argument is 8 characters~
40+
FAIL ~DEFINE_DIMORPHIC_FUNCTION ADD_ITEMTYPE_IN_2DA -> The argument '%soundDrop%' for soundDrop is %numbers% characters long. The maximum allowed length for this argument is 8 characters.~
4141
END
4242
// Checks that inventorySlotType is not a negative number, because there are no negative slot numbers.
4343
ACTION_IF (~%inventorySlotType%~ < 0) THEN
@@ -55,23 +55,23 @@ BEGIN
5555
SET numCols = 4
5656
END
5757
// Reads the entire 2DA content into memory.
58-
READ_2DA_ENTRIES_NOW ~itemtypes~ ~%numCols%~
58+
READ_2DA_ENTRIES_NOW ~itemtypes_table~ ~%numCols%~
5959

60-
FOR (rowIndex = 0; rowIndex < itemtypes; ++rowIndex) BEGIN
60+
FOR (rowIndex = 0; rowIndex < itemtypes_table; ++rowIndex) BEGIN
6161
// Gets the unique identifier for the item type from the list of item types.
62-
READ_2DA_ENTRY_FORMER itemtypes rowIndex 0 itemTypeId
62+
READ_2DA_ENTRY_FORMER itemtypes_table rowIndex 0 itemTypeId
6363
// Creates an array of item type reference numbers from the first column.
6464
SPRINT $itemtypes("%rowIndex%") "%itemTypeId%"
6565
END
6666

6767
// Finds the highest item type reference number.
6868
SET ~maxTypeNumber~ = 0
69-
PHP_EACH itemtypes AS key => value BEGIN
70-
PATCH_IF (~%maxTypeNumber%~ < ~%value%~) THEN
69+
PHP_EACH itemtypes AS key => value BEGIN
70+
PATCH_IF (IS_AN_INT ~%value%~) && (~%maxTypeNumber%~ < ~%value%~) THEN
7171
BEGIN
7272
SET ~maxTypeNumber~ = ~%value%~
7373
END
74-
END
74+
END
7575

7676
// Calculates the new item type by incrementing the existing value.
7777
// This works because hex value begins by 0x00.
@@ -86,4 +86,154 @@ BEGIN
8686
APPEND ~ITEMTYPE.2DA~ "%typeNumber% %soundGrab% %soundDrop% %inventorySlotType%"
8787
BUT_ONLY // End of "COPY_EXISTING"
8888

89-
END // End of "ADD_ITEMTYPE"
89+
END // End of "ADD_ITEMTYPE_IN_2DA"
90+
91+
92+
93+
// Purpose: Creates a Reading Sequence 2DA file.
94+
// It is basically a simplified "chapter 2da" file with stricter limitations.
95+
// Refer to this forum thread for a deeper "chapter 2da" explanation:
96+
// https://www.gibberlings3.net/forums/topic/38904-what-do-the-elements-of-chapterxxx2da-mean/
97+
// 2DA File Format:
98+
// --------------------------------------------------------------
99+
// 2DA V1.0
100+
// <background image>
101+
// 0 1 2 3 ....
102+
// SWITCH DEFAULT DEFAULT DEFAULT DEFAULT ....
103+
// DEFAULT <strref><strref><strref><strref>....
104+
// --------------------------------------------------------------
105+
// Parameters:|
106+
// ============
107+
// 2daLocation -> Path to create the 2da file.
108+
// Default is the game's override folder.
109+
// 2daFilename -> Name of the 2da file.
110+
// This has to be a string value with max 8 characters.
111+
// No file extension is needed(e.g. "myRESQ00" and not "myRESQ00.2da").
112+
// bgImage -> Name of the background image.
113+
// This has to be a string value with max 8 characters.
114+
// No file extension is needed(e.g. "myBgImg" and not "myBgImg.MOS").
115+
// strrefArray -> Array containing all string references (strrefs)
116+
// in the correct order for the "DEFAULT" row.
117+
DEFINE_ACTION_FUNCTION ~CREATE_READINGSEQUENCE_2DA~
118+
STR_VAR
119+
2daLocation = "override"
120+
2daFilename = "!_NULL"
121+
bgImage = "!_NULL"
122+
strrefArray = "!_NULL"
123+
BEGIN
124+
// Check if valid 2daFilename argument is given.
125+
ACTION_IF (~%2daFilename%~ STRING_EQUAL ~!_NULL~) THEN
126+
BEGIN
127+
FAIL ~DEFINE_ACTION_FUNCTION CREATE_READINGSEQUENCE_2DA -> The correct argument for 2daFilename is missing. The argument !_NULL is forbidden.~
128+
END
129+
// Check if valid bgImage argument is given.
130+
ACTION_IF (~%bgImage%~ STRING_EQUAL ~!_NULL~) THEN
131+
BEGIN
132+
FAIL ~DEFINE_ACTION_FUNCTION CREATE_READINGSEQUENCE_2DA -> The correct argument for bgImage is missing. The argument !_NULL is forbidden.~
133+
END
134+
// Checks if file name has more than 8 characters.
135+
ACTION_IF (STRING_LENGTH ~%2daFilename%~ > 8) THEN
136+
BEGIN
137+
OUTER_SET numbers = STRING_LENGTH ~%2daFilename%~
138+
FAIL ~DEFINE_ACTION_FUNCTION CREATE_READINGSEQUENCE_2DA -> The argument '%2daFilename%' for file name is %numbers% characters long. The maximum allowed length for this argument is 8 characters.~
139+
END
140+
// Checks if file name has more than 8 characters.
141+
ACTION_IF (STRING_LENGTH ~%bgImage%~ > 8) THEN
142+
BEGIN
143+
OUTER_SET numbers = STRING_LENGTH ~%bgImage%~
144+
FAIL ~DEFINE_ACTION_FUNCTION CREATE_READINGSEQUENCE_2DA -> The argument '%bgImage%' for background image file name is %numbers% characters long. The maximum allowed length for this argument is 8 characters.~
145+
END
146+
// Checks if a proper array name is given.
147+
ACTION_IF (~%strrefArray%~ STRING_EQUAL ~!_NULL~) THEN
148+
BEGIN
149+
FAIL ~DEFINE_ACTION_FUNCTION CREATE_READINGSEQUENCE_2DA -> The correct array name as an argument is missing. The array name !_NULL is forbidden.~
150+
END
151+
152+
// Defines file inline then creates and copies file to location(see COPY)
153+
<<<<<<<<./weidu_external/workspace/!_temporaryFile.2da
154+
2DA V1.0
155+
%bgImage%>>>>>>>>
156+
157+
COPY ~./weidu_external/workspace/!_temporaryFile.2da~ ~./%2daLocation%/%2daFilename%.2da~ EVALUATE_BUFFER
158+
// Loop variables
159+
SPRINT strrefPrevious ~~
160+
SPRINT coulmnIndexPrevious ~~
161+
SPRINT switchPrevious ~~
162+
FOR (strrefIndex = 0; VARIABLE_IS_SET $~%strrefArray%~(~%strrefIndex%~) ; ++strrefIndex)
163+
BEGIN
164+
// ---Init variables
165+
SPRINT columnIndex ~%strrefIndex%~
166+
SPRINT switch ~DEFAULT~
167+
SPRINT strref $~%strrefArray%~(~%strrefIndex%~)
168+
// --- Concatenates
169+
SPRINT coulmnIndexRow ~%coulmnIndexPrevious% ~^~ %columnIndex%~
170+
SPRINT switchRow ~%switchPrevious% ~^~ %switch%~
171+
SPRINT strrefRow ~%strrefPrevious% ~^~ %strref%~
172+
// --- Prepairs previous values for next iteration
173+
SPRINT coulmnIndexPrevious ~%coulmnIndexRow%~
174+
SPRINT switchPrevious ~%switchRow%~
175+
SPRINT strrefPrevious ~%strrefRow%~
176+
END
177+
// --- Fills content
178+
APPEND ~%2daFilename%.2da~ ~%coulmnIndexRow%~
179+
APPEND ~%2daFilename%.2da~ ~SWITCH%TAB%%switchRow%~
180+
APPEND ~%2daFilename%.2da~ ~DEFAULT%TAB%%strrefRow%~
181+
182+
// --- Formating 2DA file (It is recommended to use PRETTY_PRINT_2DA after all the content of the 2DA is written into file or else inconsistent behaviour can happen.)
183+
COPY ~./%2daLocation%/%2daFilename%.2da~ ~./%2daLocation%/%2daFilename%.2da~ PRETTY_PRINT_2DA
184+
185+
END // End of "CREATE_READINGSEQUENCE_2DA"
186+
187+
188+
189+
// Purpose: Adds a conversable item(Such as Lilarcor) into the ITEMDIAL.2DA.
190+
// -------------------------------------------------------------------------
191+
// Parameters:|
192+
// ============
193+
// strref -> This is a string reference from dialog.tlk that
194+
// references to the text for the button in the item's description windows.
195+
// itemName -> This is the filename of the item with which a conversation is possible (Tag is set to Conversable).
196+
// dialogFile -> (Optional) The filename of the dialogue file for the item. If not provided,
197+
// the `itemName` will be used as the dialogue filename.
198+
DEFINE_DIMORPHIC_FUNCTION ~ADD_CONVERSABLE_ITEM_IN_2DA~
199+
INT_VAR
200+
strref = "-1"
201+
STR_VAR
202+
itemName = "!_NULL"
203+
dialogFile = "!_NULL"
204+
BEGIN
205+
// Checks if the string reference value is not a negative value.
206+
ACTION_IF (~%strref%~ < 0) THEN
207+
BEGIN
208+
FAIL ~DEFINE_DIMORPHIC_FUNCTION ADD_CONVERSABLE_ITEM_IN_2DA -> The provided value for the 'strref' parameter, '%strref%', is not a valid non-negative integer. Please ensure you provide a whole number greater than or equal to 0.~
209+
END
210+
// Check if valid itemName argument is given.
211+
ACTION_IF (~%itemName%~ STRING_EQUAL ~!_NULL~) THEN
212+
BEGIN
213+
FAIL ~DEFINE_ACTION_FUNCTION CREATE_READINGSEQUENCE_2DA -> The correct argument for itemName is missing. The argument !_NULL is forbidden.~
214+
END
215+
// Checks if item name has more than 8 characters.
216+
ACTION_IF (STRING_LENGTH ~%itemName%~ > 8) THEN
217+
BEGIN
218+
OUTER_SET numbers = STRING_LENGTH ~%itemName%~
219+
FAIL ~DEFINE_ACTION_FUNCTION CREATE_READINGSEQUENCE_2DA -> The argument '%itemName%' for itemName is %numbers% characters long. The maximum allowed length for this argument is 8 characters.~
220+
END
221+
// Checks if dialog file has more than 8 characters.
222+
ACTION_IF (STRING_LENGTH ~%dialogFile%~ > 8) THEN
223+
BEGIN
224+
OUTER_SET numbers = STRING_LENGTH ~%dialogFile%~
225+
FAIL ~DEFINE_ACTION_FUNCTION CREATE_READINGSEQUENCE_2DA -> The argument '%dialogFile%' for dialogFile is %numbers% characters long. The maximum allowed length for this argument is 8 characters.~
226+
END
227+
228+
// --- BUSINESS LOGIC
229+
ACTION_IF (~%dialogFile%~ STRING_EQUAL ~!_NULL~) THEN
230+
BEGIN
231+
// If dialogFile value is not specified, use itemName value as the dialogue file.
232+
APPEND ~ITEMDIAL.2DA~ ~%itemName% %strref% %itemName%~
233+
END ELSE
234+
BEGIN
235+
// Adds a new row to the bottom of the file with all the needed information.
236+
APPEND ~ITEMDIAL.2DA~ ~%itemName% %strref% %dialogFile%~
237+
END
238+
239+
END // End of "ADD_CONVERSABLE_ITEM_IN_2DA"

akgear/setup-akgear.tp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// -----------
55
BACKUP ~weidu_external/akgear/backup~ // %MOD_FOLDER% doesn't work here
66
AUTHOR ~Incrementis (incrementis[@]gmail.com)~
7-
VERSION ~v1.4.11~
7+
VERSION ~v1.4.12~
88
AUTO_EVAL_STRINGS// This will prevent the need of EVAL for string variables
99

1010

0 commit comments

Comments
 (0)