/* http://www.cgsoso.com/forum-211-1.html CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源! CGSOSO 主打游戏开发,影视设计等CG资源素材。 插件如若商用,请务必官网购买! daily assets update for try. U should buy the asset from home store if u use it in your project! */ using UnityEngine; using System.Runtime.InteropServices; using System; using System.Collections; using Paroxe.PdfRenderer; 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(); if (s_Instance == null) s_Instance = new GameObject("WebGL_JSRuntime").AddComponent(); return s_Instance; } } public PDFJS_PromiseCoroutine PreparePromiseCoroutine( Func 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 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 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 PDFJS_TryTerminateRenderWorker(promiseHandle); #endif } #if UNITY_WEBGL && !UNITY_EDITOR [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)] private static extern void PDFJS_TryTerminateRenderWorker(string promiseHandle); #endif } }