Beispiel #1
0
        public void ServerSave()
        {
            try
            {
                foreach (var item in Grids.TrackedGrids)
                {
                    GridMethods methods = new GridMethods(item.Key, HangarFolderDir);
                    if (methods.LoadInfoFile(out PlayerInfo Data))
                    {
                        foreach (var StampKey in item.Value)
                        {
                            if (!StampKey.Key)
                            {
                                //This means that we successfully saved the server with the grid loaded into the server
                                //Need to delete the file now
                                Log.Warn("Server Successfully saved with " + StampKey.Value.GridName + " owned by " + item.Key + " loaded in server! Deleting bak!");
                                string GridPath = Path.Combine(methods.FolderPath, StampKey.Value.GridName + ".bak");
                                File.Delete(GridPath);
                            }
                            else
                            {
                                Log.Warn("Server succesfully saved with grid " + StampKey.Value.GridName + " owned by " + item.Key + " in hangar! Clearing ID!");
                                int?Grid = Data.Grids.FindIndex(x => x.GridName.Equals(StampKey.Value.GridName));
                                if (Grid.HasValue && Data.Grids.IsValidIndex(Grid.Value))
                                {
                                    //This grid no longer esists?
                                    Data.Grids[Grid.Value].ServerPort = 0;
                                }
                            }
                        }

                        methods.SaveInfoFile(Data);
                    }
                }

                Log.Warn("Deleted Hangar Files via GridTracker!");

                Grids.TrackedGrids.Clear();
                SaveFile(Grids);
            }catch (Exception ex)
            {
                Log.Fatal(ex);
            }
        }
Beispiel #2
0
        private void InitilizeReadSystem()
        {
            //True = SavedToFile
            //False = LoadedIntoServer


            foreach (var item in Grids.TrackedGrids)
            {
                GridMethods methods = new GridMethods(item.Key, HangarFolderDir);
                methods.LoadInfoFile(out PlayerInfo Data);


                List <int> GridsToBeRemoved = new List <int>();
                foreach (var StampKey in item.Value)
                {
                    if (StampKey.Key)
                    {
                        //If true, that mean the grid was saved to hangar before server saved.
                        //Means that there is a duplicate grid in the server


                        //To fix this, we will remove this grid from the players file to revert back to the version that is still ingame
                        int?i = Data.Grids.FindIndex(x => x.GridName.Equals(StampKey.Value.GridName));
                        if (!i.HasValue || !Data.Grids.IsValidIndex(i.Value))
                        {
                            continue;
                        }

                        //Now that weve got the index,
                        Log.Warn("Grid needs to be removed: " + Data.Grids[i.Value].GridName);
                        string GridPath = Path.Combine(methods.FolderPath, Data.Grids[i.Value].GridName + ".bak");


                        File.Delete(GridPath);
                        GridsToBeRemoved.Add(i.Value);
                    }
                    else
                    {
                        //If false, this means the grid was loaded into the world before server saved.
                        //Means the grid is gone on the user side

                        //To fix this, we wont delete grids in peoples hangars until server saves.
                        //This will allow us to simply add this stamp to the players info file (the sbc has to be renamed from the .bak created on load)
                        Data.Grids.Add(StampKey.Value);
                        string GridBakPath = Path.Combine(methods.FolderPath, StampKey.Value.GridName + ".bak");
                        string GridPath    = Path.Combine(methods.FolderPath, StampKey.Value.GridName + ".sbc");
                        Log.Warn($"Restored grid that was not saved after being unhangared: {StampKey.Value.GridName}");
                        //File.Move(GridBakPath, GridPath);
                    }
                }

                //Remove grids
                foreach (int indice in GridsToBeRemoved.OrderByDescending(v => v))
                {
                    Data.Grids.RemoveAt(indice);
                }

                //Save the new files
                methods.SaveInfoFile(Data);
            }


            Grids.TrackedGrids.Clear();
            SaveFile(Grids);
        }
