1- using CathodeLib ;
1+ using CATHODE . Scripting ;
2+ using CathodeLib ;
3+ using System ;
24using System . Collections . Generic ;
35using System . IO ;
46using System . Runtime . InteropServices ;
@@ -8,48 +10,38 @@ namespace CATHODE
810 /* DATA/ENV/PRODUCTION/x/WORLD/MATERIAL_MAPPINGS.PAK */
911 public class MaterialMappings : CathodeFile
1012 {
11- public List < Mapping > Entries = new List < Mapping > ( ) ;
13+ public List < Entry > Entries = new List < Entry > ( ) ;
1214 public static new Implementation Implementation = Implementation . LOAD | Implementation . SAVE ;
1315 public MaterialMappings ( string path ) : base ( path ) { }
14-
15- private byte [ ] _headerJunk = new byte [ 8 ] ;
16+
17+ //This is always the start of the mapping filepath - remove it for ease when adding new ones
18+ private const string _path = "n:/content/build/library/_material_libraries_/mappings/" ;
1619
1720 #region FILE_IO
1821 override protected bool LoadInternal ( )
1922 {
2023 using ( BinaryReader reader = new BinaryReader ( File . OpenRead ( _filepath ) ) )
2124 {
22- //Parse header
23- _headerJunk = reader . ReadBytes ( 8 ) ; //TODO: Work out what this contains
25+ reader . BaseStream . Position += 8 ; //magic, version
2426 int entryCount = reader . ReadInt32 ( ) ;
2527
26- //Parse entries (XML is broken in the build files - doesn't get shipped)
2728 for ( int x = 0 ; x < entryCount ; x ++ )
2829 {
29- //This entry
30- Mapping entry = new Mapping ( ) ;
31- entry . MapHeader = reader . ReadBytes ( 4 ) ; //TODO: Work out the significance of this value, to be able to construct new PAKs from scratch.
32- entry . MapEntryCoupleCount = reader . ReadInt32 ( ) ;
33- entry . MapJunk = reader . ReadBytes ( 4 ) ; //TODO: Work out if this is always null.
34- for ( int p = 0 ; p < ( entry . MapEntryCoupleCount * 2 ) + 1 ; p ++ )
30+ Entry entry = new Entry ( ) ;
31+ reader . BaseStream . Position += 4 ; //shortguid hash of filename (useful?)
32+ int count = reader . ReadInt32 ( ) ;
33+ reader . BaseStream . Position += 4 ; //this is to->from id count, stored last, but always empty
34+ int strLength = reader . ReadInt32 ( ) ;
35+ entry . Name = Utilities . ReadString ( reader . ReadBytes ( strLength ) ) ;
36+ entry . Name = entry . Name . Substring ( _path . Length , entry . Name . Length - 4 - _path . Length ) ;
37+ for ( int p = 0 ; p < count ; p ++ )
3538 {
36- //String
37- int length = reader . ReadInt32 ( ) ;
38- string materialString = "" ;
39- for ( int i = 0 ; i < length ; i ++ )
40- {
41- materialString += reader . ReadChar ( ) ;
42- }
43-
44- //First string is filename, others are materials
45- if ( p == 0 )
46- {
47- entry . MapFilename = materialString ;
48- }
49- else
50- {
51- entry . MapMatEntries . Add ( materialString ) ;
52- }
39+ Entry . Mapping mapping = new Entry . Mapping ( ) ;
40+ strLength = reader . ReadInt32 ( ) ;
41+ mapping . from = Utilities . ReadString ( reader . ReadBytes ( strLength ) ) ;
42+ strLength = reader . ReadInt32 ( ) ;
43+ mapping . to = Utilities . ReadString ( reader . ReadBytes ( strLength ) ) ;
44+ entry . Mappings . Add ( mapping ) ;
5345 }
5446 Entries . Add ( entry ) ;
5547 }
@@ -62,19 +54,23 @@ override protected bool SaveInternal()
6254 using ( BinaryWriter writer = new BinaryWriter ( File . OpenWrite ( _filepath ) ) )
6355 {
6456 writer . BaseStream . SetLength ( 0 ) ;
65- writer . Write ( _headerJunk ) ;
57+ writer . Write ( new byte [ 4 ] { 0xAE , 0xB0 , 0xEB , 0xDE } ) ;
58+ writer . Write ( 4 ) ;
6659 writer . Write ( Entries . Count ) ;
67- foreach ( Mapping entry in Entries )
60+ foreach ( Entry entry in Entries )
6861 {
69- writer . Write ( entry . MapHeader ) ;
70- writer . Write ( entry . MapEntryCoupleCount ) ;
71- writer . Write ( entry . MapJunk ) ;
72- writer . Write ( entry . MapFilename . Length ) ;
73- Utilities . WriteString ( entry . MapFilename , writer ) ;
74- foreach ( string name in entry . MapMatEntries )
62+ string fullPath = _path + entry . Name + ".xml" ;
63+ Utilities . Write ( writer , ShortGuidUtils . Generate ( fullPath , false ) ) ;
64+ writer . Write ( entry . Mappings . Count ) ;
65+ writer . Write ( 0 ) ;
66+ writer . Write ( fullPath . Length ) ;
67+ Utilities . WriteString ( fullPath , writer ) ;
68+ foreach ( Entry . Mapping mapping in entry . Mappings )
7569 {
76- writer . Write ( name . Length ) ;
77- Utilities . WriteString ( name , writer ) ;
70+ writer . Write ( mapping . from . Length ) ;
71+ Utilities . WriteString ( mapping . from , writer ) ;
72+ writer . Write ( mapping . to . Length ) ;
73+ Utilities . WriteString ( mapping . to , writer ) ;
7874 }
7975 }
8076 }
@@ -83,13 +79,26 @@ override protected bool SaveInternal()
8379 #endregion
8480
8581 #region STRUCTURES
86- public class Mapping
82+ public class Entry
8783 {
88- public byte [ ] MapHeader = new byte [ 4 ] ;
89- public byte [ ] MapJunk = new byte [ 4 ] ; //I think this is always null
90- public string MapFilename = "" ;
91- public int MapEntryCoupleCount = 0 ; //materials will be 2* this number
92- public List < string > MapMatEntries = new List < string > ( ) ;
84+ public string Name ;
85+ public List < Mapping > Mappings = new List < Mapping > ( ) ;
86+
87+ public class Mapping
88+ {
89+ public string from ;
90+ public string to ;
91+
92+ public override string ToString ( )
93+ {
94+ return from + "->" + to ;
95+ }
96+ }
97+
98+ public override string ToString ( )
99+ {
100+ return Name + " [" + Mappings . Count + "]" ;
101+ }
93102 }
94103 #endregion
95104 }
0 commit comments