LibT2FS 0.1
C API for accessing TEngine data in memory.
Loading...
Searching...
No Matches
t2fs.h
Go to the documentation of this file.
1/* This file is part of LibT2FS.
2 *
3 * LibT2FS is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * LibT2FS is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17#ifndef H_T2FS_TUROK2_FILESYSTEM
18#define H_T2FS_TUROK2_FILESYSTEM
19
20#include <stdint.h>
21#include <stdio.h>
22
23#include "config.h"
24#include "schema.h"
25#include "rnc.h"
26
37
38#define T2FS_NODE_SANE_MAX_COUNT 65000
39#define T2FS_NODE_SANE_MIN_OFFSET 0x10
40
41#define T2FS_PRINT_ERROR_NODE(node, ...) t2fs_print_error_node(T2FS_PRINT_FLAG_ERROR, __func__, node, __VA_ARGS__)
42#define T2FS_PRINT_WARN_NODE(node, ...) t2fs_print_error_node(T2FS_PRINT_FLAG_WARNING, __func__, node, __VA_ARGS__)
43#define T2FS_PRINT_DEBUG_NODE(node, ...) t2fs_print_error_node(T2FS_PRINT_FLAG_DEBUG, __func__, node, __VA_ARGS__)
44
45
51
52
53typedef struct s_t2fs t_t2fs;
54typedef struct s_t2fsInput t2fsInput;
55typedef size_t (t2fs_read_callback)(t2fsInput *input, void *dest, size_t size);
56typedef size_t (t2fs_read_value_callback)(t2fsInput *input, void *dest, t2fsReadSize size);
57typedef int (t2fs_seek_callback)(t2fsInput *input, long offset, int whence);
58
61typedef struct s_t2fsInput {
64 FILE *fp;
65
69
70 // Some node(s) are compressed with RNC, we need to
71 // decompress the data read from file first before we can process it.
72 // buf is the decompressed buffer.
75 uint8_t *buf;
78 uint32_t bufSize;
81 uint32_t bufOffset;
82
87
92
101} t2fsInput;
102
103
104/**********************************************************************
105 **********************************************************************
106 **********************************************************************/
107
108
109typedef struct s_t2fs t_t2fs;
110typedef struct s_t2fsNode t2fsNode;
111
114typedef struct s_t2fsNode {
116
119 uint32_t id;
120
128 uint32_t offset;
129
136 uint32_t size;
137
147 uint8_t *data;
148
156
162
163 // when this is a branch node
166 uint32_t childCount;
167
173
177
180 uint8_t hasBeenRead;
181
185} t2fsNode;
186
187
194
195
201void
203
204
209int
211
212
217int
219
220
226int
227t2fs_node_read_recurse(t_t2fs *lib, t2fsNode *node, uint8_t loadData);
228
229
234uint8_t
235t2fs_node_has_index(const t2fsNode *const node, uint32_t index);
236
237
242t2fsNode *
243t2fs_node_find_by_name(t2fsNode *start, const char *name, uint32_t level, uint32_t maxLevel);
244
245
246void
247t2fs_node_print_recursive(const t2fsNode *const node, uint32_t indent);
248
249int
250t2fs_node_guess_read_recursive(t_t2fs *lib, t2fsNode *node, uint8_t loadData, uint8_t decompress);
251
252
253#define NODE_MAX_LEVEL 64
254#define NODE_PATH_END (uint32_t)-1
255typedef struct s_t2fsNodePath {
257 uint32_t count;
259
260void
262
263void
265
266void
267t2fs_node_path_print(const t_t2fs *const lib, const t2fsNodePath *const np);
268
269int
270t2fs_node_path_to_string(const t2fsNodePath *const np, char *buf, size_t bufsize);
271
272void
273t2fs_print_error_node(int printType, const char *const func, t2fsNode *node, const char *const fmt, ...);
274
275
276/**********************************************************************
277 **********************************************************************
278 **********************************************************************/
279
280
281typedef enum {
284} t2fsStatus;
285
286
317
318
321void
323
342 const char *const filepath,
343 const char *const schemaPath,
344 uint8_t searchSchema);
345
346
349void
351
352#endif
struct s_T2FSFileConfig T2FSFileConfig
struct s_rnc t_rnc
struct s_t2fsSchemaNode t2fsSchemaNode
Definition schema.h:28
object to handle input reading
Definition t2fs.h:61
t2fs_read_callback * read
The function used to read, this will depend on whether we are reading from file or from a decompresse...
Definition t2fs.h:86
long fileSize
Total filesize.
Definition t2fs.h:68
uint8_t * buf
Decompressed buffer.
Definition t2fs.h:75
t2fs_seek_callback * seek
The function used to seek, this will depend on whether we are reading from file or from a decompresse...
Definition t2fs.h:91
FILE * fp
For reading data from file.
Definition t2fs.h:64
uint32_t bufSize
Decompressed buffer size.
Definition t2fs.h:78
uint32_t bufOffset
Current offset mimicking FILE.
Definition t2fs.h:81
t2fs_read_value_callback * readValue
Special function to read values for standard types, use case for this is when we handle BE files,...
Definition t2fs.h:100
Definition t2fs.h:255
uint32_t count
Definition t2fs.h:257
uint32_t segs[NODE_MAX_LEVEL]
Definition t2fs.h:256
This is the core object to describe a path inside the tree.
Definition t2fs.h:114
t2fsSchemaNode * schema
Definition t2fs.h:115
uint8_t isCompressed
Indicator that the data is compressed.
Definition t2fs.h:176
uint8_t hasBeenRead
Indicator that the data for this node has been read.
Definition t2fs.h:180
t2fsNode * childNodes
Pointer to the child node(s).
Definition t2fs.h:172
t2fsInput * input
For reading data.
Definition t2fs.h:155
uint8_t * data
Data of this node, can be another offset table (branch node) or data (data/leaf node) depending on sc...
Definition t2fs.h:147
t_t2fs * lib
Pointer to our context for convenience.
Definition t2fs.h:184
uint32_t size
Data size of this node.
Definition t2fs.h:136
uint32_t offset
Offset to this node.
Definition t2fs.h:128
uint32_t id
Index of this node.
Definition t2fs.h:119
uint32_t childCount
How many child nodes this node has.
Definition t2fs.h:166
t2fsNode * parent
The parent node.
Definition t2fs.h:161
This is the core LibT2FS object that you will be passing around.
Definition t2fs.h:289
t_rnc rnc
RNC decompression context.
Definition t2fs.h:308
t2fsNode * spawnpoints
Definition t2fs.h:314
t2fsNode * levels
Definition t2fs.h:313
T2FSFileConfig config
We might need different config for different files, these configs are pre-defined and contain info li...
Definition t2fs.h:296
t2fsNode * models
Definition t2fs.h:311
t2fsNode * textures
Definition t2fs.h:312
t2fsNode * scenes
Definition t2fs.h:315
t2fsInput input
File input reading.
Definition t2fs.h:300
t2fsNode root
Root node.
Definition t2fs.h:304
enum e_t2fsReadSize t2fsReadSize
To handle swapping endianness.
int t2fs_node_guess_read_recursive(t_t2fs *lib, t2fsNode *node, uint8_t loadData, uint8_t decompress)
void t2fs_node_init(t2fsNode *node)
int t2fs_seek_callback(t2fsInput *input, long offset, int whence)
Definition t2fs.h:57
e_t2fsReadSize
To handle swapping endianness.
Definition t2fs.h:47
@ T2FS_READ_16
Definition t2fs.h:48
@ T2FS_READ_32
Definition t2fs.h:49
#define NODE_MAX_LEVEL
Definition t2fs.h:253
void t2fs_print_error_node(int printType, const char *const func, t2fsNode *node, const char *const fmt,...)
int t2fs_node_read_data(t2fsNode *node)
Read node data.
void t2fs_close(t_t2fs *lib)
Close opened file and free all allocated node/data memory.
void t2fs_node_free_childeren(t2fsNode *node)
This should be called after successfully call to t2fs_node_read_branch().
int t2fs_node_read_recurse(t_t2fs *lib, t2fsNode *node, uint8_t loadData)
Recursive read all child branch node(s) and optionally load the endpoint(s) data.
int t2fs_node_path_to_string(const t2fsNodePath *const np, char *buf, size_t bufsize)
t2fsNode * t2fs_node_find_by_name(t2fsNode *start, const char *name, uint32_t level, uint32_t maxLevel)
Find child node by schema name.
void t2fs_init(t_t2fs *lib)
Always initialize your t_t2fs object.
size_t t2fs_read_callback(t2fsInput *input, void *dest, size_t size)
Definition t2fs.h:55
size_t t2fs_read_value_callback(t2fsInput *input, void *dest, t2fsReadSize size)
Definition t2fs.h:56
void t2fs_node_print_recursive(const t2fsNode *const node, uint32_t indent)
struct s_t2fsInput t2fsInput
object to handle input reading
Definition t2fs.h:54
uint8_t t2fs_node_has_index(const t2fsNode *const node, uint32_t index)
Check if the node has a child node with the given index.
void t2fs_node_path_parse(t2fsNodePath *np, t2fsNode *node)
t2fsStatus
Definition t2fs.h:281
@ T2FS_OK
Definition t2fs.h:282
@ T2FS_ERROR
Definition t2fs.h:283
t2fsStatus t2fs_open(t_t2fs *lib, const char *const filepath, const char *const schemaPath, uint8_t searchSchema)
Open .dat/.lss/.lsm file.
struct s_t2fsNode t2fsNode
This is the core object to describe a path inside the tree.
Definition t2fs.h:110
void t2fs_node_path_print(const t_t2fs *const lib, const t2fsNodePath *const np)
struct s_t2fsNodePath t2fsNodePath
void t2fs_node_path_init(t2fsNodePath *np)
struct s_t2fs t_t2fs
This is the core LibT2FS object that you will be passing around.
Definition t2fs.h:53
int t2fs_node_read_branch(t_t2fs *lib, t2fsNode *node)
Read child branch node(s).