Results 1 to 4 of 4

Thread: save/load overall statistic of the script work to inifile

  1. #1
    Join Date
    May 2012
    Location
    Moscow, Russia
    Posts
    661
    Mentioned
    35 Post(s)
    Quoted
    102 Post(s)

    Lightbulb save/load overall statistic of the script work to inifile

    Hello. I apologize in advance for the quality of English language, but when writing I will try to make quality was as good as possible.

    Studying SIMBA documentation I stumbled on the function of writing and reading from ini files. And the first question I had was 'Why there is a function of writing info to a file INI in SIMBA?' What can one apply it for? Storing settings? But it is easier to store settings in a script because they can often be changed. Thus, keeping settings in the file would be impractical. And I came up with an idea of how this feature can be used. ini files may be very convenient to store the statistics of script usage. Any statistics for the time of its work. I think it will be useful not only for me but for other developers. And if it seems useless to you, I'm just sharing with you my experience of working with ini files in Simba.
    To storage the statistics I came up with the following structure:
    Simba Code:
    //just simple
    type TStat = record//stats struct
    TotalRunTime: integer;//Total running time
    TotalExp: integer;//Total experience gained
    TotalRunCount: integer;//The total number of launches
    end;
    You can use any other structure, it was coined solely as an example. Unfortunately, Simba can save only a string in an ini file. So I had to extend the standard features 'read\write' to ini files to be able to record integer and Boolean values to an ini file.
    Simba Code:
    //internal function for convert boolean to int
    function BoolToInt(ABool: boolean): integer;
    begin
      if ABool then
        Result := 1
      else
        Result := 0;
    end;
    //internal function for convert int to boolean
    function IntToBool(i: integer): boolean;
     begin
      if i=1 then result:=true else result:=false;
     end;
    //Write Integer value to ini
    procedure WriteInteger(Section,KeyName,Filename: string; value: integer);
     begin
      WriteINI(Section,Keyname,toStr(value),Filename);
     end;
    //Write boolean value to ini
    procedure WriteBool(Section,KeyName,Filename: string; value: boolean);
     begin
       WriteInteger(Section,KeyName,Filename,BoolToInt(value));
     end;
    //Read integer value from ini
     function ReadInteger(Section,Keyname,filename: string): integer;
     var
      i: integer;
      s: string;
     begin
      s:=ReadINI(Section,Keyname,Filename);
      i:=StrToInt(s);
      result:=i;
     end;
     //Read boolean value from ini
     function ReadBoolean(Section,Keyname,filename: string): boolean;
     var
      s: string;
      i: integer;
      b: boolean;
     begin
      s:=ReadINI(Section,Keyname,Filename);
      i:=StrToInt(s);
      b:=IntToBool(i);
      result:=b;
     end;
    After such an extension of standard mechanisms ini files work, the task of storing statistics regarding the use of script is reduced to writing\reading the values from the ini file to your structure and then using this structure in the script.
    Simba Code:
    //save stat struct
    procedure SaveHistory(hist: TStat;ininame: string);
     begin
      if not FileExists(ininame) then CreateFile(ininame);
         WriteInteger('TOTAL','TotalTime',ininame,hist.TotalRunTime);
         WriteInteger('TOTAL','TotalExp',ininame,hist.TotalExp);
         WriteInteger('TOTAL','RunCount',ininame,hist.TotalRunCount);
     end;
    //function for loading stat history, return TStat struct
    function LoadHistory(ininame: string):TStat;
    var
     hist: TStat;
     begin
      if not FileExists(ininame) then
      begin
       WriteLn('File'+'_'+ininame+'_'+'not found');
      end;
        hist.TotalRunTime:=ReadInteger('TOTAL','TotalTime',ininame);
        hist.TotalExp:=ReadInteger('TOTAL','TotalExp',ininame);
        hist.TotalRunCount:=ReadInteger('TOTAL','RunCount',ininame);
        result:=hist;
     end;
    Testing script:
    Simba Code:
    var
    RStat,WStat: TStat;
    FileName: string;
    begin
    Filename:=AppPath + 'Scripts\MyScriptStats.ini';
    WStat.TotalRunTime:=2400;
    WStat.TotalExp:=13000000;
    WStat.TotalRunCount:=200;
    SaveHistory(WStat,Filename);
    RStat:=LoadHistory(Filename);
    WriteLn('Total Time:'+toStr(RStat.TotalRunTime)+'/'+'Total Exp:'+ToStr(RStat.TotalExp)+'/'+'Total Run Count:'+ToStr(RStat.TotalRunCount));
    end.
    Test output:
    Simba Code:
    Compiled successfully in 31 ms.
    Total Time:2400/Total Exp:13000000/Total Run Count:200

    Thus, this algorithm can be used not only for the storage of statistics, but also to store whatever information. For example, storing form settings, the state of global variables, etc. I hope it was useful for you.Thank you for your attention. Enjoy!

    See the full test script in the attachment.

    Full test script see in attachment.
    Last edited by CynicRus; 06-26-2012 at 05:15 PM.
    Per aspera ad Astra!
    ----------------------------------------
    Slow and steady wins the race.

  2. #2
    Join Date
    Feb 2012
    Location
    Wonderland
    Posts
    1,988
    Mentioned
    41 Post(s)
    Quoted
    272 Post(s)

    Default

    Huh, I was browsing the tutorial section and found this.
    I like your work, it's a good fit for intermediate learning in my opinion, especially introducing people to utilizing .ini files in scripts.
    Reminds me of a thing in msi;
    Code:
    C:\Simba\Includes\MSI\MSI\Extras\Accounts\CharacterLevels.html

  3. #3
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    There really isn't a use for Bool To Int :S You can use StrToBool, and BoolToStr.

    Also I do not see the usefulness in wrapping a function just to add an integer :S

    That entire wrapper can easily be:
    Simba Code:
    WriteINI(Section, Key, ToStr(...), FilePath);

    and
    Simba Code:
    StrToType(ReadINI(Section, Key, Path));

    // OR

    StrToTypeDef(ReadINI(Section, Key, Path), DefaultValue);

    Where StrToType can be:

    StrToInt
    StrToBool
    StrToIntDef(Val, DefaultVal)

    etc..

    Also an INI file can be made just as useful as an XML file. It's a way of storing Data for accessing later. I used it to store default settings of a Form. Someone loads a form to enter player info and stuff, you don't want them to have to enter it into the form every single time. Thus an INI file can help there as you can Initialize the form with previous data upon start.
    I am Ggzz..
    Hackintosher

  4. #4
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    I used ini files before to store the statistics of my loops before. Average time taken and such. This way my script would pick out extra ordinary waits itself.

    Also in lape:
    Simba Code:
    procedure WriteINI(Section, Keyname:String; value:Variant; Filename:String); overload;
    begin
      WriteINI(Section, Keyname, ToStr(value), Filename);
    end;

    In pascalscript:
    Simba Code:
    procedure WriteINIUnTyped(Section, Keyname:String; value:Variant; Filename:String);
    begin
      WriteINI(Section, Keyname, ToStr(value), Filename);
    end;
    Working on: Tithe Farmer

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •