Repository navigation
Expand file tree
/
Copy pathConfig.cs
More file actions
97 lines (86 loc) · 3.09 KB
/
Copy pathConfig.cs
File metadata and controls
97 lines (86 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using GTA;
using System;
using System.IO;
namespace iFruitAddon2
{
/// <summary>
/// Loads and manages the configuration settings.
/// </summary>
internal class Config
{
/// <summary>
/// Object to hold the configuration settings.
/// </summary>
private ScriptSettings _config;
/// <summary>
/// Full path to the mod config file.
/// </summary>
private static readonly string _configFile = Path.Combine(Tools.GetiFruitAddonDirectory(), "config.ini");
public Config()
{
// Initialize the configuration settings
LoadConfigFile();
}
/// <summary>
/// Returns the contact start index from the configuration.
/// It is used to determine where the first contact will be added in the phone.
/// </summary>
internal int GetContactStartIndex()
{
// Loading contact Index
Logger.Debug("Reading contact index...");
// Ensure index is positive
int _value = Math.Max(_config.GetValue<int>("General", "StartIndex", 40), 0);
Logger.Debug($"Contact config start index: {_value}");
return _value;
}
/// <summary>
/// Loads configuration values from the configuration file.
/// </summary>
private void LoadConfigFile()
{
try
{
// Ensure the mod directory exists
EnsureMainDirectoryExists();
// Ensuring that the config file is loaded. If not, create with default values.
if (!File.Exists(_configFile))
{
File.WriteAllText(_configFile, Properties.Resources.config);
Logger.Debug("Config file did not exist; one has been created using defaults.");
}
Logger.Debug($"Loading configuration from {_configFile}...");
_config = ScriptSettings.Load(_configFile);
}
catch (Exception ex)
{
Logger.Error($"Failed to load configuration from {_configFile}: {ex.Message}");
}
}
/// <summary>
/// Ensures that the main mod directory exists, creating it if necessary.
/// </summary>
/// <remarks>If the directory does not exist, it is created.</remarks>
private void EnsureMainDirectoryExists()
{
string mainDir = Tools.GetiFruitAddonDirectory();
try
{
if (!Directory.Exists(mainDir))
{
Directory.CreateDirectory(mainDir);
Logger.Debug($"Created directory {mainDir} due to nonexistence.");
}
else
{
Logger.Debug($"Directory {mainDir} already exists. Skipping creation...");
}
}
catch (Exception ex)
{
Logger.Error($"Error ensuring {nameof(mainDir)} existence: {ex.Message}");
throw;
}
}
}
}