Switching project to C# (because why not)

This commit is contained in:
Aurélie Delhaie
2022-05-19 18:49:04 +02:00
commit b013815a96
44 changed files with 18946 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenSaveCloudClient.Models
{
public class AsyncTaskInformation
{
private readonly string label;
private readonly int max;
private int progress;
private AsyncTaskStatus status;
public string Label { get { return label; } }
public int Max { get { return max; } }
public int Progress { get { return progress; } set { progress = value; } }
public AsyncTaskStatus Status { get { return status; } set { status = value; } }
public AsyncTaskInformation(string label, int max)
{
this.label = label;
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 OpenSaveCloudClient.Models
{
public enum AsyncTaskStatus
{
Running,
Failed,
Stopped,
Ended
}
}

View File

@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
namespace OpenSaveCloudClient.Models
{
public class Configuration
{
private static Configuration? instance;
private Dictionary<string, string> _values;
private Configuration()
{
_values = new Dictionary<string, string>();
Load();
}
public static Configuration GetInstance()
{
if (instance == null)
{
instance = new Configuration();
}
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,90 @@
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;
namespace OpenSaveCloudClient.Models
{
public class GameSave
{
private int id;
private string uuid;
private readonly string name;
private readonly string folderPath;
private readonly string description;
private string hash;
private readonly string? coverPath;
private readonly int revision;
private bool synced;
private bool localOnly;
public int 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; } }
public string Hash { get { return hash; } }
public string? CoverPath { get { return coverPath; } }
public int Revision { get { return revision; } }
public bool Synced { get { return synced; } set { synced = value; } }
public bool LocalOnly { get { return localOnly; } set { localOnly = 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;
localOnly = true;
}
[JsonConstructor]
public GameSave(int id, string uuid, string name, string folderPath, string description, string hash, string? coverPath, int revision, bool synced, bool localOnly)
{
this.id = id;
this.uuid = uuid;
this.name = name;
this.folderPath = folderPath;
this.description = description;
this.hash = hash;
this.coverPath = coverPath;
this.revision = revision;
this.synced = synced;
this.localOnly = localOnly;
}
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);
}
ZipFile.CreateFromDirectory(folderPath, archivePath, CompressionLevel.SmallestSize, true);
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(archivePath))
{
var h = md5.ComputeHash(stream);
hash = BitConverter.ToString(h).Replace("-", "").ToLowerInvariant();
}
}
}
}
}

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 OpenSaveCloudClient.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 OpenSaveCloudClient.Models.Remote
{
public class Credential
{
[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 OpenSaveCloudClient.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 OpenSaveCloudClient.Models.Remote
{
public class TokenValidation
{
[JsonPropertyName("valid")]
public bool Valid { get; set; }
}
}