Permalink
Browse files

add config editor

  • Loading branch information...
1 parent d2af445 commit a4491a1d5b612a4a1ab0dccd9a7f8096891e9e4f @oscar-broman committed Jul 20, 2012
View
36 gamemodes/modules/PBP/Config/callbacks/OnDialogResponse.inc
@@ -0,0 +1,36 @@
+/*!
+ * PBP/Config/callbacks/OnDialogResponse.inc
+ *
+ * OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
+ *
+ > Priority: 0
+ */
+
+{
+ switch (dialogid) {
+ case this.DIALOG_CONFIG_MAIN: {
+ if (response == 1) {
+ this.ShowEditorDialog(playerid, listitem);
+ }
+ }
+
+ case this.DIALOG_CONFIG_CATEGORY: {
+ if (response == 1) {
+ this.ShowEditorDialog(playerid, _, listitem);
+ } else {
+ this.ShowEditorDialog(playerid);
+ }
+ }
+
+ case this.DIALOG_CONFIG_EDITOR: {
+ if (response == 1) {
+ if (!isnull(inputtext))
+ this.SetValueFromString(GetPVarInt(playerid, #this.InEntry), inputtext);
+ }
+
+ DeletePVar(playerid, #this.InEntry);
+
+ this.ShowEditorDialog(playerid, GetPVarInt(playerid, #this.InModule) | 0x80000000);
+ }
+ }
+}
View
11 gamemodes/modules/PBP/Config/commands.inc
@@ -4,3 +4,14 @@
> Priority: 0
*/
+CommandDescription<config> = @"Show the config editor.";
+
+YCMD(GROUP_RCON, GROUP_MANAGEMENT):config(playerid, params[], help) {
+ if (help) {
+ SendClientMessage(playerid, COLOR_COMMAND_HELP, @"* Show the config editor.");
+ } else {
+ this.ShowEditorDialog(playerid);
+ }
+
+ return 1;
+}
View
236 gamemodes/modules/PBP/Config/functions.inc
@@ -257,7 +257,7 @@ this.SaveEntries() {
fclose(fp);
}
-stock this.RegisterVariableEntry(const _key[], tag, size, GLOBAL_TAG_TYPES:...) {
+stock this.RegisterVariableEntry(const _key[], tag, size, module, GLOBAL_TAG_TYPES:...) {
new
address,
key[64 char],
@@ -270,7 +270,7 @@ stock this.RegisterVariableEntry(const _key[], tag, size, GLOBAL_TAG_TYPES:...)
PBP.ResolveSymbolName(key);
- #emit LOAD.S.pri 24
+ #emit LOAD.S.pri 28
#emit STOR.S.pri address
if (tag == tagof(Float:))
@@ -281,7 +281,7 @@ stock this.RegisterVariableEntry(const _key[], tag, size, GLOBAL_TAG_TYPES:...)
type = this.TYPE_INT;
if (size > 1) {
- if (numargs() > 4 && getarg(4))
+ if (numargs() > 5 && getarg(5))
type = (type << 8) | this.TYPE_ARRAY;
else
type = this.TYPE_STRING;
@@ -298,9 +298,239 @@ stock this.RegisterVariableEntry(const _key[], tag, size, GLOBAL_TAG_TYPES:...)
this.ConfigEntries[i][Address] = address;
this.ConfigEntries[i][Type] = type;
this.ConfigEntries[i][Size] = size;
+ this.ConfigEntries[i][Module] = module;
+
+ if (module != -1)
+ Bit_Let(this.ModulesWithEntries, module);
if (type == this.TYPE_STRING)
this.ConfigEntries[i][Size] |= (_:ispacked(@ptr[address]) << 31);
else if (type == this.TYPE_MALLOC_STRING && @ptr[address])
this.ConfigEntries[i][Size] |= (_:ispacked(mstr(Alloc:@ptr[address])) << 31);
+}
+
+this.GetValueAsString(entry) {
+ static buffer[1024];
+
+ buffer = "";
+
+ switch (this.ConfigEntries[entry][Type] & 0xFF) {
+ case this.TYPE_INT:
+ strcatf(buffer, _, !"%d", @ptr[this.ConfigEntries[entry][Address]]);
+
+ case this.TYPE_FLOAT:
+ strcatf(buffer, _, !"%f", @ptr[this.ConfigEntries[entry][Address]]);
+
+ case this.TYPE_ARRAY: {
+ switch (this.ConfigEntries[entry][Type] >>> 8) {
+ case this.TYPE_FLOAT: {
+ for (new j = 0; j < this.ConfigEntries[entry][Size]; j++) {
+ if (j)
+ strcatf(buffer, _, !", %f", @ptr[this.ConfigEntries[entry][Address]][j]);
+ else
+ strcatf(buffer, _, !"%f", @ptr[this.ConfigEntries[entry][Address]][j]);
+ }
+ }
+
+ default: {
+ for (new j = 0; j < this.ConfigEntries[entry][Size]; j++) {
+ if (j)
+ strcatf(buffer, _, !", %d", @ptr[this.ConfigEntries[entry][Address]][j]);
+ else
+ strcatf(buffer, _, !"%d", @ptr[this.ConfigEntries[entry][Address]][j]);
+ }
+ }
+ }
+ }
+
+ case this.TYPE_STRING:
+ strcat(buffer, "\""), strcat(buffer, @ptr[this.ConfigEntries[entry][Address]]), strcat(buffer, "\"");
+
+ case this.TYPE_MALLOC_STRING: {
+ if (@ptr[this.ConfigEntries[entry][Address]])
+ strcat(buffer, "\""), strcat(buffer, mstr(Alloc:@ptr[this.ConfigEntries[entry][Address]])), strcat(buffer, "\"");
+ else
+ strcat(buffer, "\"\"");
+ }
+ }
+
+ return buffer;
+}
+
+this.SetValueFromString(entry, input[]) {
+ new pos, key_unpacked[64];
+
+ switch (this.ConfigEntries[entry][Type] & 0xFF) {
+ case this.TYPE_INT: {
+ new value = strval(input);
+
+ if (@ptr[this.ConfigEntries[entry][Address]] != value) {
+ @ptr[this.ConfigEntries[entry][Address]] = value;
+
+ strunpack(key_unpacked, mstr(this.ConfigEntries[entry][Key]));
+
+ CallFunction(this.CB_OnConfigValueChange, ref(key_unpacked), this.ConfigEntries[entry][KeyHash]);
+ }
+ }
+
+ case this.TYPE_FLOAT: {
+ new value = _:floatstr(input);
+
+ if (@ptr[this.ConfigEntries[entry][Address]] != value) {
+ @ptr[this.ConfigEntries[entry][Address]] = value;
+
+ strunpack(key_unpacked, mstr(this.ConfigEntries[entry][Key]));
+
+ CallFunction(this.CB_OnConfigValueChange, ref(key_unpacked), this.ConfigEntries[entry][KeyHash]);
+ }
+ }
+
+ case this.TYPE_ARRAY: {
+ new bool:modified = false;
+ new old_val;
+ new bool:zerofill = false;
+ new type = this.ConfigEntries[entry][Type] >>> 8;
+
+ for (new j = 0; j < this.ConfigEntries[entry][Size]; j++) {
+ if (zerofill) {
+ @ptr[this.ConfigEntries[entry][Address]][j] = 0;
+
+ continue;
+ }
+
+ if (-1 != (pos = strfind(input, !",")))
+ input[pos] = 0;
+
+ if (!modified)
+ old_val = @ptr[this.ConfigEntries[entry][Address]][j];
+
+ if (type == this.TYPE_FLOAT)
+ @ptr[this.ConfigEntries[entry][Address]][j] = _:floatstr(input);
+ else
+ @ptr[this.ConfigEntries[entry][Address]][j] = strval(input);
+
+ if (!modified && old_val != @ptr[this.ConfigEntries[entry][Address]][j])
+ modified = true;
+
+ if (pos == -1) {
+ zerofill = true;
+
+ continue;
+ } else {
+ input[pos++] = ',';
+ }
+
+ while (0 < input[pos] <= ' ')
+ pos++;
+
+ strdel(input, 0, pos);
+ }
+
+ if (modified) {
+ strunpack(key_unpacked, mstr(this.ConfigEntries[entry][Key]));
+
+ CallFunction(this.CB_OnConfigValueChange, ref(key_unpacked), this.ConfigEntries[entry][KeyHash]);
+ }
+ }
+
+ case this.TYPE_STRING: {
+ if (strcmp(@ptr[this.ConfigEntries[entry][Address]], input, false) || isnull(@ptr[this.ConfigEntries[entry][Address]]) != isnull(input)) {
+ if (this.ConfigEntries[entry][Size] & (1 << 31))
+ strpack(@ptr[this.ConfigEntries[entry][Address]], input, this.ConfigEntries[entry][Size] & (-1 >>> 1));
+ else
+ strunpack(@ptr[this.ConfigEntries[entry][Address]], input, this.ConfigEntries[entry][Size]);
+
+ strunpack(key_unpacked, mstr(this.ConfigEntries[entry][Key]));
+
+ CallFunction(this.CB_OnConfigValueChange, ref(key_unpacked), this.ConfigEntries[entry][KeyHash]);
+ }
+ }
+
+ case this.TYPE_MALLOC_STRING: {
+ if (!@ptr[this.ConfigEntries[entry][Address]] || strcmp(mstr(Alloc:@ptr[this.ConfigEntries[entry][Address]]), input, false) || IsNull(mstr(Alloc:@ptr[this.ConfigEntries[entry][Address]])) != IsNull(input)) {
+ if (@ptr[this.ConfigEntries[entry][Address]])
+ free(Alloc:@ptr[this.ConfigEntries[entry][Address]]);
+
+ @ptr[this.ConfigEntries[entry][Address]] = _:Malloc_NewS(input, !!(this.ConfigEntries[entry][Size] & (1 << 31)));
+
+ strunpack(key_unpacked, mstr(this.ConfigEntries[entry][Key]));
+
+ CallFunction(this.CB_OnConfigValueChange, ref(key_unpacked), this.ConfigEntries[entry][KeyHash]);
+ }
+ }
+ }
+}
+
+this.ShowEditorDialog(playerid, selected_module = -1, selected_entry = -1) {
+ static buffer[1024];
+
+ if (selected_module == -1 && selected_entry == -1) {
+ buffer = "";
+
+ foreach (new module : Bits(this.ModulesWithEntries)) {
+ strcat(buffer, PBP.Modules[module][Name]), strcat(buffer, "\n");
+ }
+
+ ShowPlayerDialog(playerid, this.DIALOG_CONFIG_MAIN, DIALOG_STYLE_LIST, @"Config editor - Select category", buffer, @"OK", @"Close");
+ } else if (selected_entry == -1) {
+ new module = 0, bool:first = true;
+
+ buffer = "";
+
+ if (selected_module & 0x80000000) {
+ module = selected_module & 0x7FFFFFFF;
+ } else {
+ foreach (new i : Bits(this.ModulesWithEntries)) {
+ if (selected_module == module++) {
+ module = i;
+
+ break;
+ }
+ }
+ }
+
+ for (new i = 0; i < this.NumConfigEntries; i++) {
+ if (this.ConfigEntries[i][Module] != module)
+ continue;
+
+ if (first)
+ first = false;
+ else if (this.ShowValuesInEditor)
+ strcat(buffer, " \n");
+
+ if (this.ShowValuesInEditor) {
+ strcatf(buffer, _, "%S\n\t%s\n", mstr(this.ConfigEntries[i][Key]), this.GetValueAsString(i));
+ } else {
+ strcatf(buffer, _, "%S\n", mstr(this.ConfigEntries[i][Key]));
+ }
+ }
+
+ SetPVarInt(playerid, #this.InModule, module);
+
+ ShowPlayerDialog(playerid, this.DIALOG_CONFIG_CATEGORY, DIALOG_STYLE_LIST, @"Config editor - Select entry", buffer, @"OK", @"Back");
+ } else {
+ new module = GetPVarInt(playerid, #this.InModule);
+
+ new entry = 0;
+
+ if (this.ShowValuesInEditor)
+ selected_entry /= 3;
+
+ for (new i = 0; i < this.NumConfigEntries; i++) {
+ if (this.ConfigEntries[i][Module] != module)
+ continue;
+
+ if (selected_entry == entry++) {
+ entry = i;
+
+ break;
+ }
+ }
+
+ SetPVarInt(playerid, #this.InEntry, entry);
+
+ format(buffer, _, @"You are editing %S.\n\nCurrent value: %s", mstr(this.ConfigEntries[entry][Key]), this.GetValueAsString(entry));
+
+ ShowPlayerDialog(playerid, this.DIALOG_CONFIG_EDITOR, DIALOG_STYLE_INPUT, @"Config editor - Edit entry", buffer, @"OK", @"Cancel");
+ }
}
View
26 gamemodes/modules/PBP/Config/header.inc
@@ -10,13 +10,13 @@ stock RegisterConfigVariable;
#define RegisterConfigVariable:%1; \
forward %1@Pc_(); \
public %1@Pc_() { \
- Config.RegisterVariableEntry(#%1, tagof((%1)), sizeof((%1)), _:%1); \
+ Config.RegisterVariableEntry(#%1, tagof((%1)), sizeof((%1)), PBP.CURRENT_MODULE, _:%1); \
}
#define RegisterConfigArray:%1; \
forward %1@Pc_(); \
public %1@Pc_() { \
- Config.RegisterVariableEntry(#%1, tagof((%1)), sizeof((%1)), _:%1, true); \
+ Config.RegisterVariableEntry(#%1, tagof((%1)), sizeof((%1)), PBP.CURRENT_MODULE, _:%1, true); \
}
@@ -25,7 +25,14 @@ enum this.e_CONFIG_ENTRY {
Alloc:Key,
Address,
Type,
- Size
+ Size,
+ Module
+};
+
+enum {
+ this.DIALOG_CONFIG_MAIN = this.OFFSET,
+ this.DIALOG_CONFIG_CATEGORY,
+ this.DIALOG_CONFIG_EDITOR
};
enum {
@@ -43,10 +50,15 @@ stock const
new
- this.NumConfigEntries = 0,
- this.ConfigEntries[1024][this.e_CONFIG_ENTRY],
- this.CB_OnConfigValueChange
+ this.NumConfigEntries = 0,
+ this.ConfigEntries[1024][this.e_CONFIG_ENTRY],
+ this.CB_OnConfigValueChange,
+ BitArray:this.ModulesWithEntries<sizeof(PBP.Modules)>,
+ bool:this.ShowValuesInEditor = true
;
-forward this.RegisterVariableEntry(const _key[], tag, size, GLOBAL_TAG_TYPES:...);
+forward this.RegisterVariableEntry(const _key[], tag, size, module, GLOBAL_TAG_TYPES:...);
+forward this.GetValueAsString(entry);
+
+RegisterConfigVariable: this.ShowValuesInEditor;
View
16 scriptfiles/languages/sv.lang.inc
@@ -112,3 +112,19 @@
"* Usage: /help [command]" = "* Användning: /help [kommando]"
"* Invalid command." = "* Ogiltigt kommando."
+
+"Config editor - Select category" = "Konfigurerings redigerare - Välj kategori"
+
+"Config editor - Select entry" = "Konfigurerings redigerare - Välj post"
+
+"Back" = "Bakåt"
+
+"You are editing %S.\n\nCurrent value: %s" = "Du redigerar %S.\n\nNuvarande värde: %s"
+
+"Config editor - Edit entry" = "Konfigurerings redigerare - Redigera post"
+
+"Show the config editor." = "Visa konfigurerings redigeraren."
+
+"* Show the config editor." = "* Visa konfigurerings redigeraren."
+
+"Armour" = "Kroppsskydd"

0 comments on commit a4491a1

Please sign in to comment.