Newer
Older
using System.IO; //
using UnityEngine.Networking;
using static CommunicationEvents;
using static UIconfig;
//Uploading Files from StreamingAsset folder to the Persistent Folder for Android Devices
public static class StreamingAssetLoader
{
Stefan Richter
committed
public static string StreamToDataPath_Folder = "StreamToDataPath";
public static string StreamToDataPath_Folder_Cookie = "cookie_dataPath.txt";
public static string StreamToPersistentDataPath_Folder = "StreamToPersistentDataPath";
public static string StreamToPersistentDataPath_Folder_Cookie = "cookie_persistentDataPath.txt";
public static string StreamToDataPath_withHandler_Folder = "StreamToDataPath_withHandler";
public static string PersDataPathToPlayerSaveGame_Path = "stages/SaveGames";
public static string Target_StreamToDataPath_UnityEditor = "UnityEditor";
public static string ConfigDir = "Config";
public static string ConfigFile_Network = "Network.JSON";
//For Android, Everything in StreamingAssets must be registered!
public static string StreamToPersistentDataPath_FILE_1 = "scrolls.json";
public static string Stage_Folder = "Stages";
public static string Stage_1 = "TechDemo A.JSON";
public static string Stage_2 = "TechDemo B.JSON";
public static void ResetPlayerConfig()
{
string sourcePath = Path.Combine(Application.streamingAssetsPath, StreamToPersistentDataPath_Folder, ConfigDir);
string targetFolder = Application.persistentDataPath;
RereadFileWithUnityWebRequest(sourcePath, ConfigDir, ConfigFile_Network, targetFolder);
NetworkJSON_Load();
}
public static bool ReloadManualy_StreamToPers()
{
ResetPlayerConfig();
string sourcePath = Path.Combine(Application.streamingAssetsPath, StreamToPersistentDataPath_Folder);
string targetFolder = Application.persistentDataPath;
RereadFileWithUnityWebRequest(sourcePath, "", StreamToPersistentDataPath_Folder_Cookie, targetFolder);
RereadFileWithUnityWebRequest(sourcePath, "", StreamToPersistentDataPath_FILE_1, targetFolder);
NetworkJSON_Load();
//Debug.Log("Reloaded_PP");
return true;
}
public static bool ReloadManualy_StreamToDataPath()
{
string sourcePath = Path.Combine(Application.streamingAssetsPath, StreamToDataPath_Folder);
string targetFolder = Application.dataPath;
RereadFileWithUnityWebRequest(sourcePath, "", StreamToDataPath_Folder_Cookie, targetFolder);
NetworkJSON_Load();
//Debug.Log("Reloaded_DP");
return true;
}
public static bool ReloadManualy_StreamToDataPathWithHandler(string TargetDir_1)
{
string sourcePath = Path.Combine(Application.streamingAssetsPath, StreamToDataPath_withHandler_Folder);
//Debug.Log(targetFolder_dpwh);
string sourcePath2 = Path.Combine(Application.streamingAssetsPath, StreamToDataPath_withHandler_Folder, Stage_Folder);
RereadFileWithUnityWebRequest(sourcePath2, Stage_Folder, Stage_1, targetFolder_dpwh);
RereadFileWithUnityWebRequest(sourcePath2, Stage_Folder, Stage_2, targetFolder_dpwh);
NetworkJSON_Load();
//Debug.Log("Reloaded_DPwH");
return true;
}
//---------------------------------------------------------------------------------------
Stefan Richter
committed
//public static ID_toPath toPath = ID_toPath.DataPath;
public enum ID_toPath
{
DataPath = 0,
PersistentDataPath = 1,
Stefan Richter
committed
Stefan Richter
committed
}
Stefan Richter
committed
public static bool checkPersistentDataPath()
Stefan Richter
committed
return checkFileExistence(Application.persistentDataPath, StreamToPersistentDataPath_Folder_Cookie);
}
public static bool checkDataPath()
{
return checkFileExistence(Application.dataPath, StreamToDataPath_Folder_Cookie);
}
public static bool checkFileExistence(string sourcepath, string filename)
{
Stefan Richter
committed
string filePath = sourcepath;
filePath = Path.Combine(filePath, filename);
if (System.IO.File.Exists(filePath))
{
//Debug.Log("FileFound: " + filePath );
return true;
}
//Debug.Log("NoFileFound: " + filePath);
return false;
Stefan Richter
committed
}
public static void NetworkJSON_Save()
{
NetworkJSON myObject = new NetworkJSON();
//MyClass myObject = new MyClass();
myObject.newIP = CommunicationEvents.newIP;
myObject.lastIP = CommunicationEvents.lastIP;
myObject.IPslot1 = CommunicationEvents.IPslot1;
myObject.IPslot2 = CommunicationEvents.IPslot2;
myObject.IPslot3 = CommunicationEvents.IPslot3;
Stefan Richter
committed
myObject.IPslot4 = CommunicationEvents.IPslot4;
myObject.IPslot5 = CommunicationEvents.IPslot5;
myObject.selecIP = CommunicationEvents.selecIP;
myObject.ControlMode = UIconfig.controlMode.ToString();
myObject.TouchMode = UIconfig.touchControlMode;
myObject.TAvisibility = UIconfig.TAvisibility;
myObject.autoOSrecognition = CommunicationEvents.autoOSrecognition;
myObject.autoSettingsAdaption = UIconfig.autoSettingsAdaption;
myObject.Opsys = CommunicationEvents.Opsys.ToString();
myObject.FrameITUIversion = UIconfig.FrameITUIversion;
myObject.InputManagerVersion = UIconfig.InputManagerVersion;
myObject.colliderScale_all = UIconfig.colliderScale_all;
myObject.cursorSize = UIconfig.cursorSize;
Stefan Richter
committed
myObject.camRotatingSensitivity = UIconfig.camRotatingSensitivity;
Stefan Richter
committed
myObject.MouseKeepingInWindow = UIconfig.MouseKeepingInWindow;
Stefan Richter
committed
myObject.scrolldirection = UIconfig.scrolldirection;
Stefan Richter
committed
//Data storage
SafeCreateDirectory(Path.Combine(Application.persistentDataPath, ConfigDir));
//string json = JsonUtility.ToJson(date);
string json = JsonUtility.ToJson(myObject);
StreamWriter Writer = new StreamWriter(Path.Combine(Application.persistentDataPath, ConfigDir, ConfigFile_Network));
Writer.Write(json);
Writer.Flush();
Writer.Close();
}
public static DirectoryInfo SafeCreateDirectory(string path)
{
//Generate if you don't check if the directory exists
if (Directory.Exists(path))
{
Stefan Richter
committed
//Debug.Log(path + " exists");
return null;
Stefan Richter
committed
Stefan Richter
committed
//Debug.Log(path + " create");
return Directory.CreateDirectory(path);
}
public static void ResetPlayerSaveGame()
{
string path_a = Path.Combine(Application.persistentDataPath, PersDataPathToPlayerSaveGame_Path);
deleteADirectoryAndSubDir(path_a);
SafeCreateDirectory(path_a);
}
Stefan Richter
committed
public static void ResetPersistentDataPath()
{
RereadFiles_PersistentDataPath();
NetworkJSON_Load();
}
public static void ResetDataPath()
RereadFiles_DataPath();
public static void ReloadStreamingAsset()
{
RereadFiles_PersistentDataPath();
RereadFiles_DataPath();
NetworkJSON_Load();
//CSform.CheckIPAdr();
}
public static void deleteADirectoryAndSubDir(string path)
{
// Delete a directory and all subdirectories with Directory static method...
if (System.IO.Directory.Exists(@path))
{
try
{
System.IO.Directory.Delete(@path, true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
}
public static void RereadFiles_PersistentDataPath()
//Debug.Log("Reread_PersistentDataPath");
Stefan Richter
committed
//RereadFileUWR(StreamingAssetLoader.file_1_path, StreamingAssetLoader.file_1, ID_toPath.PersistentDataPath);
//----
string rootPath = Path.Combine(Application.streamingAssetsPath, StreamToPersistentDataPath_Folder);
string targetFolder = Application.persistentDataPath;
public static void RereadFiles_DataPath()
Stefan Richter
committed
string rootPath = Path.Combine(Application.streamingAssetsPath, StreamToDataPath_Folder);
string targetFolder = Application.dataPath;
if (Application.isEditor)
{
targetFolder = Path.Combine(Application.persistentDataPath, Target_StreamToDataPath_UnityEditor);
}
Stefan Richter
committed
RereadFiles_DataPath_withHandler();
if (!ReReadFiles(rootPath, targetFolder))
{
ReloadManualy_StreamToDataPath();
}
Stefan Richter
committed
}
public static void RereadFiles_DataPath_withHandler()
{
string rootPath = Path.Combine(Application.streamingAssetsPath, StreamToDataPath_withHandler_Folder);
string targetFolder_wh = Application.dataPath;
//Debug.Log(CommunicationEvents.Opsys);
Stefan Richter
committed
if (CommunicationEvents.Opsys == OperationSystem.Android)
{
targetFolder_wh = Application.persistentDataPath;
//Debug.Log(OperationSystem.Android + " " + targetFolder_wh);
Stefan Richter
committed
}
if (Application.isEditor)
{
targetFolder_wh = Path.Combine(Application.persistentDataPath, Target_StreamToDataPath_UnityEditor);
}
Stefan Richter
committed
if (!ReReadFiles(rootPath, targetFolder_wh))
{
//Debug.Log( " 2 " + targetFolder_wh);
ReloadManualy_StreamToDataPathWithHandler(targetFolder_wh);
}
Stefan Richter
committed
}
public static bool ReReadFiles(string rootPath, string targetFolder)//ID_toPath PathHandler)
if (!Directory.Exists(rootPath)) { Debug.Log("no Dir: " + rootPath); return false; }
Stefan Richter
committed
//----
Stefan Richter
committed
string dir = "";
DirectoryInfo dirInfo = new DirectoryInfo(@rootPath);
Stefan Richter
committed
FileInfo[] Files = dirInfo.GetFiles("*");
foreach (FileInfo file in Files)
{
if (file.Name.Contains(".meta")) { continue; };
if (!RereadFileWithUnityWebRequest(rootPath, dir, file.Name, targetFolder))
{
return false;
}
Stefan Richter
committed
}
//----
Stefan Richter
committed
string[] dirs = Directory.GetDirectories(rootPath, "*", SearchOption.AllDirectories);
foreach (string dir_fullpath in dirs)
{
int pos = dir_fullpath.IndexOf(rootPath);
int endpos = pos;
if (dir_fullpath.Length > (rootPath.Length + 1)) { endpos = rootPath.Length + 1; } else { endpos = rootPath.Length; }
dir = dir_fullpath.Remove(pos, endpos);
dirInfo = new DirectoryInfo(@dir_fullpath);
Stefan Richter
committed
Files = dirInfo.GetFiles("*");
foreach (FileInfo file in Files)
{
if (file.Name.Contains(".meta")) { continue; };
if (!RereadFileWithUnityWebRequest(dir_fullpath, dir, file.Name, targetFolder))
{
Stefan Richter
committed
}
}
//Debug.Log("Dir Reloaded");
return true;
}
public static void NetworkJSON_Load()
{
NetworkJSON_Load_x(Application.persistentDataPath);
}
Stefan Richter
committed
public static void NetworkJSON_Load_0()
{
var x = Path.Combine(Application.streamingAssetsPath, StreamToPersistentDataPath_Folder);
Stefan Richter
committed
Stefan Richter
committed
public static void NetworkJSON_Load_x(string path)
var reader = new StreamReader(Path.Combine(path, ConfigDir, ConfigFile_Network));
string json = reader.ReadToEnd();
reader.Close();
NetworkJSONonlyString myObjsOnlyStrings = JsonUtility.FromJson<NetworkJSONonlyString>(json);
NetworkJSON myObjs = JsonUtility.FromJson<NetworkJSON>(json);
if (string.IsNullOrEmpty(myObjsOnlyStrings.newIP))
{
Stefan Richter
committed
CommunicationEvents.newIP = "";
CommunicationEvents.newIP = myObjs.newIP;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.lastIP))
{
CommunicationEvents.lastIP = "";
CommunicationEvents.lastIP = myObjs.lastIP;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.IPslot1))
{
CommunicationEvents.IPslot1 = "";
CommunicationEvents.IPslot1 = myObjs.IPslot1;//myObjs.IPslot1;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.IPslot2))
{
CommunicationEvents.IPslot2 = "";//"Empty";
CommunicationEvents.IPslot2 = myObjs.IPslot2;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.IPslot3))
{
CommunicationEvents.IPslot3 = "";
CommunicationEvents.IPslot3 = myObjs.IPslot3;
}
Stefan Richter
committed
if (string.IsNullOrEmpty(myObjsOnlyStrings.IPslot4))
{
CommunicationEvents.IPslot4= "";
}
else
{
CommunicationEvents.IPslot4 = myObjs.IPslot4;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.IPslot5))
{
CommunicationEvents.IPslot5 = "";
}
else
{
CommunicationEvents.IPslot5 = myObjs.IPslot5;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.selecIP))
{
CommunicationEvents.selecIP = "";
CommunicationEvents.selecIP = myObjs.selecIP;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.ControlMode))
{
}
else
{
UIconfig.controlMode = (ControlMode)Enum.Parse(typeof(ControlMode), myObjs.ControlMode);
if (string.IsNullOrEmpty(myObjsOnlyStrings.TouchMode))
{
}
else
{
UIconfig.touchControlMode = myObjs.TouchMode;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.TAvisibility))
{
}
else
{
UIconfig.TAvisibility = myObjs.TAvisibility;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.autoOSrecognition))
{
}
else
{
CommunicationEvents.autoOSrecognition = myObjs.autoOSrecognition;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.autoOSrecognition))
{
}
else
{
UIconfig.autoSettingsAdaption = myObjs.autoSettingsAdaption;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.Opsys))
{
}
else
{
CommunicationEvents.Opsys = (OperationSystem)Enum.Parse(typeof(OperationSystem), myObjs.Opsys);
if (string.IsNullOrEmpty(myObjsOnlyStrings.FrameITUIversion))
{
}
else
{
UIconfig.FrameITUIversion = myObjs.FrameITUIversion;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.InputManagerVersion))
{
}
else
{
UIconfig.InputManagerVersion = myObjs.InputManagerVersion;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.colliderScale_all))
{
UIconfig.colliderScale_all = myObjs.colliderScale_all;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.cursorSize))
{
}
else
{
UIconfig.cursorSize = myObjs.cursorSize;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.camRotatingSensitivity))
{
}
else
{
Stefan Richter
committed
UIconfig.camRotatingSensitivity = myObjs.camRotatingSensitivity;
}
if (string.IsNullOrEmpty(myObjsOnlyStrings.MouseKeepingInWindow))
{
}
else
{
Stefan Richter
committed
UIconfig.MouseKeepingInWindow = myObjs.MouseKeepingInWindow;
}
Stefan Richter
committed
if (string.IsNullOrEmpty(myObjsOnlyStrings.scrolldirection))
{
}
else
{
UIconfig.scrolldirection = myObjs.scrolldirection;
}
}
Stefan Richter
committed
public static void RereadFileUWR(string pathfolders, string fileName, ID_toPath toMainpath)
Stefan Richter
committed
string sourcePath = Path.Combine(Application.streamingAssetsPath, pathfolders);
string destpathf = pathfolders;
string destname = fileName;
sourcePath = Path.Combine(sourcePath, fileName);
Marco Zimmer
committed
using var loadingRequest = UnityWebRequest.Get(sourcePath);
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone)
{
if (loadingRequest.result == UnityWebRequest.Result.ConnectionError || loadingRequest.result == UnityWebRequest.Result.ProtocolError)
{
break;
}
}
if (loadingRequest.result == UnityWebRequest.Result.ConnectionError || loadingRequest.result == UnityWebRequest.Result.ProtocolError)
{
}
else
{
//copies and unpacks file from apk to persistentDataPath where it can be accessed
Stefan Richter
committed
string destinationPath;
if (toMainpath == ID_toPath.DataPath && CommunicationEvents.Opsys != OperationSystem.Android) { destinationPath = Path.Combine(Application.dataPath, destpathf); }
else
{
destinationPath = Path.Combine(Application.persistentDataPath, destpathf);
}
Stefan Richter
committed
SafeCreateDirectory(destinationPath);
File.WriteAllBytes(Path.Combine(destinationPath, destname), loadingRequest.downloadHandler.data);
}
}
public static bool RereadFileWithUnityWebRequest(string sourcePath1, string pathfolders, string fileName, string targetpath)
Stefan Richter
committed
{
if (fileName == "") { Debug.Log("no File"); return false; }
Stefan Richter
committed
string destpathf = pathfolders;
string destname = fileName;
string sourcePath = Path.Combine(sourcePath1, fileName);
//Debug.Log(sourcePath);
Stefan Richter
committed
using var loadingRequest = UnityWebRequest.Get(sourcePath);
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone)
{
if (loadingRequest.result == UnityWebRequest.Result.ConnectionError || loadingRequest.result == UnityWebRequest.Result.ProtocolError)
Stefan Richter
committed
break;
Stefan Richter
committed
}
if (loadingRequest.result == UnityWebRequest.Result.ConnectionError || loadingRequest.result == UnityWebRequest.Result.ProtocolError)
{
Debug.Log("ConnectionError" + sourcePath);
return false;
Stefan Richter
committed
}
else
{
string destinationPath = Path.Combine(targetpath, destpathf);
Stefan Richter
committed
//Debug.Log("ss" + destinationPath + "," + Application.persistentDataPath + "," + Application.dataPath + "," + destpathf + " , " + destname);
SafeCreateDirectory(destinationPath);
File.WriteAllBytes(Path.Combine(destinationPath, destname), loadingRequest.downloadHandler.data);
Stefan Richter
committed
//Debug.Log("ss" + destinationPath);
}
Stefan Richter
committed
//Path.Combine() but without the Path.IsPathRooted()
public static string CombineTwoPaths(string path1, string path2)
{
if (path1 == null || path2 == null)
{
throw new ArgumentNullException((path1 == null) ? "path1" : "path2");
}
//Path.CheckInvalidPathChars(path1, false);
//Path.CheckInvalidPathChars(path2, false);
if (path2.Length == 0)
{
return path1;
}
if (path1.Length == 0)
{
return path2;
}
char c = path1[path1.Length - 1];
if (c != Path.DirectorySeparatorChar && c != Path.AltDirectorySeparatorChar && c != Path.VolumeSeparatorChar)
{
return path1 + Path.DirectorySeparatorChar + path2;
}
return path1 + path2;
}
//WWW has been replaced with UnityWebRequest.
/*
public static string RereadFileNA(string pathfolders, string fileName, string destpathf, string destname)
{
// copies and unpacks file from apk to persistentDataPath where it can be accessed
string destinationPath = Path.Combine(Application.persistentDataPath, destpathf);
if (Directory.Exists(destinationPath) == false)
{
Directory.CreateDirectory(destinationPath);
}
destinationPath = Path.Combine(destinationPath, destname);
string sourcePath = Path.Combine(Application.streamingAssetsPath, pathfolders);
sourcePath = Path.Combine(sourcePath, fileName);
#if UNITY_EDITOR
//string sourcePath = Path.Combine(Application.streamingAssetsPath, pathfolders);
//sourcePath = Path.Combine(sourcePath, fileName);
#else
//string sourcePath = "jar:file://" + Application.dataPath + "!/assets/" + fileName;
//UnityEngine.Debug.Log(string.Format("{0}-{1}-{2}-{3}", sourcePath, File.GetLastWriteTimeUtc(sourcePath), File.GetLastWriteTimeUtc(destinationPath)));
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//if DB does not exist in persistent data folder (folder "Documents" on iOS) or source DB is newer then copy it
//if (!File.Exists(destinationPath) || (File.GetLastWriteTimeUtc(sourcePath) > File.GetLastWriteTimeUtc(destinationPath)))
if (true)
{
if (sourcePath.Contains("://"))
{
// Android
WWW www = new WWW(sourcePath);
while (!www.isDone) {; } // Wait for download to complete - not pretty at all but easy hack for now
if (string.IsNullOrEmpty(www.error))
{
File.WriteAllBytes(destinationPath, www.bytes);
}
else
{
Debug.Log("ERROR: the file DB named " + fileName + " doesn't exist in the StreamingAssets Folder, please copy it there.");
}
}
else
{
// Mac, Windows, Iphone
//validate the existens of the DB in the original folder (folder "streamingAssets")
if (File.Exists(sourcePath))
{
//copy file - alle systems except Android
File.Copy(sourcePath, destinationPath, true);
}
else
{
Debug.Log("ERROR: the file DB named " + fileName + " doesn't exist in the StreamingAssets Folder, please copy it there.");
}
}
}
StreamReader reader = new StreamReader(destinationPath);
var jsonString = reader.ReadToEnd();
public static void RereadFileUW4(string pathfolders, string fileName, string destpathf, string destname)
{
string sourcePath = Path.Combine(Application.streamingAssetsPath, pathfolders);
sourcePath = Path.Combine(sourcePath, fileName);
using var loadingRequest = UnityWebRequest.Get(sourcePath);
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone)
{
if (loadingRequest.result == UnityWebRequest.Result.ConnectionError || loadingRequest.result == UnityWebRequest.Result.ProtocolError)
}
if (loadingRequest.result == UnityWebRequest.Result.ConnectionError || loadingRequest.result == UnityWebRequest.Result.ProtocolError)
{
}
else
{
//copies and unpacks file from apk to persistentDataPath where it can be accessed
string destinationPath = Path.Combine(Application.persistentDataPath, destpathf);
if (Directory.Exists(destinationPath) == false)
{
Directory.CreateDirectory(destinationPath);
File.WriteAllBytes(Path.Combine(destinationPath, destname), loadingRequest.downloadHandler.data);
}
}
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
public class MyClass
{
public int level;
public float timeElapsed;
public string playerName;
}
public static void Score_Save(string Directory_path, string date)
{
MyClass myObject = new MyClass();
myObject.level = 1;
myObject.timeElapsed = 47.5f;
myObject.playerName = "Dr Charles Francis";
//Data storage
SafeCreateDirectory(Application.persistentDataPath + "/" + Directory_path);
//string json = JsonUtility.ToJson(date);
string json = JsonUtility.ToJson(myObject);
StreamWriter Writer = new StreamWriter(Application.persistentDataPath + "/" + Directory_path + "/date.json");
Writer.Write(json);
Writer.Flush();
Writer.Close();
}
public static string Score_Load(string Directory_path)
{
//Data acquisition
var reader = new StreamReader(Path.Combine(Application.persistentDataPath, ConfigDir, ConfigFile_Network));
string json = reader.ReadToEnd();
reader.Close();
//MyClass myObjs = JsonUtility.FromJson<MyClass>(json);
//SampleData mySampleFile = JsonUtility.FromJson<SampleData>(jsonStr);
return json;//Convert for ease of use
//return myObjs.level.ToString();
}
}