Skip to content

Commit

Permalink
FileAccess related fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Nov 12, 2023
1 parent 1a2ac80 commit 2ffc536
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 40 deletions.
10 changes: 5 additions & 5 deletions addons/material_maker/engine/loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func get_predefined_generators_from_dir(path : String) -> void:
while file_name != "":
if !dir.current_is_dir() and file_name.get_extension() == "mmg":
var file : FileAccess = FileAccess.open(path+"/"+file_name, FileAccess.READ)
if file.is_open():
if file != null:
var generator = string_to_dict_tree(file.get_as_text())
if CHECK_PREDEFINED:
if generator.has("shader_model") and generator.shader_model.has("global") and generator.shader_model.global != "":
Expand Down Expand Up @@ -66,7 +66,7 @@ func update_predefined_generators()-> void:
get_predefined_generators_from_dir(path)
if false:
var file : FileAccess = FileAccess.open("predefined_nodes.json", FileAccess.WRITE)
if file.is_open():
if file != null:
file.store_string(JSON.stringify(predefined_generators))
file.close()

Expand Down Expand Up @@ -170,7 +170,7 @@ func load_gen(filename: String) -> MMGenBase:

func save_gen(filename : String, generator : MMGenBase) -> void:
var file : FileAccess = FileAccess.open(filename, FileAccess.WRITE)
if file.is_open():
if file != null:
var data = generator.serialize()
data.name = filename.get_file().get_basename()
data.node_position = { x=0, y=0 }
Expand Down Expand Up @@ -310,7 +310,7 @@ func load_external_export_targets():
while file_name != "":
if file_name.get_extension() == "mme":
var file : FileAccess = FileAccess.open(USER_EXPORT_DIR.path_join(file_name), FileAccess.READ)
if file.is_open():
if file != null:
var export_data : Dictionary = string_to_dict_tree(file.get_as_text())
if export_data != {}:
var material : String = export_data.material
Expand All @@ -328,7 +328,7 @@ func get_external_export_targets(material_name : String) -> Dictionary:
func save_export_target(material_name : String, export_target_name : String, export_target : Dictionary) -> String:
var file_name : String = get_export_file_name(material_name, export_target_name)
var file : FileAccess = FileAccess.open(USER_EXPORT_DIR.path_join(file_name), FileAccess.WRITE)
if file.is_open():
if file != null:
export_target.name = export_target_name
file.store_string(dict_tree_to_string(export_target))
return file_name
Expand Down
2 changes: 1 addition & 1 deletion addons/material_maker/engine/nodes/gen_material.gd
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ func create_file_from_template(template : String, file_name : String, export_con
else:
DirAccess.remove_absolute(file_name)
var out_file = FileAccess.open(file_name, FileAccess.WRITE)
if ! out_file.is_open():
if out_file == null:
print("Cannot write file '"+file_name+"' ("+str(out_file.get_error())+")")
return false
out_file.store_string(processed_template)
Expand Down
2 changes: 1 addition & 1 deletion addons/material_maker/engine/preprocessor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func get_file(file_name : String, include_paths : Array = []) -> String:
shader_text = shader_files[p]
else:
var file = FileAccess.open(p, FileAccess.READ)
if file.is_open():
if file != null:
shader_text = file.get_as_text()
file = null
shader_files[file_name] = shader_text
Expand Down
2 changes: 1 addition & 1 deletion material_maker/locale/locale.gd
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func read_translations():
file_name = dir.get_next()
for fn in csv_files:
var f : FileAccess = FileAccess.open(LOCALE_DIR+"/"+fn, FileAccess.READ)
if f.is_open():
if f != null:
var l : String = f.get_line()
if l.left(2) == "id":
var separator = l[2]
Expand Down
31 changes: 16 additions & 15 deletions material_maker/main_window.gd
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,14 @@ func _on_LoadRecent_id_pressed(id) -> void:

func load_recents() -> void:
var f : FileAccess = FileAccess.open("user://recent_files.bin", FileAccess.READ)
if f.is_open():
if f != null:
var test_json_conv = JSON.new()
test_json_conv.parse(f.get_as_text())
recent_files = test_json_conv.get_data()

func save_recents() -> void:
var f : FileAccess = FileAccess.open("user://recent_files.bin", FileAccess.WRITE)
if f.is_open():
if f != null:
f.store_string(JSON.stringify(recent_files))

func add_recent(path, save = true) -> void:
Expand Down Expand Up @@ -552,7 +552,7 @@ func on_html5_load_file(file_name, _file_type, file_data):

func get_file_absolute_path(filename : String) -> String:
var file : FileAccess = FileAccess.open(filename, FileAccess.READ)
if ! file:
if file == null:
return ""
return file.get_path_absolute()

Expand Down Expand Up @@ -783,7 +783,7 @@ func edit_load_selection() -> void:
if files.size() == 1:
mm_globals.config.set_value("path", "selection", files[0].get_base_dir())
var file : FileAccess = FileAccess.open(files[0], FileAccess.READ)
if file.is_open():
if file != null:
var test_json_conv = JSON.new()
test_json_conv.parse(file.get_as_text())
graph_edit.do_paste(test_json_conv.get_data())
Expand All @@ -804,7 +804,7 @@ func edit_save_selection() -> void:
if files.size() == 1:
mm_globals.config.set_value("path", "selection", files[0].get_base_dir())
var file : FileAccess = FileAccess.open(files[0], FileAccess.WRITE)
if file.is_open():
if file != null:
file.store_string(JSON.stringify(graph_edit.serialize_selection()))
file.close()

Expand Down Expand Up @@ -978,18 +978,19 @@ func get_current_node(graph_edit : MMGraphEdit) -> Node:
func update_preview_2d() -> void:
var graph_edit : MMGraphEdit = get_current_graph_edit()
if graph_edit != null:
var previews : Array = [ get_panel("Preview2D"), get_panel("Preview2D (2)") ]
for i in range(2):
var preview = graph_edit.get_current_preview(i)
var generator : MMGenBase = null
var output_index : int = -1
if preview != null:
preview_2d[i].set_generator(preview.generator, preview.output_index)
if i == 0:
histogram.set_generator(preview.generator, preview.output_index)
preview_2d_background.set_generator(preview.generator, preview.output_index)
else:
preview_2d[i].set_generator(null)
if i == 0:
histogram.set_generator(null)
preview_2d_background.set_generator(null)
generator = preview.generator
output_index = preview.output_index
if previews[i] != null:
previews[i].set_generator(generator, output_index)
if i == 0:
histogram.set_generator(generator, output_index)
preview_2d_background.set_generator(generator, output_index)

var current_gen_material = null
func update_preview_3d(previews : Array, _sequential = false) -> void:
Expand Down Expand Up @@ -1150,7 +1151,7 @@ func on_files_dropped(files : PackedStringArray) -> void:
await get_tree().process_frame
for f in files:
var file : FileAccess = FileAccess.open(f, FileAccess.READ)
if ! file.is_open():
if file == null:
continue
f = file.get_path_absolute()
match f.get_extension():
Expand Down
2 changes: 1 addition & 1 deletion material_maker/nodes/generic/generic.gd
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func do_load_generator(file_name : String) -> void:
var new_generator = null
if file_name.ends_with(".mmn"):
var file : FileAccess = FileAccess.open(file_name, FileAccess.READ)
if file.is_open():
if file != null:
new_generator = MMGenShader.new()
var test_json_conv = JSON.new()
test_json_conv.parse(file.get_as_text())
Expand Down
4 changes: 2 additions & 2 deletions material_maker/panels/graph_edit/graph_edit.gd
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func crash_recovery_save() -> void:
i += 1
var data = top_generator.serialize()
var file : FileAccess = FileAccess.open(save_crash_recovery_path, FileAccess.WRITE)
if file.is_open():
if file != null:
file.store_string(JSON.stringify(data))
need_save_crash_recovery = false

Expand Down Expand Up @@ -634,7 +634,7 @@ func save_file(filename) -> bool:
JavaScriptBridge.download_buffer(JSON.stringify(data, "\t", true).to_ascii_buffer(), filename)
else:
var file : FileAccess = FileAccess.open(filename, FileAccess.WRITE)
if file.is_open():
if file != null:
file.store_string(JSON.stringify(data, "\t", true))
else:
return false
Expand Down
4 changes: 2 additions & 2 deletions material_maker/panels/library/library.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func _ready() -> void:

func init_expanded_items() -> void:
var f = FileAccess.open("user://expanded_items.bin", FileAccess.READ)
if f:
if f != null:
var test_json_conv = JSON.new()
test_json_conv.parse(f.get_as_text())
var json = test_json_conv.get_data()
Expand All @@ -61,7 +61,7 @@ func init_expanded_items() -> void:

func _exit_tree() -> void:
var f = FileAccess.open("user://expanded_items.bin", FileAccess.WRITE)
if f.is_open():
if f != null:
f.store_string(JSON.stringify(expanded_items))

func _unhandled_input(event : InputEvent) -> void:
Expand Down
2 changes: 1 addition & 1 deletion material_maker/tools/library_manager/library.gd
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func get_sections() -> Array:
func save_library() -> void:
DirAccess.open("res://").make_dir_recursive(library_path.get_base_dir())
var file : FileAccess = FileAccess.open(library_path, FileAccess.WRITE)
if file.is_open():
if file != null:
file.store_string(JSON.stringify({name=library_name, lib=library_items}, "\t", true))
file.close()

Expand Down
4 changes: 2 additions & 2 deletions material_maker/tools/library_manager/library_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func _exit_tree():
return
DirAccess.open("res://").make_dir_recursive(item_usage_file.get_base_dir())
var file = FileAccess.open(item_usage_file, FileAccess.WRITE)
if file.is_open():
if file != null:
file.store_string(JSON.stringify(item_usage, "\t", true))

# Libraries
Expand Down Expand Up @@ -262,7 +262,7 @@ func save_aliases() -> void:
return
DirAccess.open("res://").make_dir_recursive(user_aliases_file_name.get_base_dir())
var file = FileAccess.open(user_aliases_file_name, FileAccess.WRITE)
if file.is_open():
if file != null:
file.store_string(JSON.stringify(user_item_aliases, "\t", true))
file.close()

Expand Down
4 changes: 2 additions & 2 deletions material_maker/tools/obj_loader/obj_loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func load_obj_file(path : String) -> ArrayMesh:
var st := SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)

