mirror of
https://github.com/RaindropViewer/RaindropViewer.git
synced 2026-07-28 15:32:16 +00:00
yea. lazy.
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e0b79538718542d1a429b6ff68b51626
|
||||
timeCreated: 1643183261
|
||||
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using OpenMetaverse;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Disk
|
||||
{
|
||||
public static class DirectoryHelpers
|
||||
{
|
||||
|
||||
// Gives us the user-accessible "external directories" in Android.
|
||||
// Params: preferSDcard - set true if we want the SD card directory
|
||||
// - if SD card is not present, the emulated external directory will be returned.
|
||||
public static string GetAndroidExternalFilesDir(bool preferSDcard)
|
||||
{
|
||||
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
||||
{
|
||||
using (AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
|
||||
{
|
||||
// Get all available external file directories (emulated and sdCards)
|
||||
AndroidJavaObject[] externalFilesDirectories = context.Call<AndroidJavaObject[],AndroidJavaObject[]>("getExternalFilesDirs", null);
|
||||
AndroidJavaObject emulated = null;
|
||||
AndroidJavaObject sdCard = null;
|
||||
|
||||
for (int i = 0; i < externalFilesDirectories.Length; i++)
|
||||
{
|
||||
AndroidJavaObject directory = externalFilesDirectories[i];
|
||||
using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment"))
|
||||
{
|
||||
// Check which one is the emulated and which the sdCard.
|
||||
bool isRemovable = environment.CallStatic<bool> ("isExternalStorageRemovable", directory);
|
||||
bool isEmulated = environment.CallStatic<bool> ("isExternalStorageEmulated", directory);
|
||||
if (isEmulated)
|
||||
emulated = directory;
|
||||
else if (isRemovable && isEmulated == false)
|
||||
sdCard = directory;
|
||||
}
|
||||
}
|
||||
// Return the sdCard if available
|
||||
if (sdCard != null && preferSDcard)
|
||||
return sdCard.Call<string>("getAbsolutePath");
|
||||
else
|
||||
return emulated.Call<string>("getAbsolutePath");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// gives us the base directory where we should be storing the cache files
|
||||
// returns the internal storage if android.
|
||||
public static string GetInternalStorageDir()
|
||||
{
|
||||
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
|
||||
return Application.persistentDataPath; //appdata
|
||||
#endif
|
||||
#if UNITY_ANDROID
|
||||
return GetAndroidExternalFilesDir(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if UNITY_ANDROID
|
||||
// returns path to the external storage in android phone.
|
||||
// returns null if the sd card is not inserted
|
||||
public static string Android_GetExternalCacheDir_WithInternalAsFallback()
|
||||
{
|
||||
return GetAndroidExternalFilesDir(true); //todo : correctly implement this.
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
//easily write to a file
|
||||
//filePath = fully-specified file path
|
||||
public static void WriteToFile(byte[] outbytes, string filePath)
|
||||
{
|
||||
//create parent subfolders
|
||||
var parentDir = Path.GetDirectoryName(filePath);
|
||||
System.IO.Directory.CreateDirectory(parentDir);
|
||||
|
||||
//write file
|
||||
try
|
||||
{
|
||||
System.IO.File.WriteAllBytes(filePath, outbytes);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
OpenMetaverse.Logger.Log("error writing bytes to filepath : "+ filePath + " " + e.ToString(), Helpers.LogLevel.Error);
|
||||
}
|
||||
// Debug.Log($"write: {filePath} ");
|
||||
}
|
||||
|
||||
//easily write to a file
|
||||
//filePath = fully-specified file path
|
||||
public static void WriteToFile(string toWrite, string filePath)
|
||||
{
|
||||
//create parent subfolders
|
||||
var parentDir = Path.GetDirectoryName(filePath);
|
||||
System.IO.Directory.CreateDirectory(parentDir);
|
||||
|
||||
//write file
|
||||
try
|
||||
{
|
||||
System.IO.File.WriteAllText(filePath, toWrite);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
OpenMetaverse.Logger.Log("error writing lines to filepath : "+ filePath, Helpers.LogLevel.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d04c57fafed647e0bacdc153c5b4a849
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Disk
|
||||
{
|
||||
public class FileHelpers
|
||||
{
|
||||
const int BYTES_TO_READ = sizeof(Int64);
|
||||
|
||||
public static bool FilesAreEqual(FileInfo first, FileInfo second)
|
||||
{
|
||||
if (first.Length != second.Length)
|
||||
return false;
|
||||
|
||||
if (string.Equals(first.FullName, second.FullName, StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
|
||||
using (FileStream fs1 = first.OpenRead())
|
||||
using (FileStream fs2 = second.OpenRead())
|
||||
{
|
||||
if (!FileStreamsAreEqual(fs1, fs2)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool FileStreamsAreEqual(Stream fs1, Stream fs2)
|
||||
{
|
||||
int iterations = (int)Math.Ceiling((double)fs1.Length / BYTES_TO_READ);
|
||||
|
||||
byte[] one = new byte[BYTES_TO_READ];
|
||||
byte[] two = new byte[BYTES_TO_READ];
|
||||
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
fs1.Read(one, 0, BYTES_TO_READ);
|
||||
fs2.Read(two, 0, BYTES_TO_READ);
|
||||
|
||||
if (BitConverter.ToInt64(one, 0) != BitConverter.ToInt64(two, 0))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c9963f0b47db4d45b4223b064f8ea941
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,154 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Disk;
|
||||
using OpenMetaverse;
|
||||
using SearchOption = System.IO.SearchOption;
|
||||
|
||||
// on every boot, as soon as possible,
|
||||
// copy any missing/changed items in streaming assets to the data path in user-accessible-space
|
||||
|
||||
|
||||
// CopyStreamingAssetsToPersistentDataPath
|
||||
namespace UnityScripts.Disk
|
||||
{
|
||||
public class StaticFilesCopier
|
||||
{
|
||||
private StaticFilesCopier()
|
||||
{
|
||||
}
|
||||
|
||||
private static StaticFilesCopier _instance;
|
||||
public static StaticFilesCopier GetInstance()
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = new StaticFilesCopier();
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
|
||||
public bool CopyIsDoneAndNoErrors = false;
|
||||
|
||||
// do copy (scan the files in device, copy if not same as staticassets.).
|
||||
public int Work()
|
||||
{
|
||||
BetterStreamingAssets.Initialize(); //fuck, this is easy to forget.
|
||||
int res = CheckOmvDataFolderAndUpdateItIfNecessary();
|
||||
if (res != -1)
|
||||
CopyIsDoneAndNoErrors = true;
|
||||
return res;
|
||||
}
|
||||
|
||||
//returns -1 if failed.
|
||||
private int CheckOmvDataFolderAndUpdateItIfNecessary()
|
||||
{
|
||||
List<string> updatedFiles = null;
|
||||
try
|
||||
{
|
||||
updatedFiles = CopyIfRequired_StreamingAssetsFolder(
|
||||
DirectoryHelpers.GetInternalStorageDir()
|
||||
);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (updatedFiles.Count > 0)
|
||||
{
|
||||
var printString = String.Join(Environment.NewLine, updatedFiles);
|
||||
OpenMetaverse.Logger.Log(
|
||||
"These files were copied from staticAssets : "
|
||||
+ Environment.NewLine
|
||||
+ printString,
|
||||
Helpers.LogLevel.Info
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
OpenMetaverse.Logger.Log(
|
||||
"staticAssets are already latest version in : "
|
||||
+ DirectoryHelpers.GetInternalStorageDir(),
|
||||
Helpers.LogLevel.Info
|
||||
);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// scan all files in streamingAssets folder, and copy any changed/missing files into userDataRoot.
|
||||
// return some files that were updated.
|
||||
private List<string> CopyIfRequired_StreamingAssetsFolder(string appRootPath)
|
||||
{
|
||||
List<String> updatedFiles = new List<string>();
|
||||
|
||||
//everything in StreamingAssets\
|
||||
string slash = Path.DirectorySeparatorChar.ToString();
|
||||
if (!BetterStreamingAssets.DirectoryExists(slash))
|
||||
{
|
||||
throw new Exception("StreamingAssets root not accessible.");
|
||||
}
|
||||
string[] paths = BetterStreamingAssets.GetFiles(slash, "*", SearchOption.AllDirectories);
|
||||
|
||||
if (paths.Length == 0)
|
||||
{
|
||||
throw new Exception("StreamingAssets has no files in it.");
|
||||
}
|
||||
foreach (var sourcePath in paths)
|
||||
{
|
||||
string relativePath = "";
|
||||
|
||||
//find the equivalent path in appRootPath
|
||||
string root = Directory.GetDirectoryRoot(sourcePath);
|
||||
if (root != null/*todo: check for failed get root.*/)
|
||||
{
|
||||
// gotta remove that root slash character '\'
|
||||
relativePath = sourcePath.Substring(root.Length - 1);
|
||||
}
|
||||
|
||||
//1. check for missing file
|
||||
string targetPath = Path.Combine(appRootPath, relativePath);
|
||||
if (!File.Exists(targetPath))
|
||||
{
|
||||
updatedFiles.Add(targetPath);
|
||||
DirectoryHelpers.WriteToFile(
|
||||
BetterStreamingAssets.ReadAllBytes(sourcePath),
|
||||
targetPath);
|
||||
}
|
||||
|
||||
//2. check for diffs in the file.
|
||||
bool fileIsDifferent = false;
|
||||
using (Stream fs1 = File.OpenRead(targetPath))
|
||||
using (Stream fs2 = BetterStreamingAssets.OpenRead(sourcePath))
|
||||
{
|
||||
if (FilesStreamsAreSame(fs1, fs2))
|
||||
{
|
||||
//do nothing
|
||||
}
|
||||
else
|
||||
{
|
||||
fileIsDifferent = true;
|
||||
}
|
||||
}
|
||||
if (fileIsDifferent)
|
||||
{
|
||||
updatedFiles.Add(targetPath);
|
||||
DirectoryHelpers.WriteToFile(
|
||||
BetterStreamingAssets.ReadAllBytes(sourcePath),
|
||||
targetPath);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return updatedFiles;
|
||||
}
|
||||
|
||||
// true if same data in both files.
|
||||
// false if either source or dest file is missing
|
||||
private bool FilesStreamsAreSame(Stream fs1, Stream fs2)
|
||||
{
|
||||
return FileHelpers.FileStreamsAreEqual(fs1, fs2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c516553780c52c448827236a5a5ae788
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "GenericScripts",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:560b04d1a97f54a4e82edc0cbbb69285",
|
||||
"GUID:b6ef318018e211441b43da945a1caf13",
|
||||
"GUID:11f3455556175aa41b2b4d4f2ec8b146"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": true,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9dcc7c0610df60a49a59da5f5f76ac0b
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Assets
|
||||
{
|
||||
[RequireComponent(typeof(RawImage))]
|
||||
class TGALoader_TestComponent : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField]
|
||||
public string pathToTGA;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (string.IsNullOrEmpty(pathToTGA))
|
||||
{
|
||||
Debug.Log("image directory not specified");
|
||||
}
|
||||
|
||||
float timeStart = Time.realtimeSinceStartup;
|
||||
var tex = OpenMetaverse.Imaging.LoadTGAClass.LoadTGA(pathToTGA);
|
||||
|
||||
float timeEnd = Time.realtimeSinceStartup;
|
||||
if (tex == null)
|
||||
{
|
||||
Debug.Log("reading of TGA at path " + pathToTGA + " failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("reading of TGA at path " + pathToTGA + " SUCCESS! \nTook: " + (timeEnd-timeStart) + "seconds");
|
||||
gameObject.GetComponent<RawImage>().texture = tex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 96eb5f46fd666a24abe5d232a59de8c9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,28 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UniRx;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(Toggle))]
|
||||
public class toggleUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Toggle toggle;
|
||||
[SerializeField] public GameObject targetToToggle;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (targetToToggle == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (toggle == null)
|
||||
{
|
||||
toggle = this.GetComponent<Toggle>();
|
||||
}
|
||||
|
||||
toggle.onValueChanged.AddListener(_ => targetToToggle.SetActive(_));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1d49dc2b6dcdc8940a1ac3cb6a4d8683
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user