Beispiel #3
0
        private void AutoHangarWorker(Hangar Plugin)
        {
            //Significant performance increase
            if (MySession.Static.Ready)
            {
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();

                List <MyCubeGrid> ExportedGrids          = new List <MyCubeGrid>();
                List <MyIdentity> ExportPlayerIdentities = new List <MyIdentity>();

                Hangar.Debug("AutoHangar: Getting Players!");
                var PlayerIdentities = MySession.Static.Players.GetAllIdentities().OfType <MyIdentity>();


                foreach (MyIdentity player in PlayerIdentities)
                {
                    if (player == null)
                    {
                        continue;
                    }

                    DateTime LastLogin;
                    LastLogin = player.LastLoginTime;

                    ulong SteamID = MySession.Static.Players.TryGetSteamId(player.IdentityId);
                    if (LastLogin.AddDays(Plugin.Config.AutoHangarDayAmount) < DateTime.Now)
                    {
                        //AutoHangarBlacklist
                        if (!Plugin.Config.AutoHangarPlayerBlacklist.Any(x => x.SteamID == SteamID))
                        {
                            ExportPlayerIdentities.Add(player);
                        }
                    }
                }

                Hangar.Debug("AutoHangar: Total players to check-" + ExportPlayerIdentities.Count());
                int GridCounter = 0;

                //This gets all the grids
                foreach (MyIdentity player in ExportPlayerIdentities)
                {
                    ulong id = 0;
                    try
                    {
                        id = MySession.Static.Players.TryGetSteamId(player.IdentityId);
                    }
                    catch
                    {
                        Hangar.Debug("Identitiy doesnt have a SteamID! Shipping!");
                        continue;
                    }


                    if (id == 0)
                    {
                        //Sanity check
                        continue;
                    }

                    GridMethods methods = new GridMethods(id, Plugin.Config.FolderDirectory);
                    //string path = GridMethods.CreatePathForPlayer(Config.FolderDirectory, id);

                    if (!methods.LoadInfoFile(out PlayerInfo Data))
                    {
                        return;
                    }



                    ConcurrentBag <List <MyCubeGrid> > gridGroups = GridFinder.FindGridList(player.IdentityId, false);
                    if (gridGroups.Count == 0)
                    {
                        continue;
                    }


                    long LargestGridID = 0;

                    if (Plugin.Config.KeepPlayersLargestGrid)
                    {
                        //First need to find their largets grid
                        int BlocksCount = 0;

                        foreach (List <MyCubeGrid> grids in gridGroups)
                        {
                            int        GridBlockCounts        = 0;
                            int        LargestSingleGridCount = 0;
                            MyCubeGrid LargetsGrid            = grids[0];
                            foreach (MyCubeGrid grid in grids)
                            {
                                if (grid.BlocksCount > LargestSingleGridCount)
                                {
                                    LargestSingleGridCount = grid.BlocksCount;
                                    LargetsGrid            = grid;
                                }
                            }

                            GridBlockCounts = LargetsGrid.BlocksCount;

                            if (GridBlockCounts > BlocksCount)
                            {
                                BlocksCount   = GridBlockCounts;
                                LargestGridID = LargetsGrid.EntityId;
                            }
                        }
                    }



                    foreach (List <MyCubeGrid> grids in gridGroups)
                    {
                        if (grids.Count == 0)
                        {
                            continue;
                        }


                        if (grids[0].IsRespawnGrid && Plugin.Config.DeleteRespawnPods)
                        {
                            grids[0].Close();
                            continue;
                        }


                        Result result = new Result();
                        result.grids = grids;

                        var BiggestGrid = grids[0];
                        foreach (MyCubeGrid grid in grids)
                        {
                            if (grid.BlocksCount > BiggestGrid.BlocksCount)
                            {
                                BiggestGrid = grid;
                            }
                        }


                        if (Plugin.Config.KeepPlayersLargestGrid)
                        {
                            if (BiggestGrid.EntityId == LargestGridID)
                            {
                                //Skip players largest grid
                                continue;
                            }
                        }


                        //Grid Size Checks
                        if (BiggestGrid.GridSizeEnum == MyCubeSize.Large)
                        {
                            if (BiggestGrid.IsStatic && !Plugin.Config.AutoHangarStaticGrids)
                            {
                                continue;
                            }
                            else if (!BiggestGrid.IsStatic && !Plugin.Config.AutoHangarLargeGrids)
                            {
                                continue;
                            }
                        }
                        else if (BiggestGrid.GridSizeEnum == MyCubeSize.Small && !Plugin.Config.AutoHangarSmallGrids)
                        {
                            continue;
                        }



                        result.biggestGrid = BiggestGrid;
                        result.GetGrids    = true;

                        //Check for existing grid names
                        Utils.FormatGridName(Data, result);



                        if (methods.SaveGrids(result.grids, result.GridName, Plugin))
                        {
                            //Load player file and update!
                            //Fill out grid info and store in file
                            HangarChecks.GetBPDetails(result, Plugin.Config, out GridStamp Grid);

                            Grid.GridName = result.GridName;
                            Data.Grids.Add(Grid);


                            GridCounter++;
                            Hangar.Debug(result.biggestGrid.DisplayName + " was sent to Hangar due to inactivity!");
                        }
                        else
                        {
                            Hangar.Debug(result.biggestGrid.DisplayName + " FAILED to Hangar due to inactivity!");
                        }
                    }

                    //Save players file!
                    methods.SaveInfoFile(Data);
                }
                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;

                Hangar.Debug("AutoHangar: Finished Hangaring -" + GridCounter + " grids! Action took: " + ts.ToString());
            }
        }
