Skip to content

Commit

Permalink
Actors: Bunch of fixes to try and resolve issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Greavesy1899 committed Oct 11, 2023
1 parent b4f1980 commit 993f9f6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
63 changes: 36 additions & 27 deletions Mafia2Libs/ResourceTypes/FileTypes/Actors/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,46 @@ public Actor(FileInfo InFileInfo)

private string BuildDefinitions()
{
// Only fill buffer if we have definitions.
// If we have no definitions we leave the pool empty.
string bufferPool = String.Empty;
if (definitions.Count > 0)
if(Definitions.Count == 0)
{
bufferPool += "<scene>\0";
Dictionary<string, int> names = new Dictionary<string, int>();
for (int i = 0; i < definitions.Count; i++)
// TODO: Check if this is correct?
return string.Empty;
}

// First we generate the Dictionary to store definition names
Dictionary<string, int> NameToOffsetLookup = new Dictionary<string, int>();
foreach (ActorDefinition Definition in Definitions)
{
string Name = Definition.Name;
if (NameToOffsetLookup.ContainsKey(Name) == false)
{
NameToOffsetLookup.Add(Name, -1);
}
}

// Next we iterate through all of the actors and fill in the buffer pool
// It seems like the official tools also did this, so we will do the same.
// We want to replicate their pipeline as much as possible.
string OutBufferPool = "<scene>\0";
foreach (ActorEntry CurrentEntry in items)
{
string NameToSave = CurrentEntry.DefinitionName;
if (NameToOffsetLookup.ContainsKey(NameToSave) && NameToOffsetLookup[NameToSave] == -1)
{
int startPos = 0;

if (!string.IsNullOrEmpty(definitions[i].Name))
{
if (!names.ContainsKey(definitions[i].Name))
{
startPos = bufferPool.Length;
bufferPool += definitions[i].Name;
bufferPool += '\0';
names.Add(definitions[i].Name, startPos);
definitions[i].NamePos = (ushort)startPos;
}
else
{
names.TryGetValue(definitions[i].Name, out startPos);
definitions[i].NamePos = (ushort)startPos;
}
}
int StartOffset = OutBufferPool.Length;
OutBufferPool += NameToSave;
OutBufferPool += '\0';
NameToOffsetLookup[CurrentEntry.DefinitionName] = StartOffset;
}
}

return bufferPool;

// Now we iterate through the definition list again, and update offsets
foreach (ActorDefinition Definition in Definitions)
{
Definition.NamePos = (ushort)NameToOffsetLookup[Definition.Name];
}

return OutBufferPool;
}

private void Sanitize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ public override void ReadFromFile(MemoryStream reader, bool isBigEndian)

public override void WriteToFile(BinaryWriter writer)
{
// NB: We do not want to save the local transform of an actor into the frame resource.
// This is because the original Mafia II pipeline removed the transform from linked actors,
// so we will do the same to avoid any chance of discrepencies which may appear.
Matrix4x4 OldTransform = LocalTransform;
LocalTransform = Matrix4x4.Identity;

base.WriteToFile(writer);
actorHash.WriteToFile(writer);
}

protected override void SanitizeOnSave()
{
base.SanitizeOnSave();

if(ActorHash.Hash != 0)
{
LocalTransform = Matrix4x4.Identity;
}
// Now revert back to the original transform
LocalTransform = OldTransform;
}

public override string ToString()
Expand Down

0 comments on commit 993f9f6

Please sign in to comment.