99using System . Runtime . InteropServices . ComTypes ;
1010using System . Threading . Tasks ;
1111using CATHODE . Scripting . Internal . Parsers ;
12+ using System . Runtime . CompilerServices ;
13+
14+
1215
1316
1417#if UNITY_EDITOR || UNITY_STANDALONE_WIN
@@ -32,6 +35,9 @@ public class Commands : CathodeFile
3235 private CollisionMaps _colMaps ;
3336 private RenderableElements _reds ;
3437
38+ public bool Compressed { get { return _compressed ; } set { _compressed = value ; } }
39+ private bool _compressed = false ;
40+
3541 public Commands ( string path , EnvironmentAnimations envAnims , CollisionMaps colMaps , RenderableElements reds ) : base ( path )
3642 {
3743 _envAnims = envAnims ;
@@ -41,6 +47,19 @@ public Commands(string path, EnvironmentAnimations envAnims, CollisionMaps colMa
4147 Utils = new CommandsUtils ( this ) ;
4248
4349 _loaded = Load ( ) ;
50+
51+ //Overwrite loaded data with the data from BIN
52+ switch ( Path . GetExtension ( _filepath ) . ToUpper ( ) )
53+ {
54+ case ".BIN" :
55+ case ".GZ" :
56+ foreach ( var composites in CommandsBIN . EntityNames )
57+ foreach ( var entities in composites . Value )
58+ Utils . SetEntityName ( composites . Key , entities . Key , entities . Value ) ;
59+ foreach ( var others in CommandsBIN . ParameterNames )
60+ ShortGuidUtils . Generate ( others . Value ) ; // do we even need to do this?
61+ break ;
62+ }
4463 }
4564
4665 public void ClearReferences ( )
@@ -63,12 +82,6 @@ public void ClearReferences()
6382 private ShortGuid [ ] _entryPoints = null ;
6483 private Composite [ ] _entryPointObjects = null ;
6584
66- /// <summary>
67- /// Set this value to true before initialising your Commands object to load with non-capitalised composite names.
68- /// Changing this value AFTER loading will not make any difference to the loaded Commands object, only future ones.
69- /// </summary>
70- public static bool UsePrettyPaths = false ;
71-
7285 /// <summary>
7386 /// Use this to access various utilities for dealing with Commands data.
7487 /// Additional metadata generated by these utils will write to your Commands file when saved.
@@ -78,29 +91,21 @@ public void ClearReferences()
7891 #region FILE_IO
7992 override protected bool LoadInternal ( MemoryStream stream )
8093 {
81- byte [ ] content = stream . ToArray ( ) ;
94+ _compressed = _filepath != null && _filepath != "" && Path . GetExtension ( _filepath ) . ToLower ( ) == ".gz" ;
95+
8296 switch ( Path . GetExtension ( _filepath ) . ToUpper ( ) )
8397 {
8498 case ".PAK" :
8599 CommandsPAK . Read ( stream , out _entryPoints , out Entries , _envAnims , _colMaps , _reds ) ;
86100 break ;
101+ case ".GZ" :
87102 case ".BIN" :
88- CommandsBIN . Read ( stream , out _entryPoints , out Entries , _envAnims , _colMaps , _reds ) ;
103+ CommandsBIN . Read ( _compressed ? Utilities . GZIPDecompress ( stream ) : stream , out _entryPoints , out Entries , _envAnims , _colMaps , _reds ) ;
89104 break ;
90105 default :
91106 return false ;
92107 }
93108
94- if ( UsePrettyPaths )
95- {
96- foreach ( Composite composite in Entries )
97- {
98- string prettyPath = CustomTable . Vanilla . CompositePaths . GetPrettyPath ( composite . shortGUID ) ;
99- if ( prettyPath != "" ) composite . name = prettyPath ;
100- composite . name = composite . name . Replace ( "/" , "\\ " ) ;
101- }
102- }
103-
104109 return true ;
105110 }
106111
@@ -110,6 +115,11 @@ override protected bool SaveInternal()
110115 if ( Entries . Count == 0 ) return false ;
111116 if ( _entryPoints == null ) _entryPoints = new ShortGuid [ 3 ] ;
112117
118+ if ( _compressed && Path . GetExtension ( _filepath ) . ToLower ( ) != ".gz" )
119+ _filepath += ".gz" ;
120+ else if ( ! _compressed && Path . GetExtension ( _filepath ) . ToLower ( ) == ".gz" )
121+ _filepath = _filepath . Substring ( 0 , _filepath . Length - 3 ) ;
122+
113123 #region FIX_POTENTIAL_ERRORS
114124 //If we have composites but the entry points are broken, correct them first!
115125 if ( GetComposite ( _entryPoints [ 2 ] ) == null )
@@ -228,17 +238,24 @@ override protected bool SaveInternal()
228238 case ".PAK" :
229239 CommandsPAK . Write ( _entryPoints , Entries , out content , _envAnims , _colMaps , _reds ) ;
230240 break ;
241+ case ".GZ" :
231242 case ".BIN" :
232243 CommandsBIN . Write ( _entryPoints , Entries , out content , _envAnims , _colMaps , _reds ) ;
233244 break ;
234245 default :
235246 return false ;
236247 }
237- using ( BinaryWriter writer = new BinaryWriter ( File . OpenWrite ( _filepath ) ) )
248+
249+ using ( Stream stream = File . OpenWrite ( _filepath ) )
250+ using ( BinaryWriter writer = new BinaryWriter ( stream ) )
238251 {
239252 writer . BaseStream . SetLength ( 0 ) ;
240253 writer . Write ( content ) ;
241254 }
255+
256+ if ( _compressed )
257+ Utilities . GZIPCompress ( _filepath ) ;
258+
242259 return true ;
243260 }
244261 #endregion
0 commit comments