コード例 #1
0
        private void SaveToCache(byte[] fileData)
        {
            if ((fileData == null) || (fileData.Length == 0))
            {
                return;
            }
            var objectName   = Common.CalculateSha256Hash(fileData);
            var objectFile   = CommonUtil.CombinePaths(CacheDirectory, objectName + ".txt");
            var incomingFile = CommonUtil.CombinePaths(CacheDirectory, "Incoming", objectName + ".txt");

            if (!File.Exists(objectFile))
            {
                File.WriteAllBytes(incomingFile, fileData);
                File.Move(incomingFile, objectFile);
                CurrentCacheSize             += fileData.Length;
                FileLengths[objectName]       = fileData.Length;
                FileCreationTimes[objectName] = new FileInfo(objectFile).CreationTime;
            }
            else
            {
                File.SetCreationTime(objectFile, DateTime.Now);
                FileCreationTimes[objectName] = new FileInfo(objectFile).CreationTime;
            }
        }
コード例 #2
0
        public static void GenerateUniverse(string saveName)
        {
            var universeFolder = CommonUtil.CombinePaths(Client.KspPath, "Universe");

            if (Directory.Exists(universeFolder))
            {
                Directory.Delete(universeFolder, true);
            }

            var saveFolder = CommonUtil.CombinePaths(SavesFolder, saveName);

            if (!Directory.Exists(saveFolder))
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', Save directory doesn't exist");
                ScreenMessages.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', Save directory doesn't exist", 5f,
                                                 ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            var persistentFile = CommonUtil.CombinePaths(saveFolder, "persistent.sfs");

            if (!File.Exists(persistentFile))
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', persistent.sfs doesn't exist");
                ScreenMessages.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', persistent.sfs doesn't exist", 5f,
                                                 ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            Directory.CreateDirectory(universeFolder);
            var vesselFolder = CommonUtil.CombinePaths(universeFolder, "Vessels");

            Directory.CreateDirectory(vesselFolder);
            var scenarioFolder = CommonUtil.CombinePaths(universeFolder, "Scenarios");

            Directory.CreateDirectory(scenarioFolder);
            var playerScenarioFolder = CommonUtil.CombinePaths(scenarioFolder, SettingsSystem.CurrentSettings.PlayerName);

            Directory.CreateDirectory(playerScenarioFolder);
            var kerbalFolder = CommonUtil.CombinePaths(universeFolder, "Kerbals");

            Directory.CreateDirectory(kerbalFolder);

            //Load game data
            var persistentData = ConfigNode.Load(persistentFile);

            if (persistentData == null)
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', failed to load persistent data");
                ScreenMessages.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', failed to load persistent data", 5f, ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            var gameData = persistentData.GetNode("GAME");

            if (gameData == null)
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', failed to load game data");
                ScreenMessages.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', failed to load game data", 5f,
                                                 ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            //Save vessels
            var flightState = gameData.GetNode("FLIGHTSTATE");

            if (flightState == null)
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', failed to load flight state data");
                ScreenMessages.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', failed to load flight state data", 5f,
                                                 ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            var vesselNodes = flightState.GetNodes("VESSEL");

            if (vesselNodes != null)
            {
                foreach (var cn in vesselNodes)
                {
                    var vesselId = Common.ConvertConfigStringToGuidString(cn.GetValue("pid"));
                    LunaLog.Log($"[LMP]: Saving vessel {vesselId}, Name: {cn.GetValue("Name")}");
                    cn.Save(CommonUtil.CombinePaths(vesselFolder, $"{vesselId}.txt"));
                }
            }
            //Save scenario data
            var scenarioNodes = gameData.GetNodes("SCENARIO");

            if (scenarioNodes != null)
            {
                foreach (var cn in scenarioNodes)
                {
                    var scenarioName = cn.GetValue("Name");
                    LunaLog.Log($"[LMP]: Saving scenario: {scenarioName}");
                    cn.Save(CommonUtil.CombinePaths(playerScenarioFolder, $"{scenarioName}.txt"));
                }
            }
            //Save kerbal data
            var kerbalNodes = gameData.GetNode("ROSTER").GetNodes("CREW");

            if (kerbalNodes != null)
            {
                var kerbalIndex = 0;
                foreach (var cn in kerbalNodes)
                {
                    LunaLog.Log($"[LMP]: Saving kerbal {kerbalIndex}, Name: {cn.GetValue("Name")}");
                    cn.Save(CommonUtil.CombinePaths(kerbalFolder, $"{kerbalIndex}.txt"));
                    kerbalIndex++;
                }
            }
            LunaLog.Log($"[LMP]: Generated KSP_folder/Universe from {saveName}");
            ScreenMessages.PostScreenMessage($"Generated KSP_folder/Universe from {saveName}", 5f,
                                             ScreenMessageStyle.UPPER_CENTER);
        }
