Skip to content

Commit

Permalink
Tor Processor (#2)
Browse files Browse the repository at this point in the history
* TOR Processor

---------

Co-authored-by: chenjunbiao <[email protected]>
  • Loading branch information
pumachen and chenjunbiao authored Aug 14, 2023
1 parent bf1afe9 commit 7d5444f
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Editor/API/HDAProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void ProcessHDAAsync(this HDAProcessorPreset preset, Action<ZipArc
ProcessHDAAsync(preset.hda, preset.parms, completed, failed, timeout);
}

public static void ProcessHDAAsync(string hda, IEnumerable<HouParm> parms, Action<ZipArchive> completed,
public static void ProcessHDAAsync(string hda, IEnumerable<Parm> parms, Action<ZipArchive> completed,
Action failed = null, int timeout = 3000)
{
Uri uri = GetUri(hda);
Expand Down Expand Up @@ -97,7 +97,7 @@ public static void ProcessHDAAsync(string hda, IEnumerable<HouParm> parms, Actio
});
}

public static IEnumerator ProcessHDARoutine(string hda, IEnumerable<HouParm> parms, Action<ZipArchive> completed,
public static IEnumerator ProcessHDARoutine(string hda, IEnumerable<Parm> parms, Action<ZipArchive> completed,
Action failed = null, int timeout = 300)
{
Uri uri = GetUri(hda);
Expand Down
45 changes: 45 additions & 0 deletions Editor/API/TORLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Harpoon.Utils;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;

namespace Harpoon
{
public static class TORLibrary
{
/*public static string[] TorLibrary()
{
GetTORLibraryAsync((tors) =>
{
foreach (var tor in tors)
{
Debug.Log(tor);
}
});
}*/

public static void GetTORLibraryAsync(Action<string[]> completed, Action failed = null)
{
Uri uri = new Uri(HarpoonUriBuilder.Root, "api/torlibrary");
UnityWebRequest request = UnityWebRequest.Get(uri);
request.SendWebRequest((request) =>
{
if (request.result == UnityWebRequest.Result.Success)
{
dynamic torLibrary = JsonConvert.DeserializeObject(request.downloadHandler.text);
string[] tors = torLibrary.ToObject<string[]>();
completed?.Invoke(tors);
}
else
{
Debug.Log(request.error);
failed?.Invoke();
}
});
}
}
}
11 changes: 11 additions & 0 deletions Editor/API/TORLibrary.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

136 changes: 136 additions & 0 deletions Editor/API/TORProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
using Newtonsoft.Json;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEditor.PackageManager.Requests;
using UnityEngine;
using UnityEngine.Networking;
using Harpoon.Utils;
using UnityEditor.PackageManager;

namespace Harpoon
{
public static class TORProcessor
{
private static Uri GetUri(string torName)
{
return new Uri(HarpoonUriBuilder.Root, $"api/torprocessor/{torName}");
}

public static void GetTORHeaderAsync(string torName, Action<dynamic> completed, Action failed = null)
{
Uri uri = GetUri(torName);
UnityWebRequest request = UnityWebRequest.Get(uri);
request.SendWebRequest((request) =>
{
if (request.result == UnityWebRequest.Result.Success)
{
completed?.Invoke(JsonConvert.DeserializeObject(request.downloadHandler.text));
}
else
{
Debug.LogError(request.error);
failed?.Invoke();
}
});
}

public static IEnumerator GetTORHeaderRoutine(string torName, Action<dynamic> completed, Action failed = null)
{
Uri uri = GetUri(torName);
using (UnityWebRequest get = UnityWebRequest.Get(uri))
{
var request = get.SendWebRequest();
while (!request.isDone)
yield return null;
if (get.result != UnityWebRequest.Result.Success)
{
if (failed == null)
Debug.LogError(get.error);
else
failed?.Invoke();
}
completed?.Invoke(JsonConvert.DeserializeObject(get.downloadHandler.text));
}
}

/*public static void ProcessTORAsync(this TORProcessorPreset preset, Action<ZipArchive> completed, Action failed = null, int timeout = 300)
{
ProcessTORAsync(preset.tor, preset.parms, completed, failed, timeout);
}*/

public static void ProcessTORAsync(string tor, IEnumerable<Parm> parms, Action<ZipArchive> completed,
Action failed = null, int timeout = 3000)
{
Uri uri = GetUri(tor);
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
foreach (var parm in parms)
{
formData.Add(parm.formSection);
}

string downloadedFile = Path.Combine(Path.GetDirectoryName(Application.temporaryCachePath), "Temp", $"Harpoon_Response{DateTime.Now.Ticks}.zip");
UnityWebRequest post = UnityWebRequest.Post(uri, formData);
post.useHttpContinue = false;
post.timeout = timeout;
post.downloadHandler = new DownloadHandlerFile(downloadedFile);
post.SendWebRequest((request) =>
{
if (request.result == UnityWebRequest.Result.Success)
{
using (var zipArchive = ZipFile.OpenRead(downloadedFile))
{
completed?.Invoke(zipArchive);
}

File.Delete(downloadedFile);
}
else
{
failed?.Invoke();
}
});
}

public static IEnumerator ProcessTORRoutine(string tor, IEnumerable<Parm> parms, Action<ZipArchive> completed,
Action failed = null, int timeout = 300)
{
Uri uri = GetUri(tor);
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
foreach (var parm in parms)
{
formData.Add(parm.formSection);
}
string downloadedFile = Path.Combine(Path.GetDirectoryName(Application.temporaryCachePath), "Temp", $"Harpoon_Response{DateTime.Now.Ticks}.zip");
using (UnityWebRequest post = UnityWebRequest.Post(uri, formData))
{
post.useHttpContinue = false;
post.timeout = timeout;
post.downloadHandler = new DownloadHandlerFile(downloadedFile);
var request = post.SendWebRequest();
while (!request.isDone)
yield return null;
if (post.result == UnityWebRequest.Result.Success)
{
using (var zipArchive = ZipFile.OpenRead(downloadedFile))
{
completed?.Invoke(zipArchive);
}
}
else
{
if (failed == null)
Debug.LogError(post.error);
else
failed?.Invoke();
}
File.Delete(downloadedFile);
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/API/TORProcessor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 80 additions & 12 deletions Editor/Parm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@
namespace Harpoon
{
[Serializable]
public abstract class HouParm
public abstract class Parm
{
public abstract ParmTemplate parmTemplate { get; }
public abstract void GUILayout();

public abstract IMultipartFormSection formSection { get; }
public string name => parmTemplate.name;

public static IEnumerable<HouParm> CreateParms(dynamic hdaHeader)
public static IEnumerable<Parm> CreateParms(dynamic parmTemplateGroup)
{
IEnumerable<dynamic> parmTemplates = hdaHeader.parmTemplateGroup.parmTemplates;
IEnumerable<dynamic> parmTemplates = parmTemplateGroup.parmTemplates;
foreach (var parmTemplate in parmTemplates)
{
ParmTemplate template = parmTemplate.ToObject<ParmTemplate>();
if (template.isHidden)
continue;
switch (template.dataType)
{
case (ParmData.Int):
Expand All @@ -53,7 +51,7 @@ public static IEnumerable<HouParm> CreateParms(dynamic hdaHeader)
}

[Serializable]
public class FloatParm : HouParm
public class FloatParm : Parm
{
public override ParmTemplate parmTemplate => template;
public FloatParmTemplate template;
Expand All @@ -68,11 +66,43 @@ public FloatParm(FloatParmTemplate template)

public override void GUILayout()
{
if(template.isHidden)
return;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(template.label);
for (int i = 0; i < template.numComponents; ++i)
if (template.numComponents == 1)
{
value[i] = EditorGUILayout.FloatField(value[i]);
if (template.minIsStrict && template.maxIsStrict)
{
value[0] = EditorGUILayout.Slider(value[0], template.minValue, template.maxValue);
}
else
{
value[0] = EditorGUILayout.FloatField(value[0]);
}
if (template.minIsStrict)
{
value[0] = Mathf.Max(value[0], template.minValue);
}
if (template.maxIsStrict)
{
value[0] = Mathf.Min(value[0], template.maxValue);
}
}
else
{
for (int i = 0; i < template.numComponents; ++i)
{
value[i] = EditorGUILayout.FloatField(value[i]);
if (template.minIsStrict)
{
value[i] = Mathf.Max(value[i], template.minValue);
}
if (template.maxIsStrict)
{
value[i] = Mathf.Min(value[i], template.maxValue);
}
}
}
EditorGUILayout.EndHorizontal();
}
Expand All @@ -84,7 +114,7 @@ public override IMultipartFormSection formSection
}

[Serializable]
public class IntParm : HouParm
public class IntParm : Parm
{
public override ParmTemplate parmTemplate => template;
public IntParmTemplate template;
Expand All @@ -103,18 +133,54 @@ public override IMultipartFormSection formSection

public override void GUILayout()
{
if(template.isHidden)
return;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(template.label);
for (int i = 0; i < template.numComponents; ++i)
if (template.numComponents == 1)
{
if (template.menuItems != null && template.menuItems.Length > 0)
{
value[0] = EditorGUILayout.Popup(value[0], template.menuLabels);
}
else if (template.minIsStrict && template.maxIsStrict)
{
value[0] = EditorGUILayout.IntSlider(value[0], template.minValue, template.maxValue);
}
else
{
value[0] = EditorGUILayout.IntField(value[0]);
}
if (template.minIsStrict)
{
value[0] = Mathf.Max(value[0], template.minValue);
}
if (template.maxIsStrict)
{
value[0] = Mathf.Min(value[0], template.maxValue);
}
}
else
{
value[i] = EditorGUILayout.IntField(value[i]);
for (int i = 0; i < template.numComponents; ++i)
{
value[i] = EditorGUILayout.IntField(value[i]);
if (template.minIsStrict)
{
value[i] = Mathf.Max(value[i], template.minValue);
}
if (template.maxIsStrict)
{
value[i] = Mathf.Min(value[i], template.maxValue);
}
}
}
EditorGUILayout.EndHorizontal();
}
}

[Serializable]
public class StringParm : HouParm
public class StringParm : Parm
{
public override ParmTemplate parmTemplate => template;
public StringParmTemplate template;
Expand All @@ -136,6 +202,8 @@ public StringParm(StringParmTemplate template)

public override void GUILayout()
{
if(template.isHidden)
return;
switch (template.stringType)
{
case StringParmType.FileReference:
Expand Down
Loading

0 comments on commit 7d5444f

Please sign in to comment.