Search Unity

[WebGL] PlayerPrefs and other data in indexedDB is not saved when system clock is rewound

Discussion in 'Web' started by DuckOfDoom, Jul 21, 2016.

  1. DuckOfDoom

    DuckOfDoom

    Joined:
    Apr 15, 2012
    Posts:
    7
    Hello. We're using Unity 5.3.5f1 for our WebGL builds and we stumbled upon an interesting issue.

    When user rewinds his system time on his computer after writing something to PlayerPrefs or file (using File.WriteAllText, for example), he no longer can owerwrite the file until he sets system clock to something greater than the time of saving.

    To reproduce we figured out following steps (in Firefox and Chrome on Windows machines):

    1. Start the player, write DateTime.Now.ToString() to PlayerPrefs or file
    2. Read something from that file to chek if it is saved (works as intended).
    3. Close the player and rewind the system clock, say, to 1 day in the past.
    4. Start the player, write new rewound date to PlayerPrefs.
    5. Check that it reads right. To this moment here everything works fine.
    6. Restart the player, read the saved date and you'll see the date from (1), that was in the future and didn't get overwritten by our save in (4).

    This produces all nasty behaviour when players dont have their progress saved if they rewind the clock somehow.

    I tried callings FS.syncfs() after writing to PlayerPrefs and files but this doesnt seem to help.

    I can upload the sample project to reproduce the issue if needed.
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    We have some code in place to make sure we don't write to the file system too often, as that has been causing issues with some browsers (every write will go through, but it may be delayed in some cases, and multiple writes may be done only once). This code may get confused if you change the system time.

    But, it seems pretty odd that your users would want to change the system time while playing your game. What is the use case here?
     
  3. DuckOfDoom

    DuckOfDoom

    Joined:
    Apr 15, 2012
    Posts:
    7
    Thank you for fast reply!

    Our game uses a lot of time-sensitive mechanics (like daily events and timed bonuses) which are implemented using network time. But users may be tempted to try to cheat the system by setting clock on their machines some time forward and backward(since the game is a port of mobile game and on mobile such cheating can deem working).

    It IS and odd case but since out QA guys found this, I wanted to just make sure I'm doint everything right.

    If this is an intended behaviour, than no problems. I was just worried that this was some kind of bug with Mono or something.
     
  4. juicybeast

    juicybeast

    Joined:
    Nov 12, 2014
    Posts:
    32
    Hello, we ran into the same problem. After testing our time cheating prevention system, we had a hard time determining why we couldn't save anymore. I get that you must use a system to prevent rapid overwriting, but I think it may be a bad idea to be this unflexible in an engine used by so many people in so many different contexts accomplishing so many different things?

    At least maybe put a warning or something so devs don't lose time trying to figure why their game don't save anymore?
     
  5. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    291
    Oh wow, this would also explain a ton of bug reports I've been getting.

    My game also has a lot of time sensitive stuff in it so players try to cheat that, they also play a bunch of other web games that they *can* cheat by messing with the system time.

    I save the player progress to an encrypted player pref ... if the indexed DB write won't work if they happen to have rewound the system time then they'll lose progress in the game. We *have* to be able to rely on the filesystem working, this is a huge issue!
     
  6. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    291
    Can we get an update from Unity on this issue please? For now I've had to rewrite the save game code to use localstorage instead of player prefs given that there's no way to know if a save prefs actually does anything.