Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Second attempt at CryoPod Creature addition #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions ArkSavegameToolkitNet.Domain/ArkTamedCreature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public ArkTamedCreature(IGameObject creature, IGameObject status, ISaveState sav

_saveState = saveState;

IsCryo = creature.IsCryo;
OwningPlayerId = creature.GetPropertyValue<int?>(_owningPlayerID);
OwningPlayerName = creature.GetPropertyValue<string>(_owningPlayerName);
Name = creature.GetPropertyValue<string>(_tamedName);
Expand Down Expand Up @@ -300,6 +301,7 @@ private void Construct()
public double? BabyNextCuddleTime { get; set; }
public DateTime? BabyNextCuddleTimeApprox => _saveState?.GetApproxDateTimeOf(BabyNextCuddleTime);
public bool IsNeutered { get; set; }
public bool IsCryo { get; set; }
public ArkTamedCreatureAncestor[] DinoAncestors { get; set; }
public ArkTamedCreatureAncestor[] DinoAncestorsMale { get; set; }
public sbyte[] GestationEggColors { get; set; }
Expand Down
84 changes: 83 additions & 1 deletion ArkSavegameToolkitNet/ArkSaveGame.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ArkSavegameToolkitNet.Types;
using ArkSavegameToolkitNet.Property;
using ArkSavegameToolkitNet.Structs;
using ArkSavegameToolkitNet.Types;
using log4net;
using Newtonsoft.Json;
using System;
Expand All @@ -17,6 +19,8 @@ namespace ArkSavegameToolkitNet
public class ArkSavegame : IGameObjectContainer, IDisposable
{
private static ILog _logger = LogManager.GetLogger(typeof(ArkSavegame));
private static ArkName _customItemData = ArkName.Create("CustomItemDatas");
private static ArkName _myCharacterStatusComponent = ArkName.Create("MyCharacterStatusComponent");

[JsonProperty]
public IList<GameObject> Objects
Expand Down Expand Up @@ -126,6 +130,84 @@ public bool LoadEverything()

readBinary(_archive, _mmf);



//Now parse out cryo creature data
int nextObjectId = Objects.Count();
var cryoPodEntries = Objects.Where(o => o.ClassName.Name == "PrimalItem_WeaponEmptyCryopod_C" && o.GetProperty<PropertyArray>(_customItemData) != null).ToList();

IList<GameObject> cryoCreatureObjects = new List<GameObject>();

foreach(var cryoEntry in cryoPodEntries)
{

var customData = cryoEntry.GetProperty<PropertyArray>(_customItemData);
if (customData != null && customData.Value.Count > 0)
{
StructPropertyList customProperties = (StructPropertyList)customData.Value[0];
var byteProperty = customProperties.GetProperty<PropertyStruct>("CustomDataBytes");
if (byteProperty != null)
{

StructPropertyList byteArrayProperty = (StructPropertyList)byteProperty.Value;
StructPropertyList byteArrayList = (StructPropertyList)byteArrayProperty.GetProperty<PropertyArray>("ByteArrays").Value[0];
var byteList = byteArrayList.GetProperty<PropertyArray>("Bytes");

//convert from sbytes to bytes
sbyte[] sbyteValues = new sbyte[byteList.Value.Count];
for (int x = 0; x < byteList.Value.Count; x++)
{
sbyteValues[x] = (sbyte)byteList.Value[x];
}
byte[] cryoDataBytes = (byte[])(Array)sbyteValues;

//easier to save to disk and read back as memorymappedfile
string filePath = _fileName.Substring(0, _fileName.LastIndexOf(".")) + ".cryo";
File.WriteAllBytes(filePath, cryoDataBytes);
var fi = new FileInfo(filePath);
var size = fi.Length;

using (MemoryMappedFile cryoMmf = MemoryMappedFile.CreateFromFile(filePath, FileMode.Open, null, 0L, MemoryMappedFileAccess.Read))
{
using(MemoryMappedViewAccessor cryoVa = cryoMmf.CreateViewAccessor(0L, 0L, MemoryMappedFileAccess.Read))
{
ArkArchive cryoArchive = new ArkArchive(cryoVa, size, _arkNameCache, _arkStringCache, _exclusivePropertyNameTree);
cryoArchive.GetBytes(4);

nextObjectId++;
var dino = new GameObject(cryoArchive);
dino.ObjectId = nextObjectId;
dino.IsCryo = true;

nextObjectId++;
var statusobject = new GameObject(cryoArchive);
statusobject.ObjectId = nextObjectId;

dino.loadProperties(cryoArchive, new GameObject(), 0, null);
var statusComponentRef = dino.GetProperty<PropertyObject>("MyCharacterStatusComponent");
statusComponentRef.Value.ObjectId = statusobject.ObjectId;
dino.properties.Remove(_myCharacterStatusComponent);
dino.properties.Add(_myCharacterStatusComponent, statusComponentRef);

statusobject.loadProperties(cryoArchive, new GameObject(), 0, null);

Objects.Add(dino);
Objects.Add(statusobject);

cryoVa.Dispose();

This comment was marked as resolved.

}

cryoMmf.Dispose();
}

//remove temp cryo data file
File.Delete(filePath);

}
}
}


return true;
}

Expand Down
3 changes: 2 additions & 1 deletion ArkSavegameToolkitNet/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ private enum GameObjectIs

//query helper fields
private GameObjectIs _isFlags;


public bool IsCryo { get; set; }
public bool IsCreature => (_isFlags & GameObjectIs.IsCreature) == GameObjectIs.IsCreature;
public bool IsTamedCreature => (_isFlags & GameObjectIs.IsTamedCreature) == GameObjectIs.IsTamedCreature;
public bool IsWildCreature => (_isFlags & GameObjectIs.IsWildCreature) == GameObjectIs.IsWildCreature;
Expand Down
1 change: 1 addition & 0 deletions ArkSavegameToolkitNet/IGameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IGameObject : IPropertyContainer
Guid Uuid { get; set; }

//query helper fields
bool IsCryo { get; set; }
bool IsCreature { get; }
bool IsTamedCreature { get; }
bool IsWildCreature { get; }
Expand Down