Skip to content

[Improvements] Settings and language management #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions Aura Operating System/Aura OS/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using System;
using Cosmos.System.FileSystem;
using Sys = Cosmos.System;
using Lang = Aura_OS.System.Translation;
using Aura_OS.System.Translation;
using Aura_OS.System;
using Aura_OS.System.Users;
using Aura_OS.System.Computer;
Expand Down Expand Up @@ -44,6 +44,7 @@ public class Kernel : Sys.Kernel
public static bool Logged = false;
public static string ComputerName = "aura-pc";
public static string UserDir = @"0:\Users\" + userLogged + "\\";
public static string SystemDir = @"0:\System\";
public static bool SystemExists = false;
public static bool JustInstalled = false;
public static CosmosVFS vFS = new CosmosVFS();
Expand All @@ -55,7 +56,8 @@ public class Kernel : Sys.Kernel
public static Config LocalNetworkConfig;
public static Debugger debugger;
public static string current_volume = @"0:\";

public static LangManager Lang = new LangManager();

#endregion

#region Before Run
Expand All @@ -74,8 +76,11 @@ protected override void BeforeRun()
{
try
{
// initialize languages we support
addLanguages();

CommandManager.RegisterAllCommands();

//AConsole = new System.Shell.VGA.VGAConsole(null);

Encoding.RegisterProvider(CosmosEncodingProvider.Instance);
Expand Down Expand Up @@ -114,12 +119,12 @@ protected override void BeforeRun()
if (!JustInstalled)
{

Settings config = new Settings(@"0:\System\settings.conf");
Settings config = new Settings("settings.conf");
langSelected = config.GetValue("language");

#region Language

Lang.Keyboard.Init();
Keyboard.Init();

#endregion

Expand All @@ -145,6 +150,17 @@ protected override void BeforeRun()
Crash.StopKernel(ex);
}
}

/// <summary>
/// Adds all the languages that we support
/// </summary>
protected void addLanguages()
{
Lang.addLang("en_US", new Lang("en_US"));
Lang.addLang("fr_FR", new Lang("fr_FR"));
Lang.addLang("nl_NL", new Lang("nl_NL"));
Lang.addLang("it_IT", new Lang("it_IT")):
}

#endregion

Expand Down
47 changes: 47 additions & 0 deletions Aura Operating System/Aura OS/System/Translation/Lang.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace Aura_OS.System.Translation
{
public class Lang
{
public Dictionary<string, string> lines = new Dictionary<string, string>();

private string name;
private string path;

public Lang(string name)
{
this.name = name;
path = @"0:\System\langs\" + name + ".conf";

if (Kernel.SystemExists)
{
if (File.Exists(path))
{
Load();
}
}
}

public void Load()
{
lines = File.ReadLines(this.path).Where(IsConfigurationLine).Select(line => line.Split('=')).ToDictionary(line => line[0], line => line[1]);
}

private static bool IsConfigurationLine(string line)
{
return !line.StartsWith("#") && line.Contains("=");
}

public string Get(string line)
{
string value;
if (lines.TryGetValue(line, out value)) {
return value;
} else {
return null;
}
}
}
37 changes: 37 additions & 0 deletions Aura Operating System/Aura OS/System/Translation/LangManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;

namespace Aura_OS.System.Translation
{
public class LangManager
{
private Dictionary<string, Lang> Languages = new Dictionary<string, Lang>();

private string DefaultLang = "en_US";

public void addLang(string name, Lang lang)
{
if (!Languages.ContainsKey(name))
{
Languages.Add(name, lang);
}
}

public Lang GetLang(string name)
{
Lang value;
if (Languages.TryGetValue(name, out value))
{
return value;
}
else
{
return Languages[DefaultLang];
}
}

public Dictionary<string, Lang> getLangs()
{
return Languages;
}
}
}
131 changes: 131 additions & 0 deletions Aura Operating System/Aura OS/System/Utils/ConfSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace Aura_OS.System.Utils
{
public class ConfSettings : ISettings
{

private Dictionary<string, string> config = new Dictionary<string, string>();
private string path;

public void Load(string path)
{
this.path = path;

if (Kernel.SystemExists)
{
if (File.Exists(path))
{
config = File.ReadLines(path).Where(IsConfigurationLine).Select(line => line.Split('=')).ToDictionary(line => line[0], line => line[1]);
}
}
}

private static bool IsConfigurationLine(string line)
{
return !line.StartsWith("#") && line.Contains("=");
}

public void Save()
{
if (Kernel.SystemExists)
{
string[] fileContent = File.ReadAllLines(path);
List<string> tempConfig = new List<string>();

int counter = -1;
int index = 0;
bool exists = false;

foreach (string line in fileContent)
{
tempConfig.Add(line);
}

// loop throuh dictonary
foreach (var setting in config)
{
// see if the current setting exists in the config file
foreach(string element in tempConfig)
{
counter = counter + 1;
if (element.Contains(setting.Key))
{
index = counter;
exists = true;
}
}

if (exists)
{
// update the value
tempConfig[index] = setting.Key + "=" + setting.Value;
}
else
{
// add to temp config
tempConfig.Add(setting.Key + "=" + setting.Value);
}
}

// now update file content and save
fileContent = tempConfig.ToArray();
File.WriteAllLines(path, fileContent);
}
}

public string GetString(string key)
{
string value;
if (config.TryGetValue(key, out value))
{
return value;
}

return null;
}

public void SetString(string key, string value)
{
config[key] = value;
}

public bool? GetBool(string key)
{
return GetString(key) != null ? bool.Parse(GetString(key)) : new bool?();
}

public void SetBool(string key, bool value)
{
config[key] = value.ToString();
}

public int? GetInt(string key)
{
return GetString(key) != null ? int.Parse(GetString(key)) : new int?();
}

public void SetInt(string key, int value)
{
config[key] = value.ToString();
}

public long? GetLong(string key)
{
return GetString(key) != null ? long.Parse(GetString(key)) : new long?();
}

public void SetLong(string key, long value)
{
config[key] = value.ToString();
}

public IDictionary<string, string> getConfig()
{
return config;
}

}
}
29 changes: 29 additions & 0 deletions Aura Operating System/Aura OS/System/Utils/ISettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;

namespace Aura_OS.System.Utils
{
public interface ISettings
{
void Load(string path);

void Save();

string GetString(string key);

void SetString(string key, string value);

bool? GetBool(string key);

void SetBool(string key, bool value);

int? GetInt(string key);

void SetInt(string key, int value);

long? GetLong(string key);

void SetLong(string key, long value);

IDictionary<string, string> getConfig();
}
}
Loading