This commit is contained in:
2025-06-09 00:11:54 +08:00
parent d8c6eb0bd6
commit c773a6bb8d
11207 changed files with 48929 additions and 304 deletions

View File

@@ -0,0 +1,106 @@
/* INFINITY CODE 2013-2019 */
/* http://www.infinity-code.com */
using System;
using System.Collections.Generic;
using System.IO;
namespace InfinityCode.RealWorldTerrain.Net
{
public abstract class RealWorldTerrainDownloadItem
{
public delegate void OnCompleteHandler(ref byte[] data);
public Action<RealWorldTerrainDownloadItem> OnSuccess;
public Action<RealWorldTerrainDownloadItem> OnError;
public Action<RealWorldTerrainDownloadItem> OnComplete;
public event OnCompleteHandler OnData;
public long averageSize = 0;
public bool complete;
public object exclusiveLock;
public string filename;
public bool generateErrorFile = false;
public Dictionary<string, string> headers;
public bool ignoreRequestProgress;
public string url;
private Dictionary<string, object> fields;
private string _errorFilename;
public object this[string key]
{
get
{
if (fields == null) return null;
object obj = null;
fields.TryGetValue(key, out obj);
return obj;
}
set
{
if (fields == null) fields = new Dictionary<string, object>();
fields[key] = value;
}
}
private string errorFilename
{
get
{
if (string.IsNullOrEmpty(_errorFilename))
{
if (string.IsNullOrEmpty(filename)) _errorFilename = String.Empty;
else _errorFilename = filename.Substring(0, filename.LastIndexOf(".") + 1) + "err";
}
return _errorFilename;
}
}
public bool exists
{
get { return File.Exists(filename) || File.Exists(errorFilename); }
}
public abstract float progress { get; }
public abstract void CheckComplete();
public void CreateErrorFile()
{
if (!generateErrorFile) return;
File.WriteAllBytes(errorFilename, new byte[] { });
}
public void DispatchCompete(ref byte[] data)
{
if (OnData != null) OnData(ref data);
if (OnComplete != null) OnComplete(this);
}
public virtual void Dispose()
{
fields = null;
OnSuccess = null;
OnError = null;
OnData = null;
}
public T GetField<T>(string key)
{
if (fields == null) return default(T);
object obj = null;
fields.TryGetValue(key, out obj);
return (T) obj;
}
protected void SaveWWWData(byte[] bytes)
{
if (!string.IsNullOrEmpty(filename) && bytes != null) File.WriteAllBytes(filename, bytes);
}
public abstract void Start();
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 048cd6da859c09f48b5962bd1f7b2297
timeCreated: 1532788443
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,42 @@
/* INFINITY CODE 2013-2019 */
/* http://www.infinity-code.com */
using System;
namespace InfinityCode.RealWorldTerrain.Net
{
public class RealWorldTerrainDownloadItemAction : RealWorldTerrainDownloadItem
{
public Action<RealWorldTerrainDownloadItemAction> OnStart;
public Action<RealWorldTerrainDownloadItemAction> OnCheckComplete;
public override float progress
{
get { return 0; }
}
public RealWorldTerrainDownloadItemAction()
{
RealWorldTerrainDownloadManager.Add(this);
}
public override void CheckComplete()
{
if (OnCheckComplete != null) OnCheckComplete(this);
else complete = true;
}
public override void Dispose()
{
base.Dispose();
OnStart = null;
}
public override void Start()
{
if (OnStart != null) OnStart(this);
else complete = true;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 734f722b1299e724aacdb2905dfcd054
timeCreated: 1532788443
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,101 @@
/* INFINITY CODE */
/* https://infinity-code.com */
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace InfinityCode.RealWorldTerrain.Net
{
public class RealWorldTerrainDownloadItemUnityWebRequest : RealWorldTerrainDownloadItem
{
public static Func<string, UnityWebRequest> OnCreateWebRequest;
public static Action<UnityWebRequest> OnPrepareRequest;
private static CertificateValidator _certificateValidator;
public UnityWebRequest uwr;
public static CertificateValidator certificateValidator
{
get
{
if (_certificateValidator == null) _certificateValidator = new CertificateValidator();
return _certificateValidator;
}
}
public RealWorldTerrainDownloadItemUnityWebRequest(string url)
{
UnityWebRequest request;
if (OnCreateWebRequest != null) request = OnCreateWebRequest(url);
else request = UnityWebRequest.Get(url);
request.certificateHandler = certificateValidator;
if (OnPrepareRequest != null) OnPrepareRequest(request);
RealWorldTerrainDownloadManager.Add(this);
uwr = request;
}
public RealWorldTerrainDownloadItemUnityWebRequest(UnityWebRequest uwr)
{
RealWorldTerrainDownloadManager.Add(this);
this.uwr = uwr;
}
public override float progress
{
get { return uwr.downloadProgress; }
}
public override void CheckComplete()
{
if (!uwr.isDone) return;
if (string.IsNullOrEmpty(uwr.error))
{
byte[] bytes = uwr.downloadHandler.data;
SaveWWWData(bytes);
DispatchCompete(ref bytes);
}
else Debug.LogWarning("Download failed: " + uwr.url + "\n" + uwr.error);
RealWorldTerrainDownloadManager.completeSize += averageSize;
complete = true;
Dispose();
}
public override void Dispose()
{
base.Dispose();
if (uwr != null)
{
uwr.Dispose();
uwr = null;
}
}
public override void Start()
{
if (headers != null)
{
foreach (var header in headers) uwr.SetRequestHeader(header.Key, header.Value);
}
uwr.SendWebRequest();
}
public class CertificateValidator : CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
return true;
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9685a11a17e5b734d8677a812bc73aeb
timeCreated: 1532788443
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,87 @@
/* INFINITY CODE 2013-2019 */
/* http://www.infinity-code.com */
using System;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
namespace InfinityCode.RealWorldTerrain.Net
{
public class RealWorldTerrainDownloadItemWebClient : RealWorldTerrainDownloadItem
{
public Action<WebClient> OnPrepare;
private float _progress;
private WebClient client;
public RealWorldTerrainDownloadItemWebClient(string url)
{
RealWorldTerrainDownloadManager.Add(this);
this.url = url;
}
public override float progress
{
get { return _progress; }
}
public override void CheckComplete()
{
}
public override void Dispose()
{
base.Dispose();
client.Dispose();
client = null;
}
private void OnDownloadDataComplete(object sender, DownloadDataCompletedEventArgs e)
{
if (e.Error != null)
{
Debug.LogWarning("Download error: " + url + "\n" + e.Error);
CreateErrorFile();
if (OnError != null) OnError(this);
}
else
{
byte[] data = e.Result;
if (data.Length != 0)
{
SaveWWWData(data);
DispatchCompete(ref data);
}
else CreateErrorFile();
}
RealWorldTerrainDownloadManager.completeSize += averageSize;
_progress = 1;
complete = true;
}
private void OnProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
_progress = e.ProgressPercentage / 100f;
}
public override void Start()
{
client = new WebClient();
client.DownloadDataCompleted += OnDownloadDataComplete;
client.DownloadProgressChanged += OnProgressChanged;
if (headers != null)
{
foreach (KeyValuePair<string, string> header in headers) client.Headers.Add(header.Key, header.Value);
}
if (OnPrepare != null) OnPrepare(client);
client.DownloadDataAsync(new Uri(url));
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c88fec65cf32f334e847b4675e4ae0ac
timeCreated: 1532788443
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,158 @@
/* INFINITY CODE */
/* https://infinity-code.com */
using System;
using System.Collections.Generic;
using System.Linq;
using InfinityCode.RealWorldTerrain.Windows;
using UnityEngine;
using UnityEngine.Networking;
namespace InfinityCode.RealWorldTerrain.Net
{
public static class RealWorldTerrainDownloadManager
{
private const int maxDownloadItem = 16;
public static long completeSize;
public static bool finish;
private static List<RealWorldTerrainDownloadItem> activeItems;
private static List<RealWorldTerrainDownloadItem> items;
private static long totalSize;
public static int count
{
get
{
if (items == null) return -1;
return items.Count;
}
}
public static double progress
{
get
{
if (activeItems == null || activeItems.Count == 0) return 0;
double localProgress = activeItems.Sum(i =>
{
if (i.ignoreRequestProgress) return 0;
return (double) i.progress * i.averageSize;
}) / totalSize;
double totalProgress = completeSize / (double)totalSize + localProgress;
return totalProgress;
}
}
public static int totalSizeMB
{
get { return Mathf.RoundToInt(totalSize / (float)RealWorldTerrainFileSystem.MB); }
}
public static void Add(RealWorldTerrainDownloadItem item)
{
if (items == null) items = new List<RealWorldTerrainDownloadItem>();
items.Add(item);
}
public static void CheckComplete()
{
foreach (RealWorldTerrainDownloadItem item in activeItems) item.CheckComplete();
if (!RealWorldTerrainWindow.isCapturing) return;
activeItems.RemoveAll(i => i.complete);
while (activeItems.Count < maxDownloadItem && items.Count > 0)
{
if (!StartNextDownload()) break;
}
if (activeItems.Count == 0 && items.Count == 0) finish = true;
}
public static void Dispose()
{
if (activeItems != null)
{
foreach (RealWorldTerrainDownloadItem item in activeItems) item.Dispose();
activeItems = null;
}
items = null;
}
public static string EscapeURL(string url)
{
return UnityWebRequest.EscapeURL(url);
}
public static void Start()
{
finish = false;
completeSize = 0;
if (items == null || items.Count == 0)
{
finish = true;
return;
}
activeItems = new List<RealWorldTerrainDownloadItem>();
try
{
totalSize = items.Sum(i => i.averageSize);
}
catch
{
RealWorldTerrainWindow.isCapturing = false;
Dispose();
return;
}
CheckComplete();
}
public static bool StartNextDownload()
{
if (items == null || items.Count == 0) return false;
int index = 0;
RealWorldTerrainDownloadItem current = null;
while (index < items.Count)
{
RealWorldTerrainDownloadItem item = items[index];
if (item.exclusiveLock != null)
{
bool hasAnotherSomeRequest = false;
foreach (RealWorldTerrainDownloadItem activeItem in activeItems)
{
if (item.exclusiveLock == activeItem.exclusiveLock)
{
hasAnotherSomeRequest = true;
break;
}
}
if (!hasAnotherSomeRequest)
{
current = item;
break;
}
}
else
{
current = item;
break;
}
index++;
}
if (current == null) return false;
items.RemoveAt(index);
if (current.exists) return true;
current.Start();
activeItems.Add(current);
return true;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5b993d7b611a21e4eace2e57085c6785
timeCreated: 1532788443
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: