JD2022-TU1/main/tools/legacy/UA_RemoteServer/UA_RemoteServer/RemoteTarget.cs

380 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using JD.Remoting;
using UA_RemoteServer.Plugin;
using JD.Core;
using JD.Interfaces.Remoting;
using JD.Remoting.Gui;
namespace UA_RemoteServer
{
public class RemoteTarget
{
public class TargetEvent
{
public delegate void OnAnalyzeDelegate(ref List<NodeFile> listFiles);
public delegate void OnDumpErrorsDelegate(ref List<String> listErrors);
public delegate void OnPingDelegate(string text);
public delegate void OnSignalCookDelegate(bool value);
public delegate void OnReloadedFileDelegate();
public OnAnalyzeDelegate OnAnalyzeEvent = null;
public OnDumpErrorsDelegate OnDumpErrorsEvent = null;
public OnPingDelegate OnPingEvent = null;
public OnSignalCookDelegate OnSignalCookEvent = null;
public OnReloadedFileDelegate OnReloadedFile = null;
}
private readonly TargetEvent _targetEvent = new TargetEvent();
private readonly RemoteServerPlugin _plugin;
private bool _cacheFiles = true;
private string _bufferLogCookOutput = "";
private string _bufferLogErrorsOutput = "";
private string _bufferLogEngineOutput = "";
private string _bufferLogHotReloadFile = "";
private string _dataDirectory;
private string _combinedDataDirectory;
private string _platform;
private string _globalOutput;
private EngineConnectionStatusUserControl _connectionStatusControl;
public RemoteTarget()
{
DataDirectory = JD.Core.DataRoot.Value;
_plugin = RemoteServerPlugin.Instance;
_plugin.TargetParent = this;
_plugin.ConnectedChanged += ConnectionStatusChange;
_plugin.RegisterConnectionChangeHandler();//sequenced call.
}
public EngineConnectionStatusUserControl ConnectionStatusControl
{
set { _connectionStatusControl = value; }
get { return _connectionStatusControl; }
}
public TargetEvent targetEvent
{
get { return _targetEvent; }
}
public uint Signature
{
get { return _plugin.Signature; }
}
public bool IsConnected
{
get { return _plugin != null && _plugin.IsConnected; }
}
public string Platform
{
set { _platform = value; }
get { return _platform; }
}
public string DataDirectory
{
set
{
_dataDirectory = value;
if (_dataDirectory.EndsWith("\\") || _dataDirectory.EndsWith("/"))
_combinedDataDirectory = _dataDirectory;
else
_combinedDataDirectory = _dataDirectory + "/";
_combinedDataDirectory = _combinedDataDirectory.Replace("\\", "/");
_combinedDataDirectory = _combinedDataDirectory.ToLower();
}
get { return _dataDirectory; }
}
public string GetCombinedDataDirectory(string path)
{
return _combinedDataDirectory + path;
}
public void OpenMap(string _filename)
{
string relativeFilename = GetOriginalRelativeName(_filename);
_plugin.OpenMap(relativeFilename);
}
public string GetOriginalRelativeName(string filename)
{
string tmp = filename.ToLower();
tmp = tmp.Replace("\\", "/");
string directoryClean = _combinedDataDirectory;
tmp = tmp.Replace(directoryClean, "");
return tmp;
}
public void GetLogCookText(out string _text)
{
lock (_bufferLogCookOutput)
{
_text = _bufferLogCookOutput;
_bufferLogCookOutput = "";
}
}
public void GetLogErrorsText(out string _text)
{
lock (_bufferLogErrorsOutput)
{
_text = _bufferLogErrorsOutput;
_bufferLogErrorsOutput = "";
}
}
public void GetLogEngineOutput(out string _text)
{
lock (_bufferLogEngineOutput)
{
_text = _bufferLogEngineOutput;
_bufferLogEngineOutput = "";
}
}
public void GetLogReloadFile(out string fileName)
{
lock (_bufferLogHotReloadFile)
{
fileName = _bufferLogHotReloadFile;
_bufferLogHotReloadFile = "";
}
}
public string CleanFilename(string filename)
{
string cleanedName = filename.ToLower();
cleanedName = cleanedName.Replace(".ckd", "");
cleanedName = cleanedName.Replace("cache/itf_cooked/" + _platform.ToLower() + "/", "");
return cleanedName;
}
public void AddGlobalOutput(string _text)
{
_globalOutput += _text;
_globalOutput += "/n";
}
public void OnAnalyze(ref IBlob _blob)
{
UInt32 count = 0;
_blob.Extract(out count);
List<NodeFile> nodeFileList = new List<NodeFile>();
for (UInt32 index = 0; index < count; ++index)
{
NodeFile nodeFile = new NodeFile();
nodeFile.extract(ref _blob);
nodeFileList.Add(nodeFile);
}
if (targetEvent.OnAnalyzeEvent != null)
targetEvent.OnAnalyzeEvent(ref nodeFileList);
}
public void OnSignatureIssues(ref IBlob _blob)
{
//get the cooker version
Dictionary<uint, uint> cookerSignatures = new Dictionary<uint, uint>();
Dictionary<uint, string> cookerClassResolved = new Dictionary<uint, string>();
_plugin.Engine.getObjectsSignature(ref cookerSignatures, ref cookerClassResolved);
UInt32 count = 0;
_blob.Extract(out count);
string info = "Count Target signature: " + count.ToString() + " " + "Cooker signature: " + cookerSignatures.Count.ToString() + "\r\n";
if (count != cookerSignatures.Count)
{
info += "Signature count mismatch";
}
UInt32 key = 0, signatureCRC = 0;
for (uint index = 0; index < count; index++)
{
_blob.Extract(out key);
_blob.Extract(out signatureCRC);
if (cookerSignatures.ContainsKey(key))
{
if (cookerSignatures[key] != signatureCRC)
{
info += "Mismatch >>";
info += cookerClassResolved[key].ToString();
info += "\r\n";
}
}
}
}
public void OnLogCook(string _text)
{
string finaltext = "\r\n" + _text.Replace("_COOKER]", "] => ");
lock (_bufferLogCookOutput)
{
_bufferLogCookOutput += finaltext;
}
}
public void OnLogFatal(string _text)
{
string finaltext = "[FERROR]=> " + _text + "\r\n";
using (var remoteServerErrorStatistic = new RemoteServerErrorStatistic())
{
remoteServerErrorStatistic.ErrorLog = finaltext;
}
lock (_bufferLogErrorsOutput)
{
_bufferLogErrorsOutput += finaltext;
}
}
public void OnLogError(string _text)
{
string finaltext = "[ERROR]=> " + _text + "\r\n";
using (var remoteServerErrorStatistic = new RemoteServerErrorStatistic())
{
remoteServerErrorStatistic.ErrorLog = finaltext;
}
lock (_bufferLogErrorsOutput)
{
_bufferLogErrorsOutput += finaltext;
}
}
public void OnHotReloadFile(string fileName)
{
string finaltext = "[RELOADING]=> " + fileName + "\r\n";
lock (_bufferLogHotReloadFile)
{
_bufferLogHotReloadFile += finaltext;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void OnPing(string text)
{
if (targetEvent.OnPingEvent != null)
targetEvent.OnPingEvent(text);
}
public void OnDumpEngineOutput(ref IBlob _blob)
{
UInt32 count = 0;
_blob.Extract(out count);
String tmp = "";
for (UInt32 index = 0; index < count; ++index)
{
_blob.Extract(out tmp);
_bufferLogEngineOutput += tmp;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void onSignalCook(bool value)
{
if (targetEvent.OnSignalCookEvent != null)
targetEvent.OnSignalCookEvent(value);
}
public void dumpSignatureIssues()
{
if (IsConnected)
{
_plugin.DumpSignatureIssues();
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void ping()
{
if (IsConnected)
{
_plugin.SendPing(false);
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void testBandwidth()
{
if (IsConnected)
{
_plugin.SendPing(true);
}
}
public void ConnectionStatusChange(object sender, IPlugin e)
{
bool isConnected = _plugin.IsConnected;
if (isConnected)
{
SetPlatformString(_connectionStatusControl.EngineManager.Target);
}
}
public void SetPlatformString(PlatformId _id)
{
switch (_id)
{
case PlatformId.PC:
Platform = "PC";
break;
case PlatformId.X360:
Platform = "X360";
break;
case PlatformId.PS3:
Platform = "PS3";
break;
case PlatformId.ORBIS:
Platform = "ORBIS";
break;
case PlatformId.WIIU:
Platform = "WIIU";
break;
case PlatformId.DURANGO:
Platform = "DURANGO";
break;
case PlatformId.WII:
Platform = "WII";
break;
case PlatformId.CTR:
case PlatformId.EMUWII:
case PlatformId.VITA:
case PlatformId.IPAD:
case PlatformId.UNDEFINED:
default:
Platform = "default";
break;
}
}
public bool CacheFiles
{
set { _cacheFiles = value; }
get { return _cacheFiles; }
}
}
}