Author Topic: Read and write values to/from soldat.ini via admin  (Read 3960 times)

0 Members and 1 Guest are viewing this topic.

Offline DeMo

  • Soldier
  • **
  • Posts: 127
  • Stay Metal! \m/
    • Encoder 2002
Read and write values to/from soldat.ini via admin
« on: May 11, 2007, 07:36:30 pm »
Script Name: Edit soldat.ini via admin
Script Description: These functions enable you to read (/rconf) and write (/wconf) values to/from your server's soldat.ini via admin (adminlog, ARSSE, ESA etc). You can read current values and write new ones as well, this enables you to reconfigure certain aspects of your server on the fly, without the need to re-upload soldat.ini everytime.
Original Author(s): DeMo
Core Version: 1.0.0 (sv2.6.0)
Requeriments: KeYDoN's xsplit

Procedure to read values:
Code: [Select]
procedure readConf(params : string);
var
  section, key : string;
  args : TStringArray;
begin
  args := xsplit(params, ' ');
 
  //check if params were passed
  if (ArrayHigh(args) = 0) then begin
    WriteLn('RCONF ERROR: No parameters passed.');
    exit;
  end;

  //argument ok, check if is in form section\key
  if (ContainsString(args[1], '\')) then begin
    section := uppercase(GetPiece(args[1], '\', 0));
    key := GetPiece(args[1], '\', 1);
  end
  else begin
    WriteLn('RCONF ERROR: Invalid format, use Section\Key');
    exit;
  end;
   
  //check if section is valid
  if ((section = 'GAME') OR (section = 'NETWORK') OR (section = 'BOTS')) then begin
    WriteLn('RCONF: '+ section + '\' + key + ' = ' + ReadINI('soldat.ini', section, key, 'Invalid Key!'));
  end
  else begin
    WriteLn('RCONF ERROR: Invalid Section (' + section + '), use GAME, NETWORK or BOTS');
  end;
end;
Usage: /rconf Section\Key (not case sensitive)
Example: /rconf game\max_grenades



Procedure to write values:
Code: [Select]
procedure writeConf(params : string);
var
  section, key, newfile : string;
  args, strlist : TStringArray;
  i : integer;
begin
  args := xsplit(params, ' ');

  //check if params were passed
  if (ArrayHigh(args) = 0) then begin
    WriteLn('WCONF ERROR: No parameters passed.');
    exit;
  end;

  //argument ok, check if is in form section\key
  if (ContainsString(args[1], '\')) then begin
    section := uppercase(GetPiece(args[1], '\', 0));
    key := GetPiece(args[1], '\', 1);
  end
  else begin
    WriteLn('WCONF ERROR: Invalid format, use Section\Key');
    exit;
  end;
   
  //argument and format ok, check if new value was passed
  if (ArrayHigh(args) = 1) then begin
      WriteLn('WCONF ERROR: No value specified! (ie: /writeconf network\ase_register 1)');
      exit;
  end;
 
  //all ok, check section and write value
  if ((section = 'GAME') OR (section = 'NETWORK') OR (section = 'BOTS')) then begin
    strlist := xsplit(ReadFile('soldat.ini'), chr(13)+chr(10));
    for i := 0 to ArrayHigh(strlist) do begin
      if (MaskCheck(uppercase(strlist[i]), uppercase(key + '=*')) = True) then begin
        //change the key
        strlist[i] := key + '=' + args[2];
        WriteLn('WCONF: ' + key + ' changed to: ' + args[2]);
        break;
      end
      else begin
        if (i = ArrayHigh(strlist)) then begin
          WriteLn('WCONF ERROR: Invalid key (' + key + ')!');
          exit;
        end;
      end;
    end;
    //rewrite the file
    newfile := strlist[0];
    for i := 1 to ArrayHigh(strlist) do begin
      newfile := newfile + chr(13)+chr(10) + strlist[i];
    end;
    WriteFile('soldat.ini', newfile);
    WriteLn('WCONF: soldat.ini successfully rewritten!');
  end
  else begin
    WriteLn('WCONF ERROR: Invalid Section (' + section + '), use GAME, NETWORK or BOTS');
  end;
end;
Usage: /wconf Section\Key value (not case sensitive)
Example: /wconf game\max_grenades 4



Don't forget to add the calls to the OnCommand event in Core.pas
Code: [Select]
function OnCommand(ID: Byte; Text: string): boolean;
begin
  if (StrPos('/rconf', Text) > 0) then begin
    readConf(Text);
    Result := false;
  end;
  if (StrPos('/wconf', Text) > 0) then begin
    writeConf(Text);
    Result := false;
  end;
end;

**IMPORTANT**: After writing new values to your soldat.ini, don't forget to send the /loadcon command to make the server read the new config!!


If you have any questions, critics or suggestions feel free to post here or PM me.
Also, should you find any bugs.. please let me know!
Hope you like it.
« Last Edit: June 19, 2007, 09:47:13 pm by DeMo »

<@Evil-Ville> Expect a picture of Chakra` holding his fleshlight soon!

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: Read and write values to/from soldat.ini via admin
« Reply #1 on: May 11, 2007, 08:13:52 pm »
I didn't test it, but i have to admit it's a very nice job. Can be usefull as well. I think I'm gonna use it to change what weapons are available, to enable dodge-ball mode on the fly. :D
urraka

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Read and write values to/from soldat.ini via admin
« Reply #2 on: May 18, 2007, 10:50:21 pm »
I thought that the WriteFile procedure now only adds text to the beginning of the file, as it seems to be doing for the 2.6.1 prerelease... maybe not on the 2.6.0... or im going crazy.

I havn't tested it neither but it looks great though!
« Last Edit: May 18, 2007, 10:54:33 pm by DorkeyDear »

Offline DeMo

  • Soldier
  • **
  • Posts: 127
  • Stay Metal! \m/
    • Encoder 2002
Re: Read and write values to/from soldat.ini via admin
« Reply #3 on: May 19, 2007, 10:26:22 am »
Dorkey, it should work fine in 2.6.0 and 2.6.1 using Keydon's xsplit function.

The way I programmed it, it first loads your soldat.ini into a string, then searches this string for the parameter you want to change, once it finds the parameter it replaces the value and rewrites everything back to soldat.ini. In theory there's no way it'll write something in the wrong section, but I'll do some testing.

<@Evil-Ville> Expect a picture of Chakra` holding his fleshlight soon!

Offline kusyi

  • Major(1)
  • Posts: 8
  • Ural Clan
Re: Read and write values to/from soldat.ini via admin
« Reply #4 on: June 15, 2007, 01:20:58 am »
Please post this script in .zip archive
^^

Offline mikembm

  • Soldier
  • **
  • Posts: 210
Re: Read and write values to/from soldat.ini via admin
« Reply #5 on: June 15, 2007, 01:29:27 am »
In your procedure to write values you are missing some semi-colons on a couple end statements.
You should only leave out the semi-colon if the next line is an else statement.
Until you fix these errors this script will not be compatible with my script combiner. :(

Offline DeMo

  • Soldier
  • **
  • Posts: 127
  • Stay Metal! \m/
    • Encoder 2002
Re: Read and write values to/from soldat.ini via admin
« Reply #6 on: June 19, 2007, 09:48:21 pm »
Thank you mike, guess it's fixed now.
I'm not a pascal programmer, when I wrote the code I had some trouble trying to identify where to use and not to use the semicolon. ;)

@kusyi
Why do you need a .zip? Can't you just copy and paste the code?

<@Evil-Ville> Expect a picture of Chakra` holding his fleshlight soon!

Offline kusyi

  • Major(1)
  • Posts: 8
  • Ural Clan
Re: Read and write values to/from soldat.ini via admin
« Reply #7 on: June 20, 2007, 01:21:58 am »
i can't combine my scripts(
^^

Offline mikembm

  • Soldier
  • **
  • Posts: 210
Re: Read and write values to/from soldat.ini via admin
« Reply #8 on: June 20, 2007, 01:26:37 am »
You can use my script combiner:
http://scriptcombiner.awardspace.com/