Skip to content

Commit

Permalink
Merge branch 'staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
MattFiler committed Jan 2, 2023
2 parents eacfc7e + 89478a6 commit 585c268
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 41 deletions.
6 changes: 3 additions & 3 deletions CathodeLib/CathodeLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<Authors>Matt Filer</Authors>
<Description>Provides support for parsing and writing common Alien: Isolation formats from the Cathode engine.</Description>
<Copyright>Matt Filer 2023</Copyright>
<Version>0.3.0</Version>
<Version>0.3.1</Version>
<OutputType>Library</OutputType>
<AssemblyVersion>0.3.0.0</AssemblyVersion>
<FileVersion>0.3.0.0</FileVersion>
<AssemblyVersion>0.3.1.0</AssemblyVersion>
<FileVersion>0.3.1.0</FileVersion>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down
100 changes: 76 additions & 24 deletions CathodeLib/Scripts/CATHODE/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//#define DO_PRETTY_COMPOSITES

using CATHODE.Scripting;
using CATHODE.Scripting.Internal;
using CathodeLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

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

private List<Composite> _composites = null;
public List<Composite> Composites { get { return _composites; } }
private List<Composite> _composites = new List<Composite>();

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

#region FILE_IO
/* Save all changes back out */
override public bool Save()
{
if (_entryPoints == null || _entryPoints.Length != 3 || _entryPoints[0] == null)
return false;
//Validate entry points and composite count
if (_composites.Count == 0) return false;
if (_entryPoints == null) _entryPoints = new ShortGuid[3];
if (_entryPoints[0].val == null && _entryPoints[1].val == null && _entryPoints[2].val == null && _composites.Count == 0) return false;

//If we have composites but the entry points are broken, correct them first!
if (GetComposite(_entryPoints[2]) == null)
{
Composite pausemenu = GetComposite("PAUSEMENU");
if (pausemenu == null)
{
Console.WriteLine("WARNING: PAUSEMENU composite does not exist! Creating blank placeholder.");
pausemenu = AddComposite("PAUSEMENU");
pausemenu.shortGUID = new ShortGuid("FE-7B-FE-B3");
}
_entryPoints[2] = pausemenu.shortGUID;
}
if (GetComposite(_entryPoints[1]) == null)
{
Composite global = GetComposite("GLOBAL");
if (global == null)
{
Console.WriteLine("WARNING: GLOBAL composite does not exist! Creating blank placeholder. This may cause issues with GLOBAL references.");
global = AddComposite("GLOBAL");
global.shortGUID = new ShortGuid("1D-2E-CE-E5");
}
_entryPoints[1] = global.shortGUID;
}
if (GetComposite(_entryPoints[0]) == null)
{
Console.WriteLine("WARNING: Entry point was not set! Defaulting to composite at index zero.");
_entryPoints[0] = _composites[0].shortGUID;
}
RefreshEntryPointObjects();

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

//Write entry points
for (int i = 0; i < 3; i++)
Utilities.Write<ShortGuid>(writer, _entryPoints[i]);
{
if (_entryPoints[i].val == null || GetComposite(_entryPoints[i]) == null)
writer.Write(new byte[] { 0x00, 0x00, 0x00, 0x00 });
else
Utilities.Write<ShortGuid>(writer, _entryPoints[i]);
}

//Write placeholder info for parameter/composite offsets
int offsetToRewrite = (int)writer.BaseStream.Position;
Expand Down Expand Up @@ -420,7 +460,7 @@ override public bool Save()
break;
case ResourceType.COLLISION_MAPPING:
writer.Write(resourceReferences[p].startIndex);
writer.Write(resourceReferences[p].entityID.val);
writer.Write(resourceReferences[p].collisionID.val);
break;
case ResourceType.ANIMATED_MODEL:
case ResourceType.DYNAMIC_PHYSICS_SYSTEM:
Expand Down Expand Up @@ -859,7 +899,7 @@ override protected bool Load()
break;
case ResourceType.COLLISION_MAPPING:
resource.startIndex = reader.ReadInt32(); //COLLISION.MAP entry index?
resource.entityID = new ShortGuid(reader); //ID which maps to the entity using the resource (?) - check GetFriendlyName
resource.collisionID = new ShortGuid(reader); //ID which maps to *something*
break;
case ResourceType.ANIMATED_MODEL:
case ResourceType.DYNAMIC_PHYSICS_SYSTEM:
Expand Down Expand Up @@ -1074,6 +1114,15 @@ override protected bool Load()
#endregion

#region ACCESSORS
/* Add a new composite */
public Composite AddComposite(string name, bool isRoot = false)
{
Composite comp = new Composite(name);
_composites.Add(comp);
if (isRoot) SetRootComposite(comp);
return comp;
}

/* Return a list of filenames for composites in the CommandsPAK archive */
public string[] GetCompositeNames()
{
Expand All @@ -1082,26 +1131,16 @@ public string[] GetCompositeNames()
return toReturn;
}

/* Find the a script entry object by name */
public int GetFileIndex(string FileName)
/* Get an individual composite */
public Composite GetComposite(string name)
{
for (int i = 0; i < _composites.Count; i++) if (_composites[i].name == FileName || _composites[i].name == FileName.Replace('/', '\\')) return i;
return -1;
return _composites.FirstOrDefault(o => o.name == name || o.name == name.Replace('/', '\\'));
}

/* Get an individual composite */
public Composite GetComposite(ShortGuid id)
{
if (id.val == null) return null;
return _composites.FirstOrDefault(o => o.shortGUID == id);
}
public Composite GetCompositeByIndex(int index)
{
return (index >= _composites.Count || index < 0) ? null : _composites[index];
}

/* Get all composites */
public List<Composite> Composites { get { return _composites; } }

/* Get entry point composite objects */
public Composite[] EntryPoints
Expand All @@ -1110,16 +1149,22 @@ public Composite[] EntryPoints
{
if (_entryPoints == null) return null;
if (_entryPointObjects != null) return _entryPointObjects;
_entryPointObjects = new Composite[_entryPoints.Length];
for (int i = 0; i < _entryPoints.Length; i++) _entryPointObjects[i] = GetComposite(_entryPoints[i]);
RefreshEntryPointObjects();
return _entryPointObjects;
}
}

