project_files/frontlib/model/scheme.c
author alfadur
Mon, 18 Jun 2018 09:22:53 -0400
changeset 13421 cdf69667593b
parent 10017 de822cd3df3a
permissions -rw-r--r--
partial room implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    20
#include "scheme.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    21
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    22
#include "../util/inihelper.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    23
#include "../util/logging.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    24
#include "../util/util.h"
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    25
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    26
#include <stdio.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    27
#include <stdlib.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    28
#include <limits.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    29
#include <string.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    30
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    31
flib_scheme *flib_scheme_create(const char *schemeName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    32
    flib_scheme *result = flib_calloc(1, sizeof(flib_scheme));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    33
    if(log_badargs_if(schemeName==NULL) || result==NULL) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    34
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    35
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    36
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    37
    result->name = flib_strdupnull(schemeName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    38
    result->mods = flib_calloc(flib_meta.modCount, sizeof(*result->mods));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    39
    result->settings = flib_calloc(flib_meta.settingCount, sizeof(*result->settings));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    40
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    41
    if(!result->mods || !result->settings || !result->name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    42
        flib_scheme_destroy(result);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    43
        return NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    44
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    45
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    46
    for(int i=0; i<flib_meta.settingCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    47
        result->settings[i] = flib_meta.settings[i].def;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    48
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    49
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    50
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    51
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    52
flib_scheme *flib_scheme_copy(const flib_scheme *scheme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    53
    flib_scheme *result = NULL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    54
    if(scheme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    55
        result = flib_scheme_create(scheme->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    56
        if(result) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    57
            memcpy(result->mods, scheme->mods, flib_meta.modCount * sizeof(*scheme->mods));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    58
            memcpy(result->settings, scheme->settings, flib_meta.settingCount * sizeof(*scheme->settings));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    59
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    60
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    61
    return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    62
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    63
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    64
void flib_scheme_destroy(flib_scheme* scheme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    65
    if(scheme) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    66
        free(scheme->mods);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    67
        free(scheme->settings);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    68
        free(scheme->name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    69
        free(scheme);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    70
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    71
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    72
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    73
bool flib_scheme_get_mod(const flib_scheme *scheme, const char *name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    74
    if(!log_badargs_if2(scheme==NULL, name==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    75
        for(int i=0; i<flib_meta.modCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    76
            if(!strcmp(flib_meta.mods[i].name, name)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    77
                return scheme->mods[i];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    78
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    79
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    80
        flib_log_e("Unable to find game mod %s", name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    81
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    82
    return false;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    83
}
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    84
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    85
int flib_scheme_get_setting(const flib_scheme *scheme, const char *name, int def) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    86
    if(!log_badargs_if2(scheme==NULL, name==NULL)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    87
        for(int i=0; i<flib_meta.settingCount; i++) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    88
            if(!strcmp(flib_meta.settings[i].name, name)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    89
                return scheme->settings[i];
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    90
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    91
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    92
        flib_log_e("Unable to find game setting %s", name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    93
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    94
    return def;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7497
diff changeset
    95
}