2018-05-07 09:21:31 +08:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Netcode;
|
2018-05-07 10:11:24 +08:00
|
|
|
|
|
|
|
|
|
|
2018-05-07 09:21:31 +08:00
|
|
|
|
namespace ModdedUtilitiesNetworking.Framework.Extentions
|
|
|
|
|
{
|
2018-05-07 10:31:10 +08:00
|
|
|
|
public static class GenericExtentions
|
2018-05-07 09:21:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public static string ReadString(this BinaryReader reader)
|
|
|
|
|
{
|
|
|
|
|
String s= reader.ReadString();
|
|
|
|
|
return new string(s.ToCharArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void WriteString(this BinaryWriter writer, object str)
|
|
|
|
|
{
|
2018-05-07 20:41:16 +08:00
|
|
|
|
writer.Write((string)str);
|
2018-05-07 10:11:24 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Writes a string list to a binary stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="writer"></param>
|
|
|
|
|
/// <param name="strList">The list to write.</param>
|
|
|
|
|
public static void WriteStringList(this BinaryWriter writer, object strList)
|
|
|
|
|
{
|
|
|
|
|
List<string> list =(List<string>)strList;
|
|
|
|
|
writer.Write(list.Count);
|
|
|
|
|
for(int i=0; i<list.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteString(list.ElementAt(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads a string list from the binary data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reader"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static List<String> ReadStringList(this BinaryReader reader)
|
|
|
|
|
{
|
|
|
|
|
int count = reader.ReadInt32();
|
|
|
|
|
List<string> strList = new List<string>();
|
|
|
|
|
for(int i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
string s=reader.ReadString();
|
|
|
|
|
strList.Add(s);
|
|
|
|
|
}
|
|
|
|
|
return strList;
|
2018-05-07 09:21:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read the custom info packet sent from a modded client or server.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reader"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static object[] ReadModdedInfoPacket(this BinaryReader reader)
|
|
|
|
|
{
|
|
|
|
|
object[] o = new object[2]
|
|
|
|
|
{
|
|
|
|
|
reader.ReadString(),
|
|
|
|
|
reader.ReadString()
|
|
|
|
|
};
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read the remaining byte data in an array.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reader"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static byte[] ReadAllBytes(this BinaryReader reader)
|
|
|
|
|
{
|
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
reader.BaseStream.CopyTo(memoryStream);
|
|
|
|
|
|
|
|
|
|
return memoryStream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 20:41:16 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read a data info file from a binary stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reader"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static DataInfo ReadDataInfo(this BinaryReader reader)
|
|
|
|
|
{
|
|
|
|
|
String key=reader.ReadString();
|
|
|
|
|
object data = ModCore.processTypesToRead(reader, key);
|
2018-05-09 08:21:59 +08:00
|
|
|
|
string ID = reader.ReadString();
|
|
|
|
|
DataInfo info = new DataInfo(key,data,ID);
|
2018-05-07 20:41:16 +08:00
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write a dataInfo file to binary.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="writer"></param>
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
public static void WriteDataInfo(this BinaryWriter writer, object obj)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataInfo dataInfo = (DataInfo)obj;
|
|
|
|
|
|
|
|
|
|
writer.WriteString(dataInfo.type);
|
|
|
|
|
ModCore.monitor.Log("WRITE DATA INFO FUNCTION3: " + dataInfo.type);
|
|
|
|
|
ModCore.processTypesToWrite(writer, dataInfo.type, dataInfo.data);
|
2018-05-09 08:21:59 +08:00
|
|
|
|
writer.WriteString(dataInfo.recipientID);
|
2018-05-07 20:41:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 09:21:31 +08:00
|
|
|
|
//Can do custom classes here for reading and writing.
|
|
|
|
|
//That way it will be better to save/load data
|
|
|
|
|
}
|
|
|
|
|
}
|