Move core to external libs, Starting terminal crossplatform client

This commit is contained in:
Aurélie Delhaie
2023-01-08 21:45:42 +01:00
parent 40645a80a1
commit e5274702ab
56 changed files with 146 additions and 67 deletions

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models
{
public class AsyncTaskInformation
{
private readonly string label;
private int max;
private int progress;
private AsyncTaskStatus status;
private readonly string uuid;
private bool undefined;
public string Label { get { return label; } }
public int Max { get { return max; } set { max = value; } }
public int Progress { get { return progress; } set { progress = value; } }
public AsyncTaskStatus Status { get { return status; } set { status = value; } }
public bool Undefined { get { return undefined; } set { undefined = value; } }
public string Uuid { get { return uuid; } }
public AsyncTaskInformation(string uuid, string label, bool undefined, int max)
{
this.uuid = uuid;
this.label = label;
this.undefined = undefined;
this.max = max;
this.progress = 0;
status = AsyncTaskStatus.Running;
}
public int Add(int progress)
{
this.progress += progress;
return progress;
}
}
}

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models
{
public enum AsyncTaskStatus
{
Running,
Failed,
Stopped,
Ended
}
}

View File

@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
namespace OpenSaveCloudCore.Models
{
public class UserConfiguration
{
private static UserConfiguration? instance;
private Dictionary<string, string> _values;
private UserConfiguration()
{
_values = new Dictionary<string, string>();
Load();
}
public static UserConfiguration Instance
{
get
{
if (instance == null)
{
instance = new UserConfiguration();
}
return instance;
}
}
private void Load()
{
string appdata = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "osc");
string path = Path.Combine(appdata, "settings.json");
if (!File.Exists(appdata))
{
Directory.CreateDirectory(appdata);
}
if (File.Exists(path))
{
string json = File.ReadAllText(path);
var res = JsonSerializer.Deserialize<Dictionary<string, string>>(json);
if (res != null)
{
_values = res;
}
}
}
private void Save()
{
string appdata = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "osc");
string path = Path.Combine(appdata, "settings.json");
if (!File.Exists(appdata))
{
Directory.CreateDirectory(appdata);
}
string json = JsonSerializer.Serialize<Dictionary<string, string>>(_values);
File.WriteAllText(path, json);
}
public string GetString(string key, string defaultValue)
{
try
{
return _values[key];
}
catch (KeyNotFoundException)
{
return defaultValue;
}
}
public int GetInt(string key, int defaultValue)
{
try
{
string value = _values[key];
if (!Int32.TryParse(value, out int result))
{
throw new Exception("This value is not an Int32");
}
return result;
}
catch (KeyNotFoundException)
{
return defaultValue;
}
}
public Boolean GetBoolean(string key, bool defaultValue)
{
try
{
string value = _values[key];
if (!Boolean.TryParse(value, out bool result))
{
throw new Exception("This value is not a Boolean");
}
return result;
} catch (KeyNotFoundException)
{
return defaultValue;
}
}
public void SetValue(string key, object? value, bool autoFlush = false)
{
if (value == null)
{
_values.Remove(key);
}
else
{
_values[key] = value.ToString();
}
if (autoFlush)
{
Save();
}
}
public void Flush()
{
Save();
}
}
}

View File

