Using #PB_LocalStorage with files??? (RESOLVED)

Advanced game related topics
User avatar
DanLJr
Posts: 58
Joined: Wed Jul 04, 2018 4:24 am
Location: USA

Using #PB_LocalStorage with files??? (RESOLVED)

Post by DanLJr »

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.

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
The above code creates a download file just fine.

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
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!
Last edited by DanLJr on Sat Sep 21, 2019 9:15 pm, edited 1 time in total.
-Dan L.
:: falling on my face a lot lately; learning even more because of it! ::
User avatar
DanLJr
Posts: 58
Joined: Wed Jul 04, 2018 4:24 am
Location: USA

Re: Using #PB_LocalStorage with files???

Post by DanLJr »

I eventually went "back to the drawing board" and re-wrote a test version from scratch. (Starting with a few essentials from my other code and the help files.) I got it to work.

Here is a working standalone example of using LocalStorage to read and write data using the browser cache (it's a simple "write it then read it" example, but it proves the concept!):

Code: Select all

EnableExplicit

#WorkingSavedGamesFilename$ = "skytest"
Global WorkingFileData$ = "This is a test."
#FileToWrite = 0
#FileToRead = 1

Procedure SavedGamesReadCallback(Status, Filename$, File, Size)
  If Status = #PB_Status_Loaded
    Debug "File: " + Filename$ + " - Size: " + Size + " bytes"
    
    Protected WorkingImportText$ = ReadString(#FileToRead, #PB_File_IgnoreEOL)
    CloseFile(#FileToRead)
            
    Debug WorkingImportText$
            
  ElseIf Status = #PB_Status_Error
    Debug "Error when loading the file: " + Filename$
  EndIf
EndProcedure

Procedure SavedGamesWriteCallback(Status, Filename$, File, SizeRead)
  
  Select Status
    Case #PB_Status_Saved
      ; File correctly saved
      Debug "The file: '" + #WorkingSavedGamesFilename$ + "' has been created!"
            
      OpenFile(#FileToRead, #WorkingSavedGamesFilename$, @SavedGamesReadCallback(), #PB_LocalStorage)      
      
    Case #PB_Status_Error
      Debug "The file: '" + #WorkingSavedGamesFilename$ + "' could not be created."
  EndSelect
    
EndProcedure

Procedure Main()
  If CreateFile(#FileToWrite, #WorkingSavedGamesFilename$, @SavedGamesWriteCallback(), #PB_LocalStorage) ; we create a new text file and run the callback to populate and export
    Debug "Initialized creation of file: '" + #WorkingSavedGamesFilename$ + "'. Success! Awaiting callback..."
    WriteString(#FileToWrite, WorkingFileData$) ;dump content into file
    ;ExportFile(#FileToWrite, "text/plain")
    CloseFile(#FileToWrite)
  Else
    Debug "Could not create file: '" + #WorkingSavedGamesFilename$ + "'. Failed."
  EndIf
    
EndProcedure

Main()

If 1 = 2
  SavedGamesReadCallback(0, "", 0, 0)
  SavedGamesWriteCallback(0, "", 0, 0)
EndIf
-Dan L.
:: falling on my face a lot lately; learning even more because of it! ::
User avatar
Niffo
Posts: 80
Joined: Sat Jun 16, 2018 10:30 pm

Re: Using #PB_LocalStorage with files??? (RESOLVED)

Post by Niffo »

Spam ?
User avatar
DanLJr
Posts: 58
Joined: Wed Jul 04, 2018 4:24 am
Location: USA

Re: Using #PB_LocalStorage with files??? (RESOLVED)

Post by DanLJr »

Niffo wrote:Spam ?
I don't know if I'd call it SPAM. It was a legit question. I just didn't get any quick responses and couldn't find the error, so I started over and re-wrote from scratch, eventually getting it to work, so I marked my own post as resolved, and shared the results with the group, in-case others run into similar issues.

My understanding is that it is DESIRED to post code that might be helpful for others experiencing similar challenges. I would not call that SPAM. (And it didn't come from some random anonymous source, it came from me, an active, current SB and PB developer working on specific projects in SB and posting and answering during the course of work.)

Anyhow, I'm not offended or anything - fair question - but I did want to clarify that this started as a legit topic/question, and was NOT intended to be a one-man conversation posing as SPAM. LOL ;)
-Dan L.
:: falling on my face a lot lately; learning even more because of it! ::
User avatar
Niffo
Posts: 80
Joined: Sat Jun 16, 2018 10:30 pm

Re: Using #PB_LocalStorage with files??? (RESOLVED)

Post by Niffo »

There was a SPAM, but it has been deleted ... and my post stayed ;)
Post Reply