rimworld-animation-studio/Assets/Scripts/Managers/ApplicationManager.cs

287 lines
9.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
2022-09-19 05:35:34 +00:00
using System.IO;
using UnityEngine.UI;
using SFB;
namespace RimWorldAnimationStudio
{
public class ApplicationManager : Singleton<ApplicationManager>
{
public DialogBox exitDialog;
public DialogBox newAnimationDialog;
public SelectAnimationDialog selectAnimationDialog;
2022-10-31 05:44:53 +00:00
public void Awake()
2022-10-12 06:16:08 +00:00
{
2022-10-27 05:56:04 +00:00
LoadPawnRaceDefs();
2022-10-31 05:44:53 +00:00
LoadActorAddonDefs();
2022-10-12 06:16:08 +00:00
LoadCustomArrays();
}
public void TryToCloseApplication()
{
exitDialog.Pop();
}
public void CloseApplication()
{
Debug.Log("Exiting application");
Application.Quit();
}
public void TryToLoadAnimation()
{
var paths = StandaloneFileBrowser.OpenFilePanel("Open AnimationDef File", "", "xml", false);
if (paths == null || paths.Any() == false)
2022-09-26 04:10:41 +00:00
{ Debug.LogWarning("Selected file was null or invalid"); return; }
2022-10-31 05:44:53 +00:00
AnimationDefs defs = XmlUtility.ReadXML<AnimationDefs>(paths[0]);
2022-10-12 05:22:29 +00:00
try
2022-10-27 05:56:04 +00:00
{ defs = XmlUtility.ReadXML<AnimationDefs>(paths[0]); }
2022-10-12 05:22:29 +00:00
catch
{ Debug.LogError("Could not read .xml file '" + paths[0] + "' - this file either uses a schema which is outdated, incorrect, or contains empty fields"); return; }
if (defs?.animationDefs == null)
{ Debug.LogError("Selected file contains no animation data"); return; }
if (defs.animationDefs.Count == 1)
{ LoadAnimation(defs.animationDefs[0]); return; }
2022-10-12 05:22:29 +00:00
Workspace.animationSavePath = paths[0];
selectAnimationDialog.Initialize(defs);
selectAnimationDialog.Pop();
}
public void LoadAnimation(AnimationDef animationDef)
{
2022-09-21 21:15:25 +00:00
UpdateCustomArrays(animationDef);
RunPostLoadOperations(animationDef);
2022-10-29 00:52:58 +00:00
animationDef.Initialize();
2022-09-21 21:15:25 +00:00
2022-10-27 05:56:04 +00:00
Debug.Log("Loaded AnimationDef: " + animationDef.DefName);
2022-09-25 06:15:27 +00:00
2022-10-29 00:52:58 +00:00
Workspace.Reset();
Workspace.animationDef = animationDef;
2022-09-19 00:07:44 +00:00
AnimationController.Instance.Reset();
2022-10-29 00:52:58 +00:00
AnimationController.Instance.Initialize();
2022-10-28 05:28:51 +00:00
EventsManager.OnAnimationChanged();
2022-10-27 05:56:04 +00:00
Workspace.RecordEvent("AnimationDef loaded");
2022-10-29 00:52:58 +00:00
Workspace.ActorID = 0;
EventsManager.OnActorIDChanged();
}
2022-09-21 21:15:25 +00:00
public void RunPostLoadOperations(AnimationDef animationDef)
{
2022-10-14 19:00:57 +00:00
if (animationDef.animationTimeTicksQuick <= 0)
{
2022-10-27 05:56:04 +00:00
if (animationDef.AnimationStages.Count > 1)
2022-10-14 19:00:57 +00:00
{
2022-10-27 05:56:04 +00:00
for (int i = 0; i < animationDef.AnimationStages.Count; i++)
2022-10-14 19:00:57 +00:00
{
if (i == 0) continue;
2022-10-27 05:56:04 +00:00
animationDef.AnimationStages[i].PlayTimeTicksQuick = animationDef.AnimationStages[i].PlayTimeTicks;
2022-10-14 19:00:57 +00:00
}
}
2022-10-27 05:56:04 +00:00
else if (animationDef.AnimationStages.Count == 1)
{ animationDef.AnimationStages[0].PlayTimeTicksQuick = animationDef.AnimationStages[0].PlayTimeTicks; }
2022-10-14 19:00:57 +00:00
}
2022-10-27 05:56:04 +00:00
foreach (AnimationStage stage in animationDef.AnimationStages)
{
stage.OnPostLoad();
}
2022-09-21 21:15:25 +00:00
}
public void TryToSaveAnimation()
{
if (Workspace.animationDef == null)
{ return; }
2022-10-12 05:22:29 +00:00
string path = Workspace.animationSavePath;
if (path != null && path != "" && File.Exists(path))
{ SaveAnimation(path); }
2022-10-12 05:22:29 +00:00
else
{ TryToSaveAnimationAs(); }
}
2022-10-12 05:22:29 +00:00
public void TryToSaveAnimationAs()
{
if (Workspace.animationDef == null)
{ return; }
2022-10-27 05:56:04 +00:00
string defName = Workspace.animationDef.DefName != null && Workspace.animationDef.DefName != "" ? Workspace.animationDef.DefName : "newAnimationDef";
2022-10-12 05:22:29 +00:00
string path = StandaloneFileBrowser.SaveFilePanel("Save AnimationDef File", "", defName, "xml");
if (path != null && path != "")
{ SaveAnimation(path); }
}
public void SaveAnimation(string path)
{
2022-09-19 00:07:44 +00:00
AnimationDef animationDef = Workspace.animationDef.Copy();
2022-09-21 21:15:25 +00:00
RunPreSaveOperations(animationDef);
2022-10-27 05:56:04 +00:00
Debug.Log("Saving AnimationDef: " + Workspace.animationDef.DefName);
2022-10-27 05:56:04 +00:00
AnimationDefs defs = new AnimationDefs();
defs.animationDefs.Add(animationDef);
XmlUtility.WriteXML(defs, path);
2022-10-12 05:22:29 +00:00
Workspace.animationSavePath = path;
}
2022-09-21 21:15:25 +00:00
public void RunPreSaveOperations(AnimationDef animationDef)
{
2022-10-27 05:56:04 +00:00
animationDef.OnPreSave();
2022-09-26 04:10:41 +00:00
2022-10-27 05:56:04 +00:00
foreach (Actor actor in animationDef.Actors)
{ actor.OnPreSave(); }
2022-09-26 04:10:41 +00:00
2022-10-27 05:56:04 +00:00
foreach (AnimationStage stage in animationDef.AnimationStages)
2022-09-21 21:15:25 +00:00
{
2022-10-27 05:56:04 +00:00
stage.OnPreSave();
2022-09-21 21:15:25 +00:00
2022-10-27 05:56:04 +00:00
foreach (PawnAnimationClip clip in stage.AnimationClips)
2022-09-21 21:15:25 +00:00
{
2022-10-27 05:56:04 +00:00
clip.OnPreSave();
2022-09-21 21:15:25 +00:00
2022-10-27 05:56:04 +00:00
foreach (PawnKeyframe keyframe in clip.Keyframes)
{ keyframe.OnPreSave(); }
2022-09-21 21:15:25 +00:00
}
}
}
public void TryToMakeNewAnimation()
{
if (Workspace.animationDef == null)
{ NewAnimation(); return; }
newAnimationDialog.Pop();
}
public void NewAnimation()
{
2022-09-19 05:35:34 +00:00
var path = Path.Combine(Application.streamingAssetsPath, "AnimationDefs/newAnimationDef.xml");
2022-10-27 05:56:04 +00:00
AnimationDefs defs = XmlUtility.ReadXML<AnimationDefs>(path);
2022-09-19 05:35:34 +00:00
if (defs?.animationDefs == null)
{ Debug.LogError("Default animation def file contains no animation data"); return; }
2022-09-19 05:35:34 +00:00
LoadAnimation(defs.animationDefs[0]);
2022-10-12 05:22:29 +00:00
Workspace.animationSavePath = null;
}
2022-09-21 21:15:25 +00:00
public void SaveCustomArrays()
{
2022-10-08 02:52:27 +00:00
var path = Path.Combine(Application.persistentDataPath, "customTags.xml");
2022-09-21 21:15:25 +00:00
CustomTagsHelper helper = new CustomTagsHelper();
2022-10-12 05:22:29 +00:00
helper.defNames = CustomTags.defNames;
2022-09-21 21:15:25 +00:00
helper.bodyParts = CustomTags.bodyParts;
helper.bodyDefTypes = CustomTags.bodyDefTypes;
helper.sexTypes = CustomTags.sexTypes;
helper.interactionDefTypes = CustomTags.interactionDefTypes;
helper.soundDefs = CustomTags.soundDefs;
XmlUtility.WriteXML(helper, path);
}
public void LoadCustomArrays()
{
2022-10-08 02:52:27 +00:00
string path;
2022-09-21 21:15:25 +00:00
2022-10-08 02:52:27 +00:00
if (File.Exists(Path.Combine(Application.persistentDataPath, "customTags.xml")))
{ path = Path.Combine(Application.persistentDataPath, "customTags.xml"); }
else
{ path = Path.Combine(Application.streamingAssetsPath, "customTags.xml"); }
2022-09-21 21:15:25 +00:00
if (File.Exists(path) == false)
{ SaveCustomArrays(); return; }
CustomTagsHelper helper = XmlUtility.ReadXML<CustomTagsHelper>(path);
2022-10-12 05:22:29 +00:00
CustomTags.defNames = helper.defNames;
2022-09-21 21:15:25 +00:00
CustomTags.bodyParts = helper.bodyParts;
CustomTags.bodyDefTypes = helper.bodyDefTypes;
CustomTags.sexTypes = helper.sexTypes;
CustomTags.interactionDefTypes = helper.interactionDefTypes;
CustomTags.soundDefs = helper.soundDefs;
}
public void UpdateCustomArrays(AnimationDef animationDef)
{
2022-10-27 05:56:04 +00:00
CustomTags.bodyParts.AddRangeDistinct(animationDef.Actors.SelectMany(x => x.RequiredGenitals).Except(DefaultTags.bodyParts));
CustomTags.bodyDefTypes.AddRangeDistinct(animationDef.Actors.SelectMany(x => x.BodyDefTypes).Except(DefaultTags.bodyDefTypes));
CustomTags.sexTypes.AddRangeDistinct(animationDef.SexTypes.Except(DefaultTags.sexTypes));
CustomTags.interactionDefTypes.AddRangeDistinct(animationDef.InteractionDefTypes.Except(DefaultTags.interactionDefTypes));
CustomTags.soundDefs.AddRangeDistinct(animationDef.AnimationStages.SelectMany(x => x.AnimationClips.SelectMany(y => y.Keyframes.Select(z => z.SoundEffect))).Except(DefaultTags.soundDefs));
2022-09-21 21:15:25 +00:00
SaveCustomArrays();
}
2022-09-24 07:17:40 +00:00
2022-10-27 05:56:04 +00:00
public void LoadPawnRaceDefs()
2022-09-24 07:17:40 +00:00
{
2022-10-08 02:52:27 +00:00
string path;
2022-10-27 05:56:04 +00:00
if (File.Exists(Path.Combine(Application.persistentDataPath, "pawnRaceDefs.xml")))
{ path = Path.Combine(Application.persistentDataPath, "pawnRaceDefs.xml"); }
2022-10-08 02:52:27 +00:00
else
2022-10-27 05:56:04 +00:00
{ path = Path.Combine(Application.streamingAssetsPath, "pawnRaceDefs.xml"); }
2022-09-24 07:17:40 +00:00
2022-10-27 05:56:04 +00:00
PawnRaceDefs.allDefs = XmlUtility.ReadXML<List<PawnRaceDef>>(path);
2022-10-31 05:44:53 +00:00
SavePawnRaceDefs();
2022-10-27 05:56:04 +00:00
PawnRaceDefs.OnLoad();
2022-09-24 07:17:40 +00:00
}
2022-10-27 05:56:04 +00:00
public void SavePawnRaceDefs()
2022-09-24 07:17:40 +00:00
{
2022-10-27 05:56:04 +00:00
var path = Path.Combine(Application.persistentDataPath, "pawnRaceDefs.xml");
2022-09-24 07:17:40 +00:00
2022-10-27 05:56:04 +00:00
XmlUtility.WriteXML(PawnRaceDefs.allDefs, path);
2022-09-24 07:17:40 +00:00
}
2022-10-31 05:44:53 +00:00
public void LoadActorAddonDefs()
{
string path;
if (File.Exists(Path.Combine(Application.persistentDataPath, "actorAddonDefs.xml")))
{ path = Path.Combine(Application.persistentDataPath, "actorAddonDefs.xml"); }
else
{ path = Path.Combine(Application.streamingAssetsPath, "actorAddonDefs.xml"); }
ActorAddonDefs.allDefs = XmlUtility.ReadXML<List<ActorAddonDef>>(path);
SaveActorAddonDefs();
ActorAddonDefs.OnLoad();
}
public void SaveActorAddonDefs()
{
}
}
}