/// <summary> /// Creates a quicksave /// </summary> /// <remarks>Note that this does not display the indicator and can throw exceptions</remarks> public static void DoQuickSaveEx(bool force) { if (!CoreParams.AllowSaveLoad) { throw new NotSupportedException(); } if (!force && (GameState.Instance.SaveLocked || GameState.Instance.ManualSaveLocked || !CoreParams.AllowManualSave)) { throw new SaveNotAllowedException(); } //quicksave format will be q_<hash> //since we aren't supporting campaign-unique autosaves yet, hash will just be 0 string campaignId = "0"; string saveFileName = $"q_{campaignId}.json"; //string saveFilePath = Path.Combine(CoreParams.SavePath, saveFileName); SharedUtils.SaveGame(saveFileName, true, false); Debug.Log($"Quicksave complete ({saveFileName})"); //Debug.LogWarning("Quicksave!"); }
public void OnClickSave() { //Debug.Log("OnClickSave"); //new save if saveName=null //otherwise save over old save if (!GameState.Instance.SaveLocked && !GameState.Instance.ManualSaveLocked) { string saveName = SaveNameField.text; string saveFileName; if (SelectedSaveName != null) { //assume save name is already okay saveName = SelectedSaveName; //we know it already has a prefix saveFileName = saveName + ".json"; string savePath = CoreParams.SavePath + Path.DirectorySeparatorChar + saveName + ".json"; if (File.Exists(savePath)) { File.Delete(savePath); //this "works" but seems to be bugged- race condition? } } else { saveFileName = "m_" + SaveUtils.GetSafeName(saveName) + ".json"; } if (!string.IsNullOrEmpty(saveName)) { try { SharedUtils.SaveGame(saveFileName, true, false); Modal.PushMessageModal(Sub.Replace("SaveSuccessMessage", SubList), Sub.Replace("SaveSuccess", SubList), null, null); } catch (Exception e) { Debug.LogError($"Save failed! ({e.GetType().Name})"); Debug.LogException(e); Modal.PushMessageModal(e.Message, Sub.Replace("SaveFail", SubList), null, null); } SignalPaint(); } else { Modal.PushMessageModal(Sub.Replace("SaveBadFilenameMessage", SubList), Sub.Replace("SaveFail", SubList), null, null); } } else { //can't save! Modal.PushMessageModal(Sub.Replace("SaveNotAllowedMessage", SubList), Sub.Replace("SaveNotAllowed", SubList), null, null); } }
/// <summary> /// Creates an autosave /// </summary> /// <remarks>Note that this does not display the indicator and can throw exceptions</remarks> public static void DoAutoSaveEx(bool force) { if (!CoreParams.AllowSaveLoad) { if (force) //you are not allowed to force a save if it's globally disabled; the assumption is that if it's globally disabled, it won't work at all { throw new NotSupportedException("Save/Load is disabled in core params!"); } throw new SaveNotAllowedException(); } if (GameState.Instance.SaveLocked && !force) { throw new SaveNotAllowedException(); //don't autosave if we're not allowed to } if (ConfigState.Instance.AutosaveCount <= 0 && !force) { throw new SaveNotAllowedException(); //don't autosave if we've disabled it } //autosave format will be a_<hash>_<index> //since we aren't supporting campaign-unique autosaves, yet, hash will just be 0 string campaignId = "0"; string filterString = $"a_{campaignId}_"; string savePath = CoreParams.SavePath; DirectoryInfo saveDInfo = new DirectoryInfo(savePath); FileInfo[] savesFInfo = saveDInfo.GetFiles().Where(f => f.Name.StartsWith(filterString)).OrderBy(f => f.Name).Reverse().ToArray(); //Debug.Log(savesFInfo.Select(f => f.Name).ToNiceString()); List <int> knownSaveIds = new List <int>(); int highestSaveId = 1; foreach (var saveFI in savesFInfo) { try { var nameParts = Path.GetFileNameWithoutExtension(saveFI.Name).Split('_'); int saveId = int.Parse(nameParts[2]); knownSaveIds.Add(saveId); if (saveId > highestSaveId) { highestSaveId = saveId; } } catch (Exception) { Debug.LogWarning($"Found an invalid save file: {saveFI.Name}"); } } //save this autosave string newSaveName = $"a_{campaignId}_{highestSaveId + 1}.json"; SharedUtils.SaveGame(newSaveName, false, false); //remove old autosaves //this I think is the broken part knownSaveIds.Sort(); int numAutosaves = savesFInfo.Length + 1; if (numAutosaves > ConfigState.Instance.AutosaveCount) { int autosavesToDelete = numAutosaves - ConfigState.Instance.AutosaveCount; while (autosavesToDelete > 0) { string oldSaveName = $"a_{campaignId}_{knownSaveIds[0]}.json"; try { File.Delete(Path.Combine(CoreParams.SavePath, oldSaveName)); } catch (Exception e) { Debug.LogWarning($"Failed to delete save file: {oldSaveName} ({e.GetType().Name})"); } knownSaveIds.RemoveAt(0); autosavesToDelete--; } } Debug.Log($"Autosave complete ({newSaveName})"); }
static void Save(string name) { SharedUtils.SaveGame(name, true, true); }