Class: WowDBC::DBCFile

Inherits:
Object
  • Object
show all
Defined in:
lib/wow_dbc.rb,
ext/wow_dbc/wow_dbc.c

Instance Method Summary collapse

Constructor Details

#initialize(filepath, field_definitions) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'ext/wow_dbc/wow_dbc.c', line 95

static VALUE dbc_initialize(VALUE self, VALUE filepath, VALUE field_definitions) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    rb_iv_set(self, "@filepath", filepath);
    dbc->field_definitions = rb_hash_dup(field_definitions);
    rb_iv_set(self, "@field_definitions", dbc->field_definitions);

    return self;
}

Instance Method Details

#create_recordObject



244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'ext/wow_dbc/wow_dbc.c', line 244

static VALUE dbc_create_record(VALUE self) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    uint32_t new_count = dbc->header.record_count + 1;
    REALLOC_N(dbc->records, FieldValue *, new_count);
    dbc->records[new_count - 1] = ALLOC_N(FieldValue, dbc->header.field_count);
    memset(dbc->records[new_count - 1], 0, dbc->header.field_count * sizeof(FieldValue));

    dbc->header.record_count = new_count;

    return INT2FIX(new_count - 1);
}

#create_record_with_values(values) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'ext/wow_dbc/wow_dbc.c', line 460

static VALUE dbc_create_record_with_values(VALUE self, VALUE values) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    if (!RB_TYPE_P(values, T_HASH)) {
        rb_raise(rb_eArgError, "Values must be a hash");
    }

    // Check for invalid field names
    VALUE keys = rb_funcall(values, rb_intern("keys"), 0);
    for (long i = 0; i < RARRAY_LEN(keys); i++) {
        VALUE key = rb_ary_entry(keys, i);
        if (NIL_P(rb_hash_aref(dbc->field_definitions, key))) {
            rb_raise(rb_eArgError, "Invalid field name: %"PRIsVALUE, key);
        }
    }

    uint32_t new_count = dbc->header.record_count + 1;
    REALLOC_N(dbc->records, FieldValue *, new_count);
    dbc->records[new_count - 1] = ALLOC_N(FieldValue, dbc->header.field_count);

    VALUE field_names = rb_funcall(dbc->field_definitions, rb_intern("keys"), 0);
    for (uint32_t i = 0; i < dbc->header.field_count; i++) {
        VALUE field_name = rb_ary_entry(field_names, i);
        VALUE value = rb_hash_aref(values, field_name);
        VALUE field_type = rb_hash_aref(dbc->field_definitions, field_name);
        FieldType type = ruby_to_field_type(field_type);

        if (NIL_P(value)) {
            // Set default values for missing fields
            switch (type) {
                case TYPE_UINT32:
                case TYPE_INT32:
                    dbc->records[new_count - 1][i].value.uint32_value = 0;
                    break;
                case TYPE_FLOAT:
                    dbc->records[new_count - 1][i].value.float_value = 0.0f;
                    break;
                case TYPE_STRING:
                    dbc->records[new_count - 1][i].value.string_offset = 0; // Empty string
                    break;
            }
        } else {
            if (type == TYPE_STRING) {
                char *str = StringValueCStr(value);
                uint32_t offset = dbc->header.string_block_size;
                uint32_t str_len = strlen(str) + 1;

                dbc->string_block = realloc(dbc->string_block, dbc->header.string_block_size + str_len);
                memcpy(dbc->string_block + offset, str, str_len);
                dbc->header.string_block_size += str_len;

                dbc->records[new_count - 1][i].type = TYPE_STRING;
                dbc->records[new_count - 1][i].value.string_offset = offset;
            } else {
                ruby_to_field_value(value, type, &dbc->records[new_count - 1][i]);
            }
        }
        dbc->records[new_count - 1][i].type = type;
    }

    dbc->header.record_count = new_count;

    return INT2FIX(new_count - 1);
}

#delete_record(index) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'ext/wow_dbc/wow_dbc.c', line 373

static VALUE dbc_delete_record(VALUE self, VALUE index) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    long idx = FIX2LONG(index);

    if (idx < 0 || (uint32_t)idx >= dbc->header.record_count) {
        rb_raise(rb_eArgError, "Invalid record index");
    }

    free(dbc->records[idx]);
    memmove(&dbc->records[idx], &dbc->records[idx + 1], (dbc->header.record_count - idx - 1) * sizeof(FieldValue *));
    dbc->header.record_count--;

    return Qnil;
}

#find_by(field, value) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'ext/wow_dbc/wow_dbc.c', line 390

