Skip to content

Commit 585c268

Browse files
committed
Merge branch 'staging'
2 parents eacfc7e + 89478a6 commit 585c268

File tree

8 files changed

+132
-41
lines changed

8 files changed

+132
-41
lines changed

CathodeLib/CathodeLib.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
<Authors>Matt Filer</Authors>
1111
<Description>Provides support for parsing and writing common Alien: Isolation formats from the Cathode engine.</Description>
1212
<Copyright>Matt Filer 2023</Copyright>
13-
<Version>0.3.0</Version>
13+
<Version>0.3.1</Version>
1414
<OutputType>Library</OutputType>
15-
<AssemblyVersion>0.3.0.0</AssemblyVersion>
16-
<FileVersion>0.3.0.0</FileVersion>
15+
<AssemblyVersion>0.3.1.0</AssemblyVersion>
16+
<FileVersion>0.3.1.0</FileVersion>
1717
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
1818
</PropertyGroup>
1919

CathodeLib/Scripts/CATHODE/Commands.cs

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//#define DO_PRETTY_COMPOSITES
22

33
using CATHODE.Scripting;
4+
using CATHODE.Scripting.Internal;
45
using CathodeLib;
56
using System;
67
using System.Collections.Generic;
78
using System.IO;
89
using System.Linq;
910
using System.Runtime.InteropServices;
11+
using System.Runtime.InteropServices.ComTypes;
1012

