-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathApplicationParametersParser.cs
More file actions
147 lines (117 loc) · 5.53 KB
/
Copy pathApplicationParametersParser.cs
File metadata and controls
147 lines (117 loc) · 5.53 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using CodeLess.Interfaces;
using DCL.Diagnostics;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using UnityEngine;
namespace Global.AppArgs
{
[AutoInterface]
public class ApplicationParametersParser : IAppArgs
{
private readonly Dictionary<string, string> appParameters = new ();
private static readonly IReadOnlyDictionary<string, string> ALWAYS_IN_EDITOR = new Dictionary<string, string>
{
[AppArgsFlags.DEBUG] = string.Empty,
};
public ApplicationParametersParser() : this(Environment.GetCommandLineArgs()) { }
public ApplicationParametersParser(string[] args) : this(true, args) { }
public ApplicationParametersParser(bool useInEditorFlags = true, params string[] args)
{
ParseApplicationParameters(args);
if (useInEditorFlags && Application.isEditor)
AddAlwaysInEditorFlags();
LogArguments();
}
public bool HasFlag(string flagName) =>
appParameters.ContainsKey(flagName);
public bool TryGetValue(string flagName, out string? value) =>
appParameters.TryGetValue(flagName, out value);
public IEnumerable<string> Flags() =>
appParameters.Keys;
public IReadOnlyDictionary<string, string> Args() =>
appParameters;
private void AddAlwaysInEditorFlags()
{
foreach ((string? key, string? value) in ALWAYS_IN_EDITOR)
appParameters.TryAdd(key, value);
}
private void ParseApplicationParameters(string[] cmdArgs)
{
var deepLinkFound = false;
string lastKeyStored = string.Empty;
foreach (string arg in cmdArgs)
{
if (arg.StartsWith("--"))
{
if (arg.Length > 2)
{
lastKeyStored = arg.Substring(2);
appParameters[lastKeyStored] = string.Empty;
}
else
lastKeyStored = string.Empty;
}
else if (!deepLinkFound && arg.StartsWith("decentraland://"))
{
deepLinkFound = true;
lastKeyStored = string.Empty;
// Application parameters may come embedded in a deep link:
// Example (Windows) -> start decentraland://"realm=http://127.0.0.1:8000&position=100,100&local-scene=true&otherparam=blahblah"
Dictionary<string, string> deepLinkParameters = ProcessDeepLinkParameters(arg);
foreach ((string key, string value) in deepLinkParameters)
appParameters[key] = value;
}
else if (!string.IsNullOrEmpty(lastKeyStored))
appParameters[lastKeyStored] = arg;
}
}
public static Dictionary<string, string> ProcessDeepLinkParameters(string deepLinkString)
{
var output = new Dictionary<string, string>();
// Update deep link so that Uri class allows the host name
deepLinkString = Regex.Replace(deepLinkString, @"^decentraland:/+", "https://decentraland.org/?");
if (!Uri.TryCreate(deepLinkString, UriKind.Absolute, out Uri? _)) return output;
var uri = new Uri(deepLinkString);
NameValueCollection uriQuery = HttpUtility.ParseQueryString(uri.Query);
foreach (string uriQueryKey in uriQuery.AllKeys)
{
// if the deep link is not constructed correctly (AKA 'decentraland://?&blabla=blabla') a 'null' parameter can be detected...
if (uriQueryKey == null) continue;
output[uriQueryKey] = uriQuery.Get(uriQueryKey);
}
if (output.TryGetValue(AppArgsFlags.REALM, out string? realmParamValue))
{
// Patch for WinOS sometimes affecting the 'realm' parameter in deep links putting a '/' at the end
if (realmParamValue.EndsWith('/'))
realmParamValue = realmParamValue.Remove(realmParamValue.Length - 1);
// Patch for MacOS removing the ':' from the realm parameter protocol
realmParamValue = Regex.Replace(realmParamValue, @"(https?)//(.*?)$", @"$1://$2");
output[AppArgsFlags.REALM] = realmParamValue;
}
// Patch for WinOS sometimes affecting the 'skip-auth-screen' parameter in deep links putting a '/' at the end
if (output.TryGetValue(AppArgsFlags.SKIP_AUTH_SCREEN, out string? value) && value?.EndsWith('/') == true)
output[AppArgsFlags.SKIP_AUTH_SCREEN] = value[..^1];
return output;
}
private void LogArguments()
{
const int COUNT_PER_LINE = 7;
var sb = new StringBuilder(COUNT_PER_LINE * appParameters.Count);
var count = 1;
sb.AppendLine("==================");
sb.AppendLine("Application arguments:");
sb.AppendLine("==================\n");
foreach ((string? key, string? value) in appParameters)
{
sb.Append("Arg ").Append(count).Append(": ").Append(key).Append(" = ").Append(value).Append("\n");
count++;
}
sb.AppendLine("==================\n");
ReportHub.LogProductionInfo(sb.ToString());
}
}
}