public void Persist(StatePersister reader) { reader.PersistVersion(2); // This will be the same as the existing Owner, unless control of this team has been transferred. var ownerPlayerId = Owner.Id; reader.PersistUInt32(ref ownerPlayerId); Owner = reader.Game.PlayerManager.GetPlayerByIndex(ownerPlayerId); reader.PersistAsciiString(ref _attackPriorityName); reader.PersistBoolean(ref _unknown1); reader.PersistObject(_templateData); var teamCount = (ushort)_teams.Count; reader.PersistUInt16(ref teamCount); reader.BeginArray("Teams"); if (reader.Mode == StatePersistMode.Read) { for (var i = 0; i < teamCount; i++) { reader.BeginObject(); var id = 0u; reader.PersistUInt32(ref id); var team = FindTeamById(id); if (team == null) { team = TeamFactory.AddTeamWithId(this, id); } reader.PersistObject(team, "Value"); reader.EndObject(); } } else { foreach (var team in _teams) { reader.BeginObject(); var id = team.Id; reader.PersistUInt32(ref id); reader.PersistObject(team, "Value"); reader.EndObject(); } } reader.EndArray(); }
public void Persist(StatePersister reader) { if (reader.Mode == StatePersistMode.Read) { _locomotors.Clear(); } reader.PersistVersion(1); var numLocomotorTemplates = (ushort)_locomotors.Count; reader.PersistUInt16(ref numLocomotorTemplates, "NumLocomotors"); reader.BeginArray("Locomotors"); if (reader.Mode == StatePersistMode.Read) { for (var i = 0; i < numLocomotorTemplates; i++) { reader.BeginObject(); var locomotorTemplateName = ""; reader.PersistAsciiString(ref locomotorTemplateName, "TemplateName"); var locomotorTemplate = reader.AssetStore.LocomotorTemplates.GetByName(locomotorTemplateName); var locomotor = new Locomotor(_gameObject, locomotorTemplate, 100); reader.PersistObject(locomotor); _locomotors.Add(locomotor); reader.EndArray(); } } else { foreach (var locomotor in _locomotors) { reader.BeginObject(); var templateName = locomotor.LocomotorTemplate.Name; reader.PersistAsciiString(ref templateName); reader.PersistObject(locomotor); reader.EndObject(); } } reader.EndArray(); reader.PersistEnumFlags(ref _surfaces); reader.SkipUnknownBytes(1); }
public void Persist(StatePersister reader) { reader.PersistVersion(1); var objectDefinitionName = _currentWeaponTemplateSet?.ObjectDefinition.Name; reader.PersistAsciiString(ref objectDefinitionName); var conditions = _currentWeaponTemplateSet?.Conditions ?? new BitArray <WeaponSetConditions>(); reader.PersistBitArray(ref conditions); if (reader.Mode == StatePersistMode.Read) { _currentWeaponTemplateSet = _gameObject.Definition.WeaponSets[conditions]; } // In Generals there are 3 possible weapons. // Later games have up to 5. reader.BeginArray("Weapons"); for (var i = 0; i < 3; i++) { reader.BeginObject(); var slotFilled = _weapons[i] != null; reader.PersistBoolean(ref slotFilled); if (slotFilled) { if (reader.Mode == StatePersistMode.Read) { _weapons[i] = new Weapon( _gameObject, _currentWeaponTemplateSet.Slots[i].Weapon.Value, (WeaponSlot)i, _gameObject.GameContext); } reader.PersistObject(_weapons[i], "Value"); } else { _weapons[i] = null; } reader.EndObject(); } reader.EndArray(); reader.PersistEnum(ref _currentWeaponSlot); reader.PersistUInt32(ref _unknown1); reader.PersistUInt32(ref _filledWeaponSlots); reader.PersistEnumFlags(ref _combinedAntiMask); reader.PersistUInt32(ref _unknown2); reader.PersistBoolean(ref _unknown3); reader.PersistBoolean(ref _unknown4); }
public void Persist(StatePersister reader) { reader.PersistVersion(1); reader.PersistUInt32(ref _lastTeamId); var count = (ushort)_teamTemplates.Count; reader.PersistUInt16(ref count, "TeamTemplatesCount"); if (count != _teamTemplates.Count) { throw new InvalidStateException(); } reader.BeginArray("TeamTemplates"); if (reader.Mode == StatePersistMode.Read) { for (var i = 0; i < count; i++) { reader.BeginObject(); var id = 0u; reader.PersistUInt32(ref id); var teamTemplate = _teamTemplatesById[id]; reader.PersistObject(teamTemplate); reader.EndObject(); } } else { foreach (var teamTemplate in _teamTemplates) { reader.BeginObject(); var id = teamTemplate.ID; reader.PersistUInt32(ref id); reader.PersistObject(teamTemplate); reader.EndObject(); } } reader.EndArray(); }
public void Persist(StatePersister reader) { reader.PersistVersion(2); reader.BeginObject("Base"); reader.PersistVersion(1); reader.EndObject(); reader.PersistBoolean(ref _unknownBool); if (_unknownBool) { reader.BeginObject("UnknownThing"); reader.PersistVersion(1); // Matches VertexWaterXGridCellsN and VertexWaterYGridCellsN in GameData.ini var gridCellsX = 0; reader.PersistInt32(ref gridCellsX); var gridCellsY = 0; reader.PersistInt32(ref gridCellsY); // Don't know why, but this gives the correct length for this array. var dataCount = (gridCellsX + 3) * (gridCellsY + 3) * 10; reader.SkipUnknownBytes(dataCount); reader.EndObject(); } var game = reader.Game; var area = game.Scene3D.MapFile.HeightMapData.Area; reader.PersistUInt32(ref area); if (area != game.Scene3D.MapFile.HeightMapData.Area) { throw new InvalidStateException(); } var width = game.Scene3D.MapFile.HeightMapData.Width; var height = game.Scene3D.MapFile.HeightMapData.Height; var elevations = game.Scene3D.MapFile.HeightMapData.Elevations; reader.BeginArray("Elevations"); for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { var elevation = (byte)elevations[x, y]; reader.PersistByteValue(ref elevation); elevations[x, y] = elevation; } } reader.EndArray(); if (reader.Mode == StatePersistMode.Read) { // TODO: Not great to create the initial patches, // then recreate them here. game.Scene3D.Terrain.OnHeightMapChanged(); } }
public void Persist(StatePersister reader) { reader.PersistVersion(1); reader.BeginObject("Base"); reader.PersistVersion(1); reader.EndObject(); var objectId = _gameObject?.ID ?? 0u; reader.PersistObjectID(ref objectId); if (reader.Mode == StatePersistMode.Read) { _gameObject = reader.Game.GameLogic.GetObjectById(objectId); } reader.PersistEnum(ref _geometryType); // Sometimes there's a 0xC, which is probably uninitialized data. byte geometryIsSmall = _geometryIsSmall ? (byte)1 : (byte)0; reader.PersistByte(ref geometryIsSmall); _geometryIsSmall = geometryIsSmall == 1; reader.PersistSingle(ref _geometryMajorRadius); reader.PersistSingle(ref _geometryMinorRadius); reader.PersistSingle(ref _angle); reader.PersistVector3(ref _position); reader.SkipUnknownBytes(12); reader.BeginArray("ModelsPerPlayer"); for (var i = 0; i < Player.MaxPlayers; i++) { reader.BeginObject(); byte numModels = (byte)_modelsPerPlayer[i].Count; reader.PersistByte(ref numModels); reader.BeginArray("Models"); for (var j = 0; j < numModels; j++) { reader.BeginObject(); var modelName = reader.Mode == StatePersistMode.Write ? _modelsPerPlayer[i][j].Model.Name : ""; reader.PersistAsciiString(ref modelName); Model model; ModelInstance modelInstance; if (reader.Mode == StatePersistMode.Read) { model = reader.AssetStore.Models.GetByName(modelName); modelInstance = model.CreateInstance(reader.AssetStore.LoadContext); _modelsPerPlayer[i].Add(modelInstance); } else { modelInstance = _modelsPerPlayer[i][j]; model = modelInstance.Model; } var scale = 1.0f; reader.PersistSingle(ref scale); if (scale != 1.0f) { throw new InvalidStateException(); } reader.PersistColorRgba(ref modelInstance.HouseColor, "HouseColor"); reader.PersistVersion(1); var modelTransform = reader.Mode == StatePersistMode.Write ? Matrix4x3.FromMatrix4x4(modelInstance.GetWorldMatrix()) : Matrix4x3.Identity; reader.PersistMatrix4x3(ref modelTransform, readVersion: false); if (reader.Mode == StatePersistMode.Read) { modelInstance.SetWorldMatrix(modelTransform.ToMatrix4x4()); } var numMeshes = (uint)model.SubObjects.Length; reader.PersistUInt32(ref numMeshes); if (numMeshes > 0 && numMeshes != model.SubObjects.Length) { throw new InvalidStateException(); } reader.BeginArray("Meshes"); for (var k = 0; k < numMeshes; k++) { reader.BeginObject(); var meshName = model.SubObjects[k].FullName; reader.PersistAsciiString(ref meshName); if (meshName != model.SubObjects[k].FullName) { throw new InvalidStateException(); } reader.PersistBoolean(ref modelInstance.UnknownBools[k], "UnknownBool"); var meshTransform = reader.Mode == StatePersistMode.Write ? Matrix4x3.FromMatrix4x4(modelInstance.RelativeBoneTransforms[model.SubObjects[k].Bone.Index]) : Matrix4x3.Identity; reader.PersistMatrix4x3(ref meshTransform, readVersion: false); if (reader.Mode == StatePersistMode.Read) { // TODO: meshTransform is actually absolute, not relative. modelInstance.RelativeBoneTransforms[model.SubObjects[k].Bone.Index] = meshTransform.ToMatrix4x4(); } reader.EndObject(); } reader.EndArray(); reader.EndObject(); } reader.EndArray(); reader.EndObject(); } reader.EndArray(); reader.PersistBoolean(ref _hasUnknownThing); if (_hasUnknownThing) { reader.PersistByte(ref _unknownByte); reader.PersistUInt32(ref _unknownInt); } }