File: options.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (155 lines) | stat: -rw-r--r-- 4,366 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
//
//

#include "options.h"
#include "options/OptionsManager.h"
#include "network/multi.h"
#include "network/multiui.h"
#include "scripting/api/objs/option.h"
#include "scripting/lua/LuaTable.h"

namespace scripting {
namespace api {

//**********LIBRARY: Mission
ADE_LIB(l_Options, "Options", "opt", "Options library");

ADE_VIRTVAR(Options, l_Options, nullptr, "The available options.", "option[]", "A table of all the options.")
{
	using namespace luacpp;

	auto& options = options::OptionsManager::instance()->getOptions();

	auto table = LuaTable::create(L);
	int i      = 1;

	for (auto& opt : options) {
		table.addValue(i, l_Option.Set(option_h(opt.get())));
		++i;
	}

	return ade_set_args(L, "t", &table);
}

ADE_FUNC(persistChanges,
	l_Options,
	nullptr,
	"Persist any changes made to the options system. Options can be incapable of applying changes immediately in "
	"which case they are returned here.",
	"option[]",
	"The options that did not support changing their value")
{
	using namespace luacpp;

	auto unchanged = options::OptionsManager::instance()->persistChanges();

	auto table = LuaTable::create(L);
	int i      = 1;

	for (auto& opt : unchanged) {
		table.addValue(i, l_Option.Set(option_h(opt)));
		++i;
	}

	return ade_set_args(L, "t", &table);
}

ADE_FUNC(discardChanges, l_Options, nullptr, "Discard any changes made to the options system.", "boolean",
         "True on success, false otherwise")
{
	options::OptionsManager::instance()->discardChanges();
	return ADE_RETURN_TRUE;
}

ADE_VIRTVAR(MultiLogin, l_Options, "string", "The multiplayer PXO login name", "string", "The login name")
{
	const char* login = nullptr;
	ade_get_args(L, "|s", &login);
	
	if (ADE_SETTING_VAR && (login != nullptr)) {
		strcpy_s(Multi_tracker_login, login);
		os_config_write_string("PXO", "Login", login);
	}

	return ade_set_args(L, "s", Multi_tracker_login);
}

ADE_VIRTVAR(MultiPassword, l_Options, "string", "The multiplayer PXO login password", "boolean", "True if a password is set, false otherwise")
{
	const char* pswd = nullptr;
	ade_get_args(L, "|s", &pswd);

	if (ADE_SETTING_VAR && (pswd != nullptr)) {
		strcpy_s(Multi_tracker_passwd, pswd);
		os_config_write_string("PXO", "Password", pswd);
	}

	return ade_set_args(L, "b", (strlen(Multi_tracker_passwd) != 0));
}

ADE_VIRTVAR(MultiSquad, l_Options, "string", "The multiplayer PXO squad name", "string", "The squad name")
{
	const char* squad = nullptr;
	ade_get_args(L, "|s", &squad);

	if (ADE_SETTING_VAR && (squad != nullptr)) {
		strcpy_s(Multi_tracker_squad_name, squad);
		os_config_write_string("PXO", "SquadName", squad);
	}

	return ade_set_args(L, "s", Multi_tracker_squad_name);
}

ADE_FUNC(readIPAddressTable, l_Options, nullptr, "Gets the current multiplayer IP Address list as a table", "table", "The IP Address table")
{
	auto table = luacpp::LuaTable::create(L);

	SCP_list<SCP_string> list;
	multi_join_read_ip_address_file(list);

	int idx = 1;
	for (auto const& ip : list) {
		table.addValue(idx++, ip);
	}

	return ade_set_args(L, "t", &table);
}

ADE_FUNC(writeIPAddressTable, l_Options, "table", "Saves the table to the multiplayer IP Address list", "boolean", "True if successful, false otherwise")
{
	
	auto ip_list = luacpp::LuaTable::create(L);

	if (!ade_get_args(L, "t", &ip_list)) {
		return ADE_RETURN_FALSE;
	}

	SCP_list<SCP_string> list;

	if (ip_list.isValid()) {
		for (const auto& item : ip_list) {
			if (item.second.is(luacpp::ValueType::STRING)) {
				// This'll lua-error internally if it's not fed only strings. Additionally, catch the lua exception and
				// then carry on
				try {
					SCP_string ip = item.second.getValue<SCP_string>();
					list.push_back(ip);
				} catch (const luacpp::LuaException& /*e*/) {
					// We were likely fed a userdata that was not an string.
					// Since we can't actually tell whether that's the case before we try to get the value, and the
					// attempt to get the value is printing a LuaError itself, just eat the exception here and return
					return ADE_RETURN_FALSE;
				}
			} else {
				// This happens on a non-userdata value, i.e. a number
				LuaError(L, "Table with strings to be written contained non-string values! Aborting...");
				return ADE_RETURN_FALSE;
			}
		}
	}

	return ade_set_args(L, "b", multi_join_write_ip_address_file(list));
}

} // namespace api
} // namespace scripting