private void Load() { nn.fs.EntryType entryType = 0; nn.Result result = nn.fs.FileSystem.GetEntryType(ref entryType, filePath); if (nn.fs.FileSystem.ResultPathNotFound.Includes(result)) { return; } result.abortUnlessSuccess(); result = nn.fs.File.Open(ref fileHandle, filePath, nn.fs.OpenFileMode.Read); result.abortUnlessSuccess(); long fileSize = 0; result = nn.fs.File.GetSize(ref fileSize, fileHandle); result.abortUnlessSuccess(); byte[] data = new byte[fileSize]; result = nn.fs.File.Read(fileHandle, 0, data, fileSize); result.abortUnlessSuccess(); nn.fs.File.Close(fileHandle); using (MemoryStream stream = new MemoryStream(data)) { BinaryReader reader = new BinaryReader(stream); int version = reader.ReadInt32(); Debug.Assert(version == saveDataVersion); // Save data version up counter = reader.ReadInt32(); } }
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 }
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()); }
void Start() { #if UNITY_SWITCH && !UNITY_EDITOR nn.account.Account.Initialize(); result = nn.fs.SaveData.Mount(Switch_MountName, userId); #endif filePath = string.Format("{0}:/{1}", Switch_MountName, Switch_FileName); SetupWorldMap(); }
public RankingDeta Load() { nn.fs.EntryType entryType = 0; nn.Result result = nn.fs.FileSystem.GetEntryType(ref entryType, filePath); if (nn.fs.FileSystem.ResultPathNotFound.Includes(result)) { return(null); } result.abortUnlessSuccess(); result = nn.fs.File.Open(ref fileHandle, filePath, nn.fs.OpenFileMode.Read); result.abortUnlessSuccess(); long fileSize = 0; result = nn.fs.File.GetSize(ref fileSize, fileHandle); result.abortUnlessSuccess(); byte[] data = new byte[fileSize]; result = nn.fs.File.Read(fileHandle, 0, data, fileSize); result.abortUnlessSuccess(); nn.fs.File.Close(fileHandle); string loadDeta; using (MemoryStream stream = new MemoryStream(data)) { BinaryReader reader = new BinaryReader(stream); loadDeta = reader.ReadString(); int version = reader.ReadInt32(); Debug.Assert(version == saveDataVersion); } RankingDeta ranking = new RankingDeta(); char[] Cut = { '&', '_', }; string[] CutDeta = new string[5]; CutDeta = loadDeta.Split(new char[] { '%' }, System.StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < 5; i++) { string[] splitDeta = CutDeta[i].Split(Cut, System.StringSplitOptions.RemoveEmptyEntries); ranking.name1[i] = splitDeta[0]; ranking.name2[i] = splitDeta[1]; ranking.score[i] = int.Parse(splitDeta[2]); } return(ranking); }
public void Save(string[] player1, string[] player2, int[] score) { string saveData = player1[0] + "&" + player2[0] + "_" + score[0] + "%" + player1[1] + "&" + player2[1] + "_" + score[1] + "%" + player1[2] + "&" + player2[2] + "_" + score[2] + "%" + player1[3] + "&" + player2[3] + "_" + score[3] + "%" + player1[4] + "&" + player2[4] + "_" + score[4] + "%"; byte[] data; using (MemoryStream stream = new MemoryStream(saveDataSize)) { BinaryWriter writer = new BinaryWriter(stream); writer.Write(saveData); writer.Write(saveDataVersion); stream.Close(); data = stream.GetBuffer(); Debug.Assert(data.Length == saveDataSize); } #if UNITY_SWITCH // Nintendo Switch Guideline 0080 UnityEngine.Switch.Notification.EnterExitRequestHandlingSection(); #endif nn.Result result = nn.fs.File.Delete(filePath); if (!nn.fs.FileSystem.ResultPathNotFound.Includes(result)) { result.abortUnlessSuccess(); } result = nn.fs.File.Create(filePath, saveDataSize); result.abortUnlessSuccess(); result = nn.fs.File.Open(ref fileHandle, filePath, nn.fs.OpenFileMode.Write); result.abortUnlessSuccess(); result = nn.fs.File.Write(fileHandle, 0, data, data.LongLength, nn.fs.WriteOption.Flush); result.abortUnlessSuccess(); nn.fs.File.Close(fileHandle); result = nn.fs.SaveData.Commit(mountName); result.abortUnlessSuccess(); #if UNITY_SWITCH // Nintendo Switch Guideline 0080 UnityEngine.Switch.Notification.LeaveExitRequestHandlingSection(); #endif }
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(); } }
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); }
void Start() { 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); nn.Result result = nn.fs.SaveData.Mount(mountName, userId); result.abortUnlessSuccess(); filePath = string.Format("{0}:/{1}", mountName, fileName); nn.hid.Npad.Initialize(); nn.hid.Npad.SetSupportedStyleSet(nn.hid.NpadStyle.Handheld | nn.hid.NpadStyle.JoyDual); nn.hid.Npad.SetSupportedIdType(npadIds); npadState = new nn.hid.NpadState(); }
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); }
void Start() { // textComponent = GameObject.Find("/Canvas/Text").GetComponent<UnityEngine.UI.Text>(); 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); nn.Result result = nn.fs.SaveData.Mount(mountName, userId); result.abortUnlessSuccess(); filePath = string.Format("{0}:/{1}", mountName, fileName); Load(); nn.hid.Npad.Initialize(); nn.hid.Npad.SetSupportedStyleSet(nn.hid.NpadStyle.Handheld | nn.hid.NpadStyle.JoyDual); nn.hid.Npad.SetSupportedIdType(npadIds); npadState = new nn.hid.NpadState(); }
static byte[] ReadAllBytes(string filePath) { #if UNITY_STANDALONE || UNITY_EDITOR || UNITY_IOS return(System.IO.File.ReadAllBytes(filePath)); #elif UNITY_SWITCH nn.fs.FileHandle fileHandle = new nn.fs.FileHandle(); nn.Result result = nn.fs.File.Open(ref fileHandle, filePath, nn.fs.OpenFileMode.Read); result.abortUnlessSuccess(); long fileSize = 0; result = nn.fs.File.GetSize(ref fileSize, fileHandle); result.abortUnlessSuccess(); byte[] data = new byte[fileSize]; result = nn.fs.File.Read(fileHandle, 0, data, fileSize); result.abortUnlessSuccess(); nn.fs.File.Close(fileHandle); return(data); #else return(null); #endif }
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 }