Skip to content

Commit

Permalink
Add Coroutine API
Browse files Browse the repository at this point in the history
  • Loading branch information
pumachen committed Apr 17, 2023
1 parent c4886e5 commit d7e5f65
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 32 deletions.
62 changes: 59 additions & 3 deletions Editor/API/HDAProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using System.IO;
using System.IO.Compression;
using DotLiquid.Util;
using Newtonsoft.Json;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
Expand Down Expand Up @@ -36,10 +37,29 @@ public static void GetHDAHeaderAsync(string hdaName, Action<dynamic> completed,
{
Debug.LogError(request.error);
failed?.Invoke();
}
}
});
}

public static IEnumerator GetHDAHeaderRoutine(string hdaName, Action<dynamic> completed, Action failed = null)
{
Uri uri = GetUri(hdaName);
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 ProcessHDAAsync(this HDAProcessorPreset preset, Action<ZipArchive> completed, Action failed = null, int timeout = 120)
{
ProcessHDAAsync(preset.hda, preset.parms, completed, failed, timeout);
Expand All @@ -55,7 +75,7 @@ public static void ProcessHDAAsync(string hda, IEnumerable<HouParm> parms, Actio
formData.Add(parm.formSection);
}

string downloadedFile = Path.Combine(Path.GetDirectoryName(Application.dataPath), "Temp", $"Harpoon_Response{DateTime.Now.Ticks}.zip");
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;
Expand All @@ -68,14 +88,50 @@ public static void ProcessHDAAsync(string hda, IEnumerable<HouParm> parms, Actio
{
completed?.Invoke(zipArchive);
}

File.Delete(downloadedFile);
}
else
{
Debug.LogError(request.error);
failed?.Invoke();
}
});
}

public static IEnumerator ProcessHDARoutine(string hda, IEnumerable<HouParm> parms, Action<ZipArchive> completed,
Action failed = null, int timeout = 120)
{
Uri uri = GetUri(hda);
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);
}
}
}
}
43 changes: 42 additions & 1 deletion Editor/Parm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using UnityEditor.Formats.Fbx.Exporter;
using UnityEngine;
using UnityEngine.Networking;
using Object = UnityEngine.Object;

namespace Harpoon
{
Expand All @@ -19,6 +20,36 @@ public abstract class HouParm
public abstract void GUILayout();

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

public static IEnumerable<HouParm> CreateParms(dynamic hdaHeader)
{
IEnumerable<dynamic> parmTemplates = hdaHeader.parmTemplateGroup.parmTemplates;
foreach (var parmTemplate in parmTemplates)
{
ParmTemplate template = parmTemplate.ToObject<ParmTemplate>();
if (template.isHidden)
continue;
switch (template.dataType)
{
case (ParmData.Int):
{
yield return new IntParm(parmTemplate.ToObject<IntParmTemplate>());
break;
}
case (ParmData.Float):
{
yield return new FloatParm(parmTemplate.ToObject<FloatParmTemplate>());
break;
}
case (ParmData.String):
{
yield return new StringParm(parmTemplate.ToObject<StringParmTemplate>());
break;
}
}
}
}
}

[Serializable]
Expand Down Expand Up @@ -166,12 +197,22 @@ byte[] rawModel
}
string path = Path.Combine(Application.temporaryCachePath,
$"{template.name}_{model.GetInstanceID()}.fbx");
ExportBinaryFBX(path, model);
ExportFBX(path, model);
var rawFBX = File.ReadAllBytes(path);
File.WriteAllBytes(Path.Combine(Application.temporaryCachePath, "TMP.fbx"), rawFBX);
return rawFBX;
}
}

private static void ExportFBX(string path, GameObject model)
{
model = GameObject.Instantiate(model);
ExportModelOptions exportSettings = new ExportModelOptions();
exportSettings.ExportFormat = ExportFormat.Binary;
exportSettings.EmbedTextures = true;
ModelExporter.ExportObject(path, model, exportSettings);
Object.DestroyImmediate(model);
}

private static void ExportBinaryFBX (string filePath, UnityEngine.Object singleObject)
{
Expand Down
1 change: 1 addition & 0 deletions Editor/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private static IEnumerator SendWebRequestRoutine(UnityWebRequest request, Action
{
yield return request.SendWebRequest();
completed?.Invoke(request);
request.Dispose();
}
}
}
30 changes: 3 additions & 27 deletions Editor/Window/HDAProcessorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class HDAProcessorWindow : EditorWindow
private int hdaIdx = 0;
private string[] hdas = new string[0];
private string[] hdaNames = new string[0];
private List<HouParm> parms;
private HouParm[] parms;
private float progress = 1.0f;

private string hda => hdas[hdaIdx];
Expand Down Expand Up @@ -51,32 +51,8 @@ void UpdateHDAParms()
{
HDAProcessor.GetHDAHeaderAsync(hda, (hdaHeader) =>
{
var parmTemplates = hdaHeader.parmTemplateGroup.parmTemplates;
parms = new List<HouParm>();
for (int i = 0; i < parmTemplates.Count; ++i)
{
ParmTemplate parmTemplate = parmTemplates[i].ToObject<ParmTemplate>();
if (parmTemplate.isHidden)
continue;
switch (parmTemplate.dataType)
{
case (ParmData.Int):
{
parms.Add(new IntParm(parmTemplates[i].ToObject<IntParmTemplate>()));
break;
}
case (ParmData.Float):
{
parms.Add(new FloatParm(parmTemplates[i].ToObject<FloatParmTemplate>()));
break;
}
case (ParmData.String):
{
parms.Add(new StringParm(parmTemplates[i].ToObject<StringParmTemplate>()));
break;
}
}
}
IEnumerable<HouParm> _parms = HouParm.CreateParms(hdaHeader);
parms = _parms.ToArray();
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"licenseUrl": "",
"dependencies": {
"com.unity.editorcoroutines": "1.0.0",
"com.unity.formats.fbx": "4.1.3"
"com.unity.formats.fbx": "5.0.0"
},
"keywords": [],
"author": {
Expand Down

0 comments on commit d7e5f65

Please sign in to comment.