Skip to content

Commit

Permalink
Fix crashes when attempting to import invalid pxo files
Browse files Browse the repository at this point in the history
  • Loading branch information
OverloadedOrama committed Feb 20, 2024
1 parent b2f511c commit 3f6e138
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ config/icon="res://assets/graphics/icons/icon.png"
config/macos_native_icon="res://assets/graphics/icons/icon.icns"
config/windows_native_icon="res://assets/graphics/icons/icon.ico"
config/custom_user_dir_name.X11="pixelorama"
config/Version="v0.11.4-rc1"
config/Version="v0.11.4-rc2"
config/ExtensionsAPI_Version=3
config/Pxo_Version=2

Expand Down
72 changes: 35 additions & 37 deletions src/Autoload/OpenSave.gd
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ func open_pxo_file(path: String, untitled_backup: bool = false, replace_empty: b
file.close()
return

var first_line := file.get_line()
var dict := JSON.parse(first_line)
if dict.error != OK:
print("Error, corrupt pxo file")
file.close()
return
if typeof(dict.result) != TYPE_DICTIONARY:
print("Error, json parsed result is: %s" % typeof(dict.result))
file.close()
return
var empty_project: bool = Global.current_project.is_empty() and replace_empty
var new_project: Project
if empty_project:
Expand All @@ -142,43 +152,31 @@ func open_pxo_file(path: String, untitled_backup: bool = false, replace_empty: b
else:
new_project = Project.new([], path.get_file())

var first_line := file.get_line()
var dict := JSON.parse(first_line)
if dict.error != OK:
print("Error, corrupt pxo file")
file.close()
return
else:
if typeof(dict.result) != TYPE_DICTIONARY:
print("Error, json parsed result is: %s" % typeof(dict.result))
file.close()
return

new_project.deserialize(dict.result)
for frame in new_project.frames:
for cel in frame.cels:
cel.load_image_data_from_pxo(file, new_project.size)

if dict.result.has("brushes"):
for brush in dict.result.brushes:
var b_width = brush.size_x
var b_height = brush.size_y
var buffer := file.get_buffer(b_width * b_height * 4)
var image := Image.new()
image.create_from_data(b_width, b_height, false, Image.FORMAT_RGBA8, buffer)
new_project.brushes.append(image)
Brushes.add_project_brush(image)

if dict.result.has("tile_mask") and dict.result.has("has_mask"):
if dict.result.has_mask:
var t_width = dict.result.tile_mask.size_x
var t_height = dict.result.tile_mask.size_y
var buffer := file.get_buffer(t_width * t_height * 4)
var image := Image.new()
image.create_from_data(t_width, t_height, false, Image.FORMAT_RGBA8, buffer)
new_project.tiles.tile_mask = image
else:
new_project.tiles.reset_mask()
new_project.deserialize(dict.result)
for frame in new_project.frames:
for cel in frame.cels:
cel.load_image_data_from_pxo(file, new_project.size)

if dict.result.has("brushes"):
for brush in dict.result.brushes:
var b_width = brush.size_x
var b_height = brush.size_y
var buffer := file.get_buffer(b_width * b_height * 4)
var image := Image.new()
image.create_from_data(b_width, b_height, false, Image.FORMAT_RGBA8, buffer)
new_project.brushes.append(image)
Brushes.add_project_brush(image)

if dict.result.has("tile_mask") and dict.result.has("has_mask"):
if dict.result.has_mask:
var t_width = dict.result.tile_mask.size_x
var t_height = dict.result.tile_mask.size_y
var buffer := file.get_buffer(t_width * t_height * 4)
var image := Image.new()
image.create_from_data(t_width, t_height, false, Image.FORMAT_RGBA8, buffer)
new_project.tiles.tile_mask = image
else:
new_project.tiles.reset_mask()

file.close()
if empty_project:
Expand Down

0 comments on commit 3f6e138

Please sign in to comment.