Difference between revisions of "File system"

From GiderosMobile
 
(6 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
 
== File system ==
 
== File system ==
 +
In Gideros runtime, there are now, 4 kinds of directories:
 +
*'''resource directory'''
 +
*'''document directory'''
 +
*'''temporary directory'''
 +
*'''buffer directory'''
  
In Gideros runtime, there are 3 kinds of directories: resource, document, and temporary.
+
== Resource directory ==
 
+
Your code, images, audios and all other files reside in the resource directory. Consider the test project below, and examine the 3 directories and corresponding files.
=== Resource directory ===
 
 
 
Your code, image, audio and all other files are reside at resource directory. Consider the test project below, and examine the 3 directories and corresponding files.
 
  
 
[[File:Resource directory.png]]
 
[[File:Resource directory.png]]
  
 
The files seen above are stored on a real device and Gideros Player like the following:
 
The files seen above are stored on a real device and Gideros Player like the following:
<br/>
 
 
{resource directory}/gfx/sprite1.png<br/>
 
{resource directory}/gfx/sprite2.png<br/>
 
{resource directory}/gfx/background.png<br/>
 
{resource directory}/audio/game-music.mp3<br/>
 
{resource directory}/audio/click.wav<br/>
 
{resource directory}/data/list.txt<br/>
 
{resource directory}/main.lua<br/>
 
{resource directory}/game.lua<br/>
 
<br/>
 
  
Resource directory is the default directory. Therefore, to access the files at resource directory, specify the file path as it is:
+
{'''resource directory'''}/gfx/sprite1.png<br/>
<br/>
+
{'''resource directory'''}/gfx/sprite2.png<br/>
 +
{'''resource directory'''}/gfx/background.png<br/>
 +
{'''resource directory'''}/audio/game-music.mp3<br/>
 +
{'''resource directory'''}/audio/click.wav<br/>
 +
{'''resource directory'''}/data/list.txt<br/>
 +
{'''resource directory'''}/main.lua<br/>
 +
{'''resource directory'''}/game.lua<br/>
  
<source lang="lua">
+
The resource directory is the default directory. Therefore, to access the files you specify the file path as it is:
 +
<syntaxhighlight lang="lua">
 
local sprite1 = Texture.new("gfx/sprite1.png")
 
local sprite1 = Texture.new("gfx/sprite1.png")
 
local sprite2 = Texture.new("gfx/sprite2.png")
 
local sprite2 = Texture.new("gfx/sprite2.png")
Line 35: Line 33:
 
local music = Sound.new("audio/game-music.mp3")
 
local music = Sound.new("audio/game-music.mp3")
 
local click = Sound.new("audio/click.wav")
 
local click = Sound.new("audio/click.wav")
</source>
+
</syntaxhighlight>
<br/>
 
  
Also, you can use the io library provided by Lua:
+
You can also use the io library provided by Lua:
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
io.read("data/list.txt")
 
io.read("data/list.txt")
</source>
+
</syntaxhighlight>
<br/>
+
 
  
You don't need to know the exact path of resource, document, and temporary directories because Gideros Studio provides an easy way to specify files at these directories.
+
'''Note: the resource directory is read-only and you should not try to write any files there'''
  
=== Document directory ===
 
  
You can store a file created by your application in "document" directory. The files created in this directory are permanent among application sessions. For example, you can create and then read files at document directory to save player progress, or keep latest GPS coordinates.
+
You can access the files in the resource directory by adding "|R|" at the beginning of the file name, but you don't need to:
<br/>
+
<syntaxhighlight lang="lua">
 +
local sprite1 = Texture.new("|R|gfx/sprite1.png")
 +
</syntaxhighlight>
  
In order to specify a file in document directory, append "|D|" to the beginning of the filename:
+
== Document directory ==
<br/>
+
You can store a file created by your application in the '''document directory'''. The files created in this directory are permanent among application sessions. For example, you can create and then read files in the document directory to save player progress, or keep latest GPS coordinates, ...
  
<source lang="lua">
+
In order to specify a file in the document directory, append '''|D|''' to the beginning of the filename:
 +
<syntaxhighlight lang="lua">
 
io.write("|D|save.txt")
 
io.write("|D|save.txt")
</source>
+
</syntaxhighlight>
 +
 
 +
Here is an example of how you can copy a file from the resource directory to the document directory:
 +
<syntaxhighlight lang="lua">
 +
--function to copy file
 +
local function copy(src, dst)
 +
    local srcf = io.open(src, "rb")
 +
    local dstf = io.open(dst, "wb")
 +
 
 +
    local size = 2^13    -- good buffer size (8K)
 +
    while true do
 +
        local block = srcf:read(size)
 +
        if not block then break end
 +
        dstf:write(block)
 +
    end
 +
 
 +
    srcf:close()
 +
    dstf:close()
 +
end
 +
 
 +
--function to check if file exists
 +
local function exists(file)
 +
    local f = io.open(file, "rb")
 +
    if f == nil then
 +
        return false
 +
    end
 +
    f:close()
 +
    return true
 +
end
 +
 
 +
--usage
 +
if not exists("|D|database.db") then
 +
    copy("database.db", "|D|database.db")
 +
end
 +
</syntaxhighlight>
  
=== Temporary directory ===
+
== Temporary directory ==
 +
Gideros Studio provides a '''temporary directory''' to store files that may not stay permanent between different sessions. Therefore, files created in this directory are not guaranteed to exist when the application runs next time, and may be deleted after the application session finishes.
  
Gideros Studio provides a temporary directory to store files that may not stay permanent between different sessions. Therefore, files created in this directory are not guaranteed to exist when applications runs next time, and may be deleted after application session finishes.
+
In order to specify a file in the temporary directory, append '''|T|''' to the beginning of the file name. Example:
<br/>
+
<syntaxhighlight lang="lua">
In order to specify a file at temporary directory, append "|T|" to the begining of the file name. Consider the example below:
+
io.write("|T|tempfile.txt")
<source lang="lua">
+
</syntaxhighlight>
io.write("|T|temp.txt")
 
</source>
 
<br/>
 
 
   
 
   
'''Note:''' Optionally, you can access the files at resource directory by adding "|R|" to the beginning of the file name (but you don't need to):
+
This storage may be used to display some temporary data, like in the following example, images downloaded from the internet:
<source lang="lua">
+
<syntaxhighlight lang="lua">
local sprite1 = Texture.new("|R|gfx/sprite1.png")
+
--download completed
</source>
+
local function onComplete(event)
<br/>
+
--store image in temporary folder
 +
local out = io.open("|T|image.png", "wb")
 +
out:write(event.data)
 +
out:close()
 +
 
 +
--display it to user
 +
local b = Bitmap.new(Texture.new("|T|image.png"))
 +
b:setAnchorPoint(0.5, 0.5)
 +
b:setPosition(160, 240)
 +
stage:addChild(b)
 +
end
 +
 
 +
--load image
 +
local loader = UrlLoader.new("http://www.giderosmobile.com/giderosmobile.png")
 +
 
 +
--add event listener
 +
loader:addEventListener(Event.COMPLETE, onComplete)
 +
</syntaxhighlight>
 +
 
 +
== Buffer directory ==
 +
Gideros Studio now provides a '''buffer directory''' (see: [[Buffer]]) to store data into a file. The Buffer class acts as FIFO buffer with Gideros internal file system integration. Once created, buffer data can be accessed by internal gideros functions that normally use files, such as textures or sounds.
 +
 
 +
The buffer will be deleted after the application session finishes.
  
To sum up, here's a list of file operations on the device:
+
In order to specify a file in the buffer directory, append '''|B|''' to the beginning of the buffer name. Example:
<br/>
+
<syntaxhighlight lang="lua">
 +
io.append("|B|buffer.ext")
 +
</syntaxhighlight>
 +
 +
This storage may be used to create a '''''Shoutcast''''' stream. Please see Gideros example '''\Audio\Shoutcast Player'''.
  
<source lang="lua">
+
== To sum up ==
-- open file.txt at resource directory to read
+
Here is a list of possible file operations:
io.read("file.txt")
+
<syntaxhighlight lang="lua">
-- open file.txt at resource directory to read (same as above)
+
io.read("file.txt") -- open file.txt in the resource directory to read
io.read("|R|file.txt")
+
io.read("|R|file.txt") -- open file.txt in the resource directory to read (same as above)
-- open file.txt at documents directory to read
+
io.read("|D|file.txt") -- open file.txt in the document directory to read
io.read("|D|file.txt")
+
io.get("|B|buffer.ext") -- gets the data from the buffer
-- open file.txt at temporary directory to read
+
</syntaxhighlight>
io.read("|T|file.txt")
 
</source>
 
  
=== File execution order ===
+
== File execution order ==
By default Gideros executes all lua files on both players and in real app.
+
By default Gideros executes all your project Lua files in the following order:
  
We can assume that the order of execution is pretty random, but there are two things guaranteed:
 
 
*init.lua will always be executed first
 
*init.lua will always be executed first
 +
*then all the files in alphabetical order (upper case first, then lowercase), while resolving code dependencies
 
*main.lua will always be executed last
 
*main.lua will always be executed last
  
init.lua is the file that is loaded first.
+
'''Note''': the rule about main.lua and init.lua only applies to top level files. If they are in sub directories, they lose their specificity.
Then we have files in alphabetical order (upper case first, then lowercase), while resolving code dependencies provided.
+
 
Finally, main.lua is loaded last.
+
== strict.lua ==
 +
strict.lua checks uses of undeclared global variables.
  
'''Note''': the rule about main.lua and init.lua only applies to top level files. If they are in sub directories, they lose their specificity.
+
If strict.lua is executed, all global variables must be ‘declared’ through a regular assignment (even assigning nil will do) in a main chunk before being used anywhere or assigned to inside a function.
 +
 
 +
Although optional, it is a good habit to use it when developing Lua code.
  
=== strict.lua ===
 
 
For a detailed explanation of strict.lua, please refer to http://www.lua.org/pil/14.2.html
 
For a detailed explanation of strict.lua, please refer to http://www.lua.org/pil/14.2.html
 
strict.lua checks uses of undeclared global variables. If strict.lua is executed, all global variables must be ‘declared’ through a regular assignment (even assigning nil will do) in a main chunk before being used anywhere or assigned to inside a function. Although optional, it is a good habit to use it when developing Lua code.
 
  
 
To execute strict.lua before all other Lua files, simply add strict.lua and init.lua to asset library and make strict.lua dependent to init.lua.
 
To execute strict.lua before all other Lua files, simply add strict.lua and init.lua to asset library and make strict.lua dependent to init.lua.
Line 113: Line 168:
 
'''PREV.''': [[Event system]]<br/>
 
'''PREV.''': [[Event system]]<br/>
 
'''NEXT''': [[Profiling]]
 
'''NEXT''': [[Profiling]]
 +
 +
 +
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 23:22, 18 November 2023

The Ultimate Guide to Gideros Studio

File system

In Gideros runtime, there are now, 4 kinds of directories:

  • resource directory
  • document directory
  • temporary directory
  • buffer directory

Resource directory

Your code, images, audios and all other files reside in the resource directory. Consider the test project below, and examine the 3 directories and corresponding files.

Resource directory.png

The files seen above are stored on a real device and Gideros Player like the following:

{resource directory}/gfx/sprite1.png
{resource directory}/gfx/sprite2.png
{resource directory}/gfx/background.png
{resource directory}/audio/game-music.mp3
{resource directory}/audio/click.wav
{resource directory}/data/list.txt
{resource directory}/main.lua
{resource directory}/game.lua

The resource directory is the default directory. Therefore, to access the files you specify the file path as it is:

local sprite1 = Texture.new("gfx/sprite1.png")
local sprite2 = Texture.new("gfx/sprite2.png")
local background = Texture.new("gfx/background.png")
local music = Sound.new("audio/game-music.mp3")
local click = Sound.new("audio/click.wav")

You can also use the io library provided by Lua:

io.read("data/list.txt")


Note: the resource directory is read-only and you should not try to write any files there


You can access the files in the resource directory by adding "|R|" at the beginning of the file name, but you don't need to:

local sprite1 = Texture.new("|R|gfx/sprite1.png")

Document directory

You can store a file created by your application in the document directory. The files created in this directory are permanent among application sessions. For example, you can create and then read files in the document directory to save player progress, or keep latest GPS coordinates, ...

In order to specify a file in the document directory, append |D| to the beginning of the filename:

io.write("|D|save.txt")

Here is an example of how you can copy a file from the resource directory to the document directory:

--function to copy file
local function copy(src, dst)
    local srcf = io.open(src, "rb")
    local dstf = io.open(dst, "wb")

    local size = 2^13    -- good buffer size (8K)
    while true do
        local block = srcf:read(size)
        if not block then break end
        dstf:write(block)
    end

    srcf:close()
    dstf:close()
end

--function to check if file exists
local function exists(file)
    local f = io.open(file, "rb")
    if f == nil then
        return false
    end
    f:close()
    return true
end

--usage
if not exists("|D|database.db") then
    copy("database.db", "|D|database.db")
end

Temporary directory

Gideros Studio provides a temporary directory to store files that may not stay permanent between different sessions. Therefore, files created in this directory are not guaranteed to exist when the application runs next time, and may be deleted after the application session finishes.

In order to specify a file in the temporary directory, append |T| to the beginning of the file name. Example:

io.write("|T|tempfile.txt")

This storage may be used to display some temporary data, like in the following example, images downloaded from the internet:

--download completed
local function onComplete(event)
	--store image in temporary folder
	local out = io.open("|T|image.png", "wb")
	out:write(event.data)
	out:close()

	--display it to user
	local b = Bitmap.new(Texture.new("|T|image.png"))
	b:setAnchorPoint(0.5, 0.5)
	b:setPosition(160, 240)
	stage:addChild(b)
end

--load image
local loader = UrlLoader.new("http://www.giderosmobile.com/giderosmobile.png")

--add event listener
loader:addEventListener(Event.COMPLETE, onComplete)

Buffer directory

Gideros Studio now provides a buffer directory (see: Buffer) to store data into a file. The Buffer class acts as FIFO buffer with Gideros internal file system integration. Once created, buffer data can be accessed by internal gideros functions that normally use files, such as textures or sounds.

The buffer will be deleted after the application session finishes.

In order to specify a file in the buffer directory, append |B| to the beginning of the buffer name. Example:

io.append("|B|buffer.ext")

This storage may be used to create a Shoutcast stream. Please see Gideros example \Audio\Shoutcast Player.

To sum up

Here is a list of possible file operations:

io.read("file.txt") -- open file.txt in the resource directory to read
io.read("|R|file.txt") -- open file.txt in the resource directory to read (same as above)
io.read("|D|file.txt") -- open file.txt in the document directory to read
io.get("|B|buffer.ext") -- gets the data from the buffer

File execution order

By default Gideros executes all your project Lua files in the following order:

  • init.lua will always be executed first
  • then all the files in alphabetical order (upper case first, then lowercase), while resolving code dependencies
  • main.lua will always be executed last

Note: the rule about main.lua and init.lua only applies to top level files. If they are in sub directories, they lose their specificity.

strict.lua

strict.lua checks uses of undeclared global variables.

If strict.lua is executed, all global variables must be ‘declared’ through a regular assignment (even assigning nil will do) in a main chunk before being used anywhere or assigned to inside a function.

Although optional, it is a good habit to use it when developing Lua code.

For a detailed explanation of strict.lua, please refer to http://www.lua.org/pil/14.2.html

To execute strict.lua before all other Lua files, simply add strict.lua and init.lua to asset library and make strict.lua dependent to init.lua.

You can download strict.lua from File:Strict.lua that originally comes with the Lua distribution.


PREV.: Event system
NEXT: Profiling