public static bool MoveAssetModBack(AssetMod assetMod, string gamePath) { string modRootFullPath = Path.Combine(SettingsHandler.ModsDirectory, "Asset Mods"); string fullRpfPath = Path.Combine(gamePath, "mods", assetMod.TargetRPF.Substring(1)); if (!File.Exists(fullRpfPath)) { return(false); } try { File.Move(fullRpfPath, Path.Combine(modRootFullPath, assetMod.TargetRPF.Substring(1))); } catch (Exception ex) { if (ex is IOException || ex is UnauthorizedAccessException || ex is PathTooLongException) { return(false); } throw; } return(true); }
public async Task <AssetMod> GetAssetModById(int assetModId) { await this.ModsDb.Connection.OpenAsync(); string sql = "SELECT * FROM AssetMod WHERE id=@id"; SQLiteCommand command = new SQLiteCommand(sql, this.ModsDb.Connection); command.Parameters.AddWithValue("id", assetModId); AssetMod resultAssetMod = new AssetMod(); DbDataReader reader = await command.ExecuteReaderAsync(); if (!reader.HasRows) { this.ModsDb.Connection.Close(); return(null); } while (await reader.ReadAsync()) { resultAssetMod.Id = (int)(long)reader["id"]; resultAssetMod.Name = (string)reader["name"]; resultAssetMod.Description = (string)reader["description"]; resultAssetMod.IsEnabled = Convert.ToBoolean((int)reader["isEnabled"]); resultAssetMod.IsInserted = Convert.ToBoolean((int)reader["isInserted"]); resultAssetMod.TargetRPF = (string)reader["targetRPF"]; resultAssetMod.IsUsableAssetMod = Convert.ToBoolean((int)reader["isUsableAssetMod"]); } this.ModsDb.Connection.Close(); return(resultAssetMod); }
public bool InsertAssetMod(AssetMod assetMod) { string modsPath = Path.Combine(SettingsHandler.ModsDirectory, "Asset Mods"); string targetRelativePath = assetMod.TargetRPF; targetRelativePath = targetRelativePath.Substring(1); // substring the prefix slash out for Path.Combine() string relativePathWithoutFile = Path.GetDirectoryName(targetRelativePath); if (!String.IsNullOrWhiteSpace(relativePathWithoutFile)) { Directory.CreateDirectory(Path.Combine(this.GamePath, "mods", relativePathWithoutFile)); } string targetFullPath = Path.Combine(this.GamePath, "mods", targetRelativePath); try { if (!this.AllInsertedFiles.Any(x => x.FullName == targetFullPath)) { if (File.Exists(targetFullPath)) { string backupFilePath = targetFullPath + BackupFileExtension; if (File.Exists(backupFilePath)) { File.Delete(backupFilePath); } File.Move(targetFullPath, backupFilePath); this.AllBackedUpFiles.Add(new FileInfo(backupFilePath)); } File.Move(Path.Combine(SettingsHandler.ModsDirectory, "Asset Mods", targetRelativePath), targetFullPath); this.AllInsertedFiles.Add(new FileInfo(targetFullPath)); } } catch (Exception ex) { if (ex is IOException || ex is UnauthorizedAccessException || ex is PathTooLongException) { return(false); } throw; } return(true); }
public async Task <AssetMod> CreateAssetMod(string name, string targetRPF, int orderIndex, string description = null, bool isEnabled = true, bool isUsableAssetMod = true) { AssetMod newAssetMod = new AssetMod { Name = name, Description = description, IsEnabled = isEnabled, IsInserted = false, TargetRPF = targetRPF, IsUsableAssetMod = isUsableAssetMod, OrderIndex = orderIndex }; if (isUsableAssetMod) { string relativePathWithoutFile = Path.GetDirectoryName(targetRPF.Substring(1)); if (!String.IsNullOrWhiteSpace(relativePathWithoutFile)) { Directory.CreateDirectory(Path.Combine(this.ModsRootFolder.FullName, relativePathWithoutFile)); } string fullModPackagePath = Path.Combine(this.ModsRootFolder.FullName, targetRPF.Substring(1)); File.Copy(Path.Combine(SettingsHandler.GTAVDirectory, targetRPF.Substring(1)), fullModPackagePath); await this.ModsDb.Connection.OpenAsync(); string sql = "INSERT into AssetMod (name, description, isEnabled, isInserted, targetRPF, isUsableAssetMod, orderIndex) VALUES (@name, @description, @isEnabled, @isInserted, @targetRPF, @isUsableAssetMod, @orderIndex)"; SQLiteCommand command = new SQLiteCommand(sql, this.ModsDb.Connection); command.Parameters.AddWithValue("name", newAssetMod.Name); command.Parameters.AddWithValue("description", newAssetMod.Description); command.Parameters.AddWithValue("isEnabled", Convert.ToInt32(newAssetMod.IsEnabled)); command.Parameters.AddWithValue("isInserted", Convert.ToInt32(newAssetMod.IsInserted)); command.Parameters.AddWithValue("targetRPF", newAssetMod.TargetRPF); command.Parameters.AddWithValue("isUsableAssetMod", Convert.ToInt32(newAssetMod.IsUsableAssetMod)); command.Parameters.AddWithValue("orderIndex", newAssetMod.OrderIndex); await command.ExecuteNonQueryAsync(); sql = "SELECT last_insert_rowid()"; command = new SQLiteCommand(sql, this.ModsDb.Connection); newAssetMod.Id = (int)(long)(await command.ExecuteScalarAsync()); this.ModsDb.Connection.Close(); } return(newAssetMod); }
public async Task <List <AssetMod> > GetAllAssetMods() { List <AssetMod> allAssetMods = new List <AssetMod>(); List <int> modsByIdWithNoFiles = new List <int>(); await this.ModsDb.Connection.OpenAsync(); string sql = "SELECT * FROM AssetMod 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"]); string targetRPF = (string)reader["targetRPF"]; bool isUsableAssetMod = Convert.ToBoolean((int)reader["isUsableAssetMod"]); int orderIndex = (int)reader["orderIndex"]; string fullModPackagePath = Path.Combine(this.ModsRootFolder.FullName, targetRPF.Substring(1)); if (!File.Exists(fullModPackagePath) && isUsableAssetMod && !IsInserted) { modsByIdWithNoFiles.Add(id); } else { AssetMod assetMod = new AssetMod(); allAssetMods.Add(new AssetMod() { Id = id, Name = name, Description = description, IsEnabled = isEnabled, IsInserted = IsInserted, TargetRPF = targetRPF, IsUsableAssetMod = isUsableAssetMod, OrderIndex = orderIndex }); } } this.ModsDb.Connection.Close(); if (modsByIdWithNoFiles.Count != 0) { foreach (int assetModId in modsByIdWithNoFiles) { await this.RemoveAndDeleteAssetMod(assetModId, false); } } return(allAssetMods); }