Code refactor

This commit is contained in:
AbstractConcept 2022-10-27 00:56:04 -05:00
parent cd4711a8e5
commit 757badf4f6
517 changed files with 2534 additions and 2221 deletions

View file

@ -18,7 +18,7 @@ namespace RimWorldAnimationStudio
public void Start()
{
LoadAlienRaceDefs();
LoadPawnRaceDefs();
LoadCustomArrays();
}
@ -40,10 +40,10 @@ namespace RimWorldAnimationStudio
if (paths == null || paths.Any() == false)
{ Debug.LogWarning("Selected file was null or invalid"); return; }
Defs defs = null;
AnimationDefs defs = null;
try
{ defs = XmlUtility.ReadXML<Defs>(paths[0]); }
{ defs = XmlUtility.ReadXML<AnimationDefs>(paths[0]); }
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; }
@ -65,38 +65,39 @@ namespace RimWorldAnimationStudio
UpdateCustomArrays(animationDef);
RunPostLoadOperations(animationDef);
Debug.Log("Loaded AnimationDef: " + animationDef.defName);
Debug.Log("Loaded AnimationDef: " + animationDef.DefName);
Workspace.animationDef = animationDef;
animationDef.Initialize();
AnimationController.Instance.Reset();
Workspace.Instance.Reset();
Workspace.Instance.RecordEvent("AnimationDef loaded");
Workspace.Reset();
EventsManager.OnAnimationDefChanged();
Workspace.RecordEvent("AnimationDef loaded");
}
public void RunPostLoadOperations(AnimationDef animationDef)
{
if (animationDef.animationTimeTicksQuick <= 0)
{
if (animationDef.animationStages.Count > 1)
if (animationDef.AnimationStages.Count > 1)
{
for (int i = 0; i < animationDef.animationStages.Count; i++)
for (int i = 0; i < animationDef.AnimationStages.Count; i++)
{
if (i == 0) continue;
animationDef.animationStages[i].playTimeTicksQuick = animationDef.animationStages[i].playTimeTicks;
animationDef.AnimationStages[i].PlayTimeTicksQuick = animationDef.AnimationStages[i].PlayTimeTicks;
}
}
else if (animationDef.animationStages.Count == 0)
{ animationDef.animationStages[0].playTimeTicksQuick = animationDef.animationStages[0].playTimeTicks; }
else if (animationDef.AnimationStages.Count == 1)
{ animationDef.AnimationStages[0].PlayTimeTicksQuick = animationDef.AnimationStages[0].PlayTimeTicks; }
}
foreach (AnimationStage stage in animationDef.animationStages)
foreach (AnimationStage stage in animationDef.AnimationStages)
{
stage.OnPostLoad();
}
}
public void TryToSaveAnimation()
@ -118,7 +119,7 @@ namespace RimWorldAnimationStudio
if (Workspace.animationDef == null)
{ return; }
string defName = Workspace.animationDef.defName != null && Workspace.animationDef.defName != "" ? Workspace.animationDef.defName : "newAnimationDef";
string defName = Workspace.animationDef.DefName != null && Workspace.animationDef.DefName != "" ? Workspace.animationDef.DefName : "newAnimationDef";
string path = StandaloneFileBrowser.SaveFilePanel("Save AnimationDef File", "", defName, "xml");
if (path != null && path != "")
@ -130,9 +131,9 @@ namespace RimWorldAnimationStudio
AnimationDef animationDef = Workspace.animationDef.Copy();
RunPreSaveOperations(animationDef);
Debug.Log("Saving AnimationDef: " + Workspace.animationDef.defName);
Debug.Log("Saving AnimationDef: " + Workspace.animationDef.DefName);
Defs defs = new Defs();
AnimationDefs defs = new AnimationDefs();
defs.animationDefs.Add(animationDef);
XmlUtility.WriteXML(defs, path);
@ -142,21 +143,21 @@ namespace RimWorldAnimationStudio
public void RunPreSaveOperations(AnimationDef animationDef)
{
animationDef.ValidateData();
animationDef.OnPreSave();
foreach (Actor actor in animationDef.actors)
{ actor.ValidateData(); }
foreach (Actor actor in animationDef.Actors)
{ actor.OnPreSave(); }
foreach (AnimationStage stage in animationDef.animationStages)
foreach (AnimationStage stage in animationDef.AnimationStages)
{
stage.ValidateData();
stage.OnPreSave();
foreach (PawnAnimationClip clip in stage.animationClips)
foreach (PawnAnimationClip clip in stage.AnimationClips)
{
clip.ValidateData();
clip.OnPreSave();
foreach (PawnKeyframe keyframe in clip.keyframes)
{ keyframe.ValidateData(); }
foreach (PawnKeyframe keyframe in clip.Keyframes)
{ keyframe.OnPreSave(); }
}
}
}
@ -173,7 +174,7 @@ namespace RimWorldAnimationStudio
{
var path = Path.Combine(Application.streamingAssetsPath, "AnimationDefs/newAnimationDef.xml");
Defs defs = XmlUtility.ReadXML<Defs>(path);
AnimationDefs defs = XmlUtility.ReadXML<AnimationDefs>(path);
if (defs?.animationDefs == null)
{ Debug.LogError("Default animation def file contains no animation data"); return; }
@ -224,37 +225,37 @@ namespace RimWorldAnimationStudio
public void UpdateCustomArrays(AnimationDef animationDef)
{
CustomTags.bodyParts.AddRangeDistinct(animationDef.actors.SelectMany(x => x.requiredGenitals).Except(Tags.bodyParts));
CustomTags.bodyDefTypes.AddRangeDistinct(animationDef.actors.SelectMany(x => x.bodyDefTypes).Except(Tags.bodyDefTypes));
CustomTags.sexTypes.AddRangeDistinct(animationDef.sexTypes.Except(Tags.sexTypes));
CustomTags.interactionDefTypes.AddRangeDistinct(animationDef.interactionDefTypes.Except(Tags.interactionDefTypes));
CustomTags.soundDefs.AddRangeDistinct(animationDef.animationStages.SelectMany(x => x.animationClips.SelectMany(y => y.keyframes.Select(z => z.soundEffect))).Except(Tags.soundDefs));
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));
SaveCustomArrays();
}
public void LoadAlienRaceDefs()
public void LoadPawnRaceDefs()
{
string path;
if (File.Exists(Path.Combine(Application.persistentDataPath, "alienRaceDefs.xml")))
{ path = Path.Combine(Application.persistentDataPath, "alienRaceDefs.xml"); }
if (File.Exists(Path.Combine(Application.persistentDataPath, "pawnRaceDefs.xml")))
{ path = Path.Combine(Application.persistentDataPath, "pawnRaceDefs.xml"); }
else
{ path = Path.Combine(Application.streamingAssetsPath, "alienRaceDefs.xml"); }
{ path = Path.Combine(Application.streamingAssetsPath, "pawnRaceDefs.xml"); }
if (File.Exists(path) == false)
{ SaveAlienRaceDefs(); return; }
{ SavePawnRaceDefs(); return; }
AlienRaceDefs.allDefs = XmlUtility.ReadXML<List<AlienRaceDef>>(path);
AlienRaceDefs.OnLoad();
PawnRaceDefs.allDefs = XmlUtility.ReadXML<List<PawnRaceDef>>(path);
PawnRaceDefs.OnLoad();
}
public void SaveAlienRaceDefs()
public void SavePawnRaceDefs()
{
var path = Path.Combine(Application.persistentDataPath, "alienRaceDefs.xml");
var path = Path.Combine(Application.persistentDataPath, "pawnRaceDefs.xml");
XmlUtility.WriteXML(AlienRaceDefs.allDefs, path);
XmlUtility.WriteXML(PawnRaceDefs.allDefs, path);
}
}
}