Results 1 to 11 of 11

Thread: [OGL] Trying to understand pointers in login procedure

  1. #1
    Join Date
    Aug 2012
    Posts
    188
    Mentioned
    9 Post(s)
    Quoted
    71 Post(s)

    Default [OGL] Trying to understand pointers in login procedure

    Hey guys I've been dissecting the ogl library for a couple days now and I ran across some pointer stuff I'm having trouble understanding. Could anyone take a moment to explain this to me?
    Simba Code:
    function tLogin.clickLogin():boolean;
      var
        funcPointer:pointer;
        funcPointerIndex,
          funcPointerSize:uInt32=0;
        funcX,
          funcY:int32;
      begin
        funcPointer:=glTextures(funcPointerSize);
        {if funcPointerSize<1 then
          exit();}

        if ((funcPointer+36)^=6887954) or ((funcPointer+72)^=6887954) then
          for funcPointerIndex to funcPointerSize-1 do
            begin
              if funcPointer^=1261319 then
                begin
                  clickMouse(random((funcX:=(funcPointer+12)^)-121,funcX+121),random((funcY:=(funcPointer+16)^)-18,funcY+18),1);
                  result:=true;
                  break;
                end;
              funcPointer:=funcPointer+36;
            end;
        exit(result);
      end;

    The two lines in question I'm most confused about are what this does:
    Simba Code:
    funcPointer:=glTextures(funcPointerSize);

    and what this is doing:
    Simba Code:
    if ((funcPointer+36)^=6887954) or ((funcPointer+72)^=6887954) then

    Thank you in advance for any help!

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

    Default

    I don't have time to get too in-depth, as I have to head out for dinner, but glTextures are 36 bytes. So, (funcPointer+0)^ or just funcPointer^ would be the beginning (ID) of the current texture, (funcPointer+36)^ would be the next, and (funcPointer+72)^ would be the one following.

    Simba Code:
    //~ int32 = 4 bytes
    //~ tBox = 4x int32
    type glTexture = record
    {0}        ID: int32;
    {4}        colourID: int32;
    {8}        fullColourID: int32;
    {12}       X: int32;
    {16}       Y: int32;
    {20-32}    bounds: tBox;
    end;

    So, if you wanted to get the fullColourID of the current texture, you'd use (funcPointer+8)^.

    Will touch on this more if I get home at a reasonable time.




    Skype: obscuritySRL@outlook.com

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

    Default

    Quote Originally Posted by Obscurity View Post
    ..
    Simba Code:
    {16}
    {20-32}    bounds: tBox;
    end;
    For those reading, there's a small typo here. That would be 20-36 since sizeof(TBox) is 16.


    @OP: https://villavu.com/forum/showthread.php?t=105024

    Start reading from: Pointers, Addresses, References, Bytes.
    The answers to your questions start at: Pointers to structures and arithmetic
    Last edited by Brandon; 07-20-2015 at 02:07 AM.
    I am Ggzz..
    Hackintosher

  4. #4
    Join Date
    Aug 2012
    Posts
    188
    Mentioned
    9 Post(s)
    Quoted
    71 Post(s)

    Default

    Thank you guys, I understand it now. It's definitely hard to read but I'll get used to it! And thanks brandon for all the work you're putting into developing all this stuff btw

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

    Default

    Quote Originally Posted by Brandon View Post
    For those reading, there's a small typo here. That would be 20-36 since sizeof(TBox) is 16.
    Cheers, Brandon. Though, I was just going to put {20,24,28,32} - was only putting where they were located.

    I haven't read that thread either. Thanks!




    Skype: obscuritySRL@outlook.com

  6. #6
    Join Date
    Oct 2013
    Location
    East Coast USA
    Posts
    770
    Mentioned
    61 Post(s)
    Quoted
    364 Post(s)

    Default

    @Obscurity; I know people have been attacking your code/coding style so please don't interpret this post that way. If anything is negative it's technical analysis not some hidden agenda or anything. None of my computers work with runescape/ogl so I haven't paid too much attention to oglib scripting before now.

    When I saw the code in OP was part of oglib I took a peek. The lack of constants/casting/etc was unusual - you must have chosen to code this way for a good reason. I think we can agree:

    • It's harder to read/write.
    • Human error is begging to happen (access violations).
    • If one of the data structures changes you can't find everything that references it.


    Did you do some tests that showed it was more efficient to code this way? I figured you did, and I tried myself. But my results were confusing.

    Simba Code:
    const
      LOOPS = 1000000000;
      OFFSET = 100;  {  52 to next array entry + 48 to testthis var }

    type
      TTest = record
        a,b,c: TBox;
        testthis: integer;
      end;
      TTestArray = array of TTest;

    var
      m: array[0..9] of TTest;
      p: pointer;
      tm: int64;

    begin
      SetScriptProp(SP_WriteTimeStamp, [True]);
      p := @m;

      writeln('begin member test');
      tm := getSystemTime();
      for 0 to LOOPS do
        if (m[1].testthis = 77) then
          writeln('lape is broken');
      writeln('end member test, time taken = ', getSystemTime()-tm);

      writeln('begin cast test');
      tm := getSystemTime();
      for 0 to LOOPS do
        if (TTestArray(p)[1].testthis = 77) then
          writeln('lape is broken');
      writeln('end cast test, time taken = ', getSystemTime()-tm);

      writeln('begin cast test2');
      tm := getSystemTime();
      for 0 to LOOPS do
        if (TTest((p+52)^).testthis = 77) then
          writeln('lape is broken');
      writeln('end cast test2, time taken = ', getSystemTime()-tm);

      writeln('begin pointer/offset test');
      tm := getSystemTime();
      for 0 to LOOPS do
        if ((p+100)^ = 77) then
          writeln('lape is broken');
      writeln('end pointer/offset test, time taken = ', getSystemTime()-tm);

      writeln('begin pointer/const test');
      tm := getSystemTime();
      for 0 to LOOPS do
        if ((p+OFFSET)^ = 77) then
          writeln('lape is broken');
      writeln('end pointer/const test, time taken = ', getSystemTime()-tm);
    end.

    Progress Report:
    [0:00:00]: begin member test
    [0:00:36]: end member test, time taken = 36083
    [0:00:36]: begin cast test
    [0:01:21]: end cast test, time taken = 44819
    [0:01:21]: begin cast test2
    [0:02:16]: end cast test2, time taken = 55193
    [0:02:16]: begin pointer/offset test
    [0:03:11]: end pointer/offset test, time taken = 54491
    [0:03:11]: begin pointer/const test
    [0:04:05]: end pointer/const test, time taken = 54523
    [0:04:05]: Successfully executed.

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

    Default

    I'm only on my phone at work at the moment, Bonsai, but when I ran my tests when starting ogLib, the results were almost the exact opposite. See below.

    Now, keep in mind that glxTextures() returns a pointer that we have to iterate through to create the glTextureArray. See tOGL.getTextures().

    Take a look at GLX's bank functions. To find out if the bank is open, it uses glGetTextures() which, as I mentioned, searches the entire pointer, beginning to end, returning the desired glTextures. ogLib doesn't. It searches, the same way glGetTextures() would, but breaks the search when the desired ID is found. So it does the same thing, only skipping a few steps.

    I should have used constants, but by the time I'd thought of it, or talked to Brandon about a change in model/font data types, it was already too far along. Suppose I could look into updating everything. I've got a lot of it memorized, like +24 offset for a texture's Y1, so I don't think it'd take me much.

    Cheers for the feedback, mate. Please continue. :-).




    Skype: obscuritySRL@outlook.com

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

    Default

    Progress Report:
    [0:00:00]: begin member test
    [0:00:31]: end member test, time taken = 31565
    [0:00:31]: begin cast test
    [0:01:12]: end cast test, time taken = 40386
    [0:01:12]: begin cast test2
    [0:02:03]: end cast test2, time taken = 51148
    [0:02:03]: begin pointer/offset test
    [0:02:55]: end pointer/offset test, time taken = 51814
    [0:02:55]: begin pointer/const test
    [0:03:45]: end pointer/const test, time taken = 50340
    [0:03:45]: Successfully executed.

    @bonsai;

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

    Default

    Alright, now that I have a bit more time and aren't typing on my phone.

    The following tests were done with the following, fairly standard, layout:


    Let's look at something as simple as checking if our combat type, be it full manual, legacy, revolution - sorry, you're going to need to be logged in for this, but please, feel free to run both. The combat icon has a texture IDs 364748 and 368253. For both cases, I had the full manual showing. I tried to made the SRL-GLX version as efficient as possible, however it matches (apart from using Cases) the include:
    Simba Code:
    program BM;
    {$include_once ogLib/lib/core/core.simba}


    Function glGetTextures(IDs: TIntegerArray): glTextureArray; overload;
    var
      Size, I, J: Cardinal;
      Ptr: Pointer;
    Begin
      J := 0;
      Result := [];
      Ptr := GLXTextures(Size);
      SetLength(Result, Size);

      For I := 0 To Size - 1 Do
      Begin
        if (IDs.Contains(Integer(Ptr^))) then
        begin
          Result[J] := glTexture(Ptr^);
          Inc(J);
        end;
        Ptr.Inc(sizeof(glTexture));
      End;
      SetLength(Result, J);
    End;

    Function GL_CombatMode: string;
    Var TextureArray:glTextureArray;
    Begin
      If Length(TextureArray:=glGetTextures([364748,368253])) Then
      Begin
        Case textureArray[0].ID Of
          368253:Result:='revolution';
          364748:
          Case textureArray[0].ColourID Of
            7234886:Result:='full manual';
            7496771:Result:='momentum';
          End;
        End;
      End;
      Exit(Result);
    End;

    //~ IDs:
    //~ 364748 - Full Manual/Momentum
    //~ 368253 - Revolution

    //~ ColourIDs:
    //~ 7234886 - Full Manual
    //~ 7496771 - Momentum

    const
      loopCount:int32=10000;

    var
      timeEnd,
        timeStart:int64;

    begin
      ogl.setup();
      timeStart:=getSystemTime();
      for 0 to loopCount do
        GL_CombatMode;
      timeEnd:=getSystemTime();
      writeln('end GLX test, time taken = ', timeEnd-timeStart);
    end.

    Code:
    14:43:50 | BM > setup
    end GLX test, time taken = 216515
    Successfully executed.
    Analyzing this, we can see that glGetTextures() is looking through each and every texture, starting first-drawn to the last, and looking for the texture IDs 364748 and 368253. Even if it finds it right away, which it won't because it's one of the last drawn, it keeps going and then returns the results as a glTextureArray. Regardless, it's still operating similar to the pointer/const method you posted above.

    Now, if I were to do the same, using my method:
    Simba Code:
    program BM;
    {$include_once ogLib/lib/core/core.simba}


    function GL_CombatMode:string;
      var
        funcPointer:pointer;
        funcPointerSize:uInt32=0;
      begin
        funcPointer:=glTextures(funcPointerSize)+(36*(funcPointerSize-1));
        for 0 to funcPointerSize div 4 do
          begin
            case 0+funcPointer^ of
              364748: //~ Full Manual/Momentum
                begin
                  case 0+(funcPointer+4)^ of
                    7234886:result:='full manual';
                    7496771:result:='momentum';
                  end;
                  break;
                end;
              368253: //~ Revolution
                begin
                  result:='revolution';
                  break;
                end;
            end;
            funcPointer:=funcPointer-36;
          end;
        exit(result);
      end;

    const
      loopCount:int32=10000;

    var
      timeEnd,
        timeStart:int64;

    begin
      ogl.setup();
      timeStart:=getSystemTime();
      for 0 to loopCount do
        GL_CombatMode;
      timeEnd:=getSystemTime();
      writeln('end ogLib test, time taken = ', timeEnd-timeStart);
    end.

    Code:
    14:53:38 | BM > setup
    end ogLib test, time taken = 31
    Successfully executed.
    Since, from debugging, we know that the combat icon is one of the last drawn textures, it's safe to assume we should start at the end and it'll be within the first 1/4 of textures found. For that reason, we use:
    Simba Code:
    funcPointer:=glTextures(funcPointerSize)+(36*(funcPointerSize-1));
    for 0 to funcPointerSize div 4 do

    It starts at the end. When it finds the result it's looking for, it stops. That's why I wrote my own include. :P.

    @bonsai
    Last edited by Obscurity; 07-20-2015 at 07:55 PM.




    Skype: obscuritySRL@outlook.com

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

    Default

    Also this can be helpful for understanding of pointers.
    Per aspera ad Astra!
    ----------------------------------------
    Slow and steady wins the race.

  11. #11
    Join Date
    Oct 2013
    Location
    East Coast USA
    Posts
    770
    Mentioned
    61 Post(s)
    Quoted
    364 Post(s)

    Default

    You compared apples and oranges, though.

    Can you run this version?

    Simba Code:
    program BM;
    {$include_once ogLib/lib/core/core.simba}

    function GL_CombatMode:string;
      var
        funcPointer:^glTextureArray;
        funcPointerSize:uInt32=0;
        i:uInt32=0;
      begin
        funcPointer:=glTextures(funcPointerSize);
        for i := (funcPointerSize-1) downto (funcPointerSize div 4) do
          begin
            case funcPointer^[i].ID of
              364748: //~ Full Manual/Momentum
                begin
                  case funcPointer^[i].colourID  of
                    7234886:exit('full manual');
                    7496771:exit('momentum');
                  end;
                end;
              368253: //~ Revolution
                begin
                  exit('revolution');
                end;
            end;
          end;
        exit('undefined');
      end;

    const
      loopCount:int32=10000;

    var
      timeEnd,
        timeStart:int64;

    begin
      ogl.setup();
      timeStart:=getSystemTime();
      for 0 to loopCount do
        GL_CombatMode;
      timeEnd:=getSystemTime();
      writeln('end ogLib test, time taken = ', timeEnd-timeStart);
    end.

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
  •