2022-09-15 05:17:44 +00:00
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 ;
2022-09-15 05:17:44 +00:00
using UnityEngine.UI ;
using SFB ;
namespace RimWorldAnimationStudio
{
public class ApplicationManager : Singleton < ApplicationManager >
{
public DialogBox exitDialog ;
2022-10-02 22:39:03 +00:00
public DialogBox newAnimationDialog ;
2022-09-15 05:17:44 +00:00
public SelectAnimationDialog selectAnimationDialog ;
2022-10-12 06:16:08 +00:00
public void Start ( )
{
LoadAlienRaceDefs ( ) ;
LoadCustomArrays ( ) ;
}
2022-09-15 05:17:44 +00:00
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-09-15 05:17:44 +00:00
2022-10-12 05:22:29 +00:00
Defs defs = null ;
try
{ defs = XmlUtility . ReadXML < Defs > ( 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 ; }
2022-09-15 05:17:44 +00:00
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 ] ;
2022-09-15 05:17:44 +00:00
selectAnimationDialog . Initialize ( defs ) ;
selectAnimationDialog . Pop ( ) ;
}
public void LoadAnimation ( AnimationDef animationDef )
{
2022-09-21 21:15:25 +00:00
UpdateCustomArrays ( animationDef ) ;
RunPostLoadOperations ( animationDef ) ;
2022-09-15 05:17:44 +00:00
Debug . Log ( "Loaded AnimationDef: " + animationDef . defName ) ;
2022-09-25 06:15:27 +00:00
2022-09-15 05:17:44 +00:00
Workspace . animationDef = animationDef ;
2022-09-16 22:50:15 +00:00
animationDef . Initialize ( ) ;
2022-09-19 00:07:44 +00:00
2022-10-19 02:57:43 +00:00
AnimationController . Instance . Reset ( ) ;
Workspace . Instance . Reset ( ) ;
2022-09-21 05:40:58 +00:00
Workspace . Instance . RecordEvent ( "AnimationDef loaded" ) ;
2022-09-15 05:17:44 +00:00
}
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 )
{
if ( animationDef . animationStages . Count > 1 )
{
for ( int i = 0 ; i < animationDef . animationStages . Count ; i + + )
{
if ( i = = 0 ) continue ;
animationDef . animationStages [ i ] . playTimeTicksQuick = animationDef . animationStages [ i ] . playTimeTicks ;
}
}
else if ( animationDef . animationStages . Count = = 0 )
{ animationDef . animationStages [ 0 ] . playTimeTicksQuick = animationDef . animationStages [ 0 ] . playTimeTicks ; }
}
2022-09-21 21:15:25 +00:00
}
2022-10-02 22:39:03 +00:00
public void TryToSaveAnimation ( )
2022-09-15 05:17:44 +00:00
{
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-09-15 05:17:44 +00:00
2022-10-12 05:22:29 +00:00
else
{ TryToSaveAnimationAs ( ) ; }
}
2022-09-15 05:17:44 +00:00
2022-10-12 05:22:29 +00:00
public void TryToSaveAnimationAs ( )
{
if ( Workspace . animationDef = = null )
{ return ; }
string defName = Workspace . animationDef . defName ! = null & & Workspace . animationDef . defName ! = "" ? Workspace . animationDef . defName : "newAnimationDef" ;
string path = StandaloneFileBrowser . SaveFilePanel ( "Save AnimationDef File" , "" , defName , "xml" ) ;
2022-09-15 05:17:44 +00:00
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 ) ;
Debug . Log ( "Saving AnimationDef: " + Workspace . animationDef . defName ) ;
2022-09-16 22:50:15 +00:00
2022-09-15 05:17:44 +00:00
Defs defs = new Defs ( ) ;
2022-09-16 22:50:15 +00:00
defs . animationDefs . Add ( animationDef ) ;
2022-09-15 05:17:44 +00:00
XmlUtility . WriteXML ( defs , path ) ;
2022-10-12 05:22:29 +00:00
Workspace . animationSavePath = path ;
2022-09-15 05:17:44 +00:00
}
2022-09-21 21:15:25 +00:00
public void RunPreSaveOperations ( AnimationDef animationDef )
{
2022-09-26 04:10:41 +00:00
animationDef . ValidateData ( ) ;
foreach ( Actor actor in animationDef . actors )
{ actor . ValidateData ( ) ; }
2022-09-21 21:15:25 +00:00
foreach ( AnimationStage stage in animationDef . animationStages )
{
stage . ValidateData ( ) ;
foreach ( PawnAnimationClip clip in stage . animationClips )
{
clip . ValidateData ( ) ;
foreach ( PawnKeyframe keyframe in clip . keyframes )
{ keyframe . ValidateData ( ) ; }
}
}
}
2022-10-02 22:39:03 +00:00
public void TryToMakeNewAnimation ( )
{
if ( Workspace . animationDef = = null )
{ NewAnimation ( ) ; return ; }
newAnimationDialog . Pop ( ) ;
}
2022-09-15 05:17:44 +00:00
public void NewAnimation ( )
{
2022-09-19 05:35:34 +00:00
var path = Path . Combine ( Application . streamingAssetsPath , "AnimationDefs/newAnimationDef.xml" ) ;
2022-09-15 05:17:44 +00:00
2022-09-19 05:35:34 +00:00
Defs defs = XmlUtility . ReadXML < Defs > ( path ) ;
2022-09-15 05:17:44 +00:00
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-15 05:17:44 +00:00
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-15 05:17:44 +00:00
}
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 )
{
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 ) ) ;
SaveCustomArrays ( ) ;
}
2022-09-24 07:17:40 +00:00
public void LoadAlienRaceDefs ( )
{
2022-10-08 02:52:27 +00:00
string path ;
if ( File . Exists ( Path . Combine ( Application . persistentDataPath , "alienRaceDefs.xml" ) ) )
{ path = Path . Combine ( Application . persistentDataPath , "alienRaceDefs.xml" ) ; }
else
{ path = Path . Combine ( Application . streamingAssetsPath , "alienRaceDefs.xml" ) ; }
2022-09-24 07:17:40 +00:00
if ( File . Exists ( path ) = = false )
{ SaveAlienRaceDefs ( ) ; return ; }
AlienRaceDefs . allDefs = XmlUtility . ReadXML < List < AlienRaceDef > > ( path ) ;
AlienRaceDefs . OnLoad ( ) ;
}
public void SaveAlienRaceDefs ( )
{
2022-10-08 02:52:27 +00:00
var path = Path . Combine ( Application . persistentDataPath , "alienRaceDefs.xml" ) ;
2022-09-24 07:17:40 +00:00
XmlUtility . WriteXML ( AlienRaceDefs . allDefs , path ) ;
}
2022-09-15 05:17:44 +00:00
}
}