Author Topic: Packet.RequestLoadFile for loading save data  (Read 4733 times)

appsdev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Packet.RequestLoadFile for loading save data
« on: October 23, 2014, 05:07:39 AM »
The problem I'm having is when I try to read the file it only reads the filename. What is necessary to read the contents of the file.

I have trying using the Binarywritter to first write the file locallly but I can't seem to make it as I'm getting the data from Packet.RequestLoadFile
Not for example "BinaryReader b = new BinaryReader(File.Open("some file name", FileMode.Open)). How do I make it so the file can get saved locally first? or there is another way to read the Packet.RequestLoadFile?

The following code came from another thread http://www.tasharen.com/forum/index.php?topic=9165.0
I have added the code for function OnLoadFile().  I try using the different kinds of 'reads' (char, chars...) but is only reads the filename not the contents from the file.

I think I'm getting close to the solution please help.
  1.  
  2. public class LoadGame : MonoBehaviour {
  3.  
  4.  
  5.         void Start()
  6.         {
  7.                 TNManager.SetPacketHandler(Packet.ResponseLoadFile, OnLoadFile);
  8.        
  9.         }
  10.        
  11.         void OnLoadFile (Packet response, BinaryReader reader, IPEndPoint source)
  12.         {
  13.        
  14.  
  15.        
  16.                 using (BinaryReader b = reader)
  17.                 {
  18.  
  19.  
  20.  
  21.                         // Position and length variables.
  22.                         int pos = 0;
  23.  
  24.                         // Use BaseStream.
  25.                         int length = (int)b.BaseStream.Length;
  26.                         while (pos < length)
  27.                         {
  28.  
  29.                                 // Read...
  30.                                 string v = b.ReadString();
  31.                                 Debug.Log(v.ToString());
  32.                                
  33.  
  34.                                 // Advance our position variable.
  35.                                 pos += sizeof(int);
  36.                         }
  37.                 }
  38.        
  39.  
  40.         }
  41.        
  42.         public void LoadFile (string filename)
  43.         {
  44.  
  45.        
  46.                 BinaryWriter writer = TNManager.BeginSend(Packet.RequestLoadFile);
  47.                 writer.Write(filename);
  48.                 TNManager.EndSend();
  49.        
  50.  
  51.         }
  52.        
  53. }
  54.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Packet.RequestLoadFile for loading save data
« Reply #1 on: October 23, 2014, 04:01:30 PM »
  1. TNManager.LoadFile(path, OnLoadFile);
  1. static void OnLoadFile (string filename, byte[] data)
  2. {
  3.     // 'data' is your file's contents
  4. }

appsdev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 10
    • View Profile
Re: Packet.RequestLoadFile for loading save data
« Reply #2 on: November 24, 2014, 12:07:57 PM »
referenced to this: http://www.tasharen.com/tnet/docs/class_t_n_manager.html#a242088944ea89b4c1355ce8a36230c4a

Two classes, one for save and one to load. Saving is fine, I got the file and the string to save on the server.

Loading has an issue, the loading seems to always been taken from local not from the server. I set the filename to one that had been deleted on the server. When I load it reads data from a local file as the one from the server had been deleted. I don't know where this local file reside.

According to the class reference it should read from the server. May I know what I might be missing from the script? And where might this file be located locally?

My code:
  1. public class SaveGame : MonoBehaviour {
  2.  
  3.         public void SaveAGame()
  4.         {
  5.                 BinaryWriter writer = TNManager.BeginSend(Packet.RequestSaveFile);
  6.                 writer.Write("testfile3");
  7.                 writer.Write(50);
  8.                 writer.Write("77777777");
  9.  
  10.                 TNManager.EndSend();
  11.         }
  12. }
  13.  

  1. public class LoadGame : MonoBehaviour {
  2.        
  3.         static void OnLoadFile (string filename, byte[] data)
  4.         {
  5.                 // 'data' is your file's contents
  6.                 string result = System.Text.Encoding.UTF8.GetString(data);
  7.                 Debug.Log("file content: " + result.ToString());
  8.         }
  9.        
  10.         public void LoadFile (string filename)
  11.         {
  12.        
  13.                 TNManager.LoadFile(filename, OnLoadFile);
  14.         }
  15.  
  16. }
  17.  


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Packet.RequestLoadFile for loading save data
« Reply #3 on: November 25, 2014, 02:14:39 AM »
The data you are passing in your save packet is completely wrong.

First, you are sending text while the function works with bytes. The length you specify is also 50 bytes, when your text is only 8 bytes long.

Second, you shouldn't be writing that packet yourself. You should be using TNManager.SaveFile.

Use MemoryStream.
  1. MemoryStream stream = new MemoryStream();
  2. StreamWriter writer = new StreamWriter(stream);
  3. writer.Write("Hello world");
  4. byte[] data = stream.ToArray();
  5. writer.Close();
  6. TNManager.SaveFile("testfile3", data);
Then to load it:
  1. MemoryStream stream = new MemoryStream(data);
  2. StreamReader reader = new StreamReader(stream);
  3. Debug.Log(reader.ReadToEnd());