Ejemplo n.º 1
0
 private bool DeleteFileSwitch(string fileName)
 {
     nn.fs.EntryType entryType = 0; //init to a dummy value (C# requirement)
     nn.fs.FileSystem.GetEntryType(ref entryType, saveDataPath);
     nn.Result result = nn.fs.File.Delete(saveDataPath + fileName);
     return(result.IsSuccess());
 }
Ejemplo n.º 2
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
    }
Ejemplo n.º 3
0
    private const int loadBufferSize = 1024;       // 1 KB

    public void initialize()
    {
        nn.account.Account.Initialize();
        nn.account.UserHandle userHandle = new nn.account.UserHandle();
        nn.account.Account.OpenPreselectedUser(ref userHandle);
        nn.account.Account.GetUserId(ref userId, userHandle);

        // mount save data
        nn.Result result = nn.fs.SaveData.Mount(mountName, userId);

        //print out error (debug only) and abort if the filesystem couldn't be mounted
        if (result.IsSuccess() == false)
        {
            Debug.Log("Critical Error: File System could not be mounted.");
            result.abortUnlessSuccess();
        }
    }
Ejemplo n.º 4
0
    private bool LoadInternalSwitch(ref byte[] loadedData, string fileName, int loadBufferSizeInBytes)
    {
#if (UNITY_SWITCH && !UNITY_EDITOR) || SWITCH_DEV
        nn.fs.EntryType entryType = 0; //init to a dummy value (C# requirement)
        nn.fs.FileSystem.GetEntryType(ref entryType, saveDataPath);
        nn.Result result = nn.fs.File.Open(ref fileHandle, saveDataPath + fileName, nn.fs.OpenFileMode.Read);
        if (result.IsSuccess() == false)
        {
            return(false);   // Could not open file. This can be used to detect if this is the first time a user has launched your game.
                             // (However, be sure you are not getting this error due to your file being locked by another process, etc.)
        }
        loadedData = new byte[loadBufferSizeInBytes];
        nn.fs.File.Read(fileHandle, 0, loadedData, loadBufferSizeInBytes);
        nn.fs.File.Close(fileHandle);
#endif
        return(true);
    }
Ejemplo n.º 5
0
    public bool load(ref string outputData, string filename)
    {
        nn.fs.EntryType entryType = 0; //init to a dummy value (C# requirement)
        nn.fs.FileSystem.GetEntryType(ref entryType, saveDataPath);
        nn.Result result = nn.fs.File.Open(ref fileHandle, saveDataPath + filename, nn.fs.OpenFileMode.Read);
        if (result.IsSuccess() == false)
        {
            return(false);   // Could not open file. This can be used to detect if this is the first time a user has launched your game.
                             // (However, be sure you are not getting this error due to your file being locked by another process, etc.)
        }
        byte[] loadedData = new byte[loadBufferSize];
        nn.fs.File.Read(fileHandle, 0, loadedData, loadBufferSize);
        nn.fs.File.Close(fileHandle);

        using (MemoryStream stream = new MemoryStream(loadedData))
        {
            BinaryReader reader = new BinaryReader(stream);
            outputData = reader.ReadString();
        }
        return(true);
    }
Ejemplo n.º 6
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
    }