コード例 #3
0
        public static void GenerateUniverse(string saveName)
        {
            var universeFolder = CommonUtil.CombinePaths(MainSystem.KspPath, "Universe");

            if (Directory.Exists(universeFolder))
            {
                Directory.Delete(universeFolder, true);
            }

            var saveFolder = CommonUtil.CombinePaths(SavesFolder, saveName);

            if (!Directory.Exists(saveFolder))
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', Save directory doesn't exist");
                LunaScreenMsg.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', Save directory doesn't exist", 5f,
                                                ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            var persistentFile = CommonUtil.CombinePaths(saveFolder, "persistent.sfs");

            if (!File.Exists(persistentFile))
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', persistent.sfs doesn't exist");
                LunaScreenMsg.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', persistent.sfs doesn't exist", 5f,
                                                ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            Directory.CreateDirectory(universeFolder);
            var vesselFolder = CommonUtil.CombinePaths(universeFolder, "Vessels");

            Directory.CreateDirectory(vesselFolder);
            var scenarioFolder = CommonUtil.CombinePaths(universeFolder, "Scenarios");

            Directory.CreateDirectory(scenarioFolder);
            var kerbalFolder = CommonUtil.CombinePaths(universeFolder, "Kerbals");

            Directory.CreateDirectory(kerbalFolder);

            //Load game data
            var persistentData = ConfigNode.Load(persistentFile);

            if (persistentData == null)
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', failed to load persistent data");
                LunaScreenMsg.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', failed to load persistent data", 5f, ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            var gameData = persistentData.GetNode("GAME");

            if (gameData == null)
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', failed to load game data");
                LunaScreenMsg.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', failed to load game data", 5f,
                                                ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            //Save vessels
            var flightState = gameData.GetNode("FLIGHTSTATE");

            if (flightState == null)
            {
                LunaLog.Log($"[LMP]: Failed to generate a LMP universe for '{saveName}', failed to load flight state data");
                LunaScreenMsg.PostScreenMessage($"Failed to generate a LMP universe for '{saveName}', failed to load flight state data", 5f,
                                                ScreenMessageStyle.UPPER_CENTER);
                return;
            }

            var vesselNodes = flightState.GetNodes("VESSEL");

            if (vesselNodes != null)
            {
                foreach (var cn in vesselNodes)
                {
                    var vesselId = Common.ConvertConfigStringToGuidString(cn.GetValue("pid"));
                    LunaLog.Log($"[LMP]: Saving vessel {vesselId}, Name: {cn.GetValue("name")}");

                    var xmlData = ConfigNodeXmlParser.ConvertToXml(Encoding.UTF8.GetString(ConfigNodeSerializer.Serialize(cn)));
                    File.WriteAllText(CommonUtil.CombinePaths(vesselFolder, $"{vesselId}.xml"), xmlData);
                }
            }

            //Save scenario data
            var scenarioNodes = gameData.GetNodes("SCENARIO");

            if (scenarioNodes != null)
            {
                foreach (var cn in scenarioNodes)
                {
                    var scenarioName = cn.GetValue("name");
                    if (string.IsNullOrEmpty(scenarioName))
                    {
                        continue;
                    }

                    LunaLog.Log($"[LMP]: Saving scenario: {scenarioName}");

                    var xmlData = ConfigNodeXmlParser.ConvertToXml(Encoding.UTF8.GetString(ConfigNodeSerializer.Serialize(cn)));
                    File.WriteAllText(CommonUtil.CombinePaths(scenarioFolder, $"{scenarioName}.xml"), xmlData);
                }
            }

            //Save kerbal data
            var kerbalNodes = gameData.GetNode("ROSTER").GetNodes("KERBAL");

            if (kerbalNodes != null)
            {
                foreach (var cn in kerbalNodes)
                {
                    var kerbalName = cn.GetValue("name");
                    LunaLog.Log($"[LMP]: Saving kerbal: {kerbalName}");

                    cn.Save(CommonUtil.CombinePaths(kerbalFolder, $"{kerbalName}.txt"));
                }
            }

            LunaLog.Log($"[LMP]: Generated KSP_folder/Universe from {saveName}");
            LunaScreenMsg.PostScreenMessage($"Generated KSP_folder/Universe from {saveName}", 5f, ScreenMessageStyle.UPPER_CENTER);
        }
