1 #ifndef __OGFTOOL_H__
2 #define __OGFTOOL_H__
3 
4 #include "ogfdefs.h"
5 
6 // little-endian only
7 class memory_reader {
8 private:
9 	const uint8_t	*m_data;
10 	size_t		m_size;
11 	size_t		m_offset;
12 
13 	void		reset(const uint8_t *data, size_t size, size_t offset = 0);
14 public:
15 			memory_reader();
16 			memory_reader(const void *data, size_t size);
17 			~memory_reader();
18 
19 	int		open_chunk(uint32_t, memory_reader &);
20 	int		open_chunk_next(uint32_t &, memory_reader &);
21 
22 	void		r(void *, size_t);
23 
24 	int		eof(void);
25 	size_t		elapsed(void);
26 
27 	uint32_t	r_u32(void);
28 	uint16_t	r_u16(void);
29 	uint8_t		r_u8(void);
30 
31 	int32_t		r_s32(void);
32 	int16_t		r_s16(void);
33 	int8_t		r_s8(void);
34 
35 	char		*r_stringZ(void);
36 
37 	float		r_float(void);
38 
39 	void		r_vector3f(struct vector3f &);
40 	void		r_vector2f(struct vector2f &);
41 };
42 
43 class memory_writer {
44 private:
45 	uint8_t		*m_data;
46 #define DEFAULT_BUFFER_SIZE		(32*1024)
47 	size_t		m_size;
48 	size_t		m_offset;
49 
50 #define MAX_CHUNK_LEVEL			16
51 	unsigned	m_chunk_level;
52 	size_t		m_chunk_pos[MAX_CHUNK_LEVEL];
53 
54 	void		extend_if_needed(size_t);
55 	void		clear(void);
56 
57 public:
58 			memory_writer(void);
59 			~memory_writer(void);
60 
data(void)61 	uint8_t		*data(void) { return m_data; }
size(void)62 	size_t		size(void) { return m_offset; }
63 
64 	void		open_chunk(uint32_t);
65 	void		close_chunk(void);
66 
67 	void		w(const void *, size_t);
68 	void		w_chunk(uint32_t, const void *, size_t);
69 
70 	void		w_u32(uint32_t);
71 	void		w_u16(uint16_t);
72 	void		w_u8(uint8_t);
73 
74 	void		w_s32(int32_t);
75 	void		w_s16(int16_t);
76 	void		w_s8(int8_t);
77 
78 	void		w_stringZ(const char *);
79 
80 	void		w_float(float);
81 
82 	void		w_vector3f(const struct vector3f &);
83 	void		w_vector2f(const struct vector2f &);
84 };
85 
86 struct ogf_bone_data {
87 	const char		*name;
88 	const char		*parent_name;
89 	struct obb		bbox;
90 
91 	uint32_t		partition;	// really?
92 	const char		*game_mtl_name;
93 	struct s_bone_shape	shape;
94 	struct s_joint_ik_data	joint_ik_data;
95 	struct vector3f		bind_rotation;
96 	struct vector3f		bind_position;
97 	float			mass;
98 	struct vector3f		center_of_mass;
99 };
100 
101 class ogf_data {
102 private:
103 	uint32_t		m_chunk_mask;
104 
105 	// OGF_HEADER chunk
106 	struct ogf_header	m_header;
107 
108 	// OGF_TEXTURE chunk
109 	const char		*m_texture_name;
110 	const char		*m_shader_name;
111 
112 	// OGF_VERTICES chunk
113 	uint32_t		m_vertex_format;
114 	unsigned		m_vertex_count;
115 	struct vert_boned_1w	*m_vertices_1w;
116 	struct vert_boned_2w	*m_vertices_2w;
117 
118 	// OGF_INDICES chunk
119 	unsigned		m_index_count;
120 	uint16_t		*m_indices;
121 
122 	// OGF_SWIDATA chunk
123 	uint32_t		m_slide_window_reserved[4];
124 	unsigned		m_slide_window_count;
125 	struct slide_window	*m_slide_windows;
126 
127 	// OGF_CHILDREN chunk
128 	unsigned		m_children_count;
129 	ogf_data		*m_children;
130 
131 	// OGF_S_BONE_NAMES and OGF_S_IKDATA chunks
132 	unsigned		m_bone_count;
133 	struct ogf_bone_data	*m_bones;
134 
135 	// OGF_S_MOTIONS chunk
136 	size_t			m_s_motions_size;
137 	void			*m_s_motions_raw;	// FIXME
138 
139 	// OGF_S_SMPARAMS chunk
140 	size_t			m_s_smparams_size;
141 	void			*m_s_smparams_raw;	// FIXME
142 
143 	// OGF_S_USERDATA chunk
144 	const char		*m_userdata;
145 
146 	// OGF_S_DESC chunk
147 	const char		*m_source;
148 	const char		*m_convertor;
149 	uint32_t		m_convert_time;
150 	const char		*m_creator;
151 	uint32_t		m_create_time;
152 	const char		*m_editor;
153 	uint32_t		m_edit_time;
154 
155 	// OGF_S_MOTION_REFS chunk
156 	const char		*m_s_motion_refs;
157 
158 	void	clear();
159 
160 	void	w_chunk_texture(memory_writer &);
161 	void	r_chunk_vertices(memory_reader &);
162 	void	w_chunk_vertices(memory_writer &);
163 	void	r_chunk_indices(memory_reader &);
164 	void	w_chunk_indices(memory_writer &);
165 	void	r_chunk_swidata(memory_reader &);
166 	void	w_chunk_swidata(memory_writer &);
167 	void	r_chunk_children(memory_reader &);
168 	void	w_chunk_children(memory_writer &);
169 	void	r_chunk_s_bone_names(memory_reader &);
170 	void	w_chunk_s_bone_names(memory_writer &);
171 	void	r_chunk_s_motions(memory_reader &);
172 	void	w_chunk_s_motions(memory_writer &);
173 	void	r_chunk_s_smparams(memory_reader &);
174 	void	w_chunk_s_smparams(memory_writer &);
175 	void	r_chunk_s_ikdata(memory_reader &);
176 	void	w_chunk_s_ikdata(memory_writer &);
177 	void	r_chunk_s_desc(memory_reader &);
178 	void	w_chunk_s_desc(memory_writer &);
179 public:
180 		ogf_data();
181 		~ogf_data();
182 
183 	void	read(memory_reader &r);
184 	void	write(memory_writer &w);
185 
186 	void	delete_child(unsigned);
187 	void	move(float, float, float);
188 };
189 
190 class ogf_file {
191 private:
192 	ogf_data	m_ogf_data;
193 	uint8_t		*m_raw_data;
194 
195 	void	clear(void);
196 public:
197 		ogf_file(void);
198 		~ogf_file(void);
199 
move(float dx,float dy,float dz)200 	void	move(float dx, float dy, float dz) { m_ogf_data.move(dx, dy, dz); }
delete_child(unsigned id)201 	void	delete_child(unsigned id) { m_ogf_data.delete_child(id); }
202 
203 	int	read(const char *);
204 	int	write(const char *);
205 };
206 
207 #endif
208