예제 #1
0
    private void LoadSwitch()
    {
#if UNITY_SWITCH && !UNITY_EDITOR
        WorldMapSave = new WorldMapSaveClass();
        // Attempt to open the file in read-only mode.
        result = nn.fs.File.Open(ref fileHandle, filePath, nn.fs.OpenFileMode.Read);
        if (!result.IsSuccess())
        {
            if (nn.fs.FileSystem.ResultPathNotFound.Includes(result))
            {
                Debug.LogFormat("File not found: {0}", filePath);
            }
            else
            {
                Debug.LogErrorFormat("Unable to open {0}: {1}", filePath, result.ToString());
            }
        }

        // Get the file size.
        long fileSize = 0;
        nn.fs.File.GetSize(ref fileSize, fileHandle);
        // Allocate a buffer that matches the file size.
        byte[] data = new byte[fileSize];
        // Read the save data into the buffer.
        nn.fs.File.Read(fileHandle, 0, data, fileSize);
        // Close the file.
        nn.fs.File.Close(fileHandle);
        // Decode the UTF8-encoded data and store it in the string buffer.
        WorldMapSave = PlaytraGamesLtd.Utils.DeserializeFromString <WorldMapSaveClass>(Encoding.UTF8.GetString(data));
#endif
    }
예제 #2
0
    private void SaveSwitch()
    {
#if UNITY_SWITCH
        // Nintendo Switch Guideline 0080
        UnityEngine.Switch.Notification.EnterExitRequestHandlingSection();
#endif

#if UNITY_SWITCH && !UNITY_EDITOR
        // Convert the text to UTF-8-encoded bytes.
        byte[] data = Encoding.UTF8.GetBytes(PlaytraGamesLtd.Utils.SerializeToString <WorldMapSaveClass>(WorldMapSave));


        while (true)
        {
            // Attempt to open the file in write mode.
            result = nn.fs.File.Open(ref fileHandle, filePath, nn.fs.OpenFileMode.Write);
            // Check if file was opened successfully.
            if (result.IsSuccess())
            {
                // Exit the loop because the file was successfully opened.
                break;
            }
            else
            {
                if (nn.fs.FileSystem.ResultPathNotFound.Includes(result))
                {
                    // Create a file with the size of the encoded data if no entry exists.
                    result = nn.fs.File.Create(filePath, data.LongLength);

                    // Check if the file was successfully created.
                    if (!result.IsSuccess())
                    {
                        Debug.LogErrorFormat("Failed to create {0}: {1}", filePath, result.ToString());
                    }
                }
                else
                {
                    // Generic fallback error handling for debugging purposes.
                    Debug.LogErrorFormat("Failed to open {0}: {1}", filePath, result.ToString());
                }
            }
        }

        // Set the file to the size of the binary data.
        result = nn.fs.File.SetSize(fileHandle, data.LongLength);

        // You do not need to handle this error if you are sure there will be enough space.
        if (nn.fs.FileSystem.ResultUsableSpaceNotEnough.Includes(result))
        {
            Debug.LogErrorFormat("Insufficient space to write {0} bytes to {1}", data.LongLength, filePath);
        }

        // NOTE: Calling File.Write() with WriteOption.Flush incurs two write operations.
        result = nn.fs.File.Write(fileHandle, 0, data, data.LongLength, nn.fs.WriteOption.Flush);

        // The file must be closed before committing.
        nn.fs.File.Close(fileHandle);

        // Verify that the write operation was successful before committing.
        if (!result.IsSuccess())
        {
            Debug.LogErrorFormat("Failed to write {0}: {1}", filePath, result.ToString());
        }
        // This method moves the data from the journaling area to the main storage area.
        // If you do not call this method, all changes will be lost when the application closes.
        // Only call this when you are sure that all previous operations succeeded.
        nn.fs.FileSystem.Commit(Switch_MountName);
#endif
#if UNITY_SWITCH && !UNITY_EDITOR
// Stop preventing the system from terminating the game while saving.
        UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection();
#endif
    }