Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Read txt files. Pls help!

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
dan013
Cheater
Reputation: 0

Joined: 30 Dec 2023
Posts: 28

PostPosted: Wed Jan 03, 2024 9:04 pm    Post subject: Read txt files. Pls help! Reply with quote

I'm trying to make a pose trainer. The thing was I needed a save and load function. I can already save the value of it and store it via a txt file but when I reload the trainer it will be gone. I don't know what's wrong with my code. Can someone please help me? Thanks.

Here is a snippet of my code

Code:
--Save and Delete poses
function loadTableCode(n)
    local t = findTableFile(n)
    if t ~= nil then
        local s = t.Stream
        local c = readStringLocal(s.Memory, s.Size)
        return c ~= nil and loadstring(c)()  -- load and execute the code
    end
end

-- Load the serpent.lua code
local serpentLoaded, serpent = pcall(function()
    return loadTableCode('serpent.lua')
end)

if serpentLoaded then
    print('serpent.lua loaded successfully!')
else
    print('Error loading serpent.lua code:')
    print(serpent)
    return  -- exit the script if serpent.lua cannot be loaded
end

-- Initialize savedValues
savedValues = {}

-- Function to get the file path based on the save name
local function getFilePath(saveName)
    return string.format("C://poses/saved_values_%s.txt", saveName)
end

-- Function to save a single value to a file
function saveValueToFile(saveName, value)
    local filePath = getFilePath(saveName)
    local file = io.open(filePath, "w")
    if file then
        file:write(serpent.block(value, { comment = false }))
        file:close()
    end
end

-- Function to load a single value from a file
function loadValueFromFile(saveName)
    local filePath = getFilePath(saveName)
    local file = io.open(filePath, "r")
    if file then
        local contents = file:read("*all")
        file:close()
        local loadedValue = assert(loadstring(contents))()
        return loadedValue
    end
    return nil
end

-- Function to refresh the combo box with saved values
function refreshComboBox()
    CETrainer_CEComboBox1.clear()
    for saveName, _ in pairs(savedValues) do
        CETrainer_CEComboBox1.Items.add(saveName)
    end
end

function CETrainer_SaveClick(sender)
    local xValue = readFloat(RotXhip)
    local yValue = readFloat(RotYhip)
    local zValue = readFloat(RotZhip)

    -- Prompt the user for a name
    local saveName = inputQuery("Save", "Enter a name for the save:", "Save")
    if saveName then
        local newSave = { x = xValue, y = yValue, z = zValue, name = saveName }
        savedValues[saveName] = newSave
        -- Refresh the combo box
        refreshComboBox()
        -- Save the value to the file
        saveValueToFile(saveName, newSave)
    end
end

function CETrainer_CEComboBox1Change(sender)
    local saveName = CETrainer_CEComboBox1.Items[CETrainer_CEComboBox1.ItemIndex]
    local selectedValue = savedValues[saveName]
    if selectedValue then
        writeFloat(RotXhip, selectedValue.x)
        writeFloat(RotYhip, selectedValue.y)
        writeFloat(RotZhip, selectedValue.z)
    end
end

function CETrainer_DeleteClick(sender)
    local selectedIndex = CETrainer_CEComboBox1.ItemIndex
    local saveName = CETrainer_CEComboBox1.Items[selectedIndex]
    savedValues[saveName] = nil

    -- Refresh the combo box
    refreshComboBox()

    -- Delete the corresponding file
    local filePath = getFilePath(saveName)
    os.remove(filePath)
end
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1069
Location: 0x90

PostPosted: Wed Jan 03, 2024 9:13 pm    Post subject: Reply with quote

loadstring has been deprecated. But I think that you have misunderstood the use of loadstring, which takes a string of characters and loads it as a Lua chunk for execution.

Here is an example of how loadstring works:
Code:

local code = "print('Hello, World!')"
local chunk, errorMessage = loadstring(code, "exampleChunk")

if chunk then
    chunk()  -- Execute the loaded chunk
else
    print("Error loading chunk:", errorMessage)
end


In any case, perhaps this will work for you:
Code:

-- Function to save a single value to a file
function saveValueToFile(saveName, value)
    local filePath = getFilePath(saveName)
    local file = io.open(filePath, "w")
    if file then
        file:write(serpent.block(value, { comment = false }))
        file:close()
    end
end