@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.IO.Compression;
using System.Security.Cryptography;
using OpenSaveCloudCore.Core;
using System.Globalization;
namespace OpenSaveCloudCore.Models
{
public class GameSave
{
private long id;
private string uuid;
private readonly string name;
private string folderPath;
private readonly string description;
private string hash;
private string currentHash;
private readonly string? coverPath;
private long revision;
private bool synced;
public long Id { get { return id; } set { id = value; } }
public string Uuid { get { return uuid; } }
public string Name { get { return name; } }
public string Description { get { return description; } }
public string FolderPath { get { return folderPath; } set { folderPath = value; } }
public string Hash { get { return hash; } }
public string CurrentHash { get { return currentHash; } }
public string? CoverPath { get { return coverPath; } }
public long Revision { get { return revision; } set { revision = value; } }
public bool Synced { get { return synced; } set { synced = value; } }
public GameSave(string name, string description, string folderPath, string? coverPath, int revision)
{
Guid guid = Guid.NewGuid();
this.name = name;
this.uuid = guid.ToString();
this.description = description;
this.hash = "";
this.folderPath = folderPath;
this.coverPath = coverPath;
this.revision = revision;
synced = false;
}
[JsonConstructor]
public GameSave(long id, string uuid, string name, string folderPath, string description, string hash, string currentHash, string? coverPath, long revision, bool synced)
{
this.id = id;
this.uuid = uuid;
this.name = name;
this.folderPath = folderPath;
this.description = description;
this.hash = hash;
this.currentHash = currentHash;
this.coverPath = coverPath;
this.revision = revision;
this.synced = synced;
}
public void Archive()
{
string appdata = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "osc");
string cachePath = Path.Combine(appdata, "cache");
string archivePath = Path.Combine(cachePath, uuid + ".bin");
if (!File.Exists(appdata))
{
Directory.CreateDirectory(appdata);
}
if (!File.Exists(cachePath))
{
Directory.CreateDirectory(cachePath);
}
if (File.Exists(archivePath))
{
File.Delete(archivePath);
}
ZipFile.CreateFromDirectory(folderPath, archivePath, CompressionLevel.SmallestSize, false);
using (var md5 = MD5.Create())
{
using (FileStream stream = File.OpenRead(archivePath))
{
var h = md5.ComputeHash(stream);
hash = BitConverter.ToString(h).Replace("-", "").ToLowerInvariant();
}
}
}
public bool DetectChanges()
{
byte[]? hashBytes = HashTool.HashDirectory(FolderPath);
if (hashBytes == null)
{
throw new Exception(message: string.Format(CultureInfo.CurrentCulture, "failed to get hash of directory '{0}'", FolderPath));
}
currentHash = BitConverter.ToString(hashBytes).Replace("-", "");
if (currentHash != hash)
{
synced = false;
}
return (currentHash != hash);
}
public void UpdateHash()
{
hash = currentHash;
}
public bool PathExist()
{
return Directory.Exists(FolderPath);
}
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models
{
public enum LogSeverity
{
Error,
Warning,
Information
}
public class Log
{
public DateTime Timestamp { get; set; }
public string Message { get; set; }
public LogSeverity Severity { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class AccessToken
{
[JsonPropertyName("token")]
public string Token { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class Credential
{
[JsonPropertyName("username")]
public string Username { get; set; }
[JsonPropertyName("password")]
public string Password { get; set; }
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class Game
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("rev")]
public long Revision { get; set; }
[JsonPropertyName("hash")]
public string Hash { get; set; }
[JsonPropertyName("last_update")]
public string LastUpdate { get; set; }
[JsonPropertyName("available")]
public bool Available { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class GameUploadToken
{
[JsonPropertyName("upload_token")]
public string UploadToken { get; set; }
[JsonPropertyName("expire")]
public string Expire { get; set; }
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class HttpError
{
[JsonPropertyName("status")]
public int Status { get; set; }
[JsonPropertyName("timestamp")]
public string Timestamp { get; set; }
[JsonPropertyName("error")]
public string Error { get; set; }
[JsonPropertyName("message")]
public string Message { get; set; }
[JsonPropertyName("path")]
public string Path { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class LockError
{
[JsonPropertyName("message")]
public string Message { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class NewGameInfo
{
[JsonPropertyName("name")]
public string Name { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class NewPassword
{
[JsonPropertyName("password")]
public string Password { get; set; }
[JsonPropertyName("verify_password")]
public string VerifyPassword { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class Registration
{
[JsonPropertyName("username")]
public string Username { get; set; }
[JsonPropertyName("password")]
public string Password { get; set; }
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class ServerInformation
{
[JsonPropertyName("allow_register")]
public bool AllowRegister { get; set; }
[JsonPropertyName("version")]
public string Version { get; set; }
[JsonPropertyName("api_version")]
public int ApiVersion { get; set; }
[JsonPropertyName("go_version")]
public string GoVersion { get; set; }
[JsonPropertyName("os_name")]
public string OsName { get; set; }
[JsonPropertyName("os_architecture")]
public string OsArchitecture { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class TokenValidation
{
[JsonPropertyName("valid")]
public bool Valid { get; set; }
}
}

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class UpdateUsername
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("username")]
public string Username { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class UploadGameInfo
{
[JsonPropertyName("game_id")]
public long GameId { get; set; }
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace OpenSaveCloudCore.Models.Remote
{
public class User
{
[JsonPropertyName("id")]
public long Id { get; set; }
[JsonPropertyName("username")]
public string Username { get; set; }
[JsonPropertyName("role")]
public string Role { get; set; }
[JsonPropertyName("is_admin")]
public bool IsAdmin { get; set; }
}
}