Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/fa2-main conflicts #110

Merged
merged 5 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [Bug](https://github.com/trilitech/tezos-unity-sdk/issues/102) with standalone IL2CPP build fails
- [Bug](https://github.com/trilitech/tezos-unity-sdk/issues/103) with closing Beacon database connections before app exit

### Added
- Missing namespaces

### Changed
- Issue Report Form menu item path changed (new path: `Tools/Tezos SDK for Unity/Report an Issue`)


## [1.5.1] - 2023-06-27
### Fixed
Expand Down
4 changes: 2 additions & 2 deletions Editor/IssueReportFormWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Newtonsoft.Json.Linq;
using UnityEngine.Networking;

namespace Tezos.Editor.Windows
namespace TezosSDK.Editor.Windows
{
public class IssueReportFormWindow : EditorWindow
{
Expand All @@ -19,7 +19,7 @@ public class IssueReportFormWindow : EditorWindow
private string errorMessage = "";
private string successMessage = "";

[MenuItem("Tezos SDK/Issue Report Form")]
[MenuItem("Tools/Tezos SDK for Unity/Report an Issue")]
public static void ShowWindow()
{
GetWindow<IssueReportFormWindow>("Issue Report Form");
Expand Down
File renamed without changes.
35 changes: 35 additions & 0 deletions Runtime/Scripts/FileUploaders/BaseUploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections;
using TezosSDK.Scripts.FileUploaders.IPFS;
using UnityEngine;

namespace TezosSDK.Scripts.FileUploaders
{
public abstract class BaseUploader : MonoBehaviour
{
public string SupportedFileExtensions { get; } = ".jpg, .jpeg, .png";

private void Start()
{
DontDestroyOnLoad(gameObject);
}
}

public interface IPinataUploader : IBaseUploader
{
PinataCredentials PinataCredentials { get; set; }
}

public interface IBaseUploader
{
string SupportedFileExtensions { get; }

/// <summary>
/// Upload file that user will select through native menu file picker.
/// </summary>
/// <param name="callback">
/// Executes after asset uploaded with data address.
/// </param>
IEnumerator UploadFile(Action<string> callback);
}
}
3 changes: 3 additions & 0 deletions Runtime/Scripts/FileUploaders/IPFS.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
using System.Collections;
using System.IO;
using System.Text.Json;
using TezosSDK.Scripts.IpfsUploader;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using Logger = TezosSDK.Helpers.Logger;

namespace TezosSDK.Scripts.IpfsUploader
namespace TezosSDK.Scripts.FileUploaders.IPFS
{
public class EditorUploader : BaseUploader, IFileUploader
public class EditorPinataUploader : BaseUploader, IPinataUploader
{
public PinataCredentials PinataCredentials { get; set; }

public IEnumerator UploadFile(Action<string> callback)
{
yield return null;
Expand All @@ -28,8 +31,8 @@ public IEnumerator UploadFile(Action<string> callback)
var form = new WWWForm();
form.AddBinaryData("file", File.ReadAllBytes(path), filename);

var request = UnityWebRequest.Post(ApiUrl, form);
request.SetRequestHeader("Authorization", $"Bearer {ApiKey}");
var request = UnityWebRequest.Post(PinataCredentials.ApiUrl, form);
request.SetRequestHeader("Authorization", $"Bearer {PinataCredentials.ApiKey}");
yield return request.SendWebRequest();

if (request.result == UnityWebRequest.Result.Success)
Expand All @@ -44,4 +47,4 @@ public IEnumerator UploadFile(Action<string> callback)
}
}
}
#endif
#endif
14 changes: 14 additions & 0 deletions Runtime/Scripts/FileUploaders/IPFS/PinataCredentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace TezosSDK.Scripts.FileUploaders.IPFS
{
public class PinataCredentials
{
public string ApiUrl { get; }
public string ApiKey { get; }

public PinataCredentials(string apiKey)
{
ApiUrl = "https://api.pinata.cloud/pinning/pinFileToIPFS";
ApiKey = apiKey;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Scripts/FileUploaders/IPFS/PinataCredentials.cs.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,49 @@
using System.Collections;
using System.Runtime.InteropServices;
using System.Text.Json;
using TezosSDK.Scripts.IpfsUploader;
using UnityEngine;

namespace TezosSDK.Scripts.IpfsUploader
namespace TezosSDK.Scripts.FileUploaders.IPFS
{
public class WebUploader : BaseUploader, IFileUploader
public class WebPinataUploader : BaseUploader, IPinataUploader
{
public PinataCredentials PinataCredentials { get; set; }

public void FileRequestCallback(string path)
{
WebUploaderHelper.SetResult(path);
WebPinataUploaderHelper.SetResult(path);
}

public IEnumerator UploadFile(Action<string> callback)
{
yield return null;
WebUploaderHelper.RequestFile(callback, SupportedFileExtensions);
WebPinataUploaderHelper.RequestFile(callback, SupportedFileExtensions);
}
}
public static class WebUploaderHelper

public static class WebPinataUploaderHelper
{
private static Action<string> _responseCallback;

public static WebUploader InitWebFileLoader()
public static IPinataUploader GetUploader(string apiKey)
{
const string callbackObjectName = nameof(WebUploader);
const string callbackMethodName = nameof(WebUploader.FileRequestCallback);
const string callbackObjectName = nameof(WebPinataUploader);
const string callbackMethodName = nameof(WebPinataUploader.FileRequestCallback);

var webUploaderGameObject = GameObject.Find(nameof(WebUploader));
var webUploaderGameObject = GameObject.Find(nameof(WebPinataUploader));
var webFileUploader = webUploaderGameObject != null
? webUploaderGameObject.GetComponent<WebUploader>()
: new GameObject(nameof(WebUploader)).AddComponent<WebUploader>();
? webUploaderGameObject.GetComponent<WebPinataUploader>()
: new GameObject(nameof(WebPinataUploader)).AddComponent<WebPinataUploader>();

JsInitFileLoader(
webFileUploader.PinataCredentials = new PinataCredentials(apiKey);

JsInitPinataUploader(
callbackObjectName,
callbackMethodName,
webFileUploader.ApiUrl,
webFileUploader.ApiKey);
webFileUploader.PinataCredentials.ApiUrl,
webFileUploader.PinataCredentials.ApiKey);

return webFileUploader;
}

Expand All @@ -48,7 +53,7 @@ public static void RequestFile(Action<string> callback, string extensions)
JsRequestUserFile(extensions);
_responseCallback = callback;
}

public static void SetResult(string response)
{
var ipfsResponse = JsonSerializer.Deserialize<IpfsResponse>(response);
Expand All @@ -62,7 +67,7 @@ private static void Dispose()
}

[DllImport("__Internal")]
private static extern void JsInitFileLoader(
private static extern void JsInitPinataUploader(
string objectName,
string methodName,
string apiUrl,
Expand Down
29 changes: 29 additions & 0 deletions Runtime/Scripts/FileUploaders/JSFileUploader.jslib
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mergeInto(LibraryManager.library, {
JsInitPinataUploader: function (
callbackObjectName,
callbackMethodName,
apiUrl,
apiKey
) {
InitIpfsUploader({
CallbackObjectName: UTF8ToString(callbackObjectName),
CallbackMethodName: UTF8ToString(callbackMethodName),
ApiUrl: UTF8ToString(apiUrl),
ApiKey: UTF8ToString(apiKey),
});
},

JsInitBase64Uploader: function (
callbackObjectName,
callbackMethodName
) {
InitBase64Uploader({
CallbackObjectName: UTF8ToString(callbackObjectName),
CallbackMethodName: UTF8ToString(callbackMethodName)
});
},

JsRequestUserFile: function (extensions) {
FileUploader.RequestUserFile(UTF8ToString(extensions));
},
});
3 changes: 3 additions & 0 deletions Runtime/Scripts/FileUploaders/OnChain.meta

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

38 changes: 38 additions & 0 deletions Runtime/Scripts/FileUploaders/OnChain/EditorBase64Uploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#if UNITY_EDITOR
using System;
using System.Collections;
using System.IO;
using UnityEditor;


namespace TezosSDK.Scripts.FileUploaders.OnChain
{
public class EditorBase64Uploader : BaseUploader, IBaseUploader
{
public IEnumerator UploadFile(Action<string> callback)
{
yield return null;
var imagePath = EditorUtility.OpenFilePanel(
"Select image",
string.Empty,
SupportedFileExtensions
.Replace(".", string.Empty)
.Replace(" ", string.Empty)
);
callback.Invoke(ConvertImageToBase64(imagePath));
}

private static string ConvertImageToBase64(string imagePath)
{
var fileExtension = Path
.GetExtension(imagePath)
.Replace(".", string.Empty)
.ToLower();

var imageBytes = File.ReadAllBytes(imagePath);
var base64String = Convert.ToBase64String(imageBytes);
return $"data:image/{fileExtension};base64,{base64String}";
}
}
}
#endif

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

69 changes: 69 additions & 0 deletions Runtime/Scripts/FileUploaders/OnChain/WebBase64Uploader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections;
using System.Runtime.InteropServices;
using Logger = TezosSDK.Helpers.Logger;
using UnityEngine;

namespace TezosSDK.Scripts.FileUploaders.OnChain
{
public class WebBase64Uploader : BaseUploader, IBaseUploader
{
public void FileRequestCallback(string path)
{
WebBase64UploaderHelper.SetResult(path);
}

public IEnumerator UploadFile(Action<string> callback)
{
yield return null;
WebBase64UploaderHelper.RequestFile(callback, SupportedFileExtensions);
}
}

public static class WebBase64UploaderHelper
{
private static Action<string> _responseCallback;

public static IBaseUploader GetUploader()
{
const string callbackObjectName = nameof(WebBase64Uploader);
const string callbackMethodName = nameof(WebBase64Uploader.FileRequestCallback);

var webUploaderGameObject = GameObject.Find(nameof(WebBase64Uploader));
var uploader = webUploaderGameObject != null
? webUploaderGameObject.GetComponent<WebBase64Uploader>()
: new GameObject(nameof(WebBase64Uploader)).AddComponent<WebBase64Uploader>();

JsInitBase64Uploader(
callbackObjectName,
callbackMethodName);

return uploader;
}

public static void RequestFile(Action<string> callback, string extensions)
{
JsRequestUserFile(extensions);
_responseCallback = callback;
}

public static void SetResult(string response)
{
_responseCallback.Invoke(response);
Dispose();
}

private static void Dispose()
{
_responseCallback = null;
}

[DllImport("__Internal")]
private static extern void JsInitBase64Uploader(
string objectName,
string methodName);

[DllImport("__Internal")]
private static extern void JsRequestUserFile(string extensions);
}
}

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

Loading