51 lines
2.0 KiB
C#
51 lines
2.0 KiB
C#
using System.Text;
|
|
|
|
namespace Game.Data {
|
|
internal class GameFiles {
|
|
private const string CONFIG_FILENAME = "quiz_game_config.dat";
|
|
private const string DEFAULT_DATABASE_PATH = "bank.txt";
|
|
private static readonly byte[] CONFIG_FILE_MAGIC_BYTES = new byte[] { 0x7f, 0x57, 0x77, 0x74, 0x42, 0x00 };
|
|
public static bool IsFirstTimePlaying() {
|
|
return IsValidConfigFilePresent();
|
|
}
|
|
|
|
public static bool IsValidConfigFilePresent() {
|
|
return File.Exists(CONFIG_FILENAME);
|
|
}
|
|
|
|
public static GameConfig? ReadGameConfig() {
|
|
try {
|
|
using FileStream configFile = new FileStream(CONFIG_FILENAME, FileMode.Open, FileAccess.Read);
|
|
using BinaryReader rd = new BinaryReader(configFile);
|
|
|
|
if (configFile.Length < 6) return null;
|
|
byte[] magic = rd.ReadBytes(6);
|
|
if (magic.Equals(CONFIG_FILE_MAGIC_BYTES)) {
|
|
return null;
|
|
}
|
|
|
|
ushort version = rd.ReadUInt16();
|
|
if (version == 1) return ReadConfigV1(rd);
|
|
else return null;
|
|
} catch (FileNotFoundException) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static GameConfig CreateDefaultGameConfig() {
|
|
using FileStream configFile = new FileStream(CONFIG_FILENAME, FileMode.OpenOrCreate, FileAccess.Write);
|
|
using BinaryWriter wr = new BinaryWriter(configFile, Encoding.UTF8);
|
|
wr.Write(CONFIG_FILE_MAGIC_BYTES);
|
|
wr.Write((ushort)1); // config file version
|
|
wr.Write(DEFAULT_DATABASE_PATH);
|
|
return new GameConfig() { databasePath = DEFAULT_DATABASE_PATH };
|
|
}
|
|
|
|
public static GameConfig? ReadConfigV1(BinaryReader rd) {
|
|
FileStream file = (FileStream)rd.BaseStream;
|
|
string databasePath = rd.ReadString();
|
|
return new GameConfig() { databasePath = databasePath };
|
|
}
|
|
}
|
|
}
|