var mdlFile := FileAccess.open(path, FileAccess.READ)
if ! mdlFile.is_open():
var mdlFile : FileAccess = FileAccess.open(path, FileAccess.READ)
if mdlFile == null:
print("cannot open file at path[", path,"]")
mdlFile.close()
return null
Expand Down
2 changes: 1 addition & 1 deletion material_maker/windows/material_editor/export_editor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func _on_Save_Export_pressed():
if data.has("template_name"):
export_data.material = data.template_name
var file : FileAccess = FileAccess.open(files[0], FileAccess.WRITE)
if file != null:
if file == null:
return
file.store_string(JSON.stringify(export_data))

Expand Down
13 changes: 7 additions & 6 deletions start.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ func _ready():
"Windows":
var bat_file_path : String = OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)+"\\mm_cd.bat"
var bat_file : FileAccess = FileAccess.open(bat_file_path, FileAccess.WRITE)
bat_file.store_line("cd")
bat_file.close()
OS.execute(bat_file_path, [], output)
#TODO: fix this
#dir.remove(bat_file_path)
#FileAccess.change_dir(output[0].split("\n")[2])
if bat_file != null:
bat_file.store_line("cd")
bat_file.close()
OS.execute(bat_file_path, [], output)
#TODO: fix this
#dir.remove(bat_file_path)
#FileAccess.change_dir(output[0].split("\n")[2])
var target : String = "Godot/Godot 4 Standard"
#TODO: fix this
var output_dir : String = "" #DirAccess.get_current_dir()
Expand Down

0 comments on commit 2ffc536

Please sign in to comment.