Created the Avalonia MVVM template, split the CLI into Avalonia.UI and Avalonia.Core

This commit is contained in:
2023-08-07 20:37:02 +05:30
parent 6b989db91b
commit 1e6ac3b1fc
22 changed files with 464 additions and 20 deletions
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>AutumnLauncher</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SQLite" Version="1.0.117" />
</ItemGroup>
</Project>
+25
View File
@@ -0,0 +1,25 @@
using System.Runtime.InteropServices;
namespace AutumnLauncher;
public static class Configuration
{
public static string DataDir { get; set; } = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.LocalApplicationData,
Environment.SpecialFolderOption.Create), "AutumnLauncher");
public static string ConfigDir { get; set; } = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData,
Environment.SpecialFolderOption.Create), "AutumnLauncher");
public static LauncherPlatform Platform { get; set; } = GetPlatform();
private static LauncherPlatform GetPlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return LauncherPlatform.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return LauncherPlatform.Linux;
return LauncherPlatform.Unsupported;
}
}
+55
View File
@@ -0,0 +1,55 @@
using System.Diagnostics;
namespace AutumnLauncher;
public static class ControlActions
{
public static void Run(int num)
{
try
{
var game = SqliteData.GetGameById(num);
Process.Start(game.GamePath ?? throw new InvalidOperationException());
}
catch (Exception ex)
{
Console.WriteLine($"Error running game: {ex.Message}");
}
}
public static void List()
{
var games = SqliteData.GetAllGames();
foreach (var game in games)
{
Console.WriteLine(game + "\n");
}
}
public static void Add(string? gameName, string? developer, string? date, string? genre, string? type, string? path)
{
SqliteData.AddGame(gameName, developer, date, genre, type, path);
}
public static void Remove(int num)
{
SqliteData.RemoveGame(num);
}
public static void Edit(int num, string? gameName = null, string? developer = null, string? date = null,
string? genre = null, string? type = null, string? path = null)
{
var game = SqliteData.GetGameById(num);
if (!string.IsNullOrWhiteSpace(gameName)) game.GameName = gameName;
if (!string.IsNullOrWhiteSpace(developer)) game.GameDeveloper = developer;
if (!string.IsNullOrWhiteSpace(date)) game.GameReleaseDate = date;
if (!string.IsNullOrWhiteSpace(genre)) game.GameGenre = genre;
if (!string.IsNullOrWhiteSpace(type)) game.GameType = type;
if (!string.IsNullOrWhiteSpace(path)) game.GamePath = path;
SqliteData.EditGame(game.GameId, game.GameName, game.GameDeveloper, game.GameReleaseDate, game.GameGenre,
game.GameType, game.GamePath);
}
}
+19
View File
@@ -0,0 +1,19 @@
namespace AutumnLauncher;
public class Game
{
public int GameId { get; set; }
public string? GameName { get; set; }
public string? GameDeveloper { get; set; }
public string? GameReleaseDate { get; set; }
public string? GameGenre { get; set; }
public string? GameType { get; set; }
public string? GamePath { get; set; }
public override string ToString()
{
return "Game ID: " + GameId + "\nGame Name: " + GameName + "\nGame Developer: " + GameDeveloper +
"\nGame Release Date: " + GameReleaseDate + "\nGame Genre: " + GameGenre +
"\nGame Type: " + GameType + "\nGame Path: " + GamePath;
}
}
+8
View File
@@ -0,0 +1,8 @@
namespace AutumnLauncher;
public enum LauncherPlatform
{
Unsupported,
Windows,
Linux
}
+159
View File
@@ -0,0 +1,159 @@
using System.Data.SQLite;
namespace AutumnLauncher;
public static class SqliteData
{
private static readonly string DataPath = Configuration.DataDir;
public static void CreateDatabase()
{
if (File.Exists(Path.Combine(Configuration.DataDir, "GameDB.sqlite"))) return;
Console.WriteLine("Creating Database...");
// Create a new SQLite database file
SQLiteConnection.CreateFile($"{DataPath}/GameDB.sqlite");
// Connect to the database
using var connection = new SQLiteConnection($"Data Source={DataPath}/GameDB.sqlite");
//var connection = new SQLiteConnection($"Data Source={DataPath}/GameDB.sqlite;Version=3;");
connection.Open();
// Create a Games table
const string createTableSql = "CREATE TABLE Games (GameID INTEGER PRIMARY KEY, GameName TEXT, Developer TEXT, ReleaseDate DATE, Genre TEXT, Type TEXT, FilePath TEXT)";
var createTableCmd = new SQLiteCommand(createTableSql, connection);
createTableCmd.ExecuteNonQuery();
// Close the database connection
connection.Close();
}
public static void AddGame(string? gameName, string? developer, string? date, string? genre, string? type,
string? path) // method for adding sample data
{
using var connection = new SQLiteConnection($"Data Source={DataPath}/GameDB.sqlite");
connection.Open();
// var command = connection.CreateCommand();
// Insert game data
const string insertSql = "INSERT INTO Games (GameName, Developer, ReleaseDate, Genre, Type, FilePath) VALUES (@GameName, @Developer, @ReleaseDate, @Genre, @Type, @FilePath)";
var insertGame = new SQLiteCommand(insertSql, connection);
insertGame.Parameters.AddWithValue("@GameName", gameName);
insertGame.Parameters.AddWithValue("@Developer", developer);
insertGame.Parameters.AddWithValue("@ReleaseDate", date);
insertGame.Parameters.AddWithValue("@Genre", genre);
insertGame.Parameters.AddWithValue("@Type", type);
insertGame.Parameters.AddWithValue("@FilePath", path);
insertGame.ExecuteNonQuery();
connection.Close();
}
public static void EditGame(int id, string? gameName, string? developer, string? date, string? genre, string? type,
string? path) // has to replace all at once
{
using var connection = new SQLiteConnection($"Data Source={DataPath}/GameDB.sqlite");
connection.Open();
const string editSql = "UPDATE Games SET GameName = @GameName, Developer = @Developer, ReleaseDate = @GameDate, " +
"Genre = @GameGenre, Type = @GameType, FilePath = @GamePath WHERE GameID = @Id";
var editGame = new SQLiteCommand(editSql, connection);
editGame.Parameters.AddWithValue("@Id", id);
editGame.Parameters.AddWithValue("@GameName", gameName);
editGame.Parameters.AddWithValue("@Developer", developer);
editGame.Parameters.AddWithValue("@GameDate", date);
editGame.Parameters.AddWithValue("@GameGenre", genre);
editGame.Parameters.AddWithValue("@GameType", type);
editGame.Parameters.AddWithValue("@GamePath", path);
editGame.ExecuteNonQuery();
connection.Close();
}
public static void RemoveGame(int? gameId)
{
using var connection = new SQLiteConnection($"Data Source={DataPath}/GameDB.sqlite");
connection.Open();
// var command = connection.CreateCommand();
// remove game data
const string insertSql = "DELETE FROM Games where GameID = @GameID";
var removeGame = new SQLiteCommand(insertSql, connection);
removeGame.Parameters.AddWithValue("@GameID", gameId);
removeGame.ExecuteNonQuery();
connection.Close();
}
public static List<Game> GetAllGames()
{
var games = new List<Game>();
using var connection = new SQLiteConnection($"Data Source={DataPath}/GameDB.sqlite");
connection.Open();
var command = connection.CreateCommand();
command.CommandText = @"SELECT * FROM Games";
using var reader = command.ExecuteReader();
while (reader.Read())
{
var game = new Game
{
GameId = reader.GetInt32(0),
GameName = reader.GetString(1),
GameDeveloper = reader.GetString(2),
GameReleaseDate = reader.GetString(3),
GameGenre = reader.GetString(4),
GameType = reader.GetString(5),
GamePath = reader.GetString(6)
};
games.Add(game);
}
return games;
}
// public static int NumOfGames()
// {
// using (var connection = new SQLiteConnection($"Data Source={DataPath}/GameDB.sqlite"))
// {
// connection.Open();
//
// var command = connection.CreateCommand();
// command.CommandText = @"SELECT COUNT(*) from Games";
//
// using (var reader = command.ExecuteReader())
// {
// while (reader.Read())
// {
// return reader.GetInt32(0);
// }
// }
// }
// return 0;
// }
public static Game GetGameById(int gameId)
{
using var connection = new SQLiteConnection($"Data Source={DataPath}/GameDB.sqlite");
connection.Open();
var command = connection.CreateCommand();
command.CommandText = @"SELECT * FROM Games WHERE GameID = @gameID LIMIT 1";
command.Parameters.AddWithValue("@gameID", gameId);
using var reader = command.ExecuteReader();
if (reader.Read())
return new Game
{
GameId = reader.GetInt32(0),
GameName = reader.GetString(1),
GameDeveloper = reader.GetString(2),
GameReleaseDate = reader.GetString(3),
GameGenre = reader.GetString(4),
GameType = reader.GetString(5),
GamePath = reader.GetString(6)
};
// Handle the case where no game was found with the specified ID
throw new Exception($"No game found with ID {gameId}");
}
}
+11
View File
@@ -0,0 +1,11 @@
namespace AutumnLauncher;
public class Startup
{
public void AppStart()
{
Directory.CreateDirectory(Configuration.DataDir);
Directory.CreateDirectory(Configuration.ConfigDir);
SqliteData.CreateDatabase();
}
}