javascriptc#uwp

Issue with JSON in UWP WebView using C# to JavaScript not being read properly [DEPRECATED]


IMPORTANT NOTICE: The information relative to this question is deprecated and is for historical reference only. Although the matter in question was resolved as of when the answer was posted, the development paradigm being discussed is no longer supported or accepted for its intended use.

ORIGINAL QUESTION FOLLOWS:

This is rather perplexing. I'm converting an HTML5 game to UWP by using a WebView to host the game content and would like to back up from localStorage in the WebView and create a duplicate in the localStorage for the app as a whole (that is, from browser storage to package storage). This is of course an intended safety net in case the player craps out on hardware (as I obviously want the data to back up to the player's cloud storage) however something's not quite right. Specifically, the backups are written properly but when they're read back from the host app into the HTML5 portion using Javascript methods connected to the host through a WinRT component they're not having an effect on the localStorage in the WebView (which in turn makes it look like there's no save data, so the continue option is disabled).

Here's a code sample for what I'm doing:

  var i;
  for (i = -1; i <= 200; i++) {
      var data = window.UWPConnect.getSaveFile(i);
      var key = 'File'+i;
      if (i === -1) key = 'Config';
      if (i === 0) key = 'Global';
      localStorage.setItem(key, data);
  }

This first portion reads my WinRT component to load save data from the disk.

public void doSave(int key, string data) {
    /*StorageFolder folder = ApplicationData.Current.LocalFolder;
    StorageFile saveWrite = await folder.CreateFileAsync("saveData" + key + ".json", CreationCollisionOption.ReplaceExisting);
    try { await saveWrite.DeleteAsync(); } catch { }
    String[] lines = { data };
    await FileIO.WriteLinesAsync(saveWrite, lines);*/
    if (key == -1) { System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\config.json", data); }
    else if (key == 0) { System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\global.json", data); }
    else
    {
        System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\saveData" + key + ".json", data);
    }
}
public void doStartup()
    {
    StorageFolder folder = ApplicationData.Current.LocalFolder;
    for (int i = -1; i <= 200; i++)
    {
        try {
            if (i == -1)
            {
                saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\config.json");
            } else if (i == 0)
            {
                saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\global.json");
            }
            else
            {
                saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\saveData" + i + ".json");

            }
            Debug.WriteLine(saveData[i]);
        }
        catch {}
        /*Debug.WriteLine(File.Exists(SaveFile));
        if (File.Exists(SaveFile)) {
            try { saveData[i] = File.ReadAllText(SaveFile);
            Debug.WriteLine(saveData[i]); }
            catch { }
        }*/
    }
}
public string getSaveFile(int savefileId)
{
    string data;
    try
    {
        data = saveData[savefileId];
        if (data == null) data = "";
        Debug.WriteLine(data);
    }
    catch { data = ""; }
    return data;
}

And this second portion handles the saving and loading from disk (which is taken from the WinRT component).


Solution

  • I think I figured it out. In the data reader where it loads and saves I had to use a different set of instructions on the WinRT side, so here's the new code structure:

    public void doSave(int key, string data) {
        if (key == -1) { System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\config.json", data); }
        else if (key == 0) { System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\global.json", data); }
        else
        {
            System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\file" + key + ".json", data);
        }
    }
    public void doBackup(int key, string data) {
        System.IO.File.WriteAllText(ApplicationData.Current.LocalFolder.Path + "\\backup" + key + ".json", data);
    }
    public void doStartup()
        {
        for (int i = 0; i <= 200; i++)
        {
            if (i == -1)
            {
                if (System.IO.File.Exists(ApplicationData.Current.LocalFolder.Path + "\\config.json"))
                {
                    saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.RoamingFolder.Path + "\\config.json");
                }
            }
             else if (i == 0)
            {
            if (System.IO.File.Exists(ApplicationData.Current.LocalFolder.Path + "\\global.json"))
            {
                saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\global.json");
            }
        }
        else
        {
            if (System.IO.File.Exists(ApplicationData.Current.LocalFolder.Path + "\\file" + i + ".json"))
            {
                saveData[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\file" + i + ".json");
            }
            if (System.IO.File.Exists(ApplicationData.Current.LocalFolder.Path + "\\backup" + i + ".json"))
            {
                backup[i] = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\backup" + i + ".json");
            }
    
        }
        }
    }
    public string getSaveFile(int savefileId)
    {
        string data;
        try
        {
            data = saveData[savefileId];
            if (data == null) data = "";
            Debug.WriteLine(data);
        }
        catch { data = ""; }
        return data;
    }
    public string getBackup(int savefileId)
    {
        string data;
        try
        {
            data = backup[savefileId];
            if (data == null) data = "";
            Debug.WriteLine(data);
        }
        catch { data = ""; }
        return data;
    }
    public string getConfig()
    {
        string data;
        try
        {
            data = System.IO.File.ReadAllText(ApplicationData.Current.LocalFolder.Path + "\\config.json");
            if (data == null) data = "";
            Debug.WriteLine(data);
        }
        catch { data = ""; }
        return data;
    }