1113
namespace CATHODE
1214
{
@@ -19,26 +21,64 @@ public class Commands : CathodeFile
1921
// - Root Instance (the map's entry composite, usually containing entities that call mission/environment composites)
2022
// - Global Instance (the main data handler for keeping track of mission number, etc - kinda like a big singleton)
2123
// - Pause Menu Instance
22-
private ShortGuid[] _entryPoints;
24+
private ShortGuid[] _entryPoints = null;
2325
private Composite[] _entryPointObjects = null;
2426

25-
private List<Composite> _composites = null;
27+
public List<Composite> Composites { get { return _composites; } }
28+
private List<Composite> _composites = new List<Composite>();
2629

2730
public Commands(string path) : base(path) { }
2831

2932
#region FILE_IO
3033
/* Save all changes back out */
3134
override public bool Save()
3235
{
33-
if (_entryPoints == null || _entryPoints.Length != 3 || _entryPoints[0] == null)
34-
return false;
36+
//Validate entry points and composite count
37+
if (_composites.Count == 0) return false;
38+
if (_entryPoints == null) _entryPoints = new ShortGuid[3];
39+
if (_entryPoints[0].val == null && _entryPoints[1].val == null && _entryPoints[2].val == null && _composites.Count == 0) return false;
40+
41+
//If we have composites but the entry points are broken, correct them first!
42+
if (GetComposite(_entryPoints[2]) == null)
43+
{
44+
Composite pausemenu = GetComposite("PAUSEMENU");
45+
if (pausemenu == null)
46+
{
47+
Console.WriteLine("WARNING: PAUSEMENU composite does not exist! Creating blank placeholder.");
48+
pausemenu = AddComposite("PAUSEMENU");
49+
pausemenu.shortGUID = new ShortGuid("FE-7B-FE-B3");
50+
}
51+
_entryPoints[2] = pausemenu.shortGUID;
52+
}
53+
if (GetComposite(_entryPoints[1]) == null)
54+
{
55+
Composite global = GetComposite("GLOBAL");
56+
if (global == null)
57+
{
58+
Console.WriteLine("WARNING: GLOBAL composite does not exist! Creating blank placeholder. This may cause issues with GLOBAL references.");
59+
global = AddComposite("GLOBAL");
60+
global.shortGUID = new ShortGuid("1D-2E-CE-E5");
61+
}
62+
_entryPoints[1] = global.shortGUID;
63+
}
64+
if (GetComposite(_entryPoints[0]) == null)
65+
{
66+
Console.WriteLine("WARNING: Entry point was not set! Defaulting to composite at index zero.");
67+
_entryPoints[0] = _composites[0].shortGUID;
68+
}
69+
RefreshEntryPointObjects();
3570

3671
BinaryWriter writer = new BinaryWriter(File.OpenWrite(_filepath));
3772
writer.BaseStream.SetLength(0);
3873

3974
//Write entry points
4075
for (int i = 0; i < 3; i++)
41-
Utilities.Write<ShortGuid>(writer, _entryPoints[i]);
76+
{
77+
if (_entryPoints[i].val == null || GetComposite(_entryPoints[i]) == null)
78+
writer.Write(new byte[] { 0x00, 0x00, 0x00, 0x00 });
79+
else
80+
Utilities.Write<ShortGuid>(writer, _entryPoints[i]);
81+
}
4282

4383
//Write placeholder info for parameter/composite offsets
4484
int offsetToRewrite = (int)writer.BaseStream.Position;
@@ -420,7 +460,7 @@ override public bool Save()
420460
break;
421461
case ResourceType.COLLISION_MAPPING:
422462
writer.Write(resourceReferences[p].startIndex);
423-
writer.Write(resourceReferences[p].entityID.val);
463+
writer.Write(resourceReferences[p].collisionID.val);
424464
break;
425465
case ResourceType.ANIMATED_MODEL:
426466
case ResourceType.DYNAMIC_PHYSICS_SYSTEM:
@@ -859,7 +899,7 @@ override protected bool Load()
859899
break;
860900
case ResourceType.COLLISION_MAPPING:
861901
resource.startIndex = reader.ReadInt32(); //COLLISION.MAP entry index?
862-
resource.entityID = new ShortGuid(reader); //ID which maps to the entity using the resource (?) - check GetFriendlyName
902+
resource.collisionID = new ShortGuid(reader); //ID which maps to *something*
863903
break;
864904
case ResourceType.ANIMATED_MODEL:
865905
case ResourceType.DYNAMIC_PHYSICS_SYSTEM:
@@ -1074,6 +1114,15 @@ override protected bool Load()
10741114
#endregion
10751115

10761116
#region ACCESSORS
1117+
/* Add a new composite */
1118+
public Composite AddComposite(string name, bool isRoot = false)
1119+
{
1120+
Composite comp = new Composite(name);
1121+
_composites.Add(comp);
1122+
if (isRoot) SetRootComposite(comp);
1123+
return comp;
1124+
}
1125+
10771126
/* Return a list of filenames for composites in the CommandsPAK archive */
10781127
public string[] GetCompositeNames()
10791128
{
@@ -1082,26 +1131,16 @@ public string[] GetCompositeNames()
10821131
return toReturn;
10831132
}
10841133

1085-
/* Find the a script entry object by name */
1086-
public int GetFileIndex(string FileName)
1134+
/* Get an individual composite */
1135+
public Composite GetComposite(string name)
10871136
{
1088-
for (int i = 0; i < _composites.Count; i++) if (_composites[i].name == FileName || _composites[i].name == FileName.Replace('/', '\\')) return i;
1089-
return -1;
1137+
return _composites.FirstOrDefault(o => o.name == name || o.name == name.Replace('/', '\\'));
10901138
}
1091-
1092-
/* Get an individual composite */
10931139
public Composite GetComposite(ShortGuid id)
10941140
{
10951141
if (id.val == null) return null;
10961142
return _composites.FirstOrDefault(o => o.shortGUID == id);
10971143
}
1098-
public Composite GetCompositeByIndex(int index)
1099-
{
1100-
return (index >= _composites.Count || index < 0) ? null : _composites[index];
1101-
}
1102-
1103-
/* Get all composites */
1104-
public List<Composite> Composites { get { return _composites; } }
11051144

11061145
/* Get entry point composite objects */
11071146
public Composite[] EntryPoints
@@ -1110,16 +1149,22 @@ public Composite[] EntryPoints
11101149
{
11111150
if (_entryPoints == null) return null;
11121151
if (_entryPointObjects != null) return _entryPointObjects;
1113-
_entryPointObjects = new Composite[_entryPoints.Length];
1114-
for (int i = 0; i < _entryPoints.Length; i++) _entryPointObjects[i] = GetComposite(_entryPoints[i]);
1152+
RefreshEntryPointObjects();
11151153
return _entryPointObjects;
11161154
}
11171155
}
11181156

11191157
/* Set the root composite for this COMMANDS.PAK (the root of the level - GLOBAL and PAUSEMENU are also instanced) */
1120-
public void SetRootComposite(ShortGuid id)
1158+
public void SetRootComposite(Composite composite)
1159+
{
1160+
SetRootComposite(composite.shortGUID);
1161+
}
1162+
public void SetRootComposite(ShortGuid compositeID)
11211163
{
1122-
_entryPoints[0] = id;
1164+
if (_entryPoints == null)
1165+
_entryPoints = new ShortGuid[3];
1166+
1167+
_entryPoints[0] = compositeID;
11231168
_entryPointObjects = null;
11241169
}
11251170
#endregion
@@ -1155,6 +1200,13 @@ private List<ParameterData> PruneParameterList(List<ParameterData> parameters)
11551200
}
11561201
return prunedList;
11571202
}
1203+
1204+
/* Refresh the composite pointers for our entry points */
1205+
private void RefreshEntryPointObjects()
1206+
{
1207+
_entryPointObjects = new Composite[_entryPoints.Length];
1208+
for (int i = 0; i < _entryPoints.Length; i++) _entryPointObjects[i] = GetComposite(_entryPoints[i]);
1209+
}
11581210
#endregion
11591211

