using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using UnityEngine.Networking;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.Exceptions;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine.ResourceManagement.Util;
namespace UnityEngine.ResourceManagement.ResourceProviders
{
///
/// Provides raw text from a local or remote URL.
///
[DisplayName("Text Data Provider")]
public class TextDataProvider : ResourceProviderBase
{
///
/// Controls whether errors are logged - this is disabled when trying to load from the local cache since failures are expected
///
public bool IgnoreFailures { get; set; }
internal class InternalOp
{
TextDataProvider m_Provider;
UnityWebRequestAsyncOperation m_RequestOperation;
WebRequestQueueOperation m_RequestQueueOperation;
ProvideHandle m_PI;
bool m_IgnoreFailures;
private bool m_Complete = false;
private int m_Timeout = 0;
private float GetPercentComplete() { return m_RequestOperation != null ? m_RequestOperation.progress : 0.0f; }
public void Start(ProvideHandle provideHandle, TextDataProvider rawProvider)
{
m_PI = provideHandle;
m_PI.SetWaitForCompletionCallback(WaitForCompletionHandler);
provideHandle.SetProgressCallback(GetPercentComplete);
m_Provider = rawProvider;
// override input options with options from Location if included
if (m_PI.Location.Data is ProviderLoadRequestOptions providerData)
{
m_IgnoreFailures = providerData.IgnoreFailures;
m_Timeout = providerData.WebRequestTimeout;
}
else
{
m_IgnoreFailures = rawProvider.IgnoreFailures;
m_Timeout = 0;
}
var path = m_PI.ResourceManager.TransformInternalId(m_PI.Location);
if (ResourceManagerConfig.ShouldPathUseWebRequest(path))
{
SendWebRequest(path);
}
else if (File.Exists(path))
{
#if NET_4_6
if (path.Length >= 260)
path = @"\\?\" + path;
#endif
var text = File.ReadAllText(path);
object result = ConvertText(text);
m_PI.Complete(result, result != null, result == null ? new Exception($"Unable to load asset of type {m_PI.Type} from location {m_PI.Location}.") : null);
m_Complete = true;
}
else
{
Exception exception = null;
//Don't log errors when loading from the persistentDataPath since these files are expected to not exist until created
if (m_IgnoreFailures)
{
m_PI.Complete