Skip to content
Snippets Groups Projects
Commit a3ea6b95 authored by Stefan Richter's avatar Stefan Richter
Browse files

Changed the way how Files and Folders will be loaded from the StreamingAssets folder

parent 1903af66
Branches mergeHelper
No related tags found
No related merge requests found
Showing
with 255 additions and 47 deletions
......@@ -36,7 +36,7 @@ public class GlobalBehaviour : MonoBehaviour
void Awake()
{
GenerateDemoFiles.GenerateAll();
//GenerateDemoFiles.GenerateAll();
hintAnimationStartColor = _hintAnimationStartColor;
hintAnimationEndColor = _hintAnimationEndColor;
......
......@@ -15,6 +15,12 @@
public static class StreamingAssetLoader
{
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";
//""
......@@ -53,17 +59,37 @@ public static class StreamingAssetLoader
public static string file_10_path = "";
public static string file_10 = "";
//public static ID_toPath toPath = ID_toPath.DataPath;
public enum ID_toPath
{
DataPath = 0,
PersistentDataPath = 1,
}
public static bool checkPDP()
public static bool checkPersistentDataPath()
{
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)
{
string filePath = Application.persistentDataPath + "/Config/Network.json";
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;
}
......@@ -105,11 +131,15 @@ public static void NetworkJSON_Save()
}
public static DirectoryInfo SafeCreateDirectory(string path)
{
//Generate if you don't check if the directory exists
if (Directory.Exists(path))
{
//Debug.Log(path + " exists");
return null;
}
//Debug.Log(path + " create");
return Directory.CreateDirectory(path);
}
......@@ -122,6 +152,11 @@ public static void ResetPlayerConfig()
RereadFiles_PersistentDataPath();
NetworkJSON_Load();
}
public static void ResetPersistentDataPath()
{
RereadFiles_PersistentDataPath();
NetworkJSON_Load();
}
public static void ResetDataPath()
{
......@@ -141,23 +176,81 @@ public static void ResetStreamingAsset()
public static void RereadFiles_PersistentDataPath()
{
RereadFileUWR(StreamingAssetLoader.file_8_path, StreamingAssetLoader.file_8, 1);
RereadFileUWR(StreamingAssetLoader.file_1_path, StreamingAssetLoader.file_1, 1);
//RereadFileUWR(StreamingAssetLoader.file_8_path, StreamingAssetLoader.file_8, ID_toPath.PersistentDataPath);
//RereadFileUWR(StreamingAssetLoader.file_1_path, StreamingAssetLoader.file_1, ID_toPath.PersistentDataPath);
//----
string rootPath = Path.Combine(Application.streamingAssetsPath, StreamToPersistentDataPath_Folder);
string targetFolder = Application.persistentDataPath;
ReReadFiles(rootPath, targetFolder);
}
public static void RereadFiles_DataPath()
{
string rootPath = Path.Combine(Application.streamingAssetsPath, StreamToDataPath_Folder);
string targetFolder = Application.dataPath;
ReReadFiles(rootPath, targetFolder);
RereadFiles_DataPath_withHandler();
}
public static void RereadFiles_DataPath_withHandler()
{
//RereadFileUWR(StreamingAssetLoader.file_2_path, StreamingAssetLoader.file_2, ID_toPath.DataPath);
//RereadFileUWR(StreamingAssetLoader.file_3_path, StreamingAssetLoader.file_3, ID_toPath.DataPath);
//RereadFileUWR(StreamingAssetLoader.file_4_path, StreamingAssetLoader.file_4, ID_toPath.DataPath);
//RereadFileUWR(StreamingAssetLoader.file_5_path, StreamingAssetLoader.file_5, ID_toPath.DataPath);
//RereadFileUWR(StreamingAssetLoader.file_6_path, StreamingAssetLoader.file_6, ID_toPath.DataPath);
//RereadFileUWR(StreamingAssetLoader.file_7_path, StreamingAssetLoader.file_7, ID_toPath.DataPath);
//RereadFileUWR(StreamingAssetLoader.file_9_path, StreamingAssetLoader.file_9, ID_toPath.DataPath);
//RereadFileUWR(StreamingAssetLoader.file_10_path, StreamingAssetLoader.file_10, ID_toPath.DataPath);
string rootPath = Path.Combine(Application.streamingAssetsPath, StreamToDataPath_withHandler_Folder);
string targetFolder = Application.dataPath;
if (CommunicationEvents.Opsys == OperationSystem.Android)
{
targetFolder = Application.persistentDataPath;
}
ReReadFiles(rootPath, targetFolder);
}
public static void ReReadFiles(string rootPath, string targetFolder)//ID_toPath PathHandler)
{
RereadFileUWR(StreamingAssetLoader.file_2_path, StreamingAssetLoader.file_2, 0);
RereadFileUWR(StreamingAssetLoader.file_3_path, StreamingAssetLoader.file_3, 0);
RereadFileUWR(StreamingAssetLoader.file_4_path, StreamingAssetLoader.file_4, 0);
RereadFileUWR(StreamingAssetLoader.file_5_path, StreamingAssetLoader.file_5, 0);
RereadFileUWR(StreamingAssetLoader.file_6_path, StreamingAssetLoader.file_6, 0);
RereadFileUWR(StreamingAssetLoader.file_7_path, StreamingAssetLoader.file_7, 0);
RereadFileUWR(StreamingAssetLoader.file_9_path, StreamingAssetLoader.file_9, 0);
RereadFileUWR(StreamingAssetLoader.file_10_path, StreamingAssetLoader.file_10, 0);
//----
string dir = "";
DirectoryInfo dirInfo = new DirectoryInfo(rootPath);
FileInfo[] Files = dirInfo.GetFiles("*");
foreach (FileInfo file in Files)
{
if (file.Name.Contains(".meta")) { continue; };
RereadFileWithUnityWebRequest(rootPath, dir, file.Name, targetFolder);
}
//----
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);
Files = dirInfo.GetFiles("*");
foreach (FileInfo file in Files)
{
if (file.Name.Contains(".meta")) { continue; };
RereadFileWithUnityWebRequest(dir_fullpath, dir, file.Name, targetFolder);
}
}
}
public static void NetworkJSON_Load()
public static void NetworkJSON_Load()
{
var reader = new StreamReader(Application.persistentDataPath + "/Config/Network.JSON");
string json = reader.ReadToEnd();
......@@ -243,17 +336,14 @@ public static void NetworkJSON_Load()
}
public static void RereadFileUWR(string pathfolders, string fileName, int toMainpath)
public static void RereadFileUWR(string pathfolders, string fileName, ID_toPath toMainpath)
{
if (fileName == "")
{
return;
}
if (fileName == ""){ return; }
string sourcePath = Path.Combine(Application.streamingAssetsPath, pathfolders);
string destpathf = pathfolders;
string destname = fileName;
string sourcePath = Path.Combine(Application.streamingAssetsPath, pathfolders);
sourcePath = Path.Combine(sourcePath, fileName);
using var loadingRequest = UnityWebRequest.Get(sourcePath);
loadingRequest.SendWebRequest();
......@@ -271,25 +361,87 @@ public static void RereadFileUWR(string pathfolders, string fileName, int toMain
else
{
//copies and unpacks file from apk to persistentDataPath where it can be accessed
string destinationPath = "";
if (toMainpath == 0 && CommunicationEvents.Opsys != OperationSystem.Android) { destinationPath = Path.Combine(Application.dataPath, destpathf); }
string destinationPath;
if (toMainpath == ID_toPath.DataPath && CommunicationEvents.Opsys != OperationSystem.Android) { destinationPath = Path.Combine(Application.dataPath, destpathf); }
else
{
destinationPath = Path.Combine(Application.persistentDataPath, destpathf);
}
if (Directory.Exists(destinationPath) == false)
SafeCreateDirectory(destinationPath);
File.WriteAllBytes(Path.Combine(destinationPath, destname), loadingRequest.downloadHandler.data);
}
}
public static void RereadFileWithUnityWebRequest(string sourcePath1, string pathfolders, string fileName, string targetFolder)
{
if (fileName == "") { Debug.Log("no File"); return; }
string destpathf = pathfolders;
string destname = fileName;
string sourcePath = Path.Combine(sourcePath1, fileName);
using var loadingRequest = UnityWebRequest.Get(sourcePath);
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone)
{
if (loadingRequest.result == UnityWebRequest.Result.ConnectionError || loadingRequest.result == UnityWebRequest.Result.ProtocolError)
{
Directory.CreateDirectory(destinationPath);
break;
}
File.WriteAllBytes(Path.Combine(destinationPath, destname), loadingRequest.downloadHandler.data);
}
if (loadingRequest.result == UnityWebRequest.Result.ConnectionError || loadingRequest.result == UnityWebRequest.Result.ProtocolError)
{
Debug.Log("ConnectionError");
}
else
{
string destinationPath = Path.Combine(targetFolder, destpathf);
//Debug.Log("ss" + destinationPath + "," + Application.persistentDataPath + "," + Application.dataPath + "," + destpathf + " , " + destname);
SafeCreateDirectory(destinationPath);
File.WriteAllBytes(Path.Combine(destinationPath, destname), loadingRequest.downloadHandler.data);
//Debug.Log("ss" + destinationPath);
}
}
//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.
/*
......
......@@ -64,10 +64,10 @@ void start2_CheckOS_CheckConfig()
if (!checkPDP())
if (!checkPersistentDataPath())
{
ResetStreamingAsset();
ResetPersistentDataPath();
switch (Opsys)
{
case OperationSystem.Windows:
......@@ -82,18 +82,20 @@ void start2_CheckOS_CheckConfig()
NetworkJSON_Save();
}
NetworkJSON_Load();
checkOS();
ResetDataPath();
if (!checkDataPath())
{
ResetDataPath();
}
setMouse();
break;
case OperationSystem.Android:
if (!checkPDP())
if (!checkPersistentDataPath())
{
ResetStreamingAsset();
ResetPersistentDataPath();
switch (Opsys)
{
case OperationSystem.Windows:
......@@ -105,22 +107,24 @@ void start2_CheckOS_CheckConfig()
default:
break;
}
NetworkJSON_Save();
}
NetworkJSON_Load();
checkOS();
ResetDataPath();
if (!checkDataPath())
{
ResetDataPath();
};
setMouse();
break;
default:
setMouse();
if (!checkPDP())
if (!checkPersistentDataPath())
{
ResetStreamingAsset();
ResetPersistentDataPath();
switch (Opsys)
{
case OperationSystem.Windows:
......@@ -132,11 +136,15 @@ void start2_CheckOS_CheckConfig()
default:
break;
}
NetworkJSON_Save();
}
NetworkJSON_Load();
checkOS();
if (!checkDataPath())
{
ResetDataPath();
}
setMouse();
break;
}
......
fileFormatVersion: 2
guid: 2980c8ebfb250ef4e95c96619554751f
guid: 218d2a96656c0084bbe511b0b0338d01
TextScriptImporter:
externalObjects: {}
userData:
......
fileFormatVersion: 2
guid: daaacf33c8c5ad042b89fd48f6cd5980
guid: 61155bac0d422bd42bba9368cf492aa6
TextScriptImporter:
externalObjects: {}
userData:
......
fileFormatVersion: 2
guid: adcbcdc21be4b2a4a85d96580503e38b
guid: 63b411c1287cd9e4d891bf68fb264216
folderAsset: yes
DefaultImporter:
externalObjects: {}
......
fileFormatVersion: 2
guid: c1f88002b210dd4449cbaf8b9bb46003
guid: a68a50c4fbf67b04e98165c21d086ea4
TextScriptImporter:
externalObjects: {}
userData:
......
fileFormatVersion: 2
guid: 8fe9258a5917e3c4f8935875a478ed20
guid: 9e94bd4e978e956409a167f0b9d7eef7
TextScriptImporter:
externalObjects: {}
userData:
......
## StreamToDataPath:
All files and subfolders will be copied to DataPath.
Exception: Files with "meta" in the name.
## StreamToPersistentDataPath:
All files and subfolders will be copied to PersistentDataPath.
Exception: Files with "meta" in the name.
## StreamToDataPath_withHandler:
If Operatingsystem is Android, then:
- All files and subfolders will be copied to PersistentDataPath.
- Exception: Files with "meta" in the name.
If Operatingsystem is other than Android, then:
- All files and subfolders will be copied to DataPath.
- Exception: Files with "meta" in the name.
fileFormatVersion: 2
guid: 48d49f72b4ec2504b80c20e982d69f8a
guid: 131e4927e04391e4782e51617403b381
folderAsset: yes
DefaultImporter:
externalObjects: {}
......
fileFormatVersion: 2
guid: 2c31b390bc26b0348a244891f573c7b5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: ff74962d43564f24da42a6c1fc0afd04
guid: ed14a0e8d3597d34aa385c91f48d882b
folderAsset: yes
DefaultImporter:
externalObjects: {}
......
1
\ No newline at end of file
fileFormatVersion: 2
guid: b2295653dd0acd1478dbcf793ac8762c
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: d4b09aaecee4db1449faa23b38381414
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 08ca77ca3ac12c146ba9bfe942162be8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 2980c8ebfb250ef4e95c96619554751f
guid: 218d2a96656c0084bbe511b0b0338d01
TextScriptImporter:
externalObjects: {}
userData:
......
fileFormatVersion: 2
guid: daaacf33c8c5ad042b89fd48f6cd5980
guid: 61155bac0d422bd42bba9368cf492aa6
TextScriptImporter:
externalObjects: {}
userData:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment