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.
123 lines
3.6 KiB
123 lines
3.6 KiB
//#define TEXTURE_RAW_DATA
|
|
|
|
using Grpc.Core;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace UltraCombos.Marvel.DrawHeroes
|
|
{
|
|
public class ResourceClient : GrpcClientBase
|
|
{
|
|
|
|
[Configuration.Config]
|
|
public string overrideHost = "127.0.0.1";
|
|
|
|
[System.Serializable]
|
|
public class PageEvent : UnityEvent<string> { }
|
|
|
|
[Space(10)]
|
|
[Header("Draw Your Own Heroes")]
|
|
public PageEvent onPageReceived = new PageEvent();
|
|
public UnityEvent onTextureSent = new UnityEvent();
|
|
|
|
private void Start()
|
|
{
|
|
DebugInformation.Instance.UpdateProperty("Local IP", $"{Utility.Network.GetLocalIPAddress()}");
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (channel == null || host.Equals(overrideHost) == false)
|
|
{
|
|
Init();
|
|
}
|
|
|
|
DebugInformation.Instance.UpdateProperty(name, $"{host}:{port}");
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
host = overrideHost;
|
|
var options = new List<ChannelOption> { new ChannelOption(ChannelOptions.MaxSendMessageLength, int.MaxValue) };
|
|
Connect(options);
|
|
}
|
|
|
|
bool is_index_got = false;
|
|
|
|
public void GetPage()
|
|
{
|
|
if (State != ChannelState.Ready || is_index_got)
|
|
return;
|
|
|
|
GetPageAsync();
|
|
}
|
|
|
|
private async void GetPageAsync()
|
|
{
|
|
try
|
|
{
|
|
var data = AppData.Instance;
|
|
is_index_got = true;
|
|
var client = new Resource.ResourceClient(channel);
|
|
var reply = await client.GetPageAsync(new Device() { Id = data.deviceId });
|
|
Log($"OnPageReceived: {reply.Id}");
|
|
data.roleId = reply.Id;
|
|
data.serial = reply.Serial;
|
|
onPageReceived.Invoke(reply.Id);
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Log(e.Message, LogType.Error);
|
|
is_index_got = false;
|
|
}
|
|
}
|
|
|
|
bool is_data_sent = false;
|
|
|
|
public void SendData(Texture2D painting, Texture2D signature, Texture2D thumbnail)
|
|
{
|
|
if (State != ChannelState.Ready || is_data_sent)
|
|
return;
|
|
#if false
|
|
var content = tex.GetRawTextureData();
|
|
#else
|
|
//var content = tex.EncodeToJPG();
|
|
var painting_content = painting.EncodeToPNG();
|
|
var signature_content = signature.EncodeToPNG();
|
|
var thumbnail_content = thumbnail.EncodeToPNG();
|
|
#endif
|
|
|
|
var data = AppData.Instance;
|
|
|
|
SendDataAsync(new DataRequest()
|
|
{
|
|
Id = data.roleId,
|
|
Serial = data.serial,
|
|
Content = Google.Protobuf.ByteString.CopyFrom(painting_content),
|
|
Signature = Google.Protobuf.ByteString.CopyFrom(signature_content),
|
|
Thumbnail = Google.Protobuf.ByteString.CopyFrom(thumbnail_content),
|
|
});
|
|
}
|
|
|
|
private async void SendDataAsync(DataRequest request)
|
|
{
|
|
try
|
|
{
|
|
is_data_sent = true;
|
|
var client = new Resource.ResourceClient(channel);
|
|
var reply = await client.SendDataAsync(request);
|
|
|
|
Log(reply.Result);
|
|
|
|
onTextureSent.Invoke();
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
is_data_sent = false;
|
|
Log(e.Message, LogType.Error);
|
|
}
|
|
}
|
|
}
|
|
} |