CRIWARE Unity Plugin Manual
Last Updated: 2024-07-12
File loading methods and classes
Two file loading methods are available in the CRI File System library (FileMajik PRO runtime library): the "coroutine method" and the "regular method".
You can use either one of them depending on the context.
It uses a coroutine to load a file asynchronously in the background.
The yield statement is used to wait for the completion of the file loading.
To facilitate file loading and binding, the CriWare.CriFsUtility utility class is provided.
It automatically creates and handles the basic objects in the CRI File System, i.e. the binder (CriWare.CriFsBinder) and the loader (CriWare.CriFsLoader).
Thus, you can write code which is simple and easy to understand.
Process flow
(1) Creating and staring a coroutine
The file loading function provided by the application is started as a coroutine.
StartCoroutine(this.AppLoadFunction(path));
(2) Issuing a file loading request
When the CriWare.CriFsUtility.LoadFile function is called, a file loading request is issued and the file is loaded asynchronously in the background.
The request object is returned by the function.
(3) Waiting for the completion of the file loading
The yield statement is used to wait for the completion of the file loading.
If the error returned is null, then the file was loaded successfully.
yield return request.WaitForDone(this);
if (request.error == null) { // The file is loaded successfully
// Obtain a buffer from the request object and process the data
...
}
(4) Obtaining a buffer from the request object and processing the data
For example, for a text file, the loaded data (stored in request.bytes) is converted into strings using the specified encoding.
Encoding enc = Encoding.GetEncoding("utf-8");
this.loadedText = enc.GetString(request.bytes);
Regular method (CriFsLoader class)
This is a file loading method in FileMajik PRO which is aimed at game consoles. It uses polling when a frame is updated to check for the completion of the loading operation.
With this method, the CriWare.CriFsLoader class is used to load a file.
It requires a bound file and the path of the file.
A "binder" (CriWare.CriFsBinder) is like a virtual drive or device.
You can register files, directories and CPK files with a binder (binding).
A file is loaded by the "loader" (CriWare.CriFsLoader) object via the binder.
Process flow
(1) Creating a binder
First, create a binder.
Note that to load a single file, you do not need to assign it to a binder.
(2) Obtaining the file size
Obtain the size of the target file.
int file_size = (int)binder.GetFileSize(path);
(3) Securing a file loading buffer
Secure a buffer of the file size.
byte[] buffer = new byte[file_size];
(4) Creating a loader
Create a loader object (CriWare.CriFsLoader) to load the file.
You can use a single loader to load multiple files.
You can also provide a loader for each file to load multiple files simultaneously.
CriFsLoader loader = new CriFsLoader();
(5) Issuing a file loading request
Call the CriWare.CriFsLoader.Load function. A file loading request is issued.
To load a single file, set the first argument (binder) to null.
loader.Load(null, path, 0, file_size, buffer);
(6) Checking the completion of the file loading
In order to check for the completion of the loading operation, we get the status of the loader from the CriWare.CriFsLoader.GetStatus function at each frame.
When the loading operation is completed, the status of the loader is CriFsLoader.Status.Complete.
// Execute for each frame
publicvoid Update()
{
// Is loading completed?
if (loader.GetStatus() == CriFsLoader.Status.Complete) {
// Process the loaded data
...
}
}
The status of the loader changes as depicted below.
Transitions of the loader status
(7) Processing the loaded data
Here is some sample code for processing the data loaded in the buffer.