void OnEnable() { // Clear requests: if (!Persist) { Clear(); } FileProtocol.OnRequestStarted = delegate(EventTarget et){ ContentPackage cp = et as ContentPackage; if (cp != null) { // Create set: if (Requests == null) { Requests = new ContentPackage[BufferSize]; } // Add to the buffer: Requests[Progress] = cp; // Update fill: if (Fill != Requests.Length) { Fill++; } // Wrap progress: Progress++; if (Progress == Requests.Length) { Progress = 0; } } }; }
/// <summary>Get generic binary at the given path using this protocol. Used for e.g. fonts. /// Once it's been retrieved, this must call package.GotData(theText) internally. /// Note that you can call this directly to avoid all cache checking.</summary> public virtual void OnGetDataNow(ContentPackage package) { }
/// <summary>Get the raw file data.</summary> public override void OnGetDataNow(ContentPackage package) { // Path without the protocol: string rawPath = package.location.Path; // If it contains a : and starts with / then chop it off: if (rawPath != null && rawPath.IndexOf(':') != -1 && rawPath[0] == '/') { rawPath = rawPath.Substring(1); } if (File.Exists(rawPath)) { // Partial? int partialStart; int partialEnd; byte[] data; if (package.GetRange(out partialStart, out partialEnd)) { // Partial request. FileStream fs = new FileStream(rawPath, FileMode.Open, FileAccess.Read); // Seek there: fs.Seek(partialStart, SeekOrigin.Begin); // Setup Content-Range: package.SetPartialResponse(partialStart, partialEnd, (int)fs.Length); int contentLength = (partialEnd + 1) - partialStart; // Read that many bytes into our buffer: data = new byte[contentLength]; // Read: fs.Read(data, 0, data.Length); // Got headers: package.ReceivedHeaders(contentLength); // Ok! fs.Close(); } else { // Get the bytes: data = File.ReadAllBytes(rawPath); // Got headers: package.ReceivedHeaders(data.Length); } // Got data: package.ReceivedData(data, 0, data.Length); } else { // Let the package know: package.Failed(404); } }
void OnGUI() { if (Requests == null) { PowerUIEditor.HelpBox("Monitors all requests being made via PowerUI. Use either XMLHttpRequest or e.g. DataPackage to list here (and note that both work with all your supported protocols, such as 'resources://')."); return; } ScrollPosition = EditorGUILayout.BeginScrollView(ScrollPosition); // For each record.. int index = Progress - Fill; if (index < 0) { index += Requests.Length; } for (int i = 0; i < Fill; i++) { // Get the request: ContentPackage req = Requests[index]; // Just a rough location label for now: GUILayout.Label(req.location.absolute); int readyState = req.readyState; string stateMessage = ""; switch (readyState) { case 1: stateMessage = "open"; break; case 2: stateMessage = "waiting"; break; case 3: stateMessage = "headers"; break; case 4: stateMessage = "finished"; break; } if (readyState == 4) { stateMessage += " (" + req.statusCode + ")"; } GUILayout.Label("State: " + stateMessage); // Move to next index: index++; if (index == Requests.Length) { index = 0; } } EditorGUILayout.EndScrollView(); }
/// <summary>Get the raw file data.</summary> public override void OnGetDataNow(ContentPackage package) { // Unsupported! package.Failed(501); }