Using #PB_LocalStorage with files??? (RESOLVED)
Posted: Fri Sep 20, 2019 10:04 pm
I'm having trouble utilizing local storage to save and load file data. I have it working perfectly with Local File saving a downloaded ,txt file (using the requester to re-upload) but when I try to do it through the browser cache (using #PB_LocalStorage) and not going through the requester, it SEEMS to create my file when I save (my code reports it worked - no errors - in the callback) but when I try to load using a fixed filename and the #PB_LocalStorage, it does not seem to reload my file at all...it's like it's just not there.
The above code creates a download file just fine.
The above code loads a previously downloaded file just fine. (Requires the user to pick the file from the dialog, and then it proceeds to load it, no problem.)
HOWEVER, If I modify the above code to use the same fixed file name both for reading and writing along with #PB_LocalStorage and no file selector dialog for loading, it simply does not load the file from the cache.
Could someone give me a working example of reading and writing a fixed filename (will always be the same file; can just overwrite each time) using #PB_LocalStorage so that the data is stored within the browser cache? I've tried every possible iteration of this following the help files and such for SB, and while I was able to get it working with the Local Files (which actually required more code/callbacks, etc. I was never able to make it work with the #PB_LocalStorage way.) Currently, my save slots exist in RAM, and I can manually export and import those save slots as a file (like a backup/archive) but I cannot persist in the cache, which is what I REALLY need to be doing here. (The downloading and uploading is more for archive/backup of save slots. I need "real-time" browser caching to really make this practical, so if they close, they can reload from saved slots without having to have made a manual export/download file to reload.)
Any help would be highly appreciated!
Code: Select all
If CreateFile(0, #WorkingSavedGamesFilename$, @SavedGamesWriteCallback()) ; we create a new text file and run the callback to populate and export
WriteString(0, WorkingFileData$) ;dump content into file
ExportFile(0, "text/plain")
CloseFile(0)
ProcedureReturn "Ok"
EndIf
Code: Select all
Procedure SavedGamesReadCallback(Status, Filename$, File, Size)
If Status = #PB_Status_Loaded
Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
Protected WorkingImportText$ = ReadString(0, #PB_Ascii + #PB_File_IgnoreEOL)
CloseFile(0)
CloseWindow(#WindowFileRequester)
WriteUserTextConsole(SaveGameS(#LoadFromSavedGamesDownloadFile$, #False, WorkingImportText$))
ElseIf Status = #PB_Status_Error
Debug "Error when loading the file: " + Filename$
EndIf
EndProcedure
Procedure OpenFileRequesterCallback()
If NextSelectedFile()
OpenFile(0, SelectedFileID(), @SavedGamesReadCallback(), #PB_LocalFile)
EndIf
EndProcedure
Procedure Event_ChooseFile()
OpenFileRequester("*.txt", @OpenFileRequesterCallback())
EndProcedure
Procedure SelectSavedGameFileToRestore()
OpenWindow(#WindowFileRequester, 0, 0, 300, 50, "Load " + #GameTitle$ + " Saved Games from File", #PB_Window_ScreenCentered)
ButtonGadget(#ChooseFileButton, 10, 10, 280, 30, "Choose a file...")
BindGadgetEvent(#ChooseFileButton, @Event_ChooseFile())
EndProcedure
HOWEVER, If I modify the above code to use the same fixed file name both for reading and writing along with #PB_LocalStorage and no file selector dialog for loading, it simply does not load the file from the cache.
Could someone give me a working example of reading and writing a fixed filename (will always be the same file; can just overwrite each time) using #PB_LocalStorage so that the data is stored within the browser cache? I've tried every possible iteration of this following the help files and such for SB, and while I was able to get it working with the Local Files (which actually required more code/callbacks, etc. I was never able to make it work with the #PB_LocalStorage way.) Currently, my save slots exist in RAM, and I can manually export and import those save slots as a file (like a backup/archive) but I cannot persist in the cache, which is what I REALLY need to be doing here. (The downloading and uploading is more for archive/backup of save slots. I need "real-time" browser caching to really make this practical, so if they close, they can reload from saved slots without having to have made a manual export/download file to reload.)
Any help would be highly appreciated!