This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Konlab's profile picture

Saving threads

Started by Konlab, 04 September 2015 - 04:11 PM
Konlab #1
Posted 04 September 2015 - 06:11 PM
Hi!
How can I convert threads to strings or anything save-able and convert back?
Thanks!
Edited on 04 September 2015 - 04:21 PM
KingofGamesYami #2
Posted 04 September 2015 - 06:15 PM
You can't.
Konlab #3
Posted 04 September 2015 - 06:20 PM
You can't.
:'(
Thanks for replying
Konlab #4
Posted 04 September 2015 - 06:30 PM
Idea/question:
If I save all events what happened can I simulate a thread reload?
Like this:
allevents will be a global table
os.pullEventRaw:
 function os.pullEventRaw()
local event = {coroutine.yield()}
allevents[#allevents+1] = event
return unpack(event)
end
os.hibernate:
Saving allevents in ".events.log"
startup:
Declaring global allevents
If exists .events.log then queue every event in it

Can this work?
KingofGamesYami #5
Posted 04 September 2015 - 06:45 PM
In most cases, yes. For some stuff, no (specifically, anything that uses math.random).
Konlab #6
Posted 04 September 2015 - 07:12 PM
In most cases, yes. For some stuff, no (specifically, anything that uses math.random).
Save all random values
(
Also I'm thinking about files
When restoring the fs api ruins it. Like if you have a file manager click the first file then delete then hibernate)
As typing i got an idea:
Or just saving all files at the beginning when turning on the computer and when restoring first restoring the original file structure then restore

Edit:
Or save when hibernating and disable fs API when restoring. But when using this make a lua app run it and delete it hibernate and crash. :(/> but if you do it at the beginning external editors ruin it.
Edited on 04 September 2015 - 05:20 PM
HDeffo #7
Posted 06 September 2015 - 06:04 AM

--#assume foo is the function you create the coroutine from
coroutine = string.dump(foo) --#saved as a string

coroutine = loadstring(coroutine) --#loaded as a function again again
you may also use setfenv to keep the persistence of it in terms of variables but that would take a bit more work. thats about as close as you can get to saving a thread
Edited on 06 September 2015 - 04:05 AM
Konlab #8
Posted 06 September 2015 - 08:09 AM
Thanks for replying HDeffo, I'll try this:
Save filesystem at the beginning
Save events
Save all global variables and functions when hibernating
Save filesystem when hibernating
Restore:
Restore filesystem at the beginning
Queue all events + 1 end event
If the coroutine manager gets the end event it'll compare global variables to the saved gl. Vars and the filesystem too.
If something is missing or there are extra files it'll restore the saved filesystem when hibernating and reboot to avoid data loss
flaghacker #9
Posted 07 September 2015 - 09:55 PM
You should consider stuff like disk.isPresent, os.time as well.
Konlab #10
Posted 08 September 2015 - 01:25 PM
Yes, it's a lot of work and disk space for that little thing…