forked from CBLoader/CBLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StartupFlags.cs
177 lines (166 loc) · 7.65 KB
/
StartupFlags.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace CharacterBuilderLoader
{
public class StartupFlags
{
public const string CONFIG_FILENAME = "default.cbconfig";
public bool LoadExec { get; set; }
public bool ForcedReload { get; set; }
public bool PatchFile { get; set; }
public bool Mergelater { get; set; }
public bool UpdateFirst { get; set; }
public bool CheckForUpdates { get; set; }
private static readonly XmlSerializer configSerializer = new XmlSerializer(typeof(SettingsType));
public StartupFlags()
{
LoadExec = true;
ForcedReload = false;
PatchFile = false;
Mergelater = false;
UpdateFirst = false;
CheckForUpdates = true;
}
/// <summary>
/// Parses the command line arguments and sets the necessary state flags across the applicaiton.
/// Returns a structure of startup flags to the caller.
/// </summary>
/// <returns>A structure containging flags important to how the application should load</returns>
public bool ParseCmdArgs(string[] args, FileManager fm)
{
if (args != null && args.Length > 0)
{
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "-e": this.ForcedReload = true; break;
case "-n": this.LoadExec = false; break;
case "-v": Log.VerboseMode = true; break;
case "-p": this.PatchFile = true; break;
case "-a": Utils.UpdateRegistry(); break;
case "-u":
FileManager.BasePath = getArgString(args, ref i);
if (!Directory.Exists(FileManager.BasePath))
Directory.CreateDirectory(FileManager.BasePath);
break;
case "-r":
FileManager.KeyFile = getArgString(args, ref i);
Utils.ExtractKeyFile(FileManager.KeyFile);
break;
case "-k":
FileManager.KeyFile = getArgString(args, ref i);
break;
case "-f":
fm.AddCustomFolder(getArgString(args, ref i));
break;
case "-c": // Load a different config file.
LoadFromConfig(fm,getArgString(args, ref i));
break;
case "-?":
case "-h":
Program.DisplayHelp();
return false;
// Fast Mode
case "+fm":
this.Mergelater = File.Exists(FileManager.MergedPath); break;
case "-fm":
this.Mergelater = false; break;
case "+d":
this.UpdateFirst = true;
this.LoadExec = false;
break;
case "-d":
CheckForUpdates = false;
break;
default:
if (File.Exists(args[i])) // Otherwise we lose the quotes, and Character builder can't find the file. (if there's whitespace in the path)
ProcessManager.EXECUTABLE_ARGS += " \"" + args[i] + "\"";
else
ProcessManager.EXECUTABLE_ARGS += " " + args[i]; // character Builder has args as well. Lets pass unknown ones along.
break;
}
}
}
return true;
}
/// <summary>
/// simple helper for safely pulling a string argument out of an args list
/// </summary>
private static string getArgString(string[] args, ref int i)
{
if (args.Length > i + 1)
return args[++i];
else
{
Program.DisplayHelp();
throw new FormatException("Invalid Arguments Specified");
}
}
public bool LoadFromConfig(FileManager fm)
{
return LoadFromConfig(fm, CONFIG_FILENAME);
}
// The whole point of not using app.config was that we could have more than one.
public bool LoadFromConfig(FileManager fm, string ConfigFile)
{
string fileName;
fileName = ConfigFile;
if (!File.Exists(fileName))
fileName = Path.Combine(FileManager.BasePath, ConfigFile);
if (!File.Exists(fileName))
fileName = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),ConfigFile);
if (!File.Exists(fileName))
return false;
Log.Debug("Loading Config File: " + fileName);
try
{
SettingsType settings;
using (StreamReader sr = new StreamReader(fileName))
{
settings = (SettingsType)configSerializer.Deserialize(sr);
}
if (settings.Folders != null)
foreach (string customFolder in settings.Folders)
fm.AddCustomFolder(customFolder);
if (settings.Ignore != null)
foreach (string ignoredPart in settings.Ignore)
fm.IgnoredParts.Add(ignoredPart.ToLower().Trim());
if (settings.AlwaysRemergeSpecified)
this.ForcedReload = settings.AlwaysRemerge;
if (!String.IsNullOrEmpty(settings.BasePath))
{
FileManager.BasePath = Environment.ExpandEnvironmentVariables(settings.BasePath);
if (!Directory.Exists(FileManager.BasePath))
Directory.CreateDirectory(FileManager.BasePath);
}
if (!String.IsNullOrEmpty(settings.CBPath))
Environment.CurrentDirectory = Environment.ExpandEnvironmentVariables(settings.CBPath);
if (settings.FastModeSpecified)
this.Mergelater = settings.FastMode;
if (!String.IsNullOrEmpty(settings.KeyFile))
FileManager.KeyFile = Environment.ExpandEnvironmentVariables(settings.KeyFile);
if (settings.VerboseModeSpecified)
Log.VerboseMode = settings.VerboseMode;
if (settings.UpdateFirstSpecified)
this.UpdateFirst = settings.UpdateFirst;
if (settings.LaunchBuilderSpecified)
this.LoadExec = settings.LaunchBuilder;
if (settings.NewMergeLogicSpecified)
fm.UseNewMergeLogic = settings.NewMergeLogic;
if (settings.ShowChangelogSpecified)
UpdateLog.ShowChangelog = settings.ShowChangelog;
}
catch (Exception e)
{
Log.Error("Error Loading Config File", e);
return false;
}
return true;
}
}
}