Skip to main content
GameDev.net gamedev.net
🔒 Locked

Javascript - Reading and writing files (possible?)

Started by Darkbouncer4689 Aug 25, 2011 at 1:51 AM 3 replies 1.8k views
Original Post
Darkbouncer4689
Darkbouncer4689
Hey all,

So I know that there are a lot of security risks to allowing javascript code in a browser to access the hard drive and thus it isn't allowed. This is a problem for me as I am currently implementing a terrain system for a 3D browser game. Most terrain systems stream data from the hard drive as needed since the entire terrain is too large to fit into DRAM.

I wonder if there is any way around this other than having very small terrain files? Is there some type of virtual memory that can be accessed and isn't sitting in DRAM?

What if user's pre-download a file? Are there ways to read/write to certain local files?

As a third option I suppose the server could send data as needed to the client. This may be the best (or only solution) to this problem. However, for networked games there will already be a strain on what can be sent across the wire, so this may not be the optimal solution.

Any pointers or "thinking outside of the box" is appreciated.

Thanks!
Antheus
Antheus
What if user's pre-download a file? Are there ways to read/write to certain local files?[/quote]
Not really.

The HTML5 spec has local storage, but it tends to be either 0 or isn't supported or implemented yet. And it is around 5MB per application at best.

Browsers and JS/HTML simply aren't a viable platform for this type of project.

Use Unity instead.
Adam_42
Adam_42
You could procedurally generate the terrain based on a few parameters, instead of storing it all in memory. That would mean the download size is minimal.

You could also add procedural detail to a low resolution heightfield to get more control over the appearance.
Telgin
Telgin
Unfortunately, you're probably out of luck. There are tremendous security risks involved if Javascript could access local files, and there is no API for this sort of thing in any Javascript implementation in any web browser I'm aware of. Modern browsers won't even tell you the directory of a file you're trying to upload and will mask the file path.

If you're willing to force users to download the game, that changes. I'm pretty sure Chrome at least will allow you to access file:/// URLs if you launch it with the right command line arguments and are loading a file from file:/// (any file local to the computer). Firefox might be similar but I wouldn't be surprised if IE wouldn't let you do it.

A more complete solution if you want to actually package the game would be to use something like Titanium (Google for it). Titanium is a collection of APIs, language interpreters and a packager that lets you build desktop applications using a browser, Javascript, PHP and more. It supports reading local files, even outside of file:///, and you could probably port most of your code with little difficulty. My last experience with Titanium was that its documentation sucked and it was a buggy mess though, but maybe that has improved.
Success requires no explanation. Failure allows none.
Darkbouncer4689
Darkbouncer4689
Thanks for the help guys. I think I will try a combination of procedurally generating terrain as well as just having a sandboxed environment.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.