Skip to content

Commit 660f009

Browse files
committed
Impls streaming assets consistency using web request.
1 parent 6409919 commit 660f009

File tree

11 files changed

+80
-44
lines changed

11 files changed

+80
-44
lines changed

Assets/JCSUnity/Editors/JCSUnity_About.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,9 @@ private void OnGUI()
6060
/// </summary>
6161
public static void ReadINIFile()
6262
{
63-
INI_FILE_PATH = Application.dataPath + "/JCSUnity/Editors/ini/";
63+
INI_FILE_PATH = JCS_Utility.PathCombine(Application.dataPath, "/JCSUnity/Editors/ini/");
6464

65-
//string path = Application.dataPath + "/../editor.properties";
66-
string path = INI_FILE_PATH + EDITOR_PROPERTIES_FILENAME;
65+
string path = JCS_Utility.PathCombine(INI_FILE_PATH, EDITOR_PROPERTIES_FILENAME);
6766

6867
EDITOR_INI = JCS_INIFileReader.ReadINIFile(path);
6968
}

Assets/JCSUnity/Scripts/JCS_Camera.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ public virtual string TakeScreenshot()
157157
public static string SavePath()
158158
{
159159
var gs = JCS_GameSettings.instance;
160-
return Application.dataPath + gs.SCREENSHOT_PATH;
160+
string path = JCS_Utility.PathCombine(Application.dataPath, gs.SCREENSHOT_PATH);
161+
return path;
161162
}
162163

163164
/// <summary>

Assets/JCSUnity/Scripts/SaveLoad/JCS_GameData.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* $Revision: $
55
* $Creator: Jen-Chieh Shen $
66
* $Notice: See LICENSE.txt for modification and distribution information
7-
* Copyright © 2020 by Shen, Jen-Chieh $
7+
* Copyright ?2020 by Shen, Jen-Chieh $
88
*/
99
using System.Collections;
1010
using System.Collections.Generic;
@@ -24,7 +24,8 @@ public abstract class JCS_GameData
2424
public static string SavePath()
2525
{
2626
var gs = JCS_GameSettings.instance;
27-
return Application.dataPath + gs.DATA_PATH;
27+
string path = JCS_Utility.PathCombine(Application.dataPath, gs.DATA_PATH);
28+
return path;
2829
}
2930
}
3031
}

Assets/JCSUnity/Scripts/Managers/JCS_StreamingAssets.cs renamed to Assets/JCSUnity/Scripts/Settings/JCS_StreamingAssets.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
* $Notice: See LICENSE.txt for modification and distribution information
77
* Copyright © 2020 by Shen, Jen-Chieh $
88
*/
9-
using Ookii.Dialogs;
109
using System.Collections;
1110
using System.Collections.Generic;
1211
using System.IO;
1312
using System.Text;
1413
using UnityEngine;
1514
using UnityEngine.Networking;
16-
using UnityEngine.UIElements;
1715

1816
namespace JCSUnity
1917
{
@@ -36,20 +34,16 @@ public class JCS_StreamingAssets
3634
[Header("** Check Variables (JCS_StreamingAssets) **")]
3735

3836
[Tooltip("List of file that we are going to download as target.")]
39-
[SerializeField]
4037
public List<string> downloadList = null;
4138

4239
[Tooltip("Flag to see if currently downloading the file.")]
43-
[SerializeField]
44-
private bool mRequesting = false;
40+
public bool requesting = false;
4541

4642
[Tooltip("Full request URL.")]
47-
[SerializeField]
48-
private string mRequestURL = "";
43+
public string requestURL = "";
4944

5045
[Tooltip("Request data path.")]
51-
[SerializeField]
52-
private string mRequestPath = "";
46+
public string requestPath = "";
5347

5448
[Header("** Initialize Variables (JCS_StreamingAssets) **")]
5549

@@ -58,10 +52,6 @@ public class JCS_StreamingAssets
5852

5953
/* Setter & Getter */
6054

61-
public bool Requesting { get { return this.mRequesting; } }
62-
public string RequestURL { get { return this.mRequestURL; } }
63-
public string RequestPath { get { return this.mRequestPath; } }
64-
6555
/* Functions */
6656

6757
private void Awake()
@@ -90,7 +80,8 @@ public static string StreamingAssetsPath()
9080
public static string CachePath()
9181
{
9282
var gs = JCS_GameSettings.instance;
93-
return Application.dataPath + gs.STREAMING_CACHE_PATH;
83+
string path = JCS_Utility.PathCombine(Application.dataPath, gs.STREAMING_CACHE_PATH);
84+
return path;
9485
}
9586

9687
/// <summary>
@@ -146,8 +137,14 @@ public void AddDownloadTarget(string path)
146137

147138
protected override void TransferData(JCS_StreamingAssets _old, JCS_StreamingAssets _new)
148139
{
149-
_old.downloadList = _new.downloadList;
150140
_old.requestCallback = _new.requestCallback;
141+
142+
_old.downloadList = _new.downloadList;
143+
_old.requesting = _new.requesting;
144+
_old.requestURL = _new.requestURL;
145+
_old.requestPath = _new.requestPath;
146+
147+
_old.preloadPath = _new.preloadPath;
151148
}
152149

153150
private bool ValidCacheData(string path)
@@ -181,17 +178,17 @@ private byte[] TryStreamingAssets(string path)
181178
private void TryUrlData(string path)
182179
{
183180
mResultData = null;
184-
mRequestPath = path;
185-
mRequestURL = UrlPath() + mRequestPath;
181+
requestPath = path;
182+
requestURL = UrlPath() + requestPath;
186183

187-
mRequesting = true;
184+
requesting = true;
188185

189186
StartCoroutine(GetData());
190187
}
191188

192189
private IEnumerator GetData()
193190
{
194-
UnityWebRequest www = UnityWebRequest.Get(mRequestURL);
191+
UnityWebRequest www = UnityWebRequest.Get(requestURL);
195192
yield return www.SendWebRequest();
196193

197194
bool success = false;
@@ -204,17 +201,17 @@ private IEnumerator GetData()
204201
else
205202
{
206203
mResultData = www.downloadHandler.data;
207-
WriteFileAsCache(mRequestPath, mResultData);
204+
WriteFileAsCache(requestPath, mResultData);
208205

209206
success = true;
210207
}
211208

212209
if (requestCallback != null)
213-
requestCallback.Invoke(mRequestPath, success);
210+
requestCallback.Invoke(requestPath, success);
214211
requestCallback = null;
215212

216-
downloadList.Remove(mRequestPath);
217-
mRequesting = false;
213+
downloadList.Remove(requestPath);
214+
requesting = false;
218215
}
219216

220217
/// <summary>
@@ -235,10 +232,17 @@ private static void WriteFileAsCache(string path, byte[] data)
235232
/// </summary>
236233
private void ProcessDownload()
237234
{
238-
if (downloadList.Count == 0 || mRequesting)
235+
if (downloadList.Count == 0 || requesting)
239236
return;
240237

241-
TryUrlData(downloadList[0]);
238+
string path = downloadList[0];
239+
240+
if (NeedRequestData(ReadAllBytes(path)))
241+
TryUrlData(path);
242+
else
243+
{
244+
downloadList.RemoveAt(0);
245+
}
242246
}
243247
}
244248
}

