Histacom2/TimeHACK.Engine/SaveSystem.cs

316 lines
11 KiB
C#
Raw Normal View History

2017-05-07 20:04:31 -04:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
2017-07-04 17:12:25 -04:00
using System.Diagnostics;
2017-07-21 17:14:23 -04:00
using System.Windows.Forms;
2017-05-07 20:04:31 -04:00
namespace TimeHACK.Engine
{
public static class SaveSystem
{
public static Save CurrentSave { get; set; }
public static FileSystemFolderInfo filesystemflinfo { get; set; }
2017-07-05 14:41:23 -04:00
public static bool DevMode = false;
2017-07-21 17:14:23 -04:00
public static Form troubleshooter;
2017-05-07 20:04:31 -04:00
2017-07-05 14:41:23 -04:00
public static Theme currentTheme { get; set; }
2017-05-07 20:04:31 -04:00
public static string GameDirectory
{
get
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TimeHACK");
2017-05-07 20:04:31 -04:00
}
}
2017-07-22 15:35:30 -04:00
public static string DataDirectory
{
get
{
return Path.Combine(GameDirectory, "Data");
}
}
2017-05-20 08:33:32 -04:00
public static string AllProfilesDirectory
2017-05-07 20:04:31 -04:00
{
get
{
return Path.Combine(GameDirectory, "Profiles");
}
}
2017-05-20 08:33:32 -04:00
public static string ProfileName = "";
public static string ProfileFile = "main.save";
public static string ProfileDirectory
2017-05-07 20:04:31 -04:00
{
2017-05-20 08:33:32 -04:00
get
2017-05-07 20:04:31 -04:00
{
2017-05-20 08:33:32 -04:00
return Path.Combine(GameDirectory, Path.Combine("Profiles", ProfileName));
2017-05-07 20:04:31 -04:00
}
}
public static string ProfileFileSystemDirectory
{
get
{
return Path.Combine(ProfileDirectory, "folders");
}
}
public static string ProfileMyComputerDirectory
{
get
{
2017-07-22 06:41:27 -04:00
return Path.Combine(ProfileFileSystemDirectory, "CDrive");
}
}
public static string ProfileSettingsDirectory
{
get
{
2017-06-17 17:25:33 -04:00
return Path.Combine(ProfileMyComputerDirectory, "Settings");
}
}
public static string ProfileDocumentsDirectory
{
get
{
2017-06-17 17:25:33 -04:00
return Path.Combine(ProfileMyComputerDirectory, "Doc");
}
}
public static string ProfileProgramsDirectory
{
get
{
return Path.Combine(ProfileMyComputerDirectory, "Prog");
}
}
public static string ProfileWindowsDirectory
{
get
{
return Path.Combine(ProfileMyComputerDirectory, "Win");
}
}
2017-05-20 08:33:32 -04:00
public static bool LoadSave()
{
2017-07-21 17:14:23 -04:00
try
{
// ON A FINAL RELEASE USE THE "FINAL RELEASE THINGS"
#region Final Release Things
//Read base64 string from file
//string b64 = File.ReadAllText(Path.Combine(ProfileDirectory, ProfileFile));
//Get Unicode byte array
//byte[] bytes = Convert.FromBase64String(b64);
//Decode the Unicode
//string json = Encoding.UTF8.GetString(bytes);
//Deserialize save object.
#endregion
// USE THE THINGS IN THE "DEVELOPER THINGS" FOR A DEVELOPMENT RELEASE
#region Developer Things
string json = File.ReadAllText(Path.Combine(ProfileDirectory, ProfileFile));
#endregion
CurrentSave = JsonConvert.DeserializeObject<Save>(json);
} catch
{
MessageBox.Show("WARNING! It looks like this save is corrupt!");
MessageBox.Show("We will now open the Save troubleshooter");
troubleshooter.ShowDialog();
}
2017-05-20 08:33:32 -04:00
return true;
}
2017-05-07 20:04:31 -04:00
public static void NewGame()
{
var save = new Save();
save.ExperiencedStories = new List<string>();
2017-07-04 17:12:25 -04:00
if (DevMode == true)
{
if (ProfileName == "98")
{
save.CurrentOS = "98";
2017-07-05 14:41:23 -04:00
save.ThemeName = "default98";
currentTheme = new Default98Theme();
2017-07-04 18:24:33 -04:00
}
else
{
save.CurrentOS = "95";
2017-07-05 14:41:23 -04:00
save.ThemeName = "default95";
currentTheme = new Default95Theme();
2017-07-04 17:12:25 -04:00
}
}
2017-07-04 18:24:33 -04:00
else
{
save.CurrentOS = "95";
2017-07-05 14:41:23 -04:00
save.ThemeName = "default95";
currentTheme = new Default95Theme();
2017-07-04 18:24:33 -04:00
}
CurrentSave = save;
2017-07-01 17:17:57 -04:00
CheckFiles();
SaveGame();
}
public static void CheckFiles()
{
2017-05-07 20:04:31 -04:00
if (!Directory.Exists(GameDirectory))
Directory.CreateDirectory(GameDirectory);
2017-05-20 08:33:32 -04:00
if (!Directory.Exists(AllProfilesDirectory))
Directory.CreateDirectory(AllProfilesDirectory);
2017-05-07 20:04:31 -04:00
if (!Directory.Exists(ProfileDirectory))
Directory.CreateDirectory(ProfileDirectory);
if (!Directory.Exists(ProfileFileSystemDirectory))
Directory.CreateDirectory(ProfileFileSystemDirectory);
SaveDirectoryInfo(ProfileFileSystemDirectory, false, "My Computer", false);
SaveDirectoryInfo(ProfileMyComputerDirectory, false, "Win95 (C:)", true);
2017-07-22 07:45:53 -04:00
if (CurrentSave.CurrentOS == "95" || CurrentSave.CurrentOS == "98") SaveDirectoryInfo(ProfileDocumentsDirectory, false, "My Documents", true);
if (CurrentSave.CurrentOS == "2000" || CurrentSave.CurrentOS == "ME") SaveDirectoryInfo(ProfileSettingsDirectory, false, "Documents and Settings", true);
SaveDirectoryInfo(Path.Combine(ProfileProgramsDirectory, "Accessories"), false, "Accessories", true);
SaveDirectoryInfo(ProfileProgramsDirectory, true, "Program Files", true);
SaveDirectoryInfo(ProfileWindowsDirectory, true, "Windows", true);
2017-07-01 17:17:57 -04:00
CreateWindowsFile(Path.Combine(ProfileProgramsDirectory, "Accessories", "wordpad.exe"), "wordpad");
2017-07-01 17:17:57 -04:00
CreateWindowsDirectory();
}
public static void CreateWindowsDirectory()
{
SaveDirectoryInfo(Path.Combine(ProfileWindowsDirectory, "System"), true, "System", true);
SaveDirectoryInfo(Path.Combine(ProfileWindowsDirectory, "Config"), true, "Config", true);
SaveDirectoryInfo(Path.Combine(ProfileWindowsDirectory, "Cursors"), true, "Cursors", true);
SaveDirectoryInfo(Path.Combine(ProfileWindowsDirectory, "Fonts"), true, "Fonts", true);
SaveDirectoryInfo(Path.Combine(ProfileWindowsDirectory, "Help"), true, "Help", true);
SaveDirectoryInfo(Path.Combine(ProfileWindowsDirectory, "Temp"), true, "Temp", true);
CreateWindowsFile(Path.Combine(ProfileWindowsDirectory, "calc.exe"), "calc");
CreateWindowsFile(Path.Combine(ProfileWindowsDirectory, "explorer.exe"), "explorer");
2017-07-01 17:17:57 -04:00
}
2017-07-05 14:41:23 -04:00
public static void CreateWindowsFile(string filepath, string contents)
2017-07-01 17:17:57 -04:00
{
File.WriteAllText(filepath, contents);
2017-05-07 20:04:31 -04:00
}
public static void UpgradeFileSystem(string oldOS, string newOS)
{
switch (oldOS)
{
case "95":
if (newOS == "98" || newOS == "2000" || newOS == "ME")
{
// We are upgrading from the old WinClassic file System to the new WinClassic filesystem!
// All the above OSes share basically the same file layout!
// (Excluding Documents And Settings) which is 2000 and ME only
2017-07-22 07:45:53 -04:00
// Rename the C Drive to Win98
SaveDirectoryInfo(ProfileMyComputerDirectory, false, "Win98 (C:)", true);
// Add Address Book into existance!
SaveDirectoryInfo(Path.Combine(ProfileProgramsDirectory, "Outlook Express"), false, "Outlook Express", true);
CreateWindowsFile(Path.Combine(ProfileProgramsDirectory, "Outlook Express", "WAB.exe"), "addressbook");
}
break;
}
}
2017-07-05 14:41:23 -04:00
public static void SaveDirectoryInfo(string directory, bool isProtected, string label, bool allowback)
{
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
FileSystemFolderInfo info = new FileSystemFolderInfo();
info.Isprotected = isProtected;
info.label = label;
info.allowback = allowback;
string toWrite = JsonConvert.SerializeObject(info, Formatting.Indented);
File.WriteAllText(Path.Combine(directory, "_data.info"), toWrite);
}
2017-05-07 20:04:31 -04:00
public static void SaveGame()
{
//Serialize the save to JSON.
2017-05-20 08:33:32 -04:00
string json = JsonConvert.SerializeObject(CurrentSave, Formatting.Indented);
// ADD THE TWO LINES OF CODE BELOW ON A FINAL RELEASE
2017-05-07 20:04:31 -04:00
//Get JSON bytes (Unicode format).
2017-05-20 08:33:32 -04:00
//var bytes = Encoding.UTF8.GetBytes(json);
2017-05-07 20:04:31 -04:00
//Encode the array into Base64.
2017-05-20 08:33:32 -04:00
//string b64 = Convert.ToBase64String(bytes);
2017-05-07 20:04:31 -04:00
//Write to disk.
2017-05-20 08:33:32 -04:00
// CHANGE THE "JSON" TO "B64" ON A FINAL RELEASE!
File.WriteAllText(Path.Combine(ProfileDirectory, ProfileFile), json);
2017-05-07 20:04:31 -04:00
}
2017-07-05 17:25:36 -04:00
2017-07-22 15:35:30 -04:00
public static byte[] GetAchievements()
{
byte[] byt = new byte[] { 0, 0 };
if (DevMode) File.WriteAllBytes(Path.Combine(DataDirectory, "achieved.thack"), byt);
if (File.Exists(Path.Combine(DataDirectory, "achieved.thack"))) byt = File.ReadAllBytes(Path.Combine(DataDirectory, "achieved.thack"));
else File.WriteAllBytes(Path.Combine(DataDirectory, "achieved.thack"), byt);
return byt;
}
2017-07-05 17:25:36 -04:00
public static void SetTheme()
{
switch (CurrentSave.ThemeName)
{
case "default95":
currentTheme = new Default95Theme();
break;
case "default98":
currentTheme = new Default98Theme();
break;
2017-07-05 17:25:36 -04:00
case "dangeranimals":
currentTheme = new DangerousCreaturesTheme();
break;
2017-07-05 18:41:55 -04:00
case "insidepc":
currentTheme = new InsideComputerTheme();
break;
2017-07-05 17:25:36 -04:00
}
}
2017-05-07 20:04:31 -04:00
}
public class Save
{
public string Username { get; set; }
public string CurrentOS { get; set; }
// public Dictionary<string, bool> InstalledPrograms { get; set; } InstallProgram is no longer needed... we have that data in the FileSystem
2017-05-07 20:04:31 -04:00
public List<string> ExperiencedStories { get; set; }
public bool FTime95 { get; set; }
2017-07-04 18:24:33 -04:00
public string ThemeName { get; set; }
2017-05-07 20:04:31 -04:00
}
public class FileSystemFolderInfo
{
2017-07-04 18:24:33 -04:00
public bool Isprotected { get; set; }
public string label { get; set; }
public bool allowback { get; set; }
}
2017-05-07 20:04:31 -04:00
}