Skip to content

Commit 5f3118f

Browse files
authored
Merge pull request #612 from HnZGaming/feat/dotnetenv
`.env` file support
2 parents 1a42791 + ea25271 commit 5f3118f

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎Torch/Torch.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@
272272
<Compile Include="Managers\MultiplayerManagerBase.cs" />
273273
<Compile Include="Persistent.cs" />
274274
<Compile Include="Plugins\PluginManifest.cs" />
275+
<Compile Include="Utils\DotEnv.cs" />
275276
<Compile Include="Utils\ModItemUtils.cs" />
276277
<Compile Include="Utils\SteamWorkshopTools\KeyValueExtensions.cs" />
277278
<Compile Include="Utils\MiscExtensions.cs" />

‎Torch/TorchBase.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public abstract class TorchBase : ViewModel, ITorchBase, IPlugin
6565
{
6666
static TorchBase()
6767
{
68+
DotEnv.Load();
69+
6870
MyVRageWindows.Init("SpaceEngineersDedicated", MySandboxGame.Log, null, false);
6971
ReflectedManager.Process(typeof(TorchBase).Assembly);
7072
ReflectedManager.Process(typeof(ITorchBase).Assembly);

‎Torch/Utils/DotEnv.cs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Text;
5+
using NLog;
6+
7+
namespace Torch.Utils
8+
{
9+
public static class DotEnv
10+
{
11+
static readonly ILogger Log = LogManager.GetCurrentClassLogger();
12+
13+
public static void Load()
14+
{
15+
const string filePath = ".env";
16+
if (!File.Exists(filePath)) return;
17+
18+
var txt = File.ReadAllText(filePath, Encoding.Default);
19+
var newline = new[] { "\r\n", Environment.NewLine };
20+
var equal = new[] { "=" };
21+
foreach (var line in txt.Split(newline, StringSplitOptions.RemoveEmptyEntries))
22+
{
23+
var pair = line.Split(equal, StringSplitOptions.None);
24+
if (pair.Length == 0) continue;
25+
26+
var key = pair[0].Trim();
27+
if (pair.Length == 1)
28+
{
29+
Environment.SetEnvironmentVariable(key, null);
30+
Log.Info($"{key} (no value)");
31+
continue;
32+
}
33+
34+
var value = pair[1].Trim();
35+
Log.Info($"{key} = {Mask(value)} (masked)");
36+
Environment.SetEnvironmentVariable(key, value);
37+
}
38+
}
39+
40+
static string Mask(string value)
41+
{
42+
return string.Join("", Enumerable.Repeat('*', value.Length));
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)