You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
353 lines
18 KiB
C#
353 lines
18 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using SiegeSong;
|
|
using Gaia;
|
|
using System;
|
|
|
|
namespace SiegeSong
|
|
{
|
|
public class SaveFileLoader : MonoBehaviour
|
|
{
|
|
public InstanceManager InstanceManager;
|
|
public RuntimeManager RuntimeManager;
|
|
public HUDManager HUDManager;
|
|
|
|
public GameDataPlugin[] MainPlugins;
|
|
public GameDataPlugin[] SidePlugins;
|
|
|
|
public Selector Selector;
|
|
|
|
public SaveFile ActiveSaveFile;
|
|
public MetaData ActiveSaveFileMeta;
|
|
public string ActiveSaveFileLocation;
|
|
|
|
private float StartingPositionX = -35333.8f;
|
|
private float StartingPositionY = 122.24f;
|
|
private float StartingPositionZ = 22364.81f;
|
|
|
|
private float StartingRotationX = 000.00000000000000f;
|
|
private float StartingRotationY = 173.97465515136720f;
|
|
private float StartingRotationZ = 000.00000000000000f;
|
|
|
|
private int[] StartingPlayerInventoryItemIDs = new int[2] { 3, 5 };
|
|
private int[] StartingPlayerInventoryItemQuantities = new int[2] { 1, 1 };
|
|
private int[] StartingPlayerActiveInventoryItemIDs = new int[2] { 3, 5 };
|
|
|
|
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<WorldObjectPlacement>();
|
|
foreach (var worldObjectInstance in InstanceManager.WorldObjectInstances)
|
|
{
|
|
var worldObject = new WorldObjectPlacement();
|
|
worldObject.InstanceID = worldObjectInstance.GetComponent<WorldObjectPlacement>().InstanceID;
|
|
worldObject.FriendlyName = worldObjectInstance.GetComponent<WorldObjectPlacement>().FriendlyName;
|
|
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.Key = worldObjectInstance.GetComponent<WorldObjectPlacement>().Key;
|
|
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)
|
|
{
|
|
InstanceManager.SwitchPlayerInstance(instance.Value, instance.Key);
|
|
//var terrainLoaderObject = RuntimeManager.CameraManager.ActiveCamera.GetComponentInChildren<TerrainLoader>().gameObject;
|
|
//terrainLoaderObject.transform.SetParent(instance.Value.transform);
|
|
//terrainLoaderObject.transform.localPosition = new Vector3();
|
|
//terrainLoaderObject.transform.localRotation = new Quaternion();
|
|
}
|
|
}
|
|
foreach (var worldObject in ActiveSaveFile.WorldObjects)
|
|
{
|
|
InstanceManager.InstantiateWorldObject(worldObject);
|
|
}
|
|
|
|
RuntimeManager.Calendar.SetDateTime(ActiveSaveFile.Minute, ActiveSaveFile.Hour, ActiveSaveFile.Day, ActiveSaveFile.Year);
|
|
RuntimeManager.PopulateReferences();
|
|
}
|
|
|
|
public KeyValuePair<int, int> InitializePlugins()
|
|
{
|
|
var pluginListFileName = "Plugins\\siegesong.ssglst";
|
|
|
|
var logOutput = "";
|
|
|
|
|
|
if (!System.IO.File.Exists(pluginListFileName))
|
|
return new KeyValuePair<int, int>(0, -404);
|
|
else
|
|
{
|
|
var mainSuccessCount = 0;
|
|
var pluginList = JsonUtility.FromJson<GamePluginList>(System.IO.File.ReadAllText(pluginListFileName));
|
|
var mainPlugins = new List<GameDataPlugin>();
|
|
foreach (var mainPlugin in pluginList.MainPlugins)
|
|
{
|
|
try
|
|
{
|
|
mainSuccessCount++;
|
|
mainPlugins.Add(JsonUtility.FromJson<GameDataPlugin>(System.IO.File.ReadAllText($"Plugins\\{mainPlugin}.ssgmnf")));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logOutput += $"ERROR - Main Plugin - {mainPlugin} - {ex.Message}\r\n";
|
|
}
|
|
}
|
|
var sideSuccessCount = 0;
|
|
var sidePlugins = new List<GameDataPlugin>();
|
|
foreach (var sidePlugin in pluginList.SidePlugins)
|
|
{
|
|
try
|
|
{
|
|
sideSuccessCount++;
|
|
sidePlugins.Add(JsonUtility.FromJson<GameDataPlugin>(System.IO.File.ReadAllText($"Plugins\\{sidePlugin}.ssgmnf")));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logOutput += $"ERROR - Side Plugin - {sidePlugin} - {ex.Message}\r\n";
|
|
}
|
|
}
|
|
this.MainPlugins = mainPlugins.ToArray();
|
|
this.SidePlugins = sidePlugins.ToArray();
|
|
return new KeyValuePair<int, int>(mainSuccessCount + sideSuccessCount, pluginList.MainPlugins.Length + pluginList.SidePlugins.Length - (mainSuccessCount + sideSuccessCount));
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public void RunNewGameSetup()
|
|
{
|
|
ActiveSaveFile = new SaveFile();
|
|
|
|
GameObject playerActorInstance;
|
|
var stats = new Statistics();
|
|
stats.IsPlayer = true;
|
|
stats.LocationX = StartingPositionX;
|
|
stats.LocationY = StartingPositionY;
|
|
stats.LocationZ = StartingPositionZ;
|
|
stats.RotationX = StartingRotationX;
|
|
stats.RotationY = StartingRotationY;
|
|
stats.RotationZ = StartingRotationZ;
|
|
|
|
playerActorInstance = InstanceManager.InstantiateActor(stats).Value;
|
|
playerActorInstance.tag = "Player";
|
|
|
|
var actor = playerActorInstance.GetComponent<StatisticsManager>();
|
|
actor.Stats = new Statistics();
|
|
|
|
InstanceManager.SwitchPlayerInstance(playerActorInstance, actor.Stats.ActorInstanceID);
|
|
var terrainLoader = playerActorInstance.GetComponent<TerrainLoader>();
|
|
terrainLoader.SetEnable(true);
|
|
|
|
var floatingPointFix = playerActorInstance.GetComponent<FloatingPointFix>();
|
|
floatingPointFix.SetEnable(true);
|
|
var offsetX = StartingPositionX > 1000 ? StartingPositionX - 1000.0f : StartingPositionX < -1000.0f ? StartingPositionX + 1000.0f : 0.0f;
|
|
var offsetY = StartingPositionY > 1000 ? StartingPositionY - 1000.0f : StartingPositionY < -1000.0f ? StartingPositionY + 1000.0f : 0.0f;
|
|
var offsetZ = StartingPositionZ > 1000 ? StartingPositionZ - 1000.0f : StartingPositionZ < -1000.0f ? StartingPositionZ + 1000.0f : 0.0f;
|
|
floatingPointFix.totalOffset = new Vector3(offsetX, offsetY, offsetZ);
|
|
|
|
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.InventoryManager.Inventory.AddItem(StartingPlayerInventoryItemIDs[i], StartingPlayerInventoryItemQuantities[i]);
|
|
actor.InventoryManager.AddInventoryRow(StartingPlayerInventoryItemIDs[i], StartingPlayerInventoryItemQuantities[i]);
|
|
}
|
|
|
|
foreach (var itemId in StartingPlayerActiveInventoryItemIDs)
|
|
{
|
|
if (actor.InventoryManager.GetItemByID(itemId).GetComponent<Equipable>() != null)
|
|
actor.InventoryManager.EquipmentManager.AddEquipmentToSlots(actor.InventoryManager.GetItemByID(itemId));
|
|
}
|
|
|
|
actor.InventoryManager.ApparrelManager.CurrentlyActiveIDs = StartingPlayerActiveInventoryItemIDs;
|
|
actor.InventoryManager.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.GetComponent<StatisticsManager>().HUDManager = HUDManager;
|
|
playerActorInstance.gameObject.SetActive(true);
|
|
RuntimeManager.PopulateReferences();
|
|
RuntimeManager.Calendar.SetDateTime(StartingMinute, StartingHour, StartingDay, StartingYear);
|
|
playerActorInstance.GetComponent<Motor>().CameraManager = RuntimeManager.CameraManager;
|
|
RuntimeManager.InputManager.Motor = playerActorInstance.GetComponent<Motor>();
|
|
RuntimeManager.CameraManager.Motor = playerActorInstance.GetComponent<Motor>();
|
|
Selector.Inventory = playerActorInstance.GetComponentInChildren<InventoryManager>();
|
|
|
|
for (var i = 0; i < playerActorInstance.transform.childCount; i++)
|
|
{
|
|
var child = playerActorInstance.transform.GetChild(i);
|
|
if (child.gameObject.name == "CameraTarget")
|
|
RuntimeManager.CameraManager.CameraTarget = child;
|
|
}
|
|
|
|
foreach (var camera in RuntimeManager.CameraManager.NonPlayerCameras)
|
|
camera.SetActive(false);
|
|
|
|
Selector.CameraManager = RuntimeManager.CameraManager;
|
|
RuntimeManager.CameraManager.ActiveCamera.transform.rotation = playerActorInstance.transform.rotation;
|
|
RuntimeManager.CameraManager.ActiveCamera.transform.position = playerActorInstance.transform.position;
|
|
RuntimeManager.CameraManager.ActiveMount = RuntimeManager.CameraManager.CenterMount;
|
|
RuntimeManager.InputManager.CameraManager = RuntimeManager.CameraManager;
|
|
RuntimeManager.InputManager.Selector = Selector;
|
|
RuntimeManager.InputManager.CurrentState = InputState.World;
|
|
|
|
//terrainLoaderObject.transform.SetParent(playerActorInstance.transform);
|
|
//terrainLoaderObject.transform.localPosition = new Vector3();
|
|
//terrainLoaderObject.transform.localRotation = new Quaternion();
|
|
|
|
//ActorEditor.ChangeAppearance(actor);
|
|
//StoryBeatManager.BeginNewTimeline();
|
|
|
|
foreach (var camera in RuntimeManager.CameraManager.NonPlayerCameras)
|
|
camera.SetActive(true);
|
|
}
|
|
|
|
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;
|
|
|
|
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();
|
|
|
|
System.IO.File.WriteAllBytes($"{Application.persistentDataPath.Replace("/", "\\")}\\{DirectoryName}\\_Temp.png", byteArray);
|
|
|
|
}
|
|
|
|
void Start() { }
|
|
|
|
void Update() { }
|
|
}
|
|
}
|