Onetap JavaScript config system

mail@pastecode.io avatar
unknown
javascript
25 days ago
9.3 kB
3
Indexable
Never
var configs = {};

configs.save_label_timeout = 0;
configs.load_label_timeout = 0;

configs.extension = 'hl';
configs.cache; 
configs.name = 'slot';

configs.save = function () {
    var current_name = menu.get_value('Settings', 'Configs', 'Config name', 1) + '.' + configs.extension;
    var dependency_name = 'dependency' + '.' + configs.extension;

    // Save settings to the current_name file
    for (tab in menu.items) {
        var objTab = menu.items[tab];

        for (subtab in objTab.items) {
            var objSubtab = objTab.items[subtab];

            for (item in objSubtab.items) {
                var objItem = objSubtab.items[item];

                if (!objItem) {
                    continue;
                }

                var current_item_key = [objTab.name, objSubtab.name, objItem.name].join('|');
                var value = objItem.value;
                var string_value = undefined;

                if (objItem.type == 'checkbox') {
                    string_value = value.toString();
                } else if (objItem.type == 'slider') {
                    string_value = value.toString();
                } else if (objItem.type == 'dropdown') {
                    string_value = value.toString();
                } else if (objItem.type == 'multidropdown') {
                    string_value = value.join(',');
                } else if (objItem.type == 'colorpicker') {
                    string_value = value.join(',');
                } else if (objItem.type == 'keybind') {
                    var item = menu.get_item(objTab.name, objSubtab.name, objItem.name);
                    string_value = [
                        item.key == undefined ? 0 : item.key,
                        item.mode
                    ].join(',');
                }

                if (string_value != undefined && !objItem.skip_config) {
                    DataFile.SetKey(current_name, current_item_key, string_value);
                }
            }
        }
    }

    // Save settings to the dependency_name file
    for (tab in menu.items) {
        var objTab = menu.items[tab];

        for (subtab in objTab.items) {
            var objSubtab = objTab.items[subtab];

            for (item in objSubtab.items) {
                var objItem = objSubtab.items[item];

                if (!objItem) {
                    continue;
                }

                var current_item_key = [objTab.name, objSubtab.name, objItem.name].join('|');
                var value = objItem.value;
                var string_value = undefined;

                if (objItem.type == 'checkbox') {
                    string_value = value.toString();
                } else if (objItem.type == 'slider') {
                    string_value = value.toString();
                } else if (objItem.type == 'dropdown') {
                    string_value = value.toString();
                } else if (objItem.type == 'multidropdown') {
                    string_value = value.join(',');
                } else if (objItem.type == 'colorpicker') {
                    string_value = value.join(',');
                } else if (objItem.type == 'keybind') {
                    var item = menu.get_item(objTab.name, objSubtab.name, objItem.name);
                    string_value = [
                        item.key == undefined ? 0 : item.key,
                        item.mode
                    ].join(',');
                }

                if (string_value != undefined && !objItem.skip_config) {
                    DataFile.SetKey(dependency_name, current_item_key, string_value);
                }
            }
        }
    }

    // Save both files
    DataFile.Save(current_name);
    DataFile.Save(dependency_name);

    configs.save_label_timeout = Math.floor(Globals.Realtime()) + 3;
};

configs.load = function() {
    var current_name = menu.get_value('Settings', 'Configs', 'Config name', 1) + '.' + configs.extension;
    

    configs.cache = current_name
    DataFile.Load(current_name);

    var empty_key = DataFile.GetKey(current_name, 'empty_key');

    for (tab in menu.items) {
        var objTab = menu.items[tab];

        for (subtab in objTab.items) {
            var objSubtab = objTab.items[subtab];

            for (item in objSubtab.items) {
                var objItem = objSubtab.items[item];

                if (!objItem) {
                    continue;
                }

                var current_item_key = [objTab.name, objSubtab.name, objItem.name].join('|');
                var string_value = DataFile.GetKey(current_name, current_item_key);

                if (string_value == empty_key) {
                    continue;
                }

                var new_value = undefined;

                if (objItem.type == 'checkbox') {
                    new_value = string_value === 'true';
                } else if (objItem.type == 'slider') {
                    new_value = Number(string_value);
                } else if (objItem.type == 'dropdown') {
                    new_value = Number(string_value);
                } else if (objItem.type == 'multidropdown') {
                    var value = string_value.split(',');
                    new_value = (function() {
                        var new_table = [];

                        for (i = 0; i < value.length; i++) {
                            new_table[i] = value[i] === 'true';
                        }

                        return new_table;
                    })();
                } else if (objItem.type == 'colorpicker') {
                    var value = string_value.split(',');
                    new_value = (function() {
                        var new_table = [];

                        for (i = 0; i < value.length; i++) {
                            new_table[i] = Number(value[i]);
                        }

                        return new_table;
                    })();
                } else if (objItem.type == 'keybind') {
                    var keybind_value = string_value.split(',');
                    menu.set_keybind(objTab.name, objSubtab.name, objItem.name, Number(keybind_value[0]), Number(keybind_value[1]));
                }

                if (new_value != undefined && !objItem.skip_config) {
                    menu.set_value(objTab.name, objSubtab.name, objItem.name, new_value);
                }
            }
        }
    }

    configs.load_label_timeout = Math.floor(Globals.Curtime()) + 2.5;
};

configs.load_cached = function() {
    var current_name = 'dependency' + '.' + configs.extension;

    DataFile.Load(current_name);

    var empty_key = DataFile.GetKey(current_name, 'empty_key');

    for (tab in menu.items) {
        var objTab = menu.items[tab];

        for (subtab in objTab.items) {
            var objSubtab = objTab.items[subtab];

            for (item in objSubtab.items) {
                var objItem = objSubtab.items[item];

                if (!objItem) {
                    continue;
                }

                var current_item_key = [objTab.name, objSubtab.name, objItem.name].join('|');
                var string_value = DataFile.GetKey(current_name, current_item_key);

                if (string_value == empty_key) {
                    continue;
                }

                var new_value = undefined;

                if (objItem.type == 'checkbox') {
                    new_value = string_value === 'true';
                } else if (objItem.type == 'slider') {
                    new_value = Number(string_value);
                } else if (objItem.type == 'dropdown') {
                    new_value = Number(string_value);
                } else if (objItem.type == 'multidropdown') {
                    var value = string_value.split(',');
                    new_value = (function() {
                        var new_table = [];

                        for (i = 0; i < value.length; i++) {
                            new_table[i] = value[i] === 'true';
                        }

                        return new_table;
                    })();
                } else if (objItem.type == 'colorpicker') {
                    var value = string_value.split(',');
                    new_value = (function() {
                        var new_table = [];

                        for (i = 0; i < value.length; i++) {
                            new_table[i] = Number(value[i]);
                        }

                        return new_table;
                    })();
                } else if (objItem.type == 'keybind') {
                    var keybind_value = string_value.split(',');
                    menu.set_keybind(objTab.name, objSubtab.name, objItem.name, Number(keybind_value[0]), Number(keybind_value[1]));
                }

                if (new_value != undefined && !objItem.skip_config) {
                    menu.set_value(objTab.name, objSubtab.name, objItem.name, new_value);
                }
            }
        }
    }

    configs.load_label_timeout = Math.floor(Globals.Curtime()) + 2.5;
};
Leave a Comment