static VALUE dbc_find_by(VALUE self, VALUE field, VALUE value) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    long field_idx = -1;
    VALUE field_names = rb_funcall(dbc->field_definitions, rb_intern("keys"), 0);

    for (long i = 0; i < RARRAY_LEN(field_names); i++) {
        if (rb_eql(rb_ary_entry(field_names, i), field)) {
            field_idx = i;
            break;
        }
    }

    if (field_idx < 0 || (uint32_t)field_idx >= dbc->header.field_count) {
        rb_raise(rb_eArgError, "Invalid field name");
    }

    VALUE result = rb_ary_new();

    for (uint32_t i = 0; i < dbc->header.record_count; i++) {
        VALUE field_value = field_value_to_ruby(&dbc->records[i][field_idx], dbc->string_block);
        if (rb_eql(field_value, value)) {
            VALUE record = rb_hash_new();
            for (uint32_t j = 0; j < dbc->header.field_count; j++) {
                VALUE field_name = rb_ary_entry(field_names, j);
                rb_hash_aset(record, field_name, field_value_to_ruby(&dbc->records[i][j], dbc->string_block));
            }
            rb_ary_push(result, record);
        }
    }

    return result;
}

#get_record(index) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'ext/wow_dbc/wow_dbc.c', line 299

static VALUE dbc_get_record(VALUE self, VALUE index) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    long idx = FIX2LONG(index);

    if (idx < 0 || (uint32_t)idx >= dbc->header.record_count) {
        rb_raise(rb_eArgError, "Invalid record index");
    }

    VALUE record = rb_hash_new();
    VALUE field_names = rb_funcall(dbc->field_definitions, rb_intern("keys"), 0);
    for (uint32_t i = 0; i < dbc->header.field_count; i++) {
        VALUE field_name = rb_ary_entry(field_names, i);
        rb_hash_aset(record, field_name, field_value_to_ruby(&dbc->records[idx][i], dbc->string_block));
    }

    return record;
}

#headerObject



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'ext/wow_dbc/wow_dbc.c', line 319

static VALUE dbc_get_header(VALUE self) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    VALUE header = rb_hash_new();
    rb_hash_aset(header, ID2SYM(rb_intern("magic")), rb_str_new(dbc->header.magic, 4));
    rb_hash_aset(header, ID2SYM(rb_intern("record_count")), UINT2NUM(dbc->header.record_count));
    rb_hash_aset(header, ID2SYM(rb_intern("field_count")), UINT2NUM(dbc->header.field_count));
    rb_hash_aset(header, ID2SYM(rb_intern("record_size")), UINT2NUM(dbc->header.record_size));
    rb_hash_aset(header, ID2SYM(rb_intern("string_block_size")), UINT2NUM(dbc->header.string_block_size));

    return header;
}

#readObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/wow_dbc/wow_dbc.c', line 106

static VALUE dbc_read(VALUE self) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    VALUE filepath = rb_iv_get(self, "@filepath");
    FILE *file = fopen(StringValueCStr(filepath), "rb");
    if (!file) {
        rb_raise(rb_eIOError, "Could not open file");
    }

    if (fread(&dbc->header, sizeof(DBCHeader), 1, file) != 1) {
        fclose(file);
        rb_raise(rb_eIOError, "Failed to read DBC header");
    }

    if (dbc->records) {
        for (uint32_t i = 0; i < dbc->header.record_count; i++) {
            free(dbc->records[i]);
        }
        free(dbc->records);
    }

    dbc->records = ALLOC_N(FieldValue *, dbc->header.record_count);
    VALUE field_definitions = rb_iv_get(self, "@field_definitions");
    VALUE field_names = rb_funcall(field_definitions, rb_intern("keys"), 0);

    for (uint32_t i = 0; i < dbc->header.record_count; i++) {
        dbc->records[i] = ALLOC_N(FieldValue, dbc->header.field_count);
        for (uint32_t j = 0; j < dbc->header.field_count; j++) {
            VALUE field_name = rb_ary_entry(field_names, j);
            VALUE field_type = rb_hash_aref(field_definitions, field_name);

            FieldType type;
            if (NIL_P(field_type)) {
                type = TYPE_UINT32;  // Default to UINT32 if type is nil
            } else {
                type = ruby_to_field_type(field_type);
            }
            dbc->records[i][j].type = type;

            if (fread(&dbc->records[i][j].value, sizeof(uint32_t), 1, file) != 1) {
                fclose(file);
                rb_raise(rb_eIOError, "Failed to read DBC record field");
            }

            switch (type) {
                case TYPE_UINT32:
                    break;
                case TYPE_INT32:
                    break;
                case TYPE_FLOAT:
                    break;
                case TYPE_STRING:
                    break;
            }
        }
    }

    if (dbc->string_block) {
        free(dbc->string_block);
    }

    dbc->string_block = ALLOC_N(char, dbc->header.string_block_size);
    if (fread(dbc->string_block, 1, dbc->header.string_block_size, file) != dbc->header.string_block_size) {
        fclose(file);
        rb_raise(rb_eIOError, "Failed to read DBC string block");
    }

    fclose(file);

    return self;
}

#update_record(index, field, value) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'ext/wow_dbc/wow_dbc.c', line 258