Beispiel #4
0
        public void SaveAll()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            int GridCounter      = 0;
            int TotalGridCounter = 0;

            Dictionary <long, List <Result> > ToSaveGrids = new Dictionary <long, List <Result> >();

            Parallel.ForEach(MyCubeGridGroups.Static.Physical.Groups, group =>
            {
                List <MyCubeGrid> gridList = new List <MyCubeGrid>();
                var BiggestGrid            = group.Nodes.First().NodeData;
                Result result = new Result();
                foreach (MyGroups <MyCubeGrid, MyGridPhysicalGroupData> .Node groupNodes in group.Nodes)
                {
                    MyCubeGrid grid = groupNodes.NodeData;

                    if (grid.Physics == null)
                    {
                        continue;
                    }

                    if (grid.BlocksCount > BiggestGrid.BlocksCount)
                    {
                        BiggestGrid = grid;
                    }
                    TotalGridCounter += 1;
                    gridList.Add(grid);
                }


                if (gridList.Count == 0)
                {
                    return;
                }

                result.grids       = gridList;
                result.biggestGrid = BiggestGrid;



                if (ToSaveGrids.ContainsKey(BiggestGrid.BigOwners[0]))
                {
                    //Dictionary already contains grid!
                    ToSaveGrids[BiggestGrid.BigOwners[0]].Add(result);
                }
                else
                {
                    List <Result> AllGrids = new List <Result>();
                    AllGrids.Add(result);
                    ToSaveGrids.Add(BiggestGrid.BigOwners[0], AllGrids);
                }
            });


            //Attempt save!
            foreach (var item in ToSaveGrids)
            {
                ulong id = 0;
                try
                {
                    id = MySession.Static.Players.TryGetSteamId(item.Key);
                }
                catch
                {
                    Hangar.Debug("Identitiy doesnt have a SteamID! Shipping!");
                    continue;
                }


                if (id == 0)
                {
                    //Sanity check
                    continue;
                }

                GridMethods methods = new GridMethods(id, Plugin.Config.FolderDirectory);
                //string path = GridMethods.CreatePathForPlayer(Config.FolderDirectory, id);


                if (!methods.LoadInfoFile(out PlayerInfo Data))
                {
                    continue;
                }


                foreach (Result R in item.Value)
                {
                    //Fix invalid characters
                    Utils.FormatGridName(Data, R);


                    if (methods.SaveGrids(R.grids, R.GridName, Plugin))
                    {
                        //Load player file and update!
                        //Fill out grid info and store in file
                        HangarChecks.GetBPDetails(R, Plugin.Config, out GridStamp Grid);

                        Grid.GridName = R.GridName;
                        Data.Grids.Add(Grid);



                        GridCounter += 1;
                        Hangar.Debug(R.biggestGrid.DisplayName + " was sent to Hangar due to inside planet!");
                    }
                    else
                    {
                        Hangar.Debug(R.biggestGrid.DisplayName + " FAILED to Hangar due to inside planet!");
                    }
                }

                //Save file
                methods.SaveInfoFile(Data);
            }


            stopwatch.Stop();
            TimeSpan ts = stopwatch.Elapsed;

            Hangar.Debug("SaveAll: Found [" + GridCounter + "] grids out of [" + TotalGridCounter + "] total grids under planet! Action took: " + ts.ToString());
        }