/* Set the root composite for this COMMANDS.PAK (the root of the level - GLOBAL and PAUSEMENU are also instanced) */
public void SetRootComposite(ShortGuid id)
public void SetRootComposite(Composite composite)
{
SetRootComposite(composite.shortGUID);
}
public void SetRootComposite(ShortGuid compositeID)
{
_entryPoints[0] = id;
if (_entryPoints == null)
_entryPoints = new ShortGuid[3];

_entryPoints[0] = compositeID;
_entryPointObjects = null;
}
#endregion
Expand Down Expand Up @@ -1155,6 +1200,13 @@ private List<ParameterData> PruneParameterList(List<ParameterData> parameters)
}
return prunedList;
}

/* Refresh the composite pointers for our entry points */
private void RefreshEntryPointObjects()
{
_entryPointObjects = new Composite[_entryPoints.Length];
for (int i = 0; i < _entryPoints.Length; i++) _entryPointObjects[i] = GetComposite(_entryPoints[i]);
}
#endregion

/* -- */
Expand Down
17 changes: 16 additions & 1 deletion CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Composite.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CATHODE.Scripting.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR || UNITY_STANDALONE
Expand All @@ -12,6 +13,14 @@ namespace CATHODE.Scripting
[Serializable]
public class Composite
{
public Composite() { }
public Composite(string name)
{
shortGUID = ShortGuidUtils.GenerateRandom();
this.name = name;
unknownPair = new OffsetPair(5, 6); //TODO: what on earth this this?
}

public ShortGuid shortGUID; //The id when this composite is used as an entity in another composite
public string name = ""; //The string name of the composite

Expand Down Expand Up @@ -54,6 +63,12 @@ public void SortEntities()
}