Assets/JCSUnity/Scripts/Managers/JCS_StreamingAssets.cs.meta renamed to Assets/JCSUnity/Scripts/Settings/JCS_StreamingAssets.cs.meta

File renamed without changes.

Assets/JCSUnity/Scripts/Utilities/JCS_Utility.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,5 +1321,23 @@ public static byte[] StringToBytes(string data, JCS_CharsetType charset)
13211321
JCS_Debug.LogError("This shouldn't happens, charset `string to bytes`");
13221322
return null;
13231323
}
1324+
1325+
/// <summary>
1326+
/// Safe way to combine multiple path to one path with slash.
1327+
/// </summary>
1328+
/// <param name="list"> List of path. </param>
1329+
/// <returns> Converted path. </returns>
1330+
public static string PathCombine(params string[] list)
1331+
{
1332+
string result = list[0];
1333+
for (int index = 1; index < list.Length; ++index)
1334+
{
1335+
string path = list[index];
1336+
result += "/" + path;
1337+
}
1338+
result = result.Replace("\\", "/");
1339+
result = result.Replace("///", "/");
1340+
return result;
1341+
}
13241342
}
13251343
}

Assets/JCSUnity/Scripts/Webcam/JCS_Webcam.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ public string TakeSnapshotWebcam()
241241
public static string SavePath()
242242
{
243243
var gs = JCS_GameSettings.instance;
244-
return Application.dataPath + gs.WEBCAM_PATH;
244+
string path = JCS_Utility.PathCombine(Application.dataPath, gs.WEBCAM_PATH);
245+
return path;
245246
}
246247

247248
/// <summary>
0 Bytes
Binary file not shown.

Assets/_BossFight_Assets/Scripts/Settings/BF_GameSettings.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@
1212
using System.IO;
1313

1414
/// <summary>
15-
///
15+
/// Game settings for Boss Fight example game.
1616
/// </summary>
1717
public class BF_GameSettings
1818
: JCS_Settings<BF_GameSettings>
1919
{
2020
/* Variables */
2121

22+
[Header("** Check Variables (BF_GameSettings) **")]
23+
24+
[SerializeField]
25+
private string mFullFilePath = "";
26+
27+
[SerializeField]
28+
private string mFullFileName = "";
29+
2230
[Header("- Player")]
2331

2432
[Tooltip("How many character on players team.")]
@@ -37,10 +45,6 @@ public class BF_GameSettings
3745

3846
public static BF_GameData BF_GAME_DATA = null;
3947

40-
private string mFullFilePath = "";
41-
42-
private string mFullFileName = "";
43-
4448
[Header("- Game Feature")]
4549

4650
[Tooltip("Maximum mob in the scene.")]
@@ -106,7 +110,7 @@ private void InitPath()
106110
{
107111
var gs = JCS_GameSettings.instance;
108112

109-
mFullFilePath = Application.dataPath + gs.DATA_PATH + FILE_PATH;
113+
mFullFilePath = JCS_Utility.PathCombine(Application.dataPath, gs.DATA_PATH, FILE_PATH);
110114
mFullFileName = FILE_NAME + gs.DATA_EXTENSION;
111115
}
112116
private void LoadGameData()

Assets/_FrameworkTest_Assets/Scripts/FT_SaveLoad_Test.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* $Notice: See LICENSE.txt for modification and distribution information
77
* Copyright © 2019 by Shen, Jen-Chieh $
88
*/
9+
using JCSUnity;
910
using System.Collections;
1011
using System.Collections.Generic;
1112
using UnityEngine;
@@ -22,7 +23,7 @@ public class FT_SaveLoad_Test : MonoBehaviour
2223
/* Functions */
2324
private void Awake()
2425
{
25-
string path = Application.dataPath + "/JCS_GameData/SavedData/FT_GameData.jcs";
26+
string path = JCS_Utility.PathCombine(Application.dataPath, "/JCS_GameData/SavedData/FT_GameData.jcs");
2627

2728
FT_JSONGameData data = new FT_JSONGameData();
2829
data.Save<FT_JSONGameData>(path);

0 commit comments

Comments
 (0)