Lua Script problems on Super Nintendo emulation - lua

I'm currently trying to run a lua script to find the future values of a static rng seed from "Fire Emblem - Seisen no Keifu" in the snes9x-1.52-rr-r185 emulator by reading the memory values.
Unfortunately I get the following message when i run the code: "rng.lua:26: attempt to call field 'text' (a nil value)"
The code is below:
emu = snes9x
function get_rng()
return {
step = memory.readbyte(0x7e04a2),
table = memory.readbyterange(0x7e046b,0x37)
}
end
function rand(rng)
rng.step = rng.step + 1
if rng.step >= 0x37 then
for i = 0, 0x36 do
local j if i < 0x18 then j = i+0x1f else j = i-0x18 end
rng.table[i+1] = rng.table[i+1] - rng.table[j+1]
if rng.table[i+1] < 0 then rng.table[i+1] = rng.table[i+1] + 100 end
end
rng.step = 0
end
local res = rng.table[rng.step+1]-1
if res < 0 then res = 99 end
return res
end
while true do
local rng = get_rng()
for i = 1, 0x10 do
gui.text(i*12, 0,string.format("%02x",rng.step))
gui.text(i*12,10,string.format("%2u",rand(rng)))
end
emu.frameadvance()
end
I expected the rng values to be shown on screen but got the aforementioned message.

Related

How to get webp image EXIF metadata in lua?

I can get this data with the following code. But it runs too slow:
local handle = io.popen("exiftool image.webp")
local result = handle:read("*a")
handle:close()
Is there a more elegant way to get the metadata?
UPD:
I use this software:
docker (20.10.7)
openresty/openresty:xenial (1.15.8.3)
luarocks (3.2.1)
LuaJIT (2.1.0-beta3)
Here is an example of a picture with a UserComment field: link
Exiftool sees this property:
$ exiftool -EXIF:UserComment Johnrogershousemay2020.webp
User Comment : {"foo":"bar"}
This method is less elegant, but it does not require to run external application :-)
function get_webp_user_comment(file_name)
local file = io.open(file_name, "rb")
local exif_offset, exif_found, is_big_endian, user_comment = 12
local function read_string(offset, size)
file:seek("set", offset)
return file:read(size)
end
local function read_uint(offset, size)
local n, s = 0, read_string(offset, size)
for j = 1, size do
n = n * 256 + s:byte(is_big_endian and j or size + 1 - j)
end
return n
end
local function read_uint32(disp)
return read_uint(exif_offset + disp, 4)
end
local function read_uint16(disp)
return read_uint(exif_offset + disp, 2)
end
local function search_for_tag(ifd_disp, tag)
if ifd_disp ~= 0 then
local entry_disp = ifd_disp + 2
for j = 1, read_uint16(ifd_disp) do
if read_uint16(entry_disp) == tag then
return read_uint32(entry_disp + 8), read_uint32(entry_disp + 4)
end
entry_disp = entry_disp + 12
end
return search_for_tag(read_uint32(entry_disp), tag)
end
end
if read_string(0, 4) == "RIFF" and read_string(8, 4) == "WEBP" then
local max_offset = read_uint32(-8)
while exif_offset < max_offset do
local section_name = read_string(exif_offset, 4)
exif_offset = exif_offset + 8
if section_name == "EXIF" then
local endianness = read_string(exif_offset, 2)
is_big_endian = endianness == "MM"
exif_found = is_big_endian or endianness == "II"
if exif_found then
break
end
end
exif_offset = exif_offset + read_uint32(-4)
exif_offset = exif_offset + exif_offset % 2
end
end
if exif_found then
local exif_ifd = search_for_tag(read_uint32(4), 0x8769)
if exif_ifd then
local disp, count = search_for_tag(exif_ifd, 0x9286)
user_comment = read_string(exif_offset + disp + 8, count - 8)
end
end
file:close()
return user_comment
end
Usage example:
local file_name = "path/to/Johnrogershousemay2020.webp"
print(get_webp_user_comment(file_name))
If all you need is 'UserComment', then pass that as a parameter during your popen call:
local handle = io.popen("exiftool -EXIF:UserComment image.webp")

how do you make a string dictionary function in lua?

