【发布时间】:2015-07-30 11:15:05
【问题描述】:
场景: 我有一个程序,它使用一个简单的类来生成游戏数据。使用上述程序,我使用序列化和 BinaryFormatter 将数据写入文件以供第二个程序使用。从这个初始程序中读取数据没有问题。
问题: 这可能是由于我对序列化文件的处理方式一无所知,但我无法将这些数据加载到第二个程序中,即实际游戏本身。
保存游戏代码(在程序 1 中):
static List<GameData> gameData;
static GameData currentData;
private void saveGame(Sudoku sdk) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = null;
try {
if(!File.Exists(gameDataFile[currentDifficulty])) {
file = File.Open(gameDataFile[currentDifficulty], FileMode.CreateNew);
} else {
file = File.Open(gameDataFile[currentDifficulty], FileMode.Append);
}
currentData = setGameData(sdk);
bf.Serialize(file, currentData);
savePuzzleLog();
} catch(Exception e) {
Debug.LogException("saveGame", e);
}
if(file != null) {
file.Close();
}
}
loadGameData:(在程序 2 中)
public static List<GameData> gameData;
public bool loadGameData() {
if(gameData == null) {
gameData = new List<GameData>();
} else {
gameData.Clear();
}
bool loadData = true;
bool OK = false;
if(File.Exists(gameDataFile[currentDifficulty])) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(gameDataFile[currentDifficulty], FileMode.Open);
while(loadData) {
try {
GameData gd = new GameData();
gd = (GameData)bf.Deserialize(file);
gameData.Add(gd);
OK = true;
if(file.Position == file.Length) {
loadData = false;
}
} catch(Exception e) {
Debug.LogException(e);
loadData = false;
OK = false;
}
}
if(file != null) {
file.Close();
}
} else {
Debug.LogWarning(gameDataFile[currentDifficulty] + " does not exist!");
}
return OK;
}
GameData 类:(第一个程序)
[Serializable]
class GameData {
private int gameID;
private List<int> contentArray;
private int difficultyValue;
public GameData(List<int> data = null) {
id = -1;
difficulty = -1;
if(content != null) {
content.Clear();
} else {
content = new List<int>();
}
if(data != null) {
foreach(int i in data) {
content.Add(i);
}
}
}
public int id {
get {
return this.gameID;
}
set {
this.gameID = value;
}
}
public int difficulty {
get {
return this.difficultyValue;
}
set {
this.difficultyValue = value;
}
}
public List<int> content {
get {
return this.contentArray;
}
set {
this.contentArray = value;
}
}
}
GameData 类:(第二个程序)唯一的区别是声明为公共
[Serializable]
public class GameData {
private int gameID;
private List<int> contentArray;
private int difficultyValue;
public GameData(List<int> data = null) {
id = -1;
difficulty = -1;
if(content != null) {
content.Clear();
} else {
content = new List<int>();
}
if(data != null) {
foreach(int i in data) {
content.Add(i);
}
}
}
public int id {
get {
return this.gameID;
}
set {
this.gameID = value;
}
}
public int difficulty {
get {
return this.difficultyValue;
}
set {
this.difficultyValue = value;
}
}
public List<int> content {
get {
return this.contentArray;
}
set {
this.contentArray = value;
}
}
}
我的问题是,我如何将数据保存在一个程序中并能够使用不同的程序加载它而不会出现序列化错误,或者我是否需要使用备用保存/加载方法和/或类结构?
【问题讨论】:
-
我确实试图回答你,但我不得不完全猜测你的问题是什么,一条错误消息(你所说的异常)会很有帮助
-
如果您在同一个程序集中序列化/反序列化数据,仅使用
BinaryFormatter。还有很多其他的序列化器,不清楚为什么一定要用这个序列化器。 -
在游戏中,我赌的是反作弊 :),基于文本的序列化程序会生成一个太容易修改的文件
-
也许我应该解释一下,辅助应用程序是用 Unity 编写的,我收到的错误是:FileNotFoundException:无法加载文件或程序集'Sudoku Manager,Version=1.0.0.0,Culture=neutral, PublicKeyToken=null' 或其依赖项之一。该系统找不到指定的文件。 ...其中“Seduko Manager”是生成数据文件的程序 1 的名称。 @Sidewinder94 你是绝对正确的,正如你所说,这是为了反作弊,使用基于文本的文件很容易找到解决方案。
标签: c# serialization binaryfiles binary-data