Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: Is this useful?

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default Is this useful?

    I made a miniscript that can read 'objects' you create in a file, making file reading easier. Credits to jani for the only hard part (except for change field value), the line function

    Simba Code:
    program fileReader;

    type
      TFileReader = record
        filePath, fileData:string;
      end;

      TFileObject = record
        name, fields:string;
      end;

      TFileObjectArray = array of TFileObject;

      TFileString = string;

    function TFileReader.updateData():boolean;
    var
      fileNum:integer;
    begin
      fileNum := openFile(self.filePath, false);
      if (fileNum > -1) then
      begin
        readFileString(fileNum, self.fileData, fileSize(fileNum));
        result := true;
        closeFile(fileNum);
      end;
    end;

    function TFileReader.init(path:String):boolean;
    var
      fileNum:integer;
    begin
      self.filePath := path;
      result := self.updateData();
    end;

    function TFileReader.fileExists():boolean;
    var
      fileNum:integer;
    begin
      fileNum := openFile(self.filePath, false);
      result := (fileNum <> -1);
      if (result) then
        closeFile(fileNum);
    end;

    function TFileReader.getObject(objectName:string):TFileString;
    begin
      result := between('<objectName=' + objectName + '>', '</objectName>', self.fileData);
    end;

    function Lines(str: string): TStringArray;
    var
      p, h, i, x, o, m, l, y, z: Integer;
      d: TStringArray;
    begin
      d := [#13#10, #13, #10];
      h := High(d);
      if ((h > -1) and (str <> '')) then
      begin
        o := 1;
        SetLength(Result, Length(str));
        repeat
          l := 0;
          for x := 0 to h do
          begin
            p := Pos(d[x], str);
            if (p < 1) then
            begin
              z := High(d);
              if ((x <= z) and (x > -1)) then
              begin
                for y := x to (z - 1) do
                  d[y] := d[(y + 1)];
                SetLength(d, z);
              end;
              Dec(x);
              Dec(h);
            end else
              if ((l = 0) or (p < l)) then
              begin
                m := x;
                l := p;
              end;
          end;
          if (l > 0) then
          begin
            Result[i] := Copy(str, 1, (l - 1));
            Delete(str, 1, ((l + Length(d[m])) - 1));
            Inc(i);
          end else
            Result[i] := Copy(str, 1, Length(str));
        until (l = 0);
        SetLength(Result, (i + 1));
      end else
        Result := [string(str)];
    end;

    function StartsWith(s, str: string): Boolean;
    begin
      if ((s <> '') and (str <> '')) then
        Result := (s = Copy(str, 1, Length(s)))
      else
        Result := False;
    end;

    function TFileReader.getObjects():TStringArray;
    var
      lns:TstringArray;
      i:integer;
    begin
      lns := lines(self.fileData);
      for i := 0 to high(lns) do
        if (lns[i] <> '') then
          if StartsWith('<objectName=', trim(lns[i])) then
          begin
            setLength(result, length(result) + 1);
            result[high(result)] := between('<objectName=', '>', lns[i]);
          end;
    end;

    function string.firstLetter(s:string):boolean;
    begin
      if (length(self) > 0) then
        if (self[1] = s) then
          result := true;
    end;

    function TFileString.getObjectFields():TStringArray;
    var
      lns:TStringArray;
      i:integer;
    begin
      lns := lines(self);
      for i := 0 to high(lns) do
        if (lns[i] <> '') then
          if trim(lns[i]).firstLetter('<') then
          begin
            setLength(result, length(result) + 1);
            result[high(result)] := between('<', '>', lns[i]);
          end;
    end;

    function TFileString.getInteger():integer;
    begin
      try
        result := StrToInt(self);
      except
        result := -1337;
      end;
    end;

    function TFileString.getIntegerArray():TIntegerArray;
    var
      strArr:TStringArray;
      strArrPtr: ^String;
      i, len:integer;
    begin
      if (self = '') then
        exit
      else begin
        self := Replace(self, ']', '', [rfReplaceAll]);
        self := Replace(self, '[', '', [rfReplaceAll]);
        strArr := explode(',', self);
        len := length(strArr);
        setLength(result, len);
        strArrPtr := @strArr[0];
        for i := 0 to (len - 1) do
          result[i] := StrToInt((strArrPtr[i])^);
      end;
    end;

    function TFileString.getFieldValue(fieldName:string):TFileString;
    begin
      result := between('<' + fieldName + '>', '</' + fieldName + '>', self);
    end;

    function TFileString.getString():string;
    begin
      result := self;
    end;
    function TFileString.getStringArray():TStringArray;
    var
      strArr:TStringArray;
      strArrPtr: ^String;
      i, len:integer;
    begin
      if (self = '') then
        exit
      else begin
        self := Replace(self, ']', '', [rfReplaceAll]);
        self := Replace(self, '[', '', [rfReplaceAll]);
        strArr := explode('^', self);
        len := length(strArr);
        setLength(result, len);
        strArrPtr := @strArr[0];
        for i := 0 to (len - 1) do
          result[i] := strArrPtr[i]^;
      end;
    end;

    function TFileString.getExtended():Extended;
    begin
    end;

    function TFileString.getExtendedArray():TExtendedArray;
    var
      strArr:TStringArray;
      strArrPtr: ^String;
      i, len:integer;
    begin
      if (self = '') then
        exit
      else begin
        self := Replace(self, ']', '', [rfReplaceAll]);
        self := Replace(self, '[', '', [rfReplaceAll]);
        strArr := explode(',', self);
        len := length(strArr);
        setLength(result, len);
        strArrPtr := @strArr[0];
        for i := 0 to (len - 1) do
          result[i] := StrToFloat((strArrPtr[i])^);
      end;
    end;

    function TFileString.getVariant():Variant;
    begin
    end;

    function TFileReader.getObjectFieldValue(obj:string):TFileString;
    var
      t:TFileString;
      params:TStringArray;
    begin
      params := explode('.', obj);
      if (length(params) = 2) then
      begin
        t := self.getObject(params[0]);
        result := t.getFieldValue(params[1]);
      end else
        writeln(obj, ' are not valid parameters, OBJECTNAME.FIELDNAME');
    end;

    function TFileReader.createObject(objectName:string;Fields:TStringArray):boolean;
    var
      fileNum, i:integer;
    begin
      if (self.fileExists()) then
      begin
        fileNum := appendFile(self.filePath);
        writeFileString(fileNum, #13#10);
        writeFileString(fileNum, #13#10 + '<objectName=' + objectName + '>');
        for i := 0 to high(Fields) do
          writeFileString(fileNum, #13#10 + '  <' + Fields[i] + '>' + '</' + Fields[i] + '>');
        writeFileString(fileNum, #13#10 + '</objectName>');
        closeFile(fileNum);
        result := true;
      end;
    end;

    function TFileReader.createObject(objectName:string):boolean;overload;
    begin
      result := self.createObject(objectName, ['']);
    end;

    function TFileReader.changeFieldValue(objectName, fieldName, value:string):boolean;
    var
      i, k, index, fileNum:integer;
      lns, lns2:TStringArray;
      foundField:boolean;
    begin
      self.updateData();
      lns := lines(self.fileData);
      for i := 0 to high(lns) do
        if (lns[i] <> '') then
          if StartsWith('<objectName=' + objectName + '>', trim(lns[i])) then
          begin
            lns2 := lines(between('<objectName=' + objectName + '>', '</objectName>', self.fileData));
            for k := 0 to high(lns2) do
            begin
              if startsWith('<' + fieldName + '>', trim(lns2[k])) then
              begin
                index := i + k;
                foundField := true;
                break(2);
              end;
            end;
          end;
          if (foundField) then
          begin
            for i := 0 to high(lns) do
              if (i = index) then
                lns[i] := '<' + fieldName + '>' + value + '</' + fieldName + '>';
            fileNum := rewriteFile(self.filePath, false);
            for i := 0 to high(lns) do
              if (i = 0) then
                writeFileString(fileNum, lns[i])
              else
                writeFileString(fileNum, #13#10 + lns[i]);
            closeFile(fileNum);
            result := true
          end else
            result := false;
    end;    
    var
      monsterProfiles:TFileReader;
      clashClan:TFileReader;


    begin
      monsterProfiles.init('C:\Users\rj\Desktop\New Text Document.txt');
      monsterProfiles.createObject('Fire Giant', ['color', 'uptext']);
      monsterProfiles.changeFieldValue('Fire Giant', 'uptext', '[''ck Fire^k Fire'']');
      writeln(monsterProfiles.getObjectFieldValue('Fire Giant.uptext').getStringArray());
    end.


    Example on how to store something:

    Code:
    <objectName=Fire Giant>
        <color>[3558815,3097206,1579815,3569078]</color>
        <tol>[9,11,2,13]</tol>
        <filter>40</filter>
        <hue>[0.12, 0.16, 1.16, 0.44]</hue>
        <sat>[0.77, 1.03, 0.73, 1.67]</sat>
        <mod>[0.00, 0.00, 0.00, 0.00]</mod>
        <uptext>['ck Fire^k Fire']</uptext>
      </objectName>

    To get any value(such as uptext)just go
    Simba Code:
    TFileObject.getObjectFieldValue('Fire Giant.uptext');
    this will give you the raw string, if you want it's true value T
    Simba Code:
    FileObject.getObjectFieldValue('Fire Giant.uptext').getStringArray();

  2. #2
    Join Date
    Jan 2012
    Posts
    1,596
    Mentioned
    78 Post(s)
    Quoted
    826 Post(s)

  3. #3
    Join Date
    May 2012
    Location
    Glorious Nippon
    Posts
    1,011
    Mentioned
    50 Post(s)
    Quoted
    505 Post(s)

    Default

    Cool! I'd say yes, this is useful.

    This is very similar to what I did with my auto-coloring projects. I have a bunch of files for NPCs and inventory items in a very similar format. My functions were pretty clunky though

  4. #4
    Join Date
    Sep 2012
    Location
    Netherlands
    Posts
    2,752
    Mentioned
    193 Post(s)
    Quoted
    1468 Post(s)

    Default

    everything you make is useless @rj;

    gj

  5. #5
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by hoodz View Post
    everything you make is useless @rj;

    gj
    I try

  6. #6
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default

    I was just about to do something like this for another project, cheers for the ideas.




    Skype: obscuritySRL@outlook.com

  7. #7
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    So, um, you made a really simple xml implementation?

  8. #8
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by tls View Post
    So, um, you made a really simple xml implementation?
    yes

  9. #9
    Join Date
    May 2014
    Posts
    633
    Mentioned
    8 Post(s)
    Quoted
    322 Post(s)

    Default

    Maybe try writing/reading json next?

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

    Default

    Good job, keep up the hard work

    cheers,
    lj

  11. #11
    Join Date
    Feb 2006
    Location
    Australia
    Posts
    628
    Mentioned
    15 Post(s)
    Quoted
    105 Post(s)

    Default

    Looks cool, saves clogging the script with static values.

  12. #12
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Added a couple more procedures,

    function TFileReader.changeFieldValue(objectName, fieldName, value:string):boolean;
    function TFileReader.createObject(objectName:string;Fields: TStringArray):boolean;

    functions
    Simba Code:
    function TFileReader.createObject(objectName:string;Fields:TStringArray):boolean;
    var
      fileNum, i:integer;
    begin
      if (self.fileExists()) then
      begin
        fileNum := appendFile(self.filePath);
        writeFileString(fileNum, #13#10);
        writeFileString(fileNum, #13#10 + '<objectName=' + objectName + '>');
        for i := 0 to high(Fields) do
          writeFileString(fileNum, #13#10 + '  <' + Fields[i] + '>' + '</' + Fields[i] + '>');
        writeFileString(fileNum, #13#10 + '</objectName>');
        closeFile(fileNum);
        result := true;
      end;
    end;

    function TFileReader.createObject(objectName:string):boolean;overload;
    begin
      result := self.createObject(objectName, ['']);
    end;

    function TFileReader.changeFieldValue(objectName, fieldName, value:string):boolean;
    var
      i, k, index, fileNum:integer;
      lns, lns2:TStringArray;
      foundField:boolean;
    begin
      self.updateData();
      lns := lines(self.fileData);
      for i := 0 to high(lns) do
        if (lns[i] <> '') then
          if StartsWith('<objectName=' + objectName + '>', trim(lns[i])) then
          begin
            lns2 := lines(between('<objectName=' + objectName + '>', '</objectName>', self.fileData));
            for k := 0 to high(lns2) do
            begin
              if startsWith('<' + fieldName + '>', trim(lns2[k])) then
              begin
                index := i + k;
                foundField := true;
                break(2);
              end;
            end;
          end;
          if (foundField) then
          begin
            for i := 0 to high(lns) do
              if (i = index) then
                lns[i] := '<' + fieldName + '>' + value + '</' + fieldName + '>';
            fileNum := rewriteFile(self.filePath, false);
            for i := 0 to high(lns) do
              if (i = 0) then
                writeFileString(fileNum, lns[i])
              else
                writeFileString(fileNum, #13#10 + lns[i]);
            closeFile(fileNum);
            result := true
          end else
            result := false;
    end;


    I could add addFieldToObject but IDK if it would really be worth doing

  13. #13
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    Don't think this sorta thing exists in srl atm. Good job man.

  14. #14
    Join Date
    May 2012
    Posts
    499
    Mentioned
    23 Post(s)
    Quoted
    228 Post(s)

    Default

    I wouldn't mind seeing something like this in SRL, and if it does get added eventually I change my opinion on using it for the community fighter.

  15. #15
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Or export http://wiki.freepascal.org/fcl-json or http://wiki.freepascal.org/fcl-xml or any other *robust* serialization library available to fpc.

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

    Default

    OP is using OpenFile when he should be using TFileStream or TMemoryStream for Binary Serialization instead: https://villavu.com/forum/showthread.php?t=108702

    Would seriously make serializing objects way easier. For Objects with primitive types, you could just do. FileStream.writeBuffer(Obj, sizeof(Obj); FileStream.readBuffer(Obj, sizeof(Obj)); and be done with it.

    Serializing Objects as string is out dated (unless it's JSON and you're dealing with API calls).


    But GJ anyway.
    Last edited by Brandon; 02-06-2016 at 02:52 AM.
    I am Ggzz..
    Hackintosher

  17. #17
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    OP is using OpenFile when he should be using TFileStream or TMemoryStream for Binary Serialization instead: https://villavu.com/forum/showthread.php?t=108702

    Would seriously make serializing objects way easier. For Objects with primitive types, you could just do. FileStream.writeBuffer(Obj, sizeof(Obj); FileStream.readBuffer(Obj, sizeof(Obj)); and be done with it.

    Serializing Objects as string is out dated (unless it's JSON and you're dealing with API calls).


    But GJ anyway.
    didn't know that, thanks

    It's still fun to create your own stuff though

  18. #18
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    Serializing Objects as string is out dated (unless it's JSON and you're dealing with API calls).
    Not if you are using it for configuration.
    I think string serialization would fit perfect for this use case.

  19. #19
    Join Date
    Mar 2012
    Posts
    107
    Mentioned
    2 Post(s)
    Quoted
    49 Post(s)

    Default

    What is the difference, or should I say reason for using this over record with all the values you need? Just curious.

  20. #20
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Quote Originally Posted by One Kid View Post
    What is the difference, or should I say reason for using this over record with all the values you need? Just curious.
    This could potentially be loaded on demand, instead of set at script compile. So you could create a form that can be opened at any point during a script's lifetime, and it would load values from a file. Thus instead of having to restart the script, you could just change the values in a file and reload them at runtime.

  21. #21
    Join Date
    Mar 2012
    Posts
    107
    Mentioned
    2 Post(s)
    Quoted
    49 Post(s)

    Default

    Quote Originally Posted by tls View Post
    This could potentially be loaded on demand, instead of set at script compile. So you could create a form that can be opened at any point during a script's lifetime, and it would load values from a file. Thus instead of having to restart the script, you could just change the values in a file and reload them at runtime.
    So essentially, for instance if this was used to do say slayer, you could then have all the information you need on every task and just detect which task you're on and only load the necessary information? Does seem like that would be faster.

  22. #22
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Quote Originally Posted by One Kid View Post
    So essentially, for instance if this was used to do say slayer, you could then have all the information you need on every task and just detect which task you're on and only load the necessary information? Does seem like that would be faster.
    Speed isn't the goal, flexibility is. It also wouldn't really be faster during runtime, as you could load all the data you need on compile. It would just use more memory(a negligible amount).

  23. #23
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default

    He's more saying... If you had a form that had, say, a username edit box, you could save the last used username and load it so that it's pre-typed next time the form loads or script runs.




    Skype: obscuritySRL@outlook.com

  24. #24
    Join Date
    Sep 2008
    Location
    Not here.
    Posts
    5,422
    Mentioned
    13 Post(s)
    Quoted
    242 Post(s)

    Default

    Quote Originally Posted by Obscurity View Post
    He's more saying... If you had a form that had, say, a username edit box, you could save the last used username and load it so that it's pre-typed next time the form loads or script runs.
    Better example: You start a combat script with options set to fight goblins. While the script is running, you notice that it isn't finding them very well, so you update the autocolor information in your configuration file and tell the script to reload. This is all done in a single compile.

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

    Default

    Quote Originally Posted by tls View Post
    Better example: You start a combat script with options set to fight goblins. While the script is running, you notice that it isn't finding them very well, so you update the autocolor information in your configuration file and tell the script to reload. This is all done in a single compile.
    Bad idea imp, because you're going to have to open the file every single time, read what you want, close; repeat. Updates to files aren't dynamic. Also opening a file while in use can be a problem.
    I am Ggzz..
    Hackintosher

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Posting Permissions

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