static VALUE dbc_update_record(VALUE self, VALUE index, VALUE field, VALUE value) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    long idx = FIX2LONG(index);
    long field_idx = -1;

    VALUE field_names = rb_funcall(dbc->field_definitions, rb_intern("keys"), 0);
    for (long i = 0; i < RARRAY_LEN(field_names); i++) {
        if (rb_eql(rb_ary_entry(field_names, i), field)) {
            field_idx = i;
            break;
        }
    }

    if (idx < 0 || (uint32_t)idx >= dbc->header.record_count || field_idx < 0 || (uint32_t)field_idx >= dbc->header.field_count) {
        rb_raise(rb_eArgError, "Invalid record or field index");
    }

    VALUE field_type = rb_hash_aref(dbc->field_definitions, field);
    FieldType type = ruby_to_field_type(field_type);

    if (type == TYPE_STRING) {
        // For string fields, we need to update the string block
        char *str = StringValueCStr(value);
        uint32_t offset = dbc->header.string_block_size;
        uint32_t str_len = strlen(str) + 1;

        dbc->string_block = realloc(dbc->string_block, dbc->header.string_block_size + str_len);
        memcpy(dbc->string_block + offset, str, str_len);
        dbc->header.string_block_size += str_len;

        dbc->records[idx][field_idx].type = TYPE_STRING;
        dbc->records[idx][field_idx].value.string_offset = offset;
    } else {
        ruby_to_field_value(value, type, &dbc->records[idx][field_idx]);
    }

    return Qnil;
}

#update_record_multi(index, updates) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'ext/wow_dbc/wow_dbc.c', line 333

static VALUE dbc_update_record_multi(VALUE self, VALUE index, VALUE updates) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    long idx = FIX2LONG(index);

    if (idx < 0 || (uint32_t)idx >= dbc->header.record_count) {
        rb_raise(rb_eArgError, "Invalid record index");
    }

    if (RB_TYPE_P(updates, T_HASH)) {
        VALUE keys = rb_funcall(updates, rb_intern("keys"), 0);
        for (long i = 0; i < RARRAY_LEN(keys); i++) {
            VALUE key = rb_ary_entry(keys, i);
            VALUE value = rb_hash_aref(updates, key);
            long field_idx = -1;

            VALUE field_names = rb_funcall(dbc->field_definitions, rb_intern("keys"), 0);
            for (long j = 0; j < RARRAY_LEN(field_names); j++) {
                if (rb_eql(rb_ary_entry(field_names, j), key)) {
                    field_idx = j;
                    break;
                }
            }

            if (field_idx >= 0 && (uint32_t)field_idx < dbc->header.field_count) {
                VALUE field_type = rb_hash_aref(dbc->field_definitions, key);
                FieldType type = ruby_to_field_type(field_type);
                ruby_to_field_value(value, type, &dbc->records[idx][field_idx]);
            } else {
                rb_raise(rb_eArgError, "Invalid field name: %"PRIsVALUE, key);
            }
        }
    } else {
        rb_raise(rb_eArgError, "Updates must be a hash");
    }

    return Qnil;
}

#writeObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/wow_dbc/wow_dbc.c', line 179

static VALUE dbc_write(VALUE self) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    VALUE filepath = rb_iv_get(self, "@filepath");
    FILE *file = fopen(StringValueCStr(filepath), "wb");
    if (!file) {
        rb_raise(rb_eIOError, "Could not open file for writing");
    }

    if (fwrite(&dbc->header, sizeof(DBCHeader), 1, file) != 1) {
        fclose(file);
        rb_raise(rb_eIOError, "Failed to write DBC header");
    }

    for (uint32_t i = 0; i < dbc->header.record_count; i++) {
        for (uint32_t j = 0; j < dbc->header.field_count; j++) {
            if (fwrite(&dbc->records[i][j].value, sizeof(uint32_t), 1, file) != 1) {
                fclose(file);
                rb_raise(rb_eIOError, "Failed to write DBC record field");
            }
        }
    }

    if (fwrite(dbc->string_block, 1, dbc->header.string_block_size, file) != dbc->header.string_block_size) {
        fclose(file);
        rb_raise(rb_eIOError, "Failed to write DBC string block");
    }

    fclose(file);
    return self;
}

#write_to(new_filepath) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'ext/wow_dbc/wow_dbc.c', line 425

static VALUE dbc_write_to(VALUE self, VALUE new_filepath) {
    DBCFile *dbc;
    TypedData_Get_Struct(self, DBCFile, &dbc_data_type, dbc);

    Check_Type(new_filepath, T_STRING);
    const char *path = StringValueCStr(new_filepath);

    FILE *file = fopen(path, "wb");
    if (!file) {
        rb_raise(rb_eIOError, "Could not open file for writing: %s", path);
    }

    if (fwrite(&dbc->header, sizeof(DBCHeader), 1, file) != 1) {
        fclose(file);
        rb_raise(rb_eIOError, "Failed to write DBC header");
    }

    for (uint32_t i = 0; i < dbc->header.record_count; i++) {
        for (uint32_t j = 0; j < dbc->header.field_count; j++) {
            if (fwrite(&dbc->records[i][j].value, sizeof(uint32_t), 1, file) != 1) {
                fclose(file);
                rb_raise(rb_eIOError, "Failed to write DBC record");
            }
        }
    }

    if (fwrite(dbc->string_block, 1, dbc->header.string_block_size, file) != dbc->header.string_block_size) {
        fclose(file);
        rb_raise(rb_eIOError, "Failed to write DBC string block");
    }

    fclose(file);
    return self;
}