11601212
/* -- */

CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using CATHODE.Scripting.Internal;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
#if UNITY_EDITOR || UNITY_STANDALONE
@@ -12,6 +13,14 @@ namespace CATHODE.Scripting
1213
[Serializable]
1314
public class Composite
1415
{
16+
public Composite() { }
17+
public Composite(string name)
18+
{
19+
shortGUID = ShortGuidUtils.GenerateRandom();
20+
this.name = name;
21+
unknownPair = new OffsetPair(5, 6); //TODO: what on earth this this?
22+
}
23+
1524
public ShortGuid shortGUID; //The id when this composite is used as an entity in another composite
1625
public string name = ""; //The string name of the composite
1726

@@ -54,6 +63,12 @@ public void SortEntities()
5463
}
5564

5665
/* Add a new function entity */
66+
public FunctionEntity AddFunction(FunctionType function, bool autopopulateParameters = false)
67+
{
68+
FunctionEntity func = new FunctionEntity(function, autopopulateParameters);
69+
functions.Add(func);
70+
return func;
71+
}
5772
public FunctionEntity AddFunction(string function, bool autopopulateParameters = false)
5873
{
5974
FunctionEntity func = new FunctionEntity(function, autopopulateParameters);

CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CATHODE.Assets.Utilities;
22
using CATHODE.Scripting;
3+
using CATHODE.Scripting.Internal;
34
using CathodeLib;
45
using CathodeLib.Properties;
56
using System;
@@ -8,8 +9,9 @@
89
using System.Diagnostics.SymbolStore;
910
using System.Linq;
1011
using System.Runtime.InteropServices;
12+
using System.Xml.Linq;
1113

12-
namespace CATHODE.Scripting
14+
namespace CATHODE.Scripting.Internal
1315
{
1416
/* An entity in a composite */
1517
[Serializable]
@@ -55,9 +57,12 @@ public Parameter GetParameter(ShortGuid id)
5557

5658
/* Add a data-supplying parameter to the entity */
5759
public Parameter AddParameter(string name, ParameterData data, ParameterVariant variant = ParameterVariant.PARAMETER)
60+
{
61+
return AddParameter(ShortGuidUtils.Generate(name), data, variant);
62+
}
63+
public Parameter AddParameter(ShortGuid id, ParameterData data, ParameterVariant variant = ParameterVariant.PARAMETER)
5864
{
5965
//TODO: we are limiting data-supplying params to ONE per entity here - is this correct? I think links are the only place where you can have multiple of the same.
60-
ShortGuid id = ShortGuidUtils.Generate(name);
6166
Parameter param = GetParameter(id);
6267
if (param == null)
6368
{
@@ -95,6 +100,9 @@ public void RemoveParameterLink(string parameter, Entity childEntity, string chi
95100
childLinks.RemoveAll(o => o.parentParamID == parameter_id && o.childID == childEntity.shortGUID && o.childParamID == childParameter_id);
96101
}
97102
}
103+
}
104+
namespace CATHODE.Scripting
105+
{
98106
[Serializable]
99107
public class VariableEntity : Entity
100108
{
@@ -174,6 +182,11 @@ public FunctionEntity(ShortGuid function, bool autoGenerateParameters = false) :
174182
this.function = function;
175183
if (autoGenerateParameters) EntityUtils.ApplyDefaults(this);
176184
}
185+
public FunctionEntity(FunctionType function, bool autoGenerateParameters = false) : base(EntityVariant.FUNCTION)
186+
{
187+
this.function = CommandsUtils.GetFunctionTypeGUID(function);
188+
if (autoGenerateParameters) EntityUtils.ApplyDefaults(this);
189+
}
177190

178191
public FunctionEntity(ShortGuid shortGUID, ShortGuid function, bool autoGenerateParameters = false) : base(shortGUID, EntityVariant.FUNCTION)
179192
{
@@ -185,6 +198,11 @@ public FunctionEntity(ShortGuid shortGUID, string function, bool autoGeneratePar
185198
this.function = ShortGuidUtils.Generate(function);
186199
if (autoGenerateParameters) EntityUtils.ApplyDefaults(this);
187200
}
201+
public FunctionEntity(ShortGuid shortGUID, FunctionType function, bool autoGenerateParameters = false) : base(shortGUID, EntityVariant.FUNCTION)
202+
{
203+
this.function = CommandsUtils.GetFunctionTypeGUID(function);
204+
if (autoGenerateParameters) EntityUtils.ApplyDefaults(this);
205+
}
188206

189207
public ShortGuid function; //Translates to string via ShortGuidUtils.FindString
190208
public List<ResourceReference> resources = new List<ResourceReference>(); //TODO: can we replace this with a cResource to save duplicating functionality?

CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Parameter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using CATHODE.Scripting.Internal;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
45

CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ParameterData.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using CathodeLib;
2-
using CathodeLib.Properties;
1+
using CATHODE.Scripting.Internal;
2+
using CathodeLib;
33
using System;
44
using System.Collections.Generic;
5-
using System.IO;
65
using System.Linq;
76

8-
namespace CATHODE.Scripting
7+
namespace CATHODE.Scripting.Internal
98
{
109
/* Data which can be used within a parameter */
1110
[Serializable]
@@ -136,6 +135,10 @@ public object Clone()
136135
}
137136
}
138137
}
138+
}
139+
140+
namespace CATHODE.Scripting
141+
{
139142
[Serializable]
140143
public class cTransform : ParameterData
141144
{

CathodeLib/Scripts/CATHODE/CommandsPAK/Components/ResourceReference.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ResourceReference(ResourceType type)
3636
if (x.entryType != y.entryType) return false;
3737
if (x.startIndex != y.startIndex) return false;
3838
if (x.count != y.count) return false;
39-
if (x.entityID != y.entityID) return false;
39+
if (x.collisionID != y.collisionID) return false;
4040

4141
return true;
4242
}
@@ -59,7 +59,7 @@ public override bool Equals(object obj)
5959
entryType == reference.entryType &&
6060
startIndex == reference.startIndex &&
6161
count == reference.count &&
62-
EqualityComparer<ShortGuid>.Default.Equals(entityID, reference.entityID);
62+
EqualityComparer<ShortGuid>.Default.Equals(collisionID, reference.collisionID);
6363
}
6464

6565
public override int GetHashCode()
@@ -71,7 +71,7 @@ public override int GetHashCode()
7171
hashCode = hashCode * -1521134295 + entryType.GetHashCode();
7272
hashCode = hashCode * -1521134295 + startIndex.GetHashCode();
7373
hashCode = hashCode * -1521134295 + count.GetHashCode();
74-
hashCode = hashCode * -1521134295 + entityID.GetHashCode();
74+
hashCode = hashCode * -1521134295 + collisionID.GetHashCode();
7575
return hashCode;
7676
}
7777

@@ -84,6 +84,6 @@ public override int GetHashCode()
8484
public int startIndex = -1;
8585
public int count = 1;
8686

87-
public ShortGuid entityID = new ShortGuid("FF-FF-FF-FF");
87+
public ShortGuid collisionID = new ShortGuid("FF-FF-FF-FF");
8888
}
8989
}

CathodeLib/Scripts/CATHODE/CommandsPAK/Helpers/ShortGuidUtils.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ public static string FindString(ShortGuid guid)
6464
/* Generate a random unique ShortGuid */
6565
public static ShortGuid GenerateRandom()
6666
{
67-
//TODO: we should really check the caches here to make sure it IS random, and then go again if not
68-
return Generate(DateTime.Now.ToString("G") + (new Random()).Next(0, 9999));
67+
string guid = DateTime.Now.ToString("G") + (new Random()).Next(0, 9999);
68+
while (vanilla.cache.ContainsKey(guid) || custom.cache.ContainsKey(guid))
69+
guid += "_";
70+
return Generate(guid);
6971
}
7072

7173
/* Cache a pre-generated ShortGuid */

0 commit comments

Comments
 (0)