Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Beuc committed Dec 14, 2019
1 parent 41ba770 commit a8f3a10
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
1 change: 1 addition & 0 deletions launcher/game/web.rpy
Expand Up @@ -87,6 +87,7 @@ init python:
# Filter out downloadable resources
MIN_REMOTE_SIZE=100*1024
# TODO: configurable min_size? use archives to better describe web distribution?
# TODO: predict/include title screen's resources for a smooth start (no black blink)
shutil.move(
os.path.join(destination, 'game.zip'),
os.path.join(destination, 'game-old.zip'))
Expand Down
2 changes: 1 addition & 1 deletion renpy/display/core.py
Expand Up @@ -2791,7 +2791,7 @@ def idle_frame(self, can_block, expensive):
elif step == 2:

if renpy.emscripten or os.environ.get('RENPY_SIMULATE_DOWNLOAD', False):
renpy.loader.ReloadRequest.load_downloaded_resources()
renpy.loader.ReloadRequest.process_downloaded_resources()

step += 1

Expand Down
49 changes: 30 additions & 19 deletions renpy/loader.py
Expand Up @@ -45,27 +45,34 @@
xhrs: {},
dl_new: function(path) {
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
// Create file reusing XHR's buffer (no-copy)
try { FS.unlink(path); } catch {}
FS.writeFile(path, new Uint8Array(xhr.response), {canOwn:true});
}
xhr.open('GET', path);
xhr.send();
RenPyWeb.xhrs[RenPyWeb.xhr_id] = xhr;
var ret = RenPyWeb.xhr_id;
RenPyWeb.xhr_id++;
return ret;
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.onerror = function() {
console.log("Network error", xhr);
}
xhr.onload = function() {
if (xhr.status == 200 || xhr.status == 304 || xhr.status == 206 || (xhr.status == 0 && xhr.response)) {
// Create file reusing XHR's buffer (no-copy)
try { FS.unlink(path); } catch {}
FS.writeFile(path, new Uint8Array(xhr.response), {canOwn:true});
} else {
console.log("Download error", xhr);
}
}
xhr.open('GET', path);
xhr.send();
RenPyWeb.xhrs[RenPyWeb.xhr_id] = xhr;
var ret = RenPyWeb.xhr_id;
RenPyWeb.xhr_id++;
return ret;
},
dl_get: function(xhr_id) {
return RenPyWeb.xhrs[xhr_id];
return RenPyWeb.xhrs[xhr_id];
},
dl_free: function(xhr_id) {
delete RenPyWeb.xhrs[xhr_id];
delete RenPyWeb.xhrs[xhr_id];
// Note: xhr.response kept alive until file is deleted
},
}
Expand Down Expand Up @@ -145,21 +152,25 @@ def download_completed(self):
return self.xhr is not None and self.xhr.readyState == 4

@staticmethod
def load_downloaded_resources():
def process_downloaded_resources():
postponed = []
reloaded = False

for rr in ReloadRequest.all:
# TODO: support images and sounds
if hasattr(rr, 'image'):
if rr.download_completed():
if rr.xhr.status == 0:
raise IOError("Download error: " + rr.xhr.statusText)
fullpath = os.path.join(renpy.config.gamedir,rr.image.filename)
if not os.path.exists(fullpath):
# don't rethrow exception in Ren'Py's error handler
ReloadRequest.all.remove(rr)
# show Ren'Py's error handler
raise IOError("Download error: {}: {}".format(
rr.image.filename, (rr.xhr.statusText or "network error")))
#print("reloading", rr.image.filename)
ce = renpy.display.im.cache.cache.get(rr.image)
renpy.display.im.cache.kill(ce)
renpy.display.im.cache.get(rr.image, render=True)
fullpath = os.path.join(renpy.config.gamedir,rr.image.filename)
#print("unlink", fullpath)
os.unlink(fullpath)
reloaded = True
Expand Down

0 comments on commit a8f3a10

Please sign in to comment.