public bool InsertScriptMod(ScriptMod scriptMod) { scriptMod.FilesWithPath = new List <string>(); string modsPath = Path.Combine(SettingsHandler.ModsDirectory, "Script Mods"); string scriptModDir = scriptMod.Name; DirectoryInfo fullModificationPath = new DirectoryInfo(Path.Combine(modsPath, scriptModDir)); FileInfo[] allModFiles = fullModificationPath.GetFiles("*", SearchOption.AllDirectories); foreach (FileInfo modFile in allModFiles) { string fullPath = modFile.FullName; string relativePathWithFile = fullPath.Split(new[] { fullModificationPath + @"\" }, StringSplitOptions.RemoveEmptyEntries)[0]; scriptMod.FilesWithPath.Add(relativePathWithFile); string relativePathWithoutFile = Path.GetDirectoryName(relativePathWithFile); if (!String.IsNullOrWhiteSpace(relativePathWithoutFile)) { Directory.CreateDirectory(Path.Combine(this.GamePath, relativePathWithoutFile)); } try { string destinationPath = Path.Combine(this.GamePath, relativePathWithFile); if (!this.AllInsertedFiles.Any(x => x.FullName == destinationPath)) { if (File.Exists(destinationPath)) { string backupFilePath = destinationPath + BackupFileExtension; if (File.Exists(backupFilePath)) { File.Delete(backupFilePath); } File.Move(destinationPath, backupFilePath); this.AllBackedUpFiles.Add(new FileInfo(backupFilePath)); } File.Move(fullPath, destinationPath); this.AllInsertedFiles.Add(new FileInfo(destinationPath)); } } catch (Exception ex) { if (ex is IOException || ex is UnauthorizedAccessException || ex is PathTooLongException) { return(false); } throw; } } return(true); }
public async Task <ScriptMod> CreateScriptMod(string name, int orderIndex, string description = null, bool isEnabled = true, bool createFolder = true) { ScriptMod newScriptMod = new ScriptMod { Name = name, Description = description, IsEnabled = isEnabled, IsInserted = false, OrderIndex = orderIndex }; if (createFolder) { if (Directory.Exists(Path.Combine(this.ModsRootFolder.FullName, name))) { int i = 1; string numberAppendedDir = Path.Combine(this.ModsRootFolder.FullName, String.Format("{0} ({1})", newScriptMod.Name, i)); while (Directory.Exists(numberAppendedDir)) { i++; numberAppendedDir = Path.Combine(this.ModsRootFolder.FullName, String.Format("{0} ({1})", newScriptMod.Name, i)); } Directory.CreateDirectory(numberAppendedDir); newScriptMod.Name = String.Format("{0} ({1})", newScriptMod.Name, i); } else { Directory.CreateDirectory(Path.Combine(this.ModsRootFolder.FullName, newScriptMod.Name)); } } await this.ModsDb.Connection.OpenAsync(); string sql = "INSERT into ScriptMod (name, description, isEnabled, isInserted, orderIndex) VALUES (@name, @description, @isEnabled, @isInserted, @orderIndex)"; SQLiteCommand command = new SQLiteCommand(sql, this.ModsDb.Connection); command.Parameters.AddWithValue("name", newScriptMod.Name); command.Parameters.AddWithValue("description", newScriptMod.Description); command.Parameters.AddWithValue("isEnabled", Convert.ToInt32(newScriptMod.IsEnabled)); command.Parameters.AddWithValue("isInserted", Convert.ToInt32(newScriptMod.IsInserted)); command.Parameters.AddWithValue("orderIndex", newScriptMod.OrderIndex); await command.ExecuteNonQueryAsync(); sql = "SELECT last_insert_rowid()"; command = new SQLiteCommand(sql, this.ModsDb.Connection); newScriptMod.Id = (int)(long)(await command.ExecuteScalarAsync()); this.ModsDb.Connection.Close(); return(newScriptMod); }
public async Task UpdateScriptModFilesWithPath(int scriptModId, IList <string> newFilesWithPath) { await this.ModsDb.Connection.OpenAsync(); string sql = "UPDATE ScriptMod SET filesWithPath=@filesWithPath WHERE id=@id"; SQLiteCommand command = new SQLiteCommand(sql, this.ModsDb.Connection); command.Parameters.AddWithValue("filesWithPath", ScriptMod.FilesWithPathListToJson(newFilesWithPath)); command.Parameters.AddWithValue("id", scriptModId); await command.ExecuteNonQueryAsync(); this.ModsDb.Connection.Close(); }
public async Task <ScriptMod> GetScriptModById(int scriptModId) { await this.ModsDb.Connection.OpenAsync(); string sql = "SELECT * FROM ScriptMod WHERE id=@id"; SQLiteCommand command = new SQLiteCommand(sql, this.ModsDb.Connection); command.Parameters.AddWithValue("id", scriptModId); ScriptMod resultScriptMod = new ScriptMod(); DbDataReader reader = await command.ExecuteReaderAsync(); if (!reader.HasRows) { this.ModsDb.Connection.Close(); return(null); } while (await reader.ReadAsync()) { resultScriptMod.Id = (int)(long)reader["id"]; resultScriptMod.Name = (string)reader["name"]; resultScriptMod.Description = (string)reader["description"]; resultScriptMod.IsEnabled = Convert.ToBoolean((int)reader["isEnabled"]); resultScriptMod.IsInserted = Convert.ToBoolean((int)reader["isInserted"]); List <string> filesWithPath = null; if (reader["filesWithPath"] is string filesWithPathJson) { filesWithPath = JsonConvert.DeserializeObject <List <string> >(filesWithPathJson); } resultScriptMod.FilesWithPath = filesWithPath; } this.ModsDb.Connection.Close(); return(resultScriptMod); }
public static bool MoveScriptModBack(ScriptMod scriptMod, string gamePath) { string modRootFullPath = Path.Combine(SettingsHandler.ModsDirectory, "Script Mods", scriptMod.Name); foreach (string modFilePath in scriptMod.FilesWithPath) { string fileFullPath = Path.Combine(gamePath, modFilePath); if (File.Exists(fileFullPath)) { try { File.Move(Path.Combine(gamePath, modFilePath), Path.Combine(modRootFullPath, modFilePath)); } catch (Exception ex) { if (ex is IOException || ex is UnauthorizedAccessException || ex is PathTooLongException) { return(false); } throw; } } } return(true); }
public async Task <bool> UpdateScriptModName(int scriptModId, string newName) { ScriptMod oldScriptMod = await this.GetScriptModById(scriptModId); string oldDirectoryName = oldScriptMod.Name; try { Directory.Move(Path.Combine(this.ModsRootFolder.FullName, oldDirectoryName), Path.Combine(this.ModsRootFolder.FullName, newName)); } catch (Exception ex) { if (ex is IOException || ex is UnauthorizedAccessException) { return(false); } throw; } await this.ModsDb.Connection.OpenAsync(); string sql = "UPDATE ScriptMod SET name=@name WHERE id=@id"; SQLiteCommand command = new SQLiteCommand(sql, this.ModsDb.Connection); command.Parameters.AddWithValue("name", newName); command.Parameters.AddWithValue("id", scriptModId); await command.ExecuteNonQueryAsync(); this.ModsDb.Connection.Close(); return(true); }
public async Task <List <ScriptMod> > GetAllScriptMods() { List <ScriptMod> allScriptMods = new List <ScriptMod>(); List <int> modsByIdWithNoFolders = new List <int>(); await this.ModsDb.Connection.OpenAsync(); string sql = "SELECT * FROM ScriptMod ORDER BY orderIndex ASC"; SQLiteCommand command = new SQLiteCommand(sql, this.ModsDb.Connection); DbDataReader reader = await command.ExecuteReaderAsync(); if (!reader.HasRows) { this.ModsDb.Connection.Close(); return(null); } while (await reader.ReadAsync()) { int id = (int)(long)reader["id"]; string name = (string)reader["name"]; string description = (string)reader["description"]; bool isEnabled = Convert.ToBoolean((int)reader["isEnabled"]); bool isInserted = Convert.ToBoolean((int)reader["isInserted"]); int orderIndex = (int)reader["orderIndex"]; List <string> filesWithPath = null; if (reader["filesWithPath"] is string filesWithPathJson) { filesWithPath = JsonConvert.DeserializeObject <List <string> >(filesWithPathJson); } if (!Directory.Exists(Path.Combine(this.ModsRootFolder.FullName, name))) { modsByIdWithNoFolders.Add(id); } else { ScriptMod scriptMod = new ScriptMod(); allScriptMods.Add(new ScriptMod() { Id = id, Name = name, Description = description, IsEnabled = isEnabled, IsInserted = isInserted, FilesWithPath = filesWithPath, OrderIndex = orderIndex }); } } this.ModsDb.Connection.Close(); if (modsByIdWithNoFolders.Count != 0) { foreach (int scriptModId in modsByIdWithNoFolders) { await this.RemoveAndDeleteScriptMod(scriptModId, false); } } DirectoryInfo[] allDirsInsideModsDir = this.ModsRootFolder.GetDirectories(); if (allDirsInsideModsDir.Length != allScriptMods.Count) { foreach (DirectoryInfo dir in allDirsInsideModsDir) { bool found = allScriptMods.Any(modFolder => modFolder.Name == dir.Name); if (!found) { allScriptMods.Add(await this.CreateScriptMod(dir.Name, allScriptMods.Count - 1, "* This modification was not recognized. It could have been added outside of this application, or it was an existing modification which has been renamed outside of this application. *", false, false)); } } } return(allScriptMods); }