Skip to content

Commit fb2af94

Browse files
committed
Adds namespacing
1 parent ef4713a commit fb2af94

6 files changed

Lines changed: 104 additions & 76 deletions

File tree

Assets/Examples/Scripts/Example.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using UnityEngine;
22
using UnityEngine.UI;
3+
using UnityBinaryFileSaver;
34

45
class Example : MonoBehaviour
56
{
6-
public Text input;
7-
public Text info;
7+
public Text input = null;
8+
public Text info = null;
89
public GameObject infoContainer;
910
public string saveFileName = "example.savefile";
1011

Assets/Examples/Scripts/GameSaveContainer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using UnityEngine;
22
using System;
3+
using UnityBinaryFileSaver;
34

45
[Serializable]
56
class GameSaveContainer : ISaveContainer

Assets/Scripts/BinaryFileSaver.cs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,47 @@
11
using System.Runtime.Serialization.Formatters.Binary;
22
using System.IO;
33

4-
public class BinaryFileSaver
4+
namespace UnityBinaryFileSaver
55
{
6-
static BinaryFileSaver instance;
7-
BinaryFormatter bf;
8-
9-
static BinaryFileSaver Instance
6+
public class BinaryFileSaver
107
{
11-
get {
12-
if (instance == null) instance = new BinaryFileSaver();
13-
return instance;
8+
static BinaryFileSaver instance;
9+
BinaryFormatter bf;
10+
11+
static BinaryFileSaver Instance
12+
{
13+
get {
14+
if (instance == null) instance = new BinaryFileSaver();
15+
return instance;
16+
}
17+
set { instance = value; }
1418
}
15-
set { instance = value; }
16-
}
1719

18-
static BinaryFormatter BF
19-
{
20-
get {
21-
if (Instance.bf == null) Instance.bf = new BinaryFormatter();
22-
return Instance.bf;
20+
static BinaryFormatter BF
21+
{
22+
get {
23+
if (Instance.bf == null) Instance.bf = new BinaryFormatter();
24+
return Instance.bf;
25+
}
2326
}
24-
}
2527

26-
public static void Save(ISaveFile f)
27-
{
28-
BF.Serialize(f.Writefile, f.Data);
29-
f.Close();
30-
}
28+
public static void Save(ISaveFile f)
29+
{
30+
BF.Serialize(f.Writefile, f.Data);
31+
f.Close();
32+
}
3133

32-
public static ISaveContainer Load(ISaveFile f)
33-
{
34-
ISaveContainer data = null;
35-
var file = f.Readfile;
36-
if (file != null) {
37-
try {
38-
data = (ISaveContainer) BF.Deserialize(file);
39-
} catch {}
40-
file.Close();
34+
public static ISaveContainer Load(ISaveFile f)
35+
{
36+
ISaveContainer data = null;
37+
var file = f.Readfile;
38+
if (file != null) {
39+
try {
40+
data = (ISaveContainer) BF.Deserialize(file);
41+
} catch {}
42+
file.Close();
43+
}
44+
return data;
4145
}
42-
return data;
4346
}
4447
}

Assets/Scripts/ISaveContainer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
public interface ISaveContainer
1+
namespace UnityBinaryFileSaver
22
{
3+
public interface ISaveContainer
4+
{
5+
}
36
}

Assets/Scripts/ISaveFile.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System.IO;
22

3-
public interface ISaveFile
3+
namespace UnityBinaryFileSaver
44
{
5-
FileStream Readfile { get; }
6-
FileStream Writefile { get; }
7-
ISaveContainer Data { get; set; }
8-
void Close();
5+
public interface ISaveFile
6+
{
7+
FileStream Readfile { get; }
8+
FileStream Writefile { get; }
9+
ISaveContainer Data { get; set; }
10+
void Close();
11+
}
912
}

Assets/Scripts/SaveFile.cs

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,70 @@
22
using System;
33
using System.IO;
44

5-
[System.Serializable]
6-
public class SaveFile : ISaveFile
5+
namespace UnityBinaryFileSaver
76
{
8-
string filename;
9-
Action<string> callback;
10-
ISaveContainer data;
11-
[System.NonSerialized]
12-
FileStream filestream;
13-
14-
public SaveFile(string filename)
7+
[System.Serializable]
8+
public class SaveFile : ISaveFile
159
{
16-
this.filename = Application.persistentDataPath + "/" + filename;
17-
}
10+
string filename;
11+
Action<string> callback;
12+
ISaveContainer data;
13+
[System.NonSerialized]
14+
FileStream filestream;
1815

19-
public SaveFile(string filename, ISaveContainer data)
20-
{
21-
this.filename = Application.persistentDataPath + "/" + filename;
22-
this.data = data;
23-
}
16+
string Path
17+
{
18+
get {
19+
return Application.persistentDataPath + "/";
20+
}
21+
}
2422

25-
public ISaveContainer Data
26-
{
27-
get { return data; }
28-
set { data = value; }
29-
}
23+
string Filename
24+
{
25+
get {
26+
return Path + filename;
27+
}
28+
}
3029

31-
public FileStream Writefile
32-
{
33-
get
30+
public SaveFile(string filename)
3431
{
35-
filestream = File.Create(filename);
36-
return filestream;
32+
this.filename = filename;
3733
}
38-
}
3934

40-
public FileStream Readfile
41-
{
42-
get
35+
public SaveFile(string filename, ISaveContainer data)
4336
{
44-
if (!File.Exists(filename)) return null;
45-
filestream = File.Open(filename, FileMode.Open);
46-
return filestream;
37+
this.filename = filename;
38+
this.data = data;
4739
}
48-
}
4940

50-
public void Close()
51-
{
52-
filestream.Close();
41+
public ISaveContainer Data
42+
{
43+
get { return data; }
44+
set { data = value; }
45+
}
46+
47+
public FileStream Writefile
48+
{
49+
get
50+
{
51+
filestream = File.Create(Filename);
52+
return filestream;
53+
}
54+
}
55+
56+
public FileStream Readfile
57+
{
58+
get
59+
{
60+
if (!File.Exists(Filename)) return null;
61+
filestream = File.Open(Filename, FileMode.Open);
62+
return filestream;
63+
}
64+
}
65+
66+
public void Close()
67+
{
68+
filestream.Close();
69+
}
5370
}
5471
}

0 commit comments

Comments
 (0)