memoryWriteString() ?

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
zer0
Posts: 213
Joined: Sat Feb 16, 2008 11:55 pm

memoryWriteString() ?

#1 Post by zer0 » Sun Jul 06, 2008 11:15 pm

@Elverion
There is a memoryReadString() but no memoryWriteString(). Can that please be added, I need it for a script.


here is my attempt at writing the function in lua. I don't think it works though I haven't tested.

Code: Select all

function memoryWriteString(proc, address, msg)
  local l_msg_len = msg:len()
  local l_char = nil;
  for i=0, (l_msg_len-1) do
    l_char = msg:byte(i)
    --debug_message("char: " .. l_char);
    memoryWriteByte(proc, address + i, l_char)
  end
  memoryWriteByte(proc, address + l_msg_len, 0)
end

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: memoryWriteString() ?

#2 Post by Administrator » Sun Jul 06, 2008 11:33 pm

Hmm, well, I guess I could add it, however... There would be a good chance that it would overwrite memory it shouldn't, which would crash the game. Essentially, a memoryWriteString() would do just what you've written.

The problem with writing a string like that is that when a string is declared, it only reserves enough space for how many bytes it needs. In C, you would do something like:

Code: Select all

char mystr[] = "This is a string";
or

Code: Select all

char *mystr = new char[32];
strcpy(mystr, "This is a string");

delete mystr;
If the game uses the first method, then the string you write can contain no more than 16 characters (excluding NULL terminator). If the second method is used, then it can contain no more than 31 characters (with NULL terminator). Once this boundary is stepped beyond...expect hell to be raised. And, worst of all, there is, as far as I know, no way to determine the reserved size of the string. The best you could do is go by NULL terminator, but that's not always the case.

Because of this limitation, I have not written a function to do this. It's only real purpose would be to patch memory. And at that rate, you'd want to use memoryWriteByte/memoryWriteBytePtr anyways, as it seems more logical.

zer0
Posts: 213
Joined: Sat Feb 16, 2008 11:55 pm

Re: memoryWriteString() ?

#3 Post by zer0 » Mon Jul 07, 2008 2:29 am

I see. so if I knew the String memory size was 128 bytes. Could their be a function that puts in the string then fills the rest with nulls?

ex:

Code: Select all

memoryWriteString(proc, "test", 128)
would that work, or have I been smoking crack? :lol:

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: memoryWriteString() ?

#4 Post by Administrator » Mon Jul 07, 2008 10:24 pm

If you know it should be 128 bytes max, then you should structure your for loop to do, at most, 128 iterations.

Code: Select all

function memoryWriteString(proc, addr, msg, len)
  if( len == nil ) then len = msg:len();

  for i=1, len do
    local l_char = msg:byte(i)
    memoryWriteByte(proc, address + i, l_char)
  end
  memoryWriteByte(proc, address + len, 0)
end
^Shouldd o the trick.

zer0
Posts: 213
Joined: Sat Feb 16, 2008 11:55 pm

Re: memoryWriteString() ?

#5 Post by zer0 » Thu Jul 17, 2008 7:43 am

Here is the fixed function in case anybody needs it, it doesn't know how long the length is supposed be, so be sure you don't insert a string longer than the allocated space. ;)

Code: Select all

function memoryWriteString(proc, address, msg)
  local l_msg_len = msg:len()
  local l_char = nil;
  for i=0, (l_msg_len-1) do
    l_char = msg:byte(i)
    --debug_message("char: " .. l_char);
    memoryWriteByte(proc, address + i, l_char)
  end
  memoryWriteByte(proc, address + l_msg_len, 0)
end

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: memoryWriteString() ?

#6 Post by Administrator » Thu Jul 17, 2008 8:23 am

I've setup a section in the Wiki for stuff like this. You're free to add it if you would like:
http://solarimpact.servegame.com/wiki/i ... e=Snippets

Hopefully, it will (eventually) lead to more exposure. If left in a forum, these things typically go forgotten unless stickied.

User avatar
Rishijin
Posts: 49
Joined: Sat Jul 04, 2009 4:25 pm
Location: Kauai

Re: memoryWriteString() ?

#7 Post by Rishijin » Sat Jul 18, 2009 2:24 am

I've gotten this to work, however I am having one problem with it.
When I put a numeric variable into the msg that I want to write, I don't know how to get it to round the numbers off to some reasonable point.
It is flooding too far out in the space that I have it writing to in game.
I tried looking up some rounding features in lua, but the ones I found didn't seem like they would work properly here.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: memoryWriteString() ?

#8 Post by Administrator » Sat Jul 18, 2009 3:13 am

It is done the same way it is done in C.

Code: Select all

local msg = sprintf("%0.3f", 3.1415926);
-- msg is now the string: 3.141

User avatar
Rishijin
Posts: 49
Joined: Sat Jul 04, 2009 4:25 pm
Location: Kauai

Re: memoryWriteString() ?

#9 Post by Rishijin » Sat Jul 18, 2009 5:14 am

I tried that, and when it wrote the string in game, it gave some decimals in front of the value, so I don't think it was reading it properly.

Anyway, I got it to work using the math.floor and math.ceil commands.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests