protected override void Save(string rawPath, Hashing.Hash128 hash, Dictionary <string, string> materialNameMapping, Dictionary <string, object> tags)
        {
            using (PoolManager.Get(out HashSet <string> shapeNames))
            {
                if (tags.TryGetValue(MyImporterConstants.TAG_HAVOK_DESTRUCTION, out var destructionBufferIn) ||
                    tags.TryGetValue(MyImporterConstants.TAG_HAVOK_COLLISION_GEOMETRY, out destructionBufferIn))
                {
                    using (var hkWorld = new HkWorld(true, 50000, float.MaxValue, false, 4, 0.6f))
                    {
                        hkWorld.MarkForWrite();
                        hkWorld.DestructionWorld = new HkdWorld(hkWorld);
                        hkWorld.UnmarkForWrite();
                        using (var destructionStorage = new HkDestructionStorage(hkWorld.DestructionWorld))
                        {
                            var result = destructionStorage.LoadDestructionDataFromBuffer((byte[])destructionBufferIn)[0];
                            CollectShapeNamesRecursive(result, shapeNames);
//                            RenameShapesRecursive(result, "_" + hash);
//                            var rawOutput = Path.GetTempFileName();
//                            destructionStorage.SaveDestructionData(result, rawOutput);
//                            var rawBytes = File.ReadAllBytes(rawOutput);
//                            FixupMaterialReferences(rawBytes, materialNameMapping);
//                            tags[MyImporterConstants.TAG_HAVOK_DESTRUCTION] = rawBytes;
//                            File.Delete(rawOutput);
                        }
                    }
                }

                if (tags.TryGetValue(MyImporterConstants.TAG_HAVOK_COLLISION_GEOMETRY, out var havokCollision))
                {
                    using (PoolManager.Get(out Dictionary <string, string> dict))
                    {
                        foreach (var kv in materialNameMapping)
                        {
                            dict[kv.Key] = kv.Value;
                        }
                        foreach (var name in shapeNames)
                        {
                            var hb = Hashing.Builder();
                            hb.Add(name);
                            hb.Add(hash);
                            dict[name] = hb.Build().ToLimitedString(name.Length);
                        }

                        FixupMaterialReferences((byte[])havokCollision, dict);
                    }
                }
            }

            var fullPath = Path.Combine(_exportContentRoot, rawPath);

            using (var writer = new BinaryWriter(File.Open(fullPath, FileMode.Create)))
                ModelExporter.ExportModelData(writer, tags);
        }
        protected override void UnloadData()
        {
            TemporaryWorld.MarkForWrite(); //must be marked for write when deleting
            Storage.Dispose();
            Storage = null;
            TemporaryWorld.DestructionWorld.Dispose();

            TemporaryWorld.Dispose();
            TemporaryWorld = null;

            BlockShapePool.Free();
            BlockShapePool = null;

            Static = null;
        }
        public override void LoadData()
        {
            if (!HkBaseSystem.DestructionEnabled)
            {
                MyLog.Default.WriteLine("Havok Destruction is not availiable in this build.");
                throw new System.InvalidOperationException("Havok Destruction is not availiable in this build.");
            }

            if (Static != null)
            {
                MyLog.Default.WriteLine("Destruction data was not freed. Unloading now...");
                //throw new System.InvalidOperationException("Destruction data was not freed");
                UnloadData();
            }
            Static = this;
            BlockShapePool = new MyBlockShapePool();

            TemporaryWorld = new HkWorld(true, 50000, MyPhysics.RestingVelocity, MyFakes.ENABLE_HAVOK_MULTITHREADING, 4);
            TemporaryWorld.MarkForWrite();
            TemporaryWorld.DestructionWorld = new HkdWorld(TemporaryWorld);
            TemporaryWorld.UnmarkForWrite();
            Storage = new HkDestructionStorage(TemporaryWorld.DestructionWorld);

            // pre-fracture cube blocks
            {
                foreach (var groupName in MyDefinitionManager.Static.GetDefinitionPairNames())
                {
                    var group = MyDefinitionManager.Static.GetDefinitionGroup(groupName);

                    if (group.Large != null)
                    {
                        var model = MyModels.GetModel(group.Large.Model);
                        if (model == null)
                            continue;

                        bool isGenerated = group.Large.IsGeneratedBlock && (group.Large.GeneratedBlockType == GENERATED_BLOCK_TYPE_PILLAR);
                        if (!MyFakes.LAZY_LOAD_DESTRUCTION || (model != null && model.HavokBreakableShapes != null)) //reload materials
                            LoadModelDestruction(group.Large.Model, group.Large, isGenerated, group.Large.Size * (MyDefinitionManager.Static.GetCubeSize(group.Large.CubeSize)));
                       
                       foreach(var progress in group.Large.BuildProgressModels)
                       {
                           model = MyModels.GetModel(progress.File);
                           if (model == null)
                               continue;

                           if (!MyFakes.LAZY_LOAD_DESTRUCTION || (model != null && model.HavokBreakableShapes != null)) //reload materials
                               LoadModelDestruction(progress.File, group.Large, isGenerated, group.Large.Size * (MyDefinitionManager.Static.GetCubeSize(group.Large.CubeSize)));
                       }
                        
                        
                        if (MyFakes.CHANGE_BLOCK_CONVEX_RADIUS)
                        {
                            if (model != null && model.HavokBreakableShapes != null)
                            {
                                var shape = model.HavokBreakableShapes[0].GetShape();
                                if(shape.ShapeType != HkShapeType.Sphere && shape.ShapeType != HkShapeType.Capsule)
                                    SetConvexRadius(model.HavokBreakableShapes[0], MyDestructionConstants.LARGE_GRID_CONVEX_RADIUS);
                            }
                        }
                    }

                    if (group.Small != null)
                    {
                        var model = MyModels.GetModel(group.Small.Model);
                        if (model == null)
                            continue;
                        bool isGenerated = group.Small.IsGeneratedBlock && (group.Small.GeneratedBlockType == GENERATED_BLOCK_TYPE_PILLAR);
                        if (!MyFakes.LAZY_LOAD_DESTRUCTION || (model != null && model.HavokBreakableShapes != null)) //reload materials
                            LoadModelDestruction(group.Small.Model, group.Small, isGenerated, group.Small.Size * (MyDefinitionManager.Static.GetCubeSize(group.Small.CubeSize)));

                        foreach (var progress in group.Small.BuildProgressModels)
                        {
                            model = MyModels.GetModel(progress.File);
                            if (model == null)
                                continue;
                            if (!MyFakes.LAZY_LOAD_DESTRUCTION || (model != null && model.HavokBreakableShapes != null)) //reload materials
                                LoadModelDestruction(progress.File, group.Small, isGenerated, group.Large.Size * (MyDefinitionManager.Static.GetCubeSize(group.Large.CubeSize)));
                        }

                        if (MyFakes.CHANGE_BLOCK_CONVEX_RADIUS)
                        {
                            if (model != null && model.HavokBreakableShapes != null)
                            {
                                var shape = model.HavokBreakableShapes[0].GetShape();
                                if (shape.ShapeType != HkShapeType.Sphere && shape.ShapeType != HkShapeType.Capsule)
                                    SetConvexRadius(model.HavokBreakableShapes[0], MyDestructionConstants.LARGE_GRID_CONVEX_RADIUS);
                            }
                        }
                    }
                }
                if (!MyFakes.LAZY_LOAD_DESTRUCTION)
                    BlockShapePool.Preallocate();
            }

            foreach (var enviroment in MyDefinitionManager.Static.GetEnvironmentItemDefinitions())
            {
                LoadModelDestruction(enviroment.Model, enviroment, false, Vector3.One, false, true);
            }
        }
        protected override void UnloadData()
        {

            TemporaryWorld.MarkForWrite(); //must be marked for write when deleting
            Storage.Dispose();
            Storage = null;
            TemporaryWorld.DestructionWorld.Dispose();

            TemporaryWorld.Dispose();
            TemporaryWorld = null;

            BlockShapePool.Free();
            BlockShapePool = null;

            Static = null;
        }
        public override void LoadData()
        {
            if (!HkBaseSystem.DestructionEnabled)
            {
                MyLog.Default.WriteLine("Havok Destruction is not availiable in this build.");
                throw new System.InvalidOperationException("Havok Destruction is not availiable in this build.");
            }

            if (Static != null)
            {
                MyLog.Default.WriteLine("Destruction data was not freed. Unloading now...");
                //throw new System.InvalidOperationException("Destruction data was not freed");
                UnloadData();
            }
            Static         = this;
            BlockShapePool = new MyBlockShapePool();

            TemporaryWorld = new HkWorld(true, 50000, MyPhysics.RestingVelocity, MyFakes.ENABLE_HAVOK_MULTITHREADING);
            TemporaryWorld.MarkForWrite();
            TemporaryWorld.DestructionWorld = new HkdWorld(TemporaryWorld);
            TemporaryWorld.UnmarkForWrite();
            Storage = new HkDestructionStorage(TemporaryWorld.DestructionWorld);

            // pre-fracture cube blocks
            if (!MyFakes.LAZY_LOAD_DESTRUCTION)
            {
                foreach (var groupName in MyDefinitionManager.Static.GetDefinitionPairNames())
                {
                    var group = MyDefinitionManager.Static.GetDefinitionGroup(groupName);

                    if (group.Large != null)
                    {
                        bool isGenerated = group.Large.IsGeneratedBlock && (group.Large.GeneratedBlockType == GENERATED_BLOCK_TYPE_PILLAR);
                        LoadModelDestruction(group.Large, isGenerated, group.Large.Size * (MyDefinitionManager.Static.GetCubeSize(group.Large.CubeSize)));
                        if (MyFakes.CHANGE_BLOCK_CONVEX_RADIUS)
                        {
                            var model = MyModels.GetModelOnlyData(group.Large.Model);
                            if (model != null && model.HavokBreakableShapes != null)
                            {
                                var shape = model.HavokBreakableShapes[0].GetShape();
                                if (shape.ShapeType != HkShapeType.Sphere && shape.ShapeType != HkShapeType.Capsule)
                                {
                                    SetConvexRadius(model.HavokBreakableShapes[0], MyDestructionConstants.LARGE_GRID_CONVEX_RADIUS);
                                }
                            }
                        }
                    }

                    if (group.Small != null)
                    {
                        bool isGenerated = group.Small.IsGeneratedBlock && (group.Small.GeneratedBlockType == GENERATED_BLOCK_TYPE_PILLAR);
                        LoadModelDestruction(group.Small, isGenerated, group.Small.Size * (MyDefinitionManager.Static.GetCubeSize(group.Small.CubeSize)));
                        if (MyFakes.CHANGE_BLOCK_CONVEX_RADIUS)
                        {
                            var model = MyModels.GetModelOnlyData(group.Small.Model);
                            if (model != null && model.HavokBreakableShapes != null)
                            {
                                var shape = model.HavokBreakableShapes[0].GetShape();
                                if (shape.ShapeType != HkShapeType.Sphere && shape.ShapeType != HkShapeType.Capsule)
                                {
                                    SetConvexRadius(model.HavokBreakableShapes[0], MyDestructionConstants.LARGE_GRID_CONVEX_RADIUS);
                                }
                            }
                        }
                    }
                }
                BlockShapePool.Preallocate();
            }

            foreach (var enviroment in MyDefinitionManager.Static.GetEnvironmentItemDefinitions())
            {
                LoadModelDestruction(enviroment, false, Vector3.One, false, true);
            }
        }
Beispiel #6
0
        public override void LoadData()
        {
            if (!HkBaseSystem.DestructionEnabled)
            {
                MyLog.Default.WriteLine("Havok Destruction is not availiable in this build.");
                throw new System.InvalidOperationException("Havok Destruction is not availiable in this build.");
            }

            if (Static != null)
            {
                MyLog.Default.WriteLine("Destruction data was not freed. Unloading now...");
                //throw new System.InvalidOperationException("Destruction data was not freed");
                UnloadData();
            }
            Static         = this;
            BlockShapePool = new MyBlockShapePool();

            TemporaryWorld = new HkWorld(true, 50000, MyPhysics.RestingVelocity, MyFakes.ENABLE_HAVOK_MULTITHREADING, 4);
            TemporaryWorld.MarkForWrite();
            TemporaryWorld.DestructionWorld = new HkdWorld(TemporaryWorld);
            TemporaryWorld.UnmarkForWrite();
            Storage = new HkDestructionStorage(TemporaryWorld.DestructionWorld);

            // pre-fracture cube blocks
            {
                foreach (var groupName in MyDefinitionManager.Static.GetDefinitionPairNames())
                {
                    var group = MyDefinitionManager.Static.GetDefinitionGroup(groupName);

                    if (group.Large != null)
                    {
                        var model = VRage.Game.Models.MyModels.GetModel(group.Large.Model);
                        if (model == null)
                        {
                            continue;
                        }

                        if (!MyFakes.LAZY_LOAD_DESTRUCTION || (model != null && model.HavokBreakableShapes != null)) //reload materials
                        {
                            LoadModelDestruction(group.Large.Model, group.Large, group.Large.Size * (MyDefinitionManager.Static.GetCubeSize(group.Large.CubeSize)));
                        }

                        foreach (var progress in group.Large.BuildProgressModels)
                        {
                            model = VRage.Game.Models.MyModels.GetModel(progress.File);
                            if (model == null)
                            {
                                continue;
                            }

                            if (!MyFakes.LAZY_LOAD_DESTRUCTION || (model != null && model.HavokBreakableShapes != null)) //reload materials
                            {
                                LoadModelDestruction(progress.File, group.Large, group.Large.Size * (MyDefinitionManager.Static.GetCubeSize(group.Large.CubeSize)));
                            }
                        }


                        if (MyFakes.CHANGE_BLOCK_CONVEX_RADIUS)
                        {
                            if (model != null && model.HavokBreakableShapes != null)
                            {
                                var shape = model.HavokBreakableShapes[0].GetShape();
                                if (shape.ShapeType != HkShapeType.Sphere && shape.ShapeType != HkShapeType.Capsule)
                                {
                                    SetConvexRadius(model.HavokBreakableShapes[0], MyDestructionConstants.LARGE_GRID_CONVEX_RADIUS);
                                }
                            }
                        }
                    }

                    if (group.Small != null)
                    {
                        var model = VRage.Game.Models.MyModels.GetModel(group.Small.Model);
                        if (model == null)
                        {
                            continue;
                        }

                        if (!MyFakes.LAZY_LOAD_DESTRUCTION || (model != null && model.HavokBreakableShapes != null)) //reload materials
                        {
                            LoadModelDestruction(group.Small.Model, group.Small, group.Small.Size * (MyDefinitionManager.Static.GetCubeSize(group.Small.CubeSize)));
                        }

                        foreach (var progress in group.Small.BuildProgressModels)
                        {
                            model = VRage.Game.Models.MyModels.GetModel(progress.File);
                            if (model == null)
                            {
                                continue;
                            }
                            if (!MyFakes.LAZY_LOAD_DESTRUCTION || (model != null && model.HavokBreakableShapes != null)) //reload materials
                            {
                                LoadModelDestruction(progress.File, group.Small, group.Large.Size * (MyDefinitionManager.Static.GetCubeSize(group.Large.CubeSize)));
                            }
                        }

                        if (MyFakes.CHANGE_BLOCK_CONVEX_RADIUS)
                        {
                            if (model != null && model.HavokBreakableShapes != null)
                            {
                                var shape = model.HavokBreakableShapes[0].GetShape();
                                if (shape.ShapeType != HkShapeType.Sphere && shape.ShapeType != HkShapeType.Capsule)
                                {
                                    SetConvexRadius(model.HavokBreakableShapes[0], MyDestructionConstants.LARGE_GRID_CONVEX_RADIUS);
                                }
                            }
                        }
                    }
                }
                if (!MyFakes.LAZY_LOAD_DESTRUCTION)
                {
                    BlockShapePool.Preallocate();
                }
            }

            foreach (var def in MyDefinitionManager.Static.GetAllDefinitions <MyPhysicalModelDefinition>())
            {
                LoadModelDestruction(def.Model, def, Vector3.One, false, true);
            }
        }