libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
opts.c
1#include "error.h"
2#include "region_impl.h"
3
4bool is_vanilla_compatible(const struct clod_region_opts *opts) {
5 return
6 opts->dims == 2 &&
7 memcmp(opts->prefix, "region", strlen("region")) == 0 &&
8 (
9 memcmp(opts->region_ext, "mca", strlen("mca")) == 0 ||
10 memcmp(opts->region_ext, "mcr", strlen("mcr")) == 0
11 );
12}
13enum clod_region_result read_opts(struct clod_region_opts *dst, const struct clod_region_opts *src) {
14 if (src->dims) {
15 if (src->dims > CLOD_REGION_DIMENSIONS_MAX) {
16 return region_error(CLOD_REGION_INVALID_USAGE,
17 "Invalid opts.dims %d. Must be <= %d.", src->dims,
18 CLOD_REGION_DIMENSIONS_MAX);
19 }
20 dst->dims = src->dims;
21 } else {
22 dst->dims = 2;
23 }
24
25 if (src->mode) {
26 if (src->mode != CLOD_REGION_MODE_RDWR && src->mode != CLOD_REGION_MODE_RDONLY) {
27 return region_error(CLOD_REGION_INVALID_USAGE,
28 "Invalid opts.mode %d. Must be CLOD_REGION_MODE_RDWR or CLOD_REGION_MODE_RDONLY.",
29 src->mode);
30 }
31 dst->mode = src->mode;
32 } else {
33 dst->mode = CLOD_REGION_MODE_RDWR;
34 }
35
36 if (src->unix_fd) {
37 if (src->unix_fd < 0) {
38 return region_error(CLOD_REGION_INVALID_USAGE,
39 "Invalid opts.unix_fd %d. Must be >= 0.",
40 src->unix_fd);
41 }
42 dst->unix_fd = src->unix_fd;
43 }
44
45 if (src->unix_file_perms) {
47 }
48
49 if (src->prefix[0]) {
50 if (memchr(src->prefix, '.', CLOD_REGION_PREFIX_MAX) != nullptr) {
51 return region_error(CLOD_REGION_INVALID_USAGE,
52 "Invalid opts.prefix %s. Must not contain a '.' character.",
53 src->prefix);
54 }
55 strncpy(dst->prefix, src->prefix, CLOD_REGION_PREFIX_MAX);
56 dst->prefix[CLOD_REGION_PREFIX_MAX] = '\0';
57 } else {
58 strncpy(dst->prefix, "region", CLOD_REGION_PREFIX_MAX + 1);
59 }
60
61 if (src->region_ext[0]) {
62 strncpy(dst->region_ext, src->region_ext, CLOD_REGION_EXTENSION_MAX);
63 dst->region_ext[CLOD_REGION_EXTENSION_MAX] = '\0';
64 } else {
65 strncpy(dst->region_ext, "mcr", CLOD_REGION_EXTENSION_MAX + 1);
66 }
67
68 if (src->chunk_ext[0]) {
69 strncpy(dst->chunk_ext, src->chunk_ext, CLOD_REGION_EXTENSION_MAX);
70 dst->chunk_ext[CLOD_REGION_EXTENSION_MAX] = '\0';
71 } else {
72 strncpy(dst->chunk_ext, "mcc", CLOD_REGION_EXTENSION_MAX + 1);
73 }
74
75 if (src->compression) {
76 if (!clod_compression_support(src->compression)) {
77 return region_error(CLOD_REGION_INVALID_USAGE,
78 "Invalid opts.compression %d. Either the required compression library has been intentionally disabled, or the compression mode is invalid.",
79 src->compression);
80 }
81 dst->compression = src->compression;
82 } else if (is_vanilla_compatible(dst)) {
83 if (!clod_compression_support(CLOD_ZLIB)) {
84 return region_error(CLOD_REGION_INVALID_USAGE,
85 "libdeflate has been disabled, but it is required to compress/decompress minecraft-compatible region files.");
86 }
87 } else {
88 if (clod_compression_support(CLOD_LZ4F)) {
90 } else {
92 }
93 }
94
95 return true;
96}
@ CLOD_UNCOMPRESSED
Definition compression.h:38
@ CLOD_LZ4F
Definition compression.h:47
@ CLOD_ZLIB
Definition compression.h:59
clod_region_result
Definition region.h:33
@ CLOD_REGION_INVALID_USAGE
Definition region.h:39
uint8_t dims
Definition region.h:160
uint8_t mode
Definition region.h:163
enum clod_compression_method compression
Definition region.h:168
char chunk_ext[10+1]
Definition region.h:194
char region_ext[10+1]
Definition region.h:190
uint32_t unix_file_perms
Definition region.h:182
char prefix[30+1]
Definition region.h:186