|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using SiegeSong;
|
|
|
|
|
|
|
|
|
|
namespace SiegeSong
|
|
|
|
|
{
|
|
|
|
|
public class SaveFileLoader : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public GameObject PlayerTracker;
|
|
|
|
|
|
|
|
|
|
public InstanceManager InstanceManager;
|
|
|
|
|
public RuntimeManager RuntimeManager;
|
|
|
|
|
|
|
|
|
|
public SaveFile ActiveSaveFile;
|
|
|
|
|
public MetaData ActiveSaveFileMeta;
|
|
|
|
|
public string ActiveSaveFileLocation;
|
|
|
|
|
|
|
|
|
|
private float StartingPositionX = 106.979240417480470f;
|
|
|
|
|
private float StartingPositionY = 060.595188140869140f;
|
|
|
|
|
private float StartingPositionZ = 292.302947998046900f;
|
|
|
|
|
|
|
|
|
|
private float StartingRotationX = 007.244431018829346f;
|
|
|
|
|
private float StartingRotationY = 291.974426269531270f;
|
|
|
|
|
private float StartingRotationZ = 002.719870090484619f;
|
|
|
|
|
|
|
|
|
|
private int[] StartingPlayerInventoryItemIDs = new int[1] { 3 };
|
|
|
|
|
private int[] StartingPlayerInventoryItemQuantities = new int[1] { 1 };
|
|
|
|
|
private int[] StartingPlayerActiveInventoryItemIDs = new int[1] { 3 };
|
|
|
|
|
|
|
|
|
|
private int StartingMinute = 30;
|
|
|
|
|
private int StartingHour = 9;
|
|
|
|
|
private int StartingDay = 1;
|
|
|
|
|
private int StartingYear = 1136;
|
|
|
|
|
private WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
|
|
|
|
|
|
|
|
|
|
private string defaultPlayerSpeciesKey = "_Human";
|
|
|
|
|
|
|
|
|
|
public string DirectoryName = "SaveGameData";
|
|
|
|
|
|
|
|
|
|
public MetaData[] GetSaveFilesMetaData()
|
|
|
|
|
{
|
|
|
|
|
if (!System.IO.Directory.Exists($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\"))
|
|
|
|
|
System.IO.Directory.CreateDirectory($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\");
|
|
|
|
|
|
|
|
|
|
var metaDataList = new List<MetaData>();
|
|
|
|
|
var filePaths = System.IO.Directory.GetFiles($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\");
|
|
|
|
|
for (var i = 0; i < filePaths.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var filePath = filePaths[i];
|
|
|
|
|
if (filePath.EndsWith(".sngm"))
|
|
|
|
|
{
|
|
|
|
|
var fileMetaData = JsonUtility.FromJson<MetaData>(System.IO.File.ReadAllText(filePath));
|
|
|
|
|
metaDataList.Add(fileMetaData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return metaDataList.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveGameFile(string overwrittenFilePath = "")
|
|
|
|
|
{
|
|
|
|
|
if (!System.IO.Directory.Exists($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\"))
|
|
|
|
|
System.IO.Directory.CreateDirectory($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\");
|
|
|
|
|
|
|
|
|
|
if (ActiveSaveFileMeta == null)
|
|
|
|
|
ActiveSaveFileMeta = new MetaData();
|
|
|
|
|
if (ActiveSaveFile == null)
|
|
|
|
|
ActiveSaveFile = new SaveFile();
|
|
|
|
|
|
|
|
|
|
var playerFriendlyFullName = "";
|
|
|
|
|
var actorList = new List<Statistics>();
|
|
|
|
|
for (var i = 0; i < InstanceManager.ActorInstances.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var instance = InstanceManager.ActorInstances[i];
|
|
|
|
|
var instanceID = InstanceManager.ActorInstanceIDs[i];
|
|
|
|
|
if (instanceID == InstanceManager.CurrentPlayerActorInstanceID)
|
|
|
|
|
{
|
|
|
|
|
var actor = instance.GetComponent<StatisticsManager>();
|
|
|
|
|
actorList.Add(actor.Stats);
|
|
|
|
|
playerFriendlyFullName = $"{actor.Stats.FirstName} {actor.Stats.LastName}";
|
|
|
|
|
ActiveSaveFileMeta.PlayerFullName = playerFriendlyFullName;
|
|
|
|
|
ActiveSaveFileMeta.PlayerLevel = actor.Stats.Level;
|
|
|
|
|
ActiveSaveFileMeta.PlayerActiveQuestFriendlyName = "None";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ActiveSaveFile.Actors = actorList;
|
|
|
|
|
var worldObjectList = new List<WorldObject>();
|
|
|
|
|
foreach (var worldObjectInstance in InstanceManager.WorldObjectInstances)
|
|
|
|
|
{
|
|
|
|
|
var worldObject = new WorldObject();
|
|
|
|
|
worldObject.WorldObjectInstanceID = worldObjectInstance.GetComponent<Selectable>().InstanceID;
|
|
|
|
|
worldObject.FriendlyName = worldObjectInstance.GetComponent<Selectable>().Name;
|
|
|
|
|
worldObject.PositionX = worldObjectInstance.transform.position.x;
|
|
|
|
|
worldObject.PositionY = worldObjectInstance.transform.position.y;
|
|
|
|
|
worldObject.PositionZ = worldObjectInstance.transform.position.z;
|
|
|
|
|
worldObject.RotationX = worldObjectInstance.transform.eulerAngles.x;
|
|
|
|
|
worldObject.RotationY = worldObjectInstance.transform.eulerAngles.y;
|
|
|
|
|
worldObject.RotationZ = worldObjectInstance.transform.eulerAngles.z;
|
|
|
|
|
worldObject.DroppableKey = worldObjectInstance.GetComponent<Selectable>().DroppableKey;
|
|
|
|
|
worldObject.StationKey = worldObjectInstance.GetComponent<Selectable>().StationKey;
|
|
|
|
|
worldObject.ArchitectureKey = worldObjectInstance.GetComponent<Selectable>().ArchitectureKey;
|
|
|
|
|
worldObjectList.Add(worldObject);
|
|
|
|
|
}
|
|
|
|
|
ActiveSaveFile.WorldObjects = worldObjectList;
|
|
|
|
|
|
|
|
|
|
ActiveSaveFileMeta.ScreenWidth = Screen.width;
|
|
|
|
|
ActiveSaveFileMeta.ScreenHeight = Screen.height;
|
|
|
|
|
|
|
|
|
|
var systemDateTime = System.DateTime.Now;
|
|
|
|
|
ActiveSaveFileMeta.LastPlayedOn = $"{(systemDateTime.Hour > 12 ? $"{(systemDateTime.Hour - (systemDateTime.Hour == 12 ? 0 : 12)).ToString("D2")}:{systemDateTime.Minute.ToString("D2")}{(systemDateTime.Hour > 12 ? "PM" : "AM")}" : $"{systemDateTime.Hour.ToString("D2")}:{systemDateTime.Minute.ToString("D2")}{(systemDateTime.Hour > 12 ? "PM" : "AM")}")} {systemDateTime.Day.ToString("D2")}/{systemDateTime.Month.ToString("D2")}/{systemDateTime.Year}";
|
|
|
|
|
ActiveSaveFileMeta.InGameDate = $"{(RuntimeManager.Calendar.Hour > 12 ? $"{(RuntimeManager.Calendar.Hour - (systemDateTime.Hour == 12 ? 0 : 12)).ToString("D2")}:{RuntimeManager.Calendar.Minute.ToString("D2")}{(systemDateTime.Hour > 12 ? "PM" : "AM")}" : $"{RuntimeManager.Calendar.Hour.ToString("D2")}:{RuntimeManager.Calendar.Minute.ToString("D2")}{(systemDateTime.Hour > 12 ? "PM" : "AM")}")} {RuntimeManager.Calendar.Day}/{RuntimeManager.Calendar.Month.ToString("D2")}/{RuntimeManager.Calendar.Year}";
|
|
|
|
|
|
|
|
|
|
ActiveSaveFile.Year = RuntimeManager.Calendar.Year;
|
|
|
|
|
ActiveSaveFile.Day = RuntimeManager.Calendar.Day;
|
|
|
|
|
ActiveSaveFile.Minute = RuntimeManager.Calendar.Minute;
|
|
|
|
|
ActiveSaveFile.Hour = RuntimeManager.Calendar.Hour;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(overwrittenFilePath))
|
|
|
|
|
{
|
|
|
|
|
OutputScreenCapture($"{overwrittenFilePath.Replace("/", "\\")}.png");
|
|
|
|
|
System.IO.File.WriteAllText($"{overwrittenFilePath.Replace("/", "\\")}.ssng", JsonUtility.ToJson(ActiveSaveFile));
|
|
|
|
|
System.IO.File.WriteAllText($"{overwrittenFilePath.Replace("/", "\\")}.sngm", JsonUtility.ToJson(ActiveSaveFileMeta));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ActiveSaveFileLocation = $"{Application.persistentDataPath}/{DirectoryName}/{ActiveSaveFileMeta.PlayerFullName.Replace(" ", "_")}";
|
|
|
|
|
var saveFileNameSuffix = "00001";
|
|
|
|
|
var i = 1;
|
|
|
|
|
while (System.IO.File.Exists($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\{ActiveSaveFileMeta.PlayerFullName.Replace(" ", "_")}_{saveFileNameSuffix}.sngm"))
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
saveFileNameSuffix = i.ToString("D5");
|
|
|
|
|
}
|
|
|
|
|
ActiveSaveFileMeta.FileNameSuffix = saveFileNameSuffix;
|
|
|
|
|
ActiveSaveFileLocation = $"{Application.persistentDataPath}/{DirectoryName}/{ActiveSaveFileMeta.PlayerFullName.Replace(" ", "_")}_{saveFileNameSuffix}";
|
|
|
|
|
OutputScreenCapture($"{ActiveSaveFileLocation.Replace("/", "\\")}.png");
|
|
|
|
|
ActiveSaveFileMeta.Title = $"{playerFriendlyFullName} - Save Game {i}";
|
|
|
|
|
System.IO.File.WriteAllText($"{ActiveSaveFileLocation.Replace("/", "\\")}.ssng", JsonUtility.ToJson(ActiveSaveFile));
|
|
|
|
|
System.IO.File.WriteAllText($"{ActiveSaveFileLocation.Replace("/", "\\")}.sngm", JsonUtility.ToJson(ActiveSaveFileMeta));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LoadGameFile()
|
|
|
|
|
{
|
|
|
|
|
RuntimeManager.DePopulateReferences();
|
|
|
|
|
InstanceManager.ClearAndUnload();
|
|
|
|
|
|
|
|
|
|
ActiveSaveFile = JsonUtility.FromJson<SaveFile>(System.IO.File.ReadAllText($"{ActiveSaveFileLocation.Replace("/", "\\")}.ssng"));
|
|
|
|
|
foreach (var stats in ActiveSaveFile.Actors)
|
|
|
|
|
{
|
|
|
|
|
var instance = InstanceManager.InstantiateActor(stats);
|
|
|
|
|
if (stats.IsPlayer)
|
|
|
|
|
{
|
|
|
|
|
PlayerTracker.transform.parent = instance.Value.transform;
|
|
|
|
|
RuntimeManager.PlayerActorInstance = instance.Value;
|
|
|
|
|
InstanceManager.CurrentPlayerActorInstanceID = instance.Key;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach (var worldObject in ActiveSaveFile.WorldObjects)
|
|
|
|
|
{
|
|
|
|
|
InstanceManager.InstantiateWorldObject(worldObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RuntimeManager.Calendar.SetDateTime(ActiveSaveFile.Minute, ActiveSaveFile.Hour, ActiveSaveFile.Day, ActiveSaveFile.Year);
|
|
|
|
|
RuntimeManager.PopulateReferences();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RunNewGameSetup()
|
|
|
|
|
{
|
|
|
|
|
ActiveSaveFile = new SaveFile();
|
|
|
|
|
|
|
|
|
|
GameObject playerActorInstance = null;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < InstanceManager.SpeciesDefinitionsContainer.transform.childCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var species = InstanceManager.SpeciesDefinitionsContainer.transform.GetChild(i);
|
|
|
|
|
if (species.gameObject.name == defaultPlayerSpeciesKey)
|
|
|
|
|
playerActorInstance = GameObject.Instantiate(species).gameObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var actor = playerActorInstance.GetComponent<StatisticsManager>();
|
|
|
|
|
actor.Stats = new Statistics();
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < playerActorInstance.transform.childCount; i++)
|
|
|
|
|
{
|
|
|
|
|
var child = playerActorInstance.transform.GetChild(i);
|
|
|
|
|
if (child.gameObject.name == "PlayerAgent")
|
|
|
|
|
{
|
|
|
|
|
child.gameObject.active = true;
|
|
|
|
|
PlayerTracker.transform.parent = playerActorInstance.transform;
|
|
|
|
|
//playerActorInstance.transform.eulerAngles = new Vector3(StartingRotationX, StartingRotationY, StartingRotationZ);
|
|
|
|
|
playerActorInstance.transform.position = new Vector3(StartingPositionX, StartingPositionY, StartingPositionZ);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < StartingPlayerInventoryItemIDs.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
actor.Inventory.SelectedItemID = StartingPlayerInventoryItemIDs[i];
|
|
|
|
|
actor.Inventory.SelectedAmount = StartingPlayerInventoryItemQuantities[i];
|
|
|
|
|
actor.Inventory.AddItem();
|
|
|
|
|
actor.Inventory.SelectedItemID = 0;
|
|
|
|
|
actor.Inventory.SelectedAmount = 0;
|
|
|
|
|
actor.Inventory.SelectedItemIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var itemId in StartingPlayerActiveInventoryItemIDs)
|
|
|
|
|
{
|
|
|
|
|
if (actor.Inventory.GetItemByID(itemId).GetComponent<Equipable>() != null)
|
|
|
|
|
actor.Inventory.EquipmentManager.AddEquipmentToSlots(actor.Inventory.GetItemByID(itemId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actor.Inventory.ApparrelManager.CurrentlyActiveIDs = StartingPlayerActiveInventoryItemIDs;
|
|
|
|
|
actor.Inventory.ApparrelManager.UpdateApparrel();
|
|
|
|
|
|
|
|
|
|
actor.Stats.ActorInstanceID = InstanceManager.GenerateActorInstanceID();
|
|
|
|
|
playerActorInstance.gameObject.name = $"{actor.Stats.FirstName} {actor.Stats.LastName}";
|
|
|
|
|
InstanceManager.ActorInstances.Add(playerActorInstance);
|
|
|
|
|
InstanceManager.ActorInstanceIDs.Add(actor.Stats.ActorInstanceID);
|
|
|
|
|
InstanceManager.CurrentPlayerActorInstanceID = actor.Stats.ActorInstanceID;
|
|
|
|
|
RuntimeManager.PlayerActorInstance = playerActorInstance;
|
|
|
|
|
playerActorInstance.transform.parent = InstanceManager.ActorInstanceContainer.transform;
|
|
|
|
|
playerActorInstance.gameObject.active = true;
|
|
|
|
|
RuntimeManager.PopulateReferences();
|
|
|
|
|
RuntimeManager.Calendar.SetDateTime(StartingMinute, StartingHour, StartingDay, StartingYear);
|
|
|
|
|
//ActorEditor.ChangeAppearance(actor);
|
|
|
|
|
//StoryBeatManager.BeginNewTimeline();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OutputScreenCapture(string savedCaptureOutputFilePath = "")
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(savedCaptureOutputFilePath))
|
|
|
|
|
{
|
|
|
|
|
if (System.IO.File.Exists($"{savedCaptureOutputFilePath.Replace("/", "\\")}"))
|
|
|
|
|
System.IO.File.Delete($"{savedCaptureOutputFilePath.Replace("/", "\\")}");
|
|
|
|
|
System.IO.File.Move($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\_Temp.png", $"{savedCaptureOutputFilePath.Replace("/", "\\")}");
|
|
|
|
|
}
|
|
|
|
|
System.IO.File.Delete($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\_Temp.png");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerator TakeTemporaryScreenCapture()
|
|
|
|
|
{
|
|
|
|
|
var camera = RuntimeManager.PlayerActorInstance.GetComponentInChildren<CameraManager>().ActiveCamera.GetComponentInChildren<Camera>();
|
|
|
|
|
var screenTexture = new RenderTexture(Screen.width, Screen.height, 16);
|
|
|
|
|
var oldTargetTexture = camera.targetTexture;
|
|
|
|
|
|
|
|
|
|
RuntimeManager.PlayerActorInstance.GetComponentInChildren<HUDManager>().UIWrapper.active = false;
|
|
|
|
|
|
|
|
|
|
camera.targetTexture = screenTexture;
|
|
|
|
|
camera.Render();
|
|
|
|
|
var renderedTexture = new Texture2D(Screen.width, Screen.height);
|
|
|
|
|
yield return waitForEndOfFrame;
|
|
|
|
|
renderedTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
|
|
|
|
|
camera.targetTexture = oldTargetTexture;
|
|
|
|
|
|
|
|
|
|
var byteArray = renderedTexture.EncodeToPNG();
|
|
|
|
|
|
|
|
|
|
RuntimeManager.PlayerActorInstance.GetComponentInChildren<HUDManager>().UIWrapper.active = true;
|
|
|
|
|
|
|
|
|
|
System.IO.File.WriteAllBytes($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\_Temp.png", byteArray);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Start() { }
|
|
|
|
|
|
|
|
|
|
void Update() { }
|
|
|
|
|
}
|
|
|
|
|
}
|