123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using UnityEngine;
- using System.Runtime.InteropServices;
- using System;
- using System.Collections;
- using Paroxe.PdfRenderer;
- using Paroxe.PdfRenderer.Internal;
- namespace Paroxe.PdfRenderer.WebGL
- {
- public class PDFJS_Library : MonoBehaviour
- {
- private Hashtable m_PromiseCoroutineMap = new Hashtable();
- private bool m_IsInitialized;
- private static PDFJS_Library s_Instance;
- public static PDFJS_Library Instance
- {
- get
- {
- if (s_Instance == null)
- s_Instance = FindObjectOfType<PDFJS_Library>();
- if (s_Instance == null)
- s_Instance = new GameObject("WebGL_JSRuntime").AddComponent<PDFJS_Library>();
- return s_Instance;
- }
- }
- public PDFJS_PromiseCoroutine PreparePromiseCoroutine(
- Func<PDFJS_PromiseCoroutine, IPDFJS_Promise, object, IEnumerator> coroutine, IPDFJS_Promise promise, object parameters)
- {
- m_PromiseCoroutineMap[promise.PromiseHandle] = new PDFJS_PromiseCoroutine(this, promise, coroutine, parameters);
- return (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promise.PromiseHandle];
- }
- private void Awake()
- {
- DontDestroyOnLoad(gameObject);
- }
- private string GetMessagePromiseHandle(string message)
- {
- string promiseHandle = message.Substring(message.IndexOf('{'));
- return promiseHandle.Substring(0, promiseHandle.IndexOf('}') + 1).Trim();
- }
- private string GetMessageObjectHandle(string message)
- {
- return message.Substring(message.IndexOf("objectHandle:")).Replace("objectHandle:", "").Trim();
- }
- private string GetMessageProgress(string message)
- {
- return message.Substring(message.IndexOf("progress:")).Replace("progress:", "").Trim();
- }
- private void OnPromiseProgress(string message)
- {
- string promiseHandle = GetMessagePromiseHandle(message);
- string progress = GetMessageProgress(message);
- if (m_PromiseCoroutineMap.Contains(promiseHandle))
- {
- PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
- promiseCoroutine.Promise.Progress = float.Parse(progress);
- }
- }
- private void OnPromiseThen(string message)
- {
- string promiseHandle = GetMessagePromiseHandle(message);
- string objectHandle = GetMessageObjectHandle(message);
- if (m_PromiseCoroutineMap.Contains(promiseHandle))
- {
- PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
- promiseCoroutine.Promise.JSObjectHandle = objectHandle;
- promiseCoroutine.Promise.HasSucceeded = true;
- promiseCoroutine.Promise.HasReceivedJSResponse = true;
- m_PromiseCoroutineMap.Remove(promiseHandle);
- }
- }
- private void OnPromiseCatch(string message)
- {
- string promiseHandle = GetMessagePromiseHandle(message);
- string objectHandle = GetMessageObjectHandle(message);
- if (m_PromiseCoroutineMap.Contains(promiseHandle))
- {
- PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
- promiseCoroutine.Promise.JSObjectHandle = objectHandle;
- promiseCoroutine.Promise.HasSucceeded = false;
- promiseCoroutine.Promise.HasReceivedJSResponse = true;
- m_PromiseCoroutineMap.Remove(promiseHandle);
- }
- }
- private void OnPromiseCancel(string message)
- {
- string promiseHandle = GetMessagePromiseHandle(message);
- string objectHandle = GetMessageObjectHandle(message);
- if (m_PromiseCoroutineMap.Contains(promiseHandle))
- {
- PDFJS_PromiseCoroutine promiseCoroutine = (PDFJS_PromiseCoroutine)m_PromiseCoroutineMap[promiseHandle];
- promiseCoroutine.Promise.JSObjectHandle = objectHandle;
- promiseCoroutine.Promise.HasBeenCancelled = true;
- promiseCoroutine.Promise.HasSucceeded = false;
- promiseCoroutine.Promise.HasReceivedJSResponse = true;
- m_PromiseCoroutineMap.Remove(promiseHandle);
- }
- }
- public void OnLibraryInitialized(string message)
- {
- PDFLibrary.Instance.IsInitialized = true;
- }
- public void TryTerminateRenderingWorker(string promiseHandle)
- {
- #if UNITY_WEBGL && !UNITY_EDITOR
- NativeMethods.PDFJS_TryTerminateRenderWorker(promiseHandle);
- #endif
- }
- }
- }
|