초기 커밋.
This commit is contained in:
235
Assets/ADL-Plugins/brotli/Scripts/brotlitest.cs
Normal file
235
Assets/ADL-Plugins/brotli/Scripts/brotlitest.cs
Normal file
@@ -0,0 +1,235 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Threading;
|
||||
using System.IO;
|
||||
|
||||
|
||||
|
||||
public class brotlitest : MonoBehaviour {
|
||||
#if (!UNITY_TVOS && !UNITY_WEBGL) || UNITY_EDITOR
|
||||
|
||||
// some variables to get status returns from the functions
|
||||
private int lz1, lz2, lz3, lz4, fbuftest, nFbuftest;
|
||||
|
||||
// a single item ulong array to get the progress of the compression
|
||||
private ulong[] progress = new ulong[1];
|
||||
private ulong[] progress2 = new ulong[1];
|
||||
private ulong[] progress3 = new ulong[1];
|
||||
private ulong[] progress4 = new ulong[1];
|
||||
|
||||
// a test file that will be downloaded to run the tests
|
||||
private string myFile = "testLZ4.tif";
|
||||
|
||||
// the adress from where we download our test file
|
||||
private string uri = "https://dl.dropbox.com/s/r1ccmnreyd460vr/";
|
||||
|
||||
// our path where we do the tests
|
||||
private string ppath;
|
||||
|
||||
private bool compressionStarted;
|
||||
private bool downloadDone, downloadError;
|
||||
|
||||
// a reusable buffer
|
||||
private byte[] buff;
|
||||
|
||||
// buffer operations buffers
|
||||
byte[] bt = null, bt2 = null;
|
||||
|
||||
// fixed size buffer, that don't gets resized, to perform decompression of buffers in them and avoid memory allocations.
|
||||
private byte[] fixedOutBuffer = new byte[1024*768*3];
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
ppath = Application.persistentDataPath;
|
||||
|
||||
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
|
||||
ppath=".";
|
||||
#endif
|
||||
|
||||
buff = new byte[0];
|
||||
|
||||
Debug.Log(ppath);
|
||||
|
||||
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
||||
|
||||
if (!File.Exists(ppath + "/" + myFile))
|
||||
StartCoroutine(DownloadTestFile());
|
||||
else downloadDone = true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); }
|
||||
}
|
||||
|
||||
|
||||
void OnGUI() {
|
||||
|
||||
if (downloadDone) {
|
||||
GUI.Label(new Rect(50, 0, 350, 30), "package downloaded, ready to extract");
|
||||
GUI.Label(new Rect(50, 30, 450, 90), ppath);
|
||||
|
||||
|
||||
if (GUI.Button(new Rect(50, 150, 250, 50), "start brotli test")) {
|
||||
compressionStarted = true;
|
||||
lz1 = 0; lz2 = 0; progress[0] = 0; progress2[0] = 0; progress3[0] = 0; progress4[0] = 0;
|
||||
|
||||
// call the decompresion demo functions.
|
||||
// DoTests();
|
||||
// we call the test function on a thread to able to see progress. WebGL does not support threads.
|
||||
|
||||
Thread th = new Thread(DoTests); th.Start();
|
||||
// native FileBuffer test
|
||||
#if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN
|
||||
StartCoroutine(nativeFileBufferTest());
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
if(downloadError) GUI.Label(new Rect(50, 150, 250, 50), "Download Error!");
|
||||
}
|
||||
|
||||
if (compressionStarted) {
|
||||
// if the return code is 1 then the decompression was succesful.
|
||||
GUI.Label(new Rect(50, 220, 250, 40), "brotli Compress: " + lz1.ToString() + " : " + progress[0].ToString());
|
||||
GUI.Label(new Rect(50, 260, 250, 40), "brotli Decompress: " + lz2.ToString() + " : " + progress2[0].ToString());
|
||||
GUI.Label(new Rect(50, 300, 250, 40), "Buffer Compress: " + lz3.ToString() + " : " + progress3[0].ToString());
|
||||
GUI.Label(new Rect(50, 340, 250, 40), "Buffer Decompress: " + lz4.ToString());
|
||||
|
||||
#if (UNITY_EDITOR || UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX) && !UNITY_EDITOR_WIN
|
||||
GUI.Label(new Rect(50, 380, 250, 40), "FileBuffer test: " + fbuftest.ToString() + " : " + progress3[0].ToString() );
|
||||
GUI.Label(new Rect(50, 420, 320, 40), "Native FileBuffer test: " + nFbuftest.ToString() + " : " + progress4[0].ToString());
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void DoTests() {
|
||||
|
||||
// File tests
|
||||
// compress a file to brotli format.
|
||||
lz1 = brotli.compressFile(ppath+ "/" + myFile, ppath + "/" + myFile + ".br", progress);
|
||||
|
||||
// decompress the previously compressed archive
|
||||
lz2 = brotli.decompressFile(ppath + "/" + myFile + ".br", ppath + "/" + myFile + ".Br.tif", progress2);
|
||||
|
||||
// Buffer tests
|
||||
if (File.Exists(ppath + "/" + myFile)) {
|
||||
bt = File.ReadAllBytes(ppath + "/" + myFile);
|
||||
|
||||
//compress a byte buffer (we write the output buffer to a file for debug purposes.)
|
||||
if (brotli.compressBuffer(bt, ref buff, progress3, true)){
|
||||
lz3 = 1;
|
||||
File.WriteAllBytes(ppath + "/buffer1.brbuf", buff);
|
||||
}
|
||||
|
||||
bt2 = File.ReadAllBytes(ppath + "/buffer1.brbuf");
|
||||
|
||||
// decompress a byte buffer (we write the output buffer to a file for debug purposes.)
|
||||
if (brotli.decompressBuffer(bt2, ref buff, true)){
|
||||
lz4 = 1;
|
||||
File.WriteAllBytes(ppath + "/buffer1.tif", buff);
|
||||
}
|
||||
|
||||
// FIXED BUFFER FUNCTION:
|
||||
int decompressedSize = brotli.decompressBuffer(bt2, fixedOutBuffer, true);
|
||||
if (decompressedSize > 0) Debug.Log(" # Decompress Fixed size Buffer: " + decompressedSize);
|
||||
|
||||
// NEW BUFFER FUNCTION
|
||||
var newBuffer = brotli.decompressBuffer(bt2, true);
|
||||
if (newBuffer != null) { File.WriteAllBytes(ppath + "/buffer1NEW.tif", newBuffer); Debug.Log(" # new Buffer: " + newBuffer.Length); }
|
||||
newBuffer = null;
|
||||
}
|
||||
|
||||
// make FileBuffer test on supported platfoms.
|
||||
#if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN
|
||||
// make a temp buffer to read a br file in.
|
||||
if (File.Exists(ppath + "/" + myFile + ".br")){
|
||||
progress2[0] = 0;
|
||||
byte[] FileBuffer = File.ReadAllBytes(ppath + "/" + myFile + ".br");
|
||||
fbuftest = brotli.decompressFile(null, ppath + "/" + myFile + "FB.tif", progress2, FileBuffer);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
IEnumerator DownloadTestFile() {
|
||||
|
||||
// make sure a previous brotli file having the same name with the one we want to download does not exist in the ppath folder
|
||||
if (File.Exists(ppath + "/" + myFile)) File.Delete(ppath + "/" + myFile);
|
||||
|
||||
Debug.Log("starting download");
|
||||
|
||||
string fileD = uri + myFile;
|
||||
|
||||
// replace the link to the brotli file with your own (although this will work also)
|
||||
using (UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(fileD)) {
|
||||
#if UNITY_5 || UNITY_4
|
||||
yield return www.Send();
|
||||
#else
|
||||
yield return www.SendWebRequest();
|
||||
#endif
|
||||
|
||||
if (www.error != null) {
|
||||
Debug.Log(www.error);
|
||||
downloadError = true;
|
||||
} else {
|
||||
downloadDone = true;
|
||||
|
||||
// write the downloaded brotli file to the ppath directory so we can have access to it
|
||||
// depending on the Install Location you have set for your app, set the Write Access accordingly!
|
||||
File.WriteAllBytes(ppath + "/" + myFile, www.downloadHandler.data);
|
||||
|
||||
Debug.Log("download done");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if (UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_STANDALONE_LINUX || UNITY_EDITOR) && !UNITY_EDITOR_WIN && !UNITY_WEBGL
|
||||
// native file buffer test
|
||||
// For iOS, Android, Linux and MacOSX the plugin can handle a byte buffer as a file. (in this case a native file buffer)
|
||||
// This way you can extract the file or parts of it without writing it to disk.
|
||||
IEnumerator nativeFileBufferTest() {
|
||||
|
||||
//make a check that the intermediate native buffer is not being used!
|
||||
if(brotli.nativeBufferIsBeingUsed) { Debug.Log("Native buffer download is in use"); yield break; }
|
||||
|
||||
// A bool for download checking
|
||||
bool downloadDoneN = false;
|
||||
|
||||
// A native memory pointer
|
||||
IntPtr nativePointer = IntPtr.Zero;
|
||||
|
||||
// int to get the downloaded file size
|
||||
int zsize = 0;
|
||||
|
||||
Debug.Log("Downloading Brotli file to native memory buffer");
|
||||
|
||||
// Here we are calling the coroutine for a pointer. We also get the downloaded file size.
|
||||
StartCoroutine(brotli.downloadBrFileNative("http://telias.free.fr/temp/testLZ4.tif.br", r => downloadDoneN = r, pointerResult => nativePointer = pointerResult, size => zsize = size));
|
||||
|
||||
while (!downloadDoneN) yield return true;
|
||||
|
||||
nFbuftest = brotli.decompressFile(zsize.ToString(), ppath + "/nativeBufferToFileBR.tif", progress4, nativePointer);
|
||||
|
||||
// free the native memory buffer!
|
||||
brotli.brReleaseBuffer(nativePointer);
|
||||
}
|
||||
#endif
|
||||
|
||||
#else
|
||||
void OnGUI(){
|
||||
GUI.Label(new Rect(10,10,500,40),"Please run the WebGL/tvOS demo.");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
12
Assets/ADL-Plugins/brotli/Scripts/brotlitest.cs.meta
Normal file
12
Assets/ADL-Plugins/brotli/Scripts/brotlitest.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5988116fa7305f0408d78ed62421172b
|
||||
timeCreated: 1453302062
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
127
Assets/ADL-Plugins/brotli/Scripts/brotlitestWebGL.cs
Normal file
127
Assets/ADL-Plugins/brotli/Scripts/brotlitestWebGL.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
public class brotlitestWebGL : MonoBehaviour {
|
||||
#if UNITY_WEBGL || UNITY_TVOS
|
||||
|
||||
private bool downloadDone, downloadError;
|
||||
|
||||
private int lz4 = 0;
|
||||
private int fixedDecompress = 0, newBufferSize = 0;
|
||||
// a reusable buffer
|
||||
private byte[] buff;
|
||||
|
||||
// buffer operations buffers
|
||||
byte[] bt = null, bt2 = null;
|
||||
|
||||
private ulong[] progress3 = new ulong[1];
|
||||
|
||||
private Texture2D tex = null;
|
||||
|
||||
// fixed size buffer, that don't gets resized, to perform decompression of buffers in them and avoid memory allocations.
|
||||
private byte[] fixedOutBuffer = new byte[1024*1024*4];
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
buff = new byte[0];
|
||||
|
||||
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
||||
|
||||
tex = new Texture2D(1,1,TextureFormat.RGBA32, false);
|
||||
|
||||
StartCoroutine(DownloadTestFile());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); }
|
||||
}
|
||||
|
||||
|
||||
void OnGUI() {
|
||||
|
||||
if (downloadDone) {
|
||||
GUI.Label(new Rect(50, 0, 350, 30), "package downloaded, ready to extract");
|
||||
} else {
|
||||
if(downloadError) GUI.Label(new Rect(50, 150, 250, 50), "Download Error!");
|
||||
}
|
||||
|
||||
if(lz4 == 1) GUI.DrawTexture(new Rect(360, 10, 575, 300), tex);
|
||||
|
||||
GUI.Label(new Rect(50, 60, 250, 40), "Fixed buffer: " + fixedDecompress.ToString());
|
||||
GUI.Label(new Rect(50, 100, 250, 40), "New buffer: " + fixedDecompress.ToString());
|
||||
}
|
||||
|
||||
|
||||
IEnumerator DoTests() {
|
||||
|
||||
bt2 = new byte[1];
|
||||
|
||||
// decompress the .brotli buffer that contains an image
|
||||
if (brotli.decompressBuffer(bt, ref bt2, true)) lz4 =1;
|
||||
|
||||
// verify by loading the extracted buffer to a texture
|
||||
tex.LoadImage(bt2);
|
||||
|
||||
yield return true;
|
||||
|
||||
// decompress to a fixed size buffer
|
||||
fixedDecompress = brotli.decompressBuffer(bt, fixedOutBuffer, true);
|
||||
|
||||
// decompress to a new returned buffer
|
||||
var newBuffer = brotli.decompressBuffer(bt, true);
|
||||
if(newBuffer != null) newBufferSize = newBuffer.Length;
|
||||
|
||||
newBuffer = null; buff = null;
|
||||
bt2 = null; bt = null;
|
||||
|
||||
}
|
||||
|
||||
IEnumerator DownloadTestFile() {
|
||||
|
||||
Debug.Log("starting download");
|
||||
|
||||
string fileD = "https://dl.dropbox.com/s/jri8c8a4zg5g2o6/bufferpng.brbuf";
|
||||
|
||||
// replace the link to the brotli file with your own (although this will work also)
|
||||
using (UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(fileD)) {
|
||||
#if UNITY_5 || UNITY_4
|
||||
yield return www.Send();
|
||||
#else
|
||||
yield return www.SendWebRequest();
|
||||
#endif
|
||||
|
||||
if (www.error != null) {
|
||||
Debug.Log(www.error);
|
||||
downloadError = true;
|
||||
} else {
|
||||
downloadDone = true;
|
||||
|
||||
bt = new byte[www.downloadHandler.data.Length];
|
||||
Array.Copy(www.downloadHandler.data, bt, www.downloadHandler.data.Length);
|
||||
yield return true;
|
||||
|
||||
StartCoroutine (DoTests());
|
||||
|
||||
Debug.Log("download done");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
void OnGUI(){
|
||||
GUI.Label(new Rect(10, 10, 500, 40), "Only for WebGL or tvOS.");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
11
Assets/ADL-Plugins/brotli/Scripts/brotlitestWebGL.cs.meta
Normal file
11
Assets/ADL-Plugins/brotli/Scripts/brotlitestWebGL.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 572110a2739007042955b0424f0850a4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user