Enable placeable sun deck attractions and finish project rebrand
This commit is contained in:
145
Assets/BigShipTycoon/Runtime/CareerStore.cs
Normal file
145
Assets/BigShipTycoon/Runtime/CareerStore.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BigShipTycoon
|
||||
{
|
||||
[Serializable]
|
||||
internal sealed class CareerSave
|
||||
{
|
||||
public int Version = 1;
|
||||
public string Id, Name;
|
||||
public long SavedUtc;
|
||||
public int Page, DeckIndex;
|
||||
public Company Company;
|
||||
public FleetPassageSnapshot[] Passages;
|
||||
}
|
||||
|
||||
internal sealed class CareerStore
|
||||
{
|
||||
// Compatibility with the storage directory and serialized types used by older careers.
|
||||
internal const string LegacyIdentity = "Soundings";
|
||||
private readonly string directory;
|
||||
|
||||
internal CareerStore(string directory) => this.directory = directory;
|
||||
|
||||
internal string RecentId
|
||||
{
|
||||
get
|
||||
{
|
||||
string path = Path.Combine(directory, "recent");
|
||||
try { return File.Exists(path) ? File.ReadAllText(path) : null; }
|
||||
catch (Exception error) when (IsFileError(error)) { return null; }
|
||||
}
|
||||
}
|
||||
|
||||
internal List<CareerSave> List()
|
||||
{
|
||||
var result = new List<CareerSave>();
|
||||
if (!Directory.Exists(directory))
|
||||
return result;
|
||||
try
|
||||
{
|
||||
foreach (string file in Directory.GetFiles(directory, "*.json"))
|
||||
{
|
||||
CareerSave saved = Read(Path.GetFileNameWithoutExtension(file));
|
||||
if (saved != null)
|
||||
result.Add(saved);
|
||||
}
|
||||
}
|
||||
catch (Exception error) when (IsFileError(error))
|
||||
{
|
||||
Debug.LogWarning("Could not list careers. " + error.Message);
|
||||
}
|
||||
result.Sort((a, b) => b.SavedUtc.CompareTo(a.SavedUtc));
|
||||
return result;
|
||||
}
|
||||
|
||||
internal CareerSave Read(string id)
|
||||
{
|
||||
if (!Guid.TryParseExact(id, "N", out _))
|
||||
return null;
|
||||
try
|
||||
{
|
||||
string path = Path.Combine(directory, id + ".json");
|
||||
if (!File.Exists(path))
|
||||
return null;
|
||||
CareerSave saved = JsonUtility.FromJson<CareerSave>(File.ReadAllText(path));
|
||||
return saved is { Version: 1, Company: not null } && saved.Id == id && !string.IsNullOrWhiteSpace(saved.Name)
|
||||
? saved : null;
|
||||
}
|
||||
catch (Exception error) when (IsFileError(error) || error is ArgumentException)
|
||||
{
|
||||
Debug.LogWarning("Could not read a career. " + error.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
internal void ImportMissing(CareerStore legacy)
|
||||
{
|
||||
string preferred = RecentId;
|
||||
if (Read(preferred) == null)
|
||||
preferred = legacy.RecentId;
|
||||
foreach (CareerSave saved in legacy.List())
|
||||
{
|
||||
if (Read(saved.Id) == null && saved.Company.CanLoad() && Write(saved, false) && preferred == null)
|
||||
preferred = saved.Id;
|
||||
}
|
||||
if (preferred != null && RecentId != preferred && Read(preferred) != null)
|
||||
Select(preferred);
|
||||
}
|
||||
|
||||
internal bool Write(CareerSave saved, bool select = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
ReplaceFile(Path.Combine(directory, saved.Id + ".json"), JsonUtility.ToJson(saved));
|
||||
}
|
||||
catch (Exception error) when (IsFileError(error))
|
||||
{
|
||||
Debug.LogWarning("Could not write a career. " + error.Message);
|
||||
return false;
|
||||
}
|
||||
if (select)
|
||||
Select(saved.Id);
|
||||
return true;
|
||||
}
|
||||
|
||||
internal void Select(string id)
|
||||
{
|
||||
try { ReplaceFile(Path.Combine(directory, "recent"), id); }
|
||||
catch (Exception error) when (IsFileError(error))
|
||||
{
|
||||
Debug.LogWarning("Could not set the most recent career. " + error.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ReplaceFile(string path, string contents)
|
||||
{
|
||||
string temporary = path + "." + Guid.NewGuid().ToString("N") + ".tmp";
|
||||
try
|
||||
{
|
||||
using (var file = new FileStream(temporary, FileMode.Create, FileAccess.Write, FileShare.None))
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(contents);
|
||||
file.Write(bytes, 0, bytes.Length);
|
||||
file.Flush(true);
|
||||
}
|
||||
if (File.Exists(path))
|
||||
File.Replace(temporary, path, null);
|
||||
else
|
||||
File.Move(temporary, path);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(temporary))
|
||||
File.Delete(temporary);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsFileError(Exception error) => error is IOException or UnauthorizedAccessException;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user