89 lines
3.5 KiB
C#
89 lines
3.5 KiB
C#
using Google.Protobuf;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
namespace bts.utils.proto {
|
|
static class StreamHelper {
|
|
public static void WriteProtos<T>(Stream stream, T value) where T : Google.Protobuf.IMessage<T> =>
|
|
WriteProtos<T>(stream, new[] { value });
|
|
|
|
public static void WriteProtos<T>(Stream stream, IEnumerable<T> source) where T : Google.Protobuf.IMessage<T> {
|
|
foreach (var value in source)
|
|
Google.Protobuf.MessageExtensions.WriteDelimitedTo(value, stream);
|
|
}
|
|
|
|
public static IEnumerable<T> ReadProtos<T>(byte[] bytes) where T : IMessage<T>, new() =>
|
|
ReadProtos<T>(new MemoryStream(bytes));
|
|
|
|
public static IEnumerable<T> ReadProtos<T>(Stream fs) where T : IMessage<T>, new() {
|
|
var parser = ProtoHelper<T>.parser;
|
|
while (fs.Position < fs.Length) {
|
|
T val;
|
|
try {
|
|
val = parser.ParseDelimitedFrom(fs);
|
|
} catch {
|
|
break;
|
|
}
|
|
yield return val;
|
|
}
|
|
}
|
|
|
|
public static byte[] ToDelimitedByteArray<T>(IEnumerable<T> source) where T : IMessage<T> {
|
|
using (var stream = new MemoryStream()) {
|
|
foreach (var v in source)
|
|
v.WriteDelimitedTo(stream);
|
|
return stream.ToArray();
|
|
}
|
|
}
|
|
|
|
public static class ProtoHelper<T> where T : IMessage<T>, new() {
|
|
public static readonly MessageParser<T> parser = new MessageParser<T>(() => new T());
|
|
}
|
|
}
|
|
|
|
static class FileStreamHelper {
|
|
public static List<T> ReadProtos<T>(string path, bool check_existence_first = true) where T : Google.Protobuf.IMessage<T>, new() {
|
|
if (!check_existence_first || File.Exists(path)) {
|
|
var fs = SafeOpen(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
if (fs != null) {
|
|
using (fs)
|
|
return new List<T>(StreamHelper.ReadProtos<T>(fs));
|
|
}
|
|
}
|
|
return new List<T>(0);
|
|
}
|
|
|
|
public static void AppendProtos<T>(string path, T value) where T : IMessage<T> =>
|
|
AppendProtos<T>(path, new[] { value });
|
|
|
|
public static void AppendProtos<T>(string path, IEnumerable<T> source) where T : IMessage<T> {
|
|
using (var fs = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read))
|
|
StreamHelper.WriteProtos(fs, source);
|
|
}
|
|
|
|
public static void WriteProtos<T>(string path, IEnumerable<T> source) where T : IMessage<T> {
|
|
using (var fs = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
StreamHelper.WriteProtos(fs, source);
|
|
}
|
|
|
|
public static FileStream SafeOpen(string path, FileMode mode, FileAccess access, FileShare share) =>
|
|
SafeOpen(path, mode, access, share, out string _);
|
|
|
|
public static FileStream SafeOpen(string path, FileMode mode, FileAccess access, FileShare share, out string error_message) {
|
|
FileStream rv;
|
|
try {
|
|
rv = File.Open(path, mode, access, share);
|
|
error_message = null;
|
|
} catch (Exception ex) {
|
|
error_message = ex.ToString();
|
|
rv = null;
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
public static class ProtoHelper<T> where T : IMessage<T>, new() {
|
|
public static readonly string default_file_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), new T().Descriptor.Name);
|
|
}
|
|
}
|
|
} |