-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathProgramConstants.cs
More file actions
152 lines (120 loc) · 5.96 KB
/
ProgramConstants.cs
File metadata and controls
152 lines (120 loc) · 5.96 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
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
using Rampastring.Tools;
using ClientCore.Extensions;
namespace ClientCore
{
/// <summary>
/// Contains various static variables and constants that the client uses for operation.
/// </summary>
public static class ProgramConstants
{
public static readonly string StartupExecutable = Assembly.GetEntryAssembly().Location;
public static readonly string StartupPath = SafePath.CombineDirectoryPath(new FileInfo(StartupExecutable).Directory.FullName);
public static readonly string GamePath = SafePath.CombineDirectoryPath(GetGamePath(StartupPath));
public static string ClientUserFilesPath => SafePath.CombineDirectoryPath(GamePath, "Client");
public static event EventHandler PlayerNameChanged;
public const string QRES_EXECUTABLE = "qres.dat";
public const string CNCNET_PROTOCOL_REVISION = "A";
public const string LAN_PROTOCOL_REVISION = "RL8";
public const int LAN_PORT = 1234;
public const int LAN_INGAME_PORT = 1234;
public const int LAN_LOBBY_PORT = 1232;
public const int LAN_GAME_LOBBY_PORT = 1233;
public const char LAN_DATA_SEPARATOR = (char)01;
public const char LAN_MESSAGE_SEPARATOR = (char)02;
public const string SPAWNMAP_INI = "spawnmap.ini";
public const string SPAWNER_SETTINGS = "spawn.ini";
public const string SAVED_GAME_SPAWN_INI = "Saved Games/spawnSG.ini";
/// <summary>
/// The locale code that corresponds to the language the hardcoded client strings are in.
/// </summary>
public const string HARDCODED_LOCALE_CODE = "en";
/// <summary>
/// Used to denote <see cref="Environment.NewLine"/> in the INI files.
/// </summary>
/// <remarks>
/// Historically Westwood used '@' for this purpose, so we keep it for compatibility.
/// </remarks>
public const string INI_NEWLINE_PATTERN = "@";
public const int GAME_ID_MAX_LENGTH = 4;
public static readonly Encoding LAN_ENCODING = Encoding.UTF8;
#if NETFRAMEWORK
private static bool? isMono;
/// <summary>
/// Gets a value whether or not the application is running under Mono. Uses lazy loading and caching.
/// </summary>
public static bool ISMONO => isMono ??= Type.GetType("Mono.Runtime") != null;
#endif
public static string GAME_VERSION = "Undefined";
private static string PlayerName = "No name";
public static string PLAYERNAME
{
get { return PlayerName; }
set
{
string oldPlayerName = PlayerName;
PlayerName = value;
if (oldPlayerName != PlayerName)
PlayerNameChanged?.Invoke(null, EventArgs.Empty);
}
}
public static string BASE_RESOURCE_PATH = "Resources";
public static string RESOURCES_DIR = BASE_RESOURCE_PATH;
public static int LOG_LEVEL = 1;
public static bool IsInGame { get; set; }
public static string GetResourcePath()
{
return SafePath.CombineDirectoryPath(GamePath, RESOURCES_DIR);
}
public static string GetBaseResourcePath()
{
return SafePath.CombineDirectoryPath(GamePath, BASE_RESOURCE_PATH);
}
public const string GAME_INVITE_CTCP_COMMAND = "INVITE";
public const string GAME_INVITATION_FAILED_CTCP_COMMAND = "INVITATION_FAILED";
public static string GetAILevelName(int aiLevel)
{
if (aiLevel > -1 && aiLevel < AI_PLAYER_NAMES.Count)
return AI_PLAYER_NAMES[aiLevel];
return "";
}
public static readonly List<string> TEAMS = new List<string> { "A", "B", "C", "D" };
// Static fields might be initialized before the translation file is loaded. Change to readonly properties here.
public static List<string> AI_PLAYER_NAMES => new List<string> { "Easy AI".L10N("Client:Main:EasyAIName"), "Medium AI".L10N("Client:Main:MediumAIName"), "Hard AI".L10N("Client:Main:HardAIName") };
public static string LogFileName { get; set; }
/// <summary>
/// This method finds the "Resources" directory by traversing the directory tree upwards from the startup path.
/// </summary>
/// <remarks>
/// This method is needed by both ClientCore and DXMainClient. However, since it is usually called at the very beginning,
/// where DXMainClient could not refer to ClientCore, this method is copied to both projects.
/// Remember to keep <see cref="ClientCore.ProgramConstants.SearchResourcesDir"/> and <see cref="DTAClient.Program.SearchResourcesDir"/> consistent if you have modified its source codes.
/// </remarks>
private static string SearchResourcesDir(string startupPath)
{
DirectoryInfo currentDir = new(startupPath);
for (int i = 0; i < 3; i++)
{
// Determine if currentDir is the "Resources" folder
if (currentDir.Name.ToLowerInvariant() == "Resources".ToLowerInvariant())
return currentDir.FullName;
// Additional check. This makes developers to debug the client inside Visual Studio a little bit easier.
DirectoryInfo resourcesDir = currentDir.GetDirectories("Resources", SearchOption.TopDirectoryOnly).FirstOrDefault();
if (resourcesDir is not null)
return resourcesDir.FullName;
currentDir = currentDir.Parent;
}
throw new Exception("Could not find Resources directory.");
}
private static string GetGamePath(string startupPath)
{
string resourceDir = SearchResourcesDir(startupPath);
return new DirectoryInfo(resourceDir).Parent.FullName;
}
}
}