Beispiel #5
0
        private void UnderPlanetWorker(Hangar Plugin)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            int GridCounter      = 0;
            int TotalGridCounter = 0;

            Dictionary <long, List <Result> > ToSaveGrids = new Dictionary <long, List <Result> >();

            Parallel.ForEach(MyCubeGridGroups.Static.Physical.Groups, group =>
            {
                try
                {
                    List <MyCubeGrid> gridList = new List <MyCubeGrid>();
                    var BiggestGrid            = group.Nodes.First().NodeData;
                    Result result = new Result();
                    foreach (MyGroups <MyCubeGrid, MyGridPhysicalGroupData> .Node groupNodes in group.Nodes)
                    {
                        MyCubeGrid grid = groupNodes.NodeData;

                        if (grid.Physics == null)
                        {
                            continue;
                        }

                        if (grid.BlocksCount > BiggestGrid.BlocksCount)
                        {
                            BiggestGrid = grid;
                        }
                        TotalGridCounter += 1;
                        gridList.Add(grid);
                    }


                    if (gridList.Count == 0)
                    {
                        return;
                    }

                    result.grids       = gridList;
                    result.biggestGrid = BiggestGrid;
                    result.GridName    = BiggestGrid.DisplayName;



                    Vector3D Position = BiggestGrid.PositionComp.GetPosition();
                    if (!Vector3D.IsZero(MyGravityProviderSystem.CalculateNaturalGravityInPoint(Position)))
                    {
                        MyPlanet planet = MyGamePruningStructure.GetClosestPlanet(Position);

                        //Main.Debug("Planet Min Radius: " + planet.MinimumRadius);

                        double distance = Vector3D.Distance(Position, planet.PositionComp.GetPosition());

                        //Main.Debug("Your distance from center: " + distance);

                        if (distance < planet.MinimumRadius * .7)
                        {
                            //Will save grid!


                            if (ToSaveGrids.ContainsKey(BiggestGrid.BigOwners[0]))
                            {
                                //Dictionary already contains grid!
                                ToSaveGrids[BiggestGrid.BigOwners[0]].Add(result);
                            }
                            else
                            {
                                List <Result> AllGrids = new List <Result>();
                                AllGrids.Add(result);
                                ToSaveGrids.Add(BiggestGrid.BigOwners[0], AllGrids);
                            }


                            return;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Hangar.Debug("Help", ex);
                }
            });


            //Attempt save!
            foreach (var item in ToSaveGrids)
            {
                ulong id = 0;
                try
                {
                    id = MySession.Static.Players.TryGetSteamId(item.Key);
                }
                catch
                {
                    Hangar.Debug("Identitiy doesnt have a SteamID! Shipping!");
                    continue;
                }


                if (id == 0)
                {
                    //Sanity check
                    continue;
                }

                GridMethods methods = new GridMethods(id, Plugin.Config.FolderDirectory);
                //string path = GridMethods.CreatePathForPlayer(Config.FolderDirectory, id);


                if (!methods.LoadInfoFile(out PlayerInfo Data))
                {
                    continue;
                }


                foreach (Result R in item.Value)
                {
                    //Fix invalid characters
                    Utils.FormatGridName(Data, R);

                    if (methods.SaveGrids(R.grids, R.GridName, Plugin))
                    {
                        //Load player file and update!
                        //Fill out grid info and store in file
                        HangarChecks.GetBPDetails(R, Plugin.Config, out GridStamp Grid);
                        Grid.ForceSpawnNearPlayer = true;
                        Grid.GridName             = R.GridName;
                        Data.Grids.Add(Grid);


                        GridCounter += 1;
                        Hangar.Debug(R.biggestGrid.DisplayName + " was sent to Hangar due to inside planet!");
                    }
                    else
                    {
                        Hangar.Debug(R.biggestGrid.DisplayName + " FAILED to Hangar due to inside planet!");
                    }
                }

                //Save file
                methods.SaveInfoFile(Data);
            }



            stopwatch.Stop();
            TimeSpan ts = stopwatch.Elapsed;

            Hangar.Debug("PlanetHangar: Found [" + GridCounter + "] grids out of [" + TotalGridCounter + "] total grids under planet! Action took: " + ts.ToString());
        }