コード例 #4
0
        public void ExpireCache()
        {
            Debug.Log("[LMP]: Expiring cache!");
            //No folder, no delete.
            if (!Directory.Exists(CommonUtil.CombinePaths(CacheDirectory, "Incoming")))
            {
                Debug.Log("[LMP]: No sync cache folder, skipping expire.");
                return;
            }
            //Delete partial incoming files
            var incomingFiles = Directory.GetFiles(CommonUtil.CombinePaths(CacheDirectory, "Incoming"));

            foreach (var incomingFile in incomingFiles)
            {
                Debug.Log($"[LMP]: Deleting partially cached object {incomingFile}");
                File.Delete(incomingFile);
            }
            //Delete old files
            var cacheObjects = GetCachedObjects();

            CurrentCacheSize = 0;
            foreach (var cacheObject in cacheObjects)
            {
                var cacheFile = CommonUtil.CombinePaths(CacheDirectory, cacheObject + ".txt");
                //If the file is older than a week, delete it.
                if (File.GetCreationTime(cacheFile).AddDays(7d) < DateTime.Now)
                {
                    Debug.Log($"[LMP]: Deleting cached object {cacheObject}, reason: Expired!");
                    File.Delete(cacheFile);
                }
                else
                {
                    var fi = new FileInfo(cacheFile);
                    FileCreationTimes[cacheObject] = fi.CreationTime;
                    FileLengths[cacheObject]       = fi.Length;
                    CurrentCacheSize += fi.Length;
                }
            }
            //While the directory is over (cacheSize) MB
            while (CurrentCacheSize > SettingsSystem.CurrentSettings.CacheSize * 1024 * 1024)
            {
                string deleteObject = null;
                //Find oldest file
                foreach (var testFile in FileCreationTimes)
                {
                    if (deleteObject == null)
                    {
                        deleteObject = testFile.Key;
                    }
                    if (testFile.Value < FileCreationTimes[deleteObject])
                    {
                        deleteObject = testFile.Key;
                    }
                }
                Debug.Log($"[LMP]: Deleting cached object {deleteObject}, reason: Cache full!");
                var deleteFile = CommonUtil.CombinePaths(CacheDirectory, deleteObject + ".txt");
                File.Delete(deleteFile);
                CurrentCacheSize -= FileLengths[deleteObject];
                if (FileCreationTimes.ContainsKey(deleteObject))
                {
                    FileCreationTimes.Remove(deleteObject);
                }
                if (FileLengths.ContainsKey(deleteObject))
                {
                    FileLengths.Remove(deleteObject);
                }
            }
        }