-- Function to load a single value from a file
function loadValueFromFile(saveName)
    local filePath = getFilePath(saveName)
    local file = io.open(filePath, "r")
    if file then
        local contents = file:read("*all")
        file:close()

        -- Use serpent.load to deserialize the value
        local loadedValue, loadError = serpent.load(contents)
       
        if loadedValue then
            return loadedValue
        else
            print("Error loading file:", loadError)
        end
    end
    return nil
end
Back to top
View user's profile Send private message
dan013
Cheater
Reputation: 0

Joined: 30 Dec 2023
Posts: 28

PostPosted: Wed Jan 03, 2024 11:07 pm    Post subject: Reply with quote

LeFiXER wrote:
loadstring has been deprecated. But I think that you have misunderstood the use of loadstring, which takes a string of characters and loads it as a Lua chunk for execution.

Here is an example of how loadstring works:
Code:

local code = "print('Hello, World!')"
local chunk, errorMessage = loadstring(code, "exampleChunk")

if chunk then
    chunk()  -- Execute the loaded chunk
else
    print("Error loading chunk:", errorMessage)
end


In any case, perhaps this will work for you:
Code:

-- Function to save a single value to a file
function saveValueToFile(saveName, value)
    local filePath = getFilePath(saveName)
    local file = io.open(filePath, "w")
    if file then
        file:write(serpent.block(value, { comment = false }))
        file:close()
    end
end

-- Function to load a single value from a file
function loadValueFromFile(saveName)
    local filePath = getFilePath(saveName)
    local file = io.open(filePath, "r")
    if file then
        local contents = file:read("*all")
        file:close()

        -- Use serpent.load to deserialize the value
        local loadedValue, loadError = serpent.load(contents)
       
        if loadedValue then
            return loadedValue
        else
            print("Error loading file:", loadError)
        end
    end
    return nil
end



Sadly its still not working. My theory is the trainer will make a memory table and it will make another table on different memory after i reopen it. So to load it is to point out the new memory table which i dont know how to code it. Im still learning how to code lua...
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1069
Location: 0x90

PostPosted: Wed Jan 03, 2024 11:40 pm    Post subject: Reply with quote

Are you presented with any error? If so what error(s)?
Back to top
View user's profile Send private message
dan013
Cheater
Reputation: 0

Joined: 30 Dec 2023
Posts: 28

PostPosted: Thu Jan 04, 2024 12:27 am    Post subject: Reply with quote

LeFiXER wrote:
Are you presented with any error? If so what error(s)?


No error just it cant load the txt files i saved.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1522

PostPosted: Thu Jan 04, 2024 1:28 pm    Post subject: Reply with quote

To read the "txt" recording folder and list the names of all files with ".txt" extension in the folder in a table; Run this code when the trainer first starts.

Or use the parts of the code that are useful to you.


Code:
--If the user does not have a folder with the same name, create this folder yourself.
local tmp = os.getenv('temp')
path = tmp..[[\poses\]] -- or "C://poses/"
--print(path)
if path then os.execute("mkdir "..path) end

function scandir(directory,tbl)
    local i, popen = 0, io.popen
    local pfile = popen('dir "'..directory..'" /b')
    for filename in pfile:lines() do
     --print(filename)
         if (filename):find(".txt") then
           i = i + 1
           tbl[i] = string.gsub(filename,".txt","")
         end
    end
    pfile:close()
end
local nameTable = {}
--trainer open = execute ..
scandir(path,nameTable)
--test
for k,l in pairs(nameTable) do
 print(k,l) -- l
end

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
dan013
Cheater
Reputation: 0

Joined: 30 Dec 2023
Posts: 28

PostPosted: Thu Jan 04, 2024 4:08 pm    Post subject: Reply with quote

AylinCE wrote:
To read the "txt" recording folder and list the names of all files with ".txt" extension in the folder in a table; Run this code when the trainer first starts.

Or use the parts of the code that are useful to you.


Code:
--If the user does not have a folder with the same name, create this folder yourself.
local tmp = os.getenv('temp')
path = tmp..[[\poses\]] -- or "C://poses/"
--print(path)
if path then os.execute("mkdir "..path) end

function scandir(directory,tbl)
    local i, popen = 0, io.popen
    local pfile = popen('dir "'..directory..'" /b')
    for filename in pfile:lines() do
     --print(filename)
         if (filename):find(".txt") then
           i = i + 1
           tbl[i] = string.gsub(filename,".txt","")
         end
    end
    pfile:close()
end
local nameTable = {}
--trainer open = execute ..
scandir(path,nameTable)
--test
for k,l in pairs(nameTable) do
 print(k,l) -- l
end


Thanks I will try this later
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites