You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
5.4 KiB
172 lines
5.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
|
|
|
|
namespace Sog.IO
|
|
{
|
|
/// <summary>
|
|
/// protobuf保存成文件,某些不是很重要的服务器数据可以采用这种方法保存,丢了也无所谓的
|
|
/// </summary>
|
|
public static class FileDBSave
|
|
{
|
|
public static int SaveToFile(Google.Protobuf.IMessage message,string filepath)
|
|
{
|
|
try
|
|
{
|
|
byte[] dataByte = Google.Protobuf.MessageExtensions.ToByteArray(message);
|
|
|
|
FileStream fileStream = File.OpenWrite(filepath);
|
|
//清空文件
|
|
fileStream.SetLength(0);
|
|
|
|
BinaryWriter sw = new BinaryWriter(fileStream);
|
|
sw.Write(dataByte);
|
|
sw.Flush();
|
|
sw.Dispose();
|
|
fileStream.Dispose();
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Error("FileDBSave.SaveToFile write file failed! {0}", filepath);
|
|
TraceLog.Exception(ex);
|
|
return -2;
|
|
}
|
|
}
|
|
|
|
public static Google.Protobuf.IMessage ReadFromFile(Google.Protobuf.MessageParser parser,string filepath)
|
|
{
|
|
if (File.Exists(filepath) == false)
|
|
{
|
|
//这个不能算错误
|
|
TraceLog.Debug("FileDBSave.ReadFromFile file not exist {0}", filepath);
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
byte[] data = File.ReadAllBytes(filepath);
|
|
Google.Protobuf.IMessage message = parser.ParseFrom(data);
|
|
|
|
return message;
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
TraceLog.Error("FileDBSave.ReadFromFile parse message failed! {0}", filepath);
|
|
TraceLog.Exception(ex);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
public static int SaveToFile<T>(ref T structMessage, string filepath)
|
|
where T : struct, ProtoCSStruct.IStructMessage<T>
|
|
{
|
|
try
|
|
{
|
|
int size = structMessage.CalculateSize();
|
|
byte[] dataByte = new byte[size];
|
|
ProtoCSStruct.CodedOutputStream ouput = new ProtoCSStruct.CodedOutputStream(dataByte);
|
|
structMessage.WriteTo(ouput);
|
|
|
|
FileStream fileStream = File.OpenWrite(filepath);
|
|
//清空文件
|
|
fileStream.SetLength(0);
|
|
|
|
BinaryWriter sw = new BinaryWriter(fileStream);
|
|
sw.Write(dataByte);
|
|
sw.Flush();
|
|
sw.Dispose();
|
|
fileStream.Dispose();
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Error("FileDBSave.SaveToFile write file failed! {0}", filepath);
|
|
TraceLog.Exception(ex);
|
|
return -2;
|
|
}
|
|
}
|
|
|
|
public static bool ReadFromFile<T>(ref T structMessage, string filepath)
|
|
where T : struct, ProtoCSStruct.IStructMessage<T>
|
|
{
|
|
if (File.Exists(filepath) == false)
|
|
{
|
|
//这个不能算错误
|
|
TraceLog.Debug("FileDBSave.ReadFromFile file not exist {0}", filepath);
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
byte[] data = File.ReadAllBytes(filepath);
|
|
ProtoCSStruct.CodedInputStream input = new ProtoCSStruct.CodedInputStream(data);
|
|
structMessage.ReadFrom(input);
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Error("FileDBSave.ReadFromFile parse message failed! {0}", filepath);
|
|
TraceLog.Exception(ex);
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public static class FileDBSaveByBinSerialize
|
|
{
|
|
public static int SaveToFile(object obj, string filepath)
|
|
{
|
|
try
|
|
{
|
|
byte[] dataByte = BinarySerialize.BinSerialize.Serialize(obj);
|
|
|
|
FileStream fileStream = File.OpenWrite(filepath);
|
|
//清空文件
|
|
fileStream.SetLength(0);
|
|
|
|
BinaryWriter sw = new BinaryWriter(fileStream);
|
|
sw.Write(dataByte);
|
|
sw.Flush();
|
|
sw.Dispose();
|
|
fileStream.Dispose();
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Error("FileDBSaveByBinSerialize.SaveToFile write file failed! {0}", filepath);
|
|
TraceLog.Exception(ex);
|
|
return -2;
|
|
}
|
|
}
|
|
|
|
public static T ReadFromFile<T>(string filepath)where T : class
|
|
{
|
|
if (File.Exists(filepath) == false)
|
|
{
|
|
//这个不能算错误
|
|
TraceLog.Debug("FileDBSaveByBinSerialize.ReadFromFile file not exist {0}", filepath);
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
byte[] data = File.ReadAllBytes(filepath);
|
|
return BinarySerialize.BinSerialize.Deserialize<T>(data);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TraceLog.Error("FileDBSaveByBinSerialize.ReadFromFile parse message failed! {0}", filepath);
|
|
TraceLog.Exception(ex);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|