MM2 reading files

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
User avatar
lisa
Posts: 8330
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

MM2 reading files

#1 Post by lisa » Thu Jul 28, 2016 12:30 am

I checked wiki but didn't find anything for what I was after, also found a mistake.
http://www.solarstrike.net/wiki/index.p ... ess_Module
getModuleFilename
and the example has "string process.getModuleAddress(handle process)" which is the previous command.


Ok anyway to what I am after, currently working on a game that keeps records of text from in game constantly, so instead of trying to read this from memory I am looking at constantly reading the files to see what is happening in game. In old MM I would just do the io.open() and do it that way but this is MM2 and so wondering if there is a way to monitor files with MM2 efficiently.
I used filesystem.fileExists() and it confirms the file exists but I couldn't find anything in wiki for reading the files.

So long story short, am I best to use io.open(filename, "r") or is there another way in MM2 to deal with file reading?
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

User avatar
lisa
Posts: 8330
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: MM2 reading files

#2 Post by lisa » Thu Jul 28, 2016 2:32 am

What I am currently doing is in the macro.main loop

Code: Select all

			--get event table
			if( filesystem.fileExists(filename) ) then
				eventtable = {}
				local file = io.open(filename, "r");
				for line in file:lines() do 
					table.insert(eventtable,line)
				end
				file:close()
			end	
and then just use a function to read from the table.

Code: Select all

function checkevent(msg)
	for k,v in pairs(eventtable) do
		if string.match(v, msg) then 
			return true
		end
	end
	return false
end
I think there was a function for table and contains but I just don't remember it.

Either way this seems to be working and I am currently wiping the file at intervals so it doesn't get to big, there are a few lines added pretty much every second, so it gets rather large over time if I didn't wipe it.
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

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

Re: MM2 reading files

#3 Post by Administrator » Thu Jul 28, 2016 11:24 am

Thanks for pointing that out. I'll have to fix that after work. And one of these days I need to post here about the new library I've got going and/or maybe do a short video demo (it's super exciting, I promise! I just haven't had time nor the energy to finish it).

I don't believe there's any way to capture the event of content being written to a file, short of input redirection (which isn't going to work for what you're trying to do anyways).

Does the game just continually tack data onto the end of the file, or does it sometimes (during runtime) reset the file's contents (for example, only keeps the last 100 lines)? If it just keeps appending data on the end, I would keep track of the file's length after each read, then on your next read attempt you can use file:seek() to jump to the location you last read from. This way, you'd only ever be pulling new data instead of re-reading the same old garbage unnecessarily.

User avatar
lisa
Posts: 8330
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: MM2 reading files

#4 Post by lisa » Thu Jul 28, 2016 8:25 pm

just keeps adding lines constantly, it does start a new file but I am not sure at what point, if it each log in or after ## lines or each day, haven't determined that yet.

The lines are constantly being added when you do something, so when I am active it is adding more lines but when I sit there doing nothing then no lines are added, so checking constantly is probably the best thing for now> It may develop into something bigger but for now I am looking for 3 lines and I respond to those 3 lines differently, eventually I could expand that to many more lines for different tasks but for now 3 is enough.

The game doesn't hold the file in any way, so I can freely read and write to it as I want, the game doesn't seem to read the file and only adds in a single line at the very end when it does add info. So I think just monitoring the file constantly and wiping it clean as I need to will probably do the trick.

I usually wipe the file when I find a line that I want, this way next time I read the file I am not getting that same line in my search, I am sure there are very complicated ways of determining if I have read that line before and therefore ignoring it but pure and simple wiping file clean doesn't have any bad side affects, that I know of yet lol
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

Exempt
Posts: 197
Joined: Wed Jan 20, 2010 9:55 am

Re: MM2 reading files

#5 Post by Exempt » Sat Aug 06, 2016 11:30 pm

when I call the macro.init()...

Code: Select all

file = io.open("filename", "r")
--loop to the the end of the file at the start
for c in file:lines() do; end
then in the macro.main()

Code: Select all

for event in file:lines() do
	if(string.find(event, "some text") then
	    --do something
	end
end

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest