docs: replace File with FileAccess

This commit is contained in:
Jiri Suchan 2023-01-30 22:21:36 +08:00
parent 312011fade
commit 4a4adec33d
4 changed files with 10 additions and 16 deletions

View file

@ -37,8 +37,8 @@
return [{"name": "my_option", "default_value": false}] return [{"name": "my_option", "default_value": false}]
func _import(source_file, save_path, options, platform_variants, gen_files): func _import(source_file, save_path, options, platform_variants, gen_files):
var file = File.new() var file = FileAccess.open(source_file, FileAccess.READ)
if file.open(source_file, File.READ) != OK: if file == null:
return FAILED return FAILED
var mesh = ArrayMesh.new() var mesh = ArrayMesh.new()
# Fill the Mesh with data read in "file", left as an exercise to the reader. # Fill the Mesh with data read in "file", left as an exercise to the reader.

View file

@ -15,8 +15,7 @@
extends EditorTranslationParserPlugin extends EditorTranslationParserPlugin
func _parse_file(path, msgids, msgids_context_plural): func _parse_file(path, msgids, msgids_context_plural):
var file = File.new() var file = FileAccess.open(path, FileAccess.READ)
file.open(path, File.READ)
var text = file.get_as_text() var text = file.get_as_text()
var split_strs = text.split(",", false) var split_strs = text.split(",", false)
for s in split_strs: for s in split_strs:

View file

@ -11,15 +11,14 @@
const CHUNK_SIZE = 102 const CHUNK_SIZE = 102
func hash_file(path): func hash_file(path):
var ctx = HashingContext.new()
var file = File.new()
# Start a SHA-256 context.
ctx.start(HashingContext.HASH_SHA256)
# Check that file exists. # Check that file exists.
if not file.file_exists(path): if not FileAccess.file_exists(path):
return return
# Start a SHA-256 context.
var ctx = HashingContext.new()
ctx.start(HashingContext.HASH_SHA256)
# Open the file to hash. # Open the file to hash.
file.open(path, File.READ) var file = FileAccess.open(path, FileAccess.READ)
# Update the context after reading each chunk. # Update the context after reading each chunk.
while not file.eof_reached(): while not file.eof_reached():
ctx.update(file.get_buffer(CHUNK_SIZE)) ctx.update(file.get_buffer(CHUNK_SIZE))

View file

@ -21,21 +21,17 @@
[codeblocks] [codeblocks]
[gdscript] [gdscript]
func load_mp3(path): func load_mp3(path):
var file = File.new() var file = FileAccess.open(path, FileAccess.READ)
file.open(path, File.READ)
var sound = AudioStreamMP3.new() var sound = AudioStreamMP3.new()
sound.data = file.get_buffer(file.get_length()) sound.data = file.get_buffer(file.get_length())
file.close()
return sound return sound
[/gdscript] [/gdscript]
[csharp] [csharp]
public AudioStreamMP3 LoadMP3(string path) public AudioStreamMP3 LoadMP3(string path)
{ {
var file = new File(); using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
file.Open(path, File.READ);
var sound = new AudioStreamMP3(); var sound = new AudioStreamMP3();
sound.Data = file.GetBuffer(file.GetLength()); sound.Data = file.GetBuffer(file.GetLength());
file.Close();
return sound; return sound;
} }
[/csharp] [/csharp]