Skip to content

Commit d3cf03f

Browse files
committed
refactor(SaveLoad): Improve save load API
1 parent d2459c7 commit d3cf03f

File tree

7 files changed

+49
-57
lines changed

7 files changed

+49
-57
lines changed

Assets/JCSUnity/Scripts/SaveLoad/JCS_AppData.cs

Lines changed: 37 additions & 1 deletion
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 (c) 2020 by Shen, Jen-Chieh $
88
*/
99
using UnityEngine;
1010

@@ -16,6 +16,42 @@ namespace JCSUnity
1616
[System.Serializable]
1717
public abstract class JCS_AppData
1818
{
19+
/* Variables */
20+
21+
private bool mInitialized = false;
22+
23+
public string Copyright = "";
24+
public string Version = "";
25+
26+
/* Setter & Getter */
27+
28+
/* Functions */
29+
30+
protected void InitJCSFile()
31+
{
32+
if (JCS_PackageDataSettings.instance == null)
33+
{
34+
JCS_Debug.LogError("Failed to load the copyright and version text");
35+
return;
36+
}
37+
38+
Copyright = JCS_PackageDataSettings.instance.CopyrightString;
39+
Version = JCS_PackageDataSettings.instance.VersionString;
40+
41+
this.mInitialized = true;
42+
}
43+
44+
/// <summary>
45+
/// Return true if data is initialized.
46+
/// </summary>
47+
public bool Initialized()
48+
{
49+
return this.mInitialized;
50+
}
51+
52+
public abstract void Save<T>(string filePath, string fileName);
53+
public abstract void Save<T>(string fullFilePath);
54+
1955
/// <summary>
2056
/// Get complete save data path.
2157
/// </summary>

Assets/JCSUnity/Scripts/SaveLoad/JCS_BinData.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,17 @@ public abstract class JCS_BinData : JCS_AppData
1919
{
2020
/* Variables */
2121

22-
public string Copyright = "";
23-
public string Version = "";
24-
2522
/* Setter & Getter */
2623

2724
/* Functions */
2825

29-
private void InitJCSFile()
30-
{
31-
if (JCS_PackageDataSettings.instance == null)
32-
{
33-
JCS_Debug.LogError("Failed to load the copyright and version text");
34-
return;
35-
}
36-
37-
Copyright = JCS_PackageDataSettings.instance.CopyrightString;
38-
Version = JCS_PackageDataSettings.instance.VersionString;
39-
}
40-
4126
/// <summary>
4227
/// Save the game data into binary file format.
4328
/// </summary>
4429
/// <typeparam name="T"> type of the data save. </typeparam>
4530
/// <param name="filePath"> where to save. </param>
4631
/// <param name="fileName"> name of the file u want to save. </param>
47-
public void Save<T>(string filePath, string fileName)
32+
public override void Save<T>(string filePath, string fileName)
4833
{
4934
string fullFilePath = filePath + fileName;
5035

@@ -56,7 +41,7 @@ public void Save<T>(string filePath, string fileName)
5641
/// </summary>
5742
/// <typeparam name="T"> type of the data save. </typeparam>
5843
/// <param name="fullFilePath"> file path direct where to save. </param>
59-
public void Save<T>(string fullFilePath)
44+
public override void Save<T>(string fullFilePath)
6045
{
6146
string filePath = Path.GetDirectoryName(fullFilePath);
6247

Assets/JCSUnity/Scripts/SaveLoad/JCS_JSONData.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,17 @@ public abstract class JCS_JSONData : JCS_AppData
2020
{
2121
/* Variables */
2222

23-
public string Copyright = "";
24-
public string Version = "";
25-
2623
/* Setter & Getter */
2724

2825
/* Functions */
2926

30-
private void InitJCSFile()
31-
{
32-
if (JCS_PackageDataSettings.instance == null)
33-
{
34-
JCS_Debug.LogError("Failed to load the copyright and version text");
35-
return;
36-
}
37-
38-
Copyright = JCS_PackageDataSettings.instance.CopyrightString;
39-
Version = JCS_PackageDataSettings.instance.VersionString;
40-
}
41-
4227
/// <summary>
4328
/// Save the game data into xml file format.
4429
/// </summary>
4530
/// <typeparam name="T"> type of the data save. </typeparam>
4631
/// <param name="filePath"> where to save. </param>
4732
/// <param name="fileName"> name of the file u want to save. </param>
48-
public void Save<T>(string filePath, string fileName)
33+
public override void Save<T>(string filePath, string fileName)
4934
{
5035
string fullFilePath = filePath + fileName;
5136

@@ -58,7 +43,7 @@ public void Save<T>(string filePath, string fileName)
5843
/// <typeparam name="T"> type of the data save. </typeparam>
5944
/// <param name="filePath"> where to save. </param>
6045
/// <param name="fileName"> name of the file u want to save. </param>
61-
public void Save<T>(string fullFilePath)
46+
public override void Save<T>(string fullFilePath)
6247
{
6348
string filePath = Path.GetDirectoryName(fullFilePath);
6449

Assets/JCSUnity/Scripts/SaveLoad/JCS_XMLData.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,17 @@ public abstract class JCS_XMLData : JCS_AppData
1818
{
1919
/* Variables */
2020

21-
public string Copyright = "";
22-
public string Version = "";
23-
2421
/* Setter & Getter */
2522

2623
/* Functions */
2724

28-
private void InitJCSFile()
29-
{
30-
if (JCS_PackageDataSettings.instance == null)
31-
{
32-
JCS_Debug.LogError("Failed to load the copyright and version text");
33-
return;
34-
}
35-
36-
Copyright = JCS_PackageDataSettings.instance.CopyrightString;
37-
Version = JCS_PackageDataSettings.instance.VersionString;
38-
}
39-
4025
/// <summary>
4126
/// Save the game data into xml file format.
4227
/// </summary>
4328
/// <typeparam name="T"> type of the data save. </typeparam>
4429
/// <param name="filePath"> where to save. </param>
4530
/// <param name="fileName"> name of the file u want to save. </param>
46-
public void Save<T>(string filePath, string fileName)
31+
public override void Save<T>(string filePath, string fileName)
4732
{
4833
string fullFilePath = filePath + fileName;
4934

@@ -56,7 +41,7 @@ public void Save<T>(string filePath, string fileName)
5641
/// <typeparam name="T"> type of the data save. </typeparam>
5742
/// <param name="filePath"> where to save. </param>
5843
/// <param name="fileName"> name of the file u want to save. </param>
59-
public void Save<T>(string fullFilePath)
44+
public override void Save<T>(string fullFilePath)
6045
{
6146
string filePath = Path.GetDirectoryName(fullFilePath);
6247

Assets/_BossFight/Scripts/Settings/BF_AppSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private void Start()
5050
InitPath();
5151

5252
// only load once
53-
if (APP_DATA == null)
53+
if (!APP_DATA.Initialized())
5454
LoadAppData();
5555

5656
// set load and save game data

Assets/_RunningCrush/Scripts/Settings/RC_AppSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private void Start()
5050
InitPath();
5151

5252
// only load once
53-
if (APP_DATA == null)
53+
if (!APP_DATA.Initialized())
5454
LoadAppData();
5555

5656
// set load and save game data

docs/ScriptReference/SaveLoad/JCS_AppData.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Base application data structure.
44

55
## Functions
66

7-
| Name | Description |
8-
|:---------|:-----------------------------|
9-
| SavePath | Get complete save data path. |
7+
| Name | Description |
8+
|:------------|:------------------------------------|
9+
| Initialized | Return true if data is initialized. |
10+
| SavePath | Get complete save data path. |

0 commit comments

Comments
 (0)