/* Add a new function entity */
public FunctionEntity AddFunction(FunctionType function, bool autopopulateParameters = false)
{
FunctionEntity func = new FunctionEntity(function, autopopulateParameters);
functions.Add(func);
return func;
}
public FunctionEntity AddFunction(string function, bool autopopulateParameters = false)
{
FunctionEntity func = new FunctionEntity(function, autopopulateParameters);
Expand Down
22 changes: 20 additions & 2 deletions CathodeLib/Scripts/CATHODE/CommandsPAK/Components/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CATHODE.Assets.Utilities;
using CATHODE.Scripting;
using CATHODE.Scripting.Internal;
using CathodeLib;
using CathodeLib.Properties;
using System;
Expand All @@ -8,8 +9,9 @@
using System.Diagnostics.SymbolStore;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;

namespace CATHODE.Scripting
namespace CATHODE.Scripting.Internal
{
/* An entity in a composite */
[Serializable]
Expand Down Expand Up @@ -55,9 +57,12 @@ public Parameter GetParameter(ShortGuid id)

/* Add a data-supplying parameter to the entity */
public Parameter AddParameter(string name, ParameterData data, ParameterVariant variant = ParameterVariant.PARAMETER)
{
return AddParameter(ShortGuidUtils.Generate(name), data, variant);
}
public Parameter AddParameter(ShortGuid id, ParameterData data, ParameterVariant variant = ParameterVariant.PARAMETER)
{
//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.
ShortGuid id = ShortGuidUtils.Generate(name);
Parameter param = GetParameter(id);
if (param == null)
{
Expand Down Expand Up @@ -95,6 +100,9 @@ public void RemoveParameterLink(string parameter, Entity childEntity, string chi
childLinks.RemoveAll(o => o.parentParamID == parameter_id && o.childID == childEntity.shortGUID && o.childParamID == childParameter_id);
}
}
}
namespace CATHODE.Scripting
{
[Serializable]
public class VariableEntity : Entity
{
Expand Down Expand Up @@ -174,6 +182,11 @@ public FunctionEntity(ShortGuid function, bool autoGenerateParameters = false) :
this.function = function;
if (autoGenerateParameters) EntityUtils.ApplyDefaults(this);
}
public FunctionEntity(FunctionType function, bool autoGenerateParameters = false) : base(EntityVariant.FUNCTION)
{
this.function = CommandsUtils.GetFunctionTypeGUID(function);
if (autoGenerateParameters) EntityUtils.ApplyDefaults(this);
}

public FunctionEntity(ShortGuid shortGUID, ShortGuid function, bool autoGenerateParameters = false) : base(shortGUID, EntityVariant.FUNCTION)
{
Expand All @@ -185,6 +198,11 @@ public FunctionEntity(ShortGuid shortGUID, string function, bool autoGeneratePar
this.function = ShortGuidUtils.Generate(function);
if (autoGenerateParameters) EntityUtils.ApplyDefaults(this);
}
public FunctionEntity(ShortGuid shortGUID, FunctionType function, bool autoGenerateParameters = false) : base(shortGUID, EntityVariant.FUNCTION)
{
this.function = CommandsUtils.GetFunctionTypeGUID(function);
if (autoGenerateParameters) EntityUtils.ApplyDefaults(this);
}

public ShortGuid function; //Translates to string via ShortGuidUtils.FindString
public List<ResourceReference> resources = new List<ResourceReference>(); //TODO: can we replace this with a cResource to save duplicating functionality?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CATHODE.Scripting.Internal;
using System;
using System.Collections.Generic;
using System.Text;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using CathodeLib;
using CathodeLib.Properties;
using CATHODE.Scripting.Internal;
using CathodeLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace CATHODE.Scripting
namespace CATHODE.Scripting.Internal
{
/* Data which can be used within a parameter */
[Serializable]
Expand Down Expand Up @@ -136,6 +135,10 @@ public object Clone()
}
}
}
}

namespace CATHODE.Scripting
{
[Serializable]
public class cTransform : ParameterData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ResourceReference(ResourceType type)
if (x.entryType != y.entryType) return false;
if (x.startIndex != y.startIndex) return false;
if (x.count != y.count) return false;
if (x.entityID != y.entityID) return false;
if (x.collisionID != y.collisionID) return false;

return true;
}
Expand All @@ -59,7 +59,7 @@ public override bool Equals(object obj)
entryType == reference.entryType &&
startIndex == reference.startIndex &&
count == reference.count &&
EqualityComparer<ShortGuid>.Default.Equals(entityID, reference.entityID);
EqualityComparer<ShortGuid>.Default.Equals(collisionID, reference.collisionID);
}

public override int GetHashCode()
Expand All @@ -71,7 +71,7 @@ public override int GetHashCode()
hashCode = hashCode * -1521134295 + entryType.GetHashCode();
hashCode = hashCode * -1521134295 + startIndex.GetHashCode();
hashCode = hashCode * -1521134295 + count.GetHashCode();
hashCode = hashCode * -1521134295 + entityID.GetHashCode();
hashCode = hashCode * -1521134295 + collisionID.GetHashCode();
return hashCode;
}

Expand All @@ -84,6 +84,6 @@ public override int GetHashCode()
public int startIndex = -1;
public int count = 1;

public ShortGuid entityID = new ShortGuid("FF-FF-FF-FF");
public ShortGuid collisionID = new ShortGuid("FF-FF-FF-FF");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public static string FindString(ShortGuid guid)
/* Generate a random unique ShortGuid */
public static ShortGuid GenerateRandom()
{
//TODO: we should really check the caches here to make sure it IS random, and then go again if not
return Generate(DateTime.Now.ToString("G") + (new Random()).Next(0, 9999));
string guid = DateTime.Now.ToString("G") + (new Random()).Next(0, 9999);
while (vanilla.cache.ContainsKey(guid) || custom.cache.ContainsKey(guid))
guid += "_";
return Generate(guid);
}

/* Cache a pre-generated ShortGuid */
Expand Down

0 comments on commit 585c268

Please sign in to comment.