Is there a way if a string is close to a string in a table it will replace it with the one in the table?
Like a spellcheck function, that searches through a table and if the input is close to one in the table it will fix it , so the one in the table and the string is the same?
You can use this code :) Reference code is from here : https://github.com/badarsh2/Algorithm-Implementations/blob/master/Levenshtein_distance/Lua/Yonaba/levenshtein.lua
local function min(a, b, c)
return math.min(math.min(a, b), c)
end
local function matrix(row,col)
local m = {}
for i = 1,row do m[i] = {}
for j = 1,col do m[i][j] = 0 end
end
return m
end
local function lev(strA,strB)
local M = matrix(#strA+1,#strB+1)
local i, j, cost
local row, col = #M, #M[1]
for i = 1, row do M[i][1] = i - 1 end
for j = 1, col do M[1][j] = j - 1 end
for i = 2, row do
for j = 2, col do
if (strA:sub(i - 1, i - 1) == strB:sub(j - 1, j - 1)) then cost = 0
else cost = 1
end
M[i][j] = min(M[i-1][j] + 1,M[i][j - 1] + 1,M[i - 1][j - 1] + cost)
end
end
return M[row][col]
end
local refTable = {"hell", "screen"}
local function getClosestWord(pInput, pTable, threesold)
cDist = -1
cWord = ""
for key, val in pairs(pTable) do
local levRes = lev(pInput, val)
if levRes < cDist or cDist == -1 then
cDist = levRes
cWord = val
end
end
print(cDist)
if cDist <= threesold then
return cWord
else
return pInput
end
end
a = getClosestWord("hello", refTable, 3)
b = getClosestWord("screw", refTable, 3)
print(a, b)
Third parameter is threesold, if min distance is higher than threesold, word is not replaced.

ROBLOX - Why is this fly script working only in the Studio?

I really need help..
I have this code for fly, as a backpack item:
Name = "Fly"
pi = 3.141592653589793238462643383279502884197163993751
a = 0
s = 0
ndist = 13
rs = 0.025
siz = Vector3.new(1, 1, 1)
form = 0
flow = {}
function CFC(P1,P2)
local Place0 = CFrame.new(P1.CFrame.x,P1.CFrame.y,P1.CFrame.z)
local Place1 = P2.Position
P1.Size = Vector3.new(P1.Size.x,P1.Size.y,(Place0.p - Place1).magnitude)
P1.CFrame = CFrame.new((Place0.p + Place1)/2,Place0.p)
end
function checktable(table, parentneeded)
local i
local t = {}
for i = 1, #table do
if table[i] ~= nil then
if string.lower(type(table[i])) == "userdata" then
if parentneeded == true then
if table[i].Parent ~= nil then
t[#t + 1] = table[i]
end
else
t[#t + 1] = table[i]
end
end
end
end
return t
end
if script.Parent.Name ~= Name then
User = game:service("Players").Nineza
HB = Instance.new("HopperBin")
HB.Name = Name
HB.Parent = User.StarterGear
script.Parent = HB
User.Character:BreakJoints()
end
speed = 50
script.Parent.Selected:connect(function(mar)
s = 1
torso = script.Parent.Parent.Parent.Character.Torso
LeftShoulder = torso["Left Shoulder"]
RightShoulder = torso["Right Shoulder"]
LeftHip = torso["Left Hip"]
RightHip = torso["Right Hip"]
human = script.Parent.Parent.Parent.Character.Humanoid
bv = Instance.new("BodyVelocity")
bv.maxForce = Vector3.new(0,math.huge,0)
bv.velocity = Vector3.new(0,0,0)
bv.Parent = torso
bg = Instance.new("BodyGyro")
bg.maxTorque = Vector3.new(0,0,0)
bg.Parent = torso
connection = mar.Button1Down:connect(function()
a = 1
bv.maxForce = Vector3.new(math.huge,math.huge,math.huge)
bg.maxTorque = Vector3.new(900000,900000,900000)
bg.cframe = CFrame.new(torso.Position,mar.hit.p) * CFrame.fromEulerAnglesXYZ(math.rad(-90),0,0)
bv.velocity = CFrame.new(torso.Position,mar.hit.p).lookVector * speed
moveconnect = mar.Move:connect(function()
bg.maxTorque = Vector3.new(900000,900000,900000)
bg.cframe = CFrame.new(torso.Position,mar.hit.p) * CFrame.fromEulerAnglesXYZ(math.rad(-90),0,0)
bv.velocity = CFrame.new(torso.Position,mar.hit.p).lookVector * speed
end)
upconnect = mar.Button1Up:connect(function()
a = 0
moveconnect:disconnect()
upconnect:disconnect()
bv.velocity = Vector3.new(0,0,0)
bv.maxForce = Vector3.new(0,math.huge,0)
torso.Velocity = Vector3.new(0,0,0)
bg.cframe = CFrame.new(torso.Position,torso.Position + Vector3.new(torso.CFrame.lookVector.x,0,torso.CFrame.lookVector.z))
wait(1)
end)
end)
while s == 1 do
wait(0.02)
flow = checktable(flow, true)
local i
for i = 1,#flow do
flow[i].Transparency = flow[i].Transparency + rs
if flow[i].Transparency >= 1 then flow[i]:remove() end
end
if a == 1 then
flow[#flow + 1] = Instance.new("Part")
local p = flow[#flow]
p.formFactor = form
p.Size = siz
p.Anchored = true
p.CanCollide = false
p.TopSurface = 0
p.BottomSurface = 0
if #flow - 1 > 0 then
local pr = flow[#flow - 1]
p.Position = torso.Position - torso.Velocity/ndist
CFC(p, pr)
else
p.CFrame = CFrame.new(torso.Position - torso.Velocity/ndist, torso.CFrame.lookVector)
end
p.BrickColor = BrickColor.new("Cyan")
p.Transparency = 1
p.Parent = torso
local marm = Instance.new("BlockMesh")
marm.Scale = Vector3.new(1.9, 0.9, 1.725)
marm.Parent = p
local amplitude
local frequency
amplitude = pi
desiredAngle = amplitude
RightShoulder.MaxVelocity = 0.4
LeftShoulder.MaxVelocity = 0.4
RightHip.MaxVelocity = pi/10
LeftHip.MaxVelocity = pi/10
RightShoulder.DesiredAngle = desiredAngle
LeftShoulder.DesiredAngle = -desiredAngle
RightHip.DesiredAngle = 0
LeftHip.DesiredAngle = 0
end
end
end)
script.Parent.Deselected:connect(function()
a = 0
s = 0
bv:remove()
bg:remove()
if connection ~= nil then
connection:disconnect()
end
if moveconnect ~= nil then
moveconnect:disconnect()
end
if upconnect ~= nil then
upconnect:disconnect()
end
while s == 0 do
wait()
if #flow > 0 then
flow = checktable(flow, true)
local i
for i = 1,#flow do
flow[i].Transparency = flow[i].Transparency + rs
if flow[i].Transparency >= 1 then flow[i]:remove() end
end
end
end
end)
while true do
wait()
if s == 1 then
return
end
end
script:remove()
The script is in a HopperBin Object in the game's StarterPack Folder.
Now if you try it on your own, you'll see that it will work, BUT if you publish the game, play it via ROBLOX(not the studio) and try to use the item, you won't fly.
Any ideas why?
It's been a long time since I haven't played ROBLOX, so my answer might be inaccurate.
First, you need to understand the difference between a Script and a Localscript. In a nutshell, a script runs server-side whereas a Localscript runs client-side. Your fly script is expected to run on a player's client, thus you have to use a Localscript.
Your regular script works in build-mode because scripts are run on your client, and thus are considered as Localscripts. When you publish your game and play it, your computer doesn't work as a server anymore, but as a client.
In addition to this, as you use a localscript, you may have to change the following line :
-- gets the client (local player) on which the script is running
User = Game:GetService("Players").LocalPlayer
Also, avoid relative paths such as script.Parent.Parent.Parent..... You want to get the client's torso, so just use your User variable as per : User.Character.Torso. Do the same for the character's Humanoid object.
I think it's because the time to load not existing try this at line1:
repeat wait() until game.Players.LocalPlayer.Character

Memory is not released after a dofile in lua

I am new to lua and was working on NodeMCU. I was trying to extract data from an xml file.
Here is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<netconfig>
<mode>0</mode>
<stamac>18-FE-34-A4-4B-05</stamac>
<staip>XXX.XXX.XXX.XXX</staip>
<stanetmask>XXX.XXX.XXX.XXX</stanetmask>
<stagateway>XXX.XXX.XXX.XXX</stagateway>
<apmac>1A-FE-34-A4-4B-05</apmac>
<apip>192.168.4.1</apip>
<apnetmask>255.255.255.0</apnetmask>
<apgateway>192.168.4.1</apgateway>
<port>80</port>
<dns>XXX.XXX.XXX.XXX</dns>
<dhcp>1</dhcp>
<stacustomconfig></stacustomconfig>
<timezone>10</timezone>
<serial>0x00000001</serial>
<connssid>ESP-10767109</connssid>
<ssid></ssid>
<passwd></passwd>
<hostname>ESP-10767109</hostname>
<reboot></reboot>
<message></message>
</netconfig>
Here is my xmlparser:
return function (xmlfile, xmlword)
file.open(xmlfile,"r")
local eofflag = 0
local i, j, k, l, xmloutput
while(eofflag < 1) do
local m = file.readline()
if(m == nil) then
eofflag = eofflag + 1
elseif (string.find(m, xmlword) ~= nil) then
i, j = string.find(m, xmlword, 1)
i = i - 1
j = j + 2
k, l = string.find(m, xmlword, j)
k = k - 3
l = l + 1
xmloutput = string.sub(m, j, k)
eoffile = 1
end
end
file.close()
return xmloutput
end
I call this file by using:
local port = dofile("xmlparser.lc")("netconfig.xml", "port")
But I always endup with memory less than I started with even though I call the garbagecollector. Moreover The heap seems to decrease further if the word I am searching for is near the end of file. I also observed that if the word I am looking for is not present, the heap has the less difference I started with.
Am I missing something?
and thanks for taking a look.
Alternatively, is the XML strictly for settings/only used by your lua code? If so I found it much easier and less memory and compute intensive to create the settings file in lua syntax, and then simply execute it. Declare a global table and exec the file:
cfg = {}
dofile("settings.lua")
and in the settings.lua file assign members:
cfg.port = "80"
cfg.dhcp = "1"
cfg.mode = "0"
You can write the table to file easily:
local buf = ""
for mykey,myval in pairs(cfg) do
buf = "cfg." .. mykey .. " = \"" .. myval .. "\""
file.writeline(buf)
end
For what it's worth...
Slight changes to your module:
local module =...
return function (xmlfile, xmlword)
file.open(xmlfile,"r")
local eofflag = 0
local i, j, k, l, xmloutput
while(eofflag < 1) do
local m = file.readline()
if(m == nil) then
eofflag = eofflag + 1
elseif (string.find(m, xmlword) ~= nil) then
i, j = string.find(m, xmlword, 1)
i = i - 1
j = j + 2
k, l = string.find(m, xmlword, j)
k = k - 3
l = l + 1
xmloutput = string.sub(m, j, k)
eoffile = 1
end
end
file.close()
package.loaded[module] = nil
module = nil
return xmloutput
end
That makes the whole thing local and dereferences the module in the packages.loaded table, allowing it to be garbage collected.
And call it using...
xmplarser = require("xmplarser")
v = xmplarser("netconfig.xml", "port")
Hope it helps.

Ruby String Arrays Overwriting Themselves?

For some reason, string is getting rewritten as mirror by the operation mirror[j] = string[i]
I have no idea why this would be, I'm not commanding it to. The outputs of my reverse(string) function will stop at the middle, like:
ABCDEF -> FEDDEF
but I'm trying to do:
ABCDEF -> FEDCBA
def reverse(string)
mirror = string
i = string.length - 1
j = 0
while j < string.length
mirror[j] = string[i]
puts(mirror)
j = j + 1
i = i - 1
end
return mirror
end
You need to use #dup. In your case, mirror = string means both the variables are holding the reference of the same object. So, when you change the object, that can be reflected through both string and mirror.
def reverse(string)
mirror = string.dup
i = string.length - 1
j = 0
while j < string.length
mirror[j] = string[i]
j += 1 # same as j = j + 1
i -= 1 # same as i = i - 1
end
mirror
# last expression is returned by default, so you can write only `mirror` instead of `return mirror`
end
p reverse 'ABCDEF' # "FEDCBA"

Resources