3
0
mirror of https://github.com/CrazyRedMachine/popnhax.git synced 2024-12-02 17:47:16 +01:00
popnhax/util/xmlprop.hpp

95 lines
2.6 KiB
C++
Raw Normal View History

2023-02-10 20:29:11 +01:00
#ifndef __XML_H__
#define __XML_H__
#include <stdio.h>
#include "imports/avs.h"
2024-06-16 20:44:13 +02:00
#include "util/membuf.h"
2024-06-24 22:07:40 +02:00
#include "util/psmap.h"
2023-02-10 20:29:11 +01:00
int reader_callback(uint32_t context, void *bytes, size_t nbytes) {
return fread(bytes, 1, nbytes, (FILE *)TlsGetValue(context));
}
2024-06-16 20:44:13 +02:00
int membuf_reader_callback(uint32_t context, void *bytes, size_t nbytes) {
int written = nbytes;
membuf_t *membuf = (membuf_t *)TlsGetValue(context);
if ( membuf->idx + nbytes > membuf->size )
written = membuf->size - membuf->idx;
memcpy(bytes, (membuf->buffer)+membuf->idx, written);
membuf->idx += written;
return written;
}
struct property *load_prop_membuf(membuf_t *membuf) {
if (!membuf) {
printf("Could not open membuf %p\n", membuf);
return nullptr;
}
DWORD tlsIndex = TlsAlloc();
TlsSetValue(tlsIndex, (void *)membuf);
const size_t size = property_read_query_memsize(membuf_reader_callback, tlsIndex, NULL, NULL);
membuf_rewind(membuf);
struct property_node *buffer =
(struct property_node *)calloc(size + 0x10000, sizeof(unsigned char));
struct property *config_data = property_create(0x17, buffer, size + 0x10000);
if (size > 0) {
const int ret = property_insert_read(config_data, 0, membuf_reader_callback, tlsIndex);
if (!ret) {
printf("Could not load buffer\n");
exit(-6);
}
}
return config_data;
}
2023-02-10 20:29:11 +01:00
struct property *load_prop_file(const char *filename) {
FILE *file = fopen(filename, "rb");
DWORD tlsIndex = TlsAlloc();
TlsSetValue(tlsIndex, (void *)file);
if (!file) {
printf("Could not open config file: %s\n", filename);
return nullptr;
}
const size_t size = property_read_query_memsize(reader_callback, tlsIndex, NULL, NULL);
rewind(file);
struct property_node *buffer =
(struct property_node *)calloc(size + 0x10000, sizeof(unsigned char));
struct property *config_data = property_create(0x17, buffer, size + 0x10000);
if (size > 0) {
const int ret = property_insert_read(config_data, 0, reader_callback, tlsIndex);
if (ret) {
fclose(file);
} else {
printf("Could not load %s\n", filename);
exit(-6);
}
}
return config_data;
}
2023-07-30 18:17:59 +02:00
bool _load_config(const char *filename, void *dest, const struct property_psmap *psmap) {
2023-02-10 20:29:11 +01:00
struct property *config_xml = load_prop_file(filename);
2024-06-24 22:07:40 +02:00
if (!config_xml || !(property_psmap_import(config_xml, nullptr, dest, psmap))) {
2023-07-30 18:17:59 +02:00
return false;
2023-02-10 20:29:11 +01:00
}
2023-07-30 18:17:59 +02:00
return true;
2023-02-10 20:29:11 +01:00
}
#endif